• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.14
  • kdepim
  • libkdepim
  • progresswidget
progressmanager.cpp
Go to the documentation of this file.
1 /*
2  progressmanager.cpp
3 
4  This file is part of libkdepim.
5 
6  Copyright (c) 2004 Till Adam <adam@kde.org>
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 #include "progressmanager.h"
25 
26 #include <KDebug>
27 #include <KLocale>
28 #include <KGlobal>
29 
30 namespace KPIM {
31 
32 unsigned int KPIM::ProgressManager::uID = 42;
33 
34 ProgressItem::ProgressItem( ProgressItem *parent, const QString &id,
35  const QString &label, const QString &status,
36  bool canBeCanceled, CryptoStatus cryptoStatus )
37  : mId( id ),
38  mLabel( label ),
39  mStatus( status ),
40  mParent( parent ),
41  mCanBeCanceled( canBeCanceled ),
42  mProgress( 0 ),
43  mTotal( 0 ),
44  mCompleted( 0 ),
45  mCryptoStatus(cryptoStatus),
46  mType( 0 ),
47  mWaitingForKids( false ),
48  mCanceled( false ),
49  mUsesBusyIndicator( false ),
50  mCompletedCalled( false )
51 {
52 }
53 
54 ProgressItem::~ProgressItem()
55 {
56 }
57 
58 void ProgressItem::setComplete()
59 {
60  // kDebug() << label();
61  if ( mChildren.isEmpty() ) {
62  if ( mCompletedCalled )
63  return;
64  if ( !mCanceled ) {
65  setProgress( 100 );
66  }
67  mCompletedCalled = true;
68  if ( parent() ) {
69  parent()->removeChild( this );
70  }
71  emit progressItemCompleted( this );
72  } else {
73  mWaitingForKids = true;
74  }
75 }
76 
77 void ProgressItem::reset()
78 {
79  setProgress( 0 );
80  setStatus( QString() );
81  mCompleted = 0;
82 }
83 
84 void ProgressItem::addChild( ProgressItem *kiddo )
85 {
86  mChildren.insert( kiddo, true );
87 }
88 
89 void ProgressItem::removeChild( ProgressItem *kiddo )
90 {
91  if ( mChildren.isEmpty() ) {
92  mWaitingForKids = false;
93  return;
94  }
95 
96  if ( mChildren.remove( kiddo ) == 0 ) {
97  // do nothing if the specified item is not in the map
98  return;
99  }
100 
101  // in case we were waiting for the last kid to go away, now is the time
102  if ( mChildren.count() == 0 && mWaitingForKids ) {
103  emit progressItemCompleted( this );
104  }
105 }
106 
107 unsigned int ProgressItem::typeProgressItem() const
108 {
109  return mType;
110 }
111 
112 void ProgressItem::setTypeProgressItem(unsigned int type)
113 {
114  mType = type;
115 }
116 
117 void ProgressItem::cancel()
118 {
119  if ( mCanceled || !mCanBeCanceled ) {
120  return;
121  }
122 
123  kDebug() << label();
124  mCanceled = true;
125  // Cancel all children.
126  QList<ProgressItem* > kids = mChildren.keys();
127  QList<ProgressItem* >::Iterator it( kids.begin() );
128  QList<ProgressItem* >::Iterator end( kids.end() );
129  for ( ; it != end; it++ ) {
130  ProgressItem *kid = *it;
131  if ( kid->canBeCanceled() ) {
132  kid->cancel();
133  }
134  }
135  setStatus( i18n( "Aborting..." ) );
136  emit progressItemCanceled( this );
137 }
138 
139 void ProgressItem::updateProgress()
140 {
141  setProgress( mTotal? mCompleted * 100 / mTotal : 0 );
142 }
143 
144 void ProgressItem::setProgress( unsigned int v )
145 {
146  mProgress = v;
147  // kDebug() << label() << " :" << v;
148  emit progressItemProgress( this, mProgress );
149 }
150 
151 void ProgressItem::setLabel( const QString &v )
152 {
153  mLabel = v;
154  emit progressItemLabel( this, mLabel );
155 }
156 
157 void ProgressItem::setStatus( const QString &v )
158 {
159  mStatus = v;
160  emit progressItemStatus( this, mStatus );
161 }
162 
163 void ProgressItem::setCanBeCanceled(bool b)
164 {
165  mCanBeCanceled = b;
166 }
167 
168 void ProgressItem::setCryptoStatus( ProgressItem::CryptoStatus v )
169 {
170  mCryptoStatus = v;
171  emit progressItemCryptoStatus( this, v );
172 }
173 
174 void ProgressItem::setUsesBusyIndicator( bool useBusyIndicator )
175 {
176  mUsesBusyIndicator = useBusyIndicator;
177  emit progressItemUsesBusyIndicator( this, useBusyIndicator );
178 }
179 
180 // ======================================
181 
182 struct ProgressManagerPrivate {
183  ProgressManager instance;
184 };
185 
186 K_GLOBAL_STATIC( ProgressManagerPrivate, progressManagerPrivate )
187 
188 ProgressManager::ProgressManager()
189  : QObject()
190 {
191 
192 }
193 
194 ProgressManager::~ProgressManager()
195 {
196 
197 }
198 
199 ProgressManager *ProgressManager::instance()
200 {
201  return progressManagerPrivate.isDestroyed() ? 0 : &progressManagerPrivate->instance ;
202 }
203 
204 QString ProgressManager::getUniqueID()
205 {
206  return QString::number( ++uID );
207 }
208 
209 bool ProgressManager::isEmpty() const
210 {
211  return mTransactions.isEmpty();
212 }
213 
214 ProgressItem *ProgressManager::createProgressItem(const QString &id, const QString &label, const QString &status, bool canBeCanceled, ProgressItem::CryptoStatus cryptoStatus)
215 {
216  return instance()->createProgressItemImpl( 0, id, label, status,
217  canBeCanceled, cryptoStatus );
218 }
219 
220 ProgressItem *ProgressManager::createProgressItem(const QString &parent, const QString &id, const QString &label, const QString &status, bool canBeCanceled, ProgressItem::CryptoStatus cryptoStatus)
221 {
222  return instance()->createProgressItemImpl( parent, id, label,
223  status, canBeCanceled, cryptoStatus );
224 }
225 
226 ProgressItem *ProgressManager::createProgressItem(ProgressItem *parent, const QString &id, const QString &label, const QString &status, bool canBeCanceled, ProgressItem::CryptoStatus cryptoStatus)
227 {
228  return instance()->createProgressItemImpl( parent, id, label, status,
229  canBeCanceled, cryptoStatus );
230 }
231 
232 ProgressItem *ProgressManager::createProgressItem(const QString &label)
233 {
234  return instance()->createProgressItemImpl( 0, getUniqueID(), label,
235  QString(), true, KPIM::ProgressItem::Unencrypted );
236 }
237 
238 ProgressItem *ProgressManager::createProgressItem(unsigned int progressType, const QString &label)
239 {
240  return instance()->createProgressItemImpl( 0, getUniqueID(), label,
241  QString(), true, KPIM::ProgressItem::Unencrypted, progressType );
242 }
243 
244 ProgressItem *ProgressManager::createProgressItemImpl( ProgressItem *parent,
245  const QString &id,
246  const QString &label,
247  const QString &status,
248  bool cancellable,
249  ProgressItem::CryptoStatus cryptoStatus,
250  unsigned int progressType)
251 {
252  ProgressItem *t = 0;
253  if ( !mTransactions.value( id ) ) {
254  t = new ProgressItem ( parent, id, label, status, cancellable, cryptoStatus );
255  t->setTypeProgressItem(progressType);
256  mTransactions.insert( id, t );
257  if ( parent ) {
258  ProgressItem *p = mTransactions.value( parent->id() );
259  if ( p ) {
260  p->addChild( t );
261  }
262  }
263  // connect all signals
264  connect ( t, SIGNAL(progressItemCompleted(KPIM::ProgressItem*)),
265  this, SLOT(slotTransactionCompleted(KPIM::ProgressItem*)) );
266  connect ( t, SIGNAL(progressItemProgress(KPIM::ProgressItem*,uint)),
267  this, SIGNAL(progressItemProgress(KPIM::ProgressItem*,uint)) );
268  connect ( t, SIGNAL(progressItemAdded(KPIM::ProgressItem*)),
269  this, SIGNAL(progressItemAdded(KPIM::ProgressItem*)) );
270  connect ( t, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
271  this, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)) );
272  connect ( t, SIGNAL(progressItemStatus(KPIM::ProgressItem*,QString)),
273  this, SIGNAL(progressItemStatus(KPIM::ProgressItem*,QString)) );
274  connect ( t, SIGNAL(progressItemLabel(KPIM::ProgressItem*,QString)),
275  this, SIGNAL(progressItemLabel(KPIM::ProgressItem*,QString)) );
276  connect ( t, SIGNAL(progressItemCryptoStatus(KPIM::ProgressItem*,KPIM::ProgressItem::CryptoStatus)),
277  this, SIGNAL(progressItemCryptoStatus(KPIM::ProgressItem*,KPIM::ProgressItem::CryptoStatus)) );
278  connect ( t, SIGNAL(progressItemUsesBusyIndicator(KPIM::ProgressItem*,bool)),
279  this, SIGNAL(progressItemUsesBusyIndicator(KPIM::ProgressItem*,bool)) );
280 
281  emit progressItemAdded( t );
282  } else {
283  // Hm, is this what makes the most sense?
284  t = mTransactions.value( id );
285  }
286  return t;
287 }
288 
289 ProgressItem *ProgressManager::createProgressItemImpl( const QString &parent,
290  const QString &id,
291  const QString &label,
292  const QString &status,
293  bool canBeCanceled,
294  ProgressItem::CryptoStatus cryptoStatus,
295  unsigned int progressType )
296 {
297  ProgressItem *p = mTransactions.value( parent );
298  return createProgressItemImpl( p, id, label, status, canBeCanceled, cryptoStatus, progressType );
299 }
300 
301 void ProgressManager::emitShowProgressDialogImpl()
302 {
303  emit showProgressDialog();
304 }
305 
306 // slots
307 
308 void ProgressManager::slotTransactionCompleted( ProgressItem *item )
309 {
310  mTransactions.remove( item->id() );
311  emit progressItemCompleted( item );
312 }
313 
314 void ProgressManager::slotStandardCancelHandler( ProgressItem *item )
315 {
316  item->setComplete();
317 }
318 
319 ProgressItem *ProgressManager::singleItem() const
320 {
321  ProgressItem *item = 0;
322  QHash< QString, ProgressItem* >::const_iterator it = mTransactions.constBegin();
323  QHash< QString, ProgressItem* >::const_iterator end = mTransactions.constEnd();
324  while ( it != end ) {
325 
326  // No single item for progress possible, as one of them is a busy indicator one.
327  if ( (*it)->usesBusyIndicator() )
328  return 0;
329 
330  if ( !(*it)->parent() ) { // if it's a top level one, only those count
331  if ( item ) {
332  return 0; // we found more than one
333  } else {
334  item = (*it);
335  }
336  }
337  ++it;
338  }
339  return item;
340 }
341 
342 void ProgressManager::emitShowProgressDialog()
343 {
344  instance()->emitShowProgressDialogImpl();
345 }
346 
347 void ProgressManager::slotAbortAll()
348 {
349  QHashIterator<QString, ProgressItem *> it(mTransactions);
350  while (it.hasNext()) {
351  it.next();
352  it.value()->cancel();
353  }
354 
355 }
356 
357 } // namespace
358 
KPIM::ProgressItem::id
const QString & id() const
Definition: progressmanager.h:62
KPIM::ProgressManager::instance
static ProgressManager * instance()
Definition: progressmanager.cpp:199
KPIM::ProgressItem::~ProgressItem
virtual ~ProgressItem()
Definition: progressmanager.cpp:54
KPIM::ProgressManager::slotAbortAll
void slotAbortAll()
Aborts all running jobs.
Definition: progressmanager.cpp:347
KPIM::ProgressItem::removeChild
void removeChild(ProgressItem *kiddo)
Definition: progressmanager.cpp:89
KPIM::ProgressManager::isEmpty
bool isEmpty() const
Definition: progressmanager.cpp:209
KPIM::ProgressItem::Unencrypted
Definition: progressmanager.h:54
KPIM::ProgressItem::setCryptoStatus
void setCryptoStatus(ProgressItem::CryptoStatus v)
Set whether this item uses crypted communication, so listeners can display a nice crypto icon...
Definition: progressmanager.cpp:168
QHashIterator::hasNext
bool hasNext() const
KPIM::ProgressItem::progressItemProgress
void progressItemProgress(KPIM::ProgressItem *, unsigned int)
Emitted when the progress value of an item changes.
KPIM::ProgressManager::progressItemCompleted
void progressItemCompleted(KPIM::ProgressItem *)
KPIM::ProgressManager::progressItemProgress
void progressItemProgress(KPIM::ProgressItem *, unsigned int)
KPIM::ProgressItem::updateProgress
void updateProgress()
Recalculate progress according to total/completed items and update.
Definition: progressmanager.cpp:139
KPIM::ProgressItem::progressItemLabel
void progressItemLabel(KPIM::ProgressItem *, const QString &)
Emitted when the label of an item changed.
KPIM::ProgressManager::progressItemStatus
void progressItemStatus(KPIM::ProgressItem *, const QString &)
KPIM::ProgressItem::canBeCanceled
bool canBeCanceled() const
Definition: progressmanager.h:92
KPIM::ProgressItem::parent
ProgressItem * parent() const
Definition: progressmanager.h:67
KPIM::ProgressManager::progressItemLabel
void progressItemLabel(KPIM::ProgressItem *, const QString &)
KPIM::ProgressItem::label
const QString & label() const
Definition: progressmanager.h:72
QMap::keys
QList< Key > keys() const
KPIM::ProgressItem::setLabel
void setLabel(const QString &v)
Definition: progressmanager.cpp:151
QString::number
QString number(int n, int base)
KPIM::ProgressManager
The ProgressManager singleton keeps track of all ongoing transactions and notifies observers (progres...
Definition: progressmanager.h:285
KPIM::ProgressItem::setTypeProgressItem
void setTypeProgressItem(unsigned int)
Definition: progressmanager.cpp:112
QObject
KPIM::ProgressItem::setComplete
void setComplete()
Tell the item it has finished.
Definition: progressmanager.cpp:58
KPIM::ProgressItem::reset
void reset()
Reset the progress value of this item to 0 and the status string to the empty string.
Definition: progressmanager.cpp:77
QHashIterator
progressmanager.h
KPIM::ProgressItem::progressItemUsesBusyIndicator
void progressItemUsesBusyIndicator(KPIM::ProgressItem *item, bool value)
Emitted when the busy indicator state of an item changes.
KPIM::ProgressItem::addChild
void addChild(ProgressItem *kiddo)
Definition: progressmanager.cpp:84
KPIM::ProgressManager::~ProgressManager
virtual ~ProgressManager()
Definition: progressmanager.cpp:194
KPIM::ProgressItem::progressItemCryptoStatus
void progressItemCryptoStatus(KPIM::ProgressItem *, KPIM::ProgressItem::CryptoStatus)
Emitted when the crypto status of an item changed.
QString
QList
KPIM::ProgressItem::cancel
void cancel()
Definition: progressmanager.cpp:117
KPIM::ProgressManager::progressItemUsesBusyIndicator
void progressItemUsesBusyIndicator(KPIM::ProgressItem *, bool)
QList::end
iterator end()
QHashIterator::next
Item next()
KPIM::ProgressItem::setProgress
void setProgress(unsigned int v)
Set the progress (percentage of completion) value of this item.
Definition: progressmanager.cpp:144
QHash::const_iterator
KPIM::ProgressManager::getUniqueID
static QString getUniqueID()
Use this to acquire a unique id number which can be used to discern an operation from all others goin...
Definition: progressmanager.cpp:204
KPIM::ProgressItem::setUsesBusyIndicator
void setUsesBusyIndicator(bool useBusyIndicator)
Sets whether this item uses a busy indicator instead of real progress for its progress bar...
Definition: progressmanager.cpp:174
KPIM::ProgressItem::typeProgressItem
unsigned int typeProgressItem() const
Definition: progressmanager.cpp:107
KPIM::ProgressItem::progressItemStatus
void progressItemStatus(KPIM::ProgressItem *, const QString &)
Emitted when the status message of an item changed.
KPIM::ProgressManager::slotStandardCancelHandler
void slotStandardCancelHandler(KPIM::ProgressItem *item)
Calls setCompleted() on the item, to make sure it goes away.
Definition: progressmanager.cpp:314
KPIM::ProgressItem
Definition: progressmanager.h:45
KPIM::ProgressItem::progressItemCompleted
void progressItemCompleted(KPIM::ProgressItem *)
Emitted when a progress item was completed.
KPIM::ProgressItem::CryptoStatus
CryptoStatus
Definition: progressmanager.h:52
KPIM::ProgressManager::progressItemAdded
void progressItemAdded(KPIM::ProgressItem *)
KPIM::ProgressManager::progressItemCryptoStatus
void progressItemCryptoStatus(KPIM::ProgressItem *, KPIM::ProgressItem::CryptoStatus)
QMap::insert
iterator insert(const Key &key, const T &value)
QMap::isEmpty
bool isEmpty() const
KPIM::ProgressItem::progressItemCanceled
void progressItemCanceled(KPIM::ProgressItem *)
Emitted when an item was canceled.
KPIM::ProgressManager::progressItemCanceled
void progressItemCanceled(KPIM::ProgressItem *)
QHashIterator::value
const T & value() const
KPIM::ProgressManager::showProgressDialog
void showProgressDialog()
Emitted when an operation requests the listeners to be shown.
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QMap::count
int count(const Key &key) const
QList::begin
iterator begin()
KPIM::ProgressManager::emitShowProgressDialog
static void emitShowProgressDialog()
Ask all listeners to show the progress dialog, because there is something that wants to be shown...
Definition: progressmanager.cpp:342
KPIM::ProgressItem::setCanBeCanceled
void setCanBeCanceled(bool b)
Definition: progressmanager.cpp:163
KPIM::ProgressItem::ProgressItem
ProgressItem(ProgressItem *parent, const QString &id, const QString &label, const QString &status, bool isCancellable, CryptoStatus cryptoStatus)
Definition: progressmanager.cpp:34
KPIM::ProgressManager::singleItem
ProgressItem * singleItem() const
Definition: progressmanager.cpp:319
KPIM::ProgressItem::setStatus
void setStatus(const QString &v)
Set the string to be used for showing this item's current status.
Definition: progressmanager.cpp:157
QMap::remove
int remove(const Key &key)
KPIM::ProgressManager::createProgressItem
static ProgressItem * createProgressItem(unsigned int progressType, const QString &label)
Creates a ProgressItem with a unique id and the given label.
Definition: progressmanager.cpp:238
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal