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 
00066     void setLabel( const QString& v );
00067 
00071     const QString& status() const { return mStatus; }
00076     void setStatus( const QString& v );
00077 
00081     bool canBeCanceled() const { return mCanBeCanceled; }
00082 
00087     bool usesCrypto() const { return mUsesCrypto; }
00088 
00094     void setUsesCrypto( bool v );
00095 
00099     unsigned int progress() const { return mProgress; }
00100 
00105     void setProgress( unsigned int v );
00106 
00114     void setComplete();
00115 
00120     void reset() { setProgress( 0 ); setStatus( QString::null ); mCompleted = 0; }
00121 
00122     void cancel();
00123 
00124     // Often needed values for calculating progress.
00125     void setTotalItems( unsigned int v ) { mTotal = v; }
00126     unsigned int totalItems() const { return mTotal; }
00127     void setCompletedItems( unsigned int v ) { mCompleted = v; }
00128     void incCompletedItems( unsigned int v = 1 ) { mCompleted += v; }
00129     unsigned int completedItems() const { return mCompleted; }
00130 
00134     void updateProgress() { setProgress( mTotal? mCompleted*100/mTotal: 0 ); };
00135 
00136     void addChild( ProgressItem *kiddo );
00137     void removeChild( ProgressItem *kiddo );
00138 
00139     bool canceled() const { return mCanceled; }
00140 
00141 signals:
00146     void progressItemAdded( KPIM::ProgressItem* );
00152     void progressItemProgress( KPIM::ProgressItem*, unsigned int );
00159     void progressItemCompleted( KPIM::ProgressItem* );
00170     void progressItemCanceled( KPIM::ProgressItem* );
00177     void progressItemStatus( KPIM::ProgressItem*, const QString& );
00184     void progressItemLabel( KPIM::ProgressItem*, const QString& );
00191     void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
00192 
00193 
00194   protected:
00195     /* Only to be used by our good friend the ProgressManager */
00196     ProgressItem( ProgressItem* parent,
00197                              const QString& id,
00198                              const QString& label,
00199                              const QString& status,
00200                              bool isCancellable,
00201                              bool usesCrypto );
00202     virtual ~ProgressItem();
00203 
00204 
00205   private:
00206     QString mId;
00207     QString mLabel;
00208     QString mStatus;
00209     ProgressItem* mParent;
00210     bool mCanBeCanceled;
00211     unsigned int mProgress;
00212     ProgressItemMap mChildren;
00213     unsigned int mTotal;
00214     unsigned int mCompleted;
00215     bool mWaitingForKids;
00216     bool mCanceled;
00217     bool mUsesCrypto;
00218 };
00219 
00241 class KDE_EXPORT ProgressManager : public QObject
00242 {
00243 
00244   Q_OBJECT
00245 
00246   public:
00247     virtual ~ProgressManager();
00248 
00252     static ProgressManager * instance();
00253 
00260     static QString getUniqueID() { return QString::number( ++uID ); };
00261 
00267      static ProgressItem * createProgressItem( const QString &label ) {
00268        return instance()->createProgressItemImpl( 0, getUniqueID(), label,
00269                                                   QString::null, true, false );
00270      }
00271 
00286      static ProgressItem * createProgressItem( ProgressItem* parent,
00287                                                const QString& id,
00288                                                const QString& label,
00289                                                const QString& status = QString::null,
00290                                                bool canBeCanceled = true,
00291                                                bool usesCrypto = false ) {
00292        return instance()->createProgressItemImpl( parent, id, label, status,
00293                                                   canBeCanceled, usesCrypto );
00294      }
00295 
00300      static ProgressItem * createProgressItem( const QString& parent,
00301                                                const QString& id,
00302                                                const QString& label,
00303                                                const QString& status = QString::null,
00304                                                bool canBeCanceled = true,
00305                                                bool usesCrypto = false ) {
00306        return instance()->createProgressItemImpl( parent, id, label,
00307                                                  status, canBeCanceled, usesCrypto );
00308      }
00309 
00313      static ProgressItem * createProgressItem( const QString& id,
00314                                                const QString& label,
00315                                                const QString& status = QString::null,
00316                                                bool canBeCanceled = true,
00317                                                bool usesCrypto = false ) {
00318        return instance()->createProgressItemImpl( 0, id, label, status,
00319                                                   canBeCanceled, usesCrypto );
00320      }
00321 
00322 
00326     bool isEmpty() const { return mTransactions.isEmpty(); }
00327 
00332     ProgressItem* singleItem() const;
00333 
00338     static void emitShowProgressDialog() {
00339        instance()->emitShowProgressDialogImpl();
00340     }
00341 
00342   signals:
00344     void progressItemAdded( KPIM::ProgressItem* );
00346     void progressItemProgress( KPIM::ProgressItem*, unsigned int );
00348     void progressItemCompleted( KPIM::ProgressItem* );
00350     void progressItemCanceled( KPIM::ProgressItem* );
00352     void progressItemStatus( KPIM::ProgressItem*, const QString& );
00354     void progressItemLabel( KPIM::ProgressItem*, const QString& );
00356     void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
00357 
00362     void showProgressDialog();
00363   public slots:
00364 
00370     void slotStandardCancelHandler( KPIM::ProgressItem* item );
00371 
00375     void slotAbortAll();
00376 
00377   private slots:
00378     void slotTransactionCompleted( KPIM::ProgressItem *item );
00379 
00380   private:
00381     ProgressManager();
00382      // prevent unsolicited copies
00383     ProgressManager( const ProgressManager& );
00384 
00385     virtual ProgressItem* createProgressItemImpl(
00386                 ProgressItem* parent, const QString& id,
00387                 const QString& label, const QString& status,
00388                 bool cancellable, bool usesCrypto );
00389     virtual ProgressItem* createProgressItemImpl(
00390                 const QString& parent,  const QString& id,
00391                 const QString& label, const QString& status,
00392                 bool cancellable, bool usesCrypto );
00393     void emitShowProgressDialogImpl();
00394 
00395     QDict< ProgressItem > mTransactions;
00396     static ProgressManager *mInstance;
00397     static unsigned int uID;
00398 };
00399 
00400 }
00401 
00402 #endif // __KPIM_PROGRESSMANAGER_H__
KDE Home | KDE Accessibility Home | Description of Access Keys