4 This file is part of libkdepim.
6 Copyright (c) 2004 Till Adam <adam@kde.org>
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
24 #include "progressmanager.h"
32 unsigned int KPIM::ProgressManager::uID
= 42;
34 ProgressItem::ProgressItem( ProgressItem
*parent
, const QString
&id
,
35 const QString
&label
, const QString
&status
,
36 bool canBeCanceled
, bool usesCrypto
)
37 : mId( id
), mLabel( label
), mStatus( status
), mParent( parent
),
38 mCanBeCanceled( canBeCanceled
), mProgress( 0 ), mTotal( 0 ),
39 mCompleted( 0 ), mWaitingForKids( false ), mCanceled( false ),
40 mUsesCrypto( usesCrypto
), mUsesBusyIndicator( false )
44 ProgressItem::~ProgressItem()
48 void ProgressItem::setComplete()
50 // kDebug() << label();
52 if ( mChildren
.isEmpty() ) {
56 emit
progressItemCompleted( this );
58 parent()->removeChild( this );
62 mWaitingForKids
= true;
66 void ProgressItem::addChild( ProgressItem
*kiddo
)
68 mChildren
.insert( kiddo
, true );
71 void ProgressItem::removeChild( ProgressItem
*kiddo
)
73 mChildren
.remove( kiddo
);
74 // in case we were waiting for the last kid to go away, now is the time
75 if ( mChildren
.count() == 0 && mWaitingForKids
) {
76 emit
progressItemCompleted( this );
81 void ProgressItem::cancel()
83 if ( mCanceled
|| !mCanBeCanceled
) {
89 // Cancel all children.
90 QList
<ProgressItem
*> kids
= mChildren
.keys();
91 QList
<ProgressItem
*>::Iterator
it( kids
.begin() );
92 QList
<ProgressItem
*>::Iterator
end( kids
.end() );
93 for ( ; it
!= end
; it
++ ) {
94 ProgressItem
*kid
= *it
;
95 if ( kid
->canBeCanceled() ) {
99 setStatus( i18n( "Aborting..." ) );
100 emit
progressItemCanceled( this );
103 void ProgressItem::setProgress( unsigned int v
)
106 // kDebug() << label() << " :" << v;
107 emit
progressItemProgress( this, mProgress
);
110 void ProgressItem::setLabel( const QString
&v
)
113 emit
progressItemLabel( this, mLabel
);
116 void ProgressItem::setStatus( const QString
&v
)
119 emit
progressItemStatus( this, mStatus
);
122 void ProgressItem::setUsesCrypto( bool v
)
125 emit
progressItemUsesCrypto( this, v
);
128 void ProgressItem::setUsesBusyIndicator( bool useBusyIndicator
)
130 mUsesBusyIndicator
= useBusyIndicator
;
131 emit
progressItemUsesBusyIndicator( this, useBusyIndicator
);
134 // ======================================
136 struct ProgressManagerPrivate
{
137 ProgressManager instance
;
140 K_GLOBAL_STATIC( ProgressManagerPrivate
, progressManagerPrivate
)
142 ProgressManager::ProgressManager()
148 ProgressManager::~ProgressManager() {}
150 ProgressManager
*ProgressManager::instance()
152 return progressManagerPrivate
.isDestroyed() ? 0 : &progressManagerPrivate
->instance
;
155 ProgressItem
*ProgressManager::createProgressItemImpl( ProgressItem
*parent
,
157 const QString
&label
,
158 const QString
&status
,
163 if ( !mTransactions
.value( id
) ) {
164 t
= new ProgressItem ( parent
, id
, label
, status
, cancellable
, usesCrypto
);
165 mTransactions
.insert( id
, t
);
167 ProgressItem
*p
= mTransactions
.value( parent
->id() );
172 // connect all signals
173 connect ( t
, SIGNAL( progressItemCompleted(KPIM::ProgressItem
*) ),
174 this, SLOT( slotTransactionCompleted(KPIM::ProgressItem
*) ) );
175 connect ( t
, SIGNAL( progressItemProgress(KPIM::ProgressItem
*, unsigned int) ),
176 this, SIGNAL( progressItemProgress(KPIM::ProgressItem
*, unsigned int) ) );
177 connect ( t
, SIGNAL( progressItemAdded(KPIM::ProgressItem
*) ),
178 this, SIGNAL( progressItemAdded(KPIM::ProgressItem
*) ) );
179 connect ( t
, SIGNAL( progressItemCanceled(KPIM::ProgressItem
*) ),
180 this, SIGNAL( progressItemCanceled(KPIM::ProgressItem
*) ) );
181 connect ( t
, SIGNAL( progressItemStatus(KPIM::ProgressItem
*, const QString
&) ),
182 this, SIGNAL( progressItemStatus(KPIM::ProgressItem
*, const QString
&) ) );
183 connect ( t
, SIGNAL( progressItemLabel(KPIM::ProgressItem
*, const QString
&) ),
184 this, SIGNAL( progressItemLabel(KPIM::ProgressItem
*, const QString
&) ) );
185 connect ( t
, SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem
*, bool) ),
186 this, SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem
*, bool) ) );
187 connect ( t
, SIGNAL( progressItemUsesBusyIndicator(KPIM::ProgressItem
*, bool) ),
188 this, SIGNAL( progressItemUsesBusyIndicator(KPIM::ProgressItem
*, bool) ) );
190 emit
progressItemAdded( t
);
192 // Hm, is this what makes the most sense?
193 t
= mTransactions
.value( id
);
198 ProgressItem
*ProgressManager::createProgressItemImpl( const QString
&parent
,
200 const QString
&label
,
201 const QString
&status
,
205 ProgressItem
*p
= mTransactions
.value( parent
);
206 return createProgressItemImpl( p
, id
, label
, status
, canBeCanceled
, usesCrypto
);
209 void ProgressManager::emitShowProgressDialogImpl()
211 emit
showProgressDialog();
216 void ProgressManager::slotTransactionCompleted( ProgressItem
*item
)
218 mTransactions
.remove( item
->id() );
219 emit
progressItemCompleted( item
);
222 void ProgressManager::slotStandardCancelHandler( ProgressItem
*item
)
227 ProgressItem
*ProgressManager::singleItem() const
229 ProgressItem
*item
= 0;
230 QHash
< QString
, ProgressItem
* >::const_iterator it
= mTransactions
.constBegin();
231 while ( it
!= mTransactions
.constEnd() ) {
233 // No single item for progress possible, as one of them is a busy indicator one.
234 if ( (*it
)->usesBusyIndicator() )
237 if ( !(*it
)->parent() ) { // if it's a top level one, only those count
239 return 0; // we found more than one
249 void ProgressManager::slotAbortAll()
251 QHashIterator
<QString
, ProgressItem
*> it(mTransactions
);
252 while (it
.hasNext()) {
254 it
.value()->cancel();
261 #include "progressmanager.moc"