29 #include "jobscheduler.h"
31 #include "folderstorage.h"
32 #include "kmfoldermgr.h"
35 using namespace KMail;
37 JobScheduler::JobScheduler( TQObject* parent,
const char* name )
38 : TQObject( parent, name ), mTimer( this,
"mTimer" ),
39 mPendingImmediateTasks( 0 ),
40 mCurrentTask( 0 ), mCurrentJob( 0 )
42 connect( &mTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotRunNextJob() ) );
47 JobScheduler::~JobScheduler()
50 for( TaskList::Iterator it = mTaskList.begin(); it != mTaskList.end(); ++it ) {
59 bool immediate = task->isImmediate();
64 for( TaskList::Iterator it = mTaskList.begin(); it != mTaskList.end(); ++it ) {
65 if ( (*it)->taskTypeId() == typeId && (*it)->folder() == folder ) {
66 #ifdef DEBUG_SCHEDULER
67 kdDebug(5006) <<
"JobScheduler: already having task type " << typeId <<
" for folder " << folder->
label() << endl;
70 if ( !mCurrentTask && immediate ) {
80 if ( !mCurrentTask && immediate )
83 #ifdef DEBUG_SCHEDULER
84 kdDebug(5006) <<
"JobScheduler: adding task " << task <<
" (type " << task->
taskTypeId()
87 mTaskList.append( task );
89 ++mPendingImmediateTasks;
90 if ( !mCurrentTask && !mTimer.isActive() )
95 void JobScheduler::removeTask( TaskList::Iterator& it )
97 if ( (*it)->isImmediate() )
98 --mPendingImmediateTasks;
99 mTaskList.remove( it );
104 if ( mCurrentTask && mCurrentTask->
folder() == folder ) {
105 if ( mCurrentJob->isOpeningFolder() ) {
106 #ifdef DEBUG_SCHEDULER
107 kdDebug(5006) <<
"JobScheduler: got the opening-notification for " << folder->
label() <<
" as expected." << endl;
113 if ( mCurrentJob->isCancellable() )
114 interruptCurrentTask();
119 void JobScheduler::interruptCurrentTask()
121 Q_ASSERT( mCurrentTask );
122 #ifdef DEBUG_SCHEDULER
123 kdDebug(5006) <<
"JobScheduler: interrupting job " << mCurrentJob <<
" for folder " << mCurrentTask->
folder()->
label() << endl;
131 void JobScheduler::slotRunNextJob()
133 while ( !mCurrentJob ) {
134 #ifdef DEBUG_SCHEDULER
135 kdDebug(5006) <<
"JobScheduler: slotRunNextJob" << endl;
137 Q_ASSERT( mCurrentTask == 0 );
140 for( TaskList::Iterator it = mTaskList.begin(); it != mTaskList.end(); ++it ) {
144 #ifdef DEBUG_SCHEDULER
145 kdDebug(5006) <<
" folder for task " << (*it) <<
" was deleted" << endl;
148 if ( !mTaskList.isEmpty() )
156 kmkernel->searchFolderMgr()->tryReleasingFolder( folder );
157 #ifdef DEBUG_SCHEDULER
158 kdDebug(5006) <<
" looking at folder " << folder->
label()
160 <<
" isOpened=" << (*it)->folder()->isOpened() << endl;
176 void JobScheduler::restartTimer()
178 if ( mPendingImmediateTasks > 0 )
182 #ifdef DEBUG_SCHEDULER
183 mTimer.start( 10000 );
185 mTimer.start( 1 * 60000 );
192 Q_ASSERT( mCurrentTask == 0 );
193 if ( mCurrentTask ) {
194 interruptCurrentTask();
198 mCurrentJob = mCurrentTask->
run();
199 #ifdef DEBUG_SCHEDULER
200 kdDebug(5006) <<
"JobScheduler: task " << mCurrentTask
201 <<
" (type " << mCurrentTask->
taskTypeId() <<
")"
202 <<
" for folder " << mCurrentTask->
folder()->
label()
203 <<
" returned job " << mCurrentJob <<
" "
204 << ( mCurrentJob?mCurrentJob->className():0 ) << endl;
206 if ( !mCurrentJob ) {
209 if ( !mTaskList.isEmpty() )
214 mCurrentTask->
folder()->storage()->
addJob( mCurrentJob );
215 connect( mCurrentJob, TQ_SIGNAL( finished() ),
this, TQ_SLOT( slotJobFinished() ) );
216 mCurrentJob->start();
219 void JobScheduler::slotJobFinished()
222 #ifdef DEBUG_SCHEDULER
223 kdDebug(5006) <<
"JobScheduler: slotJobFinished" << endl;
228 if ( !mTaskList.isEmpty() )
233 void JobScheduler::pause()
235 mPendingImmediateTasks = 0;
236 if ( mCurrentJob && mCurrentJob->isCancellable() )
237 interruptCurrentTask();
241 void JobScheduler::resume()
248 KMail::ScheduledJob::ScheduledJob(
KMFolder* folder,
bool immediate )
249 : FolderJob( 0, tOther, folder ), mImmediate( immediate ),
250 mOpeningFolder( false )
256 #include "jobscheduler.moc"
void addJob(FolderJob *) const
Add job for this folder.
bool isOpened() const
Test if folder is opened.
virtual TQString label() const
Returns the label of the folder for visualization.
TQString location() const
Returns full path to folder file.
void notifyOpeningFolder(KMFolder *folder)
Called by [implementations of] FolderStorage::open() Interrupt any running job for this folder and re...
void registerTask(ScheduledTask *task)
Register a task to be done for a given folder The ownership of the task is transferred to the JobSche...
A scheduled task is some information about a folder job that should be run later.
KMFolder * 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 TQListViewItem::rtti) This allows to automatically pre...
virtual ScheduledJob * run()=0
Run this task, i.e.