PowerComponentDBus.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <QtDBus/QDBusConnection>
  2. #include <QtDBus/QDBusInterface>
  3. #include <QtDBus/QDBusReply>
  4. #include "PowerComponentDBus.h"
  5. #define DBUS_SERVICE_NAME "org.freedesktop.login1"
  6. #define DBUS_SERVICE_PATH "/org/freedesktop/login1"
  7. #define DBUS_INTERFACE "org.freedesktop.login1.Manager"
  8. /////////////////////////////////////////////////////////////////////////////////////////
  9. bool PowerComponentDBus::callPowerMethod(QString method)
  10. {
  11. if (QDBusConnection::systemBus().isConnected())
  12. {
  13. QDBusInterface iface(DBUS_SERVICE_NAME, DBUS_SERVICE_PATH, DBUS_INTERFACE,
  14. QDBusConnection::systemBus());
  15. if (iface.isValid())
  16. {
  17. QDBusReply<bool> reply = iface.call(method, true);
  18. if (reply.isValid())
  19. {
  20. return true;
  21. }
  22. else
  23. {
  24. QLOG_ERROR() << "callPowerMethod : Error while calling" << method << ":"
  25. << reply.error().message();
  26. return false;
  27. }
  28. }
  29. else
  30. {
  31. QLOG_ERROR() << "callPowerMethod : failed to retrieve interface.";
  32. }
  33. }
  34. else
  35. {
  36. QLOG_ERROR() << "callPowerMethod : could not find system bus";
  37. }
  38. return false;
  39. }
  40. /////////////////////////////////////////////////////////////////////////////////////////
  41. bool PowerComponentDBus::isPowerMethodAvailable(QString method)
  42. {
  43. if (QDBusConnection::systemBus().isConnected())
  44. {
  45. QDBusInterface iface(DBUS_SERVICE_NAME, DBUS_SERVICE_PATH, DBUS_INTERFACE,
  46. QDBusConnection::systemBus());
  47. if (iface.isValid())
  48. {
  49. QDBusReply<QString> reply = iface.call(method);
  50. if (reply.isValid())
  51. {
  52. return (reply.value() == "yes");
  53. }
  54. else
  55. {
  56. QLOG_ERROR() << "isPowerMethodAvailable : Error while calling" << method << ":"
  57. << reply.error().message();
  58. return false;
  59. }
  60. }
  61. else
  62. {
  63. QLOG_ERROR() << "isPowerMethodAvailable : failed to retrieve interface.";
  64. }
  65. }
  66. else
  67. {
  68. QLOG_ERROR() << "isPowerMethodAvailable : could not find system bus";
  69. }
  70. return false;
  71. }