GCC Code Coverage Report


Directory: ./
File: lib/configoperation.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 45 62 72.6%
Branches: 7 18 38.9%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2014 Daniel Vratil <dvratil@redhat.com>
3 * Copyright 2015 Sebastian K├╝gler <sebas@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20 #include "configoperation.h"
21 #include "backendmanager_p.h"
22 #include "configoperation_p.h"
23
24 #include "disman_debug.h"
25
26 using namespace Disman;
27
28 80 ConfigOperationPrivate::ConfigOperationPrivate(ConfigOperation* qq)
29 : QObject()
30 80 , isExec(false)
31 80 , q_ptr(qq)
32 {
33 80 }
34
35 158 ConfigOperationPrivate::~ConfigOperationPrivate()
36 {
37 }
38
39 void ConfigOperationPrivate::request_backend()
40 {
41 Q_ASSERT(BackendManager::instance()->method() == BackendManager::OutOfProcess);
42 connect(BackendManager::instance(),
43 &BackendManager::backend_ready,
44 this,
45 &ConfigOperationPrivate::backend_ready);
46 BackendManager::instance()->request_backend();
47 }
48
49 void ConfigOperationPrivate::backend_ready(org::kwinft::disman::backend* backend)
50 {
51 Q_ASSERT(BackendManager::instance()->method() == BackendManager::OutOfProcess);
52 Q_UNUSED(backend);
53
54 disconnect(BackendManager::instance(),
55 &BackendManager::backend_ready,
56 this,
57 &ConfigOperationPrivate::backend_ready);
58 }
59
60 79 void ConfigOperationPrivate::do_emit_result()
61 {
62 79 Q_Q(ConfigOperation);
63
64 79 Q_EMIT q->finished(q);
65
66 // Don't call deleteLater() when this operation is running from exec()
67 // because then the operation will be deleted when we return control to
68 // the nested QEventLoop in exec() (i.e. before loop.exec() returns)
69 // and subsequent has_error() call references deleted "this". Instead we
70 // shedule the operation for deletion manually in exec(), so that it will
71 // be deleted when control returns to parent event loop (or QApplication).
72
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 77 times.
79 if (!isExec) {
73 2 q->deleteLater();
74 }
75 79 }
76
77 80 ConfigOperation::ConfigOperation(ConfigOperationPrivate* dd, QObject* parent)
78 : QObject(parent)
79 80 , d_ptr(dd)
80 {
81 80 const bool ok = QMetaObject::invokeMethod(this, "start", Qt::QueuedConnection);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 Q_ASSERT(ok);
83 Q_UNUSED(ok);
84 80 }
85
86 158 ConfigOperation::~ConfigOperation()
87 {
88
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
158 delete d_ptr;
89 }
90
91 82 bool ConfigOperation::has_error() const
92 {
93 82 Q_D(const ConfigOperation);
94 82 return !d->error.isEmpty();
95 }
96
97 QString ConfigOperation::error_string() const
98 {
99 Q_D(const ConfigOperation);
100 return d->error;
101 }
102
103 void ConfigOperation::set_error(const QString& error)
104 {
105 Q_D(ConfigOperation);
106 d->error = error;
107 }
108
109 80 void ConfigOperation::emit_result()
110 {
111 80 Q_D(ConfigOperation);
112 80 const bool ok = QMetaObject::invokeMethod(d, "do_emit_result", Qt::QueuedConnection);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 Q_ASSERT(ok);
114 Q_UNUSED(ok);
115 80 }
116
117 77 bool ConfigOperation::exec()
118 {
119 77 Q_D(ConfigOperation);
120
121 77 QEventLoop loop;
122 77 connect(this, &ConfigOperation::finished, this, [&](ConfigOperation* op) {
123 Q_UNUSED(op);
124 77 loop.quit();
125 77 });
126
127 77 d->isExec = true;
128 77 loop.exec(QEventLoop::ExcludeUserInputEvents);
129
130 // Schedule the operation for deletion, see doemit_result()
131 77 deleteLater();
132 77 return !has_error();
133 77 }
134
135 80 Disman::Backend* ConfigOperationPrivate::loadBackend()
136 {
137
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
80 Q_ASSERT(BackendManager::instance()->method() == BackendManager::InProcess);
138 80 Q_Q(ConfigOperation);
139 80 const QString& name = QString::fromUtf8(qgetenv("DISMAN_BACKEND"));
140 80 auto backend = Disman::BackendManager::instance()->load_backend_in_process(name);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (backend == nullptr) {
142 const QString& e = QStringLiteral("Plugin does not provide valid Disman backend");
143 qCDebug(DISMAN) << e;
144 q->set_error(e);
145 q->emit_result();
146 }
147 160 return backend;
148 80 }
149