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