libkdepim

progressmanager.cpp

00001 /*
00002   progressmanager.cpp
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 #include <qvaluevector.h>
00025 #include <kdebug.h>
00026 
00027 #include <klocale.h>
00028 #include <kstaticdeleter.h>
00029 
00030 #include "progressmanager.h"
00031 
00032 namespace KPIM {
00033 
00034 KPIM::ProgressManager * KPIM::ProgressManager::mInstance = 0;
00035 unsigned int KPIM::ProgressManager::uID = 42;
00036 
00037 ProgressItem::ProgressItem(
00038        ProgressItem* parent, const QString& id,
00039        const QString& label, const QString& status, bool canBeCanceled,
00040        bool usesCrypto )
00041        :mId( id ), mLabel( label ), mStatus( status ), mParent( parent ),
00042         mCanBeCanceled( canBeCanceled ), mProgress( 0 ), mTotal( 0 ),
00043         mCompleted( 0 ), mWaitingForKids( false ), mCanceled( false ),
00044         mUsesCrypto( usesCrypto )
00045     {}
00046 
00047 ProgressItem::~ProgressItem()
00048 {
00049 }
00050 
00051 void ProgressItem::setComplete()
00052 {
00053 //   kdDebug(5300) << "ProgressItem::setComplete - " << label() << endl;
00054 
00055    if ( mChildren.isEmpty() ) {
00056      if ( !mCanceled )
00057        setProgress( 100 );
00058      emit progressItemCompleted( this );
00059      if ( parent() )
00060        parent()->removeChild( this );
00061      deleteLater();
00062    } else {
00063      mWaitingForKids = true;
00064    }
00065 }
00066 
00067 void ProgressItem::addChild( ProgressItem *kiddo )
00068 {
00069   mChildren.replace( kiddo, true );
00070 }
00071 
00072 void ProgressItem::removeChild( ProgressItem *kiddo )
00073 {
00074    mChildren.remove( kiddo );
00075    // in case we were waiting for the last kid to go away, now is the time
00076    if ( mChildren.count() == 0 && mWaitingForKids ) {
00077      emit progressItemCompleted( this );
00078      deleteLater();
00079    }
00080 }
00081 
00082 void ProgressItem::cancel()
00083 {
00084    if ( mCanceled || !mCanBeCanceled ) return;
00085    kdDebug(5300) << "ProgressItem::cancel() - " << label() << endl;
00086    mCanceled = true;
00087    // Cancel all children.
00088    QValueList<ProgressItem*> kids = mChildren.keys();
00089    QValueList<ProgressItem*>::Iterator it( kids.begin() );
00090    QValueList<ProgressItem*>::Iterator end( kids.end() );
00091    for ( ; it != end; it++ ) {
00092      ProgressItem *kid = *it;
00093      if ( kid->canBeCanceled() )
00094        kid->cancel();
00095    }
00096    setStatus( i18n( "Aborting..." ) );
00097    emit progressItemCanceled( this );
00098 }
00099 
00100 
00101 void ProgressItem::setProgress( unsigned int v )
00102 {
00103    mProgress = v;
00104    // kdDebug(5300) << "ProgressItem::setProgress(): " << label() << " : " << v << endl;
00105    emit progressItemProgress( this, mProgress );
00106 }
00107 
00108 void ProgressItem::setLabel( const QString& v )
00109 {
00110   mLabel = v;
00111   emit progressItemLabel( this, mLabel );
00112 }
00113 
00114 void ProgressItem::setStatus( const QString& v )
00115 {
00116   mStatus = v;
00117   emit progressItemStatus( this, mStatus );
00118 }
00119 
00120 void ProgressItem::setUsesCrypto( bool v )
00121 {
00122   mUsesCrypto = v;
00123   emit progressItemUsesCrypto( this, v );
00124 }
00125 
00126 // ======================================
00127 
00128 ProgressManager::ProgressManager() :QObject() {
00129   mInstance = this;
00130 }
00131 
00132 ProgressManager::~ProgressManager() { mInstance = 0; }
00133 static KStaticDeleter<ProgressManager> progressManagerDeleter;
00134 
00135 ProgressManager* ProgressManager::instance()
00136 {
00137    if ( !mInstance ) {
00138      progressManagerDeleter.setObject( mInstance, new ProgressManager() );
00139    }
00140    return mInstance;
00141 }
00142 
00143 ProgressItem* ProgressManager::createProgressItemImpl(
00144      ProgressItem* parent, const QString& id,
00145      const QString &label, const QString &status,
00146      bool cancellable, bool usesCrypto )
00147 {
00148    ProgressItem *t = 0;
00149    if ( !mTransactions[ id ] ) {
00150      t = new ProgressItem ( parent, id, label, status, cancellable, usesCrypto );
00151      mTransactions.insert( id, t );
00152      if ( parent ) {
00153        ProgressItem *p = mTransactions[ parent->id() ];
00154        if ( p ) {
00155          p->addChild( t );
00156        }
00157      }
00158      // connect all signals
00159      connect ( t, SIGNAL( progressItemCompleted(KPIM::ProgressItem*) ),
00160                this, SLOT( slotTransactionCompleted(KPIM::ProgressItem*) ) );
00161      connect ( t, SIGNAL( progressItemProgress(KPIM::ProgressItem*, unsigned int) ),
00162                this, SIGNAL( progressItemProgress(KPIM::ProgressItem*, unsigned int) ) );
00163      connect ( t, SIGNAL( progressItemAdded(KPIM::ProgressItem*) ),
00164                this, SIGNAL( progressItemAdded(KPIM::ProgressItem*) ) );
00165      connect ( t, SIGNAL( progressItemCanceled(KPIM::ProgressItem*) ),
00166                this, SIGNAL( progressItemCanceled(KPIM::ProgressItem*) ) );
00167      connect ( t, SIGNAL( progressItemStatus(KPIM::ProgressItem*, const QString&) ),
00168                this, SIGNAL( progressItemStatus(KPIM::ProgressItem*, const QString&) ) );
00169      connect ( t, SIGNAL( progressItemLabel(KPIM::ProgressItem*, const QString&) ),
00170                this, SIGNAL( progressItemLabel(KPIM::ProgressItem*, const QString&) ) );
00171      connect ( t, SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem*, bool) ),
00172                this, SIGNAL( progressItemUsesCrypto(KPIM::ProgressItem*, bool) ) );
00173 
00174      emit progressItemAdded( t );
00175    } else {
00176      // Hm, is this what makes the most sense?
00177      t = mTransactions[id];
00178    }
00179    return t;
00180 }
00181 
00182 ProgressItem* ProgressManager::createProgressItemImpl(
00183      const QString& parent, const QString &id,
00184      const QString &label, const QString& status,
00185      bool canBeCanceled, bool usesCrypto )
00186 {
00187    ProgressItem * p = mTransactions[parent];
00188    return createProgressItemImpl( p, id, label, status, canBeCanceled, usesCrypto );
00189 }
00190 
00191 void ProgressManager::emitShowProgressDialogImpl()
00192 {
00193    emit showProgressDialog();
00194 }
00195 
00196 
00197 // slots
00198 
00199 void ProgressManager::slotTransactionCompleted( ProgressItem *item )
00200 {
00201    mTransactions.remove( item->id() );
00202    emit progressItemCompleted( item );
00203 }
00204 
00205 void ProgressManager::slotStandardCancelHandler( ProgressItem *item )
00206 {
00207   item->setComplete();
00208 }
00209 
00210 ProgressItem* ProgressManager::singleItem() const
00211 {
00212   ProgressItem *item = 0;
00213   QDictIterator< ProgressItem > it( mTransactions );
00214   for ( ; it.current(); ++it ) {
00215     if ( !(*it)->parent() ) { // if it's a top level one, only those count
00216       if ( item )
00217         return 0; // we found more than one
00218       else
00219         item = (*it);
00220     }
00221   }
00222   return item;
00223 }
00224 
00225 void ProgressManager::slotAbortAll()
00226 {
00227   QDictIterator< ProgressItem > it( mTransactions );
00228   for ( ; it.current(); ++it ) {
00229     it.current()->cancel();
00230   }
00231 }
00232 
00233 } // namespace
00234 
00235 #include "progressmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys