Changeset View
Changeset View
Standalone View
Standalone View
sensors/SensorQuery.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2020 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 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 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "SensorQuery.h" | ||||
21 | | ||||
22 | #include <QDBusPendingCallWatcher> | ||||
23 | #include <QDBusReply> | ||||
24 | #include <QRegularExpression> | ||||
25 | | ||||
26 | #include "SensorDaemonInterface_p.h" | ||||
27 | #include "sensors_logging.h" | ||||
28 | | ||||
29 | using namespace KSysGuard; | ||||
30 | | ||||
31 | class SensorQuery::Private | ||||
32 | { | ||||
33 | public: | ||||
34 | enum class State { | ||||
35 | Initial, | ||||
36 | Running, | ||||
37 | Finished | ||||
38 | }; | ||||
39 | | ||||
40 | void updateResult(const QDBusPendingReply<SensorInfoMap> &reply); | ||||
41 | | ||||
42 | QString path; | ||||
43 | State state = State::Initial; | ||||
44 | QVector<QPair<QString, SensorInfo>> result; | ||||
45 | | ||||
46 | QDBusPendingCallWatcher *watcher = nullptr; | ||||
47 | }; | ||||
48 | | ||||
49 | KSysGuard::SensorQuery::SensorQuery(const QString& path, QObject* parent) | ||||
50 | : QObject(parent), d(std::make_unique<Private>()) | ||||
51 | { | ||||
52 | d->path = path; | ||||
53 | } | ||||
54 | | ||||
55 | KSysGuard::SensorQuery::~SensorQuery() | ||||
56 | { | ||||
57 | } | ||||
58 | | ||||
59 | QString KSysGuard::SensorQuery::path() const | ||||
60 | { | ||||
61 | return d->path; | ||||
62 | } | ||||
63 | | ||||
64 | void KSysGuard::SensorQuery::setPath(const QString& path) | ||||
65 | { | ||||
66 | if (path == d->path) { | ||||
67 | return; | ||||
68 | } | ||||
69 | | ||||
70 | if (d->state != Private::State::Initial) { | ||||
71 | qCWarning(LIBKSYSGUARD_SENSORS) << "Cannot modify a running or finished query"; | ||||
72 | return; | ||||
73 | } | ||||
74 | | ||||
75 | d->path = path; | ||||
76 | } | ||||
77 | | ||||
78 | bool KSysGuard::SensorQuery::execute() | ||||
79 | { | ||||
80 | if (d->state != Private::State::Initial) { | ||||
81 | return false; | ||||
82 | } | ||||
83 | | ||||
84 | d->state = Private::State::Running; | ||||
85 | | ||||
86 | | ||||
87 | auto watcher = SensorDaemonInterface::instance()->allSensors(); | ||||
88 | d->watcher = watcher; | ||||
89 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [watcher, this]() { | ||||
90 | watcher->deleteLater(); | ||||
91 | d->watcher = nullptr; | ||||
92 | d->state = Private::State::Finished; | ||||
93 | d->updateResult(QDBusPendingReply<SensorInfoMap>(*watcher)); | ||||
94 | Q_EMIT finished(this); | ||||
95 | }); | ||||
96 | | ||||
97 | return true; | ||||
98 | } | ||||
99 | | ||||
100 | bool KSysGuard::SensorQuery::waitForFinished() | ||||
101 | { | ||||
102 | if (!d->watcher) { | ||||
103 | return false; | ||||
104 | } | ||||
105 | | ||||
106 | d->watcher->waitForFinished(); | ||||
107 | return true; | ||||
108 | } | ||||
109 | | ||||
110 | QStringList KSysGuard::SensorQuery::sensorIds() const | ||||
111 | { | ||||
112 | QStringList ids; | ||||
113 | std::transform(d->result.cbegin(), d->result.cend(), std::back_inserter(ids), [](auto entry) { return entry.first; }); | ||||
114 | return ids; | ||||
115 | } | ||||
116 | | ||||
117 | QVector<QPair<QString, SensorInfo>> KSysGuard::SensorQuery::result() const | ||||
118 | { | ||||
119 | return d->result; | ||||
120 | } | ||||
121 | | ||||
122 | void KSysGuard::SensorQuery::Private::updateResult(const QDBusPendingReply<SensorInfoMap> &reply) | ||||
123 | { | ||||
124 | if (path.isEmpty()) { // add everything | ||||
125 | const SensorInfoMap response = reply.value(); | ||||
126 | for (auto it = response.constBegin(); it != response.constEnd(); it++) { | ||||
127 | result.append(qMakePair(it.key(), it.value())); | ||||
128 | } | ||||
129 | return; | ||||
130 | } | ||||
131 | auto regexp = QRegularExpression{QRegularExpression::wildcardToRegularExpression(path)}; | ||||
132 | | ||||
133 | const auto sensorIds = reply.value().keys(); | ||||
134 | for (auto id : sensorIds) { | ||||
135 | if (regexp.match(id).hasMatch()) { | ||||
136 | result.append(qMakePair(id, reply.value().value(id))); | ||||
137 | } | ||||
138 | } | ||||
139 | } |