Libksysguard

SensorQuery.cpp
1 /*
2  Copyright (C) 2020 Arjen Hiemstra <[email protected]>
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 <QCollator>
23 #include <QDBusPendingCallWatcher>
24 #include <QDBusReply>
25 #include <QRegularExpression>
26 
27 #include "SensorDaemonInterface_p.h"
28 #include "sensors_logging.h"
29 
30 using namespace KSysGuard;
31 
32 class SensorQuery::Private
33 {
34 public:
35  enum class State { Initial, Running, Finished };
36 
37  void updateResult(const QDBusPendingReply<SensorInfoMap> &reply);
38 
39  QString path;
40  State state = State::Initial;
42 
43  QDBusPendingCallWatcher *watcher = nullptr;
44 };
45 
46 KSysGuard::SensorQuery::SensorQuery(const QString &path, QObject *parent)
47  : QObject(parent)
48  , d(std::make_unique<Private>())
49 {
50  d->path = path;
51 }
52 
53 KSysGuard::SensorQuery::~SensorQuery()
54 {
55 }
56 
57 QString KSysGuard::SensorQuery::path() const
58 {
59  return d->path;
60 }
61 
62 void KSysGuard::SensorQuery::setPath(const QString &path)
63 {
64  if (path == d->path) {
65  return;
66  }
67 
68  if (d->state != Private::State::Initial) {
69  qCWarning(LIBKSYSGUARD_SENSORS) << "Cannot modify a running or finished query";
70  return;
71  }
72 
73  d->path = path;
74 }
75 
77 {
78  if (d->state != Private::State::Initial) {
79  return false;
80  }
81 
82  d->state = Private::State::Running;
83 
84  auto watcher = SensorDaemonInterface::instance()->allSensors();
85  d->watcher = watcher;
86  connect(watcher, &QDBusPendingCallWatcher::finished, this, [watcher, this]() {
87  watcher->deleteLater();
88  d->watcher = nullptr;
89  d->state = Private::State::Finished;
90  d->updateResult(QDBusPendingReply<SensorInfoMap>(*watcher));
91  Q_EMIT finished(this);
92  });
93 
94  return true;
95 }
96 
98 {
99  if (!d->watcher) {
100  return false;
101  }
102 
103  d->watcher->waitForFinished();
104  return true;
105 }
106 
108 {
109  QStringList ids;
110  std::transform(d->result.cbegin(), d->result.cend(), std::back_inserter(ids), [](auto entry) { return entry.first; });
111  return ids;
112 }
113 
115 {
116  QCollator collator;
117  collator.setNumericMode(true);
118  std::sort(d->result.begin(), d->result.end(), [this, &collator] (const QPair<QString, SensorInfo> &left, const QPair<QString, SensorInfo> &right) {
119  return collator.compare(left.second.name, right.second.name) < 0;
120  });
121 }
122 
123 QVector<QPair<QString, SensorInfo>> KSysGuard::SensorQuery::result() const
124 {
125  return d->result;
126 }
127 
128 void KSysGuard::SensorQuery::Private::updateResult(const QDBusPendingReply<SensorInfoMap> &reply)
129 {
130  if (path.isEmpty()) { // add everything
131  const SensorInfoMap response = reply.value();
132  for (auto it = response.constBegin(); it != response.constEnd(); it++) {
133  result.append(qMakePair(it.key(), it.value()));
134  }
135  return;
136  }
137 
138  auto regexp = QRegularExpression{QStringLiteral("^") % path % QStringLiteral("$")};
139 
140  const auto sensorIds = reply.value().keys();
141  for (auto id : sensorIds) {
142  if (id == path || regexp.match(id).hasMatch()) {
143  result.append(qMakePair(id, reply.value().value(id)));
144  }
145  }
146 }
void finished(QDBusPendingCallWatcher *self)
bool execute()
Start processing the query.
Definition: SensorQuery.cpp:76
void sortByName()
Sort the retrieved sensors by their user visible names.
bool waitForFinished()
Wait for the query to finish.
Definition: SensorQuery.cpp:97
QHash::const_iterator constEnd() const const
QStringList sensorIds() const
A list of sensors ids that match the query.
bool isEmpty() const const
void setNumericMode(bool on)
T1 value() const const
QHash::const_iterator constBegin() const const
int compare(const QString &s1, const QString &s2) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
State
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Mar 2 2021 02:46:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.