1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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 #include "tool_abstract.h"
26 #include "model_options.h"
27 #include "model_preferences.h"
28 #include "model_sysinfo.h"
29 #include "job_object.h"
32 #include <MUtils/OSSupport.h>
36 #include <QMutexLocker>
38 #include <QCryptographicHash>
40 QMutex AbstractTool::s_mutexStartProcess;
42 // ------------------------------------------------------------
43 // Constructor & Destructor
44 // ------------------------------------------------------------
46 AbstractTool::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 m_jobObject(jobObject),
51 m_preferences(preferences),
52 m_jobStatus(jobStatus),
55 m_semaphorePause(semaphorePause)
57 /*nothing to do here*/
60 // ------------------------------------------------------------
62 // ------------------------------------------------------------
64 unsigned int AbstractTool::checkVersion(bool &modified)
66 if(m_preferences->getSkipVersionTest())
68 log("Warning: Skipping the version check this time!");
69 return makeRevision(0xFFF0, 0xFFF0);
73 QList<QRegExp*> patterns;
76 //Init encoder-specific values
77 checkVersion_init(patterns, cmdLine);
79 log("Creating process:");
80 if(!startProcess(process, getBinaryPath(), cmdLine))
85 bool bTimeout = false;
86 bool bAborted = false;
88 unsigned int revision = UINT_MAX;
89 unsigned int coreVers = UINT_MAX;
92 while(process.state() != QProcess::NotRunning)
100 if(!process.waitForReadyRead())
102 if(process.state() == QProcess::Running)
105 qWarning("process timed out <-- killing!");
106 log("\nPROCESS TIMEOUT !!!");
111 PROCESS_PENDING_LINES(process, checkVersion_parseLine, patterns, coreVers, revision, modified);
114 if(!(bTimeout || bAborted))
116 PROCESS_PENDING_LINES(process, checkVersion_parseLine, patterns, coreVers, revision, modified);
119 process.waitForFinished();
120 if(process.state() != QProcess::NotRunning)
123 process.waitForFinished(-1);
126 while(!patterns.isEmpty())
128 QRegExp *pattern = patterns.takeFirst();
129 MUTILS_DELETE(pattern);
132 if(bTimeout || bAborted || (!checkVersion_succeeded(process.exitCode())))
134 if(!(bTimeout || bAborted))
136 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
141 if((revision == UINT_MAX) || (coreVers == UINT_MAX))
143 log(tr("\nFAILED TO DETERMINE VERSION INFO !!!"));
147 return makeRevision(coreVers, revision);
150 bool AbstractTool::checkVersion_succeeded(const int &exitCode)
152 return (exitCode == EXIT_SUCCESS);
155 // ------------------------------------------------------------
157 // ------------------------------------------------------------
159 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
161 QMutexLocker lock(&s_mutexStartProcess);
162 log(commandline2string(program, args) + "\n");
164 process.setWorkingDirectory(QDir::tempPath());
168 process.setProcessChannelMode(QProcess::MergedChannels);
169 process.setReadChannel(QProcess::StandardOutput);
173 process.setProcessChannelMode(QProcess::SeparateChannels);
174 process.setReadChannel(QProcess::StandardError);
177 process.start(program, args);
179 if(process.waitForStarted())
181 m_jobObject->addProcessToJob(&process);
182 MUtils::OS::change_process_priority(&process, m_preferences->getProcessPriority());
187 log("Process creation has failed :-(");
188 QString errorMsg= process.errorString().trimmed();
189 if(!errorMsg.isEmpty()) log(errorMsg);
192 process.waitForFinished(-1);
196 // ------------------------------------------------------------
198 // ------------------------------------------------------------
200 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
202 const QString nativeProgfram = QDir::toNativeSeparators(program);
203 QString commandline = (nativeProgfram.contains(' ') ? QString("\"%1\"").arg(nativeProgfram) : nativeProgfram);
205 for(int i = 0; i < arguments.count(); i++)
207 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
213 QString AbstractTool::stringToHash(const QString &string)
215 QByteArray result(10, char(0));
216 const QByteArray hash = QCryptographicHash::hash(string.toUtf8(), QCryptographicHash::Sha1);
218 if((hash.size() == 20) && (result.size() == 10))
220 unsigned char *out = reinterpret_cast<unsigned char*>(result.data());
221 const unsigned char *in = reinterpret_cast<const unsigned char*>(hash.constData());
222 for(int i = 0; i < 10; i++)
224 out[i] = (in[i] ^ in[10+i]);
228 return QString::fromLatin1(result.toHex().constData());
231 unsigned int AbstractTool::makeRevision(const unsigned int &core, const unsigned int &build)
233 return ((core & 0x0000FFFF) << 16) | (build & 0x0000FFFF);
236 void AbstractTool::splitRevision(const unsigned int &revision, unsigned int &core, unsigned int &build)
238 core = (revision & 0xFFFF0000) >> 16;
239 build = (revision & 0x0000FFFF);