File indexing completed on 2024-04-28 05:47:28

0001 /*******************************************************************************
0002  * Copyright (C) 2016 Ragnar Thomsen <rthomsen6@gmail.com>                     *
0003  *                                                                             *
0004  * This program is free software: you can redistribute it and/or modify it     *
0005  * under the terms of the GNU General Public License as published by the Free  *
0006  * Software Foundation, either version 2 of the License, or (at your option)   *
0007  * any later version.                                                          *
0008  *                                                                             *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT *
0010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
0011  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
0012  * more details.                                                               *
0013  *                                                                             *
0014  * You should have received a copy of the GNU General Public License along     *
0015  * with this program. If not, see <http://www.gnu.org/licenses/>.              *
0016  *******************************************************************************/
0017 
0018 #include "helper.h"
0019 
0020 #include <QtDBus>
0021 #include <QFile>
0022 
0023 #include <KAuth/HelperSupport>
0024 
0025 ActionReply Helper::saveunitfile(const QVariantMap& args)
0026 {
0027     ActionReply reply;
0028 
0029     QFile file(args[QStringLiteral("file")].toString());
0030     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0031         reply = ActionReply::HelperErrorReply();
0032         reply.addData(QStringLiteral("errorDescription"), file.errorString());
0033         //reply.setErrorCode(file.error());
0034         //reply.addData("filename", iter.key());
0035         return reply;
0036     }
0037     QTextStream stream(&file);
0038     stream << args[QStringLiteral("contents")].toString();
0039     file.close();
0040 
0041     return reply;
0042 }
0043 
0044 ActionReply Helper::dbusaction(const QVariantMap& args)
0045 {
0046     ActionReply reply;
0047     QDBusMessage dbusreply;
0048 
0049     // Get arguments to method call
0050     QString service = args[QStringLiteral("service")].toString();
0051     QString path = args[QStringLiteral("path")].toString();
0052     QString interface = args[QStringLiteral("interface")].toString();
0053     QString method = args[QStringLiteral("method")].toString();
0054     QList<QVariant> argsForCall = args[QStringLiteral("argsForCall")].toList();
0055 
0056     QDBusInterface *iface = new QDBusInterface(service,
0057                                                path,
0058                                                interface,
0059                                                QDBusConnection::systemBus(),
0060                                                this);
0061     if (iface->isValid())
0062         dbusreply = iface->callWithArgumentList(QDBus::AutoDetect, method, argsForCall);
0063     delete iface;
0064 
0065     // Error handling
0066     if (method != QLatin1String("Reexecute"))
0067     {
0068         if (dbusreply.type() == QDBusMessage::ErrorMessage)
0069         {
0070             reply.setErrorCode(ActionReply::DBusError);
0071             reply.setErrorDescription(dbusreply.errorMessage());
0072         }
0073     }
0074 
0075     // Reload systemd daemon to update the enabled/disabled status
0076     QRegularExpression rxMethods(QStringLiteral("EnableUnitFiles|DisableUnitFiles|MaskUnitFiles|UnmaskUnitFiles"));
0077     if (rxMethods.match(method).hasMatch())
0078     {
0079         // systemd does not update properties when these methods are called so we
0080         // need to reload the systemd daemon.
0081         iface = new QDBusInterface (QStringLiteral("org.freedesktop.systemd1"),
0082                                     QStringLiteral("/org/freedesktop/systemd1"),
0083                                     QStringLiteral("org.freedesktop.systemd1.Manager"),
0084                                     QDBusConnection::systemBus(),
0085                                     this);
0086         dbusreply = iface->call(QDBus::AutoDetect, QStringLiteral("Reload"));
0087         delete iface;
0088     }
0089     return reply;
0090 }
0091 
0092 KAUTH_HELPER_MAIN("org.kde.kcontrol.systemdgenie", Helper)