• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

transactionsequence.cpp

00001 /*
00002     Copyright (c) 2006-2008 Volker Krause <vkrause@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "transactionsequence.h"
00021 #include "transactionjobs.h"
00022 
00023 #include "job_p.h"
00024 
00025 #include <QtCore/QSet>
00026 
00027 using namespace Akonadi;
00028 
00029 class Akonadi::TransactionSequencePrivate : public JobPrivate
00030 {
00031   public:
00032     TransactionSequencePrivate( TransactionSequence *parent )
00033       : JobPrivate( parent ),
00034         mState( Idle ),
00035         mAutoCommit( true )
00036     {
00037     }
00038 
00039     enum TransactionState
00040     {
00041       Idle,
00042       Running,
00043       WaitingForSubjobs,
00044       RollingBack,
00045       Committing
00046     };
00047 
00048     Q_DECLARE_PUBLIC( TransactionSequence )
00049 
00050     TransactionState mState;
00051     QSet<KJob*> mIgnoredErrorJobs;
00052     bool mAutoCommit;
00053 
00054     void commitResult( KJob *job )
00055     {
00056       Q_Q( TransactionSequence );
00057 
00058       if ( job->error() ) {
00059         q->setError( job->error() );
00060         q->setErrorText( job->errorText() );
00061       }
00062       q->emitResult();
00063     }
00064 
00065     void rollbackResult( KJob *job )
00066     {
00067       Q_Q( TransactionSequence );
00068 
00069       Q_UNUSED( job );
00070       q->emitResult();
00071     }
00072 };
00073 
00074 TransactionSequence::TransactionSequence( QObject * parent )
00075   : Job( new TransactionSequencePrivate( this ), parent )
00076 {
00077 }
00078 
00079 TransactionSequence::~TransactionSequence()
00080 {
00081 }
00082 
00083 bool TransactionSequence::addSubjob(KJob * job)
00084 {
00085   Q_D( TransactionSequence );
00086 
00087   if ( d->mState == TransactionSequencePrivate::Idle ) {
00088     d->mState = TransactionSequencePrivate::Running;
00089     new TransactionBeginJob( this );
00090   }
00091   return Job::addSubjob( job );
00092 }
00093 
00094 void TransactionSequence::slotResult(KJob * job)
00095 {
00096   Q_D( TransactionSequence );
00097 
00098   if ( !job->error() || d->mIgnoredErrorJobs.contains( job ) ) {
00099     // If we have an error but want to ignore it, we can't call Job::slotResult
00100     // because it would confuse the subjob queue processing logic. Just removing
00101     // the subjob instead is fine.
00102     if ( !job->error() )
00103       Job::slotResult( job );
00104     else
00105       Job::removeSubjob( job );
00106 
00107     if ( !hasSubjobs() && d->mState == TransactionSequencePrivate::WaitingForSubjobs ) {
00108       d->mState = TransactionSequencePrivate::Committing;
00109       TransactionCommitJob *job = new TransactionCommitJob( this );
00110       connect( job, SIGNAL( result( KJob* ) ), SLOT( commitResult( KJob* ) ) );
00111     }
00112   } else {
00113     setError( job->error() );
00114     setErrorText( job->errorText() );
00115     removeSubjob( job );
00116 
00117     // cancel all subjobs in case someone else is listening (such as ItemSync), but without notifying ourselves again
00118     foreach ( KJob* job, subjobs() ) {
00119       disconnect( job, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)) );
00120       job->kill( EmitResult );
00121     }
00122     clearSubjobs();
00123 
00124     if ( d->mState == TransactionSequencePrivate::Running || d->mState == TransactionSequencePrivate::WaitingForSubjobs ) {
00125       d->mState = TransactionSequencePrivate::RollingBack;
00126       TransactionRollbackJob *job = new TransactionRollbackJob( this );
00127       connect( job, SIGNAL( result( KJob* ) ), SLOT( rollbackResult( KJob* ) ) );
00128     }
00129   }
00130 }
00131 
00132 void TransactionSequence::commit()
00133 {
00134   Q_D( TransactionSequence );
00135 
00136   if ( d->mState == TransactionSequencePrivate::Running ) {
00137     d->mState = TransactionSequencePrivate::WaitingForSubjobs;
00138   } else {
00139     // we never got any subjobs, that means we never started a transaction
00140     // so we can just quit here
00141     if ( d->mState == TransactionSequencePrivate::Idle )
00142       emitResult();
00143     return;
00144   }
00145 
00146   if ( subjobs().isEmpty() ) {
00147     if ( !error() ) {
00148       d->mState = TransactionSequencePrivate::Committing;
00149       TransactionCommitJob *job = new TransactionCommitJob( this );
00150       connect( job, SIGNAL( result( KJob* ) ), SLOT( commitResult( KJob* ) ) );
00151     } else {
00152       d->mState = TransactionSequencePrivate::RollingBack;
00153       TransactionRollbackJob *job = new TransactionRollbackJob( this );
00154       connect( job, SIGNAL( result( KJob* ) ), SLOT( rollbackResult( KJob* ) ) );
00155     }
00156   }
00157 }
00158 
00159 void TransactionSequence::setIgnoreJobFailure( KJob *job )
00160 {
00161   Q_D( TransactionSequence );
00162 
00163   // make sure this is one of our sub jobs
00164   Q_ASSERT( subjobs().contains( job ) );
00165 
00166   d->mIgnoredErrorJobs.insert( job );
00167 }
00168 
00169 void TransactionSequence::doStart()
00170 {
00171   Q_D( TransactionSequence );
00172 
00173   if ( d->mAutoCommit ) {
00174     if ( d->mState == TransactionSequencePrivate::Idle )
00175       emitResult();
00176     else
00177       commit();
00178   }
00179 }
00180 
00181 void TransactionSequence::setAutomaticCommittingEnabled(bool enable)
00182 {
00183   Q_D( TransactionSequence );
00184   d->mAutoCommit = enable;
00185 }
00186 
00187 void TransactionSequence::rollback()
00188 {
00189   Q_D( TransactionSequence );
00190 
00191   setError( UserCanceled );
00192   // we never really started
00193   if ( d->mState == TransactionSequencePrivate::Idle ) {
00194     emitResult();
00195     return;
00196   }
00197 
00198   // TODO cancel not yet executed sub-jobs here
00199 
00200   d->mState = TransactionSequencePrivate::RollingBack;
00201   TransactionRollbackJob *job = new TransactionRollbackJob( this );
00202   connect( job, SIGNAL( result( KJob* ) ), SLOT( rollbackResult( KJob* ) ) );
00203 }
00204 
00205 
00206 #include "transactionsequence.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal