CuteLogger
Fast and simple logging solution for Qt based applications
abstractjob.h
1 /*
2  * Copyright (c) 2012-2022 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 ABSTRACTJOB_H
19 #define ABSTRACTJOB_H
20 
21 #include "postjobaction.h"
22 #include "settings.h"
23 
24 #include <QProcess>
25 #include <QModelIndex>
26 #include <QList>
27 #include <QElapsedTimer>
28 #include <QThread>
29 
30 class QAction;
31 class QStandardItem;
32 
33 class AbstractJob : public QProcess
34 {
35  Q_OBJECT
36 public:
37  explicit AbstractJob(const QString &name, QThread::Priority priority = Settings.jobPriority());
38  virtual ~AbstractJob() {}
39 
40  void setStandardItem(QStandardItem *item);
41  QStandardItem *standardItem();
42  bool ran() const;
43  bool stopped() const;
44  void appendToLog(const QString &);
45  QString log() const;
46  QString label() const
47  {
48  return m_label;
49  }
50  void setLabel(const QString &label);
51  QList<QAction *> standardActions() const
52  {
53  return m_standardActions;
54  }
55  QList<QAction *> successActions() const
56  {
57  return m_successActions;
58  }
59  QTime estimateRemaining(int percent);
60  QElapsedTimer time() const
61  {
62  return m_totalTime;
63  }
64  void setPostJobAction(PostJobAction *action);
65 
66 public slots:
67  void start(const QString &program, const QStringList &arguments);
68  virtual void start();
69  virtual void stop();
70 
71 signals:
72  void progressUpdated(QStandardItem *item, int percent);
73  void finished(AbstractJob *job, bool isSuccess, QString failureTime = QString());
74 
75 protected:
76  QList<QAction *> m_standardActions;
77  QList<QAction *> m_successActions;
78  QStandardItem *m_item;
79 
80 protected slots:
81  virtual void onFinished(int exitCode, QProcess::ExitStatus exitStatus = QProcess::NormalExit);
82  virtual void onReadyRead();
83  virtual void onStarted();
84 
85 private slots:
86  void onProgressUpdated(QStandardItem *, int percent);
87 
88 private:
89  bool m_ran;
90  bool m_killed;
91  QString m_log;
92  QString m_label;
93  QElapsedTimer m_estimateTime;
94  int m_startingPercent;
95  QElapsedTimer m_totalTime;
96  QScopedPointer<PostJobAction> m_postJobAction;
97  QThread::Priority m_priority;
98 };
99 
100 #endif // ABSTRACTJOB_H