qtmobility/examples/declarative-sfw-notes/qdeclarativeservice.cpp
changeset 11 06b8e2af4411
parent 8 71781823f776
child 14 6fbed849b4f4
equal deleted inserted replaced
8:71781823f776 11:06b8e2af4411
     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: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 "qdeclarativeservice.h"
       
    42 
       
    43 QServiceWrapper::QServiceWrapper()
       
    44 : serviceInstance(0)
       
    45 {
       
    46     serviceManager = new QServiceManager();
       
    47 }
       
    48 
       
    49 QServiceWrapper::~QServiceWrapper()
       
    50 {
       
    51     delete serviceInstance;
       
    52 }
       
    53 
       
    54 bool QServiceWrapper::isValid() const
       
    55 {
       
    56     return m_descriptor.isValid();
       
    57 }
       
    58 
       
    59 void QServiceWrapper::setInterfaceDesc(const QServiceInterfaceDescriptor &desc)
       
    60 {
       
    61     if (desc == m_descriptor)
       
    62         return;
       
    63 
       
    64     m_descriptor = desc;
       
    65 
       
    66     if (serviceInstance)
       
    67         delete serviceInstance;
       
    68 
       
    69     serviceInstance = 0;
       
    70 }
       
    71 
       
    72 QServiceInterfaceDescriptor QServiceWrapper::interfaceDesc() const
       
    73 {
       
    74     return m_descriptor;
       
    75 }
       
    76 
       
    77 void QServiceWrapper::setInterfaceName(const QString &interface)
       
    78 {
       
    79     m_descriptor = serviceManager->interfaceDefault(interface);
       
    80 
       
    81     if (!isValid())
       
    82         qWarning() << "WARNING: No default service found for interface name: " << interface;
       
    83 }
       
    84 
       
    85 QString QServiceWrapper::interfaceName() const
       
    86 {
       
    87     if (isValid())
       
    88         return m_descriptor.interfaceName();
       
    89     else
       
    90         return "No Interface";
       
    91 }
       
    92 
       
    93 QString QServiceWrapper::serviceName() const
       
    94 {
       
    95     if (isValid())
       
    96         return m_descriptor.serviceName();
       
    97     else
       
    98         return "No Service";
       
    99 }
       
   100 
       
   101 QString QServiceWrapper::versionNumber() const
       
   102 {
       
   103     if (isValid())
       
   104         return (QString::number(m_descriptor.majorVersion())+"."+QString::number(m_descriptor.minorVersion()));
       
   105     else
       
   106         return "0.0";
       
   107 }
       
   108 
       
   109 QObject* QServiceWrapper::serviceObject()
       
   110 {
       
   111     if (serviceInstance) {
       
   112         return serviceInstance;
       
   113     }
       
   114 
       
   115     if (isValid()) {
       
   116         QServiceManager manager;
       
   117         serviceInstance = manager.loadInterface(m_descriptor);
       
   118         return serviceInstance;
       
   119     } else {
       
   120         return 0;
       
   121     }
       
   122 }
       
   123 
       
   124 QServiceListWrapper::QServiceListWrapper()
       
   125 {
       
   126     serviceManager = new QServiceManager();
       
   127 }
       
   128 
       
   129 QServiceListWrapper::~QServiceListWrapper()
       
   130 {
       
   131 }
       
   132 
       
   133 void QServiceListWrapper::setInterfaceName(const QString &interface)
       
   134 {
       
   135     m_interface = interface;
       
   136 
       
   137     QServiceWrapper *service;
       
   138     QServiceFilter filter(m_interface, m_version);
       
   139     QList<QServiceInterfaceDescriptor> list = serviceManager->findInterfaces(filter);
       
   140     for (int i = 0; i < list.size(); i++) {
       
   141         service = new QServiceWrapper();
       
   142         service->setInterfaceDesc(list.at(i));
       
   143         m_services.append(service);
       
   144     }
       
   145 }
       
   146 
       
   147 QString QServiceListWrapper::interfaceName() const
       
   148 {
       
   149     return m_interface;
       
   150 }
       
   151 
       
   152 void QServiceListWrapper::setMinVersion(const QString &version)
       
   153 {
       
   154     m_version = version;
       
   155 }
       
   156 
       
   157 QString QServiceListWrapper::minVersion() const
       
   158 {
       
   159     return m_version;
       
   160 }
       
   161 
       
   162 QDeclarativeListProperty<QServiceWrapper> QServiceListWrapper::services()
       
   163 {
       
   164     return QDeclarativeListProperty<QServiceWrapper>(this, m_services);
       
   165 }