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
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
58public 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
65signals:
66 void progressUpdated(QStandardItem *item, int percent);
67 void finished(AbstractJob *job, bool isSuccess, QString failureTime = QString());
68
69protected:
70 void setKilled(bool = true);
71 QList<QAction *> m_standardActions;
72 QList<QAction *> m_successActions;
73 QStandardItem *m_item;
74
75protected slots:
76 virtual void onFinished(int exitCode, QProcess::ExitStatus exitStatus = QProcess::NormalExit);
77 virtual void onReadyRead();
78 virtual void onStarted();
79
80private slots:
81 void onProgressUpdated(QStandardItem *, int percent);
82
83private:
84 bool m_ran;
85 bool m_killed;
86 QString m_log;
87 QString m_label;
88 QElapsedTimer m_estimateTime;
89 int m_startingPercent;
90 QElapsedTimer m_totalTime;
91 QScopedPointer<PostJobAction> m_postJobAction;
92 QThread::Priority m_priority;
93 QAction *m_actionPause;
94 QAction *m_actionResume;
95 bool m_isPaused;
96 QString m_target;
97};
98
99#endif // ABSTRACTJOB_H