| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2017 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 documentation of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:FDL$ |
| 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 Free Documentation License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU Free |
| 19 |
** Documentation License version 1.3 as published by the Free Software |
| 20 |
** Foundation and appearing in the file included in the packaging of |
| 21 |
** this file. Please review the following information to ensure |
| 22 |
** the GNU Free Documentation License version 1.3 requirements |
| 23 |
** will be met: https://www.gnu.org/licenses/fdl-1.3.html. |
| 24 |
** $QT_END_LICENSE$ |
| 25 |
** |
| 26 |
****************************************************************************/ |
| 27 |
|
| 28 |
/*! |
| 29 |
\example dialer |
| 30 |
|
| 31 |
\title Service Framework using QML Example |
| 32 |
|
| 33 |
\ingroup serviceframework-examples |
| 34 |
|
| 35 |
\brief This example demonstrates the usage of the Service Framework's QML API. |
| 36 |
|
| 37 |
\b {Execution} |
| 38 |
|
| 39 |
This example requires the example dialer services to be registered in order |
| 40 |
for the application to discover and load the services. This can be done by using the |
| 41 |
service framework command line tool to add the corresponding service XML file: |
| 42 |
|
| 43 |
\list |
| 44 |
\li servicefw add landlinedialerplugin.xml |
| 45 |
\li servicefw add remotedialerservice.xml |
| 46 |
\li servicefw dbusservice removedialerservice.xml dialer_service |
| 47 |
\endlist |
| 48 |
|
| 49 |
These deployment commands will register the remote IPC-based service, and the Landline dialer plugin. The last |
| 50 |
line features the ability to create an autostarting file for D-Bus if the platform supports the QtDBus module. |
| 51 |
|
| 52 |
To run the example |
| 53 |
\list |
| 54 |
\li dialer |
| 55 |
\endlist |
| 56 |
|
| 57 |
The XML files for all example services can be found in the Qt build directory |
| 58 |
under examples/qtsystems/serviceframework/xmldata. |
| 59 |
|
| 60 |
For Maemo and Linux platforms using D-Bus as the underlying IPC mechanism, the |
| 61 |
autostart feature can be initialised by running the service framework tool: |
| 62 |
\code |
| 63 |
servicefw dbusservice xmldata/remotedialerservice.xml dialer_service |
| 64 |
\endcode |
| 65 |
|
| 66 |
For other platforms that fall use QLocalSocket, then if the services |
| 67 |
binary can be found in your path it'll auto start the service. |
| 68 |
|
| 69 |
\b {Explanation} |
| 70 |
|
| 71 |
This example should demonstrate how to use the Service Framework to |
| 72 |
access a list of services in a QML context. A library plugin provides QML with elements |
| 73 |
that can reference a single service or a list of services, called 'Service' and |
| 74 |
'ServiceList' respectively. |
| 75 |
|
| 76 |
\target guidesign |
| 77 |
|
| 78 |
The GUI looks like following picture: |
| 79 |
|
| 80 |
\image DialerServiceGUI.png "GUI" |
| 81 |
|
| 82 |
The following steps outline how to make a QML based application using the Service Framework technology. |
| 83 |
It is assumed that QtMobility has been successfully built and environment variables have been set |
| 84 |
as per \l {Installation Guide}. |
| 85 |
|
| 86 |
\b {Service Framework in QML:} |
| 87 |
|
| 88 |
To include the Service Framework QML plugin we need to import it as follows: |
| 89 |
|
| 90 |
\snippet serviceframework/dialer/content/DialerList.qml 4 |
| 91 |
|
| 92 |
|
| 93 |
\b {The Services:} |
| 94 |
|
| 95 |
The services are implemented as a sevices and a shared library and can be register in the service framework. |
| 96 |
After the service is registered it can be used in different applications. |
| 97 |
In our case we'll access the services over an application that is based on QML scripting. |
| 98 |
We will be able to change between different services and call their properties, |
| 99 |
receiving their signals and so forth. |
| 100 |
|
| 101 |
In this example we've implemented a service called |
| 102 |
Voipdialer and a plugin Landlinedialer. |
| 103 |
You can find the projects for these: |
| 104 |
|
| 105 |
remotedialerservice |
| 106 |
Those projects will create a service. |
| 107 |
|
| 108 |
The service needs to be available over the Service Framework, |
| 109 |
we need to register it. |
| 110 |
In our example this will be done manually by using the servicefw tool. Refer to the project |
| 111 |
README for further details. |
| 112 |
|
| 113 |
As you can see we register the services using a xml file. |
| 114 |
This xml file basically contains all information to register the shared library in the |
| 115 |
Service Framework environment. |
| 116 |
For more information please read more about the Qt Service Framework |
| 117 |
\l {service-frameworks.html#adding-and-removing-of-services}{XML Format} |
| 118 |
|
| 119 |
The QServiceManager creates an instance of a plugin service over a QServicePluginInterface. |
| 120 |
For each plugin services we provide a Plugin. |
| 121 |
|
| 122 |
\snippet serviceframework/voipdialerplugin/voipdialerplugin.h 0 |
| 123 |
|
| 124 |
The Q_INTERFACES macro tells Qt which interfaces the class implements. |
| 125 |
|
| 126 |
Sevice plugins need to implement the QServicePluginInterface. |
| 127 |
In our case we only need to overwrite the virtual function createInstance. |
| 128 |
\snippet serviceframework/voipdialerplugin/voipdialerplugin.cpp 0 |
| 129 |
|
| 130 |
As you can see the createInstance function create the appropriate dialer object |
| 131 |
and returns it. |
| 132 |
The Q_PLUGIN_METADATA macro provides the necessary implementation for a plugin. |
| 133 |
See \l{How to Create Qt Plugins} for more details. |
| 134 |
|
| 135 |
The last thing we need to provide in our services are the states, properties, signals and slots that we |
| 136 |
want to access in out QML script later. |
| 137 |
|
| 138 |
\target voipdialer_h 0 |
| 139 |
\snippet serviceframework/voipdialerplugin/voipdialer.h 0 |
| 140 |
|
| 141 |
|
| 142 |
\b {Service access on the QML side} |
| 143 |
|
| 144 |
|
| 145 |
The QML elements are implemented in 4 different qml scripting files |
| 146 |
\l {guidesign}{ see GUI design}. |
| 147 |
|
| 148 |
The first step is to use our ServiceWrapperList to specify the interface and minimum version (optional) through QML item |
| 149 |
context, which will produce a list of ServiceWrapper objects. |
| 150 |
|
| 151 |
\snippet serviceframework/dialer/content/DialerList.qml 5 |
| 152 |
|
| 153 |
In the DialerList.qml file the services property is assigned to the ListView model property. |
| 154 |
|
| 155 |
\snippet serviceframework/dialer/content/DialerList.qml 0 |
| 156 |
|
| 157 |
To show the items of the model property we need to create a delegate component and assign it to the ListView |
| 158 |
Delegate property: |
| 159 |
|
| 160 |
\snippet serviceframework/dialer/content/DialerList.qml 1 |
| 161 |
|
| 162 |
In this component you can define how you want to draw one ListView item. |
| 163 |
You can access inside of this component the current ListWiew item by using the variable modelData. |
| 164 |
In our example we are using two text lines. Furthermore we can define whats happening if we click |
| 165 |
on a ListView item by using the MouseRegion. |
| 166 |
|
| 167 |
\target DialerList_qml_2 |
| 168 |
\snippet serviceframework/dialer/content/DialerList.qml 2 |
| 169 |
|
| 170 |
Another component can be created for highliting a list item: |
| 171 |
|
| 172 |
\snippet serviceframework/dialer/content/DialerList.qml 3 |
| 173 |
|
| 174 |
\b {Service signals and function calls on the QML site} |
| 175 |
|
| 176 |
In sfw-kinetic-example.qml we define a control named DialScreen and implement |
| 177 |
the function onDial and onHangup. |
| 178 |
As you can see in the onDial event we call the service function dialNumber and |
| 179 |
the onHangup calls hangup. |
| 180 |
Both function are implemented in the service \l {voipdialer_h_0} { (see voipdialer header file).} |
| 181 |
|
| 182 |
\snippet serviceframework/dialer/dialer.qml 0 |
| 183 |
|
| 184 |
In DialScreen.qml the dial and the hangup signals are defined. |
| 185 |
The hangup signal will be emitted if the HangUpButton was clicked: |
| 186 |
|
| 187 |
\snippet serviceframework/dialer/content/DialScreen.qml 1 |
| 188 |
|
| 189 |
The dial signal will be emitted if the CallButton was clicked: |
| 190 |
|
| 191 |
\snippet serviceframework/dialer/content/DialScreen.qml 2 |
| 192 |
|
| 193 |
Now we need to connect the stateChanged signal form the services with an event handler on the QML site. |
| 194 |
This is done in our main declarative file: |
| 195 |
|
| 196 |
\snippet serviceframework/dialer/dialer.qml 1 |
| 197 |
|
| 198 |
The DialScreen.currentDialer is assigned during a ListView item click in the |
| 199 |
\l {DialerList_qml_2}{ ServiceList.qml file}. |
| 200 |
|
| 201 |
*/ |