homescreenapp/examples/contentpublishclient/src/contentpublishclient.cpp
branchRCL_3
changeset 82 5f0182e07bfb
equal deleted inserted replaced
79:f00a6757af32 82:5f0182e07bfb
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Example of home screen content publishing client
       
    15 *
       
    16 */
       
    17 
       
    18 #include <qservicefilter.h>
       
    19 #include <qserviceinterfacedescriptor.h>
       
    20 
       
    21 #include <QMetaObject>
       
    22 #include <QMetaMethod>
       
    23 
       
    24 #include "contentpublishclient.h"
       
    25 
       
    26 /*!
       
    27     \ingroup group_content_publish_client
       
    28     \class ContentPublishClient
       
    29     \brief Example implementation for home screen client api usage.
       
    30 
       
    31     ContentPublishClient is derived from QObject and implements 
       
    32     needed functions for the home screen content publish client. 
       
    33 */
       
    34 
       
    35 /*!
       
    36     Constructor.
       
    37 */
       
    38 ContentPublishClient::ContentPublishClient(QServiceManager &manager, QObject *parent)
       
    39     : QObject(parent),
       
    40       mManager(&manager)
       
    41 {
       
    42 }
       
    43 
       
    44 /*!
       
    45     Destructor
       
    46 */
       
    47 ContentPublishClient::~ContentPublishClient()
       
    48 {
       
    49 }
       
    50 
       
    51 /*!
       
    52     Loads service interface
       
    53 */
       
    54 bool ContentPublishClient::load()
       
    55 {    
       
    56     QServiceFilter filter("com.nokia.symbian.IHomeScreenClient");
       
    57     filter.setServiceName("hshomescreenclientplugin");
       
    58     QList<QServiceInterfaceDescriptor> interfaces = mManager->findInterfaces(filter);
       
    59 
       
    60     if(interfaces.isEmpty()) {
       
    61         QServiceManager::Error error = mManager->error();
       
    62         return false;
       
    63     }
       
    64     qDebug() << interfaces.first().interfaceName()
       
    65              << interfaces.first().serviceName()
       
    66              << interfaces.first().isValid();
       
    67 
       
    68     mService = mManager->loadInterface(interfaces.first());
       
    69         
       
    70     return (mService);
       
    71 }
       
    72 
       
    73 /*!
       
    74     Adds hello world widget 
       
    75 */
       
    76 void ContentPublishClient::addHelloworldWidget()
       
    77 {
       
    78     if ( addWidget("helloworldwidgetplugin")) {
       
    79         qDebug() << "HelloWorld added"; 
       
    80     } else {
       
    81         qDebug() << "adding HelloWorld failed"; 
       
    82     }
       
    83 }
       
    84 
       
    85 /*!
       
    86     Adds clock world widget 
       
    87 */
       
    88 void ContentPublishClient::addClockWidget()
       
    89 {
       
    90     if ( addWidget("hsclockwidgetplugin")) {
       
    91         qDebug() << "Clock widget added"; 
       
    92     } else {
       
    93         qDebug() << "adding Clock widget failed"; 
       
    94     }
       
    95 }
       
    96 
       
    97 /*!
       
    98     Adds widget \a widgetUri utilizing service interface and invokeMethod call
       
    99 */
       
   100 // Start of snippet 1
       
   101 bool ContentPublishClient::addWidget(QString widgetUri)
       
   102 {   
       
   103     // find interface IHomeScreenClient from service hshomescreenclientplugin 
       
   104     QServiceManager manager;
       
   105     QServiceFilter filter("com.nokia.symbian.IHomeScreenClient");
       
   106     filter.setServiceName("hshomescreenclientplugin");
       
   107     QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
       
   108 
       
   109     if(interfaces.isEmpty()) {
       
   110         QServiceManager::Error error = manager.error();
       
   111         return false;
       
   112     }
       
   113     
       
   114     QObject* service = manager.loadInterface(interfaces.first());
       
   115 
       
   116     // access service's addWidget function
       
   117     bool retVal = false;
       
   118     bool ret = QMetaObject::invokeMethod( 
       
   119         service, 
       
   120         "addWidget",
       
   121         Qt::DirectConnection,
       
   122         Q_RETURN_ARG(bool, retVal),
       
   123         Q_ARG(QString,widgetUri),
       
   124         Q_ARG(QVariantHash,QVariantHash()));
       
   125         
       
   126     if(!ret){
       
   127         // invokeMethod returned error
       
   128         return false;
       
   129     }
       
   130     if(!retVal){
       
   131         // addWidget returned error
       
   132         return false;
       
   133     }
       
   134     return true;
       
   135 }
       
   136 // End of snippet 1
       
   137 
       
   138 /*!
       
   139     Sets home screen wallpaper as testwallpaper.png 
       
   140 */
       
   141 void ContentPublishClient::setWallpaper1()
       
   142 {
       
   143     QString wallpaper = "c:/data/images/testwallpaper.jpg";
       
   144     setWallpaper(wallpaper);
       
   145 }
       
   146 
       
   147 /*!
       
   148     Sets home screen wallpaper as testwallpaper2.png 
       
   149 */
       
   150 void ContentPublishClient::setWallpaper2()
       
   151 {
       
   152     QString wallpaper = "c:/data/images/testwallpaper2.jpg";
       
   153     setWallpaper(wallpaper);
       
   154 }
       
   155 
       
   156 /*!
       
   157     Changes home screen wallpaper to \a wallpaper image.
       
   158     Note. load function needs to be called before this, it creates mService object.
       
   159     
       
   160 */
       
   161 // Start of snippet 2
       
   162 bool ContentPublishClient::setWallpaper(QString wallpaper)
       
   163 {   
       
   164     QByteArray signature = QMetaObject::normalizedSignature("setWallpaper(QString)");
       
   165     int methodIndex = mService->metaObject()->indexOfMethod(signature);   
       
   166     if (methodIndex<0) {
       
   167         return false;
       
   168     }
       
   169     QMetaMethod method = mService->metaObject()->method(methodIndex);
       
   170     bool retVal(false);
       
   171 
       
   172     bool ret = method.invoke( mService,
       
   173                               Qt::DirectConnection,
       
   174                               Q_RETURN_ARG(bool, retVal),
       
   175                               Q_ARG(QString,wallpaper));
       
   176                     
       
   177     if(!ret){
       
   178         // invokeMethod returned error
       
   179         return false;
       
   180     }
       
   181     if(!retVal){
       
   182         // setWallpaper returned error
       
   183         return false;
       
   184     }
       
   185     return true;
       
   186 }
       
   187 
       
   188 // End of snippet 2