ActionReply Helper::dbusaction(const QVariantMap& args) { ActionReply reply; QDBusMessage dbusreply; // Get arguments to method call QString service = args["service"].toString(); QString path = args["path"].toString(); QString interface = args["interface"].toString(); QString method = args["method"].toString(); QList<QVariant> argsForCall = args["argsForCall"].toList(); QDBusConnection systembus = QDBusConnection::systemBus(); QDBusInterface *iface = new QDBusInterface (service, path, interface, systembus, this); if (iface->isValid()) dbusreply = iface->callWithArgumentList(QDBus::AutoDetect, method, argsForCall); delete iface; // Error handling if (method != "Reexecute") { if (dbusreply.type() == QDBusMessage::ErrorMessage) { reply.setErrorCode(ActionReply::DBusError); reply.setErrorDescription(dbusreply.errorMessage()); } } // Reload systemd daemon to update the enabled/disabled status if (method == "EnableUnitFiles" || method == "DisableUnitFiles" || method == "MaskUnitFiles" || method == "UnmaskUnitFiles") { // systemd does not update properties when these methods are called so we // need to reload the systemd daemon. iface = new QDBusInterface ("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", systembus, this); dbusreply = iface->call(QDBus::AutoDetect, "Reload"); delete iface; } // return a reply return reply; }
ActionReply MyHelper::write(QVariantMap args) { QString filename = args["filename"].toString(); QFile file(filename); if (!file.open(QIODevice::WriteOnly)) { ActionReply reply = ActionReply::HelperErrorReply; reply.setErrorCode(file.error()); return reply; } QTextStream stream(&file); stream << args["contents"].toString(); return ActionReply::SuccessReply; }
ActionReply Helper::createReply(int code, const QVariantMap *returnData) { ActionReply reply; if (code) { reply = ActionReply::HelperError; reply.setErrorCode(code); } else { reply = ActionReply::SuccessReply; } if (returnData) reply.setData(*returnData); return reply; }
ActionReply Helper::executeCommand(const QStringList &command) { KProcess process; process.setProgram(command); process.setOutputChannelMode(KProcess::MergedChannels); qDebug() << "Executing" << command.join(" "); int exitCode = process.execute(); ActionReply reply; if (exitCode != 0) { reply = ActionReply::HelperErrorReply(); //TO BE FIXED reply.setErrorCode(ActionReply::Error::InvalidActionError); } reply.addData("command", command); reply.addData("output", process.readAll()); return reply; }
ActionReply MyHelper::read(QVariantMap args) { ActionReply reply; QString filename = args["filename"].toString(); QFile file(filename); if (!file.open(QIODevice::ReadOnly)) { reply = ActionReply::HelperErrorReply; reply.setErrorCode(file.error()); return reply; } QTextStream stream(&file); QString contents; contents = stream.readAll(); reply.addData("contents", contents); return reply; }