1 /*
2  * Copyright (c) 2012-2019 Meltytech, LLC
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef JOBQUEUE_H
19 #define JOBQUEUE_H
20 
21 #include "jobs/abstractjob.h"
22 #include <QStandardItemModel>
23 #include <QMutex>
24 
25 class JobQueue : public QStandardItemModel
26 {
27     Q_OBJECT
28 protected:
29     JobQueue(QObject *parent);
30     void startNextJob();
31 
32 public:
33     enum ColumnRole {
34         COLUMN_ICON,
35         COLUMN_OUTPUT,
36         COLUMN_STATUS,
37         COLUMN_COUNT
38     };
39 
40     static JobQueue& singleton(QObject* parent = 0);
41     void cleanup();
42     AbstractJob* add(AbstractJob *job);
43     AbstractJob* jobFromIndex(const QModelIndex& index) const;
44     void pause();
45     void resume();
46     bool isPaused() const;
47     bool hasIncomplete() const;
48     void remove(const QModelIndex& index);
49     void removeFinished();
jobs()50     QList<AbstractJob*> jobs() const { return m_jobs; }
51 
52 signals:
53     void jobAdded();
54 
55 public slots:
56     void onProgressUpdated(QStandardItem* standardItem, int percent);
57     void onFinished(AbstractJob* job, bool isSuccess, QString time);
58 
59 private:
60     QList<AbstractJob*> m_jobs;
61     QMutex m_mutex; // protects m_jobs
62     bool m_paused;
63 };
64 
65 #define JOBS JobQueue::singleton()
66 
67 #endif // JOBQUEUE_H
68