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