| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "helpersysctl.h" |
| 5 | |
| 6 | #include "common/icryptolayer.h" |
| 7 | #include "core/icommandqueue.h" |
| 8 | #include "helperids.h" |
| 9 | #include <QByteArray> |
| 10 | #include <QDBusConnection> |
| 11 | #include <QDBusInterface> |
| 12 | #include <QDBusPendingCall> |
| 13 | #include <QString> |
| 14 | #include <format> |
| 15 | #include <stdexcept> |
| 16 | #include <utility> |
| 17 | |
| 18 | HelperSysCtl::HelperSysCtl(std::shared_ptr<ICryptoLayer> cryptoLayer, |
| 19 | bool logCommands) noexcept |
| 20 | : cryptoLayer_(std::move(cryptoLayer)) |
| 21 | , logCommands_(logCommands) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | void HelperSysCtl::init() |
| 26 | { |
| 27 | sysCtlInterface_ = std::make_unique<QDBusInterface>( |
| 28 | QStringLiteral(DBUS_HELPER_SERVICE), |
| 29 | QStringLiteral(DBUS_HELPER_SYSCTL_PATH), |
| 30 | QStringLiteral(DBUS_HELPER_SYSCTL_INTERFACE), |
| 31 | QDBusConnection::systemBus()); |
| 32 | |
| 33 | if (!sysCtlInterface_->isValid()) { |
| 34 | throw std::runtime_error( |
| 35 | std::format("Cannot connect to D-Bus interface {} (path: {})", |
| 36 | DBUS_HELPER_SYSCTL_INTERFACE, DBUS_HELPER_SYSCTL_PATH)); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void HelperSysCtl::apply(ICommandQueue &ctlCmds) |
| 41 | { |
| 42 | if (logCommands_) |
| 43 | ctlCmds.logCommands(); |
| 44 | |
| 45 | auto data = ctlCmds.toRawData(); |
| 46 | if (!data.isEmpty()) { |
| 47 | auto signature = cryptoLayer_->signature(data); |
| 48 | sysCtlInterface_->asyncCall(QStringLiteral("apply"), data, signature); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void HelperSysCtl::logCommands(bool enable) |
| 53 | { |
| 54 | logCommands_ = enable; |
| 55 | } |