12,925
回編集
(→ハイバネート) |
|||
4行目: | 4行目: | ||
== シャットダウン == | == シャットダウン == | ||
* shutdownコマンドを実行する場合 | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QProcess> | |||
#include <QInputDialog> | |||
void Converter::shutdown() | void Converter::shutdown() | ||
{ | { | ||
// GNOME Power Managementコマンドを試行 | |||
if (QProcess::startDetached("gnome-power-cmd.sh shutdown") || QProcess::startDetached("gnome-power-cmd shutdown")) { | |||
qInfo() << "Shutdown initiated via GNOME Power Management command."; | |||
return; | |||
} | |||
// | // sudo shutdownを試行 | ||
qWarning() << "Attempting to shutdown using sudo. This requires root privileges."; | |||
// パスワード入力ダイアログを表示 | |||
bool ok; | |||
QString password = QInputDialog::getText(this, "Sudo Password", "Enter your sudo password to proceed with shutdown:", | |||
QLineEdit::Password, QString(), &ok); | |||
if( | if (!ok || password.isEmpty()) { | ||
qWarning() << "Shutdown cancelled: No password provided."; | |||
qCritical() << "All shutdown methods failed."; | |||
return; | |||
} | |||
// sudoコマンドを実行 | |||
QProcess process; | |||
process.start("sudo", QStringList() << "-S" << "shutdown" << "-P" << "now"); | |||
if (!process.waitForStarted()) { | |||
qCritical() << "Failed to start sudo process:" << process.errorString(); | |||
qCritical() << "All shutdown methods failed."; | |||
return; | |||
} | } | ||
{ | // パスワードを入力 | ||
process.write(password.toUtf8() + "\n"); | |||
// プロセスの終了を待つ | |||
if (!process.waitForFinished()) { | |||
qCritical() << "Shutdown process did not finish:" << process.errorString(); | |||
qCritical() << "All shutdown methods failed."; | |||
return; | |||
} | } | ||
// | // 終了コードを確認 | ||
if (process.exitCode() != 0) { | |||
qCritical() << "Shutdown failed. Exit code:" << process.exitCode(); | |||
qCritical() << "Error output:" << process.readAllStandardError(); | |||
qCritical() << "All shutdown methods failed."; | |||
return; | |||
} | |||
qInfo() << "Shutdown initiated via sudo command."; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
* D-Busを使用する場合 | |||
<syntaxhighlight lang="c++"> | |||
#include <QDBus> | |||
#include <QDebug> | |||
void Converter::shutdown() | |||
{ | { | ||
// GNOMEセッションマネージャを試行 | |||
{ | QDBusInterface gnomeSessionManager("org.gnome.SessionManager", | ||
"/org/gnome/SessionManager", | |||
"org.gnome.SessionManager", | |||
QDBusConnection::sessionBus()); | |||
if (gnomeSessionManager.isValid()) { | |||
QDBusReply<void> reply = gnomeSessionManager.call("RequestShutdown"); | |||
if (reply.isValid()) { | |||
qInfo() << "Shutdown initiated via GNOME Session Manager."; | |||
return; | |||
} | } | ||
qWarning() << "GNOME Session Manager shutdown failed:" << reply.error().message(); | |||
} | } | ||
{ | // KDEセッションマネージャを試行 | ||
QDBusInterface kdeSessionManager("org.kde.ksmserver", | |||
"/KSMServer", | |||
"org.kde.KSMServerInterface", | |||
QDBusConnection::sessionBus()); | |||
if (kdeSessionManager.isValid()) { | |||
QDBusReply<void> reply = kdeSessionManager.call("logout", 0, 2, 2); | |||
if (reply.isValid()) { | |||
qInfo() << "Shutdown initiated via KDE Session Manager."; | |||
return; | |||
} | |||
qWarning() << "KDE Session Manager shutdown failed:" << reply.error().message(); | |||
} | } | ||
// HAL (Hardware Abstraction Layer) を試行 | |||
QDBusInterface halPower("org.freedesktop.Hal", | |||
"/org/freedesktop/Hal/devices/computer", | |||
"org.freedesktop.Hal.Device.SystemPowerManagement", | |||
QDBusConnection::systemBus()); | |||
if (halPower.isValid()) { | |||
if( | QDBusReply<void> reply = halPower.call("Shutdown"); | ||
if (reply.isValid()) { | |||
qInfo() << "Shutdown initiated via HAL."; | |||
return; | |||
} | } | ||
qWarning() << "HAL shutdown failed:" << reply.error().message(); | |||
} | } | ||
// ConsoleKitを試行 | |||
QDBusInterface consoleKit("org.freedesktop.ConsoleKit", | |||
"/org/freedesktop/ConsoleKit/Manager", | |||
"org.freedesktop.ConsoleKit.Manager", | |||
QDBusConnection::systemBus()); | |||
if (consoleKit.isValid()) { | |||
QDBusReply<void> reply = consoleKit.call("Stop"); | |||
if( | if (reply.isValid()) { | ||
qInfo() << "Shutdown initiated via ConsoleKit."; | |||
return; | |||
} | } | ||
qWarning() << "ConsoleKit shutdown failed:" << reply.error().message(); | |||
} | } | ||
qCritical() << "All shutdown methods failed."; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |