qtmobility/examples/servicebrowser/servicebrowser.cpp
changeset 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include <qservicemanager.h>
       
    45 #include <qserviceinterfacedescriptor.h>
       
    46 
       
    47 #include "servicebrowser.h"
       
    48 
       
    49 Q_DECLARE_METATYPE(QServiceInterfaceDescriptor)
       
    50 
       
    51 ServiceBrowser::ServiceBrowser(QWidget *parent, Qt::WindowFlags flags)
       
    52     : QWidget(parent, flags)
       
    53 {
       
    54     serviceManager = new QServiceManager(this);
       
    55 
       
    56     registerExampleServices();
       
    57 
       
    58     initWidgets();
       
    59     reloadServicesList();
       
    60 
       
    61     setWindowTitle(tr("Services Browser"));
       
    62 }
       
    63 
       
    64 ServiceBrowser::~ServiceBrowser()
       
    65 {
       
    66     unregisterExampleServices();
       
    67 }
       
    68 
       
    69 void ServiceBrowser::currentInterfaceImplChanged(QListWidgetItem *current, QListWidgetItem *previous)
       
    70 {
       
    71     Q_UNUSED(previous);
       
    72     if (!current)
       
    73         return;
       
    74 
       
    75     reloadAttributesList();
       
    76     reloadAttributesRadioButtonText();
       
    77 
       
    78     QServiceInterfaceDescriptor descriptor = current->data(Qt::UserRole).value<QServiceInterfaceDescriptor>();
       
    79     if (descriptor.isValid()) {
       
    80         defaultInterfaceButton->setText(tr("Set as default implementation for %1") 
       
    81                 .arg(descriptor.interfaceName()));
       
    82         defaultInterfaceButton->setEnabled(true);
       
    83     }
       
    84 }
       
    85 
       
    86 void ServiceBrowser::reloadServicesList()
       
    87 {
       
    88     servicesListWidget->clear();
       
    89 
       
    90     QStringList services = serviceManager->findServices();
       
    91     for (int i=0; i<services.count(); i++)
       
    92         servicesListWidget->addItem(services[i]);
       
    93 
       
    94     servicesListWidget->addItem(showAllServicesItem);
       
    95 }
       
    96 
       
    97 void ServiceBrowser::reloadInterfaceImplementationsList()
       
    98 {
       
    99     QString serviceName;
       
   100     if (servicesListWidget->currentItem()
       
   101             && servicesListWidget->currentItem() != showAllServicesItem) {
       
   102         serviceName = servicesListWidget->currentItem()->text();
       
   103         interfacesGroup->setTitle(tr("Interfaces implemented by %1").arg(serviceName));
       
   104     } else {
       
   105         interfacesGroup->setTitle(tr("All interface implementations"));
       
   106     }
       
   107 
       
   108     QList<QServiceInterfaceDescriptor> descriptors = serviceManager->findInterfaces(serviceName);
       
   109 
       
   110     attributesListWidget->clear();
       
   111     interfacesListWidget->clear();
       
   112     for (int i=0; i<descriptors.count(); i++) {
       
   113         QString text = QString("%1 %2.%3")
       
   114                 .arg(descriptors[i].interfaceName())
       
   115                 .arg(descriptors[i].majorVersion())
       
   116                 .arg(descriptors[i].minorVersion());
       
   117 
       
   118         if (serviceName.isEmpty())
       
   119             text += " (" + descriptors[i].serviceName() + ")";
       
   120 
       
   121         QServiceInterfaceDescriptor defaultInterfaceImpl = 
       
   122                 serviceManager->interfaceDefault(descriptors[i].interfaceName());
       
   123         if (descriptors[i] == defaultInterfaceImpl)
       
   124             text += tr(" (default)");
       
   125 
       
   126         QListWidgetItem *item = new QListWidgetItem(text);
       
   127         item->setData(Qt::UserRole, qVariantFromValue(descriptors[i]));
       
   128         interfacesListWidget->addItem(item);
       
   129     }
       
   130 
       
   131     defaultInterfaceButton->setEnabled(false);
       
   132 }
       
   133 
       
   134 void ServiceBrowser::reloadAttributesList()
       
   135 {
       
   136     QListWidgetItem *item = interfacesListWidget->currentItem();
       
   137     if (!item)
       
   138         return;
       
   139 
       
   140     QServiceInterfaceDescriptor selectedImpl =
       
   141             item->data(Qt::UserRole).value<QServiceInterfaceDescriptor>();
       
   142 
       
   143     QObject *implementationRef;
       
   144     if (selectedImplRadioButton->isChecked())
       
   145         implementationRef = serviceManager->loadInterface(selectedImpl, 0, 0);
       
   146     else
       
   147         implementationRef = serviceManager->loadInterface(selectedImpl.interfaceName(), 0, 0);
       
   148 
       
   149     attributesListWidget->clear();
       
   150     if (!implementationRef) {
       
   151         attributesListWidget->addItem(tr("(Error loading service plugin)"));
       
   152         return;
       
   153     }
       
   154 
       
   155     const QMetaObject *metaObject = implementationRef->metaObject();
       
   156     attributesGroup->setTitle(tr("Invokable attributes for %1 class")
       
   157             .arg(QString(metaObject->className())));
       
   158     for (int i=0; i<metaObject->methodCount(); i++) {
       
   159         QMetaMethod method = metaObject->method(i);
       
   160         attributesListWidget->addItem("[METHOD] " + QString(method.signature()));
       
   161     }
       
   162     for (int i=0; i<metaObject->propertyCount(); i++) {
       
   163         QMetaProperty property = metaObject->property(i);
       
   164         attributesListWidget->addItem("[PROPERTY] " + QString(property.name()));
       
   165     }
       
   166 }
       
   167 
       
   168 void ServiceBrowser::setDefaultInterfaceImplementation()
       
   169 {
       
   170     QListWidgetItem *item = interfacesListWidget->currentItem();
       
   171     if (!item)
       
   172         return;
       
   173 
       
   174     QServiceInterfaceDescriptor descriptor = item->data(Qt::UserRole).value<QServiceInterfaceDescriptor>();
       
   175     if (descriptor.isValid()) {
       
   176         if (serviceManager->setInterfaceDefault(descriptor)) {
       
   177             int currentIndex = interfacesListWidget->row(item);
       
   178             reloadInterfaceImplementationsList();
       
   179             interfacesListWidget->setCurrentRow(currentIndex);
       
   180         } else {
       
   181             qWarning() << "Unable to set default service for interface:" 
       
   182                     << descriptor.interfaceName();
       
   183         }
       
   184     }
       
   185 }
       
   186 
       
   187 void ServiceBrowser::registerExampleServices()
       
   188 {
       
   189     QStringList exampleXmlFiles;
       
   190     exampleXmlFiles << "filemanagerservice.xml" << "bluetoothtransferservice.xml";
       
   191     foreach (const QString &fileName, exampleXmlFiles) {
       
   192         QString path = QCoreApplication::applicationDirPath() + "/xmldata/" + fileName;
       
   193         serviceManager->addService(path);
       
   194     }
       
   195 }
       
   196 
       
   197 void ServiceBrowser::unregisterExampleServices()
       
   198 {
       
   199     serviceManager->removeService("FileManagerService");
       
   200     serviceManager->removeService("BluetoothTransferService");
       
   201 }
       
   202 
       
   203 void ServiceBrowser::reloadAttributesRadioButtonText()
       
   204 {
       
   205     QListWidgetItem *item = interfacesListWidget->currentItem();
       
   206     if (!item)
       
   207         return;
       
   208 
       
   209     QServiceInterfaceDescriptor selectedImpl =
       
   210             item->data(Qt::UserRole).value<QServiceInterfaceDescriptor>();
       
   211     QServiceInterfaceDescriptor defaultImpl = 
       
   212             serviceManager->interfaceDefault(selectedImpl.interfaceName());
       
   213 
       
   214     defaultImplRadioButton->setText(tr("Default implementation for %1\n(currently provided by %2)")
       
   215             .arg(defaultImpl.interfaceName())
       
   216             .arg(defaultImpl.serviceName()));
       
   217 }
       
   218 
       
   219 void ServiceBrowser::initWidgets()
       
   220 {
       
   221     showAllServicesItem = new QListWidgetItem(tr("(All registered services)"));
       
   222 
       
   223     servicesListWidget = new QListWidget;
       
   224     interfacesListWidget = new QListWidget;
       
   225     interfacesListWidget->addItem(tr("(Select a service)"));
       
   226     attributesListWidget = new QListWidget;
       
   227     attributesListWidget->addItem(tr("(Select an interface implementation)"));
       
   228 
       
   229 #ifndef Q_OS_SYMBIAN
       
   230     interfacesListWidget->setMinimumWidth(450);
       
   231 #endif
       
   232 
       
   233     connect(servicesListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
       
   234             this, SLOT(reloadInterfaceImplementationsList()));
       
   235 
       
   236     connect(interfacesListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
       
   237             this, SLOT(currentInterfaceImplChanged(QListWidgetItem*,QListWidgetItem*)));
       
   238 
       
   239     defaultInterfaceButton = new QPushButton(tr("Set as default implementation"));
       
   240     defaultInterfaceButton->setEnabled(false);
       
   241     connect(defaultInterfaceButton, SIGNAL(clicked()),
       
   242             this, SLOT(setDefaultInterfaceImplementation()));
       
   243 
       
   244     selectedImplRadioButton = new QRadioButton(tr("Selected interface implementation"));
       
   245     defaultImplRadioButton = new QRadioButton(tr("Default implementation"));
       
   246     selectedImplRadioButton->setChecked(true);
       
   247 
       
   248     QButtonGroup *radioButtons = new QButtonGroup(this);
       
   249     radioButtons->addButton(selectedImplRadioButton);
       
   250     radioButtons->addButton(defaultImplRadioButton);
       
   251     connect(radioButtons, SIGNAL(buttonClicked(QAbstractButton*)),
       
   252             this, SLOT(reloadAttributesList()));
       
   253 
       
   254     QGroupBox *servicesGroup = new QGroupBox(tr("Show services for:"));
       
   255     QVBoxLayout *servicesLayout = new QVBoxLayout;
       
   256     servicesLayout->addWidget(servicesListWidget);
       
   257     servicesGroup->setLayout(servicesLayout);
       
   258 
       
   259     interfacesGroup = new QGroupBox(tr("Interface implementations"));
       
   260     QVBoxLayout *interfacesLayout = new QVBoxLayout;
       
   261     interfacesLayout->addWidget(interfacesListWidget);
       
   262     interfacesLayout->addWidget(defaultInterfaceButton);
       
   263     interfacesGroup->setLayout(interfacesLayout);
       
   264 
       
   265     attributesGroup = new QGroupBox(tr("Invokable attributes"));
       
   266     QVBoxLayout *attributesLayout = new QVBoxLayout;
       
   267     attributesLayout->addWidget(attributesListWidget);
       
   268     attributesLayout->addWidget(new QLabel(tr("Show attributes for:")));
       
   269     attributesLayout->addWidget(selectedImplRadioButton);
       
   270     attributesLayout->addWidget(defaultImplRadioButton);
       
   271     attributesGroup->setLayout(attributesLayout);
       
   272 
       
   273 #ifndef Q_OS_SYMBIAN
       
   274     QGridLayout *layout = new QGridLayout;
       
   275     layout->addWidget(servicesGroup, 0, 0);
       
   276     layout->addWidget(attributesGroup, 0, 1, 2, 1);
       
   277     layout->addWidget(interfacesGroup, 1, 0);
       
   278 #else
       
   279     QVBoxLayout *layout = new QVBoxLayout;    
       
   280     QStackedLayout *stackedLayout = new QStackedLayout;
       
   281     stackedLayout->addWidget(servicesGroup);
       
   282     stackedLayout->addWidget(interfacesGroup);
       
   283     stackedLayout->addWidget(attributesGroup);
       
   284     
       
   285     QComboBox *pageComboBox = new QComboBox;
       
   286     pageComboBox->addItem(tr("Services"));
       
   287     pageComboBox->addItem(tr("Interfaces"));
       
   288     pageComboBox->addItem(tr("Attributes"));
       
   289     connect(pageComboBox, SIGNAL(activated(int)),
       
   290             stackedLayout, SLOT(setCurrentIndex(int)));
       
   291     
       
   292     layout->addWidget(pageComboBox);
       
   293     layout->addLayout(stackedLayout);
       
   294 #endif
       
   295 
       
   296     setLayout(layout);
       
   297 }