Libksysguard

SensorQuery.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "SensorQuery.h"
8
9#include <QCollator>
10#include <QDBusPendingCallWatcher>
11#include <QDBusReply>
12#include <QRegularExpression>
13
14#include "SensorDaemonInterface_p.h"
15#include "sensors_logging.h"
16
17using namespace KSysGuard;
18
19class SensorQuery::Private
20{
21public:
22 enum class State { Initial, Running, Finished };
23
24 void updateResult(const QDBusPendingReply<SensorInfoMap> &reply);
25
26 QString path;
27 State state = State::Initial;
29
30 QDBusPendingCallWatcher *watcher = nullptr;
31};
32
33KSysGuard::SensorQuery::SensorQuery(const QString &path, QObject *parent)
34 : QObject(parent)
35 , d(std::make_unique<Private>())
36{
37 d->path = path;
38}
39
40KSysGuard::SensorQuery::~SensorQuery()
41{
42}
43
44QString KSysGuard::SensorQuery::path() const
45{
46 return d->path;
47}
48
49void KSysGuard::SensorQuery::setPath(const QString &path)
50{
51 if (path == d->path) {
52 return;
53 }
54
55 if (d->state != Private::State::Initial) {
56 qCWarning(LIBKSYSGUARD_SENSORS) << "Cannot modify a running or finished query";
57 return;
58 }
59
60 d->path = path;
61}
62
64{
65 if (d->state != Private::State::Initial) {
66 return false;
67 }
68
69 d->state = Private::State::Running;
70
71 auto watcher = SensorDaemonInterface::instance()->allSensors();
72 d->watcher = watcher;
73 connect(watcher, &QDBusPendingCallWatcher::finished, this, [watcher, this]() {
74 watcher->deleteLater();
75 d->watcher = nullptr;
76 d->state = Private::State::Finished;
77 d->updateResult(QDBusPendingReply<SensorInfoMap>(*watcher));
78 Q_EMIT finished(this);
79 });
80
81 return true;
82}
83
85{
86 if (!d->watcher) {
87 return false;
88 }
89
90 d->watcher->waitForFinished();
91 return true;
92}
93
95{
96 QStringList ids;
97 std::transform(d->result.cbegin(), d->result.cend(), std::back_inserter(ids), [](auto entry) {
98 return entry.first;
99 });
100 return ids;
101}
102
104{
105 QCollator collator;
106 collator.setNumericMode(true);
107 std::sort(d->result.begin(), d->result.end(), [&collator](const QPair<QString, SensorInfo> &left, const QPair<QString, SensorInfo> &right) {
108 return collator.compare(left.second.name, right.second.name) < 0;
109 });
110}
111
112QList<QPair<QString, SensorInfo>> KSysGuard::SensorQuery::result() const
113{
114 return d->result;
115}
116
117void KSysGuard::SensorQuery::Private::updateResult(const QDBusPendingReply<SensorInfoMap> &reply)
118{
119 if (path.isEmpty()) { // add everything
120 const SensorInfoMap response = reply.value();
121 for (auto it = response.constBegin(); it != response.constEnd(); it++) {
122 result.append(qMakePair(it.key(), it.value()));
123 }
124 return;
125 }
126
127 auto regexp = QRegularExpression{QStringLiteral("^") % path % QStringLiteral("$")};
128
129 const auto sensorIds = reply.value().keys();
130 for (auto id : sensorIds) {
131 if (id == path || regexp.match(id).hasMatch()) {
132 result.append(qMakePair(id, reply.value().value(id)));
133 }
134 }
135}
bool waitForFinished()
Wait for the query to finish.
bool execute()
Start processing the query.
QStringList sensorIds() const
A list of sensors ids that match the query.
void sortByName()
Sort the retrieved sensors by their user visible names.
QString path(const QString &relativePath)
void setNumericMode(bool on)
void finished(QDBusPendingCallWatcher *self)
typename Select< 0 >::Type value() const const
const_iterator constBegin() const const
const_iterator constEnd() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:17:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.