1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
"""
Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
All rights reserved.
Contact: Nokia Corporation (qt-info@nokia.com)
This file is part of the Qt Mobility Components.
$QT_BEGIN_LICENSE:BSD$
You may use this file under the terms of the BSD license as follows:
"Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
$QT_END_LICENSE$
"""
from PySide.QtGui import QWidget, QListWidgetItem, QListWidget, QRadioButton, QGroupBox, QPushButton, QButtonGroup, QVBoxLayout, QLabel, QStackedLayout, QComboBox, QGridLayout
from PySide.QtCore import QCoreApplication, Qt
from QtMobility.ServiceFramework import QServiceManager, QServiceInterfaceDescriptor
class ServiceBrowser(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.serviceManager = QServiceManager(self)
self.registerExampleServices()
self.initWidgets()
self.reloadServicesList()
self.setWindowTitle(self.tr("Services Browser"))
def __del__(self):
self.unregisterExampleServices()
def currentInterfaceImplChanged(self, current, previous):
if not current:
return
descriptor = current.data(Qt.UserRole)
self.reloadAttributesList()
self.reloadAttributesRadioButtonText()
if descriptor.isValid():
self.defaultInterfaceButton.setText(self.tr("Set as default implementation for %s" % str(descriptor.interfaceName())))
self.defaultInterfaceButton.setEnabled(True)
def reloadServicesList(self):
self.servicesListWidget.clear()
services = self.serviceManager.findServices()
for serv in services:
self.servicesListWidget.addItem(serv)
self.servicesListWidget.addItem(self.showAllServicesItem)
self._services = services
def reloadInterfaceImplementationsList(self):
serviceName = None
allServices = self.servicesListWidget.currentItem().text() == self.showAllServicesItem.text()
if self.servicesListWidget.currentItem() and not allServices:
serviceName = self.servicesListWidget.currentItem().text()
self.interfacesGroup.setTitle(self.tr("Interfaces implemented by %s" % str(serviceName)))
else:
self.interfacesGroup.setTitle(self.tr("All interface implementations"))
descriptors = self.serviceManager.findInterfaces(serviceName)
self.attributesListWidget.clear()
self.interfacesListWidget.clear()
self._i = []
for desc in descriptors:
text = "%s %d.%d" % (desc.interfaceName(), desc.majorVersion(), desc.minorVersion())
if not serviceName:
text += " (" + desc.serviceName() + ")"
defaultInterfaceImpl = self.serviceManager.interfaceDefault(desc.interfaceName())
if desc == defaultInterfaceImpl:
text += self.tr(" (default)")
item = QListWidgetItem(text)
item.setData(Qt.UserRole, desc)
item._data = desc
self.interfacesListWidget.addItem(item)
self.defaultInterfaceButton.setEnabled(False)
def reloadAttributesList(self):
item = self.interfacesListWidget.currentItem()
if not item:
return
selectedImpl = item.data(Qt.UserRole)
implementationRef = None
if self.selectedImplRadioButton.isChecked():
implementationRef = self.serviceManager.loadInterface(selectedImpl)
else:
implementationRef = self.serviceManager.loadInterface(selectedImpl.interfaceName())
self.attributesListWidget.clear()
if not implementationRef:
self.attributesListWidget.addItem(self.tr("(Error loading service plugin)"))
return
metaObject = implementationRef.metaObject()
self.attributesGroup.setTitle(self.tr("Invokable attributes for %s class" % metaObject.className()))
for i in range(metaObject.methodCount()):
method = metaObject.method(i)
self.attributesListWidget.addItem("[METHOD] " + method.signature())
for i in range(metaObject.propertyCount()):
p = metaObject.property(i)
self.attributesListWidget.addItem("[PROPERTY] " + p.name())
def setDefaultInterfaceImplementation(self):
item = self.interfacesListWidget.currentItem()
if not item:
return
descriptor = item.data(Qt.UserRole)
if descriptor.isValid():
if self.serviceManager.setInterfaceDefault(descriptor):
currentIndex = self.interfacesListWidget.row(item)
self.reloadInterfaceImplementationsList()
self.interfacesListWidget.setCurrentRow(currentIndex)
else:
print "Unable to set default service for interface:", descriptor.interfaceName()
def registerExampleServices(self):
exampleXmlFiles = ["filemanagerservice.xml", "bluetoothtransferservice.xml"]
for fileName in exampleXmlFiles:
path = "./xmldata/" + fileName
self.serviceManager.addService(path)
def unregisterExampleServices(self):
self.serviceManager.removeService("FileManagerService")
self.serviceManager.removeService("BluetoothTransferService")
def reloadAttributesRadioButtonText(self):
item = self.interfacesListWidget.currentItem()
if not item:
return
selectedImpl = item.data(Qt.UserRole)
defaultImpl = self.serviceManager.interfaceDefault(selectedImpl.interfaceName())
self.defaultImplRadioButton.setText(self.tr("Default implementation for %s\n(currently provided by %s)" % (str(defaultImpl.interfaceName()), str(defaultImpl.serviceName()))))
def initWidgets(self):
self.showAllServicesItem = QListWidgetItem(self.tr("(All registered services)"))
self.servicesListWidget = QListWidget()
self.interfacesListWidget = QListWidget()
self.interfacesListWidget.addItem(self.tr("(Select a service)"))
self.attributesListWidget = QListWidget()
self.attributesListWidget.addItem(self.tr("(Select an interface implementation)"))
self.interfacesListWidget.setMinimumWidth(450)
self.servicesListWidget.currentItemChanged.connect(self.reloadInterfaceImplementationsList)
self.interfacesListWidget.currentItemChanged.connect(self.currentInterfaceImplChanged)
self.defaultInterfaceButton = QPushButton(self.tr("Set as default implementation"))
self.defaultInterfaceButton.setEnabled(False)
self.defaultInterfaceButton.clicked.connect(self.setDefaultInterfaceImplementation)
self.selectedImplRadioButton = QRadioButton(self.tr("Selected interface implementation"))
self.defaultImplRadioButton = QRadioButton(self.tr("Default implementation"))
self.selectedImplRadioButton.setChecked(True)
self.radioButtons = QButtonGroup(self)
self.radioButtons.addButton(self.selectedImplRadioButton)
self.radioButtons.addButton(self.defaultImplRadioButton)
self.radioButtons.buttonClicked.connect(self.reloadAttributesList)
self.servicesGroup = QGroupBox(self.tr("Show services for:"))
servicesLayout = QVBoxLayout()
servicesLayout.addWidget(self.servicesListWidget)
self.servicesGroup.setLayout(servicesLayout)
self.interfacesGroup = QGroupBox(self.tr("Interface implementations"))
interfacesLayout = QVBoxLayout()
interfacesLayout.addWidget(self.interfacesListWidget)
interfacesLayout.addWidget(self.defaultInterfaceButton)
self.interfacesGroup.setLayout(interfacesLayout)
self.attributesGroup = QGroupBox(self.tr("Invokable attributes"))
attributesLayout = QVBoxLayout()
self.attributesGroup.setLayout(attributesLayout)
attributesLayout.addWidget(self.attributesListWidget)
attributesLayout.addWidget(QLabel(self.tr("Show attributes for:")))
attributesLayout.addWidget(self.selectedImplRadioButton)
attributesLayout.addWidget(self.defaultImplRadioButton)
self.attributesGroup.setLayout(attributesLayout)
layout = QGridLayout()
layout.addWidget(self.servicesGroup, 0, 0)
layout.addWidget(self.attributesGroup, 0, 1, 2, 1)
layout.addWidget(self.interfacesGroup, 1, 0)
self.setLayout(layout)
|