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 { Initial, Running, Finished }; | ||||
35 | | ||||
36 | void updateResult(const QDBusPendingReply<SensorInfoMap> &reply); | ||||
37 | | ||||
38 | QString path; | ||||
39 | State state = State::Initial; | ||||
40 | QVector<QPair<QString, SensorInfo>> result; | ||||
41 | | ||||
42 | QDBusPendingCallWatcher *watcher = nullptr; | ||||
43 | }; | ||||
44 | | ||||
45 | KSysGuard::SensorQuery::SensorQuery(const QString &path, QObject *parent) | ||||
46 | : QObject(parent) | ||||
47 | , d(std::make_unique<Private>()) | ||||
48 | { | ||||
49 | d->path = path; | ||||
50 | } | ||||
51 | | ||||
52 | KSysGuard::SensorQuery::~SensorQuery() | ||||
53 | { | ||||
54 | } | ||||
55 | | ||||
56 | QString KSysGuard::SensorQuery::path() const | ||||
57 | { | ||||
58 | return d->path; | ||||
59 | } | ||||
60 | | ||||
61 | void KSysGuard::SensorQuery::setPath(const QString &path) | ||||
62 | { | ||||
63 | if (path == d->path) { | ||||
64 | return; | ||||
65 | } | ||||
66 | | ||||
67 | if (d->state != Private::State::Initial) { | ||||
68 | qCWarning(LIBKSYSGUARD_SENSORS) << "Cannot modify a running or finished query"; | ||||
69 | return; | ||||
70 | } | ||||
71 | | ||||
72 | d->path = path; | ||||
73 | } | ||||
74 | | ||||
75 | bool KSysGuard::SensorQuery::execute() | ||||
76 | { | ||||
77 | if (d->state != Private::State::Initial) { | ||||
78 | return false; | ||||
79 | } | ||||
80 | | ||||
81 | d->state = Private::State::Running; | ||||
82 | | ||||
83 | auto watcher = SensorDaemonInterface::instance()->allSensors(); | ||||
84 | d->watcher = watcher; | ||||
85 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [watcher, this]() { | ||||
86 | watcher->deleteLater(); | ||||
87 | d->watcher = nullptr; | ||||
88 | d->state = Private::State::Finished; | ||||
89 | d->updateResult(QDBusPendingReply<SensorInfoMap>(*watcher)); | ||||
90 | Q_EMIT finished(this); | ||||
91 | }); | ||||
92 | | ||||
93 | return true; | ||||
94 | } | ||||
95 | | ||||
96 | bool KSysGuard::SensorQuery::waitForFinished() | ||||
97 | { | ||||
98 | if (!d->watcher) { | ||||
99 | return false; | ||||
100 | } | ||||
101 | | ||||
102 | d->watcher->waitForFinished(); | ||||
103 | return true; | ||||
104 | } | ||||
105 | | ||||
106 | QStringList KSysGuard::SensorQuery::sensorIds() const | ||||
107 | { | ||||
108 | QStringList ids; | ||||
109 | std::transform(d->result.cbegin(), d->result.cend(), std::back_inserter(ids), [](auto entry) { return entry.first; }); | ||||
110 | return ids; | ||||
111 | } | ||||
112 | | ||||
113 | QVector<QPair<QString, SensorInfo>> KSysGuard::SensorQuery::result() const | ||||
114 | { | ||||
115 | return d->result; | ||||
116 | } | ||||
117 | | ||||
118 | void KSysGuard::SensorQuery::Private::updateResult(const QDBusPendingReply<SensorInfoMap> &reply) | ||||
119 | { | ||||
120 | if (path.isEmpty()) { // add everything | ||||
121 | const SensorInfoMap response = reply.value(); | ||||
122 | for (auto it = response.constBegin(); it != response.constEnd(); it++) { | ||||
123 | result.append(qMakePair(it.key(), it.value())); | ||||
124 | } | ||||
125 | return; | ||||
126 | } | ||||
127 | auto regexp = QRegularExpression{QRegularExpression::wildcardToRegularExpression(path)}; | ||||
128 | | ||||
129 | const auto sensorIds = reply.value().keys(); | ||||
130 | for (auto id : sensorIds) { | ||||
131 | if (regexp.match(id).hasMatch()) { | ||||
132 | result.append(qMakePair(id, reply.value().value(id))); | ||||
133 | } | ||||
134 | } | ||||
135 | } |