CuteLogger
Fast and simple logging solution for Qt based applications
abstractjob.h
1 /*
2  * Copyright (c) 2012-2025 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 <QElapsedTimer>
25 #include <QList>
26 #include <QModelIndex>
27 #include <QProcess>
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  bool isFinished() const { return (ran() && state() != QProcess::Running); }
45  void appendToLog(const QString &);
46  QString log() const;
47  QString label() const { return m_label; }
48  void setLabel(const QString &label);
49  QList<QAction *> standardActions() const { return m_standardActions; }
50  QList<QAction *> successActions() const { return m_successActions; }
51  QTime estimateRemaining(int percent);
52  QElapsedTimer time() const { return m_totalTime; }
53  void setPostJobAction(PostJobAction *action);
54  bool paused() const;
55  void setTarget(const QString &target) { m_target = target; }
56  QString target() { return m_target; }
57 
58 public slots:
59  void start(const QString &program, const QStringList &arguments);
60  virtual void start();
61  virtual void stop();
62  void pause();
63  void resume();
64 
65 signals:
66  void progressUpdated(QStandardItem *item, int percent);
67  void finished(AbstractJob *job, bool isSuccess, QString failureTime = QString());
68 
69 protected:
70  QList<QAction *> m_standardActions;
71  QList<QAction *> m_successActions;
72  QStandardItem *m_item;
73 
74 protected slots:
75  virtual void onFinished(int exitCode, QProcess::ExitStatus exitStatus = QProcess::NormalExit);
76  virtual void onReadyRead();
77  virtual void onStarted();
78 
79 private slots:
80  void onProgressUpdated(QStandardItem *, int percent);
81 
82 private:
83  bool m_ran;
84  bool m_killed;
85  QString m_log;
86  QString m_label;
87  QElapsedTimer m_estimateTime;
88  int m_startingPercent;
89  QElapsedTimer m_totalTime;
90  QScopedPointer<PostJobAction> m_postJobAction;
91  QThread::Priority m_priority;
92  QAction *m_actionPause;
93  QAction *m_actionResume;
94  bool m_isPaused;
95  QString m_target;
96 };
97 
98 #endif // ABSTRACTJOB_H