Debian Codegraph @

src/imports/serviceframework/qdeclarativeservice.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 "qdeclarativeservice_p.h"
41
42 #include <QQmlEngine>
43 #include <QQmlInfo>
44
45 #include <qservicereply.h>
46
47 QT_BEGIN_NAMESPACE
48
49 /*!
50 \qmltype ServiceLoader
51 \instantiates QDeclarativeService
52
53 \brief The ServiceLoader element holds an instance of a service object.
54 \inherits QObject
55 \inqmlmodule QtServiceFramework
56 \ingroup qml-serviceframework
57
58 The ServiceLoader element is part of the Qt ServiceFramework API and
59 provides a client instance of the service object. This element allows the specification of
60 the Service::interfaceName to locate the default service implemented at this interface.
61
62 To request a service more specifically, you can filter available ServiceDescriptors with
63 the ServiceFilter element, and then request service objects based off them.
64
65 Either way, the ServiceLoader element will provide you with the QtObject provided by that service
66 interface. You can then use its properties, signals, and slots as defined by its interface.
67
68 Example:
69 \code
70 import QtQuick 2.0
71 import QtServiceFramework 5.0
72
73 QtObject {
74 property alias serviceObject: service.serviceObject //In case you want to expose it upwards
75 ServiceLoader {
76 interfaceName: "com.qt.nokia.example.interface"
77 onStatusChanged: {
78 if (status == Service.Ready)
79 foo(serviceObject); //In case you want to do something with it as soon as it loads
80 else if (status == Service.Error)
81 errorHandling(errorString()); //In case you want to do error handling.
82 }
83 }
84 }
85 \endcode
86
87 \sa ServiceList
88 */
89 QDeclarativeServiceLoader::QDeclarativeServiceLoader()
90 : m_serviceDescriptor(0), m_status(Null), m_asynchronous(true),
91 m_serviceObject(0), m_componentComplete(false), m_serviceManager(0),
92 m_serviceReply(0)
93 {
94 }
95
96 QDeclarativeServiceLoader::~QDeclarativeServiceLoader()
97 {
98 //Manager has qobject ownership
99 delete m_serviceObject;//QOBJECT parented?
100 delete m_serviceReply;
101 }
102
103 /*!
104 \qmlmethod string ServiceLoader::errorString()
105
106 This method returns a human readable description of the last error.
107
108 If the status is not ServiceLoader.Error, errorString() will return an empty string.
109 */
110 /*!
111 \qmlproperty Status ServiceLoader::status
112
113 This property contains the status of the service object. It will be one of the following:
114
115 \list
116 \li ServiceLoader.Null - the service is inactive or no service has been set
117 \li ServiceLoader.Ready - the service has been loaded
118 \li ServiceLoader.Loading - the service is currently being loaded
119 \li ServiceLoader.Error - an error occurred while loading the service
120 \endlist
121
122 If you want to do something immediately after the service loads, the recommended route is
123 to monitor this property. For example:
124 \code
125 ServiceLoader {
126 onStatusChanged: {
127 if (status == ServiceLoader.Ready)
128 doStuffWith(serviceObject)
129 else if (status == ServiceLoader.Error)
130 console.debug(errorString())
131 }
132 }
133 \endcode
134
135 */
136 /*!
137 \qmlproperty bool ServiceLoader::asynchronous
138
139 If asynchronous is false, then the element will block the main thread until a service object
140 is found or an error occurs. This will skip the Loading status. This is generally not
141 recommended, as blocking the main thread can lead to significant problems with user interface
142 responsiveness.
143
144 Default is true.
145 */
146 /*!
147 \qmlproperty string ServiceLoader::interfaceName
148
149 Set this to select a service based off of the interface name. The service name,
150 and service version, will be selected for you if a match is found.
151 */
152 /*!
153 \qmlproperty ServiceDescriptor* ServiceLoader::serviceDescriptor
154
155 Set this to select a specific service. ServiceDescriptors can be obtained
156 from the ServiceFilter element.
157
158 */
159
160
161 /*!
162 \qmlproperty QObject* ServiceLoader::serviceObject
163
164 This property holds an instance of the service object which
165 can be used to make metaobject calls to the service.
166
167 serviceObject is only valid when the status property is set to
168 ServiceLoader.Ready. Otherwise, it should be a null reference.
169 */
170 void QDeclarativeServiceLoader::componentComplete()
171 {
172 if (!m_interfaceName.isEmpty() || m_serviceDescriptor)
173 startLoading();
174 m_componentComplete = true;
175 }
176
177 void QDeclarativeServiceLoader::startLoading()
178 {
179 if (m_serviceReply) // Cancel pending requests
180 delete m_serviceReply; //Auto-disconnects signals
181
182 if (m_serviceObject) {
183 m_serviceObject->deleteLater();
184 m_serviceObject = 0;
185 emit serviceObjectChanged(0);// In sync only, you might get an extra signal
186 }
187
188 if (!m_serviceDescriptor && m_interfaceName.isEmpty()) { //Actually an 'unset' request
189 setStatus(Null);
190 return;
191 }
192
193 if (!m_serviceManager)
194 m_serviceManager = new QServiceManager(this);
195
196 if (m_asynchronous) {
197 if (m_serviceDescriptor)
198 m_serviceReply = m_serviceManager->loadInterfaceRequest(*m_serviceDescriptor);
199 else
200 m_serviceReply = m_serviceManager->loadInterfaceRequest(m_interfaceName);
201 connect(m_serviceReply, SIGNAL(finished()),
202 this, SLOT(finishLoading()));
203 setStatus(Loading);
204 } else {
205 finishLoading();
206 }
207 }
208
209 QString stringForError(QServiceManager::Error error)// ### Should we just expose the enum? Or merely pick better strings?
210 {
211 switch (error) {
212 case QServiceManager::NoError: return QLatin1String("No error occurred.");
213 case QServiceManager::StorageAccessError: return QLatin1String("Storage access error.");
214 case QServiceManager::InvalidServiceLocation: return QLatin1String("Invalid service location.");
215 case QServiceManager::InvalidServiceXml: return QLatin1String("Invalid service XML.");
216 //case QService::InvalidInterfaceDescriptor: return QLatin1String("Invalid interface descriptor.");
217 case QServiceManager::PluginLoadingFailed: return QLatin1String("Error loading service plugin.");
218 case QServiceManager::ComponentNotFound: return QLatin1String("Service component not found.");
219 case QServiceManager::ServiceCapabilityDenied: return QLatin1String("You do not have permission to access this service capability.");
220 default: break;
221 }
222 return QLatin1String("Unknown error.");
223 }
224
225 void QDeclarativeServiceLoader::finishLoading()
226 {
227 Q_ASSERT(m_serviceManager);
228 QServiceManager::Error error;
229 QObject* prevObject = m_serviceObject;
230 if (m_serviceReply) {
231 if (!m_serviceReply->isFinished())
232 return; //TODO: Evaluate/handle this error condition
233 error = m_serviceReply->error();
234 m_serviceObject = m_serviceReply->proxyObject();
235 m_serviceReply->deleteLater();
236 m_serviceReply = 0;
237 } else {
238 if (m_asynchronous)
239 qDebug() << "Uh oh..."; //TODO: Evaluate/handle this 'error' condition
240 if (m_serviceDescriptor)
241 m_serviceObject = m_serviceManager->loadInterface(*m_serviceDescriptor);
242 else
243 m_serviceObject = m_serviceManager->loadInterface(m_interfaceName);
244 error = m_serviceManager->error();
245 }
246
247 if (error != QServiceManager::NoError) {
248 m_serviceObject = 0;
249 if (!m_asynchronous)
250 emit serviceObjectChanged(0);// In sync we didn't emit the intermediate state
251 m_errorString = stringForError(error);
252 setStatus(Error);
253 } else {
254 setStatus(Ready);
255 connect(m_serviceObject, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)),
256 this, SLOT(IPCFault(QService::UnrecoverableIPCError)));
257 }
258
259 if (prevObject != m_serviceObject)
260 emit serviceObjectChanged(m_serviceObject);
261
262 delete m_serviceManager;
263 m_serviceManager = 0;
264 }
265
266 void QDeclarativeServiceLoader::IPCFault(QService::UnrecoverableIPCError errorValue)
267 {
268 switch (errorValue) {
269 default:
270 case QService::ErrorUnknown:
•271 m_errorString = QLatin1String("IPC Error: Unknown Error");
272 break;
273 case QService::ErrorServiceNoLongerAvailable:
274 m_errorString = QLatin1String("IPC Error: Service no longer available");
275 break;
276 case QService::ErrorOutofMemory:
277 m_errorString = QLatin1String("IPC Error: Out of memory");
278 break;
279 case QService::ErrorPermissionDenied:
280 m_errorString = QLatin1String("IPC Error: Permission Denied");
281 break;
282 case QService::ErrorInvalidArguments:
283 m_errorString = QLatin1String("IPC Error: Invalid Arguments");
284 break;
285 }
286 setStatus(Error);
287 m_serviceObject->deleteLater();
288 }
289
290 /*!
291 \qmltype ServiceDescriptor
292 \instantiates QDeclarativeServiceDescriptor
293
294 \brief The ServiceDescriptor element holds a description of a service.
295 \inherits QObject
296
297 \ingroup qml-serviceframework
298
299 The ServiceDescriptor element is a simplified reflection of the ServiceDescriptor class,
300 and is used merely to contain data that can uniquely identify a service.
301
302 It cannot be created manually, use a ServiceList to search for service descriptions.
303
304 \sa ServiceList
305 */
306 /*!
307 \qmlproperty QString ServiceDescriptor::serviceName
308
309 This property holds the interface name of the service.
310 */
311 /*!
312 \qmlproperty QString ServiceDescriptor::interfaceName
313
314 This property holds the interface name of the service.
315 */
316
317 /*!
318 \qmlproperty int ServiceDescriptor::majorVersion
319
320 This property holds the major version number of the service.
321 */
322 /*!
323 \qmlproperty int ServiceDescriptor::minorVersion
324
325 This property holds the minor version number of the service.
326 */
327 //TODO: Fix doc or remove property
328 /*!
329 \qmlproperty bool ServiceDescriptor::valid
330
331 I have no clue what this property does, but it's probably important.
332 */
333 /*!
334 \qmltype ServiceFilter
335 \instantiates QDeclarativeServiceFilter
336
337 \brief The ServiceFilter element holds a list of \l ServiceDescriptor objects.
338 \inherits QObject
339
340 \ingroup qml-serviceframework
341
342 The ServiceFilter element is part of the Qt ServiceFramework API and
343 provides a list of \l QServiceDesciptor objects at the interface ServiceFilter::interfaceName with
344 minimum version match ServiceFilter::minVersion properties. This list can be used to
345 select the desired service and instantiate a service object for access via the QMetaObject.
346
347 This element is a simplified reflection of the QServiceFilter class that provides a list
348 of ServiceDescriptors. Similarly, if the ServiceFilter::serviceName
349 and ServiceFilter::versionMatch are not provided they will respectively default to an empty
350 string with a minimum verison match.
351
352 Example:
353 \code
354 Item {
355 ServiceFilter {
356 id: serviceFilter
357 interfaceName: "com.qt.nokia.example.interface"
358 }
359 ServiceLoader {
360 serviceDescriptor: serviceFilter.serviceDescriptions[0] //To get the first matching service
361 }
362 Repeater{ //To instantiate an object for all matching services.
363 model: serviceFilter.serviceDescriptions
364 Item {
365 ServiceLoader {
366 serviceDescriptor: modelData
367 }
368 }
369 }
370 }
371 \endcode
372
373 \sa ServiceLoader ServiceDescriptor
374 */
375 QDeclarativeServiceFilter::QDeclarativeServiceFilter(QObject* parent)
376 : QObject(parent),
377 m_majorVersion(1), // ### Is '1' the correct unset number?
378 m_minorVersion(0),
379 m_exactVersionMatching(false),
380 m_monitorServiceRegistrations(false),
381 m_serviceManager(0),
382 m_componentComplete(false)
383 {
384 }
385
386 QDeclarativeServiceFilter::~QDeclarativeServiceFilter()
387 {
388 }
389 /*!
390 \qmlproperty QString ServiceFilter::serviceName
391
392 This property holds the interface name of the services that
393 corresponds to setting QServiceFilter::setService().
394 */
395 /*!
396 \qmlproperty QString ServiceFilter::interfaceName
397
398 This property holds the interface name of the services that
399 corresponds to setting QServiceFilter::setInterface().
400 */
401
402 /*!
403 \qmlproperty int ServiceFilter::majorVersion
404
405 This property holds the major version number of the service filter that
406 corresponds to QServiceFilter::majorVersion().
407 */
408 /*!
409 \qmlproperty int ServiceFilter::minorVersion
410
411 This property holds the minor version number of the service filter that
412 corresponds to QServiceFilter::minorVersion().
413 */
414
415 /*!
416 \qmlproperty int ServiceFilter::monitorServiceRegistrations
417
418 This property controls the behaviour of the list when new services are
419 registered or deregistered. Setting this property to true means the list
420 will be automatically updated when a service is added or removed.
421
422 Continuing to monitor the services can be expensive, so it is recommended
423 that you only enable this feature if you need it.
424
425 Caution, your service descriptor object will be deleted if the service
426 is unregistered, even if the service is still running. This is primarily
427 of note if you are using the serviceDescriptions list as a model with
428 Service element delegates.
429 */
430
431 /*!
432 \qmlproperty bool ServiceFilter::versionMatch
433
434 This property holds the version match rule of the service filter that
435 corresponds to QServiceFilter::versionMatchRule(). Within QML the values
436 ServiceFilter.Exact and ServiceFilter.Minimum correspond to
437 QServiceFilter::ExactVersionMatch and QServiceFilter::MinimumVersionMatch
438 respectively.
439 */
440
441 void QDeclarativeServiceFilter::setMonitorServiceRegistrations(bool updates)
442 {
443 if (m_monitorServiceRegistrations == updates)
444 return;
445
446 if (updates == false) {
447 disconnect(this, SLOT(servicesAddedRemoved()));
448 if (m_serviceManager)
449 delete m_serviceManager;
450 m_serviceManager = 0;
451 } else {
452 if (!m_serviceManager)
453 m_serviceManager = new QServiceManager(this);
454 connect(m_serviceManager, SIGNAL(serviceAdded(QString,QService::Scope)),
455 this, SLOT(servicesAddedRemoved()));
456 connect(m_serviceManager, SIGNAL(serviceRemoved(QString,QService::Scope)),
457 this, SLOT(servicesAddedRemoved()));
458 }
459
460 emit monitorServiceRegistrationsChanged(updates);
461 m_monitorServiceRegistrations = updates;
462 }
463
464 void QDeclarativeServiceFilter::servicesAddedRemoved()
465 {
466 // invoke in another event loop run ### Why?
467 QMetaObject::invokeMethod(this, "updateServiceList", Qt::QueuedConnection);
468 }
469
470 QList<QDeclarativeServiceDescriptor> makeDeclarative(QList<QServiceInterfaceDescriptor> in)
471 {
472 QList<QDeclarativeServiceDescriptor> out;
473 foreach (const QServiceInterfaceDescriptor &d, in)
474 out << QDeclarativeServiceDescriptor(d);
475 return out;
476 }
477
478 void QDeclarativeServiceFilter::updateServiceList()
479 {
480 if (!m_componentComplete)
481 return;
482
483 if (!m_serviceManager)
484 m_serviceManager = new QServiceManager(this);
485 const QString version = QString::number(m_majorVersion) + "." + QString::number(m_minorVersion);
486
487 QServiceFilter filter;
488
489 if (!m_serviceName.isEmpty())
490 filter.setServiceName(m_serviceName);
491
492 if (!m_interfaceName.isEmpty())
493 filter.setInterface(m_interfaceName, version, m_exactVersionMatching ?
494 QServiceFilter::ExactVersionMatch : QServiceFilter::MinimumVersionMatch);
495
496 QList<QDeclarativeServiceDescriptor> list = makeDeclarative(m_serviceManager->findInterfaces(filter));
497
498 if (list != m_services) {
499 m_services = list;
500 emit serviceDescriptionsChanged();
501 }
502
503 if (!m_monitorServiceRegistrations) {
504 delete m_serviceManager;
505 m_serviceManager = 0;
506 }
507
508 }
509
510 /*!
511 \qmlproperty QQmlListProperty ServiceFilter::serviceDescriptions
512
513 This property holds the list of \l ServiceDescriptor objects that match
514 the ServiceFilter::interfaceName and minimum ServiceFilter::versionNumber properties.
515 */
516
517 void QDeclarativeServiceFilter::componentComplete()
518 {
519 m_componentComplete = true;
520 updateServiceList();
521 }
522
523 void QDeclarativeServiceFilter::s_append(QQmlListProperty<QDeclarativeServiceDescriptor> *prop, QDeclarativeServiceDescriptor *service)
524 {
525 QDeclarativeServiceFilter* list = static_cast<QDeclarativeServiceFilter*>(prop->object);
526 list->m_services.append(*service);//### This does not maintain the reference
527 list->serviceDescriptionsChanged();
528 }
529 int QDeclarativeServiceFilter::s_count(QQmlListProperty<QDeclarativeServiceDescriptor> *prop)
530 {
531 return static_cast<QDeclarativeServiceFilter*>(prop->object)->m_services.count();
532 }
533
534 QDeclarativeServiceDescriptor* QDeclarativeServiceFilter::s_at(QQmlListProperty<QDeclarativeServiceDescriptor> *prop, int index)
535 {
536 return &(static_cast<QDeclarativeServiceFilter*>(prop->object)->m_services[index]);
537 }
538
539 void QDeclarativeServiceFilter::s_clear(QQmlListProperty<QDeclarativeServiceDescriptor> *prop)
540 {
541 QDeclarativeServiceFilter* list = static_cast<QDeclarativeServiceFilter*>(prop->object);
542 list->m_services.clear();
543 list->serviceDescriptionsChanged();
544 }
545 #include "moc_qdeclarativeservice_p.cpp"
546
547 QT_END_NAMESPACE