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