CuteLogger
Fast and simple logging solution for Qt based applications
abstractjob.h
1/*
2 * Copyright (c) 2012-2024 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
30class QAction;
31class QStandardItem;
32
33class AbstractJob : public QProcess
34{
35 Q_OBJECT
36public:
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 bool paused() const;
66
67public slots:
68 void start(const QString &program, const QStringList &arguments);
69 virtual void start();
70 virtual void stop();
71 void pause();
72 void resume();
73
74signals:
75 void progressUpdated(QStandardItem *item, int percent);
76 void finished(AbstractJob *job, bool isSuccess, QString failureTime = QString());
77
78protected:
79 QList<QAction *> m_standardActions;
80 QList<QAction *> m_successActions;
81 QStandardItem *m_item;
82
83protected slots:
84 virtual void onFinished(int exitCode, QProcess::ExitStatus exitStatus = QProcess::NormalExit);
85 virtual void onReadyRead();
86 virtual void onStarted();
87
88private slots:
89 void onProgressUpdated(QStandardItem *, int percent);
90
91private:
92 bool m_ran;
93 bool m_killed;
94 QString m_log;
95 QString m_label;
96 QElapsedTimer m_estimateTime;
97 int m_startingPercent;
98 QElapsedTimer m_totalTime;
99 QScopedPointer<PostJobAction> m_postJobAction;
100 QThread::Priority m_priority;
101 QAction *m_actionPause;
102 QAction *m_actionResume;
103};
104
105#endif // ABSTRACTJOB_H