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 <qobject.h>
00028 #include <qdict.h>
00029 #include <qstring.h>
00030 
00031 #include <kdepimmacros.h>
00032 
00033 namespace KPIM {
00034 
00035 class ProgressItem;
00036 class ProgressManager;
00037 typedef QMap<ProgressItem*, bool> ProgressItemMap;
00038 
00039 class KDE_EXPORT ProgressItem : public QObject
00040 {
00041   Q_OBJECT
00042   friend class ProgressManager;
00043   friend class QDict< ProgressItem >; // so it can be deleted from dicts
00044 
00045   public:
00046 
00051     const QString& id() const { return mId; }
00052 
00056     ProgressItem *parent() const { return mParent; }
00057 
00061     const QString& label() const { return mLabel; }
00062 
00067     void setLabel( const QString& v );
00068 
00072     const QString& status() const { return mStatus; }
00078     void setStatus( const QString& v );
00079 
00083     bool canBeCanceled() const { return mCanBeCanceled; }
00084 
00089     bool usesCrypto() const { return mUsesCrypto; }
00090 
00096     void setUsesCrypto( bool v );
00097 
00101     unsigned int progress() const { return mProgress; }
00102 
00107     void setProgress( unsigned int v );
00108 
00116     void setComplete();
00117 
00122     void reset() { setProgress( 0 ); setStatus( QString::null ); mCompleted = 0; }
00123 
00124     void cancel();
00125 
00126     // Often needed values for calculating progress.
00127     void setTotalItems( unsigned int v ) { mTotal = v; }
00128     unsigned int totalItems() const { return mTotal; }
00129     void setCompletedItems( unsigned int v ) { mCompleted = v; }
00130     void incCompletedItems( unsigned int v = 1 ) { mCompleted += v; }
00131     unsigned int completedItems() const { return mCompleted; }
00132 
00136     void updateProgress() { setProgress( mTotal? mCompleted*100/mTotal: 0 ); };
00137 
00138     void addChild( ProgressItem *kiddo );
00139     void removeChild( ProgressItem *kiddo );
00140 
00141     bool canceled() const { return mCanceled; }
00142 
00143 signals:
00148     void progressItemAdded( KPIM::ProgressItem* );
00154     void progressItemProgress( KPIM::ProgressItem*, unsigned int );
00161     void progressItemCompleted( KPIM::ProgressItem* );
00172     void progressItemCanceled( KPIM::ProgressItem* );
00179     void progressItemStatus( KPIM::ProgressItem*, const QString& );
00186     void progressItemLabel( KPIM::ProgressItem*, const QString& );
00193     void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
00194 
00195 
00196   protected:
00197     /* Only to be used by our good friend the ProgressManager */
00198     ProgressItem( ProgressItem* parent,
00199                              const QString& id,
00200                              const QString& label,
00201                              const QString& status,
00202                              bool isCancellable,
00203                              bool usesCrypto );
00204     virtual ~ProgressItem();
00205 
00206 
00207   private:
00208     QString mId;
00209     QString mLabel;
00210     QString mStatus;
00211     ProgressItem* mParent;
00212     bool mCanBeCanceled;
00213     unsigned int mProgress;
00214     ProgressItemMap mChildren;
00215     unsigned int mTotal;
00216     unsigned int mCompleted;
00217     bool mWaitingForKids;
00218     bool mCanceled;
00219     bool mUsesCrypto;
00220 };
00221 
00243 class KDE_EXPORT ProgressManager : public QObject
00244 {
00245 
00246   Q_OBJECT
00247 
00248   public:
00249     virtual ~ProgressManager();
00250 
00254     static ProgressManager * instance();
00255 
00262     static QString getUniqueID() { return QString::number( ++uID ); };
00263 
00272      static ProgressItem * createProgressItem( const QString &label ) {
00273        return instance()->createProgressItemImpl( 0, getUniqueID(), label,
00274                                                   QString::null, true, false );
00275      }
00276 
00293      static ProgressItem * createProgressItem( ProgressItem* parent,
00294                                                const QString& id,
00295                                                const QString& label,
00296                                                const QString& status = QString::null,
00297                                                bool canBeCanceled = true,
00298                                                bool usesCrypto = false ) {
00299        return instance()->createProgressItemImpl( parent, id, label, status,
00300                                                   canBeCanceled, usesCrypto );
00301      }
00302 
00307      static ProgressItem * createProgressItem( const QString& parent,
00308                                                const QString& id,
00309                                                const QString& label,
00310                                                const QString& status = QString::null,
00311                                                bool canBeCanceled = true,
00312                                                bool usesCrypto = false ) {
00313        return instance()->createProgressItemImpl( parent, id, label,
00314                                                  status, canBeCanceled, usesCrypto );
00315      }
00316 
00320      static ProgressItem * createProgressItem( const QString& id,
00321                                                const QString& label,
00322                                                const QString& status = QString::null,
00323                                                bool canBeCanceled = true,
00324                                                bool usesCrypto = false ) {
00325        return instance()->createProgressItemImpl( 0, id, label, status,
00326                                                   canBeCanceled, usesCrypto );
00327      }
00328 
00329 
00333     bool isEmpty() const { return mTransactions.isEmpty(); }
00334 
00339     ProgressItem* singleItem() const;
00340 
00345     static void emitShowProgressDialog() {
00346        instance()->emitShowProgressDialogImpl();
00347     }
00348 
00349   signals:
00351     void progressItemAdded( KPIM::ProgressItem* );
00353     void progressItemProgress( KPIM::ProgressItem*, unsigned int );
00355     void progressItemCompleted( KPIM::ProgressItem* );
00357     void progressItemCanceled( KPIM::ProgressItem* );
00359     void progressItemStatus( KPIM::ProgressItem*, const QString& );
00361     void progressItemLabel( KPIM::ProgressItem*, const QString& );
00363     void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
00364 
00369     void showProgressDialog();
00370   public slots:
00371 
00377     void slotStandardCancelHandler( KPIM::ProgressItem* item );
00378 
00382     void slotAbortAll();
00383 
00384   private slots:
00385     void slotTransactionCompleted( KPIM::ProgressItem *item );
00386 
00387   private:
00388     ProgressManager();
00389      // prevent unsolicited copies
00390     ProgressManager( const ProgressManager& );
00391 
00392     virtual ProgressItem* createProgressItemImpl(
00393                 ProgressItem* parent, const QString& id,
00394                 const QString& label, const QString& status,
00395                 bool cancellable, bool usesCrypto );
00396     virtual ProgressItem* createProgressItemImpl(
00397                 const QString& parent,  const QString& id,
00398                 const QString& label, const QString& status,
00399                 bool cancellable, bool usesCrypto );
00400     void emitShowProgressDialogImpl();
00401 
00402     QDict< ProgressItem > mTransactions;
00403     static ProgressManager *mInstance;
00404     static unsigned int uID;
00405 };
00406 
00407 }
00408 
00409 #endif // __KPIM_PROGRESSMANAGER_H__
KDE Home | KDE Accessibility Home | Description of Access Keys