1 /*
2  * Copyright (c) 2012-2018 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 <QProcess>
23 #include <QModelIndex>
24 #include <QList>
25 #include <QTime>
26 
27 class QAction;
28 class QStandardItem;
29 
30 class AbstractJob : public QProcess
31 {
32     Q_OBJECT
33 public:
34     explicit AbstractJob(const QString& name);
~AbstractJob()35     virtual ~AbstractJob() {}
36 
37     void setStandardItem(QStandardItem* item);
38     QStandardItem* standardItem();
39     bool ran() const;
40     bool stopped() const;
41     void appendToLog(const QString&);
42     QString log() const;
label()43     QString label() const { return m_label; }
44     void setLabel(const QString& label);
standardActions()45     QList<QAction*> standardActions() const { return m_standardActions; }
successActions()46     QList<QAction*> successActions() const { return m_successActions; }
47     QTime estimateRemaining(int percent);
time()48     QTime time() const { return m_totalTime; }
49     void setPostJobAction(PostJobAction* action);
50 
51 public slots:
52     virtual void start();
53     virtual void stop();
54 
55 signals:
56     void progressUpdated(QStandardItem* item, int percent);
57     void finished(AbstractJob* job, bool isSuccess, QString failureTime = QString());
58 
59 protected:
60     QList<QAction*> m_standardActions;
61     QList<QAction*> m_successActions;
62     QStandardItem*  m_item;
63 
64 protected slots:
65     virtual void onFinished(int exitCode, QProcess::ExitStatus exitStatus = QProcess::NormalExit);
66     virtual void onReadyRead();
67     virtual void onStarted();
68 
69 private slots:
70     void onProgressUpdated(QStandardItem*, int percent);
71 
72 private:
73     bool m_ran;
74     bool m_killed;
75     QString m_log;
76     QString m_label;
77     QTime m_estimateTime;
78     int m_startingPercent;
79     QTime m_totalTime;
80     QScopedPointer<PostJobAction> m_postJobAction;
81 };
82 
83 #endif // ABSTRACTJOB_H
84