qtmobility/examples/declarative/sfwexample.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     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 "sfwexample.h"
       
    43 
       
    44 QML_DEFINE_TYPE(QtSFW,1,0,Service,ServiceWrapper)
       
    45 
       
    46 ServiceWrapper::ServiceWrapper() 
       
    47 : serviceInstance(0)
       
    48 {
       
    49 }
       
    50 
       
    51 ServiceWrapper::~ServiceWrapper()
       
    52 {
       
    53     delete serviceInstance;
       
    54 }
       
    55 
       
    56 bool ServiceWrapper::isValid() const
       
    57 {
       
    58     return m_descriptor.isValid();
       
    59 }
       
    60 
       
    61 QString ServiceWrapper::interfaceName() const
       
    62 {
       
    63     if (isValid())
       
    64         return m_descriptor.interfaceName();
       
    65     else
       
    66         return "No Interface";
       
    67 }
       
    68 
       
    69 QString ServiceWrapper::serviceName() const
       
    70 {
       
    71     if (isValid()) {
       
    72         return m_descriptor.serviceName();
       
    73     } else
       
    74         return "No Service";
       
    75 }
       
    76 
       
    77 QString ServiceWrapper::version() const
       
    78 {
       
    79     if (isValid())
       
    80         return QString(QString::number(m_descriptor.majorVersion())+"."+QString::number(m_descriptor.minorVersion()));
       
    81     else
       
    82         return QString("0.0");
       
    83 }
       
    84 
       
    85 void ServiceWrapper::setNativeDescriptor(const QServiceInterfaceDescriptor &d)
       
    86 {
       
    87     if (d == m_descriptor)
       
    88         return;
       
    89 
       
    90     m_descriptor = d;
       
    91     
       
    92     if (serviceInstance)
       
    93         delete serviceInstance;
       
    94 
       
    95     serviceInstance = 0;
       
    96 }
       
    97 
       
    98 void ServiceWrapper::setDescriptor(QVariant &newDescriptor)
       
    99 {
       
   100     QServiceInterfaceDescriptor d = newDescriptor.value<QServiceInterfaceDescriptor>();
       
   101     setNativeDescriptor(d);
       
   102 }
       
   103 
       
   104 
       
   105 QObject* ServiceWrapper::serviceObject()
       
   106 {
       
   107     qDebug() << "called serviceObject";
       
   108     if (serviceInstance) {
       
   109         return serviceInstance;
       
   110     }
       
   111 
       
   112     if (isValid()) {
       
   113         QServiceManager manager;
       
   114         serviceInstance = manager.loadInterface(m_descriptor);
       
   115         return serviceInstance;
       
   116     } else {
       
   117         return 0;
       
   118     }
       
   119 }
       
   120 
       
   121 QML_DEFINE_TYPE(QtSFW,1,0,Services,ServiceRegister)
       
   122 
       
   123 ServiceRegister::ServiceRegister()
       
   124 {
       
   125     serviceManager = new QServiceManager();
       
   126     unregisterExampleServices();
       
   127     registerExampleServices();
       
   128 
       
   129     //! [1]
       
   130     ServiceWrapper *service;
       
   131     QServiceFilter filter("com.nokia.qt.examples.Dialer");
       
   132     QList<QServiceInterfaceDescriptor> allImpl = serviceManager->findInterfaces(filter);
       
   133     for (int i = 0; i < allImpl.count(); i++) {
       
   134         qDebug() << "Found service:" << allImpl.at(i).serviceName() << "(" << allImpl.at(i).interfaceName() << ")";
       
   135         service = new ServiceWrapper();
       
   136         service->setNativeDescriptor(allImpl.at(i));
       
   137         m_services.append(service);
       
   138     }
       
   139     //! [1]
       
   140 }
       
   141 
       
   142 ServiceRegister::~ServiceRegister() 
       
   143 {
       
   144     unregisterExampleServices();
       
   145 }
       
   146 
       
   147 void ServiceRegister::registerExampleServices()
       
   148 {
       
   149     //! [0]
       
   150     QStringList exampleXmlFiles;
       
   151     exampleXmlFiles << "voipdialerservice.xml" << "landlinedialerservice.xml";
       
   152     foreach (const QString &fileName, exampleXmlFiles) {
       
   153         QString path = QCoreApplication::applicationDirPath() + "/xmldata/" + fileName;
       
   154         serviceManager->addService(path);
       
   155     }
       
   156     //! [0]
       
   157 }
       
   158 
       
   159 void ServiceRegister::unregisterExampleServices()
       
   160 {
       
   161     serviceManager->removeService("VoipDialer");
       
   162     serviceManager->removeService("LandlineDialer");
       
   163 }
       
   164 
       
   165 
       
   166