Relicense all GPLv2 only code to GPLv2+.
[kdenetwork.git] / kget / core / jobqueue.cpp
blobfb4b4b45460f5b5c375d5e8c53ec838c2d89ad0c
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.
9 */
12 #include "core/jobqueue.h"
14 #include "core/scheduler.h"
15 #include "settings.h"
17 #include <kdebug.h>
19 JobQueue::JobQueue(Scheduler * scheduler)
20 : m_maxSimultaneousJobs(2),
21 m_scheduler(scheduler),
22 m_status(Stopped)
24 m_scheduler->addQueue(this);
27 JobQueue::~JobQueue()
29 m_scheduler->delQueue(this);
32 Job * JobQueue::operator[] (int i) const
34 return m_jobs[i];
37 const QList<Job *> JobQueue::runningJobs()
39 QList<Job *> jobs;
41 iterator it = begin();
42 iterator itEnd = end();
44 for( ; it!=itEnd ; ++it )
46 if( (*it)->status() == Job::Running )
47 jobs.append(*it);
49 return jobs;
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();
83 else
84 return 1000; // High value just to indicate no limit
87 void JobQueue::append(Job * job)
89 m_jobs.append(job);
91 m_scheduler->jobQueueAddedJobEvent(this, job);
94 void JobQueue::prepend(Job * job)
96 m_jobs.prepend(job);
98 m_scheduler->jobQueueAddedJobEvent(this, job);
101 void JobQueue::insert(Job * job, Job * after)
103 if((job->jobQueue() == this) || ((after) && (after->jobQueue() != this)))
104 return;
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
126 return;
129 if(!after)
131 //The job must be inserted in front of the list
132 m_jobs.prepend(job);
134 else
136 m_jobs.insert(m_jobs.indexOf(after) +1, job);
139 m_scheduler->jobQueueMovedJobEvent(this, job);