helper.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <QFile>
  21. #include <QTextStream>
  22. #ifndef NO_SYSTEMD
  23. #include <QtDBus>
  24. #endif
  25. ActionReply Helper::action(const QVariantMap &arguments)
  26. {
  27. ActionReply reply;
  28. #ifndef NO_SYSTEMD
  29. if (arguments["action"] == "dbusaction")
  30. {
  31. QString method = arguments["method"].toString();
  32. QVariantList argsForCall = arguments["arguments"].toList();
  33. QDBusConnection systembus = QDBusConnection::systemBus();
  34. QDBusInterface *iface = new QDBusInterface ("org.freedesktop.systemd1",
  35. "/org/freedesktop/systemd1",
  36. "org.freedesktop.systemd1.Manager",
  37. systembus,
  38. this);
  39. QDBusMessage dbusreply;
  40. if (iface->isValid())
  41. dbusreply = iface->callWithArgumentList(QDBus::AutoDetect, method, argsForCall);
  42. delete iface;
  43. if (method != "Reexecute")
  44. {
  45. if (dbusreply.type() == QDBusMessage::ErrorMessage)
  46. {
  47. reply.setErrorCode(ActionReply::DBusError);
  48. reply.setErrorDescription(dbusreply.errorMessage());
  49. }
  50. }
  51. }
  52. else
  53. #endif
  54. if (arguments["action"] == "read")
  55. {
  56. QString filename = arguments["filename"].toString();
  57. QFile file(filename);
  58. if (!file.open(QIODevice::ReadOnly))
  59. {
  60. reply = ActionReply::HelperErrorType;
  61. reply.setErrorCode(ActionReply::AuthorizationDeniedError);
  62. return reply;
  63. }
  64. QTextStream stream(&file);
  65. QString content = stream.readAll();
  66. QVariantMap retdata;
  67. retdata["content"] = content;
  68. reply.setData(retdata);
  69. }
  70. else if (arguments["action"] == "write")
  71. {
  72. QString filename = arguments["filename"].toString();
  73. QFile file(filename);
  74. if (!file.open(QIODevice::WriteOnly))
  75. {
  76. reply = ActionReply::HelperErrorType;
  77. reply.addData("errorDescription", file.errorString());
  78. return reply;
  79. }
  80. QTextStream stream(&file);
  81. stream << arguments["content"].toString();
  82. }
  83. return reply;
  84. }
  85. KAUTH_HELPER_MAIN("fancontrol.gui.helper", Helper)