Browse Source

Merge pull request #11672 from sledgehammer999/show_console

Option to show console when external program is run
adaptive-webui-19844
sledgehammer999 5 years ago committed by GitHub
parent
commit
3a70dd4438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      src/app/application.cpp
  2. 16
      src/base/preferences.cpp
  3. 4
      src/base/preferences.h
  4. 9
      src/gui/optionsdialog.cpp
  5. 7
      src/gui/optionsdialog.ui

34
src/app/application.cpp

@ -332,23 +332,47 @@ void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) c
logger->addMessage(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program)); logger->addMessage(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program));
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
std::unique_ptr<wchar_t[]> programWchar(new wchar_t[program.length() + 1] {}); auto programWchar = std::make_unique<wchar_t[]>(program.length() + 1);
program.toWCharArray(programWchar.get()); program.toWCharArray(programWchar.get());
// Need to split arguments manually because QProcess::startDetached(QString) // Need to split arguments manually because QProcess::startDetached(QString)
// will strip off empty parameters. // will strip off empty parameters.
// E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"` // E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`
int argCount = 0; int argCount = 0;
LPWSTR *args = ::CommandLineToArgvW(programWchar.get(), &argCount); std::unique_ptr<LPWSTR[], decltype(&::LocalFree)> args {::CommandLineToArgvW(programWchar.get(), &argCount), ::LocalFree};
QStringList argList; QStringList argList;
for (int i = 1; i < argCount; ++i) for (int i = 1; i < argCount; ++i)
argList += QString::fromWCharArray(args[i]); argList += QString::fromWCharArray(args[i]);
QProcess::startDetached(QString::fromWCharArray(args[0]), argList); #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
QProcess proc;
::LocalFree(args); proc.setProgram(QString::fromWCharArray(args[0]));
proc.setArguments(argList);
proc.setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments *args)
{
if (Preferences::instance()->isAutoRunConsoleEnabled()) {
args->flags |= CREATE_NEW_CONSOLE;
args->flags &= ~(CREATE_NO_WINDOW | DETACHED_PROCESS);
}
else {
args->flags |= CREATE_NO_WINDOW;
args->flags &= ~(CREATE_NEW_CONSOLE | DETACHED_PROCESS);
}
args->inheritHandles = false;
args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
::CloseHandle(args->startupInfo->hStdInput);
::CloseHandle(args->startupInfo->hStdOutput);
::CloseHandle(args->startupInfo->hStdError);
args->startupInfo->hStdInput = nullptr;
args->startupInfo->hStdOutput = nullptr;
args->startupInfo->hStdError = nullptr;
});
proc.startDetached();
#else #else
QProcess::startDetached(QString::fromWCharArray(args[0]), argList);
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#else // Q_OS_WIN
// Cannot give users shell environment by default, as doing so could // Cannot give users shell environment by default, as doing so could
// enable command injection via torrent name and other arguments // enable command injection via torrent name and other arguments
// (especially when some automated download mechanism has been setup). // (especially when some automated download mechanism has been setup).

16
src/base/preferences.cpp

@ -781,7 +781,7 @@ bool Preferences::isUILocked() const
void Preferences::setUILocked(const bool locked) void Preferences::setUILocked(const bool locked)
{ {
return setValue("Locking/locked", locked); setValue("Locking/locked", locked);
} }
bool Preferences::isAutoRunEnabled() const bool Preferences::isAutoRunEnabled() const
@ -791,7 +791,7 @@ bool Preferences::isAutoRunEnabled() const
void Preferences::setAutoRunEnabled(const bool enabled) void Preferences::setAutoRunEnabled(const bool enabled)
{ {
return setValue("AutoRun/enabled", enabled); setValue("AutoRun/enabled", enabled);
} }
QString Preferences::getAutoRunProgram() const QString Preferences::getAutoRunProgram() const
@ -804,6 +804,18 @@ void Preferences::setAutoRunProgram(const QString &program)
setValue("AutoRun/program", program); setValue("AutoRun/program", program);
} }
#if defined(Q_OS_WIN) && (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
bool Preferences::isAutoRunConsoleEnabled() const
{
return value("AutoRun/ConsoleEnabled", false).toBool();
}
void Preferences::setAutoRunConsoleEnabled(const bool enabled)
{
setValue("AutoRun/ConsoleEnabled", enabled);
}
#endif
bool Preferences::shutdownWhenDownloadsComplete() const bool Preferences::shutdownWhenDownloadsComplete() const
{ {
return value("Preferences/Downloads/AutoShutDownOnCompletion", false).toBool(); return value("Preferences/Downloads/AutoShutDownOnCompletion", false).toBool();

4
src/base/preferences.h

@ -238,6 +238,10 @@ public:
void setAutoRunEnabled(bool enabled); void setAutoRunEnabled(bool enabled);
QString getAutoRunProgram() const; QString getAutoRunProgram() const;
void setAutoRunProgram(const QString &program); void setAutoRunProgram(const QString &program);
#if defined(Q_OS_WIN) && (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
bool isAutoRunConsoleEnabled() const;
void setAutoRunConsoleEnabled(bool enabled);
#endif
bool shutdownWhenDownloadsComplete() const; bool shutdownWhenDownloadsComplete() const;
void setShutdownWhenDownloadsComplete(bool shutdown); void setShutdownWhenDownloadsComplete(bool shutdown);
bool suspendWhenDownloadsComplete() const; bool suspendWhenDownloadsComplete() const;

9
src/gui/optionsdialog.cpp

@ -295,6 +295,7 @@ OptionsDialog::OptionsDialog(QWidget *parent)
connect(m_ui->mailNotifPassword, &QLineEdit::textChanged, this, &ThisType::enableApplyButton); connect(m_ui->mailNotifPassword, &QLineEdit::textChanged, this, &ThisType::enableApplyButton);
connect(m_ui->autoRunBox, &QGroupBox::toggled, this, &ThisType::enableApplyButton); connect(m_ui->autoRunBox, &QGroupBox::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->lineEditAutoRun, &QLineEdit::textChanged, this, &ThisType::enableApplyButton); connect(m_ui->lineEditAutoRun, &QLineEdit::textChanged, this, &ThisType::enableApplyButton);
connect(m_ui->autoRunConsole, &QCheckBox::toggled, this, &ThisType::enableApplyButton);
const QString autoRunStr = QString("%1\n %2\n %3\n %4\n %5\n %6\n %7\n %8\n %9\n %10\n %11\n%12") const QString autoRunStr = QString("%1\n %2\n %3\n %4\n %5\n %6\n %7\n %8\n %9\n %10\n %11\n%12")
.arg(tr("Supported parameters (case sensitive):") .arg(tr("Supported parameters (case sensitive):")
@ -696,6 +697,9 @@ void OptionsDialog::saveOptions()
pref->setMailNotificationSMTPPassword(m_ui->mailNotifPassword->text()); pref->setMailNotificationSMTPPassword(m_ui->mailNotifPassword->text());
pref->setAutoRunEnabled(m_ui->autoRunBox->isChecked()); pref->setAutoRunEnabled(m_ui->autoRunBox->isChecked());
pref->setAutoRunProgram(m_ui->lineEditAutoRun->text().trimmed()); pref->setAutoRunProgram(m_ui->lineEditAutoRun->text().trimmed());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) && defined(Q_OS_WIN)
pref->setAutoRunConsoleEnabled(m_ui->autoRunConsole->isChecked());
#endif
pref->setActionOnDblClOnTorrentDl(getActionOnDblClOnTorrentDl()); pref->setActionOnDblClOnTorrentDl(getActionOnDblClOnTorrentDl());
pref->setActionOnDblClOnTorrentFn(getActionOnDblClOnTorrentFn()); pref->setActionOnDblClOnTorrentFn(getActionOnDblClOnTorrentFn());
TorrentFileGuard::setAutoDeleteMode(!m_ui->deleteTorrentBox->isChecked() ? TorrentFileGuard::Never TorrentFileGuard::setAutoDeleteMode(!m_ui->deleteTorrentBox->isChecked() ? TorrentFileGuard::Never
@ -970,6 +974,11 @@ void OptionsDialog::loadOptions()
m_ui->autoRunBox->setChecked(pref->isAutoRunEnabled()); m_ui->autoRunBox->setChecked(pref->isAutoRunEnabled());
m_ui->lineEditAutoRun->setText(pref->getAutoRunProgram()); m_ui->lineEditAutoRun->setText(pref->getAutoRunProgram());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) && defined(Q_OS_WIN)
m_ui->autoRunConsole->setChecked(pref->isAutoRunConsoleEnabled());
#else
m_ui->autoRunConsole->hide();
#endif
intValue = pref->getActionOnDblClOnTorrentDl(); intValue = pref->getActionOnDblClOnTorrentDl();
if (intValue >= m_ui->actionTorrentDlOnDblClBox->count()) if (intValue >= m_ui->actionTorrentDlOnDblClBox->count())
intValue = 0; intValue = 0;

7
src/gui/optionsdialog.ui

@ -1264,6 +1264,13 @@
<item> <item>
<widget class="QLineEdit" name="lineEditAutoRun"/> <widget class="QLineEdit" name="lineEditAutoRun"/>
</item> </item>
<item>
<widget class="QCheckBox" name="autoRunConsole">
<property name="text">
<string>Show console window</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="labelAutoRunParam"> <widget class="QLabel" name="labelAutoRunParam">
<property name="wordWrap"> <property name="wordWrap">

Loading…
Cancel
Save