OSDN Git Service

Shut up some warnings when detecting VapourSynth registry path.
[x264-launcher/x264-launcher.git] / src / tool_abstract.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #pragma once
23
24 #include <QObject>
25 #include <QUuid>
26 #include <QMutex>
27 #include <QStringList>
28
29 class OptionsModel;
30 class SysinfoModel;
31 class PreferencesModel;
32 class JobObject;
33 class QProcess;
34 class QSemaphore;
35 enum JobStatus;
36
37 // ------------------------------------------------------------
38 // Base Class
39 // ------------------------------------------------------------
40
41 class AbstractTool : public QObject
42 {
43         Q_OBJECT
44
45 public:
46         AbstractTool(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause);
47         virtual ~AbstractTool(void) {/*NOP*/}
48         
49         virtual QString getName(void) const = 0;
50
51         virtual unsigned int checkVersion(bool &modified);
52         virtual bool isVersionSupported(const unsigned int &revision, const bool &modified) = 0;
53         virtual QString printVersion(const unsigned int &revision, const bool &modified) = 0;
54
55 signals:
56         void statusChanged(const JobStatus &newStatus);
57         void progressChanged(unsigned int newProgress);
58         void messageLogged(const QString &text);
59         void detailsChanged(const QString &details);
60
61 protected:
62         static const unsigned int m_processTimeoutInterval = 2500;
63         static const unsigned int m_processTimeoutMaxCounter = 120;
64         static const unsigned int m_processTimeoutWarning = 24;
65
66         virtual QString getBinaryPath(void) const = 0;
67         virtual QStringList getExtraPaths(void) const { return QStringList(); }
68
69         virtual void checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine) = 0;
70         virtual void checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified) = 0;
71         virtual bool checkVersion_succeeded(const int &exitCode);
72
73         void log(const QString &text) { emit messageLogged(text); }
74         void setStatus(const JobStatus &newStatus) { emit statusChanged(newStatus); } 
75         void setProgress(unsigned int newProgress) { emit progressChanged(newProgress); }
76         void setDetails(const QString &text) { emit detailsChanged(text); }
77
78         bool startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels = true, const QStringList *const extraPath = NULL);
79
80         JobObject *const m_jobObject;
81         const OptionsModel *const m_options;
82         const SysinfoModel *const m_sysinfo;
83         const PreferencesModel *const m_preferences;
84         JobStatus &m_jobStatus;
85         volatile bool *const m_abort;
86         volatile bool *const m_pause;
87         QSemaphore *const m_semaphorePause;
88
89         static QString commandline2string(const QString &program, const QStringList &arguments);
90         static QStringList splitParams(const QString &params, const QString &sourceFile = QString(), const QString &outputFile = QString());
91         static QString stringToHash(const QString &string);
92         static unsigned int makeRevision(const unsigned int &core, const unsigned int &build);
93         static void splitRevision(const unsigned int &revision, unsigned int &core, unsigned int &build);
94
95         static QMutex s_mutexStartProcess;
96 };
97
98 // ------------------------------------------------------------
99 // Helper Macros
100 // ------------------------------------------------------------
101
102 #define PROCESS_PENDING_LINES(PROC, HANDLER, ...) do \
103 { \
104         while((PROC).bytesAvailable() > 0) \
105         { \
106                 QList<QByteArray> lines = (PROC).readLine().split('\r'); \
107                 while(!lines.isEmpty()) \
108                 { \
109                         const QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified(); \
110                         HANDLER(text, __VA_ARGS__); \
111                 } \
112         } \
113 } \
114 while(0)