「Qtの基礎 - シャットダウン」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「== 概要 == ソフトウェアからWindows / Linuxをシャットダウンや再起動等を行う方法を記載する。<br> <br><br> == シャットダウン == <s…」) |
編集の要約なし |
||
1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
Qtライブラリを使用して、ソフトウェアからOSのシャットダウンや再起動等を行う方法を記載する。<br> | |||
<br><br> | <br><br> | ||
84行目: | 84行目: | ||
QProcess::startDetached("sudo shutdown -P now"); | QProcess::startDetached("sudo shutdown -P now"); | ||
} | } | ||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== 再起動 == | |||
<syntaxhighlight lang="c++"> | |||
void Converter::reboot() | |||
{ | |||
bool reboot_works = false; | |||
bool gnome_power1 = false; | |||
bool gnome_power2 = false; | |||
bool hal_works = false; | |||
QDBusMessage response; | |||
// GNOME | |||
QDBusInterface gnomeSessionManager("org.gnome.SessionManager", "/org/gnome/SessionManager", "org.gnome.SessionManager", QDBusConnection::sessionBus()); | |||
response = gnomeSessionManager.call("RequestReboot"); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh reboot"); | |||
gnome_power2 = QProcess::startDetached("gnome-power-cmd reboot"); | |||
if(verbose && !gnome_power1 && !gnome_power2) | |||
{ | |||
qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work"; | |||
} | |||
} | |||
else | |||
{ | |||
reboot_works = true; | |||
} | |||
// KDE | |||
QDBusInterface kdeSessionManager("org.kde.ksmserver", "/KSMServer", "org.kde.KSMServerInterface", QDBusConnection::sessionBus()); | |||
response = kdeSessionManager.call("logout", 0, 2, 1); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
} | |||
else | |||
{ | |||
reboot_works = true; | |||
} | |||
if(!reboot_works && !gnome_power1 && !gnome_power2) | |||
{ | |||
QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", "org.freedesktop.Hal.Device.SystemPowerManagement", | |||
QDBusConnection::systemBus()); | |||
response = powermanagement.call("Reboot"); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
} | |||
else | |||
{ | |||
hal_works = true; | |||
} | |||
} | |||
if(!hal_works && !reboot_works && !gnome_power1 && !gnome_power2) | |||
{ | |||
QDBusInterface powermanagement("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", | |||
QDBusConnection::systemBus()); | |||
response = powermanagement.call("Restart"); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
QProcess::startDetached("sudo shutdown -r now"); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== サスペンド(スリープ) == | |||
<syntaxhighlight lang="c++"> | |||
void Converter::suspend() | |||
{ | |||
bool gnome_power1 = false; | |||
bool gnome_power2 = false; | |||
bool hal_works = false; | |||
QDBusMessage response; | |||
gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh suspend"); | |||
gnome_power2 = QProcess::startDetached("gnome-power-cmd suspend"); | |||
if(!gnome_power1 && !gnome_power2 && verbose) | |||
{ | |||
qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work"; | |||
} | |||
if(!gnome_power1 && !gnome_power2) | |||
{ | |||
QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", | |||
"org.freedesktop.Hal.Device.SystemPowerManagement", QDBusConnection::systemBus()); | |||
response = powermanagement.call("Suspend", 0); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
} | |||
else | |||
{ | |||
hal_works = true; | |||
} | |||
} | |||
if(!hal_works && !gnome_power1 && !gnome_power2) | |||
{ | |||
QDBusInterface powermanagement("org.freedesktop.DeviceKit.Power", "/org/freedesktop/DeviceKit/Power", | |||
"org.freedesktop.DeviceKit.Power", QDBusConnection::systemBus()); | |||
response = powermanagement.call("Suspend"); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== ハイバネート == | |||
<syntaxhighlight lang="c++"> | |||
void Converter::hibernate() | |||
{ | |||
bool gnome_power1 = false; | |||
bool gnome_power2 = false; | |||
bool hal_works = false; | |||
QDBusMessage response; | |||
gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh hibernate"); | |||
gnome_power2 = QProcess::startDetached("gnome-power-cmd hibernate"); | |||
if(!gnome_power1 && !gnome_power2 && verbose) | |||
{ | |||
qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work"; | |||
} | |||
if(!gnome_power1 && !gnome_power2) | |||
{ | |||
QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", | |||
"org.freedesktop.Hal.Device.SystemPowerManagement", QDBusConnection::systemBus()); | |||
response = powermanagement.call("Hibernate"); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
} | |||
else | |||
{ | |||
hal_works = true; | |||
} | |||
} | |||
if(!hal_works && !gnome_power1 && !gnome_power2) | |||
{ | |||
QDBusInterface powermanagement("org.freedesktop.DeviceKit.Power", "/org/freedesktop/DeviceKit/Power", | |||
"org.freedesktop.DeviceKit.Power", QDBusConnection::systemBus()); | |||
if(response.type() == QDBusMessage::ErrorMessage) | |||
{ | |||
if(verbose) | |||
{ | |||
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage(); | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== 画面のロック == | |||
<syntaxhighlight lang="c++"> | |||
void Converter::lock() | |||
{ | |||
if(system("gnome-screensaver-command -l") && system("dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock")) | |||
{ | |||
QMessageBox::warning(this, "Error", "Unable to lock computer.", QMessageBox::Ok, NULL); | |||
} | } | ||
} | } |
2021年1月25日 (月) 23:48時点における版
概要
Qtライブラリを使用して、ソフトウェアからOSのシャットダウンや再起動等を行う方法を記載する。
シャットダウン
void Converter::shutdown()
{
bool shutdown_works = false;
bool gnome_power1 = false;
bool gnome_power2 = false;
bool hal_works = false;
QDBusMessage response;
// GNOME
QDBusInterface gnomeSessionManager("org.gnome.SessionManager", "/org/gnome/SessionManager", "org.gnome.SessionManager", QDBusConnection::sessionBus());
response = gnomeSessionManager.call("RequestShutdown");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh shutdown");
gnome_power2 = QProcess::startDetached("gnome-power-cmd shutdown");
if(verbose && !gnome_power1 && !gnome_power2)
qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work";
}
else
{
shutdown_works = true;
}
// KDE
QDBusInterface kdeSessionManager("org.kde.ksmserver", "/KSMServer", "org.kde.KSMServerInterface", QDBusConnection::sessionBus());
response = kdeSessionManager.call("logout", 0, 2, 2);
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
else
{
shutdown_works = true;
}
if(!shutdown_works && !gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement", QDBusConnection::systemBus());
response = powermanagement.call("Shutdown");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
else
{
hal_works = true;
}
}
if(!hal_works && !shutdown_works && !gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager",
"org.freedesktop.ConsoleKit.Manager", QDBusConnection::systemBus());
response = powermanagement.call("Stop");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
QProcess::startDetached("sudo shutdown -P now");
}
}
}
再起動
void Converter::reboot()
{
bool reboot_works = false;
bool gnome_power1 = false;
bool gnome_power2 = false;
bool hal_works = false;
QDBusMessage response;
// GNOME
QDBusInterface gnomeSessionManager("org.gnome.SessionManager", "/org/gnome/SessionManager", "org.gnome.SessionManager", QDBusConnection::sessionBus());
response = gnomeSessionManager.call("RequestReboot");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh reboot");
gnome_power2 = QProcess::startDetached("gnome-power-cmd reboot");
if(verbose && !gnome_power1 && !gnome_power2)
{
qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work";
}
}
else
{
reboot_works = true;
}
// KDE
QDBusInterface kdeSessionManager("org.kde.ksmserver", "/KSMServer", "org.kde.KSMServerInterface", QDBusConnection::sessionBus());
response = kdeSessionManager.call("logout", 0, 2, 1);
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
else
{
reboot_works = true;
}
if(!reboot_works && !gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", "org.freedesktop.Hal.Device.SystemPowerManagement",
QDBusConnection::systemBus());
response = powermanagement.call("Reboot");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
else
{
hal_works = true;
}
}
if(!hal_works && !reboot_works && !gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager",
QDBusConnection::systemBus());
response = powermanagement.call("Restart");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
QProcess::startDetached("sudo shutdown -r now");
}
}
}
サスペンド(スリープ)
void Converter::suspend()
{
bool gnome_power1 = false;
bool gnome_power2 = false;
bool hal_works = false;
QDBusMessage response;
gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh suspend");
gnome_power2 = QProcess::startDetached("gnome-power-cmd suspend");
if(!gnome_power1 && !gnome_power2 && verbose)
{
qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work";
}
if(!gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement", QDBusConnection::systemBus());
response = powermanagement.call("Suspend", 0);
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
else
{
hal_works = true;
}
}
if(!hal_works && !gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.DeviceKit.Power", "/org/freedesktop/DeviceKit/Power",
"org.freedesktop.DeviceKit.Power", QDBusConnection::systemBus());
response = powermanagement.call("Suspend");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
}
}
ハイバネート
void Converter::hibernate()
{
bool gnome_power1 = false;
bool gnome_power2 = false;
bool hal_works = false;
QDBusMessage response;
gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh hibernate");
gnome_power2 = QProcess::startDetached("gnome-power-cmd hibernate");
if(!gnome_power1 && !gnome_power2 && verbose)
{
qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work";
}
if(!gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement", QDBusConnection::systemBus());
response = powermanagement.call("Hibernate");
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
else
{
hal_works = true;
}
}
if(!hal_works && !gnome_power1 && !gnome_power2)
{
QDBusInterface powermanagement("org.freedesktop.DeviceKit.Power", "/org/freedesktop/DeviceKit/Power",
"org.freedesktop.DeviceKit.Power", QDBusConnection::systemBus());
if(response.type() == QDBusMessage::ErrorMessage)
{
if(verbose)
{
qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
}
}
}
}
画面のロック
void Converter::lock()
{
if(system("gnome-screensaver-command -l") && system("dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock"))
{
QMessageBox::warning(this, "Error", "Unable to lock computer.", QMessageBox::Ok, NULL);
}
}