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