GCC Code Coverage Report


Directory: ./
File: lib/setconfigoperation.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 37 69 53.6%
Branches: 17 32 53.1%

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 "setconfigoperation.h"
20
21 #include "backend.h"
22 #include "backendmanager_p.h"
23 #include "config.h"
24 #include "configoperation_p.h"
25 #include "configserializer_p.h"
26 #include "disman_debug.h"
27 #include "output.h"
28
29 #include <QDBusPendingCall>
30 #include <QDBusPendingCallWatcher>
31
32 using namespace Disman;
33
34 namespace Disman
35 {
36
37 class SetConfigOperationPrivate : public ConfigOperationPrivate
38 {
39 Q_OBJECT
40
41 public:
42 explicit SetConfigOperationPrivate(const Disman::ConfigPtr& config, ConfigOperation* qq);
43
44 void backend_ready(org::kwinft::disman::backend* backend) override;
45 void onConfigSet(QDBusPendingCallWatcher* watcher);
46 void normalizeOutputPositions();
47
48 Disman::ConfigPtr config;
49
50 private:
51 Q_DECLARE_PUBLIC(SetConfigOperation)
52 };
53
54 }
55
56 19 SetConfigOperationPrivate::SetConfigOperationPrivate(const ConfigPtr& config, ConfigOperation* qq)
57 : ConfigOperationPrivate(qq)
58 19 , config(config)
59 {
60 19 }
61
62 void SetConfigOperationPrivate::backend_ready(org::kwinft::disman::backend* backend)
63 {
64 ConfigOperationPrivate::backend_ready(backend);
65
66 Q_Q(SetConfigOperation);
67
68 if (!backend) {
69 q->set_error(tr("Failed to prepare backend"));
70 q->emit_result();
71 return;
72 }
73
74 const QVariantMap map = ConfigSerializer::serialize_config(config).toVariantMap();
75 if (map.isEmpty()) {
76 q->set_error(tr("Failed to serialize request"));
77 q->emit_result();
78 return;
79 }
80
81 QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(backend->setConfig(map), this);
82 connect(
83 watcher, &QDBusPendingCallWatcher::finished, this, &SetConfigOperationPrivate::onConfigSet);
84 }
85
86 void SetConfigOperationPrivate::onConfigSet(QDBusPendingCallWatcher* watcher)
87 {
88 Q_Q(SetConfigOperation);
89
90 QDBusPendingReply<QVariantMap> reply = *watcher;
91 watcher->deleteLater();
92
93 if (reply.isError()) {
94 q->set_error(reply.error().message());
95 q->emit_result();
96 return;
97 }
98
99 config = ConfigSerializer::deserialize_config(reply.value());
100 if (!config) {
101 q->set_error(tr("Failed to deserialize backend response"));
102 }
103
104 q->emit_result();
105 }
106
107 19 SetConfigOperation::SetConfigOperation(const ConfigPtr& config, QObject* parent)
108 19 : ConfigOperation(new SetConfigOperationPrivate(config, this), parent)
109 {
110 19 }
111
112 72 SetConfigOperation::~SetConfigOperation()
113 {
114 72 }
115
116 ConfigPtr SetConfigOperation::config() const
117 {
118 Q_D(const SetConfigOperation);
119 return d->config;
120 }
121
122 19 void SetConfigOperation::start()
123 {
124 19 Q_D(SetConfigOperation);
125 19 d->normalizeOutputPositions();
126
1/2
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 if (BackendManager::instance()->method() == BackendManager::InProcess) {
127 19 auto backend = d->loadBackend();
128 19 backend->set_config(d->config);
129 19 emit_result();
130 } else {
131 d->request_backend();
132 }
133 19 }
134
135 19 void SetConfigOperationPrivate::normalizeOutputPositions()
136 {
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (!config) {
138 15 return;
139 }
140 19 double offsetX = INT_MAX;
141 19 double offsetY = INT_MAX;
142
2/2
✓ Branch 9 taken 47 times.
✓ Branch 10 taken 19 times.
66 for (auto const& [key, output] : config->outputs()) {
143
2/2
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 44 times.
47 if (!output->positionable()) {
144 3 continue;
145 }
146 44 offsetX = qMin(output->geometry().left(), offsetX);
147 44 offsetY = qMin(output->geometry().top(), offsetY);
148 19 }
149
150
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
19 if (!offsetX && !offsetY) {
151 15 return;
152 }
153
2/2
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 4 times.
8 qCDebug(DISMAN) << "Correcting output positions by:" << QPoint(offsetX, offsetY);
154
2/2
✓ Branch 9 taken 6 times.
✓ Branch 10 taken 4 times.
10 for (auto const& [key, output] : config->outputs()) {
155
2/2
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
6 if (!output->enabled()) {
156 3 continue;
157 }
158 auto newPos
159 3 = QPointF(output->geometry().left() - offsetX, output->geometry().top() - offsetY);
160
2/2
✓ Branch 19 taken 3 times.
✓ Branch 20 taken 3 times.
6 qCDebug(DISMAN) << "Moved output from" << output->geometry().topLeft() << "to" << newPos;
161 3 output->set_position(newPos);
162 4 }
163 }
164
165 #include "setconfigoperation.moc"
166