progressmanager.h
00001 /* 00002 progressmanager.h 00003 00004 This file is part of KDEPIM. 00005 00006 Author: Till Adam <adam@kde.org> (C) 2004 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #ifndef __KPIM_PROGRESSMANAGER_H__ 00025 #define __KPIM_PROGRESSMANAGER_H__ 00026 00027 #include <tqobject.h> 00028 #include <tqdict.h> 00029 #include <tqstring.h> 00030 00031 #include <kdepimmacros.h> 00032 00033 namespace KPIM { 00034 00035 class ProgressItem; 00036 class ProgressManager; 00037 typedef TQMap<ProgressItem*, bool> ProgressItemMap; 00038 00039 class KDE_EXPORT ProgressItem : public TQObject 00040 { 00041 Q_OBJECT 00042 TQ_OBJECT 00043 friend class ProgressManager; 00044 friend class TQDict< ProgressItem >; // so it can be deleted from dicts 00045 00046 public: 00047 00052 const TQString& id() const { return mId; } 00053 00057 ProgressItem *parent() const { return mParent; } 00058 00062 const TQString& label() const { return mLabel; } 00063 00068 void setLabel( const TQString& v ); 00069 00073 const TQString& status() const { return mStatus; } 00079 void setStatus( const TQString& v ); 00080 00084 bool canBeCanceled() const { return mCanBeCanceled; } 00085 00090 bool usesCrypto() const { return mUsesCrypto; } 00091 00097 void setUsesCrypto( bool v ); 00098 00102 bool usesBusyIndicator() const { return mUsesBusyIndicator; } 00103 00109 void setUsesBusyIndicator( bool useBusyIndicator ); 00110 00114 unsigned int progress() const { return mProgress; } 00115 00120 void setProgress( unsigned int v ); 00121 00129 void setComplete(); 00130 00135 void reset() { setProgress( 0 ); setStatus( TQString() ); mCompleted = 0; } 00136 00137 void cancel(); 00138 00139 // Often needed values for calculating progress. 00140 void setTotalItems( unsigned int v ) { mTotal = v; } 00141 unsigned int totalItems() const { return mTotal; } 00142 void setCompletedItems( unsigned int v ) { mCompleted = v; } 00143 void incCompletedItems( unsigned int v = 1 ) { mCompleted += v; } 00144 unsigned int completedItems() const { return mCompleted; } 00145 00149 void updateProgress() { setProgress( mTotal? mCompleted*100/mTotal: 0 ); } 00150 00151 void addChild( ProgressItem *kiddo ); 00152 void removeChild( ProgressItem *kiddo ); 00153 00154 bool canceled() const { return mCanceled; } 00155 00156 signals: 00161 void progressItemAdded( KPIM::ProgressItem* ); 00167 void progressItemProgress( KPIM::ProgressItem*, unsigned int ); 00174 void progressItemCompleted( KPIM::ProgressItem* ); 00185 void progressItemCanceled( KPIM::ProgressItem* ); 00192 void progressItemStatus( KPIM::ProgressItem*, const TQString& ); 00199 void progressItemLabel( KPIM::ProgressItem*, const TQString& ); 00206 void progressItemUsesCrypto( KPIM::ProgressItem*, bool ); 00207 00215 void progressItemUsesBusyIndicator( KPIM::ProgressItem *item, bool value ); 00216 00217 00218 protected: 00219 /* Only to be used by our good friend the ProgressManager */ 00220 ProgressItem( ProgressItem* parent, 00221 const TQString& id, 00222 const TQString& label, 00223 const TQString& status, 00224 bool isCancellable, 00225 bool usesCrypto ); 00226 virtual ~ProgressItem(); 00227 00228 00229 private: 00230 TQString mId; 00231 TQString mLabel; 00232 TQString mStatus; 00233 ProgressItem* mParent; 00234 bool mCanBeCanceled; 00235 unsigned int mProgress; 00236 ProgressItemMap mChildren; 00237 unsigned int mTotal; 00238 unsigned int mCompleted; 00239 bool mWaitingForKids; 00240 bool mCanceled; 00241 bool mUsesCrypto; 00242 bool mUsesBusyIndicator; 00243 }; 00244 00266 class KDE_EXPORT ProgressManager : public TQObject 00267 { 00268 00269 Q_OBJECT 00270 TQ_OBJECT 00271 00272 public: 00273 virtual ~ProgressManager(); 00274 00278 static ProgressManager * instance(); 00279 00286 static TQString getUniqueID() { return TQString::number( ++uID ); } 00287 00296 static ProgressItem * createProgressItem( const TQString &label ) { 00297 return instance()->createProgressItemImpl( 0, getUniqueID(), label, 00298 TQString(), true, false ); 00299 } 00300 00317 static ProgressItem * createProgressItem( ProgressItem* parent, 00318 const TQString& id, 00319 const TQString& label, 00320 const TQString& status = TQString(), 00321 bool canBeCanceled = true, 00322 bool usesCrypto = false ) { 00323 return instance()->createProgressItemImpl( parent, id, label, status, 00324 canBeCanceled, usesCrypto ); 00325 } 00326 00331 static ProgressItem * createProgressItem( const TQString& parent, 00332 const TQString& id, 00333 const TQString& label, 00334 const TQString& status = TQString(), 00335 bool canBeCanceled = true, 00336 bool usesCrypto = false ) { 00337 return instance()->createProgressItemImpl( parent, id, label, 00338 status, canBeCanceled, usesCrypto ); 00339 } 00340 00344 static ProgressItem * createProgressItem( const TQString& id, 00345 const TQString& label, 00346 const TQString& status = TQString(), 00347 bool canBeCanceled = true, 00348 bool usesCrypto = false ) { 00349 return instance()->createProgressItemImpl( 0, id, label, status, 00350 canBeCanceled, usesCrypto ); 00351 } 00352 00353 00357 bool isEmpty() const { return mTransactions.isEmpty(); } 00358 00366 ProgressItem* singleItem() const; 00367 00372 static void emitShowProgressDialog() { 00373 instance()->emitShowProgressDialogImpl(); 00374 } 00375 00376 signals: 00378 void progressItemAdded( KPIM::ProgressItem* ); 00380 void progressItemProgress( KPIM::ProgressItem*, unsigned int ); 00382 void progressItemCompleted( KPIM::ProgressItem* ); 00384 void progressItemCanceled( KPIM::ProgressItem* ); 00386 void progressItemStatus( KPIM::ProgressItem*, const TQString& ); 00388 void progressItemLabel( KPIM::ProgressItem*, const TQString& ); 00390 void progressItemUsesCrypto( KPIM::ProgressItem*, bool ); 00392 void progressItemUsesBusyIndicator( KPIM::ProgressItem*, bool ); 00393 00398 void showProgressDialog(); 00399 public slots: 00400 00406 void slotStandardCancelHandler( KPIM::ProgressItem* item ); 00407 00411 void slotAbortAll(); 00412 00413 private slots: 00414 void slotTransactionCompleted( KPIM::ProgressItem *item ); 00415 00416 private: 00417 ProgressManager(); 00418 // prevent unsolicited copies 00419 ProgressManager( const ProgressManager& ); 00420 00421 virtual ProgressItem* createProgressItemImpl( 00422 ProgressItem* parent, const TQString& id, 00423 const TQString& label, const TQString& status, 00424 bool cancellable, bool usesCrypto ); 00425 virtual ProgressItem* createProgressItemImpl( 00426 const TQString& parent, const TQString& id, 00427 const TQString& label, const TQString& status, 00428 bool cancellable, bool usesCrypto ); 00429 void emitShowProgressDialogImpl(); 00430 00431 TQDict< ProgressItem > mTransactions; 00432 static ProgressManager *mInstance; 00433 static unsigned int uID; 00434 }; 00435 00436 } 00437 00438 #endif // __KPIM_PROGRESSMANAGER_H__