qthighway/xqservice/src/xqaiwservicedriver.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /*
       
     2 *
       
     3 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 * All rights reserved.
       
     5 *
       
     6 * This program is free software: you can redistribute it and/or modify
       
     7 * it under the terms of the GNU Lesser General Public License as published by
       
     8 * the Free Software Foundation, version 2.1 of the License.
       
     9 * 
       
    10 * This program is distributed in the hope that it will be useful,
       
    11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 * GNU Lesser General Public License for more details.
       
    14 *
       
    15 * You should have received a copy of the GNU Lesser General Public License
       
    16 * along with this program.  If not, 
       
    17 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    18 *
       
    19 * Description:
       
    20 *
       
    21 */
       
    22 
       
    23 #include <QObject>
       
    24 #include <qglobal.h>
       
    25 #include <QApplication>
       
    26 #include <QLatin1String>
       
    27 #include <QLocale>
       
    28 #include <QIcon>
       
    29 #include <QFileInfo>
       
    30 
       
    31 #include <xqaiwdecl.h>
       
    32 #include <xqservicerequest.h>
       
    33 #include "xqrequestutil.h"
       
    34 #include "xqservicelog.h"
       
    35 #include "xqaiwutils.h"
       
    36 #include "xqaiwservicedriver.h"
       
    37 
       
    38 // Constants
       
    39 
       
    40 XQAiwServiceDriver::XQAiwServiceDriver(const XQAiwInterfaceDescriptor& descriptor, const QString &operation)
       
    41     : XQAiwRequestDriver(),
       
    42       currentRequest(NULL),
       
    43       asyncErrorSet(false)
       
    44     {
       
    45     mErrorMsg = "";
       
    46     mDescr = descriptor; 
       
    47     mOperation = operation; 
       
    48     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::XQAiwServiceDriver: %s %s,%x",
       
    49                           qPrintable(mDescr.interfaceName()),
       
    50                           qPrintable(mOperation),
       
    51                           mDescr.property(XQAiwInterfaceDescriptor::ImplementationId).toInt());
       
    52     
       
    53     // Allocate QtHighway request
       
    54     currentRequest = new XQServiceRequest(mDescr, mOperation);
       
    55     }
       
    56 
       
    57 XQAiwServiceDriver::~XQAiwServiceDriver()
       
    58 {
       
    59     XQSERVICE_DEBUG_PRINT("~XQAiwServiceDriver::XQAiwServiceDriver");
       
    60 
       
    61     // Disconnect error
       
    62     if (asyncErrorSet)
       
    63     {
       
    64         disconnect(currentRequest, SIGNAL(requestCompleted(const QVariant&)), this, SLOT(handleAsyncResponse(const QVariant&)));
       
    65         disconnect(currentRequest, SIGNAL(requestError(int)), this, SLOT(handleAsyncError(int)));
       
    66     }
       
    67     
       
    68     delete currentRequest; // Destructor cancels the async request
       
    69 
       
    70     XQSERVICE_DEBUG_PRINT("~XQAiwServiceDriver::XQAiwServiceDriver 2");
       
    71     removeTranslator();
       
    72     currentRequest = NULL;
       
    73 }
       
    74 
       
    75 
       
    76 
       
    77 QAction *XQAiwServiceDriver::createAction()
       
    78 {
       
    79     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::createAction");
       
    80     
       
    81     if (!qApp)
       
    82         return NULL;  // Not supported in non-GUI environments
       
    83 
       
    84     installTranslator();
       
    85     
       
    86     QString textId = mDescr.customProperty(XQCUSTOM_PROP_AIW_TEXT);
       
    87     if (textId.isEmpty())
       
    88     {
       
    89         // Applye the key name and make it visible to client as indication of
       
    90         // missing property ..
       
    91         textId = "#missing " + XQCUSTOM_PROP_AIW_TEXT;
       
    92     }
       
    93     
       
    94     QByteArray ba = textId.toLatin1();
       
    95     const char *textPtr = ba.data();
       
    96     QString text = qtTrId(textPtr);  // translate
       
    97     XQSERVICE_DEBUG_PRINT("Translated text %s", qPrintable(text));
       
    98 
       
    99     QAction *action=0;
       
   100     QIcon icon;
       
   101     QString iconFile = mDescr.customProperty(XQCUSTOM_PROP_AIW_ICON);
       
   102     if (!iconFile.isEmpty())
       
   103     {
       
   104         XQSERVICE_DEBUG_PRINT("QAction with icon and text");
       
   105         icon.addFile(iconFile);
       
   106         action = new QAction(icon, text, 0);
       
   107     }
       
   108     else
       
   109     {
       
   110         XQSERVICE_DEBUG_PRINT("QAction with text");
       
   111         action = new QAction(text,0);
       
   112     }
       
   113 
       
   114     return action;
       
   115 }
       
   116 
       
   117 void XQAiwServiceDriver::setArguments(const QList<QVariant> &arguments)
       
   118 {
       
   119     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setArguments");
       
   120     currentRequest->setArguments(arguments);
       
   121 }
       
   122 
       
   123 
       
   124 bool XQAiwServiceDriver::send(QVariant& retValue)
       
   125 {
       
   126     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::send>>>");
       
   127 
       
   128     // Update info  (the ones given by explicit method calls count)
       
   129     XQRequestInfo opt = info();
       
   130     opt.setEmbedded(mEmbedded);
       
   131     opt.setBackground(mBackground);
       
   132     currentRequest->setInfo(opt);
       
   133     
       
   134     QStringList list;
       
   135     bool res = true;
       
   136     if (!currentRequest->isSynchronous() && !asyncErrorSet)
       
   137     {
       
   138         // Async request 
       
   139         XQSERVICE_DEBUG_PRINT("request::async send");
       
   140         connect(currentRequest, SIGNAL(requestCompleted(const QVariant&)), this, SLOT(handleAsyncResponse(const QVariant&)));
       
   141         connect(currentRequest, SIGNAL(requestError(int)), this, SLOT(handleAsyncError(int)));
       
   142         asyncErrorSet = true;
       
   143     }
       
   144 
       
   145     
       
   146     XQSERVICE_DEBUG_PRINT("request::send>>>");
       
   147     res = currentRequest->send(retValue);  // Result is valid for sync request only
       
   148     XQSERVICE_DEBUG_PRINT("request::send: %d<<<", res);
       
   149     
       
   150     mErrorCode = XQRequestUtil::mapError(currentRequest->latestError());
       
   151     if (res && !mErrorCode)
       
   152     {
       
   153         mErrorMsg = "";
       
   154     }
       
   155     else
       
   156     {
       
   157         // This is for debugging/trace purposes only,  no need to localize
       
   158         mErrorMsg = XQAiwUtils::createErrorMessage(mErrorCode, "XQAiwServiceDriver", "sync send");
       
   159     }
       
   160     
       
   161     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::send: %d<<<", res);
       
   162     
       
   163     return res;
       
   164 
       
   165 }
       
   166 
       
   167 const QString& XQAiwServiceDriver::lastErrorMessage() const
       
   168 {
       
   169     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::lastErrorMessage: %s", qPrintable(mErrorMsg));
       
   170     return mErrorMsg;
       
   171 }
       
   172 
       
   173 int XQAiwServiceDriver::lastError() const
       
   174 {
       
   175     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::lastError %d", mErrorCode);
       
   176     return mErrorCode;
       
   177 }
       
   178 
       
   179 const XQAiwInterfaceDescriptor &XQAiwServiceDriver::descriptor() const 
       
   180 {
       
   181     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::descriptor");
       
   182     return mDescr; 
       
   183 }
       
   184 
       
   185 void XQAiwServiceDriver::setEmbedded(bool embedded)
       
   186 {
       
   187     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setEmbedded");
       
   188     // Embedded is option
       
   189     mEmbedded = embedded;
       
   190 }
       
   191 bool XQAiwServiceDriver::isEmbedded() const
       
   192 {
       
   193     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::isEmbedded");
       
   194     // Embedded is option
       
   195     return mEmbedded;
       
   196 }
       
   197 
       
   198 
       
   199 void XQAiwServiceDriver::setBackground(bool background )
       
   200 {
       
   201     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setBackground");
       
   202     mBackground = background;
       
   203 }
       
   204 bool XQAiwServiceDriver::isBackground() const
       
   205 {
       
   206     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::isBackground");
       
   207     return mBackground;
       
   208 }
       
   209 
       
   210 
       
   211 void XQAiwServiceDriver::setOperation(const QString &operation)
       
   212 {
       
   213     XQSERVICE_DEBUG_PRINT("XQAiwRequest::setOperation");
       
   214     mOperation = operation;
       
   215     currentRequest->setMessage(operation);
       
   216 }
       
   217 const QString &XQAiwServiceDriver::operation() const
       
   218 {
       
   219     XQSERVICE_DEBUG_PRINT("XQAiwRequest::operation");
       
   220     return mOperation;
       
   221 }
       
   222 
       
   223 void XQAiwServiceDriver::setSynchronous(bool synchronous)
       
   224 {
       
   225     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setSynchronous");
       
   226     currentRequest->setSynchronous(synchronous);
       
   227 }
       
   228 bool XQAiwServiceDriver::isSynchronous() const
       
   229 {
       
   230     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::isSynchronous");
       
   231     
       
   232     return currentRequest->isSynchronous();
       
   233 }
       
   234 
       
   235 void XQAiwServiceDriver::setInfo(const XQRequestInfo &info)
       
   236 {
       
   237     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::setInfo");
       
   238     currentRequest->setInfo(info);
       
   239 }
       
   240 
       
   241 XQRequestInfo XQAiwServiceDriver::info() const
       
   242 {
       
   243     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::info");
       
   244     return currentRequest->info();
       
   245 }
       
   246 
       
   247 void XQAiwServiceDriver::handleAsyncResponse(const QVariant& value)
       
   248 {
       
   249     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::handleAsyncResponse");
       
   250     emit requestOk(value);
       
   251 }
       
   252 
       
   253 void XQAiwServiceDriver::handleAsyncError(int err)
       
   254 {
       
   255     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::handleAsyncError");
       
   256 
       
   257    mErrorCode = XQRequestUtil::mapError(err);
       
   258    mErrorMsg = XQAiwUtils::createErrorMessage(mErrorCode, "XQAiwServiceDriver", "async send");
       
   259    emit requestError(mErrorCode, mErrorMsg);
       
   260 
       
   261 }
       
   262 
       
   263 
       
   264 bool XQAiwServiceDriver::installTranslator()
       
   265 {
       
   266     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::installTranslator");
       
   267 
       
   268     if (!qApp)
       
   269     {
       
   270         XQSERVICE_DEBUG_PRINT("\tNo application context");
       
   271         return false;
       
   272     }
       
   273 
       
   274     // The the name of the resource file containing the text
       
   275     QString textFile = mDescr.customProperty(XQCUSTOM_PROP_AIW_TEXT_FILE);
       
   276     if (textFile.isEmpty())
       
   277     {
       
   278         XQSERVICE_DEBUG_PRINT("\tCustom property missing for text file");
       
   279         return false;
       
   280     }
       
   281     
       
   282     // Check if locale has changed since last request
       
   283     QString lang = QLocale::system().name();
       
   284     XQSERVICE_DEBUG_PRINT("\tLanguage now is%s", qPrintable(lang));
       
   285     if (!this->lastLang.isEmpty() && (this->lastLang != lang))
       
   286     { 
       
   287         // Language has changed since last time
       
   288         // Remove previous translator, if any
       
   289         removeTranslator();
       
   290     }
       
   291 
       
   292     textFile = makeFileName(textFile);
       
   293     
       
   294     bool res=false;
       
   295     if (this->translator.isEmpty())
       
   296     {
       
   297         // Construct the full name of the localized resource
       
   298         XQSERVICE_DEBUG_PRINT("textFile name is %s", qPrintable(textFile));
       
   299         res = this->translator.load(textFile);
       
   300         if (res)
       
   301         {
       
   302             qApp->installTranslator(&this->translator);
       
   303             this->lastLang = lang;  // Remember the current language
       
   304             XQSERVICE_DEBUG_PRINT("Translator installed %s", qPrintable(lang));
       
   305         }
       
   306     }
       
   307     
       
   308     return res;
       
   309     
       
   310 }
       
   311 
       
   312 void XQAiwServiceDriver::removeTranslator()
       
   313 {
       
   314     if (qApp && !this->translator.isEmpty())
       
   315     {
       
   316         qApp->removeTranslator(&this->translator);
       
   317     }
       
   318 }
       
   319 
       
   320 QString XQAiwServiceDriver::makeFileName(const QString &textFile) const
       
   321 {
       
   322     QFileInfo info(textFile);
       
   323     QString ret = textFile;
       
   324     
       
   325     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::makeFileName %s", qPrintable(textFile));
       
   326     
       
   327     if (info.baseName() == info.filePath())
       
   328     {
       
   329         // No path, apply the default path
       
   330         // Drive is where app is loaded, like "C:" or "Z:"
       
   331         ret = qApp->applicationFilePath().left(2) + "/resource/qt/translations/" + textFile;
       
   332     }
       
   333 
       
   334     // Add current language
       
   335     QString lang = QLocale::system().name();
       
   336     ret += "_"; 
       
   337     ret += lang;
       
   338     
       
   339     XQSERVICE_DEBUG_PRINT("XQAiwServiceDriver::makeFileName ret=%s", qPrintable(ret));
       
   340 
       
   341     return ret;
       
   342     
       
   343 }