Browse Source

Merge pull request #14516 from Chocobo1/replace

Replace parameters in one step
adaptive-webui-19844
Mike Tzou 4 years ago committed by GitHub
parent
commit
aad6b69f00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 88
      src/app/application.cpp

88
src/app/application.cpp

@ -313,14 +313,6 @@ void Application::processMessage(const QString &message)
void Application::runExternalProgram(const BitTorrent::Torrent *torrent) const void Application::runExternalProgram(const BitTorrent::Torrent *torrent) const
{ {
QString program = Preferences::instance()->getAutoRunProgram().trimmed();
program.replace("%N", torrent->name());
program.replace("%L", torrent->category());
QStringList tags = torrent->tags().values();
std::sort(tags.begin(), tags.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>);
program.replace("%G", tags.join(','));
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
const auto chopPathSep = [](const QString &str) -> QString const auto chopPathSep = [](const QString &str) -> QString
{ {
@ -328,22 +320,74 @@ void Application::runExternalProgram(const BitTorrent::Torrent *torrent) const
return str.mid(0, (str.length() -1)); return str.mid(0, (str.length() -1));
return str; return str;
}; };
program.replace("%F", chopPathSep(Utils::Fs::toNativePath(torrent->contentPath()))); #endif
program.replace("%R", chopPathSep(Utils::Fs::toNativePath(torrent->rootPath())));
program.replace("%D", chopPathSep(Utils::Fs::toNativePath(torrent->savePath()))); QString program = Preferences::instance()->getAutoRunProgram().trimmed();
for (int i = (program.length() - 2); i >= 0; --i)
{
if (program[i] != QLatin1Char('%'))
continue;
const ushort specifier = program[i + 1].unicode();
switch (specifier)
{
case u'C':
program.replace(i, 2, QString::number(torrent->filesCount()));
break;
case u'D':
#if defined(Q_OS_WIN)
program.replace(i, 2, chopPathSep(Utils::Fs::toNativePath(torrent->savePath())));
#else #else
program.replace("%F", Utils::Fs::toNativePath(torrent->contentPath())); program.replace(i, 2, Utils::Fs::toNativePath(torrent->savePath()));
program.replace("%R", Utils::Fs::toNativePath(torrent->rootPath()));
program.replace("%D", Utils::Fs::toNativePath(torrent->savePath()));
#endif #endif
program.replace("%C", QString::number(torrent->filesCount())); break;
program.replace("%Z", QString::number(torrent->totalSize())); case u'F':
program.replace("%T", torrent->currentTracker()); #if defined(Q_OS_WIN)
// TODO: Update GUI/WebUI labels for this parameter to express its meaning more clearly program.replace(i, 2, chopPathSep(Utils::Fs::toNativePath(torrent->contentPath())));
program.replace("%I", torrent->id().toString()); #else
program.replace(i, 2, Utils::Fs::toNativePath(torrent->contentPath()));
Logger *logger = Logger::instance(); #endif
logger->addMessage(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program)); break;
case u'G':
{
QStringList tags = torrent->tags().values();
std::sort(tags.begin(), tags.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>);
program.replace(i, 2, tags.join(','));
}
break;
case u'I':
program.replace(i, 2, torrent->id().toString());
break;
case u'L':
program.replace(i, 2, torrent->category());
break;
case u'N':
program.replace(i, 2, torrent->name());
break;
case u'R':
#if defined(Q_OS_WIN)
program.replace(i, 2, chopPathSep(Utils::Fs::toNativePath(torrent->rootPath())));
#else
program.replace(i, 2, Utils::Fs::toNativePath(torrent->rootPath()));
#endif
break;
case u'T':
program.replace(i, 2, torrent->currentTracker());
break;
case u'Z':
program.replace(i, 2, QString::number(torrent->totalSize()));
break;
default:
// do nothing
break;
}
// decrement `i` to avoid unwanted replacement, example pattern: "%%N"
--i;
}
LogMsg(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program));
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
auto programWchar = std::make_unique<wchar_t[]>(program.length() + 1); auto programWchar = std::make_unique<wchar_t[]>(program.length() + 1);

Loading…
Cancel
Save