qtinternetradio/irhswidgetplugin/src/irserviceclient.cpp
branchGCC_SURGE
changeset 13 c9471d26c7f2
parent 12 608f67c22514
child 15 065198191975
equal deleted inserted replaced
9:bfc95e24a059 13:c9471d26c7f2
       
     1 /*
       
     2 * Copyright (c) 2009 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: internet radio hs widget service client
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <QSettings>
       
    20 #include <QVariant>
       
    21 #include <QString>
       
    22 #include <hbicon.h>
       
    23 #include <xqserviceglobal.h>
       
    24 #include <xqservicerequest.h>
       
    25 
       
    26 
       
    27 // User includes
       
    28 #include "irserviceclient.h"
       
    29 #include "irservicedata.h"
       
    30 
       
    31 // Constants
       
    32 // For services via Qt HighWay
       
    33 static const QString KIrServiceName = "internet_radio_10_1";
       
    34 static const QString KIrServiceControlInterfaceName = "com.nokia.symbian.IInternetRadioControl";
       
    35 static const QString KIrServiceControlOperation     = "handleCmd(int)";
       
    36 static const QString KIrServiceMonitorInterfaceName = "com.nokia.symbian.IInternetRadioMonitor";
       
    37 static const QString KIrServiceMonitorOperation     = "registerNotifications()";
       
    38 static const QString KIrServiceRefreshOperation     = "refreshAllData()";
       
    39 
       
    40 
       
    41 // Initialize the static member variable
       
    42 QMutex IrServiceClient::mMutex;
       
    43 int IrServiceClient::mRef = 0;
       
    44 bool IrServiceClient::mMonitoringStarted = false;    
       
    45 IrServiceClient *IrServiceClient::mInstatnce = NULL;
       
    46 
       
    47 
       
    48 // ==================== MEMBER FUNCTIONS ======================
       
    49 // Static function to get a singleton instance of IrServiceClient
       
    50 IrServiceClient* IrServiceClient::openInstance()
       
    51 {
       
    52     mMutex.lock();
       
    53     if (NULL == mInstatnce)
       
    54     {
       
    55         mInstatnce = new IrServiceClient();
       
    56     }       
       
    57     if (mInstatnce != NULL)
       
    58     {
       
    59         mRef++;
       
    60     }
       
    61     mMutex.unlock();
       
    62     return mInstatnce;
       
    63 }
       
    64 
       
    65 // Close a singleton instance of IrServiceClient
       
    66 void IrServiceClient::closeInstance()
       
    67 {
       
    68     if (mInstatnce != NULL)
       
    69     {
       
    70         mMutex.lock();
       
    71         if (mRef > 0)
       
    72         {
       
    73             mRef--;
       
    74         }
       
    75         
       
    76         if (0 == mRef)
       
    77         {
       
    78             delete mInstatnce;
       
    79         }
       
    80         mMutex.unlock();
       
    81     }
       
    82 }
       
    83 
       
    84 // Constructor
       
    85 IrServiceClient::IrServiceClient() :
       
    86     mIrAppInspector(NULL),
       
    87     mMonitorReqOngoing(false),    
       
    88     mControlReqOngoing(false),
       
    89     mMonitorRequest(NULL),
       
    90     mRefreshRequest(NULL),
       
    91     mControlRequest(NULL)
       
    92 {
       
    93     mIrAppInspector = new IrAppInspector(this);
       
    94     
       
    95     QObject::connect(mIrAppInspector, SIGNAL(irRunningStatusChanged(IrAppInspector::IrRunningStatus)),
       
    96         this, SLOT(handleIrRunningStatusChanged(IrAppInspector::IrRunningStatus)));
       
    97 }
       
    98 
       
    99 // Destructor
       
   100 IrServiceClient::~IrServiceClient()
       
   101 {   
       
   102     delete mMonitorRequest;
       
   103     mMonitorRequest = NULL;
       
   104     
       
   105     delete mRefreshRequest;
       
   106     mRefreshRequest = NULL;
       
   107     
       
   108     delete mControlRequest;
       
   109     mControlRequest = NULL;
       
   110 }
       
   111 
       
   112 bool IrServiceClient::startMonitoringIrState()
       
   113 {
       
   114     if (mMonitoringStarted)
       
   115     {
       
   116         refreshAllActiveHsWidgets();
       
   117         return true;
       
   118     }
       
   119        
       
   120     // if this is the first active hs widget
       
   121     mMonitoringStarted = true;
       
   122     bool retVal = false;
       
   123     if (mIrAppInspector->isIrRunning())
       
   124     {
       
   125         retVal = doSendRefreshRequest();
       
   126     }
       
   127     else
       
   128     {
       
   129         if (isStationPlayed())
       
   130         {
       
   131             initHsWidgetNoRunStopped();
       
   132         }
       
   133         else
       
   134         {
       
   135             notifyIrStateChanged(IrAppState::NoRunInit);
       
   136         }
       
   137     }
       
   138     
       
   139     return mIrAppInspector->startInspectingIrRunningStatus() && retVal;    
       
   140 }
       
   141 
       
   142 
       
   143 void IrServiceClient::initHsWidgetNoRunStopped()
       
   144 {    
       
   145     QString stationName;
       
   146     if (loadStationName(stationName))
       
   147     {
       
   148         notifyStationNameUpdated(stationName);
       
   149     }
       
   150     else
       
   151     {
       
   152 #ifdef SUBTITLE_STR_BY_LOCID
       
   153         notifyStationNameUpdated(hbTrId("txt_irad_info_unnamed_station"));
       
   154 #else
       
   155         notifyStationNameUpdated(hbTrId("Unnamed station"));
       
   156 #endif
       
   157     }
       
   158     
       
   159     notifyStationLogoUpdated(loadStationLogoFlag());
       
   160     
       
   161     notifyIrStateChanged(IrAppState::NoRunStopped);    
       
   162 }
       
   163 
       
   164 void IrServiceClient::refreshAllActiveHsWidgets()
       
   165 {
       
   166     emit stationNameUpdated(mStationName);
       
   167     emit stationLogoUpdated(mStationLogoAvailable);
       
   168     emit metaDataUpdated(mMetaData);
       
   169     emit irStateChanged(mIrState);
       
   170 }
       
   171 
       
   172 /******************************************************************
       
   173 ****************   Monitor Service Request  **********************
       
   174 *******************************************************************/
       
   175 bool IrServiceClient::doSendMonitorRequest()
       
   176 {
       
   177     if(mMonitorReqOngoing)
       
   178     {
       
   179         return false;
       
   180     }
       
   181     
       
   182     if (NULL == mMonitorRequest
       
   183         &&!createMonitorServiceRequest())
       
   184     {
       
   185         return false;
       
   186     }
       
   187 
       
   188     prepareRequestInfo(mMonitorRequest, DoNotChange);
       
   189     return mMonitorRequest->send();
       
   190 }
       
   191 
       
   192 bool IrServiceClient::createMonitorServiceRequest()
       
   193 {
       
   194     bool retVal = false;
       
   195 
       
   196     if (NULL == mMonitorRequest)
       
   197     {
       
   198         mMonitorRequest = mApplicationManager.create(KIrServiceName,
       
   199                 KIrServiceMonitorInterfaceName,
       
   200                 KIrServiceMonitorOperation,
       
   201                 false);
       
   202 
       
   203         if (mMonitorRequest)
       
   204         {
       
   205             retVal = true;
       
   206 
       
   207             connect(mMonitorRequest, SIGNAL(requestOk(const QVariant&)),
       
   208                 this, SLOT(handleMonitorRequestOk(const QVariant&)));
       
   209             connect(mMonitorRequest, SIGNAL(requestError(int,const QString&)),
       
   210                 this, SLOT(handleMonitorRequestError(int,const QString&)));
       
   211 
       
   212             mMonitorRequest->setEmbedded(false);
       
   213             mMonitorRequest->setSynchronous(false);
       
   214         }
       
   215     }
       
   216     else
       
   217     {
       
   218         retVal = true;
       
   219     }
       
   220 
       
   221     return retVal;
       
   222 }
       
   223 
       
   224 // ==================== Refresh Service Request ===========================
       
   225 bool IrServiceClient::doSendRefreshRequest()
       
   226 {
       
   227     if(mMonitorReqOngoing)
       
   228     {
       
   229         return false;
       
   230     }
       
   231     
       
   232     if (NULL == mRefreshRequest
       
   233         && !createRefreshServiceRequest())
       
   234     {
       
   235         return false;
       
   236     }
       
   237        
       
   238     prepareRequestInfo(mRefreshRequest, DoNotChange);        
       
   239     return mRefreshRequest->send();
       
   240 }
       
   241 
       
   242 bool IrServiceClient::createRefreshServiceRequest()
       
   243 {
       
   244     bool retVal = false;
       
   245 
       
   246     if (NULL == mRefreshRequest)
       
   247     {
       
   248         mRefreshRequest = mApplicationManager.create(KIrServiceName,
       
   249                 KIrServiceMonitorInterfaceName,
       
   250                 KIrServiceRefreshOperation,
       
   251                 false);
       
   252 
       
   253         if (mRefreshRequest)
       
   254         {
       
   255             retVal = true;
       
   256 
       
   257             connect(mRefreshRequest, SIGNAL(requestOk(const QVariant&)),
       
   258                 this, SLOT(handleMonitorRequestOk(const QVariant&)));
       
   259             connect(mRefreshRequest, SIGNAL(requestError(int,const QString&)),
       
   260                 this, SLOT(handleMonitorRequestError(int,const QString&)));
       
   261             
       
   262             mRefreshRequest->setEmbedded(false);
       
   263             mRefreshRequest->setSynchronous(false);
       
   264         }
       
   265     }
       
   266     else
       
   267     {
       
   268         retVal = true;
       
   269     }
       
   270 
       
   271     return retVal;
       
   272 }
       
   273 
       
   274 void IrServiceClient::handleMonitorRequestOk(const QVariant &aRetValue)
       
   275 {
       
   276     mMonitorReqOngoing = false;
       
   277     
       
   278     doSendMonitorRequest();
       
   279 
       
   280     if (aRetValue.isValid()
       
   281          && aRetValue.canConvert<IrServiceDataList>())
       
   282     {
       
   283         IrServiceDataList dataList;
       
   284         dataList = qVariantValue<IrServiceDataList>(aRetValue);
       
   285         foreach (const IrServiceData& data, dataList)
       
   286         {
       
   287             processNotificationData(data);
       
   288         }
       
   289     }
       
   290 }
       
   291 
       
   292 void IrServiceClient::handleMonitorRequestError(int aErrCode, const QString &aErrMessage)
       
   293 {
       
   294     mMonitorReqOngoing = false;
       
   295     
       
   296     Q_UNUSED(aErrCode);
       
   297     Q_UNUSED(aErrMessage);
       
   298 }
       
   299 
       
   300 /******************************************************************
       
   301 ****************   Control Service Request  **********************
       
   302 *******************************************************************/
       
   303 bool IrServiceClient::bringIrForeground()
       
   304 {
       
   305     QVariant commandArgument(QVariant::Int);
       
   306     commandArgument.setValue((int)IrServiceCmd::Foreground);
       
   307     return doSendControlRequest(commandArgument, true, ToForeground);
       
   308 }
       
   309 
       
   310 bool IrServiceClient::launchIrNormally()
       
   311 {
       
   312     QVariant commandArgument(QVariant::Int);
       
   313     commandArgument.setValue((int)IrServiceCmd::LaunchNormally);
       
   314     return doSendControlRequest(commandArgument, true, ToForeground);
       
   315 }
       
   316 
       
   317 bool IrServiceClient::launchIrNowPlaying()
       
   318 {
       
   319     QVariant commandArgument(QVariant::Int);
       
   320     commandArgument.setValue((int)IrServiceCmd::LaunchNowPlayingView);
       
   321     return doSendControlRequest(commandArgument, false, ToBackground);
       
   322 }
       
   323 
       
   324 
       
   325 bool IrServiceClient::startPlaying()
       
   326 {
       
   327     QVariant commandArgument(QVariant::Int);
       
   328     commandArgument.setValue((int)IrServiceCmd::Play);
       
   329     return doSendControlRequest(commandArgument, true, DoNotChange);
       
   330 }
       
   331 
       
   332 
       
   333 bool IrServiceClient::stopPlaying()
       
   334 {
       
   335     QVariant commandArgument(QVariant::Int);
       
   336     commandArgument.setValue((int)IrServiceCmd::Stop);
       
   337     return doSendControlRequest(commandArgument, true, DoNotChange);
       
   338 }
       
   339 
       
   340 bool IrServiceClient::cancelLoading()
       
   341 {
       
   342     QVariant commandArgument(QVariant::Int);
       
   343     commandArgument.setValue((int)IrServiceCmd::Cancel);
       
   344     return doSendControlRequest(commandArgument, true, DoNotChange);
       
   345 }
       
   346 
       
   347 bool IrServiceClient::doSendControlRequest(const QVariant &aArgument, bool aIsSync, IrAppVisibilty aVisibility)
       
   348 {
       
   349     if(mControlReqOngoing)
       
   350     {
       
   351         return false;
       
   352     }
       
   353     
       
   354     if (NULL == mControlRequest &&
       
   355         !createControlServiceRequest())
       
   356     {
       
   357         return false;
       
   358     }
       
   359 
       
   360     QList<QVariant> arguments;
       
   361     arguments.append(aArgument);
       
   362     mControlRequest->setArguments(arguments);
       
   363 
       
   364     prepareRequestInfo(mControlRequest, aVisibility);
       
   365     mControlRequest->setSynchronous(aIsSync);    
       
   366     return mControlRequest->send();
       
   367 }
       
   368 
       
   369 bool IrServiceClient::createControlServiceRequest()
       
   370 {
       
   371     bool retVal = false;
       
   372 
       
   373     if (NULL == mControlRequest)
       
   374     {
       
   375         mControlRequest = mApplicationManager.create(KIrServiceName,
       
   376                 KIrServiceControlInterfaceName,
       
   377                 KIrServiceControlOperation,
       
   378                 false);
       
   379 
       
   380         if (mControlRequest)
       
   381         {
       
   382             retVal = true;
       
   383             
       
   384             connect(mControlRequest, SIGNAL(requestOk(const QVariant&)),
       
   385                 this, SLOT(handleControlRequestOk(const QVariant&)));
       
   386             connect(mControlRequest, SIGNAL(requestError(int,const QString&)),
       
   387                 this, SLOT(handleControlRequestError(int,const QString&)));
       
   388             
       
   389             mControlRequest->setEmbedded(false);
       
   390         }
       
   391     }
       
   392     else
       
   393     {
       
   394         retVal = true;
       
   395     }
       
   396 
       
   397     return retVal;
       
   398 }
       
   399 
       
   400 void IrServiceClient::handleControlRequestOk(const QVariant &aRetValue)
       
   401 {
       
   402     mControlReqOngoing = false;    
       
   403     
       
   404     if (aRetValue.isValid()
       
   405          && aRetValue.canConvert(QVariant::Int))
       
   406     {
       
   407         if (IrServiceResult::Fail == aRetValue.toInt())
       
   408         {
       
   409             emit controlFailed();
       
   410         }
       
   411     }    
       
   412 }
       
   413 
       
   414 void IrServiceClient::handleControlRequestError(int aErrCode, const QString &aErrMessage)
       
   415 {
       
   416     mControlReqOngoing = false;
       
   417     
       
   418     emit controlFailed();
       
   419     
       
   420     Q_UNUSED(aErrCode);
       
   421     Q_UNUSED(aErrMessage);    
       
   422 }
       
   423 /******************************************************************
       
   424 *************************   Others   ******************************
       
   425 *******************************************************************/
       
   426 // slot connected to signals from IrAppInspector,
       
   427 // used to monitor the startup / exit of IR
       
   428 void IrServiceClient::handleIrRunningStatusChanged(IrAppInspector::IrRunningStatus aNewStatus)
       
   429 {
       
   430     switch (aNewStatus)
       
   431     {
       
   432         case IrAppInspector::StartingUp :
       
   433             doSendRefreshRequest();
       
   434             break;
       
   435             
       
   436         case IrAppInspector::Exiting :
       
   437         {
       
   438             clearMonitorServiceRequest();
       
   439             QString stationName;
       
   440             if (loadStationName(stationName))
       
   441             {
       
   442                 notifyMetaDataUpdated(QString(""));
       
   443                 notifyStationNameUpdated(stationName);        
       
   444                 notifyStationLogoUpdated(loadStationLogoFlag());
       
   445                 notifyIrStateChanged(IrAppState::NoRunStopped);
       
   446             }
       
   447             else
       
   448             {
       
   449                 notifyIrStateChanged(IrAppState::NoRunInit);
       
   450             }            
       
   451             break;
       
   452         }
       
   453             
       
   454         default:
       
   455             break;
       
   456     }
       
   457 }
       
   458 
       
   459 void IrServiceClient::clearMonitorServiceRequest()
       
   460 {
       
   461     delete mMonitorRequest;
       
   462     mMonitorRequest = NULL;
       
   463 
       
   464     delete mRefreshRequest;
       
   465     mRefreshRequest = NULL;
       
   466 }
       
   467 
       
   468 // used to process service data sent from IR app.
       
   469 void IrServiceClient::processNotificationData(const IrServiceData &aServiceData)
       
   470 {
       
   471     QVariant data = aServiceData.mData;
       
   472     if (!data.isValid())
       
   473     {
       
   474         return;
       
   475     }
       
   476     switch (aServiceData.mType)
       
   477     {
       
   478         case IrServiceNotification::StationName:
       
   479             if (data.canConvert(QVariant::String))
       
   480             {
       
   481                 notifyStationNameUpdated(data.toString());
       
   482             }
       
   483             break;
       
   484 
       
   485         case IrServiceNotification::MetaData:
       
   486             if (data.canConvert(QVariant::String))
       
   487             {
       
   488                 notifyMetaDataUpdated(data.toString());
       
   489             }
       
   490             break;
       
   491 
       
   492         case IrServiceNotification::StationLogo:
       
   493             if (data.canConvert(QVariant::Bool))
       
   494             {
       
   495                 notifyStationLogoUpdated(data.toBool());
       
   496             }            
       
   497             break;
       
   498 
       
   499         case IrServiceNotification::IrState:
       
   500             if (data.canConvert(QVariant::Int))
       
   501             {
       
   502                 notifyIrStateChanged((IrAppState::Type)data.toInt());
       
   503             }
       
   504             break;
       
   505 
       
   506         default:
       
   507             break;
       
   508     }
       
   509 
       
   510 }
       
   511 
       
   512 // prepare the request send to IR app, including control request, refresh request, monitor request.
       
   513 void IrServiceClient::prepareRequestInfo(XQAiwRequest *aRequest, IrAppVisibilty aVisibility)
       
   514 {
       
   515     if (NULL == aRequest)
       
   516     {
       
   517         return;
       
   518     } 
       
   519         
       
   520     XQRequestInfo reqInfo;
       
   521     switch (aVisibility)
       
   522     {
       
   523         case ToForeground:
       
   524             reqInfo.setForeground(true);
       
   525             break;
       
   526         case ToBackground:
       
   527             reqInfo.setBackground(true);
       
   528             break;
       
   529         case DoNotChange:
       
   530             break;
       
   531         default:
       
   532             break;
       
   533     }
       
   534 
       
   535     aRequest->setInfo(reqInfo);
       
   536 }
       
   537 
       
   538 void IrServiceClient::notifyStationNameUpdated(const QString &aStationName)
       
   539 {
       
   540     mStationName = aStationName;
       
   541     emit stationNameUpdated(mStationName);
       
   542 }
       
   543 
       
   544 void IrServiceClient::notifyStationLogoUpdated(bool aIsLogoAvailable)
       
   545 {
       
   546     mStationLogoAvailable = aIsLogoAvailable;
       
   547     emit stationLogoUpdated(mStationLogoAvailable);    
       
   548 }
       
   549 
       
   550 void IrServiceClient::notifyMetaDataUpdated(const QString &aMetaData)
       
   551 {
       
   552     mMetaData = aMetaData;
       
   553     emit metaDataUpdated(mMetaData);    
       
   554 }
       
   555 
       
   556 void IrServiceClient::notifyIrStateChanged(IrAppState::Type aNewState)
       
   557 {
       
   558     mIrState = aNewState;
       
   559     emit irStateChanged(mIrState);    
       
   560 }       
       
   561         
       
   562 bool IrServiceClient::isStationPlayed()
       
   563 {
       
   564     QSettings settings(KIrSettingOrganization, KIrSettingApplication);
       
   565     QString stationName = settings.value(KIrSettingStationName,KIrDefaultStationName).toString();
       
   566     return stationName != KIrDefaultStationName;
       
   567 }
       
   568 
       
   569 bool IrServiceClient::loadStationLogoFlag()
       
   570 {
       
   571     QSettings settings(KIrSettingOrganization, KIrSettingApplication);
       
   572     return settings.value(KIrSettingStationLogoAvailable,false).toBool();
       
   573 }
       
   574 
       
   575 bool IrServiceClient::loadStationName(QString &aStationName)
       
   576 {
       
   577     QSettings settings(KIrSettingOrganization, KIrSettingApplication);
       
   578     aStationName = settings.value(KIrSettingStationName,KIrDefaultStationName).toString();
       
   579     if(aStationName != KIrDefaultStationName)
       
   580     {
       
   581         return true;
       
   582     }
       
   583     else
       
   584     {
       
   585         return false;
       
   586     }
       
   587 }
       
   588 
       
   589 Q_IMPLEMENT_USER_METATYPE(IrServiceData)
       
   590 Q_IMPLEMENT_USER_METATYPE_NO_OPERATORS(IrServiceDataList)
       
   591