From 6a862013eb1aaf6baff33686417fe287ed20c234 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Tue, 29 Oct 2013 02:05:43 +0100 Subject: [PATCH] Added lamexp_init_process() function, which can be used to setup a QProcess object. --- src/Config.h | 2 +- src/Dialog_MainWindow.cpp | 25 ++++++++-------- src/Dialog_Update.cpp | 13 ++++----- src/Global.cpp | 50 +++++++++++++++++++++++++++++++- src/Global.h | 2 ++ src/Thread_CueSplitter.cpp | 5 ++-- src/Thread_FileAnalyzer_Task.cpp | 8 ++--- src/Thread_Initialization.cpp | 13 +++++---- src/Tool_Abstract.cpp | 9 +----- 9 files changed, 84 insertions(+), 43 deletions(-) diff --git a/src/Config.h b/src/Config.h index 7cdfefd1..e42eff2c 100644 --- a/src/Config.h +++ b/src/Config.h @@ -35,7 +35,7 @@ #define VER_LAMEXP_MINOR_LO 9 #define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_PATCH 5 -#define VER_LAMEXP_BUILD 1435 +#define VER_LAMEXP_BUILD 1437 #define VER_LAMEXP_CONFG 1348 /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Dialog_MainWindow.cpp b/src/Dialog_MainWindow.cpp index ce585594..c77b86cc 100644 --- a/src/Dialog_MainWindow.cpp +++ b/src/Dialog_MainWindow.cpp @@ -3868,30 +3868,31 @@ void MainWindow::showCustomParamsHelpScreen(const QString &toolName, const QStri return; } - QProcess *process = new QProcess(); - process->setProcessChannelMode(QProcess::MergedChannels); - process->setReadChannel(QProcess::StandardOutput); - process->start(binary, command.isEmpty() ? QStringList() : QStringList() << command); + QProcess process; + lamexp_init_process(process, QFileInfo(binary).absolutePath()); + + process.start(binary, command.isEmpty() ? QStringList() : QStringList() << command); + qApp->setOverrideCursor(QCursor(Qt::WaitCursor)); - if(process->waitForStarted(15000)) + if(process.waitForStarted(15000)) { qApp->processEvents(); - process->waitForFinished(15000); + process.waitForFinished(15000); } - if(process->state() != QProcess::NotRunning) + if(process.state() != QProcess::NotRunning) { - process->kill(); - process->waitForFinished(-1); + process.kill(); + process.waitForFinished(-1); } qApp->restoreOverrideCursor(); QStringList output; bool spaceFlag = true; - while(process->canReadLine()) + while(process.canReadLine()) { - QString temp = QString::fromUtf8(process->readLine()); + QString temp = QString::fromUtf8(process.readLine()); TRIM_STRING_RIGHT(temp); if(temp.isEmpty()) { @@ -3903,8 +3904,6 @@ void MainWindow::showCustomParamsHelpScreen(const QString &toolName, const QStri } } - LAMEXP_DELETE(process); - if(output.count() < 1) { qWarning("Empty output, cannot show help screen!"); diff --git a/src/Dialog_Update.cpp b/src/Dialog_Update.cpp index be5ff383..adbb8eaa 100644 --- a/src/Dialog_Update.cpp +++ b/src/Dialog_Update.cpp @@ -31,6 +31,7 @@ #include "Dialog_LogView.h" #include "Model_Settings.h" #include "WinSevenTaskbar.h" +#include "Tool_Abstract.h" //Qt includes #include @@ -649,9 +650,7 @@ bool UpdateDialog::getFile(const QString &url, const QString &outFile, unsigned } QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); - process.setWorkingDirectory(output.absolutePath()); + lamexp_init_process(process, output.absolutePath()); QStringList args; args << "--no-cache" << "--no-dns-cache" << QString().sprintf("--max-redirect=%u", maxRedir); @@ -724,9 +723,7 @@ bool UpdateDialog::checkSignature(const QString &file, const QString &signature) } QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); - process.setWorkingDirectory(QFileInfo(file).absolutePath()); + lamexp_init_process(process, QFileInfo(file).absolutePath()); QEventLoop loop; connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit())); @@ -884,6 +881,8 @@ void UpdateDialog::applyUpdate(void) QStringList args; QEventLoop loop; + lamexp_init_process(process, QFileInfo(m_binaryUpdater).absolutePath()); + connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit())); connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit())); @@ -903,7 +902,7 @@ void UpdateDialog::applyUpdate(void) if(updateStarted) { m_updaterProcess = lamexp_process_id(&process); - loop.exec(); + loop.exec(QEventLoop::ExcludeUserInputEvents); } m_updaterProcess = NULL; QApplication::restoreOverrideCursor(); diff --git a/src/Global.cpp b/src/Global.cpp index 1172235b..51cfce37 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -2846,7 +2846,14 @@ bool lamexp_change_process_priority(const int priority) */ bool lamexp_change_process_priority(const QProcess *proc, const int priority) { - return lamexp_change_process_priority(proc->pid()->hProcess, priority); + if(Q_PID qPid = proc->pid()) + { + return lamexp_change_process_priority(qPid->hProcess, priority); + } + else + { + return false; + } } /* @@ -3045,6 +3052,47 @@ bool lamexp_open_media_file(const QString &mediaFilePath) return false; } +/* + * Setup QPorcess object + */ +void lamexp_init_process(QProcess &process, const QString &wokringDir) +{ + //Environment variable names + static const char *const s_envvar_names[] = + { + "WGETRC", "SYSTEM_WGETRC", "HTTP_PROXY", "FTP_PROXY", "NO_PROXY", "GNUPGHOME", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LANG", NULL + }; + + const QString tempDir = QDir::toNativeSeparators(lamexp_temp_folder2()); + + QProcessEnvironment env = process.processEnvironment(); + if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment(); + + //Setup TEMP directory + env.insert("TEMP", tempDir); + env.insert("TMP", tempDir); + env.insert("TMPDIR", tempDir); + env.insert("HOME", tempDir); + env.insert("USERPROFILE", tempDir); + env.insert("HOMEPATH", tempDir); + + //Setup PATH variable + const QString path = env.value("PATH", QString()); + env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path)); + + //Clean a number of enviroment variables that might affect our tools + for(size_t i = 0; s_envvar_names[i]; i++) + { + env.remove(QString::fromLatin1(s_envvar_names[i])); + env.remove(QString::fromLatin1(s_envvar_names[i]).toLower()); + } + + process.setWorkingDirectory(wokringDir); + process.setProcessChannelMode(QProcess::MergedChannels); + process.setReadChannel(QProcess::StandardOutput); + process.setProcessEnvironment(env); +} + /* * Entry point checks */ diff --git a/src/Global.h b/src/Global.h index 110c5d63..018b2a66 100644 --- a/src/Global.h +++ b/src/Global.h @@ -218,8 +218,10 @@ unsigned __int64 lamexp_current_file_time(void); void lamexp_natural_string_sort(QStringList &list, const bool bIgnoreCase); bool lamexp_open_media_file(const QString &mediaFilePath); QString lamexp_path_to_short(const QString &longPath); +void lamexp_init_process(QProcess &process, const QString &wokringDir); void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL); + //Debug-only functions unsigned long lamexp_dbg_private_bytes(void); diff --git a/src/Thread_CueSplitter.cpp b/src/Thread_CueSplitter.cpp index 51532978..6eac0511 100644 --- a/src/Thread_CueSplitter.cpp +++ b/src/Thread_CueSplitter.cpp @@ -297,9 +297,8 @@ void CueSplitter::splitFile(const QString &output, const int trackNo, const QStr QRegExp rxDuration("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d).(\\d\\d)", Qt::CaseInsensitive); QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); - process.setWorkingDirectory(m_outputDir); + lamexp_init_process(process, m_outputDir); + process.start(m_soxBin, args); if(!process.waitForStarted()) diff --git a/src/Thread_FileAnalyzer_Task.cpp b/src/Thread_FileAnalyzer_Task.cpp index 4e35d023..fc245c42 100644 --- a/src/Thread_FileAnalyzer_Task.cpp +++ b/src/Thread_FileAnalyzer_Task.cpp @@ -180,8 +180,8 @@ const AudioFileModel AnalyzeTask::analyzeFile(const QString &filePath, int *type params << QDir::toNativeSeparators(filePath); QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); + lamexp_init_process(process, QFileInfo(m_mediaInfoBin).absolutePath()); + process.start(m_mediaInfoBin, params); if(!process.waitForStarted()) @@ -513,8 +513,8 @@ void AnalyzeTask::retrieveCover(AudioFileModel &audioFile, cover_t coverType, co bool AnalyzeTask::analyzeAvisynthFile(const QString &filePath, AudioFileModel &info) { QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); + lamexp_init_process(process, QFileInfo(m_avs2wavBin).absolutePath()); + process.start(m_avs2wavBin, QStringList() << QDir::toNativeSeparators(filePath) << "?"); if(!process.waitForStarted()) diff --git a/src/Thread_Initialization.cpp b/src/Thread_Initialization.cpp index 84dc50cc..bcd18cd8 100644 --- a/src/Thread_Initialization.cpp +++ b/src/Thread_Initialization.cpp @@ -24,6 +24,7 @@ #include "LockedFile.h" #include "Tools.h" +#include "Tool_Abstract.h" #include #include @@ -463,8 +464,8 @@ void InitializationThread::initNeroAac(void) } QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); + lamexp_init_process(process, neroFileInfo[0].absolutePath()); + process.start(neroFileInfo[0].canonicalFilePath(), QStringList() << "-help"); if(!process.waitForStarted()) @@ -569,8 +570,8 @@ void InitializationThread::initFhgAac(void) } QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); + lamexp_init_process(process, fhgFileInfo[0].absolutePath()); + process.start(fhgFileInfo[0].canonicalFilePath(), QStringList() << "--version"); if(!process.waitForStarted()) @@ -667,8 +668,8 @@ void InitializationThread::initQAac(void) } QProcess process; - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); + lamexp_init_process(process, qaacFileInfo[0].absolutePath()); + process.start(qaacFileInfo[0].canonicalFilePath(), QStringList() << "--check"); if(!process.waitForStarted()) diff --git a/src/Tool_Abstract.cpp b/src/Tool_Abstract.cpp index 4147af35..a54fda3c 100644 --- a/src/Tool_Abstract.cpp +++ b/src/Tool_Abstract.cpp @@ -96,15 +96,8 @@ bool AbstractTool::startProcess(QProcess &process, const QString &program, const } emit messageLogged(commandline2string(program, args) + "\n"); + lamexp_init_process(process, QFileInfo(program).absolutePath()); - QProcessEnvironment env = process.processEnvironment(); - if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment(); - env.insert("TEMP", QDir::toNativeSeparators(lamexp_temp_folder2())); - env.insert("TMP", QDir::toNativeSeparators(lamexp_temp_folder2())); - process.setProcessEnvironment(env); - - process.setProcessChannelMode(QProcess::MergedChannels); - process.setReadChannel(QProcess::StandardOutput); process.start(program, args); if(process.waitForStarted())