33 namespace MailCommon {
36 : mCurrentFolder( folder ), mImmediate( immediate )
45 :
QObject( parent ), mTimer( this ),
46 mPendingImmediateTasks( 0 ),
47 mCurrentTask( 0 ), mCurrentJob( 0 )
49 connect( &mTimer, SIGNAL(timeout()), SLOT(slotRunNextJob()) );
56 qDeleteAll( mTaskList );
68 const Akonadi::Collection folder = task->
folder();
70 TaskList::Iterator end( mTaskList.end() );
71 for ( TaskList::Iterator it = mTaskList.begin(); it != end; ++it ) {
72 if ( (*it)->taskTypeId() == typeId && (*it)->folder() == folder ) {
73 #ifdef DEBUG_SCHEDULER
74 kDebug() <<
"JobScheduler: already having task type" << typeId <<
"for folder" << folder->label();
77 if ( !mCurrentTask && immediate ) {
87 if ( !mCurrentTask && immediate )
90 #ifdef DEBUG_SCHEDULER
91 kDebug() <<
"JobScheduler: adding task" << task <<
"(type" << task->
taskTypeId()
92 <<
") for folder" << task->
folder() << task->
folder().name();
94 mTaskList.append( task );
96 ++mPendingImmediateTasks;
97 if ( !mCurrentTask && !mTimer.isActive() )
102 void JobScheduler::removeTask( TaskList::Iterator& it )
104 if ( (*it)->isImmediate() )
105 --mPendingImmediateTasks;
106 mTaskList.erase( it );
109 void JobScheduler::interruptCurrentTask()
111 Q_ASSERT( mCurrentTask );
112 #ifdef DEBUG_SCHEDULER
113 kDebug() <<
"JobScheduler: interrupting job" << mCurrentJob <<
"for folder" << mCurrentTask->
folder()->label();
121 void JobScheduler::slotRunNextJob()
123 while ( !mCurrentJob ) {
124 #ifdef DEBUG_SCHEDULER
125 kDebug() <<
"JobScheduler: slotRunNextJob";
127 Q_ASSERT( mCurrentTask == 0 );
128 ScheduledTask* task = 0;
130 TaskList::Iterator end( mTaskList.end() );
131 for ( TaskList::Iterator it = mTaskList.begin(); it != end; ++it ) {
133 const Akonadi::Collection folder = (*it)->folder();
134 if ( !folder.isValid() ) {
135 #ifdef DEBUG_SCHEDULER
136 kDebug() <<
" folder for task" << (*it) <<
"was deleted";
139 if ( !mTaskList.isEmpty() )
145 #ifdef DEBUG_SCHEDULER
146 kDebug() <<
" looking at folder" << folder.name();
160 void JobScheduler::restartTimer()
162 if ( mPendingImmediateTasks > 0 )
166 #ifdef DEBUG_SCHEDULER
167 mTimer.start( 10000 );
169 mTimer.start( 1 * 60000 );
174 void JobScheduler::runTaskNow( ScheduledTask* task )
176 Q_ASSERT( mCurrentTask == 0 );
177 if ( mCurrentTask ) {
178 interruptCurrentTask();
182 mCurrentJob = mCurrentTask->
run();
183 #ifdef DEBUG_SCHEDULER
184 kDebug() <<
"JobScheduler: task" << mCurrentTask
185 <<
"(type" << mCurrentTask->
taskTypeId() <<
")"
186 <<
"for folder" << mCurrentTask->
folder()->label()
187 <<
"returned job" << mCurrentJob
188 << ( mCurrentJob?mCurrentJob->className():0 );
190 if ( !mCurrentJob ) {
193 if ( !mTaskList.isEmpty() )
199 mCurrentTask->
folder()->storage()->addJob( mCurrentJob );
201 connect( mCurrentJob, SIGNAL(finished()),
this, SLOT(slotJobFinished()) );
202 mCurrentJob->
start();
205 void JobScheduler::slotJobFinished()
208 #ifdef DEBUG_SCHEDULER
209 kDebug() <<
"JobScheduler: slotJobFinished";
214 if ( !mTaskList.isEmpty() )
221 mPendingImmediateTasks = 0;
223 interruptCurrentTask();
235 : mImmediate( immediate )
247 #include "jobscheduler.moc"
ScheduledTask(const Akonadi::Collection &folder, bool immediate)
Create a scheduled task for a given folder If immediate is true, the scheduler will run this task as ...
A scheduled task is some information about a folder job that should be run later. ...
ScheduledJob(const Akonadi::Collection &folder, bool immediate)
virtual void kill()
Interrupt the job.
JobScheduler(QObject *parent)
void registerTask(ScheduledTask *task)
Register a task to be done for a given folder The ownership of the task is transferred to the JobSche...
Akonadi::Collection folder() const
The folder which this task is about, 0 if it was deleted meanwhile.
virtual int taskTypeId() const =0
An identifier for the type of task (a bit like QListViewItem::rtti) This allows to automatically prev...
void start()
Start the job.
Akonadi::Collection mSrcFolder
virtual ScheduledJob * run()=0
Run this task, i.e.
bool isCancellable() const