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