File indexing completed on 2024-04-14 04:51:57

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 #include "core/jobqueue.h"
0011 
0012 #include "core/scheduler.h"
0013 #include "settings.h"
0014 
0015 #include "kget_debug.h"
0016 #include <QDebug>
0017 
0018 JobQueue::JobQueue(Scheduler *parent)
0019     : QObject(parent)
0020     , m_maxSimultaneousJobs(2)
0021     , m_scheduler(parent)
0022     , m_status(Running)
0023 {
0024 }
0025 
0026 JobQueue::~JobQueue()
0027 {
0028 }
0029 
0030 const QList<Job *> JobQueue::runningJobs()
0031 {
0032     QList<Job *> jobs;
0033 
0034     iterator it = begin();
0035     iterator itEnd = end();
0036 
0037     for (; it != itEnd; ++it) {
0038         if ((*it)->status() == Job::Running)
0039             jobs.append(*it);
0040     }
0041     return jobs;
0042 }
0043 
0044 void JobQueue::setStatus(Status queueStatus)
0045 {
0046     m_status = queueStatus;
0047 
0048     // Now make sure to reset all the job policy that shouldn't
0049     // be applied anymore.
0050     iterator it = begin();
0051     iterator itEnd = end();
0052 
0053     for (; it != itEnd; ++it) {
0054         if ((m_status == JobQueue::Running) && ((*it)->status() == Job::Running)) {
0055             (*it)->setPolicy(Job::None);
0056         }
0057 
0058         if ((m_status == JobQueue::Stopped) && ((*it)->status() == Job::Stopped)) {
0059             (*it)->setPolicy(Job::None);
0060         }
0061     }
0062 
0063     m_scheduler->jobQueueChangedEvent(this, m_status);
0064 }
0065 
0066 int JobQueue::maxSimultaneousJobs() const
0067 {
0068     const int maxConnections = Settings::maxConnections();
0069     return (maxConnections ? maxConnections : 1000); // High value just to indicate no limit
0070 }
0071 
0072 void JobQueue::append(Job *job)
0073 {
0074     m_jobs.append(job);
0075 
0076     m_scheduler->jobQueueAddedJobEvent(this, job);
0077 }
0078 
0079 void JobQueue::append(const QList<Job *> &jobs)
0080 {
0081     m_jobs.append(jobs);
0082 
0083     m_scheduler->jobQueueAddedJobsEvent(this, jobs);
0084 }
0085 
0086 void JobQueue::prepend(Job *job)
0087 {
0088     m_jobs.prepend(job);
0089 
0090     m_scheduler->jobQueueAddedJobEvent(this, job);
0091 }
0092 
0093 void JobQueue::insert(Job *job, Job *after)
0094 {
0095     if ((job->jobQueue() == this) || ((after) && (after->jobQueue() != this)))
0096         return;
0097 
0098     m_jobs.insert(m_jobs.indexOf(after) + 1, job);
0099     m_scheduler->jobQueueAddedJobEvent(this, job);
0100 }
0101 
0102 void JobQueue::remove(Job *job)
0103 {
0104     m_jobs.removeAll(job);
0105 
0106     m_scheduler->jobQueueRemovedJobEvent(this, job);
0107 }
0108 
0109 void JobQueue::remove(const QList<Job *> jobs)
0110 {
0111     foreach (Job *job, jobs) {
0112         m_jobs.removeAll(job);
0113     }
0114 
0115     m_scheduler->jobQueueRemovedJobsEvent(this, jobs);
0116 }
0117 
0118 void JobQueue::move(Job *job, Job *after)
0119 {
0120     qCDebug(KGET_DEBUG) << "JobQueue::move";
0121 
0122     if ((m_jobs.removeAll(job) == 0) || (job == after) || ((after) && (after->jobQueue() != this))) {
0123         // The job doesn't belong to this JobQueue or the requested
0124         // operations doesn't make any sense since job==after
0125         return;
0126     }
0127 
0128     if (!after) {
0129         // The job must be inserted in front of the list
0130         m_jobs.prepend(job);
0131     } else {
0132         m_jobs.insert(m_jobs.indexOf(after) + 1, job);
0133     }
0134 
0135     m_scheduler->jobQueueMovedJobEvent(this, job);
0136 }
0137 
0138 #include "moc_jobqueue.cpp"