GUI: add separate actions for hibernation/sleeping instead of just suspending

This commit is contained in:
Moritz Bunkus 2017-04-06 18:35:38 +02:00
parent ddb5c21083
commit a8f880b216
10 changed files with 69 additions and 37 deletions

View File

@ -8,9 +8,9 @@
queue completion.
* MKVToolNix GUI: implemented several built-in actions that can be executed
either on special events or once via the "watch jobs" tool. These are:
playing an audio file (implemented for all operating systems); suspending
and shutting down the computer (only implemented for Windows and for Linux
systems using systemd).
playing an audio file (implemented for all operating systems); hibernating,
sleeping and shutting down the computer (only implemented for Windows and
for Linux systems using systemd).
## Bug fixes

View File

@ -104,8 +104,11 @@ ProgramRunner::run(Util::Settings::RunProgramForEvent forEvent,
else if (runConfig->m_type == Util::Settings::RunProgramType::ShutDownComputer)
shutDownComputer(*runConfig);
else if (runConfig->m_type == Util::Settings::RunProgramType::SuspendComputer)
suspendComputer(*runConfig);
else if (runConfig->m_type == Util::Settings::RunProgramType::HibernateComputer)
hibernateComputer(*runConfig);
else if (runConfig->m_type == Util::Settings::RunProgramType::SleepComputer)
sleepComputer(*runConfig);
}
}
@ -215,7 +218,12 @@ ProgramRunner::shutDownComputer(Util::Settings::RunProgramConfig &/* config */)
}
void
ProgramRunner::suspendComputer(Util::Settings::RunProgramConfig &/* config */) {
ProgramRunner::hibernateComputer(Util::Settings::RunProgramConfig &/* config */) {
// Not supported in an OS-agnostic way.
}
void
ProgramRunner::sleepComputer(Util::Settings::RunProgramConfig &/* config */) {
// Not supported in an OS-agnostic way.
}

View File

@ -49,7 +49,8 @@ protected:
virtual void executeProgram(Util::Settings::RunProgramConfig &config, std::function<void(VariableMap &)> const &setupVariables, VariableMap const &generalVariables);
virtual void playAudioFile(Util::Settings::RunProgramConfig &config);
virtual void shutDownComputer(Util::Settings::RunProgramConfig &config);
virtual void suspendComputer(Util::Settings::RunProgramConfig &config);
virtual void hibernateComputer(Util::Settings::RunProgramConfig &config);
virtual void sleepComputer(Util::Settings::RunProgramConfig &config);
public:
static std::unique_ptr<ProgramRunner> create();

View File

@ -24,7 +24,7 @@ LinuxProgramRunner::isRunProgramTypeSupported(Util::Settings::RunProgramType typ
if (ProgramRunner::isRunProgramTypeSupported(type))
return true;
return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::SuspendComputer);
return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::HibernateComputer, Util::Settings::RunProgramType::SleepComputer);
}
void
@ -40,15 +40,24 @@ LinuxProgramRunner::shutDownComputer(Util::Settings::RunProgramConfig &) {
}
void
LinuxProgramRunner::suspendComputer(Util::Settings::RunProgramConfig &) {
qDebug() << "LinuxProgramRunner::suspendComputer: about to shut down the computer via systemctl";
LinuxProgramRunner::hibernateOrSleepComputer(bool hibernate) {
auto action = Q(hibernate ? "hibernate" : "suspend");
qDebug() << "LinuxProgramRunner::hibernateOrSleepComputer: about to" << action << "the computer via systemctl";
auto result = QProcess::execute("systemctl suspend");
auto result = QProcess::execute(Q("systemctl %1").arg(action));
if (result == 0)
return;
if (result != 0)
qDebug() << "LinuxProgramRunner::hibernateOrSleepComputer: 'systemctl" << action << "' failed:" << result;
}
qDebug() << "LinuxProgramRunner::suspendComputer: 'systemctl poweroff' failed:" << result;
void
LinuxProgramRunner::hibernateComputer(Util::Settings::RunProgramConfig &) {
hibernateOrSleepComputer(true);
}
void
LinuxProgramRunner::sleepComputer(Util::Settings::RunProgramConfig &) {
hibernateOrSleepComputer(false);
}
}}}

View File

@ -22,7 +22,9 @@ public:
protected:
virtual void shutDownComputer(Util::Settings::RunProgramConfig &config) override;
virtual void suspendComputer(Util::Settings::RunProgramConfig &config) override;
virtual void hibernateComputer(Util::Settings::RunProgramConfig &config) override;
virtual void sleepComputer(Util::Settings::RunProgramConfig &config) override;
virtual void hibernateOrSleepComputer(bool hibernate);
};
}}}

View File

@ -28,7 +28,7 @@ WindowsProgramRunner::isRunProgramTypeSupported(Util::Settings::RunProgramType t
if (ProgramRunner::isRunProgramTypeSupported(type))
return true;
return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::SuspendComputer);
return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::HibernateComputer, Util::Settings::RunProgramType::SleepComputer);
}
void
@ -68,22 +68,27 @@ WindowsProgramRunner::shutDownComputer(Util::Settings::RunProgramConfig &) {
}
void
WindowsProgramRunner::suspendComputer(Util::Settings::RunProgramConfig &) {
qDebug() << "WindowsProgramRunner::suspendComputer: about to hibernate";
WindowsProgramRunner::hibernateOrSleepComputer(bool hibernate) {
auto action = Q(hibernate ? "hibernate" : "sleep");
qDebug() << "WindowsProgramRunner::hibernateOrSleepComputer: about to" << action;
addShutdownNamePrivilege();
if (SetSuspendState(true, false, false))
if (SetSuspendState(hibernate, false, false))
return;
auto error = GetLastError();
qDebug() << "WindowsProgramRunner::suspendComputer: hibernate failed, about to sleep; error:" << error << Q(mtx::sys::format_windows_message(error));
qDebug() << "WindowsProgramRunner::hibernateOrSleepComputer:" << action << "failed; error:" << error << Q(mtx::sys::format_windows_message(error));
}
if (SetSuspendState(false, false, false))
return;
void
WindowsProgramRunner::hibernateComputer(Util::Settings::RunProgramConfig &) {
hibernateOrSleepComputer(true);
}
error = GetLastError();
qDebug() << "WindowsProgramRunner::suspendComputer: sleep failed, too. Not trying anything else. Error:" << error << Q(mtx::sys::format_windows_message(error));
void
WindowsProgramRunner::sleepComputer(Util::Settings::RunProgramConfig &) {
hibernateOrSleepComputer(false);
}
QString

View File

@ -22,7 +22,9 @@ public:
protected:
virtual void shutDownComputer(Util::Settings::RunProgramConfig &config) override;
virtual void suspendComputer(Util::Settings::RunProgramConfig &config) override;
virtual void hibernateComputer(Util::Settings::RunProgramConfig &config) override;
virtual void sleepComputer(Util::Settings::RunProgramConfig &config) override;
virtual void hibernateOrSleepComputer(bool hibernate);
virtual void addShutdownNamePrivilege();
};

View File

@ -195,12 +195,14 @@ PrefsRunProgramWidget::setupTypeControl(Util::Settings::RunProgramConfig const &
addItemIfSupported(QY("Execute a program"), Util::Settings::RunProgramType::ExecuteProgram);
addItemIfSupported(QY("Play an audio file"), Util::Settings::RunProgramType::PlayAudioFile);
addItemIfSupported(QY("Shut down the computer"), Util::Settings::RunProgramType::ShutDownComputer);
addItemIfSupported(QY("Suspend the computer"), Util::Settings::RunProgramType::SuspendComputer);
addItemIfSupported(QY("Hibernate the computer"), Util::Settings::RunProgramType::HibernateComputer);
addItemIfSupported(QY("Sleep the computer"), Util::Settings::RunProgramType::SleepComputer);
d->pagesByType[Util::Settings::RunProgramType::ExecuteProgram] = d->ui->executeProgramTypePage;
d->pagesByType[Util::Settings::RunProgramType::PlayAudioFile] = d->ui->playAudioFileTypePage;
d->pagesByType[Util::Settings::RunProgramType::ShutDownComputer] = d->ui->emptyTypePage;
d->pagesByType[Util::Settings::RunProgramType::SuspendComputer] = d->ui->emptyTypePage;
d->pagesByType[Util::Settings::RunProgramType::ExecuteProgram] = d->ui->executeProgramTypePage;
d->pagesByType[Util::Settings::RunProgramType::PlayAudioFile] = d->ui->playAudioFileTypePage;
d->pagesByType[Util::Settings::RunProgramType::ShutDownComputer] = d->ui->emptyTypePage;
d->pagesByType[Util::Settings::RunProgramType::HibernateComputer] = d->ui->emptyTypePage;
d->pagesByType[Util::Settings::RunProgramType::SleepComputer] = d->ui->emptyTypePage;
showPageForType(cfg.m_type);

View File

@ -51,11 +51,12 @@ Settings::RunProgramConfig::name()
if (!m_name.isEmpty())
return m_name;
return m_type == RunProgramType::ExecuteProgram ? nameForExternalProgram()
: m_type == RunProgramType::PlayAudioFile ? nameForPlayAudioFile()
: m_type == RunProgramType::ShutDownComputer ? QY("Shut down the computer")
: m_type == RunProgramType::SuspendComputer ? QY("Suspend the computer")
: Q("unknown");
return m_type == RunProgramType::ExecuteProgram ? nameForExternalProgram()
: m_type == RunProgramType::PlayAudioFile ? nameForPlayAudioFile()
: m_type == RunProgramType::ShutDownComputer ? QY("Shut down the computer")
: m_type == RunProgramType::HibernateComputer ? QY("Hibernate the computer")
: m_type == RunProgramType::SleepComputer ? QY("Sleep the computer")
: Q("unknown");
}
QString
@ -420,7 +421,8 @@ Settings::addDefaultRunProgramConfigurations(QSettings &reg) {
auto numConfigurationsBefore = m_runProgramConfigurations.count();
addDefaultRunProgramConfigurationForType(reg, RunProgramType::PlayAudioFile, [](RunProgramConfig &cfg) { cfg.m_audioFile = App::programRunner().defaultAudioFileName(); });
addDefaultRunProgramConfigurationForType(reg, RunProgramType::SuspendComputer);
addDefaultRunProgramConfigurationForType(reg, RunProgramType::SleepComputer);
addDefaultRunProgramConfigurationForType(reg, RunProgramType::HibernateComputer);
addDefaultRunProgramConfigurationForType(reg, RunProgramType::ShutDownComputer);
if (numConfigurationsBefore != m_runProgramConfigurations.count())

View File

@ -26,7 +26,8 @@ public:
ExecuteProgram,
PlayAudioFile,
ShutDownComputer,
SuspendComputer,
HibernateComputer,
SleepComputer,
Max,
Default = ExecuteProgram,
};