Adapted for recent changes in NVEncC progress output.
[simple-x264-launcher.git] / src / tool_abstract.h
blob18f1ce37108e48e80bc1a130264603efc9006f14
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #pragma once
24 #include <QObject>
25 #include <QUuid>
26 #include <QMutex>
27 #include <QStringList>
28 #include <QHash>
30 class OptionsModel;
31 class SysinfoModel;
32 class PreferencesModel;
33 class JobObject;
34 class QProcess;
35 class QSemaphore;
36 enum JobStatus;
38 // ------------------------------------------------------------
39 // Base Class
40 // ------------------------------------------------------------
42 class AbstractTool : public QObject
44 Q_OBJECT
46 public:
47 AbstractTool(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause);
48 virtual ~AbstractTool(void) {/*NOP*/}
50 virtual QString getName(void) const = 0;
52 virtual unsigned int checkVersion(bool &modified);
53 virtual bool isVersionSupported(const unsigned int &revision, const bool &modified) = 0;
54 virtual QString printVersion(const unsigned int &revision, const bool &modified) = 0;
56 signals:
57 void statusChanged(const JobStatus &newStatus);
58 void progressChanged(unsigned int newProgress);
59 void messageLogged(const QString &text);
60 void detailsChanged(const QString &details);
62 protected:
63 static const unsigned int m_processTimeoutInterval = 2500;
64 static const unsigned int m_processTimeoutMaxCounter = 120;
65 static const unsigned int m_processTimeoutWarning = 24;
67 virtual QString getBinaryPath(void) const = 0;
68 virtual QHash<QString, QString> getExtraEnv(void) const { return QHash<QString, QString>(); }
69 virtual QStringList getExtraPaths(void) const { return QStringList(); }
71 virtual void checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine) = 0;
72 virtual void checkVersion_parseLine(const QString &line, const QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified) = 0;
73 virtual bool checkVersion_succeeded(const int &exitCode);
75 void log(const QString &text) { emit messageLogged(text); }
76 void setStatus(const JobStatus &newStatus) { emit statusChanged(newStatus); }
77 void setProgress(unsigned int newProgress) { emit progressChanged(newProgress); }
78 void setDetails(const QString &text) { emit detailsChanged(text); }
80 bool startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels = true, const QStringList *const extraPath = NULL, const QHash<QString, QString> *const extraEnv = NULL);
82 JobObject *const m_jobObject;
83 const OptionsModel *const m_options;
84 const SysinfoModel *const m_sysinfo;
85 const PreferencesModel *const m_preferences;
86 JobStatus &m_jobStatus;
87 volatile bool *const m_abort;
88 volatile bool *const m_pause;
89 QSemaphore *const m_semaphorePause;
91 static QString commandline2string(const QString &program, const QStringList &arguments);
92 static QStringList splitParams(const QString &params, const QString &sourceFile = QString(), const QString &outputFile = QString());
93 static QString stringToHash(const QString &string);
94 static unsigned int makeRevision(const unsigned int &core, const unsigned int &build);
95 static void splitRevision(const unsigned int &revision, unsigned int &core, unsigned int &build);
97 static QMutex s_mutexStartProcess;
100 // ------------------------------------------------------------
101 // Helper Macros
102 // ------------------------------------------------------------
104 #define PROCESS_PENDING_LINES(PROC, HANDLER, ...) do \
106 while((PROC).bytesAvailable() > 0) \
108 QList<QByteArray> lines = (PROC).readLine().split('\r'); \
109 while(!lines.isEmpty()) \
111 const QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified(); \
112 HANDLER(text, __VA_ARGS__); \
116 while(0)