akonadi
resourcescheduler.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "resourcescheduler_p.h"
00021
00022 #include <kdebug.h>
00023
00024 #include <QtCore/QTimer>
00025 #include <QtDBus/QDBusInterface>
00026 #include <QtDBus/QDBusConnectionInterface>
00027
00028 using namespace Akonadi;
00029
00030 qint64 ResourceScheduler::Task::latestSerial = 0;
00031 static QDBusAbstractInterface *s_resourcetracker = 0;
00032
00033
00034
00035 ResourceScheduler::ResourceScheduler( QObject *parent ) :
00036 QObject( parent ),
00037 mOnline( false )
00038 {
00039 }
00040
00041 void ResourceScheduler::scheduleFullSync()
00042 {
00043 Task t;
00044 t.type = SyncAll;
00045 if ( !mTaskList.isEmpty() && ( mTaskList.last() == t || mCurrentTask == t ) )
00046 return;
00047 mTaskList << t;
00048 signalTaskToTracker( t, "SyncAll" );
00049 scheduleNext();
00050 }
00051
00052 void ResourceScheduler::scheduleCollectionTreeSync()
00053 {
00054 Task t;
00055 t.type = SyncCollectionTree;
00056 if ( !mTaskList.isEmpty() && ( mTaskList.last() == t || mCurrentTask == t ) )
00057 return;
00058 mTaskList << t;
00059 signalTaskToTracker( t, "SyncCollectionTree" );
00060 scheduleNext();
00061 }
00062
00063 void ResourceScheduler::scheduleSync(const Collection & col)
00064 {
00065 Task t;
00066 t.type = SyncCollection;
00067 t.collection = col;
00068 if ( !mTaskList.isEmpty() && ( mTaskList.last() == t || mCurrentTask == t ) )
00069 return;
00070 mTaskList << t;
00071 signalTaskToTracker( t, "SyncCollection" );
00072 scheduleNext();
00073 }
00074
00075 void ResourceScheduler::scheduleItemFetch(const Item & item, const QSet<QByteArray> &parts, const QDBusMessage & msg)
00076 {
00077 Task t;
00078 t.type = FetchItem;
00079 t.item = item;
00080 t.itemParts = parts;
00081 t.dbusMsg = msg;
00082 if ( !mTaskList.isEmpty() && ( mTaskList.last() == t || mCurrentTask == t ) )
00083 return;
00084 mTaskList << t;
00085 signalTaskToTracker( t, "FetchItem" );
00086 scheduleNext();
00087 }
00088
00089 void ResourceScheduler::scheduleResourceCollectionDeletion()
00090 {
00091 Task t;
00092 t.type = DeleteResourceCollection;
00093 if ( !mTaskList.isEmpty() && ( mTaskList.last() == t || mCurrentTask == t ) )
00094 return;
00095 mTaskList << t;
00096 signalTaskToTracker( t, "DeleteResourceCollection" );
00097 scheduleNext();
00098 }
00099
00100 void ResourceScheduler::scheduleChangeReplay()
00101 {
00102 Task t;
00103 t.type = ChangeReplay;
00104 if ( mTaskList.contains( t ) )
00105 return;
00106 mTaskList << t;
00107 signalTaskToTracker( t, "ChangeReplay" );
00108 scheduleNext();
00109 }
00110
00111 void Akonadi::ResourceScheduler::scheduleFullSyncCompletion()
00112 {
00113 Task t;
00114 t.type = SyncAllDone;
00115 mTaskList << t;
00116 signalTaskToTracker( t, "SyncAllDone" );
00117 scheduleNext();
00118 }
00119
00120 void ResourceScheduler::taskDone()
00121 {
00122 if ( isEmpty() )
00123 emit status( AgentBase::Idle );
00124
00125 if ( s_resourcetracker ) {
00126 QList<QVariant> argumentList;
00127 argumentList << QString::number( mCurrentTask.serial )
00128 << QString();
00129 s_resourcetracker->asyncCallWithArgumentList(QLatin1String("jobEnded"), argumentList);
00130 }
00131
00132 mCurrentTask = Task();
00133 scheduleNext();
00134 }
00135
00136 void ResourceScheduler::deferTask()
00137 {
00138 if ( s_resourcetracker ) {
00139 QList<QVariant> argumentList;
00140 argumentList << QString::number( mCurrentTask.serial )
00141 << QString();
00142 s_resourcetracker->asyncCallWithArgumentList(QLatin1String("jobEnded"), argumentList);
00143 }
00144
00145 Task t = mCurrentTask;
00146 mCurrentTask = Task();
00147 mTaskList << t;
00148 signalTaskToTracker( t, "DeferedTask" );
00149
00150 scheduleNext();
00151 }
00152
00153 bool ResourceScheduler::isEmpty()
00154 {
00155 return mTaskList.isEmpty();
00156 }
00157
00158 void ResourceScheduler::scheduleNext()
00159 {
00160 if ( mCurrentTask.type != Invalid || mTaskList.isEmpty() || !mOnline )
00161 return;
00162 QTimer::singleShot( 0, this, SLOT(executeNext()) );
00163 }
00164
00165 void ResourceScheduler::executeNext()
00166 {
00167 if( mCurrentTask.type != Invalid || mTaskList.isEmpty() )
00168 return;
00169
00170 mCurrentTask = mTaskList.takeFirst();
00171
00172 if ( s_resourcetracker ) {
00173 QList<QVariant> argumentList;
00174 argumentList << QString::number( mCurrentTask.serial );
00175 s_resourcetracker->asyncCallWithArgumentList(QLatin1String("jobStarted"), argumentList);
00176 }
00177
00178 switch ( mCurrentTask.type ) {
00179 case SyncAll:
00180 emit executeFullSync();
00181 break;
00182 case SyncCollectionTree:
00183 emit executeCollectionTreeSync();
00184 break;
00185 case SyncCollection:
00186 emit executeCollectionSync( mCurrentTask.collection );
00187 break;
00188 case FetchItem:
00189 emit executeItemFetch( mCurrentTask.item, mCurrentTask.itemParts );
00190 break;
00191 case DeleteResourceCollection:
00192 emit executeResourceCollectionDeletion();
00193 break;
00194 case ChangeReplay:
00195 emit executeChangeReplay();
00196 break;
00197 case SyncAllDone:
00198 emit fullSyncComplete();
00199 break;
00200 default:
00201 Q_ASSERT( false );
00202 }
00203 }
00204
00205 ResourceScheduler::Task ResourceScheduler::currentTask() const
00206 {
00207 return mCurrentTask;
00208 }
00209
00210 void ResourceScheduler::setOnline(bool state)
00211 {
00212 if ( mOnline == state )
00213 return;
00214 mOnline = state;
00215 if ( mOnline ) {
00216 scheduleNext();
00217 } else if ( mCurrentTask.type != Invalid ) {
00218
00219 mTaskList.prepend( mCurrentTask );
00220 mCurrentTask = Task();
00221 }
00222 }
00223
00224 void ResourceScheduler::signalTaskToTracker( const Task &task, const QByteArray &taskType )
00225 {
00226
00227 if ( !s_resourcetracker && QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.akonadiconsole") ) ) {
00228 s_resourcetracker = new QDBusInterface( QLatin1String("org.kde.akonadiconsole"),
00229 QLatin1String("/resourcesJobtracker"),
00230 QLatin1String("org.freedesktop.Akonadi.JobTracker"),
00231 QDBusConnection::sessionBus(), 0 );
00232 }
00233
00234 if ( s_resourcetracker ) {
00235 QList<QVariant> argumentList;
00236 argumentList << static_cast<AgentBase*>( parent() )->identifier()
00237 << QString::number( task.serial )
00238 << QString()
00239 << QString::fromLatin1( taskType );
00240 s_resourcetracker->asyncCallWithArgumentList(QLatin1String("jobCreated"), argumentList);
00241 }
00242 }
00243
00244
00245
00246 #include "resourcescheduler_p.moc"