1 /* This file is part of the KDE project
3 Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
12 #include "core/jobqueue.h"
14 #include "core/scheduler.h"
19 JobQueue::JobQueue(Scheduler
* scheduler
)
20 : m_maxSimultaneousJobs(2),
21 m_scheduler(scheduler
),
24 m_scheduler
->addQueue(this);
29 m_scheduler
->delQueue(this);
32 Job
* JobQueue::operator[] (int i
) const
37 const QList
<Job
*> JobQueue::runningJobs()
41 iterator it
= begin();
42 iterator itEnd
= end();
44 for( ; it
!=itEnd
; ++it
)
46 if( (*it
)->status() == Job::Running
)
52 void JobQueue::setStatus(Status queueStatus
)
54 m_status
= queueStatus
;
56 // Now make sure to reset all the job policy that shouldn't
57 // be applied anymore.
58 iterator it
= begin();
59 iterator itEnd
= end();
61 for( ; it
!=itEnd
; ++it
)
63 if( ( m_status
== JobQueue::Running
) &&
64 ( (*it
)->status() == Job::Running
) )
66 (*it
)->setPolicy(Job::None
);
69 if( ( m_status
== JobQueue::Stopped
) &&
70 ( (*it
)->status() == Job::Stopped
) )
72 (*it
)->setPolicy(Job::None
);
76 m_scheduler
->jobQueueChangedEvent(this, m_status
);
79 int JobQueue::maxSimultaneousJobs() const
81 if(Settings::limitDownloads())
82 return Settings::maxConnections();
84 return 1000; // High value just to indicate no limit
87 void JobQueue::append(Job
* job
)
91 m_scheduler
->jobQueueAddedJobEvent(this, job
);
94 void JobQueue::prepend(Job
* job
)
98 m_scheduler
->jobQueueAddedJobEvent(this, job
);
101 void JobQueue::insert(Job
* job
, Job
* after
)
103 if((job
->jobQueue() == this) || ((after
) && (after
->jobQueue() != this)))
106 m_jobs
.insert(m_jobs
.indexOf(after
) +1, job
);
107 m_scheduler
->jobQueueAddedJobEvent(this, job
);
110 void JobQueue::remove(Job
* job
)
112 m_jobs
.removeAll(job
);
114 m_scheduler
->jobQueueRemovedJobEvent(this, job
);
117 void JobQueue::move(Job
* job
, Job
* after
)
119 kDebug(5001) << "JobQueue::move";
121 if( (m_jobs
.removeAll(job
) == 0) || (job
== after
) ||
122 ((after
) && (after
->jobQueue() != this)) )
124 //The job doesn't belong to this JobQueue or the requested
125 //operations doesn't make any sense since job==after
131 //The job must be inserted in front of the list
136 m_jobs
.insert(m_jobs
.indexOf(after
) +1, job
);
139 m_scheduler
->jobQueueMovedJobEvent(this, job
);