systemdcommunicator.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2015 Malte Veerman <maldela@halloarsch.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include "systemdcommunicator.h"
  20. #include <KF5/KAuth/kauthexecutejob.h>
  21. using namespace KAuth;
  22. #include <QDebug>
  23. #include <QVariant>
  24. SystemdCommunicator::SystemdCommunicator(QObject *parent) : QObject(parent)
  25. {
  26. m_serviceInterface = nullptr;
  27. m_error = "Success";
  28. m_managerInterface = new QDBusInterface("org.freedesktop.systemd1",
  29. "/org/freedesktop/systemd1",
  30. "org.freedesktop.systemd1.Manager",
  31. QDBusConnection::systemBus(),
  32. this);
  33. setServiceName("fancontrol");
  34. }
  35. void SystemdCommunicator::setServiceName(const QString &name)
  36. {
  37. if (name != m_serviceName)
  38. {
  39. m_serviceName = name;
  40. if (serviceExists())
  41. {
  42. QVariantList arguments;
  43. arguments << QVariant(m_serviceName + ".service");
  44. QDBusMessage dbusreply = m_managerInterface->callWithArgumentList(QDBus::AutoDetect, "LoadUnit", arguments);
  45. if (dbusreply.type() == QDBusMessage::ErrorMessage)
  46. {
  47. m_error = dbusreply.errorMessage();
  48. emit errorChanged();
  49. m_serviceObjectPath.clear();
  50. }
  51. else
  52. {
  53. m_serviceObjectPath = qdbus_cast<QDBusObjectPath>(dbusreply.arguments().at(0)).path();
  54. if (m_serviceInterface)
  55. m_serviceInterface->deleteLater();
  56. m_serviceInterface = new QDBusInterface("org.freedesktop.systemd1",
  57. m_serviceObjectPath,
  58. "org.freedesktop.systemd1.Unit",
  59. QDBusConnection::systemBus(),
  60. this);
  61. }
  62. }
  63. }
  64. emit serviceNameChanged();
  65. emit serviceEnabledChanged();
  66. }
  67. bool SystemdCommunicator::serviceExists()
  68. {
  69. QDBusMessage dbusreply;
  70. if (m_managerInterface && m_managerInterface->isValid())
  71. dbusreply = m_managerInterface->call(QDBus::AutoDetect, "ListUnitFiles");
  72. if (dbusreply.type() == QDBusMessage::ErrorMessage)
  73. {
  74. m_error = dbusreply.errorMessage();
  75. emit errorChanged();
  76. return false;
  77. }
  78. SystemdUnitFileList list = qdbus_cast<SystemdUnitFileList>(dbusreply.arguments().at(0));
  79. foreach (const SystemdUnitFile &unitFile, list)
  80. {
  81. if (unitFile.path.contains(m_serviceName + ".service"))
  82. {
  83. m_error = "Success";
  84. emit errorChanged();
  85. return true;
  86. }
  87. }
  88. m_error = "Service " + m_serviceName + " doesn't exist";
  89. emit errorChanged();
  90. return false;
  91. }
  92. bool SystemdCommunicator::serviceActive()
  93. {
  94. if (m_serviceInterface && m_serviceInterface->isValid())
  95. {
  96. if (m_serviceInterface->property("ActiveState").toString() == "active")
  97. {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. bool SystemdCommunicator::serviceEnabled()
  104. {
  105. if (m_serviceInterface && m_serviceInterface->isValid())
  106. {
  107. if (m_serviceInterface->property("UnitFileState").toString() == "enabled")
  108. {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. void SystemdCommunicator::setServiceEnabled(bool enabled)
  115. {
  116. if (enabled != serviceEnabled() && serviceExists())
  117. {
  118. QVariantList arguments;
  119. QStringList files = QStringList() << m_serviceName + ".service";
  120. if (enabled)
  121. {
  122. arguments << files << false << true;
  123. dbusAction("EnableUnitFiles", arguments);
  124. dbusAction("Reload");
  125. }
  126. else
  127. {
  128. arguments << files << false;
  129. dbusAction("DisableUnitFiles", arguments);
  130. dbusAction("Reload");
  131. }
  132. emit serviceEnabledChanged();
  133. }
  134. }
  135. void SystemdCommunicator::dbusAction(const QString &method, const QVariantList &arguments)
  136. {
  137. QDBusMessage dbusreply;
  138. if (m_managerInterface && m_managerInterface->isValid())
  139. {
  140. if (arguments.isEmpty())
  141. dbusreply = m_managerInterface->call(QDBus::AutoDetect, method);
  142. else
  143. dbusreply = m_managerInterface->callWithArgumentList(QDBus::AutoDetect, method, arguments);
  144. }
  145. if (dbusreply.type() == QDBusMessage::ErrorMessage)
  146. {
  147. #ifndef NO_KF5_AUTH
  148. if (dbusreply.errorMessage() == "Interactive authentication required.")
  149. {
  150. Action action("fancontrol.gui.helper.dbusaction");
  151. action.setHelperId("fancontrol.gui.helper");
  152. QVariantMap map;
  153. map["method"] = method;
  154. map["arguments"] = arguments;
  155. action.setArguments(map);
  156. ExecuteJob *reply = action.execute();
  157. if (!reply->exec())
  158. {
  159. m_error = reply->errorString();
  160. emit errorChanged();
  161. }
  162. else
  163. {
  164. m_error = method + " succeeded";
  165. emit errorChanged();
  166. }
  167. return;
  168. }
  169. #endif
  170. m_error = dbusreply.errorMessage();
  171. emit errorChanged();
  172. }
  173. else
  174. {
  175. m_error = method + " succeeded";
  176. emit errorChanged();
  177. }
  178. }
  179. QDBusArgument& operator <<(QDBusArgument &argument, const SystemdUnitFile &unitFile)
  180. {
  181. argument.beginStructure();
  182. argument << unitFile.path << unitFile.state;
  183. argument.endStructure();
  184. return argument;
  185. }
  186. const QDBusArgument& operator >>(const QDBusArgument &argument, SystemdUnitFile &unitFile)
  187. {
  188. argument.beginStructure();
  189. argument >> unitFile.path >> unitFile.state;
  190. argument.endStructure();
  191. return argument;
  192. }