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::scheduleChangeReplay()
00073 {
00074 Task t;
00075 t.type = ChangeReplay;
00076 if ( mTaskList.contains( t ) )
00077 return;
00078 mTaskList << t;
00079 scheduleNext();
00080 }
00081
00082 void ResourceScheduler::taskDone()
00083 {
00084 if ( isEmpty() )
00085 emit status( AgentBase::Idle );
00086 mCurrentTask = Task();
00087 scheduleNext();
00088 }
00089
00090 bool ResourceScheduler::isEmpty()
00091 {
00092 return mTaskList.isEmpty();
00093 }
00094
00095 void ResourceScheduler::scheduleNext()
00096 {
00097 if ( mCurrentTask.type != Invalid || mTaskList.isEmpty() || !mOnline )
00098 return;
00099 QTimer::singleShot( 0, this, SLOT(executeNext()) );
00100 }
00101
00102 void ResourceScheduler::executeNext()
00103 {
00104 if( mCurrentTask.type != Invalid || mTaskList.isEmpty() )
00105 return;
00106
00107 mCurrentTask = mTaskList.takeFirst();
00108 switch ( mCurrentTask.type ) {
00109 case SyncAll:
00110 emit executeFullSync();
00111 break;
00112 case SyncCollectionTree:
00113 emit executeCollectionTreeSync();
00114 break;
00115 case SyncCollection:
00116 emit executeCollectionSync( mCurrentTask.collection );
00117 break;
00118 case FetchItem:
00119 emit executeItemFetch( mCurrentTask.item, mCurrentTask.itemParts );
00120 break;
00121 case ChangeReplay:
00122 emit executeChangeReplay();
00123 break;
00124 default:
00125 Q_ASSERT( false );
00126 }
00127 }
00128
00129 ResourceScheduler::Task ResourceScheduler::currentTask() const
00130 {
00131 return mCurrentTask;
00132 }
00133
00134 void ResourceScheduler::setOnline(bool state)
00135 {
00136 if ( mOnline == state )
00137 return;
00138 mOnline = state;
00139 if ( mOnline ) {
00140 scheduleNext();
00141 } else if ( mCurrentTask.type != Invalid ) {
00142
00143 mTaskList.prepend( mCurrentTask );
00144 mCurrentTask = Task();
00145 }
00146 }
00147
00148
00149
00150 #include "resourcescheduler.moc"