Merge pull request #5296 from Chocobo1/run_ext

Replace QProgress::startDetached with std::detatch.
This commit is contained in:
sledgehammer999 2016-06-08 13:15:32 -05:00
commit 39a81f30ff
2 changed files with 48 additions and 16 deletions

View File

@ -242,6 +242,48 @@ void Application::processMessage(const QString &message)
m_paramsQueue.append(params); m_paramsQueue.append(params);
} }
void Application::runExternalProgram(BitTorrent::TorrentHandle *const torrent) const
{
QString program = Preferences::instance()->getAutoRunProgram();
program.replace("%N", torrent->name());
program.replace("%L", torrent->category());
program.replace("%F", Utils::Fs::toNativePath(torrent->contentPath()));
program.replace("%R", Utils::Fs::toNativePath(torrent->rootPath()));
program.replace("%D", Utils::Fs::toNativePath(torrent->savePath()));
program.replace("%C", QString::number(torrent->filesCount()));
program.replace("%Z", QString::number(torrent->totalSize()));
program.replace("%T", torrent->currentTracker());
program.replace("%I", torrent->hash());
Logger *logger = Logger::instance();
logger->addMessage(tr("Torrent: %1, running external program, command: %2").arg(torrent->name()).arg(program));
#if defined(Q_OS_UNIX)
QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program});
#elif defined(Q_OS_WIN) // test cmd: `echo "%F" > "c:\ab ba.txt"`
program.prepend(QLatin1String("cmd.exe /C "));
if (program.size() >= MAX_PATH) {
logger->addMessage(tr("Torrent: %1, run external program command too long (length > %2), execution failed.").arg(torrent->name()).arg(MAX_PATH), Log::CRITICAL);
return;
}
STARTUPINFOW si = {0};
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {0};
WCHAR *arg = new WCHAR[program.size() + 1];
program.toWCharArray(arg);
arg[program.size()] = L'\0';
if (CreateProcessW(NULL, arg, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
delete[] arg;
#else
QProcess::startDetached(program);
#endif
}
void Application::sendNotificationEmail(BitTorrent::TorrentHandle *const torrent) void Application::sendNotificationEmail(BitTorrent::TorrentHandle *const torrent)
{ {
// Prepare mail content // Prepare mail content
@ -266,25 +308,14 @@ void Application::torrentFinished(BitTorrent::TorrentHandle *const torrent)
Preferences *const pref = Preferences::instance(); Preferences *const pref = Preferences::instance();
// AutoRun program // AutoRun program
if (pref->isAutoRunEnabled()) { if (pref->isAutoRunEnabled())
QString program = pref->getAutoRunProgram(); runExternalProgram(torrent);
program.replace("%N", torrent->name());
program.replace("%L", torrent->category());
program.replace("%F", Utils::Fs::toNativePath(torrent->contentPath()));
program.replace("%R", Utils::Fs::toNativePath(torrent->rootPath()));
program.replace("%D", Utils::Fs::toNativePath(torrent->savePath()));
program.replace("%C", QString::number(torrent->filesCount()));
program.replace("%Z", QString::number(torrent->totalSize()));
program.replace("%T", torrent->currentTracker());
program.replace("%I", torrent->hash());
QProcess::startDetached(program);
}
// Mail notification // Mail notification
if (pref->isMailNotificationEnabled()) if (pref->isMailNotificationEnabled()) {
Logger::instance()->addMessage(tr("Torrent: %1, sending mail notification").arg(torrent->name()));
sendNotificationEmail(torrent); sendNotificationEmail(torrent);
}
} }
void Application::allTorrentsFinished() void Application::allTorrentsFinished()

View File

@ -134,6 +134,7 @@ private:
void initializeTranslation(); void initializeTranslation();
void processParams(const QStringList &params); void processParams(const QStringList &params);
void runExternalProgram(BitTorrent::TorrentHandle *const torrent) const;
void sendNotificationEmail(BitTorrent::TorrentHandle *const torrent); void sendNotificationEmail(BitTorrent::TorrentHandle *const torrent);
}; };