Debian Codegraph @

src/publishsubscribe/qvaluespacesubscriber.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include <qvaluespacesubscriber.h>
41
42 #include "qvaluespacemanager_p.h"
43 #include "qvaluespacesubscriber_p.h"
44
45 #include <QtCore/qmetaobject.h>
46 #include <QtCore/qset.h>
47 #include <QtCore/qstringlist.h>
48
49 QT_BEGIN_NAMESPACE
50
51 /*!
52 \class QValueSpaceSubscriber
53 \brief The QValueSpaceSubscriber class allows applications to read and subscribe to Value Space paths.
54 \inmodule QtPublishSubscribe
55 \ingroup publishsubscribe
56
57 By default QValueSpaceSubscriber can read values from and report change notifications for all
58 available Value Space layers. Only data from the layer with the highest priority and that
59 contains the specific key is returned by this class.
60
61 The layers that QValueSpaceSubscriber accesses can be limited by specifying either a
62 \l {QValueSpace::LayerOptions}{filter} or a QUuid at construction time.
63
64 Applications subscribe to a particular path in the Value Space. If anything under that path
65 changes the contextChanged() signal is emitted. For example given the schema:
66
67 \code
68 /Device/Buttons = 3
69 /Device/Buttons/1/Name = Menu
70 /Device/Buttons/1/Usable = true
71 /Device/Buttons/2/Name = Select
72 /Device/Buttons/2/Usable = false
73 /Device/Buttons/3/Name = Back
74 /Device/Buttons/3/Usable = true
75 \endcode
76
77 The code:
78
79 \code
80 QValueSpaceSubscriber *buttons = new QValueSpaceSubscriber("/Device/Buttons");
81 QObject::connect(buttons, SIGNAL(contentsChanged()), this, SLOT(buttonInfoChanged()));
82 \endcode
83
84 will invoke the \c {buttonInfoChanged()} slot whenever any value under \c {/Device/Buttons}
85 changes. This includes the value of \c {/Device/Buttons} itself, a change of a subpath such as
86 \c {/Device/Buttons/2/Name} or the creation or removal of a subpath.
87 */
88
89 /*!
90 \fn QValueSpaceSubscriber::contentsChanged()
91
92 Emitted whenever any value under the current path changes.
93
94 \b {Note:} that if a value changes multiple times in quick succession, only the most recent
95 value may be accessible via the value() function.
96 */
97
98 void QValueSpaceSubscriberPrivateProxy::handleChanged(quintptr handle)
99 {
100 QAbstractValueSpaceLayer *layer = qobject_cast<QAbstractValueSpaceLayer *>(sender());
101
102 for (int i = 0; i < readers.count(); ++i) {
103 if (readers.at(i).first == layer && readers.at(i).second == handle) {
104 emit changed();
105 return;
106 }
107 }
108 }
109
110 static LayerList matchLayers(const QString &path, QValueSpace::LayerOptions filter)
111 {
112 LayerList list;
113
114 QValueSpaceManager *manager = QValueSpaceManager::instance();
115 if (!manager)
116 return list;
117
118 // Invalid filter combination.
119 if ((filter & QValueSpace::PermanentLayer && filter & QValueSpace::TransientLayer)
120 || (filter & QValueSpace::WritableLayer && filter & QValueSpace::ReadOnlyLayer)) {
121 return list;
122 }
123
124 const QList<QAbstractValueSpaceLayer *> &readerList = manager->getLayers();
125
126 for (int ii = 0; ii < readerList.count(); ++ii) {
127 QAbstractValueSpaceLayer *read = readerList.at(ii);
128 if (filter != QValueSpace::UnspecifiedLayer &&
129 !(read->layerOptions() & filter)) {
130 continue;
131 }
132
133 QAbstractValueSpaceLayer::Handle handle =
134 read->item(QAbstractValueSpaceLayer::InvalidHandle, path);
135 if (QAbstractValueSpaceLayer::InvalidHandle != handle) {
136 list.append(qMakePair(read, handle));
137
138 read->notifyInterest(handle, true);
139 }
140 }
141
142 return list;
143 }
144
145 QValueSpaceSubscriberPrivate::QValueSpaceSubscriberPrivate(const QString &path, QValueSpace::LayerOptions filter)
146 : path(qCanonicalPath(path))
147 , readers(matchLayers(this->path, filter))
148 , connections(0)
149 {
150 }
151
152 static LayerList matchLayers(const QString &path, const QUuid &uuid)
153 {
154 LayerList list;
155
156 QValueSpaceManager *manager = QValueSpaceManager::instance();
157 if (!manager)
158 return list;
159
160 const QList<QAbstractValueSpaceLayer *> &readerList = manager->getLayers();
161
162 for (int ii = 0; ii < readerList.count(); ++ii) {
163 QAbstractValueSpaceLayer *read = readerList.at(ii);
164 if (read->id() != uuid)
165 continue;
166
167 QAbstractValueSpaceLayer::Handle handle =
168 read->item(QAbstractValueSpaceLayer::InvalidHandle, path);
169 if (QAbstractValueSpaceLayer::InvalidHandle != handle) {
170 list.append(qMakePair(read, handle));
171
172 read->notifyInterest(handle, true);
173 }
174 }
175
176 return list;
177 }
178
179 QValueSpaceSubscriberPrivate::QValueSpaceSubscriberPrivate(const QString &path, const QUuid &uuid)
180 : path(qCanonicalPath(path))
181 , readers(matchLayers(this->path, uuid))
182 , connections(0)
183 {
184 }
185
186 QValueSpaceSubscriberPrivate::~QValueSpaceSubscriberPrivate()
187 {
188 for (int ii = 0; ii < readers.count(); ++ii) {
189 readers[ii].first->notifyInterest(readers[ii].second, false);
190 readers[ii].first->removeHandle(readers[ii].second);
191 }
192
193 if (connections)
194 delete connections;
195 }
196
197 void QValueSpaceSubscriberPrivate::connect(const QValueSpaceSubscriber *space) const
198 {
199 QMutexLocker locker(&lock);
200
201 if (!connections) {
202 qRegisterMetaType<quintptr>("quintptr");
203 connections = new QValueSpaceSubscriberPrivateProxy;
204 connections->readers = readers;
205 connections->connections.insert(space,1);
206 QObject::connect(connections, SIGNAL(changed()),
207 space, SIGNAL(contentsChanged()));
208 for (int ii = 0; ii < readers.count(); ++ii) {
209 readers.at(ii).first->setProperty(readers.at(ii).second, QAbstractValueSpaceLayer::Publish);
210 QObject::connect(readers.at(ii).first, SIGNAL(handleChanged(quintptr)), connections, SLOT(handleChanged(quintptr)));
211 }
212 } else if (!connections->connections.contains(space)) {
213 connections->connections[space] = 1;
214
215 QObject::connect(connections, SIGNAL(changed()), space, SIGNAL(contentsChanged()));
216 } else {
217 ++connections->connections[space];
218 }
219 }
220
221 bool QValueSpaceSubscriberPrivate::disconnect(QValueSpaceSubscriber * space)
222 {
223 QMutexLocker locker(&lock);
224
225 if (connections) {
226 QHash<const QValueSpaceSubscriber *, int>::Iterator iter = connections->connections.find(space);
227 if (iter != connections->connections.end()) {
228 --(*iter);
229 if (!*iter) {
230 QObject::disconnect(connections, SIGNAL(changed()), space, SIGNAL(contentsChanged()));
231 connections->connections.erase(iter);
232 }
233 return true;
234 }
235 }
236 return false;
237 }
238
239 /*!
240 Constructs a QValueSpaceSubscriber with the specified \a parent that refers to the root path.
241
242 The constructed Value Space subscriber will access all available layers.
243 */
244 QValueSpaceSubscriber::QValueSpaceSubscriber(QObject *parent)
245 : QObject(parent)
246 {
247 d = new QValueSpaceSubscriberPrivate(QLatin1String("/"));
248 }
249
250 /*!
251 Constructs a QValueSpaceSubscriber with the specified \a parent that refers to \a path.
252
253 The constructed Value Space subscriber will access all available layers.
254 */
255 QValueSpaceSubscriber::QValueSpaceSubscriber(const QString &path, QObject *parent)
256 : QObject(parent)
257 {
258 d = new QValueSpaceSubscriberPrivate(path);
259 }
260
261 /*!
262 Constructs a QValueSpaceSubscriber with the specified \a parent that refers to \a path. The
263 \a filter parameter is used to limit which layers this QValueSpaceSubscriber will access.
264
265 If a layer matching \a filter is not found, the constructed QValueSpaceSubscriber will be
266 unconnected.
267
268 \sa isConnected()
269 */
270 QValueSpaceSubscriber::QValueSpaceSubscriber(QValueSpace::LayerOptions filter, const QString &path, QObject *parent)
271 : QObject(parent)
272 {
273 d = new QValueSpaceSubscriberPrivate(path, filter);
274 }
275
276 /*!
277 Constructs a QValueSpaceSubscriber with the specified \a parent that refers to \a path. This
278 QValueSpaceSubscriber will only use the layer identified by \a uuid.
279
280 Use of this constructor is not platform agnostic. If possible use one of the constructors that
281 take a QValueSpace::LayerOptions parameter instead.
282
283 If a layer with a matching \a uuid is not found, the constructed QValueSpaceSubscriber will be
284 unconnected.
285
286 \sa QValueSpace, isConnected()
287 */
288 QValueSpaceSubscriber::QValueSpaceSubscriber(const QUuid &uuid, const QString &path, QObject *parent)
289 : QObject(parent)
290 {
291 d = new QValueSpaceSubscriberPrivate(path, uuid);
292 }
293
294 /*!
295 Destroys the QValueSpaceSubscriber.
296 */
297 QValueSpaceSubscriber::~QValueSpaceSubscriber()
298 {
299 if (!isConnected())
300 return;
301
302 d->disconnect(this);
303 }
304
305 /*!
306 \property QValueSpaceSubscriber::path
307
308 This property holds the current path that the QValueSpaceSubscriber refers to.
309
310 Settings this property causes the QValueSpaceSubscriber to disconnect and reconnect to the
311 Value Space with the new path. As a result all signal/slot connections are disconnected.
312 */
313 void QValueSpaceSubscriber::setPath(const QString &path)
314 {
315 if (!isConnected()) {
316 qWarning("setPath called on unconnected QValueSpaceSubscriber.");
317 return;
318 }
319
320 if (this->path() == path)
321 return;
322
323 d->disconnect(this);
324
325 disconnect();
326
327 d = new QValueSpaceSubscriberPrivate(path);
328 }
329
330 QString QValueSpaceSubscriber::path() const
331 {
332 return d->path;
333 }
334
335 /*!
336 Sets the path to the same path as \a subscriber.
337
338 Calling this function causes the QValueSpaceSubscriber to disconnect and reconnect to the value
339 space with the specified \a path.
340
341 Calling this function disconnects all signal/slot connections.
342 */
343 void QValueSpaceSubscriber::setPath(QValueSpaceSubscriber *subscriber)
344 {
345 if (!isConnected()) {
346 qWarning("setPath called on unconnected QValueSpaceSubscriber.");
347 return;
348 }
349
350 d->disconnect(this);
351
352 disconnect();
353
354 d = subscriber->d;
355 }
356
357 /*!
358 Changes the path to the absolute path if \a path starts with a '/'; otherwise changes to the
359 sub path of the current path.
360 */
361 void QValueSpaceSubscriber::cd(const QString &path)
362 {
363 if (!isConnected()) {
364 qWarning("cd called on unconnected QValueSpaceSubscriber.");
365 return;
366 }
367
368 if (path.startsWith(QLatin1Char('/')))
369 setPath(path);
370 else
371 setPath(this->path() + QLatin1Char('/') + path);
372 }
373
374 /*!
375 Sets the path to parent of the current path.
376 */
377 void QValueSpaceSubscriber::cdUp()
378 {
379 if (!isConnected()) {
380 qWarning("cdUp called on unconnected QValueSpaceSubscriber.");
381 return;
382 }
383
384 if (path() == QLatin1String("/"))
385 return;
386
387 QString p(path());
388
389 int index = p.lastIndexOf(QLatin1Char('/'));
390
391 p.truncate(index);
392
393 setPath(p);
394 }
395
396 /*!
397 Returns true if this QValueSpaceSubscriber is connected to at least one available layer;
398 otherwise returns false. An unconnected QValueSpaceSubscriber is constructed if the filtering
399 parameters passed to the constructor eliminate all available layers.
400 */
401 bool QValueSpaceSubscriber::isConnected() const
402 {
403 return !d->readers.isEmpty();
404 }
405
406 /*!
407 Returns the value of the \a subPath under this subscriber path, or the value of this subscriber
408 path if \a subPath is empty. If the value does not exists \a def is returned.
409
410 The following code shows how the subscriber path and \a subPath relate.
411
412 \code
413 QValueSpaceSubscriber base("/Settings");
414 QValueSpaceSubscriber equiv("/Settings/QtProject/General/Mappings");
415
416 // Is true
417 equiv.value() == base.value("QtProject/General/Mappings");
418 \endcode
419 */
420 QVariant QValueSpaceSubscriber::value(const QString & subPath, const QVariant &def) const
421 {
422 if (!isConnected()) {
423 qWarning("value called on unconnected QValueSpaceSubscriber.");
424 return QVariant();
425 }
426
427 QVariant value;
428 if (subPath.isEmpty()) {
429 for (int ii = d->readers.count(); ii > 0; --ii) {
430 if (d->readers[ii - 1].first->value(d->readers[ii - 1].second, &value))
431 return value;
432 }
433 } else {
434 const QString vpath(qCanonicalPath(subPath));
435 for (int ii = d->readers.count(); ii > 0; --ii) {
436 if (d->readers[ii - 1].first->value(d->readers[ii - 1].second, vpath, &value))
437 return value;
438 }
439 }
440 return def;
441 }
442
443 /*!
444 \property QValueSpaceSubscriber::value
445
446 This property holds the value of the path that this QValueSpaceSubscriber refers to.
447 */
448 QVariant QValueSpaceSubscriber::valuex(const QVariant &def) const
449 {
450 QMutexLocker locker(&d->lock);
451
452 if (!d->connections || d->connections->connections.value(this) == 0) {
453 locker.unlock();
454 d->connect(this);
455 }
456
457 return value(QString(), def);
458 }
459
460 /*!
461 \reimp
462 */
463 void QValueSpaceSubscriber::connectNotify(const QMetaMethod &signal)
464 {
465 static const QMetaMethod contentsChangedSignal = QMetaMethod::fromSignal(&QValueSpaceSubscriber::contentsChanged);
466 if (isConnected() && signal == contentsChangedSignal)
467 d->connect(this);
468 }
469
470 /*!
471 \reimp
472 */
473 void QValueSpaceSubscriber::disconnectNotify(const QMetaMethod &signal)
474 {
475 static const QMetaMethod contentsChangedSignal = QMetaMethod::fromSignal(&QValueSpaceSubscriber::contentsChanged);
476 if (isConnected() && signal == contentsChangedSignal)
477 d->disconnect(this);
478 }
479
480 /*!
481 Returns a list of sub-paths under the current path. For example, given a Value Space tree
482 containing:
483
484 \code
485 /Settings/QtProject/Device
486 /Settings/QtProject/Other
487 /Settings/Qt
488 /Device/Buttons
489 \endcode
490
491 \c { QValueSpaceSubscriber("/Settings").subPaths() } will return a list containing
492 \c { { QtProject, Qt } } in no particular order.
493 */
494 QStringList QValueSpaceSubscriber::subPaths() const
495 {
496 if (!isConnected()) {
497 qWarning("subPaths called on unconnected QValueSpaceSubscriber.");
498 return QStringList();
499 }
500
501 QSet<QString> rv;
502 for (int ii = 0; ii < d->readers.count(); ++ii)
503 rv.unite(d->readers[ii].first->children(d->readers[ii].second));
504
505 QStringList rvs;
506 for (QSet<QString>::ConstIterator iter = rv.begin(); iter != rv.end(); ++iter)
507 rvs.append(*iter);
508
509 return rvs;
510 }
511
512 QT_END_NAMESPACE