GCC Code Coverage Report


Directory: ./
File: service/backenddbuswrapper.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 0 51 0.0%
Branches: 0 30 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2014 Daniel Vratil <dvratil@redhat.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19 #include "backenddbuswrapper.h"
20 #include "backendadaptor.h"
21 #include "backendloader.h"
22 #include "disman_backend_launcher_debug.h"
23
24 #include "backend.h"
25 #include "config.h"
26 #include "configserializer_p.h"
27
28 #include <QDBusConnection>
29 #include <QDBusError>
30
31 BackendDBusWrapper::BackendDBusWrapper(Disman::Backend* backend)
32 : QObject()
33 , mBackend(backend)
34 {
35 connect(mBackend,
36 &Disman::Backend::config_changed,
37 this,
38 &BackendDBusWrapper::backendConfigChanged);
39
40 mChangeCollector.setSingleShot(true);
41 mChangeCollector.setInterval(200); // wait for 200 msecs without any change
42 // before actually emitting configChanged
43 connect(&mChangeCollector, &QTimer::timeout, this, &BackendDBusWrapper::doEmitConfigChanged);
44 }
45
46 BackendDBusWrapper::~BackendDBusWrapper()
47 {
48 }
49
50 bool BackendDBusWrapper::init()
51 {
52 QDBusConnection dbus = QDBusConnection::sessionBus();
53 new BackendAdaptor(this);
54 if (!dbus.registerObject(QStringLiteral("/backend"), this, QDBusConnection::ExportAdaptors)) {
55 qCWarning(DISMAN_BACKEND_LAUNCHER)
56 << "Failed to export backend to DBus: another launcher already running?";
57 qCWarning(DISMAN_BACKEND_LAUNCHER) << dbus.lastError().message();
58 return false;
59 }
60
61 return true;
62 }
63
64 QVariantMap BackendDBusWrapper::getConfig() const
65 {
66 auto const config = mBackend->config();
67 assert(config != nullptr);
68 if (!config) {
69 qCWarning(DISMAN_BACKEND_LAUNCHER) << "Backend provided an empty config!";
70 return QVariantMap();
71 }
72
73 const QJsonObject obj = Disman::ConfigSerializer::serialize_config(mBackend->config());
74 Q_ASSERT(!obj.isEmpty());
75 return obj.toVariantMap();
76 }
77
78 QVariantMap BackendDBusWrapper::setConfig(const QVariantMap& configMap)
79 {
80 if (configMap.isEmpty()) {
81 qCWarning(DISMAN_BACKEND_LAUNCHER) << "Received an empty config map";
82 return QVariantMap();
83 }
84
85 const Disman::ConfigPtr config = Disman::ConfigSerializer::deserialize_config(configMap);
86 mBackend->set_config(config);
87
88 mCurrentConfig = mBackend->config();
89 QMetaObject::invokeMethod(this, "doEmitConfigChanged", Qt::QueuedConnection);
90
91 // TODO: set_config should return adjusted config that was actually applied
92 const QJsonObject obj = Disman::ConfigSerializer::serialize_config(mCurrentConfig);
93 Q_ASSERT(!obj.isEmpty());
94 return obj.toVariantMap();
95 }
96
97 void BackendDBusWrapper::backendConfigChanged(const Disman::ConfigPtr& config)
98 {
99 assert(config != nullptr);
100 if (!config) {
101 qCWarning(DISMAN_BACKEND_LAUNCHER) << "Backend provided an empty config!";
102 return;
103 }
104
105 mCurrentConfig = config;
106 mChangeCollector.start();
107 }
108
109 void BackendDBusWrapper::doEmitConfigChanged()
110 {
111 assert(mCurrentConfig != nullptr);
112 if (!mCurrentConfig) {
113 return;
114 }
115
116 const QJsonObject obj = Disman::ConfigSerializer::serialize_config(mCurrentConfig);
117 Q_EMIT configChanged(obj.toVariantMap());
118
119 mCurrentConfig.reset();
120 mChangeCollector.stop();
121 }
122