helper.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "helper.h"
  20. #include <QtCore/QFile>
  21. #include <QtCore/QTextStream>
  22. #include <QtCore/QProcess>
  23. #include <QtCore/QFileInfo>
  24. #include <KAuth/KAuthHelperSupport>
  25. #include <KI18n/KLocalizedString>
  26. #ifndef NO_SYSTEMD
  27. #include <QtDBus/QDBusInterface>
  28. #include <QtDBus/QDBusMetaType>
  29. #include <QtDBus/QDBusReply>
  30. #include <QtDBus/QDBusVariant>
  31. struct StringStruct
  32. {
  33. QString type;
  34. QString filename;
  35. QString destination;
  36. };
  37. typedef QList<StringStruct> StringStructArray;
  38. Q_DECLARE_METATYPE(StringStruct)
  39. Q_DECLARE_METATYPE(StringStructArray)
  40. QDBusArgument &operator<<(QDBusArgument &argument, const StringStruct &structure)
  41. {
  42. argument.beginStructure();
  43. argument << structure.type << structure.filename << structure.destination;
  44. argument.endStructure();
  45. return argument;
  46. }
  47. const QDBusArgument &operator>>(const QDBusArgument &argument, StringStruct &structure)
  48. {
  49. argument.beginStructure();
  50. argument >> structure.type >> structure.filename >> structure.destination;
  51. argument.endStructure();
  52. return argument;
  53. }
  54. #endif
  55. ActionReply Helper::action(const QVariantMap &arguments)
  56. {
  57. ActionReply reply;
  58. #ifndef NO_SYSTEMD
  59. if (arguments[QStringLiteral("action")] == "dbusaction")
  60. {
  61. qDBusRegisterMetaType<StringStruct>();
  62. qDBusRegisterMetaType<StringStructArray>();
  63. const auto method = arguments[QStringLiteral("method")].toString();
  64. const auto argsForCall = arguments[QStringLiteral("arguments")].toList();
  65. const auto systembus = QDBusConnection::systemBus();
  66. const auto iface = new QDBusInterface (QStringLiteral("org.freedesktop.systemd1"),
  67. QStringLiteral("/org/freedesktop/systemd1"),
  68. QStringLiteral("org.freedesktop.systemd1.Manager"),
  69. systembus,
  70. this);
  71. QDBusMessage dbusmessage;
  72. if (iface->isValid())
  73. {
  74. if (argsForCall.isEmpty())
  75. dbusmessage = iface->call(QDBus::AutoDetect, method);
  76. else
  77. dbusmessage = iface->callWithArgumentList(QDBus::AutoDetect, method, argsForCall);
  78. if (method != QStringLiteral("Reexecute"))
  79. {
  80. if (dbusmessage.type() == QDBusMessage::ErrorMessage)
  81. {
  82. reply.setErrorCode(ActionReply::DBusError);
  83. reply.setErrorDescription(dbusmessage.errorMessage());
  84. }
  85. else if (dbusmessage.type() == QDBusMessage::ReplyMessage)
  86. {
  87. if (dbusmessage.signature() == QStringLiteral("a(sss)"))
  88. {
  89. QDBusReply<StringStructArray> dbusreply(dbusmessage);
  90. if (dbusreply.isValid())
  91. {
  92. QMap<QString, QVariant> map;
  93. map.insert(QStringLiteral("type"), dbusreply.value().value(0).type);
  94. map.insert(QStringLiteral("filename"), dbusreply.value().value(0).filename);
  95. map.insert(QStringLiteral("destination"), dbusreply.value().value(0).destination);
  96. reply.addData(QStringLiteral("reply"), map);
  97. }
  98. else
  99. {
  100. reply = ActionReply::HelperErrorReply();
  101. reply.setErrorDescription(dbusreply.error().message());
  102. }
  103. }
  104. else if (dbusmessage.signature() == QStringLiteral("ba(sss)"))
  105. {
  106. QDBusReply<bool> dbusreply(dbusmessage); //QDBusReply only extracts the first return argument("b")
  107. if (dbusreply.isValid())
  108. {
  109. QMap<QString, QVariant> map;
  110. map.insert(QStringLiteral("enableInfo"), dbusreply.value());
  111. auto changes = qdbus_cast<StringStructArray>(qvariant_cast<QDBusArgument>(dbusmessage.arguments().value(1))); //Extract the second argument("a(sss)")
  112. map.insert(QStringLiteral("type"), changes.value(0).type);
  113. map.insert(QStringLiteral("filename"), changes.value(0).filename);
  114. map.insert(QStringLiteral("destination"), changes.value(0).destination);
  115. reply.addData(QStringLiteral("reply"), map);
  116. }
  117. else
  118. {
  119. reply = ActionReply::HelperErrorReply();
  120. reply.setErrorDescription(dbusreply.error().message());
  121. }
  122. }
  123. else if (dbusmessage.signature() == QStringLiteral("o"))
  124. {
  125. QDBusReply<QDBusObjectPath> dbusreply(dbusmessage);
  126. if (dbusreply.isValid())
  127. {
  128. QMap<QString, QVariant> map;
  129. map.insert(QStringLiteral("job"), dbusreply.value().path());
  130. reply.addData(QStringLiteral("reply"), map);
  131. }
  132. else
  133. {
  134. reply = ActionReply::HelperErrorReply();
  135. reply.setErrorDescription(dbusreply.error().message());
  136. }
  137. }
  138. }
  139. }
  140. }
  141. else
  142. {
  143. reply = ActionReply::HelperErrorReply();
  144. reply.setErrorDescription(i18n("Could not create dbus interface"));
  145. }
  146. delete iface;
  147. }
  148. else
  149. #endif
  150. if (arguments[QStringLiteral("action")] == "read")
  151. {
  152. const auto filename = arguments[QStringLiteral("filename")].toString();
  153. QFile file(filename);
  154. if (file.open(QIODevice::ReadOnly))
  155. {
  156. QTextStream stream(&file);
  157. const auto content = stream.readAll();
  158. reply.addData(QStringLiteral("content"), content);
  159. }
  160. else
  161. {
  162. reply = ActionReply::HelperErrorReply();
  163. reply.setErrorDescription(file.errorString());
  164. }
  165. }
  166. else if (arguments[QStringLiteral("action")] == "write")
  167. {
  168. const auto filename = arguments[QStringLiteral("filename")].toString();
  169. QFile file(filename);
  170. if (file.open(QIODevice::WriteOnly))
  171. {
  172. QTextStream stream(&file);
  173. stream << arguments[QStringLiteral("content")].toString();
  174. }
  175. else
  176. {
  177. reply = ActionReply::HelperErrorReply();
  178. reply.setErrorDescription(file.errorString());
  179. }
  180. }
  181. else if (arguments[QStringLiteral("action")] == "detectSensors")
  182. {
  183. const auto program = QStringLiteral("sensors-detect");
  184. const auto arguments = QStringList() << QStringLiteral("--auto");
  185. QProcess process;
  186. process.start(program, arguments);
  187. if (!process.waitForStarted(1000))
  188. {
  189. reply = ActionReply::HelperErrorReply();
  190. reply.setErrorDescription(process.errorString());
  191. }
  192. else if (!process.waitForFinished(10000))
  193. {
  194. reply = ActionReply::HelperErrorReply();
  195. reply.setErrorDescription(process.errorString());
  196. }
  197. }
  198. else
  199. {
  200. reply = ActionReply::HelperErrorReply();
  201. reply.setErrorDescription(i18n("This action does not exist!"));
  202. }
  203. return reply;
  204. }
  205. KAUTH_HELPER_MAIN("fancontrol.gui.helper", Helper)