| 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 examples of the Qt Mobility Components. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 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 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QtGui> |
| 52 | #include <QtWidgets> |
| 53 | |
| 54 | #include <qservicemanager.h> |
| 55 | #include <qservicereply.h> |
| 56 | #include <qserviceinterfacedescriptor.h> |
| 57 | #include <qservicereply.h> |
| 58 | |
| 59 | #include "servicebrowser.h" |
| 60 | |
| 61 | Q_DECLARE_METATYPE(QServiceInterfaceDescriptor) |
| 62 | |
| 63 | ServiceBrowser::ServiceBrowser(QWidget *parent, Qt::WindowFlags flags) |
| 64 | : QWidget(parent, flags) |
| 65 | { |
| 66 | serviceManager = new QServiceManager(this); |
| 67 | systemManager = new QServiceManager(QService::SystemScope); |
| 68 | |
| 69 | registerExampleServices(); |
| 70 | |
| 71 | initWidgets(); |
| 72 | reloadServicesList(); |
| 73 | |
| 74 | setWindowTitle(tr("Services Browser")); |
| 75 | } |
| 76 | |
| 77 | ServiceBrowser::~ServiceBrowser() |
| 78 | { |
| 79 | unregisterExampleServices(); |
| 80 | } |
| 81 | |
| 82 | void ServiceBrowser::currentInterfaceImplChanged(QListWidgetItem *current, QListWidgetItem *previous) |
| 83 | { |
| 84 | Q_UNUSED(previous); |
| 85 | if (!current) |
| 86 | return; |
| 87 | |
| 88 | reloadAttributesList(); |
| 89 | reloadAttributesRadioButtonText(); |
| 90 | |
| 91 | QServiceInterfaceDescriptor descriptor = current->data(Qt::UserRole).value<QServiceInterfaceDescriptor>(); |
| 92 | if (descriptor.isValid()) { |
| 93 | defaultInterfaceButton->setText(tr("Set as default implementation for %1").arg(descriptor.interfaceName())); |
| 94 | //TODO: .arg(descriptor.interfaceName())); |
| 95 | defaultInterfaceButton->setEnabled(true); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void ServiceBrowser::reloadServicesList() |
| 100 | { |
| 101 | servicesListWidget->clear(); |
| 102 | |
| 103 | QSet<QString> services; |
| 104 | QList<QServiceInterfaceDescriptor> descriptors = serviceManager->findInterfaces(); |
| 105 | for (int i=0; i<descriptors.count(); i++) { |
| 106 | QString service = descriptors[i].serviceName(); |
| 107 | |
| 108 | if (descriptors[i].scope() == QService::SystemScope) |
| 109 | service += tr(" (system)"); |
| 110 | |
| 111 | services << service; |
| 112 | } |
| 113 | |
| 114 | foreach (const QString& service, services) |
| 115 | servicesListWidget->addItem(service); |
| 116 | |
| 117 | servicesListWidget->sortItems(); |
| 118 | servicesListWidget->addItem(showAllServicesItem); |
| 119 | } |
| 120 | |
| 121 | void ServiceBrowser::reloadInterfaceImplementationsList() |
| 122 | { |
| 123 | QString serviceName; |
| 124 | if (servicesListWidget->currentItem() |
| 125 | && servicesListWidget->currentItem() != showAllServicesItem) { |
| 126 | serviceName = servicesListWidget->currentItem()->text(); |
| 127 | interfacesGroup->setTitle(tr("Interfaces implemented by %1").arg(serviceName)); |
| 128 | } else { |
| 129 | interfacesGroup->setTitle(tr("All interface implementations")); |
| 130 | } |
| 131 | |
| 132 | QServiceManager *manager = serviceManager; |
| 133 | if (serviceName.endsWith(" (system)")) { |
| 134 | serviceName.chop(9); |
| 135 | manager = systemManager; |
| 136 | } |
| 137 | |
| 138 | QList<QServiceInterfaceDescriptor> descriptors = manager->findInterfaces(serviceName); |
| 139 | |
| 140 | attributesListWidget->clear(); |
| 141 | interfacesListWidget->clear(); |
| 142 | for (int i=0; i<descriptors.count(); i++) { |
| 143 | if (descriptors[i].scope() != manager->scope() && !serviceName.isEmpty()) |
| 144 | continue; |
| 145 | |
| 146 | QString text = QString("%1 %2.%3") |
| 147 | .arg(descriptors[i].interfaceName()) |
| 148 | .arg(descriptors[i].majorVersion()) |
| 149 | .arg(descriptors[i].minorVersion()); |
| 150 | |
| 151 | QServiceInterfaceDescriptor defaultInterfaceImpl |
| 152 | = manager->interfaceDefault(descriptors[i].interfaceName()); |
| 153 | |
| 154 | if (serviceName.isEmpty()) { |
| 155 | text += " (" + descriptors[i].serviceName() + ")"; |
| 156 | |
| 157 | if (descriptors[i].scope() == QService::SystemScope) { |
| 158 | text += tr(" (system"); |
| 159 | defaultInterfaceImpl = systemManager->interfaceDefault(descriptors[i].interfaceName()); |
| 160 | if (descriptors[i] == defaultInterfaceImpl) |
| 161 | text += tr(" default)"); |
| 162 | else |
| 163 | text += ")"; |
| 164 | defaultInterfaceImpl = QServiceInterfaceDescriptor(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (descriptors[i] == defaultInterfaceImpl) |
| 169 | text += tr(" (default)"); |
| 170 | |
| 171 | QListWidgetItem *item = new QListWidgetItem(text); |
| 172 | item->setData(Qt::UserRole, qVariantFromValue(descriptors[i])); |
| 173 | interfacesListWidget->addItem(item); |
| 174 | } |
| 175 | |
| 176 | defaultInterfaceButton->setEnabled(false); |
| 177 | } |
| 178 | |
| 179 | void ServiceBrowser::reloadAttributesList() |
| 180 | { |
| 181 | QListWidgetItem *item = interfacesListWidget->currentItem(); |
| 182 | if (!item) |
| 183 | return; |
| 184 | |
| 185 | QServiceInterfaceDescriptor selectedImpl = |
| 186 | item->data(Qt::UserRole).value<QServiceInterfaceDescriptor>(); |
| 187 | |
| 188 | QServiceReply *reply = 0; |
| 189 | if (selectedImplRadioButton->isChecked()) |
| 190 | reply = serviceManager->loadInterfaceRequest(selectedImpl); |
| 191 | else |
| 192 | reply = serviceManager->loadInterfaceRequest(selectedImpl.interfaceName()); |
| 193 | |
| 194 | connect(reply, SIGNAL(finished()), this, SLOT(handleImplementationReply())); |
| 195 | } |
| 196 | |
| 197 | void ServiceBrowser::handleImplementationReply() |
| 198 | { |
| 199 | QServiceReply *reply = static_cast< QServiceReply * >(sender()); |
| 200 | QObject *implementationRef = reply->proxyObject(); |
| 201 | attributesListWidget->clear(); |
| 202 | if (!implementationRef) { |
| 203 | attributesListWidget->addItem(tr("(Error loading service plugin)")); |
| 204 | qWarning() << "Error code for service load failure was" << reply->error(); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | const QMetaObject *metaObject = implementationRef->metaObject(); |
| 209 | attributesGroup->setTitle(tr("Invokable attributes for %1 class") |
| 210 | .arg(QString(metaObject->className()))); |
| 211 | for (int i=0; i<metaObject->methodCount(); i++) { |
| 212 | QMetaMethod method = metaObject->method(i); |
| 213 | attributesListWidget->addItem("[METHOD] " + QString(method.methodSignature())); |
| 214 | } |
| 215 | for (int i=0; i<metaObject->propertyCount(); i++) { |
| 216 | QMetaProperty property = metaObject->property(i); |
| 217 | attributesListWidget->addItem("[PROPERTY] " + QString(property.name())); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void ServiceBrowser::setDefaultInterfaceImplementation() |
| 222 | { |
| 223 | QListWidgetItem *item = interfacesListWidget->currentItem(); |
| 224 | if (!item) |
| 225 | return; |
| 226 | |
| 227 | QServiceInterfaceDescriptor descriptor = item->data(Qt::UserRole).value<QServiceInterfaceDescriptor>(); |
| 228 | if (descriptor.isValid()) { |
| 229 | QServiceManager *manager = serviceManager; |
| 230 | |
| 231 | if (descriptor.scope() == QService::SystemScope) |
| 232 | manager = systemManager; |
| 233 | |
| 234 | if (manager->setInterfaceDefault(descriptor)) { |
| 235 | int currentIndex = interfacesListWidget->row(item); |
| 236 | reloadInterfaceImplementationsList(); |
| 237 | interfacesListWidget->setCurrentRow(currentIndex); |
| 238 | } else { |
| 239 | qWarning() << "Unable to set default service for interface:" |
| 240 | << descriptor.interfaceName(); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void ServiceBrowser::registerExampleServices() |
| 246 | { |
| 247 | // QStringList exampleXmlFiles; |
| 248 | // exampleXmlFiles << "filemanagerservice.xml" << "bluetoothtransferservice.xml" << "notesmanagerservice.xml"; |
| 249 | // foreach (const QString &fileName, exampleXmlFiles) { |
| 250 | // const QString path = QCoreApplication::applicationDirPath() + "/xmldata/" + fileName; |
| 251 | // serviceManager->addService(path); |
| 252 | // } |
| 253 | } |
| 254 | |
| 255 | void ServiceBrowser::unregisterExampleServices() |
| 256 | { |
| 257 | // serviceManager->removeService("FileManagerService"); |
| 258 | // serviceManager->removeService("BluetoothTransferService"); |
| 259 | // serviceManager->removeService("NotesManagerService"); |
| 260 | } |
| 261 | |
| 262 | void ServiceBrowser::reloadAttributesRadioButtonText() |
| 263 | { |
| 264 | QListWidgetItem *item = interfacesListWidget->currentItem(); |
| 265 | if (!item) |
| 266 | return; |
| 267 | |
| 268 | QServiceInterfaceDescriptor selectedImpl = |
| 269 | item->data(Qt::UserRole).value<QServiceInterfaceDescriptor>(); |
| 270 | QServiceInterfaceDescriptor defaultImpl = |
| 271 | serviceManager->interfaceDefault(selectedImpl.interfaceName()); |
| 272 | |
| 273 | defaultImplRadioButton->setText(tr("Default implementation for %1\n(currently provided by %2)") |
| 274 | .arg(defaultImpl.interfaceName()) |
| 275 | .arg(defaultImpl.serviceName())); |
| 276 | } |
| 277 | |
| 278 | void ServiceBrowser::initWidgets() |
| 279 | { |
| 280 | showAllServicesItem = new QListWidgetItem(tr("(All registered services)")); |
| 281 | |
| 282 | servicesListWidget = new QListWidget; |
| 283 | interfacesListWidget = new QListWidget; |
| 284 | interfacesListWidget->addItem(tr("(Select a service)")); |
| 285 | attributesListWidget = new QListWidget; |
| 286 | attributesListWidget->addItem(tr("(Select an interface implementation)")); |
| 287 | |
| 288 | interfacesListWidget->setMinimumWidth(450); |
| 289 | |
| 290 | connect(servicesListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), |
| 291 | this, SLOT(reloadInterfaceImplementationsList())); |
| 292 | |
| 293 | connect(interfacesListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), |
| 294 | this, SLOT(currentInterfaceImplChanged(QListWidgetItem*,QListWidgetItem*))); |
| 295 | |
| 296 | defaultInterfaceButton = new QPushButton(tr("Set as default implementation")); |
| 297 | defaultInterfaceButton->setEnabled(false); |
| 298 | connect(defaultInterfaceButton, SIGNAL(clicked()), |
| 299 | this, SLOT(setDefaultInterfaceImplementation())); |
| 300 | |
| 301 | selectedImplRadioButton = new QRadioButton(tr("Selected interface implementation")); |
| 302 | defaultImplRadioButton = new QRadioButton(tr("Default implementation")); |
| 303 | selectedImplRadioButton->setChecked(true); |
| 304 | |
| 305 | QButtonGroup *radioButtons = new QButtonGroup(this); |
| 306 | radioButtons->addButton(selectedImplRadioButton); |
| 307 | radioButtons->addButton(defaultImplRadioButton); |
| 308 | connect(radioButtons, SIGNAL(buttonClicked(QAbstractButton*)), |
| 309 | this, SLOT(reloadAttributesList())); |
| 310 | |
| 311 | QGroupBox *servicesGroup = new QGroupBox(tr("Show services for:")); |
| 312 | QVBoxLayout *servicesLayout = new QVBoxLayout; |
| 313 | servicesLayout->addWidget(servicesListWidget); |
| 314 | servicesGroup->setLayout(servicesLayout); |
| 315 | |
| 316 | interfacesGroup = new QGroupBox(tr("Interface implementations")); |
| 317 | QVBoxLayout *interfacesLayout = new QVBoxLayout; |
| 318 | interfacesLayout->addWidget(interfacesListWidget); |
| 319 | interfacesLayout->addWidget(defaultInterfaceButton); |
| 320 | interfacesGroup->setLayout(interfacesLayout); |
| 321 | |
| 322 | attributesGroup = new QGroupBox(tr("Invokable attributes")); |
| 323 | QVBoxLayout *attributesLayout = new QVBoxLayout; |
| 324 | attributesLayout->addWidget(attributesListWidget); |
| 325 | attributesLayout->addWidget(new QLabel(tr("Show attributes for:"))); |
| 326 | attributesLayout->addWidget(selectedImplRadioButton); |
| 327 | attributesLayout->addWidget(defaultImplRadioButton); |
| 328 | attributesGroup->setLayout(attributesLayout); |
| 329 | |
| 330 | QGridLayout *layout = new QGridLayout; |
| 331 | layout->addWidget(servicesGroup, 0, 0); |
| 332 | layout->addWidget(attributesGroup, 0, 1, 2, 1); |
| 333 | layout->addWidget(interfacesGroup, 1, 0); |
| 334 | |
| 335 | setLayout(layout); |
| 336 | } |