libtdepim

progressmanager.h
1 /*
2  progressmanager.h
3 
4  This file is part of TDEPIM.
5 
6  Author: Till Adam <adam@kde.org> (C) 2004
7 
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.
12 
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.
17 
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.
22 */
23 
24 #ifndef __KPIM_PROGRESSMANAGER_H__
25 #define __KPIM_PROGRESSMANAGER_H__
26 
27 #include <tqobject.h>
28 #include <tqdict.h>
29 #include <tqstring.h>
30 
31 #include <kdemacros.h>
32 
33 namespace KPIM {
34 
35 class ProgressItem;
36 class ProgressManager;
37 typedef TQMap<ProgressItem*, bool> ProgressItemMap;
38 
39 class TDE_EXPORT ProgressItem : public TQObject
40 {
41  TQ_OBJECT
42 
43  friend class ProgressManager;
44  friend class TQDict< ProgressItem >; // so it can be deleted from dicts
45 
46  public:
47 
52  const TQString& id() const { return mId; }
53 
57  ProgressItem *parent() const { return mParent; }
58 
62  const TQString& label() const { return mLabel; }
63 
68  void setLabel( const TQString& v );
69 
73  const TQString& status() const { return mStatus; }
79  void setStatus( const TQString& v );
80 
84  bool canBeCanceled() const { return mCanBeCanceled; }
85 
90  bool usesCrypto() const { return mUsesCrypto; }
91 
97  void setUsesCrypto( bool v );
98 
102  bool usesBusyIndicator() const { return mUsesBusyIndicator; }
103 
109  void setUsesBusyIndicator( bool useBusyIndicator );
110 
114  unsigned int progress() const { return mProgress; }
115 
120  void setProgress( unsigned int v );
121 
129  void setComplete();
130 
135  void reset() { setProgress( 0 ); setStatus( TQString() ); mCompleted = 0; }
136 
137  void cancel();
138 
139  // Often needed values for calculating progress.
140  void setTotalItems( unsigned int v ) { mTotal = v; }
141  unsigned int totalItems() const { return mTotal; }
142  void setCompletedItems( unsigned int v ) { mCompleted = v; }
143  void incCompletedItems( unsigned int v = 1 ) { mCompleted += v; }
144  unsigned int completedItems() const { return mCompleted; }
145 
149  void updateProgress() { setProgress( mTotal? mCompleted*100/mTotal: 0 ); }
150 
151  void addChild( ProgressItem *kiddo );
152  void removeChild( ProgressItem *kiddo );
153 
154  bool canceled() const { return mCanceled; }
155 
156 signals:
161  void progressItemAdded( KPIM::ProgressItem* );
167  void progressItemProgress( KPIM::ProgressItem*, unsigned int );
174  void progressItemCompleted( KPIM::ProgressItem* );
185  void progressItemCanceled( KPIM::ProgressItem* );
192  void progressItemStatus( KPIM::ProgressItem*, const TQString& );
199  void progressItemLabel( KPIM::ProgressItem*, const TQString& );
206  void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
207 
215  void progressItemUsesBusyIndicator( KPIM::ProgressItem *item, bool value );
216 
217 
218  protected:
219  /* Only to be used by our good friend the ProgressManager */
220  ProgressItem( ProgressItem* parent,
221  const TQString& id,
222  const TQString& label,
223  const TQString& status,
224  bool isCancellable,
225  bool usesCrypto );
226  virtual ~ProgressItem();
227 
228 
229  private:
230  TQString mId;
231  TQString mLabel;
232  TQString mStatus;
233  ProgressItem* mParent;
234  bool mCanBeCanceled;
235  unsigned int mProgress;
236  ProgressItemMap mChildren;
237  unsigned int mTotal;
238  unsigned int mCompleted;
239  bool mWaitingForKids;
240  bool mCanceled;
241  bool mUsesCrypto;
242  bool mUsesBusyIndicator;
243 };
244 
266 class TDE_EXPORT ProgressManager : public TQObject
267 {
268 
269  TQ_OBJECT
270 
271 
272  public:
273  virtual ~ProgressManager();
274 
278  static ProgressManager * instance();
279 
286  static TQString getUniqueID() { return TQString::number( ++uID ); }
287 
296  static ProgressItem * createProgressItem( const TQString &label ) {
297  return instance()->createProgressItemImpl( 0, getUniqueID(), label,
298  TQString(), true, false );
299  }
300 
317  static ProgressItem * createProgressItem( ProgressItem* parent,
318  const TQString& id,
319  const TQString& label,
320  const TQString& status = TQString(),
321  bool canBeCanceled = true,
322  bool usesCrypto = false ) {
323  return instance()->createProgressItemImpl( parent, id, label, status,
324  canBeCanceled, usesCrypto );
325  }
326 
331  static ProgressItem * createProgressItem( const TQString& parent,
332  const TQString& id,
333  const TQString& label,
334  const TQString& status = TQString(),
335  bool canBeCanceled = true,
336  bool usesCrypto = false ) {
337  return instance()->createProgressItemImpl( parent, id, label,
338  status, canBeCanceled, usesCrypto );
339  }
340 
344  static ProgressItem * createProgressItem( const TQString& id,
345  const TQString& label,
346  const TQString& status = TQString(),
347  bool canBeCanceled = true,
348  bool usesCrypto = false ) {
349  return instance()->createProgressItemImpl( 0, id, label, status,
350  canBeCanceled, usesCrypto );
351  }
352 
353 
357  bool isEmpty() const { return mTransactions.isEmpty(); }
358 
366  ProgressItem* singleItem() const;
367 
372  static void emitShowProgressDialog() {
373  instance()->emitShowProgressDialogImpl();
374  }
375 
376  signals:
378  void progressItemAdded( KPIM::ProgressItem* );
380  void progressItemProgress( KPIM::ProgressItem*, unsigned int );
382  void progressItemCompleted( KPIM::ProgressItem* );
384  void progressItemCanceled( KPIM::ProgressItem* );
386  void progressItemStatus( KPIM::ProgressItem*, const TQString& );
388  void progressItemLabel( KPIM::ProgressItem*, const TQString& );
390  void progressItemUsesCrypto( KPIM::ProgressItem*, bool );
392  void progressItemUsesBusyIndicator( KPIM::ProgressItem*, bool );
393 
399  public slots:
400 
406  void slotStandardCancelHandler( KPIM::ProgressItem* item );
407 
411  void slotAbortAll();
412 
413  private slots:
414  void slotTransactionCompleted( KPIM::ProgressItem *item );
415 
416  private:
417  ProgressManager();
418  // prevent unsolicited copies
420 
421  virtual ProgressItem* createProgressItemImpl(
422  ProgressItem* parent, const TQString& id,
423  const TQString& label, const TQString& status,
424  bool cancellable, bool usesCrypto );
425  virtual ProgressItem* createProgressItemImpl(
426  const TQString& parent, const TQString& id,
427  const TQString& label, const TQString& status,
428  bool cancellable, bool usesCrypto );
429  void emitShowProgressDialogImpl();
430 
431  TQDict< ProgressItem > mTransactions;
432  static ProgressManager *mInstance;
433  static unsigned int uID;
434 };
435 
436 }
437 
438 #endif // __KPIM_PROGRESSMANAGER_H__
The ProgressManager singleton keeps track of all ongoing transactions and notifies observers (progres...
static void emitShowProgressDialog()
Ask all listeners to show the progress dialog, because there is something that wants to be shown.
static ProgressItem * createProgressItem(ProgressItem *parent, const TQString &id, const TQString &label, const TQString &status=TQString(), bool canBeCanceled=true, bool usesCrypto=false)
Creates a new progressItem with the given parent, id, label and initial status.
void progressItemAdded(KPIM::ProgressItem *)
static TQString getUniqueID()
Use this to acquire a unique id number which can be used to discern an operation from all others goin...
static ProgressItem * createProgressItem(const TQString &label)
Creates a ProgressItem with a unique id and the given label.
void progressItemCanceled(KPIM::ProgressItem *)
static ProgressItem * createProgressItem(const TQString &parent, const TQString &id, const TQString &label, const TQString &status=TQString(), bool canBeCanceled=true, bool usesCrypto=false)
Use this version if you have the id string of the parent and want to add a subjob to it.
void progressItemLabel(KPIM::ProgressItem *, const TQString &)
static ProgressItem * createProgressItem(const TQString &id, const TQString &label, const TQString &status=TQString(), bool canBeCanceled=true, bool usesCrypto=false)
Version without a parent.
void progressItemUsesBusyIndicator(KPIM::ProgressItem *, bool)
void progressItemStatus(KPIM::ProgressItem *, const TQString &)
void progressItemProgress(KPIM::ProgressItem *, unsigned int)
void progressItemCompleted(KPIM::ProgressItem *)
void showProgressDialog()
Emitted when an operation requests the listeners to be shown.
void progressItemUsesCrypto(KPIM::ProgressItem *, bool)
TDEPIM classes for drag and drop of mails.