libkdepim

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 QObject
00040 {
00041   Q_OBJECT
00042   friend class ProgressManager;
00043   friend class TQDict< ProgressItem >; // so it can be deleted from dicts
00044 
00045   public:
00046 
00051     const TQString& id() const { return mId; }
00052 
00056     ProgressItem *parent() const { return mParent; }
00057 
00061     const TQString& label() const { return mLabel; }
00062 
00067     void setLabel( const TQString& v );
00068 
00072     const TQString& status() const { return mStatus; }
00078     void setStatus( const TQString& v );
00079 
00083     bool canBeCanceled() const { return mCanBeCanceled; }
00084 
00089     bool usesCrypto() const { return mUsesCrypto; }
00090 
00096     void setUsesCrypto( bool v );
00097 
00101     bool usesBusyIndicator() const { return mUsesBusyIndicator; }
00102 
00108     void setUsesBusyIndicator( bool useBusyIndicator );
00109 
00113     unsigned int progress() const { return mProgress; }
00114 
00119     void setProgress( unsigned int v );
00120 
00128     void setComplete();
00129 
00134     void reset() { setProgress( 0 ); setStatus( TQString::null ); mCompleted = 0; }
00135 
00136     void cancel();
00137 
00138     // Often needed values for calculating progress.
00139     void setTotalItems( unsigned int v ) { mTotal = v; }
00140     unsigned int totalItems() const { return mTotal; }
00141     void setCompletedItems( unsigned int v ) { mCompleted = v; }
00142     void incCompletedItems( unsigned int v = 1 ) { mCompleted += v; }
00143     unsigned int completedItems() const { return mCompleted; }
00144 
00148     void updateProgress() { setProgress( mTotal? mCompleted*100/mTotal: 0 ); }
00149 
00150     void addChild( ProgressItem *kiddo );
00151     void removeChild( ProgressItem *kiddo );
00152 
00153     bool canceled() const { return mCanceled; }
00154 
00155 signals:
00160     void progressItemAdded( KPIM::ProgressItem* );
00166     void progressItemProgress( KPIM::ProgressItem*, unsigned int );
00173     void progressItemCompleted( KPIM::ProgressItem* );
00184     void progressItemCanceled( KPIM::ProgressItem* );
00191     void progressItemStatus( KPIM::ProgressItem*, const TQString& );
00198     void progressItemLabel( KPIM::ProgressItem*, const TQString& );
00205     void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
00206 
00214     void progressItemUsesBusyIndicator( KPIM::ProgressItem *item, bool value );
00215 
00216 
00217   protected:
00218     /* Only to be used by our good friend the ProgressManager */
00219     ProgressItem( ProgressItem* parent,
00220                              const TQString& id,
00221                              const TQString& label,
00222                              const TQString& status,
00223                              bool isCancellable,
00224                              bool usesCrypto );
00225     virtual ~ProgressItem();
00226 
00227 
00228   private:
00229     TQString mId;
00230     TQString mLabel;
00231     TQString mStatus;
00232     ProgressItem* mParent;
00233     bool mCanBeCanceled;
00234     unsigned int mProgress;
00235     ProgressItemMap mChildren;
00236     unsigned int mTotal;
00237     unsigned int mCompleted;
00238     bool mWaitingForKids;
00239     bool mCanceled;
00240     bool mUsesCrypto;
00241     bool mUsesBusyIndicator;
00242 };
00243 
00265 class KDE_EXPORT ProgressManager : public QObject
00266 {
00267 
00268   Q_OBJECT
00269 
00270   public:
00271     virtual ~ProgressManager();
00272 
00276     static ProgressManager * instance();
00277 
00284     static TQString getUniqueID() { return TQString::number( ++uID ); }
00285 
00294      static ProgressItem * createProgressItem( const TQString &label ) {
00295        return instance()->createProgressItemImpl( 0, getUniqueID(), label,
00296                                                   TQString::null, true, false );
00297      }
00298 
00315      static ProgressItem * createProgressItem( ProgressItem* parent,
00316                                                const TQString& id,
00317                                                const TQString& label,
00318                                                const TQString& status = TQString::null,
00319                                                bool canBeCanceled = true,
00320                                                bool usesCrypto = false ) {
00321        return instance()->createProgressItemImpl( parent, id, label, status,
00322                                                   canBeCanceled, usesCrypto );
00323      }
00324 
00329      static ProgressItem * createProgressItem( const TQString& parent,
00330                                                const TQString& id,
00331                                                const TQString& label,
00332                                                const TQString& status = TQString::null,
00333                                                bool canBeCanceled = true,
00334                                                bool usesCrypto = false ) {
00335        return instance()->createProgressItemImpl( parent, id, label,
00336                                                  status, canBeCanceled, usesCrypto );
00337      }
00338 
00342      static ProgressItem * createProgressItem( const TQString& id,
00343                                                const TQString& label,
00344                                                const TQString& status = TQString::null,
00345                                                bool canBeCanceled = true,
00346                                                bool usesCrypto = false ) {
00347        return instance()->createProgressItemImpl( 0, id, label, status,
00348                                                   canBeCanceled, usesCrypto );
00349      }
00350 
00351 
00355     bool isEmpty() const { return mTransactions.isEmpty(); }
00356 
00364     ProgressItem* singleItem() const;
00365 
00370     static void emitShowProgressDialog() {
00371        instance()->emitShowProgressDialogImpl();
00372     }
00373 
00374   signals:
00376     void progressItemAdded( KPIM::ProgressItem* );
00378     void progressItemProgress( KPIM::ProgressItem*, unsigned int );
00380     void progressItemCompleted( KPIM::ProgressItem* );
00382     void progressItemCanceled( KPIM::ProgressItem* );
00384     void progressItemStatus( KPIM::ProgressItem*, const TQString& );
00386     void progressItemLabel( KPIM::ProgressItem*, const TQString& );
00388     void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
00390     void progressItemUsesBusyIndicator( KPIM::ProgressItem*, bool );
00391 
00396     void showProgressDialog();
00397   public slots:
00398 
00404     void slotStandardCancelHandler( KPIM::ProgressItem* item );
00405 
00409     void slotAbortAll();
00410 
00411   private slots:
00412     void slotTransactionCompleted( KPIM::ProgressItem *item );
00413 
00414   private:
00415     ProgressManager();
00416      // prevent unsolicited copies
00417     ProgressManager( const ProgressManager& );
00418 
00419     virtual ProgressItem* createProgressItemImpl(
00420                 ProgressItem* parent, const TQString& id,
00421                 const TQString& label, const TQString& status,
00422                 bool cancellable, bool usesCrypto );
00423     virtual ProgressItem* createProgressItemImpl(
00424                 const TQString& parent,  const TQString& id,
00425                 const TQString& label, const TQString& status,
00426                 bool cancellable, bool usesCrypto );
00427     void emitShowProgressDialogImpl();
00428 
00429     TQDict< ProgressItem > mTransactions;
00430     static ProgressManager *mInstance;
00431     static unsigned int uID;
00432 };
00433 
00434 }
00435 
00436 #endif // __KPIM_PROGRESSMANAGER_H__