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 | #ifndef QDECLARATIVESERVICE_P_H |
35 | #define QDECLARATIVESERVICE_P_H |
36 | |
37 | // |
38 | // W A R N I N G |
39 | // ------------- |
40 | // |
41 | // This file is not part of the Qt API. It exists purely as an |
42 | // implementation detail. This header file may change from version to |
43 | // version without notice, or even be removed. |
44 | // |
45 | // We mean it. |
46 | // |
47 | |
48 | #include <QtCore> |
49 | #include <qserviceinterfacedescriptor.h> |
50 | #include <qservicemanager.h> |
51 | #include <qqml.h> |
52 | #include <qqmllist.h> |
53 | |
54 | QT_BEGIN_NAMESPACE |
55 | |
56 | class QDeclarativeServiceDescriptor : public QObject, public QServiceInterfaceDescriptor { |
57 | Q_OBJECT |
58 | Q_PROPERTY(QString serviceName READ serviceName CONSTANT) |
59 | Q_PROPERTY(QString interfaceName READ interfaceName CONSTANT) |
60 | Q_PROPERTY(int majorVersion READ majorVersion CONSTANT) |
61 | Q_PROPERTY(int minorVersion READ minorVersion CONSTANT) |
62 | Q_PROPERTY(bool valid READ isValid CONSTANT) |
63 | |
64 | public: |
65 | QDeclarativeServiceDescriptor(QObject* parent = 0) : QObject(parent) {} |
66 | QDeclarativeServiceDescriptor(const QDeclarativeServiceDescriptor& other) |
67 | : QObject(0), QServiceInterfaceDescriptor(other) {} |
68 | QDeclarativeServiceDescriptor(const QServiceInterfaceDescriptor& other) |
69 | : QObject(0), QServiceInterfaceDescriptor(other) {} |
70 | bool operator==(const QServiceInterfaceDescriptor& other) const |
71 | { return QServiceInterfaceDescriptor::operator==(other); } |
72 | QDeclarativeServiceDescriptor& operator=(const QServiceInterfaceDescriptor& other) |
73 | { |
74 | QServiceInterfaceDescriptor::operator=(other); |
75 | return *this; |
76 | } |
77 | QDeclarativeServiceDescriptor& operator=(const QDeclarativeServiceDescriptor& other) |
78 | { |
79 | QServiceInterfaceDescriptor::operator=(other); |
80 | return *this; |
81 | } |
82 | }; |
83 | |
84 | class QDeclarativeServiceLoader : public QObject, public QQmlParserStatus { |
85 | Q_OBJECT |
86 | Q_INTERFACES(QQmlParserStatus) |
87 | Q_PROPERTY(QString interfaceName READ interfaceName WRITE setInterfaceName NOTIFY interfaceNameChanged) |
88 | // ### Rename descriptor? |
89 | Q_PROPERTY(QDeclarativeServiceDescriptor* serviceDescriptor READ serviceDescriptor WRITE setServiceDescriptor NOTIFY serviceDescriptorChanged RESET resetServiceDescriptor) //Takes precedence over interfaceName if set |
90 | Q_PROPERTY(Status status READ status NOTIFY statusChanged) |
91 | Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) |
92 | // ### Rename object? |
93 | Q_PROPERTY(QObject* serviceObject READ serviceObject NOTIFY serviceObjectChanged) |
94 | Q_ENUMS(Status) |
95 | |
96 | public: |
97 | enum Status { |
98 | Null = 0, |
99 | Ready, |
100 | Loading, |
101 | Error |
102 | }; |
103 | |
104 | QDeclarativeServiceLoader(); |
105 | ~QDeclarativeServiceLoader(); |
106 | |
107 | Q_INVOKABLE QString errorString() const |
108 | { |
109 | if (m_status == Error) |
110 | return m_errorString; |
111 | else |
112 | return "" ; |
113 | } |
114 | |
115 | QString interfaceName() const |
116 | { |
117 | return m_interfaceName; |
118 | } |
119 | |
120 | QDeclarativeServiceDescriptor* serviceDescriptor() const |
121 | { |
122 | return m_serviceDescriptor; |
123 | } |
124 | |
125 | void resetServiceDescriptor() |
126 | { |
127 | setServiceDescriptor(0); |
128 | } |
129 | |
130 | Status status() const |
131 | { |
132 | return m_status; |
133 | } |
134 | |
135 | bool asynchronous() const |
136 | { |
137 | return m_asynchronous; |
138 | } |
139 | |
140 | QObject* serviceObject() const |
141 | { |
142 | return m_serviceObject; |
143 | } |
144 | |
145 | public slots: |
146 | void setInterfaceName(QString arg) |
147 | { |
148 | if (m_interfaceName != arg) { |
149 | m_interfaceName = arg; |
150 | emit interfaceNameChanged(arg); |
151 | if (!m_serviceDescriptor && m_componentComplete) |
152 | startLoading(); |
153 | } |
154 | } |
155 | |
156 | void setServiceDescriptor(QDeclarativeServiceDescriptor* arg) |
157 | { |
158 | if (m_serviceDescriptor != arg) { |
159 | m_serviceDescriptor = arg; |
160 | emit serviceDescriptorChanged(arg); |
161 | if (m_componentComplete) |
162 | startLoading(); |
163 | } |
164 | } |
165 | |
166 | void setAsynchronous(bool arg) |
167 | { |
168 | if (m_asynchronous != arg) { |
169 | m_asynchronous = arg; |
170 | emit asynchronousChanged(arg); |
171 | } |
172 | } |
173 | |
174 | private Q_SLOTS: |
175 | void setStatus(Status arg) |
176 | { |
177 | if (m_status != arg) { |
178 | m_status = arg; |
179 | emit statusChanged(arg); |
180 | } |
181 | } |
182 | |
183 | protected: |
184 | virtual void classBegin() {} |
185 | virtual void componentComplete(); |
186 | |
187 | signals: |
188 | void interfaceNameChanged(QString arg); |
189 | |
190 | void serviceDescriptorChanged(QServiceInterfaceDescriptor* arg); |
191 | |
192 | void statusChanged(Status arg); |
193 | |
194 | void asynchronousChanged(bool arg); |
195 | |
196 | void serviceObjectChanged(QObject* arg); |
197 | |
198 | private slots: |
199 | void startLoading(); |
200 | void finishLoading(); |
201 | void IPCFault(QService::UnrecoverableIPCError); |
202 | |
203 | private: |
204 | QString m_interfaceName; |
205 | QDeclarativeServiceDescriptor* m_serviceDescriptor; |
206 | Status m_status; |
207 | bool m_asynchronous; |
208 | QObject* m_serviceObject; |
209 | QString m_errorString; |
210 | |
211 | bool m_componentComplete; |
212 | QServiceManager* m_serviceManager; |
213 | QServiceReply* m_serviceReply; |
214 | }; |
215 | |
216 | class QDeclarativeServiceFilter : public QObject, public QQmlParserStatus { |
217 | Q_OBJECT |
218 | Q_INTERFACES(QQmlParserStatus) |
219 | Q_PROPERTY(QString serviceName READ serviceName WRITE setServiceName NOTIFY serviceNameChanged) |
220 | Q_PROPERTY(QString interfaceName READ interfaceName WRITE setInterfaceName NOTIFY interfaceNameChanged) |
221 | Q_PROPERTY(int majorVersion READ majorVersion WRITE setMajorVersion NOTIFY majorVersionChanged) |
222 | Q_PROPERTY(int minorVersion READ minorVersion WRITE setMinorVersion NOTIFY minorVersionChanged) |
223 | Q_PROPERTY(bool exactVersionMatching READ exactVersionMatching WRITE setExactVersionMatching NOTIFY exactVersionMatchingChanged) |
224 | // ### Rename monitorRegistrations ? |
225 | Q_PROPERTY(bool monitorServiceRegistrations READ monitorServiceRegistrations WRITE setMonitorServiceRegistrations NOTIFY monitorServiceRegistrationsChanged) |
226 | // ### Rename services ? |
227 | Q_PROPERTY(QQmlListProperty<QDeclarativeServiceDescriptor> serviceDescriptions READ serviceDescriptions NOTIFY serviceDescriptionsChanged) |
228 | public: |
229 | QDeclarativeServiceFilter(QObject* parent = 0); |
230 | ~QDeclarativeServiceFilter(); |
231 | QString serviceName() const |
232 | { |
233 | return m_serviceName; |
234 | } |
235 | |
236 | QString interfaceName() const |
237 | { |
238 | return m_interfaceName; |
239 | } |
240 | |
241 | int majorVersion() const |
242 | { |
243 | return m_majorVersion; |
244 | } |
245 | |
246 | int minorVersion() const |
247 | { |
248 | return m_minorVersion; |
249 | } |
250 | |
251 | bool exactVersionMatching() const |
252 | { |
253 | return m_exactVersionMatching; |
254 | } |
255 | |
256 | bool monitorServiceRegistrations() const |
257 | { |
258 | return m_monitorServiceRegistrations; |
259 | } |
260 | |
261 | QQmlListProperty<QDeclarativeServiceDescriptor> serviceDescriptions() |
262 | { |
263 | return QQmlListProperty<QDeclarativeServiceDescriptor> (this, 0, s_append, s_count, s_at, s_clear); |
264 | } |
265 | |
266 | public slots: |
267 | void setServiceName(QString arg) |
268 | { |
269 | if (m_serviceName != arg) { |
270 | m_serviceName = arg; |
271 | emit serviceNameChanged(arg); |
272 | updateServiceList(); |
273 | } |
274 | } |
275 | |
276 | void setInterfaceName(QString arg) |
277 | { |
278 | if (m_interfaceName != arg) { |
279 | m_interfaceName = arg; |
280 | emit interfaceNameChanged(arg); |
281 | updateServiceList(); |
282 | } |
283 | } |
284 | |
285 | void setMajorVersion(int arg) |
286 | { |
287 | if (m_majorVersion != arg) { |
288 | m_majorVersion = arg; |
289 | emit majorVersionChanged(arg); |
290 | updateServiceList(); |
291 | } |
292 | } |
293 | |
294 | void setMinorVersion(int arg) |
295 | { |
296 | if (m_minorVersion != arg) { |
297 | m_minorVersion = arg; |
298 | emit minorVersionChanged(arg); |
299 | updateServiceList(); |
300 | } |
301 | } |
302 | |
303 | void setExactVersionMatching(bool arg) |
304 | { |
305 | if (m_exactVersionMatching != arg) { |
306 | m_exactVersionMatching = arg; |
307 | emit exactVersionMatchingChanged(arg); |
308 | updateServiceList(); |
309 | } |
310 | } |
311 | |
312 | void setMonitorServiceRegistrations(bool updates); |
313 | |
314 | protected: |
315 | virtual void classBegin() {} |
316 | virtual void componentComplete(); |
317 | signals: |
318 | void serviceNameChanged(QString arg); |
319 | |
320 | void interfaceNameChanged(QString arg); |
321 | |
322 | void majorVersionChanged(int arg); |
323 | |
324 | void minorVersionChanged(int arg); |
325 | |
326 | void exactVersionMatchingChanged(bool arg); |
327 | |
328 | void monitorServiceRegistrationsChanged(bool arg); |
329 | |
330 | void serviceDescriptionsChanged(); |
331 | |
332 | private slots: |
333 | void updateServiceList(); |
334 | void servicesAddedRemoved(); |
335 | private: |
336 | QString m_serviceName; |
337 | QString m_interfaceName; |
338 | int m_majorVersion; |
339 | int m_minorVersion; |
340 | bool m_exactVersionMatching; |
341 | bool m_monitorServiceRegistrations; |
342 | QQmlListProperty<QDeclarativeServiceDescriptor*> m_serviceDescriptions; |
343 | QList<QDeclarativeServiceDescriptor> m_services; |
344 | |
345 | QServiceManager* m_serviceManager; |
346 | bool m_componentComplete; |
347 | |
348 | static void s_append(QQmlListProperty<QDeclarativeServiceDescriptor> *prop, QDeclarativeServiceDescriptor *service); |
349 | static int s_count(QQmlListProperty<QDeclarativeServiceDescriptor> *prop); |
350 | static QDeclarativeServiceDescriptor* s_at(QQmlListProperty<QDeclarativeServiceDescriptor> *prop, int index); |
351 | static void s_clear(QQmlListProperty<QDeclarativeServiceDescriptor> *prop); |
352 | }; |
353 | |
354 | QT_END_NAMESPACE |
355 | |
356 | QML_DECLARE_TYPE(QDeclarativeServiceLoader); |
357 | QML_DECLARE_TYPE(QDeclarativeServiceDescriptor); |
358 | QML_DECLARE_TYPE(QDeclarativeServiceFilter); |
359 | |
360 | |
361 | #endif // QDECLARATIVESERVICE_P_H |
362 | |