libtdepim

progressmanager.cpp
1 /*
2  progressmanager.cpp
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 #include <tqvaluevector.h>
25 #include <kdebug.h>
26 
27 #include <tdelocale.h>
28 #include <kstaticdeleter.h>
29 
30 #include "progressmanager.h"
31 
32 namespace KPIM {
33 
34 KPIM::ProgressManager * KPIM::ProgressManager::mInstance = 0;
35 unsigned int KPIM::ProgressManager::uID = 42;
36 
37 ProgressItem::ProgressItem(
38  ProgressItem* parent, const TQString& id,
39  const TQString& label, const TQString& status, bool canBeCanceled,
40  bool usesCrypto )
41  :mId( id ), mLabel( label ), mStatus( status ), mParent( parent ),
42  mCanBeCanceled( canBeCanceled ), mProgress( 0 ), mTotal( 0 ),
43  mCompleted( 0 ), mWaitingForKids( false ), mCanceled( false ),
44  mUsesCrypto( usesCrypto ), mUsesBusyIndicator( false )
45  {}
46 
47 ProgressItem::~ProgressItem()
48 {
49 }
50 
51 void ProgressItem::setComplete()
52 {
53 // kdDebug(5300) << "ProgressItem::setComplete - " << label() << endl;
54 
55  if ( mChildren.isEmpty() ) {
56  if ( !mCanceled )
57  setProgress( 100 );
58  emit progressItemCompleted( this );
59  if ( parent() )
60  parent()->removeChild( this );
61  deleteLater();
62  } else {
63  mWaitingForKids = true;
64  }
65 }
66 
67 void ProgressItem::addChild( ProgressItem *kiddo )
68 {
69  mChildren.replace( kiddo, true );
70 }
71 
72 void ProgressItem::removeChild( ProgressItem *kiddo )
73 {
74  mChildren.remove( kiddo );
75  // in case we were waiting for the last kid to go away, now is the time
76  if ( mChildren.count() == 0 && mWaitingForKids ) {
77  emit progressItemCompleted( this );
78  deleteLater();
79  }
80 }
81 
82 void ProgressItem::cancel()
83 {
84  if ( mCanceled || !mCanBeCanceled ) return;
85  kdDebug(5300) << "ProgressItem::cancel() - " << label() << endl;
86  mCanceled = true;
87  // Cancel all children.
88  TQValueList<ProgressItem*> kids = mChildren.keys();
89  TQValueList<ProgressItem*>::Iterator it( kids.begin() );
90  TQValueList<ProgressItem*>::Iterator end( kids.end() );
91  for ( ; it != end; it++ ) {
92  ProgressItem *kid = *it;
93  if ( kid->canBeCanceled() )
94  kid->cancel();
95  }
96  setStatus( i18n( "Aborting..." ) );
97  emit progressItemCanceled( this );
98 }
99 
100 
101 void ProgressItem::setProgress( unsigned int v )
102 {
103  mProgress = v;
104  // kdDebug(5300) << "ProgressItem::setProgress(): " << label() << " : " << v << endl;
105  emit progressItemProgress( this, mProgress );
106 }
107 
108 void ProgressItem::setLabel( const TQString& v )
109 {
110  mLabel = v;
111  emit progressItemLabel( this, mLabel );
112 }
113 
114 void ProgressItem::setStatus( const TQString& v )
115 {
116  mStatus = v;
117  emit progressItemStatus( this, mStatus );
118 }
119 
120 void ProgressItem::setUsesCrypto( bool v )
121 {
122  mUsesCrypto = v;
123  emit progressItemUsesCrypto( this, v );
124 }
125 
126 void ProgressItem::setUsesBusyIndicator( bool useBusyIndicator )
127 {
128  mUsesBusyIndicator = useBusyIndicator;
129  emit progressItemUsesBusyIndicator( this, useBusyIndicator );
130 }
131 
132 // ======================================
133 
134 ProgressManager::ProgressManager() :TQObject() {
135  mInstance = this;
136 }
137 
138 ProgressManager::~ProgressManager() { mInstance = 0; }
139 static KStaticDeleter<ProgressManager> progressManagerDeleter;
140 
141 ProgressManager* ProgressManager::instance()
142 {
143  if ( !mInstance ) {
144  progressManagerDeleter.setObject( mInstance, new ProgressManager() );
145  }
146  return mInstance;
147 }
148 
149 ProgressItem* ProgressManager::createProgressItemImpl(
150  ProgressItem* parent, const TQString& id,
151  const TQString &label, const TQString &status,
152  bool cancellable, bool usesCrypto )
153 {
154  ProgressItem *t = 0;
155  if ( !mTransactions[ id ] ) {
156  t = new ProgressItem ( parent, id, label, status, cancellable, usesCrypto );
157  mTransactions.insert( id, t );
158  if ( parent ) {
159  ProgressItem *p = mTransactions[ parent->id() ];
160  if ( p ) {
161  p->addChild( t );
162  }
163  }
164  // connect all signals
165  connect ( t, TQ_SIGNAL( progressItemCompleted(KPIM::ProgressItem*) ),
166  this, TQ_SLOT( slotTransactionCompleted(KPIM::ProgressItem*) ) );
167  connect ( t, TQ_SIGNAL( progressItemProgress(KPIM::ProgressItem*, unsigned int) ),
168  this, TQ_SIGNAL( progressItemProgress(KPIM::ProgressItem*, unsigned int) ) );
169  connect ( t, TQ_SIGNAL( progressItemAdded(KPIM::ProgressItem*) ),
170  this, TQ_SIGNAL( progressItemAdded(KPIM::ProgressItem*) ) );
171  connect ( t, TQ_SIGNAL( progressItemCanceled(KPIM::ProgressItem*) ),
172  this, TQ_SIGNAL( progressItemCanceled(KPIM::ProgressItem*) ) );
173  connect ( t, TQ_SIGNAL( progressItemStatus(KPIM::ProgressItem*, const TQString&) ),
174  this, TQ_SIGNAL( progressItemStatus(KPIM::ProgressItem*, const TQString&) ) );
175  connect ( t, TQ_SIGNAL( progressItemLabel(KPIM::ProgressItem*, const TQString&) ),
176  this, TQ_SIGNAL( progressItemLabel(KPIM::ProgressItem*, const TQString&) ) );
177  connect ( t, TQ_SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem*, bool) ),
178  this, TQ_SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem*, bool) ) );
179  connect ( t, TQ_SIGNAL( progressItemUsesBusyIndicator(KPIM::ProgressItem*, bool) ),
180  this, TQ_SIGNAL( progressItemUsesBusyIndicator(KPIM::ProgressItem*, bool) ) );
181 
182  emit progressItemAdded( t );
183  } else {
184  // Hm, is this what makes the most sense?
185  t = mTransactions[id];
186  }
187  return t;
188 }
189 
190 ProgressItem* ProgressManager::createProgressItemImpl(
191  const TQString& parent, const TQString &id,
192  const TQString &label, const TQString& status,
193  bool canBeCanceled, bool usesCrypto )
194 {
195  ProgressItem * p = mTransactions[parent];
196  return createProgressItemImpl( p, id, label, status, canBeCanceled, usesCrypto );
197 }
198 
199 void ProgressManager::emitShowProgressDialogImpl()
200 {
201  emit showProgressDialog();
202 }
203 
204 
205 // slots
206 
207 void ProgressManager::slotTransactionCompleted( ProgressItem *item )
208 {
209  mTransactions.remove( item->id() );
210  emit progressItemCompleted( item );
211 }
212 
213 void ProgressManager::slotStandardCancelHandler( ProgressItem *item )
214 {
215  item->setComplete();
216 }
217 
218 ProgressItem* ProgressManager::singleItem() const
219 {
220  ProgressItem *item = 0;
221  TQDictIterator< ProgressItem > it( mTransactions );
222  for ( ; it.current(); ++it ) {
223 
224  // No single item for progress possible, as one of them is a busy indicator one.
225  if ( (*it)->usesBusyIndicator() )
226  return 0;
227 
228  if ( !(*it)->parent() ) { // if it's a top level one, only those count
229  if ( item )
230  return 0; // we found more than one
231  else
232  item = (*it);
233  }
234  }
235  return item;
236 }
237 
238 void ProgressManager::slotAbortAll()
239 {
240  TQDictIterator< ProgressItem > it( mTransactions );
241  for ( ; it.current(); ++it ) {
242  it.current()->cancel();
243  }
244 }
245 
246 } // namespace
247 
248 #include "progressmanager.moc"
The ProgressManager singleton keeps track of all ongoing transactions and notifies observers (progres...
TDEPIM classes for drag and drop of mails.