1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifndef JOB_CONTROLLER
25 #define JOB_CONTROLLER
26 
27 #include <QObject>
28 
29 class Thread;
30 class QProcess;
31 
32 class Job : public QObject
33 {
34     Q_OBJECT
35 public:
36     Job();
~Job()37     virtual ~Job() { }
38 
requestAbort()39     virtual void requestAbort() { abortRequested=true; }
40     virtual void start()=0;
41     virtual void stop()=0;
42     void setFinished(bool f);
success()43     bool success() { return finished; }
44 
45 Q_SIGNALS:
46     void exec();
47     void progress(int);
48     void done();
49 
50 protected:
51     bool abortRequested;
52     bool finished;
53 };
54 
55 class StandardJob : public Job
56 {
57     Q_OBJECT
58 public:
59     StandardJob();
~StandardJob()60     virtual ~StandardJob() { stop(); }
61 
62     virtual void start();
63     virtual void stop();
64 
65 private Q_SLOTS:
66     virtual void run() =0;
67 
68 private:
69     Thread *thread;
70 };
71 
72 class JobController : public QObject
73 {
74     Q_OBJECT
75 public:
76     static JobController * self();
77     JobController();
78 
79     void setMaxActive(int m);
80     void add(Job *job);
81     void finishedWith(Job *job);
82     void startJobs();
83     void cancel();
84 
85 private Q_SLOTS:
86     void jobDone();
87 
88 private:
89     int maxActive;
90     QList<Job *> active;
91     QList<Job *> jobs;
92 };
93 
94 #endif
95