one entry for korganizer in khelpcenters navigation tree is enough
[kdepim.git] / libkdepim / progressmanager.cpp
blob0e38446f5590fb25a73b97329ff0ad5d906755ca
1 /*
2 progressmanager.cpp
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"
26 #include <KDebug>
27 #include <KLocale>
28 #include <KGlobal>
30 namespace KPIM {
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() ) {
53 if ( !mCanceled ) {
54 setProgress( 100 );
56 emit progressItemCompleted( this );
57 if ( parent() ) {
58 parent()->removeChild( this );
60 deleteLater();
61 } else {
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 );
77 deleteLater();
81 void ProgressItem::cancel()
83 if ( mCanceled || !mCanBeCanceled ) {
84 return;
87 kDebug() << label();
88 mCanceled = true;
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() ) {
96 kid->cancel();
99 setStatus( i18n( "Aborting..." ) );
100 emit progressItemCanceled( this );
103 void ProgressItem::setProgress( unsigned int v )
105 mProgress = v;
106 // kDebug() << label() << " :" << v;
107 emit progressItemProgress( this, mProgress );
110 void ProgressItem::setLabel( const QString &v )
112 mLabel = v;
113 emit progressItemLabel( this, mLabel );
116 void ProgressItem::setStatus( const QString &v )
118 mStatus = v;
119 emit progressItemStatus( this, mStatus );
122 void ProgressItem::setUsesCrypto( bool v )
124 mUsesCrypto = 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()
143 : QObject()
148 ProgressManager::~ProgressManager() {}
150 ProgressManager *ProgressManager::instance()
152 return progressManagerPrivate.isDestroyed() ? 0 : &progressManagerPrivate->instance ;
155 ProgressItem *ProgressManager::createProgressItemImpl( ProgressItem *parent,
156 const QString &id,
157 const QString &label,
158 const QString &status,
159 bool cancellable,
160 bool usesCrypto )
162 ProgressItem *t = 0;
163 if ( !mTransactions.value( id ) ) {
164 t = new ProgressItem ( parent, id, label, status, cancellable, usesCrypto );
165 mTransactions.insert( id, t );
166 if ( parent ) {
167 ProgressItem *p = mTransactions.value( parent->id() );
168 if ( p ) {
169 p->addChild( t );
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 );
191 } else {
192 // Hm, is this what makes the most sense?
193 t = mTransactions.value( id );
195 return t;
198 ProgressItem *ProgressManager::createProgressItemImpl( const QString &parent,
199 const QString &id,
200 const QString &label,
201 const QString &status,
202 bool canBeCanceled,
203 bool usesCrypto )
205 ProgressItem *p = mTransactions.value( parent );
206 return createProgressItemImpl( p, id, label, status, canBeCanceled, usesCrypto );
209 void ProgressManager::emitShowProgressDialogImpl()
211 emit showProgressDialog();
214 // slots
216 void ProgressManager::slotTransactionCompleted( ProgressItem *item )
218 mTransactions.remove( item->id() );
219 emit progressItemCompleted( item );
222 void ProgressManager::slotStandardCancelHandler( ProgressItem *item )
224 item->setComplete();
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() )
235 return 0;
237 if ( !(*it)->parent() ) { // if it's a top level one, only those count
238 if ( item ) {
239 return 0; // we found more than one
240 } else {
241 item = (*it);
244 ++it;
246 return item;
249 void ProgressManager::slotAbortAll()
251 QHashIterator<QString, ProgressItem *> it(mTransactions);
252 while (it.hasNext()) {
253 it.next();
254 it.value()->cancel();
259 } // namespace
261 #include "progressmanager.moc"