emailservices/nmregister/src/nmmailboxregisterinterface.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     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:
       
    15  *
       
    16  */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 
       
    20 #include <qservicemanager.h>
       
    21 #include <qserviceinterfacedescriptor.h>
       
    22 #include <hbicon.h>
       
    23 #include <HbApplication>
       
    24 #include <QTranslator>
       
    25 #include "nmicons.h"
       
    26 #include "nmmailboxregisterinterface.h" 
       
    27 #include "nmmailboxregisterinterface_p.h" 
       
    28 
       
    29 //mandatory keys 
       
    30 const QString hsItemName("item:name");
       
    31 const QString hsItemLocName("item:locname");
       
    32 const QString hsitemLaunchUri("item:launchuri");
       
    33 const QString hsitemPublisherId("item:publisherId");
       
    34 //not mandatory 
       
    35 const QString hsItemWidgetUri("widget:uri");
       
    36 const QString hsItemDescription("item:description");
       
    37 const QString hsItemLocDescription("item:locdescription");
       
    38 //for icon - not mandatory 
       
    39 const QString hsIconFileName("icon:filename");//to display specific icon from file 
       
    40 const QString hsIconName("icon:name");// HbIcon 
       
    41 const QString hsIconApplicationId("icon:applicationid"); // icon from associated application 
       
    42 
       
    43 //Custom for nmHsWidgets
       
    44 //NmHsWidgetParamAccountId is used to identify what email account will be monitored by 
       
    45 //the registered hs widget
       
    46 const QString NmHsWidgetParamAccountId("widgetparam:accountId");
       
    47 //same as above but for pushing the widget to homescreen
       
    48 const QString NmHsAccountId("accountId");
       
    49 //NmHsWidgetParamAccountIconName is used to identify mailbox icon in application library
       
    50 const QString NmHsWidgetParamAccountIconName("widgetparam:accountIconName");
       
    51 //same as above but for pushing the widget to homescreen
       
    52 const QString NmHsAccountIconName("accountIconName");
       
    53 //This is used to identify nmhswidget in homescreen
       
    54 const QString NmHsWidget("nmhswidget");
       
    55 
       
    56 //parameter values
       
    57 const QString NmPublisherName("NmMailboxRegisterInterface");
       
    58 const QString NmLaunchUri("appto://0x200255BA?activityname=EmailInboxView");
       
    59 //URI for the defined service
       
    60 const QLatin1String interfaceUri("com.nokia.symbian.IEmailRegisterAccount");
       
    61 
       
    62 /** Localization file name*/
       
    63 const QString KNmRegisterLocFileName = "nmregister_";
       
    64 /** Localization file location*/
       
    65 const QString KNmRegisterLocLocation = "/translations/";
       
    66 
       
    67 /*!
       
    68  Constructor
       
    69  */
       
    70 NmMailboxRegisterInterface::NmMailboxRegisterInterface(QObject *parent) :
       
    71     QObject(parent), m_d(new NmMailboxRegisterInterfacePrivate(this))
       
    72 {
       
    73     NM_FUNCTION;
       
    74 }
       
    75 
       
    76 /*!
       
    77  Destructor
       
    78  */
       
    79 NmMailboxRegisterInterface::~NmMailboxRegisterInterface()
       
    80 {
       
    81     NM_FUNCTION;
       
    82     
       
    83     delete m_d;
       
    84 }
       
    85 
       
    86 /*!
       
    87  registerNewMailbox
       
    88  */
       
    89 bool NmMailboxRegisterInterface::registerNewMailbox(
       
    90     quint64 accountId,
       
    91     QString accountName,
       
    92     QString accountIconName)
       
    93 {
       
    94     NM_FUNCTION;
       
    95     
       
    96     return m_d->registerNewMailbox(accountId, accountName, accountIconName);
       
    97 }
       
    98 
       
    99 /*!
       
   100  updateMailboxName
       
   101  */
       
   102 bool NmMailboxRegisterInterface::updateMailboxName(quint64 accountId, QString newName)
       
   103 {
       
   104     NM_FUNCTION;
       
   105     
       
   106     return m_d->updateMailboxName(accountId, newName);
       
   107 }
       
   108 
       
   109 /*!
       
   110  unregisterMailbox
       
   111  */
       
   112 bool NmMailboxRegisterInterface::unregisterMailbox(quint64 accountId)
       
   113 {
       
   114     NM_FUNCTION;
       
   115     
       
   116     return m_d->unregisterMailbox(accountId);
       
   117 }
       
   118 
       
   119 /*!
       
   120  Constructor
       
   121  */
       
   122 NmMailboxRegisterInterfacePrivate::NmMailboxRegisterInterfacePrivate(QObject *parent) :
       
   123     XQServiceProvider(interfaceUri, parent), 
       
   124     mManager(new QServiceManager()), 
       
   125     mService(0),
       
   126     mTranslator(new QTranslator())
       
   127 {
       
   128     NM_FUNCTION;
       
   129 }
       
   130 
       
   131 /*!
       
   132  loadMenuClientService()
       
   133  Loads the Menu client service to mService. 
       
   134  \return true if mService exists or is succesfully loaded, otherwise false.
       
   135  */
       
   136 bool NmMailboxRegisterInterfacePrivate::loadMenuClientService()
       
   137 {
       
   138     if (mService){ 
       
   139         return true; //service is already loaded
       
   140     }
       
   141     
       
   142     QServiceFilter filter("com.nokia.symbian.IMenuClient");
       
   143     QList<QServiceInterfaceDescriptor> interfaces = mManager->findInterfaces(filter);
       
   144     if(interfaces.isEmpty()){
       
   145         return false; //no maching interfaces available
       
   146     }
       
   147     
       
   148     mService = mManager->loadInterface(interfaces.first());
       
   149     if(!mService){
       
   150         return false; //loading interface failed
       
   151     }
       
   152     
       
   153     return true; //succesfully loaded the service
       
   154 }
       
   155 
       
   156 /*!
       
   157  Destructor
       
   158  */
       
   159 NmMailboxRegisterInterfacePrivate::~NmMailboxRegisterInterfacePrivate()
       
   160 {
       
   161     NM_FUNCTION;
       
   162 
       
   163     delete mTranslator;
       
   164     mTranslator = NULL;
       
   165 
       
   166     delete mService;
       
   167     mService = NULL;
       
   168 
       
   169     delete mManager;
       
   170     mManager = NULL;
       
   171 }
       
   172 
       
   173 bool NmMailboxRegisterInterfacePrivate::registerNewMailbox(
       
   174     quint64 accountId,
       
   175     QString accountName,
       
   176     QString accountIconName)
       
   177 {
       
   178     NM_FUNCTION;
       
   179 	
       
   180     if(!loadMenuClientService()){
       
   181         NM_ERROR(1,"loadService() failed!");
       
   182         return false; 
       
   183     }
       
   184     
       
   185     //localization
       
   186     QString lang = QLocale::system().name();
       
   187     bool loadSucceed = mTranslator->load(KNmRegisterLocFileName + lang, KNmRegisterLocLocation);
       
   188     QCoreApplication::installTranslator(mTranslator);
       
   189     
       
   190     //check that accountIconName is valid otherwise pass default mailbox icon
       
   191     HbIcon mailBoxIcon(accountIconName);
       
   192     if (mailBoxIcon.pixmap().height() == 0) {
       
   193         mailBoxIcon = NmIcons::getIcon(NmIcons::NmIconDefaultMailbox);
       
   194         accountIconName = mailBoxIcon.iconName();
       
   195     }
       
   196 
       
   197     QMap<QString, QVariant> map;
       
   198     //------------------------------
       
   199     map[hsItemLocName] = "loc://" + KNmRegisterLocFileName + "/txt_mailwdgt_dblist_mail";
       
   200     map[hsitemLaunchUri] = formLaunchUri(accountId);
       
   201     map[hsitemPublisherId] = NmPublisherName;
       
   202     map[hsItemWidgetUri] = NmHsWidget;
       
   203     map[hsItemDescription] = accountName;
       
   204     map[hsIconFileName] = accountIconName;
       
   205     // to add widget params that are mapped to widgets properties
       
   206     map[NmHsWidgetParamAccountId] = QString::number(accountId);
       
   207     map[NmHsWidgetParamAccountIconName] = accountIconName;
       
   208     //------------------------------
       
   209 
       
   210     bool retVal(false);
       
   211     bool ret(false);
       
   212     ret = QMetaObject::invokeMethod( mService,"add",
       
   213         Qt::DirectConnection,
       
   214         Q_RETURN_ARG(bool, retVal),
       
   215         Q_ARG(QVariantMap, map));
       
   216 
       
   217     //Push the registered widget also to homescreen right away
       
   218     bool pushRetVal = pushWidgetToHomescreen(accountId, accountIconName);
       
   219 
       
   220     return (retVal && ret && pushRetVal);
       
   221 }
       
   222 
       
   223 bool NmMailboxRegisterInterfacePrivate::updateMailboxName(quint64 accountId, QString newName)
       
   224 {
       
   225     NM_FUNCTION;
       
   226 
       
   227     if(!loadMenuClientService()){
       
   228     	NM_ERROR(1,"loadService() failed!");
       
   229         return false; 
       
   230     }
       
   231     
       
   232     QMap<QString, QVariant> map;
       
   233     //------------------------------
       
   234     map[hsitemLaunchUri] = formLaunchUri(accountId);
       
   235     map[hsitemPublisherId] = NmPublisherName;
       
   236     map[hsItemWidgetUri] = NmHsWidget;
       
   237     map[NmHsWidgetParamAccountId] = QString::number(accountId);
       
   238     //------------------------------
       
   239 
       
   240     QList<QVariantMap> list; //list of items that mach the query
       
   241     bool retVal(false);
       
   242     bool ret(false);
       
   243     ret = QMetaObject::invokeMethod(mService, "getList", 
       
   244         Qt::DirectConnection, 
       
   245         Q_RETURN_ARG(QList<QVariantMap> , list),
       
   246         Q_ARG(QVariantMap, map));
       
   247 
       
   248     //Normally there should be only one instance, but just in case
       
   249     while (!list.isEmpty()) {
       
   250         //update the account name
       
   251         list.first()[hsItemDescription] = newName;
       
   252         //commit changes
       
   253         ret = QMetaObject::invokeMethod( mService,"add",
       
   254             Qt::DirectConnection,
       
   255             Q_RETURN_ARG(bool, retVal),
       
   256             Q_ARG(QVariantMap, list.first()));
       
   257         list.removeFirst();
       
   258     }
       
   259     //if either invoke or "getList" or "add" return failure
       
   260     return (retVal && ret);
       
   261 }
       
   262 
       
   263 bool NmMailboxRegisterInterfacePrivate::unregisterMailbox(quint64 accountId)
       
   264 {
       
   265     NM_FUNCTION;
       
   266 
       
   267     if(!loadMenuClientService()){
       
   268     	NM_ERROR(1,"loadService() failed!");
       
   269         return false; 
       
   270     }
       
   271     
       
   272     QMap<QString, QVariant> map;
       
   273     //------------------------------
       
   274     map[hsitemLaunchUri] = formLaunchUri(accountId);
       
   275     map[hsitemPublisherId] = NmPublisherName;
       
   276     map[hsItemWidgetUri] = NmHsWidget;
       
   277     map[NmHsWidgetParamAccountId] = QString::number(accountId);
       
   278     //------------------------------
       
   279 
       
   280     QList<QVariantMap> list; //list of items that mach the query
       
   281     bool retVal(false);
       
   282     bool ret(false);
       
   283     ret = QMetaObject::invokeMethod(mService, "getList", 
       
   284         Qt::DirectConnection, 
       
   285         Q_RETURN_ARG(QList<QVariantMap> , list), 
       
   286         Q_ARG(QVariantMap, map));
       
   287 
       
   288     //Normally there should be only one instance, but just in case
       
   289     //Otherwise there will be ghost instances in the application list
       
   290     while (!list.isEmpty()) {
       
   291 
       
   292         //commit changes
       
   293         ret = QMetaObject::invokeMethod( mService,"remove",
       
   294             Qt::DirectConnection,
       
   295             Q_RETURN_ARG(bool, retVal),
       
   296             Q_ARG(QVariantMap, list.first()));
       
   297         list.removeFirst();
       
   298     }
       
   299 
       
   300     return (retVal && ret);
       
   301 }
       
   302 
       
   303 bool NmMailboxRegisterInterfacePrivate::pushWidgetToHomescreen(
       
   304     quint64 accountId,
       
   305     QString accountIconName)
       
   306 {
       
   307     NM_FUNCTION;
       
   308     
       
   309     // load plugin
       
   310     QServiceManager manager;
       
   311     QServiceFilter filter("com.nokia.symbian.IHomeScreenClient");
       
   312     filter.setServiceName("hshomescreenclientplugin");
       
   313     QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
       
   314 
       
   315     if (interfaces.isEmpty()) {
       
   316         return false;
       
   317     }
       
   318     QObject* service = manager.loadInterface(interfaces.first());
       
   319     if (!service) {
       
   320         NM_ERROR(1,"service is null ");
       
   321         return false;
       
   322     }
       
   323 
       
   324     //--------------------------------
       
   325     QVariantHash map;
       
   326     map[NmHsAccountId] = QString::number(accountId);
       
   327     map[NmHsAccountIconName] = accountIconName;
       
   328     //--------------------------------
       
   329 
       
   330     // invoke function synchronously
       
   331     bool retVal(false);
       
   332     bool ret(false);
       
   333     ret = QMetaObject::invokeMethod( service, "addWidget",
       
   334         Qt::DirectConnection,
       
   335         Q_RETURN_ARG(bool, retVal),
       
   336         Q_ARG(QString, NmHsWidget),
       
   337         Q_ARG(QVariantHash, map));
       
   338 
       
   339     delete service;
       
   340     service = NULL;
       
   341 
       
   342     return (retVal && ret);
       
   343 }
       
   344 
       
   345 QString NmMailboxRegisterInterfacePrivate::formLaunchUri(quint64 accountId) const
       
   346 {
       
   347     return NmLaunchUri + QString::number(accountId);
       
   348 }