1/****************************************************************************
2**
3** Copyright (C) 2015 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:LGPL21$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://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 2.1 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34#include "qdeclarativevaluespacepublisher_p.h"
35#include "qdeclarativevaluespacepublishermetaobject_p.h"
36#include <QtCore/qregexp.h>
37
38QT_BEGIN_NAMESPACE
39
40/*!
41 \qmltype ValueSpacePublisher
42 \instantiates QDeclarativeValueSpacePublisher
43 \inqmlmodule QtPublishSubscribe
44 \ingroup qml-publishsubscribe
45
46 \brief The ValueSpacePublisher allows application to publish values to Value Space.
47
48 ValueSpacePublishers are constructed with a fixed \a path which cannot be changed. And you should
49 set the \a path property before publishing any values. If you need to publish within multiple
50 different paths, you will need multiple publishers.
51
52 For the keys within the path chosen, if the key names to be published are alphanumeric, they can
53 be accessed through dynamic properties by setting the \a keys list.
54
55 Example:
56 \code
57 ValueSpacePublisher {
58 id: battery
59 path: "/power/battery"
60 keys: ["charge", "charging"]
61 }
62
63 MouseArea {
64 onClicked: {
65 battery.charge = 50
66 battery.charging = true
67 }
68 }
69 \endcode
70
71 Alternatively, for key names that can't be mapped to properties, or for key names shadowed by
72 existing properties (like "value" or "path"), you can also access the \a value property of the
73 publisher itself.
74
75 \code
76 ValueSpacePublisher {
77 id: nonalpha
78 path: "/something/with a space/value"
79 }
80
81 MouseArea {
82 onClicked: {
83 nonalpha.value = "example"
84 }
85 }
86 \endcode
87*/
88
89QDeclarativeValueSpacePublisher::QDeclarativeValueSpacePublisher(QObject *parent)
90 : QObject(parent)
91 , metaObj(new QDeclarativeValueSpacePublisherMetaObject(this))
92 , hasSubscriber(false)
93 , publisher(0)
94{
95 d_ptr.data()->metaObject = metaObj;
96}
97
98QDeclarativeValueSpacePublisher::~QDeclarativeValueSpacePublisher()
99{
100 d_ptr.data()->metaObject = 0;
101 delete metaObj;
102}
103
104/*!
105 \qmlproperty string ValueSpacePublisher::path
106
107 This property holds the base path of the publisher, and it should be written before publishing
108 any data. Note it can only be written once, and further writing has no effects.
109 */
110QString QDeclarativeValueSpacePublisher::path() const
111{
112 if (publisher)
113 return publisher->path();
114 else
115 return QString::null;
116}
117
118void QDeclarativeValueSpacePublisher::setPath(const QString &path)
119{
120 if (!publisher) {
121 publisher = new QValueSpacePublisher(path);
122 connect(sender: publisher, SIGNAL(interestChanged(QString,bool)),
123 receiver: this, SLOT(onInterestChanged(QString,bool)));
124 }
125}
126
127/*!
128 \qmlproperty QVariant ValueSpacePublisher::value
129
130 This property publishes a new value to the ValueSpace at the
131 path given through the \a path property.
132 This property is write only.
133*/
134void QDeclarativeValueSpacePublisher::setValue(const QVariant &value)
135{
136 if (publisher)
137 publisher->setValue(name: QString::null, data: value);
138}
139
140QVariant QDeclarativeValueSpacePublisher::dummyValue() const
141{
142 return QVariant();
143}
144
145/*!
146 \qmlproperty bool ValueSpacePublisher::hasSubscribers
147
148 This property is true if there are subscribers currently subscribed to
149 the ValueSpace path being published by this Publisher.
150
151 This property is read only.
152*/
153bool QDeclarativeValueSpacePublisher::hasSubscribers() const
154{
155 return hasSubscriber;
156}
157
158/*!
159 \qmlproperty QStringList ValueSpacePublisher::keys
160
161 Setting this property creates a set of dynamic properties allowing
162 easy access to set the values of keys under this Publisher's path.
163*/
164void QDeclarativeValueSpacePublisher::setKeys(const QStringList &keys)
165{
166 foreach (const QString &key, keys) {
167 if (key.contains(rx: QRegExp(QString(QStringLiteral("[^a-zA-Z0-9]"))))
168 || key == QString(QStringLiteral("value"))
169 || key == QString(QStringLiteral("path"))
170 || key == QString(QStringLiteral("keys"))
171 || key == QString(QStringLiteral("hasSubscribers"))) {
172 continue;
173 }
174
175 metaObj->createProperty(name: key.toUtf8().constData(), type: "QVariant");
176 }
177}
178
179QStringList QDeclarativeValueSpacePublisher::keys() const
180{
181 QStringList keys;
182 QList<QPair<QString, QVariant> > properties = metaObj->dynamicProperties.values();
183 for (QList<QPair<QString, QVariant> >::const_iterator i = properties.constBegin(); i != properties.constEnd(); ++i)
184 keys << (*i).first;
185 return keys;
186}
187
188/*!
189 \internal
190*/
191void QDeclarativeValueSpacePublisher::onInterestChanged(const QString &path, bool interested)
192{
193 Q_UNUSED(path)
194
195 hasSubscriber = interested;
196 emit subscribersChanged();
197}
198
199QT_END_NAMESPACE
200

source code of qtsystems/src/imports/publishsubscribe/qdeclarativevaluespacepublisher.cpp