File indexing completed on 2024-04-28 08:49:20

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 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 
0011 #ifndef JOBQUEUE_H
0012 #define JOBQUEUE_H
0013 
0014 /**
0015  * @brief JobQueue class
0016  *
0017  * This class abstracts the concept of queue. A queue is, basically, a
0018  * group of jobs that should be executed by the scheduler (if the queue
0019  * is marked as active). The scheduler will execute a maximum of n jobs
0020  * belonging to this queue at a time, where n can be set calling the
0021  * setMaxSimultaneousJobs(int n)
0022  *
0023  */
0024 
0025 #include "kget_export.h"
0026 #include <QList>
0027 #include <QObject>
0028 
0029 class Job;
0030 class Scheduler;
0031 
0032 class KGET_EXPORT JobQueue : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036     enum Status { Running, Stopped };
0037     typedef QList<Job *>::iterator iterator;
0038 
0039     JobQueue(Scheduler *parent);
0040     ~JobQueue() override;
0041 
0042     /**
0043      * Sets the JobQueue status
0044      *
0045      * @param queueStatus the new JobQueue status
0046      */
0047     virtual void setStatus(Status queueStatus);
0048 
0049     /**
0050      * @return the jobQueue status
0051      */
0052     Status status() const
0053     {
0054         return m_status;
0055     }
0056 
0057     /**
0058      * @return the begin of the job's list
0059      */
0060     iterator begin()
0061     {
0062         return m_jobs.begin();
0063     }
0064 
0065     /**
0066      * @return the end of the job's list
0067      */
0068     iterator end()
0069     {
0070         return m_jobs.end();
0071     }
0072 
0073     /**
0074      * @return the last job in the job's list
0075      */
0076     Job *last()
0077     {
0078         return m_jobs.last();
0079     }
0080 
0081     /**
0082      * @return the number of jobs in the queue
0083      */
0084     int size() const
0085     {
0086         return m_jobs.size();
0087     }
0088 
0089     /**
0090      * @param job The job for which we want to find the index
0091      *
0092      * @return the job index for the given job. If the given
0093      *         job can't be found, it returns -1
0094      */
0095     int indexOf(Job *job) const
0096     {
0097         return m_jobs.indexOf(job);
0098     }
0099 
0100     /**
0101      * @returns the Job in the queue at the given index i
0102      */
0103     Job *operator[](int i) const
0104     {
0105         return m_jobs[i];
0106     }
0107 
0108     /**
0109      * @return a list with the running Jobs
0110      */
0111     const QList<Job *> runningJobs();
0112 
0113     /**
0114      * FIXME not implemented
0115      * Sets the maximum number of jobs belonging to this queue that
0116      * should executed simultaneously by the scheduler
0117      *
0118      * @param n The maximum number of jobs
0119      */
0120     void setMaxSimultaneousJobs(int n);
0121 
0122     /**
0123      * @return the maximum number of jobs the scheduler should ever
0124      * execute simultaneously (in this queue).
0125      */
0126     int maxSimultaneousJobs() const;
0127 
0128 protected:
0129     /**
0130      * appends a job to the current queue
0131      *
0132      * @param job The job to append to the current queue
0133      */
0134     void append(Job *job);
0135 
0136     /**
0137      * appends jobs to the current queue
0138      *
0139      * @param jobs to append to the current queue
0140      */
0141     void append(const QList<Job *> &jobs);
0142 
0143     /**
0144      * prepends a job to the current queue
0145      *
0146      * @param job The job to prepend to the current queue
0147      */
0148     void prepend(Job *job);
0149 
0150     /**
0151      * inserts a job to the current queue after the given job
0152      *
0153      * @param job The job to add in the current queue
0154      * @param after The job after which to add the job
0155      */
0156     void insert(Job *job, Job *after);
0157 
0158     /**
0159      * removes a job from the current queue
0160      *
0161      * @param job The job to remove from the current queue
0162      */
0163     void remove(Job *job);
0164 
0165     /**
0166      * removes jobs from the current queue
0167      *
0168      * @param jobs The jobs to remove from the current queue
0169      */
0170     void remove(const QList<Job *> jobs);
0171 
0172     /**
0173      * Moves a job in the queue. Both the given jobs must belong to this queue
0174      *
0175      * @param job The job to move
0176      * @param after The job after which we have to move the given job
0177      */
0178     void move(Job *job, Job *after);
0179 
0180     Scheduler *scheduler()
0181     {
0182         return m_scheduler;
0183     }
0184 
0185 private:
0186     QList<Job *> m_jobs;
0187 
0188     int m_maxSimultaneousJobs;
0189 
0190     Scheduler *m_scheduler;
0191     Status m_status;
0192 };
0193 
0194 #endif