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.h"
00021
00022 #include <kdebug.h>
00023
00024 #include <QtCore/QTimer>
00025
00026 using namespace Akonadi;
00027
00028
00029
00030 ResourceScheduler::ResourceScheduler( QObject *parent ) :
00031 QObject( parent ),
00032 mOnline( false )
00033 {
00034 }
00035
00036 void ResourceScheduler::scheduleFullSync()
00037 {
00038 Task t;
00039 t.type = SyncAll;
00040 mTaskList << t;
00041 scheduleNext();
00042 }
00043
00044 void ResourceScheduler::scheduleCollectionTreeSync()
00045 {
00046 Task t;
00047 t.type = SyncCollectionTree;
00048 mTaskList << t;
00049 scheduleNext();
00050 }
00051
00052 void ResourceScheduler::scheduleSync(const Collection & col)
00053 {
00054 Task t;
00055 t.type = SyncCollection;
00056 t.collection = col;
00057 mTaskList << t;
00058 scheduleNext();
00059 }
00060
00061 void ResourceScheduler::scheduleItemFetch(const Item & item, const QSet<QByteArray> &parts, const QDBusMessage & msg)
00062 {
00063 Task t;
00064 t.type = FetchItem;
00065 t.item = item;
00066 t.itemParts = parts;
00067 t.dbusMsg = msg;
00068 mTaskList << t;
00069 scheduleNext();
00070 }
00071
00072 void ResourceScheduler::scheduleResourceCollectionDeletion()
00073 {
00074 Task t;
00075 t.type = DeleteResourceCollection;
00076 mTaskList << t;
00077 scheduleNext();
00078 }
00079
00080 void ResourceScheduler::scheduleChangeReplay()
00081 {
00082 Task t;
00083 t.type = ChangeReplay;
00084 if ( mTaskList.contains( t ) )
00085 return;
00086 mTaskList << t;
00087 scheduleNext();
00088 }
00089
00090 void ResourceScheduler::taskDone()
00091 {
00092 if ( isEmpty() )
00093 emit status( AgentBase::Idle );
00094 mCurrentTask = Task();
00095 scheduleNext();
00096 }
00097
00098 bool ResourceScheduler::isEmpty()
00099 {
00100 return mTaskList.isEmpty();
00101 }
00102
00103 void ResourceScheduler::scheduleNext()
00104 {
00105 if ( mCurrentTask.type != Invalid || mTaskList.isEmpty() || !mOnline )
00106 return;
00107 QTimer::singleShot( 0, this, SLOT(executeNext()) );
00108 }
00109
00110 void ResourceScheduler::executeNext()
00111 {
00112 if( mCurrentTask.type != Invalid || mTaskList.isEmpty() )
00113 return;
00114
00115 mCurrentTask = mTaskList.takeFirst();
00116 switch ( mCurrentTask.type ) {
00117 case SyncAll:
00118 emit executeFullSync();
00119 break;
00120 case SyncCollectionTree:
00121 emit executeCollectionTreeSync();
00122 break;
00123 case SyncCollection:
00124 emit executeCollectionSync( mCurrentTask.collection );
00125 break;
00126 case FetchItem:
00127 emit executeItemFetch( mCurrentTask.item, mCurrentTask.itemParts );
00128 break;
00129 case DeleteResourceCollection:
00130 emit executeResourceCollectionDeletion();
00131 break;
00132 case ChangeReplay:
00133 emit executeChangeReplay();
00134 break;
00135 default:
00136 Q_ASSERT( false );
00137 }
00138 }
00139
00140 ResourceScheduler::Task ResourceScheduler::currentTask() const
00141 {
00142 return mCurrentTask;
00143 }
00144
00145 void ResourceScheduler::setOnline(bool state)
00146 {
00147 if ( mOnline == state )
00148 return;
00149 mOnline = state;
00150 if ( mOnline ) {
00151 scheduleNext();
00152 } else if ( mCurrentTask.type != Invalid ) {
00153
00154 mTaskList.prepend( mCurrentTask );
00155 mCurrentTask = Task();
00156 }
00157 }
00158
00159
00160
00161 #include "resourcescheduler.moc"