qtmobility/examples/servicenotesmanager/declarative-sfw-notes/sfwnotes.cpp
changeset 4 90517678cc4f
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 "sfwnotes.h"
       
    43 
       
    44 ServiceWrapper::ServiceWrapper()
       
    45 : serviceInstance(0)
       
    46 {
       
    47     serviceManager = new QServiceManager(this);
       
    48 
       
    49     registerExampleServices();
       
    50 }
       
    51 
       
    52 ServiceWrapper::~ServiceWrapper()
       
    53 {
       
    54     delete serviceInstance;
       
    55     unregisterExampleServices();
       
    56 }
       
    57 
       
    58 QString ServiceWrapper::interfaceName() const
       
    59 {
       
    60     return m_interface;
       
    61 }
       
    62 
       
    63 void ServiceWrapper::setInterfaceName(const QString &interface)
       
    64 {
       
    65     m_interface = interface;
       
    66 }
       
    67 
       
    68 QString ServiceWrapper::serviceName() const
       
    69 {
       
    70     return m_service;
       
    71 }
       
    72 
       
    73 void ServiceWrapper::setServiceName(const QString &service)
       
    74 {
       
    75     m_service = service;
       
    76 }
       
    77 
       
    78 QString ServiceWrapper::version() const
       
    79 {
       
    80     return m_version;
       
    81 }
       
    82 
       
    83 void ServiceWrapper::setVersion(const QString &version)
       
    84 {
       
    85     m_version = version;
       
    86 }
       
    87 
       
    88 bool ServiceWrapper::existsInterface()
       
    89 {
       
    90     return (m_interface != "");
       
    91 }
       
    92 
       
    93 bool ServiceWrapper::existsService()
       
    94 {
       
    95     return (m_service != "");
       
    96 }
       
    97 
       
    98 bool ServiceWrapper::existsVersion()
       
    99 {
       
   100     return (m_version != "");
       
   101 }
       
   102 
       
   103 void ServiceWrapper::findServiceInterface()
       
   104 {
       
   105     QServiceFilter filter;
       
   106     filter.setServiceName(m_service);
       
   107     
       
   108     if (existsInterface())
       
   109         filter.setInterface(m_interface, m_version, QServiceFilter::ExactVersionMatch);
       
   110 
       
   111     QList<QServiceInterfaceDescriptor> list = serviceManager->findInterfaces(filter);
       
   112 
       
   113     if (list.size() == 0) return;
       
   114 
       
   115     if (existsInterface()) {
       
   116         if (existsVersion()) {
       
   117             m_descriptor = list[0];
       
   118         } else {
       
   119             m_descriptor = serviceManager->interfaceDefault(m_interface);
       
   120         }
       
   121 
       
   122     } else if (existsService()) {
       
   123         if (existsVersion()) {
       
   124             for (int i=0; i<list.count(); i++) {
       
   125                 QString currVersion = QString::number(list[i].majorVersion()) + "." + 
       
   126                                       QString::number(list[i].minorVersion());
       
   127 
       
   128                 if ( currVersion == m_version) {
       
   129                     m_descriptor = list[i];
       
   130                     return;
       
   131                 }
       
   132             }
       
   133         } else {
       
   134             int major = -1;
       
   135             int minor = -1;
       
   136 
       
   137             for (int i=0; i<list.count(); i++) {
       
   138                 int currMajor = list[i].majorVersion();
       
   139                 int currMinor = list[i].minorVersion();
       
   140                 
       
   141                 if (currMajor > major) {
       
   142                     major = currMajor;
       
   143                     minor = -1;
       
   144                 }
       
   145                     
       
   146                 if (currMinor > minor) {
       
   147                     minor = currMinor;
       
   148                     m_descriptor = list[i];
       
   149                 }
       
   150             }
       
   151         }
       
   152     }
       
   153 }
       
   154 
       
   155 QObject* ServiceWrapper::serviceObject()
       
   156 {
       
   157     findServiceInterface();
       
   158 
       
   159     if (serviceInstance) {
       
   160         return serviceInstance;
       
   161     }
       
   162 
       
   163     if (m_descriptor.isValid()) {
       
   164         QServiceManager manager;
       
   165         serviceInstance = manager.loadInterface(m_descriptor);
       
   166         return serviceInstance;
       
   167     } else {
       
   168         return 0;
       
   169     }
       
   170 }
       
   171 
       
   172 void ServiceWrapper::registerExampleServices()
       
   173 {
       
   174     QStringList exampleXmlFiles;
       
   175     exampleXmlFiles << "notesmanagerservice.xml";
       
   176     foreach (const QString &fileName, exampleXmlFiles) {
       
   177         QString path = QCoreApplication::applicationDirPath() + "/xmldata/" + fileName;
       
   178         serviceManager->addService(path);
       
   179     }
       
   180 }
       
   181 
       
   182 void ServiceWrapper::unregisterExampleServices()
       
   183 {
       
   184     serviceManager->removeService("NotesManagerService");
       
   185 }