「Qtの基礎 - シャットダウン」の版間の差分

228行目: 228行目:


== ハイバネート ==
== ハイバネート ==
* GNOME Power Managementコマンドを実行する場合
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
#include <QProcess>
  void Converter::hibernate()
  void Converter::hibernate()
  {
  {
     bool gnome_power1 = false;
     // GNOME Power Management コマンドを試行
     bool gnome_power2 = false;
     if (QProcess::startDetached("gnome-power-cmd.sh hibernate") ||
    bool hal_works = false;
      QProcess::startDetached("gnome-power-cmd hibernate")) {
     QDBusMessage response;
      qInfo() << "Hibernation initiated via GNOME Power Management.";
      return;
     }
   
   
     gnome_power1 = QProcess::startDetached("gnome-power-cmd.sh hibernate");
     qWarning() << "GNOME Power Management commands failed. Trying DBus methods.";
    gnome_power2 = QProcess::startDetached("gnome-power-cmd hibernate");
}
    if(!gnome_power1 && !gnome_power2 && verbose)
</syntaxhighlight>
    {
<br>
      qWarning() << "W: gnome-power-cmd and gnome-power-cmd.sh didn't work";
* D-Busを使用する場合
    }
<syntaxhighlight lang="c++">
#include <QDBusInterface>
#include <QDBusReply>
#include <QDebug>
   
   
     if(!gnome_power1 && !gnome_power2)
void Converter::hibernate()
     {
{
      QDBusInterface powermanagement("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer",
     // HAL (Hardware Abstraction Layer) を試行
                                      "org.freedesktop.Hal.Device.SystemPowerManagement", QDBusConnection::systemBus());
     QDBusInterface halPower("org.freedesktop.Hal",  
      response = powermanagement.call("Hibernate");
                            "/org/freedesktop/Hal/devices/computer",
                            "org.freedesktop.Hal.Device.SystemPowerManagement",  
                            QDBusConnection::systemBus());
   
   
      if(response.type() == QDBusMessage::ErrorMessage)
    if (halPower.isValid()) {
       {
      QDBusReply<void> reply = halPower.call("Hibernate");
          if(verbose)
       if (reply.isValid()) {
           {
           qInfo() << "Hibernation initiated via HAL.";
            qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
           return;
           }
      }
      else
      {
          hal_works = true;
       }
       }
      qWarning() << "HAL hibernation failed:" << reply.error().message();
    }
    else {
      qWarning() << "Failed to connect to HAL power management.";
     }
     }
   
   
     if(!hal_works && !gnome_power1 && !gnome_power2)
     // DeviceKit Powerを試行
     {
     QDBusInterface dkPower("org.freedesktop.DeviceKit.Power",  
      QDBusInterface powermanagement("org.freedesktop.DeviceKit.Power", "/org/freedesktop/DeviceKit/Power",
                          "/org/freedesktop/DeviceKit/Power",
                                      "org.freedesktop.DeviceKit.Power", QDBusConnection::systemBus());
                          "org.freedesktop.DeviceKit.Power",  
                          QDBusConnection::systemBus());
      if(response.type() == QDBusMessage::ErrorMessage)
   
      {
    if (dkPower.isValid()) {
          if(verbose)
        QDBusReply<void> reply = dkPower.call("Hibernate");
          {
        if (reply.isValid()) {
            qWarning() << "W: " << response.errorName() << ":" << response.errorMessage();
            qInfo() << "Hibernation initiated via DeviceKit Power.";
          }
            return;
       }
       }
      qWarning() << "DeviceKit Power hibernation failed:" << reply.error().message();
     }
     }
    else {
      qWarning() << "Failed to connect to DeviceKit Power.";
    }
    qCritical() << "All hibernation methods failed.";
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>