From 263524e8560591107f208a7be9053f7683cf2ac5 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 12 May 2018 00:52:00 +0800 Subject: [PATCH] Improve "Run External Program" behavior This follows utorrent behavior: they don't append backslash character at the end of path variables. Closes #8836. --- src/app/application.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index 7eb98c0d8..cd37f0a4c 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -290,9 +290,21 @@ void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) c std::sort(tags.begin(), tags.end(), Utils::String::naturalLessThan); program.replace("%G", tags.join(',')); +#if defined(Q_OS_WIN) + const auto chopPathSep = [](const QString &str) -> QString + { + if (str.endsWith('\\')) + return str.mid(0, (str.length() -1)); + return str; + }; + program.replace("%F", chopPathSep(Utils::Fs::toNativePath(torrent->contentPath()))); + program.replace("%R", chopPathSep(Utils::Fs::toNativePath(torrent->rootPath()))); + program.replace("%D", chopPathSep(Utils::Fs::toNativePath(torrent->savePath()))); +#else program.replace("%F", Utils::Fs::toNativePath(torrent->contentPath())); program.replace("%R", Utils::Fs::toNativePath(torrent->rootPath())); program.replace("%D", Utils::Fs::toNativePath(torrent->savePath())); +#endif program.replace("%C", QString::number(torrent->filesCount())); program.replace("%Z", QString::number(torrent->totalSize())); program.replace("%T", torrent->currentTracker()); @@ -301,9 +313,7 @@ void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) c Logger *logger = Logger::instance(); logger->addMessage(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program)); -#if defined(Q_OS_UNIX) - QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program}); -#else +#if defined(Q_OS_WIN) std::unique_ptr programWchar(new wchar_t[program.length() + 1] {}); program.toWCharArray(programWchar.get()); @@ -320,6 +330,8 @@ void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) c QProcess::startDetached(QString::fromWCharArray(args[0]), argList); ::LocalFree(args); +#else + QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program}); #endif }