qthighway/xqservice/src/xqaiwuridriver.cpp
changeset 1 2b40d63a9c3d
child 11 06b8e2af4411
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 #include <QObject>
       
    23 #include <qglobal.h>
       
    24 #include <QDesktopServices>
       
    25 #include <QLatin1String>
       
    26 
       
    27 #include <xqaiwdecl.h>
       
    28 #include "xqserviceglobal.h"
       
    29 #include "xqservicelog.h"
       
    30 #include "xqaiwuridriver.h"
       
    31 #include <qservicemanager.h>  // Integrate to activity manager
       
    32 
       
    33 using namespace QtMobility;
       
    34 
       
    35 XQAiwUriDriver::XQAiwUriDriver(const QUrl &uri, const XQAiwInterfaceDescriptor& descriptor, const QString &operation)
       
    36     : XQAiwRequestDriver(),
       
    37        mUtilities(NULL)
       
    38 {
       
    39 
       
    40     mErrorMsg = "";
       
    41     mDescr = descriptor;
       
    42     mUri = uri;
       
    43     mOperation = operation;
       
    44 
       
    45     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::XQAiwUriDriver: %s",
       
    46                           qPrintable(mUri.toString()));
       
    47     
       
    48 }
       
    49 
       
    50 XQAiwUriDriver::~XQAiwUriDriver()
       
    51 {
       
    52     XQSERVICE_DEBUG_PRINT("~XQAiwUriDriver::XQAiwUriDriver");
       
    53     delete mUtilities;
       
    54     
       
    55 }
       
    56 
       
    57 QAction *XQAiwUriDriver::createAction()
       
    58 {
       
    59     return NULL;  //  Not supported
       
    60 }
       
    61 
       
    62 void XQAiwUriDriver::setArguments(const QList<QVariant> &arguments)
       
    63 {
       
    64     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::setArguments");
       
    65     mArguments =  arguments;
       
    66 }
       
    67 
       
    68 
       
    69 bool XQAiwUriDriver::send(QVariant& retValue)
       
    70 {
       
    71     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::send %s", qPrintable(mUri.toString()));
       
    72 
       
    73     mErrorCode = XQService::ENoError;
       
    74     
       
    75     // Browser URI
       
    76     if (mUri.scheme().compare("http", Qt::CaseInsensitive) == 0 ||
       
    77         mUri.scheme().compare("https", Qt::CaseInsensitive) == 0)
       
    78     {
       
    79         XQSERVICE_DEBUG_PRINT("Apply QDesktopServices::openUrl");
       
    80         bool b = QDesktopServices::openUrl(mUri);
       
    81         if (!b)
       
    82         {
       
    83             mErrorCode = XQService::EConnectionError;
       
    84             mErrorMsg = XQAiwUtils::createErrorMessage(mErrorCode, "QDesktopServices::openUrl", mUri.toString());
       
    85         }
       
    86         return b;
       
    87     }
       
    88 
       
    89     // "appto" URI
       
    90     if (mUri.scheme().compare(XQURI_SCHEME_ACTIVITY,Qt::CaseInsensitive) == 0)
       
    91     {
       
    92         return handleApptoUri(mUri);
       
    93     }
       
    94 
       
    95     //
       
    96     // The rest
       
    97     //
       
    98     if (mUtilities == NULL)
       
    99         mUtilities = new XQAiwUtils();
       
   100     if (mUtilities == NULL)
       
   101     {
       
   102         XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::Can not create aiw utils");
       
   103         return false;
       
   104     }
       
   105 
       
   106     int applicationId = 0;
       
   107     if (mUtilities->findApplication(mUri, applicationId) != XQService::ENoError)
       
   108     {
       
   109         // Not found
       
   110         mErrorCode = XQService::EServerNotFound;
       
   111         mErrorMsg = XQAiwUtils::createErrorMessage(mErrorCode, "XQAiwUriDriver", mUri.toString());
       
   112         return false;  // No application registered for uri
       
   113     }
       
   114     XQSERVICE_DEBUG_PRINT("Application id %x", applicationId);
       
   115     
       
   116     // Create space separated command line args
       
   117     QString args = mUtilities->createCmdlineArgs(mArguments);
       
   118     XQSERVICE_DEBUG_PRINT("args %s", qPrintable(args));
       
   119     
       
   120     mErrorCode = mUtilities->launchApplication(applicationId, args);
       
   121     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::errorCode %d", mErrorCode);
       
   122     
       
   123     QVariant ret(!mErrorCode);
       
   124     retValue = ret;
       
   125     
       
   126     return (!mErrorCode);
       
   127 }
       
   128 
       
   129 const QString& XQAiwUriDriver::lastErrorMessage() const
       
   130 {
       
   131     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::lastErrorMessage: %s", qPrintable(mErrorMsg));
       
   132     return mErrorMsg;
       
   133 }
       
   134 
       
   135 int XQAiwUriDriver::lastError() const
       
   136 {
       
   137     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::lastError %d", mErrorCode);
       
   138     return mErrorCode;
       
   139 }
       
   140 
       
   141 const XQAiwInterfaceDescriptor &XQAiwUriDriver::descriptor() const 
       
   142 {
       
   143     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::descriptor");
       
   144     return mDescr; 
       
   145 }
       
   146 
       
   147 void XQAiwUriDriver::setEmbedded(bool embedded)
       
   148 {
       
   149     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::setEmbedded");
       
   150     mEmbedded = embedded;
       
   151 }
       
   152 bool XQAiwUriDriver::isEmbedded() const
       
   153 {
       
   154     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::isEmbedded");
       
   155     return mEmbedded;
       
   156 }
       
   157 
       
   158 void XQAiwUriDriver::setOperation(const QString &operation)
       
   159 {
       
   160     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::setOperation");
       
   161     mOperation = operation;
       
   162 }
       
   163 const QString &XQAiwUriDriver::operation() const
       
   164 {
       
   165     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::operation");
       
   166     return mOperation;
       
   167 }
       
   168 
       
   169 void XQAiwUriDriver::setSynchronous(bool synchronous)
       
   170 {
       
   171     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::setSynchronous");
       
   172     mSynchronous = synchronous;
       
   173 }
       
   174 bool XQAiwUriDriver::isSynchronous() const
       
   175 {
       
   176     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::isSynchronous");
       
   177     return mSynchronous;
       
   178 }
       
   179 
       
   180 //
       
   181 // Check if we can handle the URI
       
   182 //
       
   183 bool XQAiwUriDriver::hasCustomHandler(const QUrl &uri)
       
   184 {
       
   185     QString scheme = uri.scheme();
       
   186     return  (scheme.compare(XQURI_SCHEME_ACTIVITY,Qt::CaseInsensitive) == 0 ||
       
   187             scheme.compare(XQURI_SCHEME_HTTP,Qt::CaseInsensitive) == 0 ||
       
   188             scheme.compare(XQURI_SCHEME_HTTPS,Qt::CaseInsensitive) == 0 || 
       
   189             scheme.compare(QLatin1String(XQURI_SCHEME_FILE),Qt::CaseInsensitive) == 0);
       
   190 }
       
   191 
       
   192 
       
   193 void XQAiwUriDriver::handleAsyncResponse(const QVariant& value)
       
   194 {
       
   195     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::handleAsyncResponse");
       
   196     emit requestOk(value);
       
   197 }
       
   198 
       
   199 void XQAiwUriDriver::handleAsyncError(int err)
       
   200 {
       
   201     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::handleAsyncError");
       
   202 
       
   203    mErrorCode = err;
       
   204    mErrorMsg = XQAiwUtils::createErrorMessage(mErrorCode,"XQAiwUriDriver", "async send");
       
   205    emit requestError(mErrorCode, mErrorMsg);
       
   206 }
       
   207 
       
   208 void XQAiwUriDriver::setBackground(bool background )
       
   209 {
       
   210     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::setBackground");
       
   211     mBackground = background;
       
   212 }
       
   213 bool XQAiwUriDriver::isBackground() const
       
   214 {
       
   215     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::isBackground");
       
   216     return mBackground;
       
   217 }
       
   218 
       
   219 bool XQAiwUriDriver::handleApptoUri(const QUrl &uri)
       
   220 {
       
   221     //
       
   222     // Use Qt service framework errors as is (they are in different range than XQ errors)
       
   223     //
       
   224     QLatin1String xml("Z:/resource/activity/activityserviceplugin.xml");
       
   225     mErrorCode = QServiceManager::NoError;
       
   226                 
       
   227     QtMobility::QServiceManager serviceManager;
       
   228     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::handleApptoUri");
       
   229 
       
   230     bool b = serviceManager.addService(xml);
       
   231     mErrorCode = serviceManager.error();
       
   232     XQSERVICE_DEBUG_PRINT("\taddService=%d", mErrorCode);
       
   233     switch (mErrorCode)
       
   234     {
       
   235         case QServiceManager::NoError:
       
   236         case QServiceManager::ServiceAlreadyExists:
       
   237         case QServiceManager::ImplementationAlreadyExists:
       
   238             // These are OK cases.
       
   239             mErrorCode = QServiceManager::NoError;
       
   240             break;
       
   241         default:
       
   242             return false;
       
   243     }
       
   244     
       
   245     QObject* activityManager = serviceManager.loadInterface("com.nokia.qt.activities.ActivityManager");
       
   246     mErrorCode = serviceManager.error();
       
   247     XQSERVICE_DEBUG_PRINT("\tloadInterface %s=%d", "com.nokia.qt.activities.ActivityManager", mErrorCode);
       
   248     if (!activityManager)
       
   249     {
       
   250        mErrorCode = serviceManager.error();
       
   251        return false;
       
   252     }
       
   253     
       
   254     QMetaObject::invokeMethod(activityManager, "launchActivity", Q_ARG(QString, uri.toString()));
       
   255     mErrorCode = serviceManager.error();
       
   256     
       
   257     XQSERVICE_DEBUG_PRINT("\tinvokeMethod=%d", mErrorCode);
       
   258     delete activityManager;
       
   259 
       
   260     XQSERVICE_DEBUG_PRINT("XQAiwUriDriver::handleApptoUri done");
       
   261     
       
   262     return mErrorCode == QServiceManager::NoError;
       
   263 }