1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Tool_Abstract.h"
27 #include "JobObject.h"
30 #include <MUtils/Global.h>
31 #include <MUtils/OSSupport.h>
36 #include <QMutexLocker>
38 #include <QProcessEnvironment>
44 QScopedPointer<JobObject> AbstractTool::s_jobObject;
45 QMutex AbstractTool::s_jobObjMtx;
50 quint64 AbstractTool::s_startProcessTimer = 0ui64;
51 QMutex AbstractTool::s_startProcessMutex;
56 static const unsigned int START_DELAY = 50U; //in milliseconds
57 static const quint64 START_DELAY_NANO = quint64(START_DELAY) * 10000ui64; //in 100-nanosecond intervals
62 AbstractTool::AbstractTool(void)
64 QMutexLocker lock(&s_jobObjMtx);
66 if(s_jobObject.isNull())
68 s_jobObject.reset(new JobObject());
77 AbstractTool::~AbstractTool(void)
82 * Initialize and launch process object
84 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
86 QMutexLocker lock(&s_startProcessMutex);
88 while(MUtils::OS::current_file_time() <= s_startProcessTimer)
91 MUtils::OS::sleep_ms(START_DELAY);
95 emit messageLogged(commandline2string(program, args) + "\n");
96 MUtils::init_process(process, QFileInfo(program).absolutePath());
98 process.start(program, args);
100 if(process.waitForStarted())
104 if(!s_jobObject->addProcessToJob(&process))
106 qWarning("Failed to assign process to job object!");
110 MUtils::OS::change_process_priority(&process, -1);
114 emit statusUpdated(0);
115 m_firstLaunch = false;
118 s_startProcessTimer = MUtils::OS::current_file_time() + START_DELAY_NANO;
122 emit messageLogged("Process creation has failed :-(");
123 QString errorMsg= process.errorString().trimmed();
124 if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
127 process.waitForFinished(-1);
129 s_startProcessTimer = MUtils::OS::current_file_time() + START_DELAY_NANO;
134 * Convert program arguments to single string
136 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
138 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
140 for(int i = 0; i < arguments.count(); i++)
142 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));