hswidgetplugin/fmradiohswidgetplugin/src/fmradiohswidgetradioserviceclient.cpp
changeset 32 189d20c34778
parent 28 075425b8d9a4
child 33 11b6825f0862
equal deleted inserted replaced
28:075425b8d9a4 32:189d20c34778
     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: FM Radio widget radio service client
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <xqserviceglobal.h>
       
    20 
       
    21 // User includes
       
    22 #include "fmradiohswidgetradioserviceclient.h"
       
    23 #include "fmradiohswidget.h"
       
    24 #include "radioservicedef.h"
       
    25 #include "radionotificationdata.h"
       
    26 #include "radiologger.h"
       
    27 
       
    28 /*!
       
    29  Constructor
       
    30  */
       
    31 FmRadioHsWidgetRadioServiceClient::FmRadioHsWidgetRadioServiceClient(QObject *parent) :
       
    32     QObject(parent),
       
    33     mRequestPending(false),
       
    34     mRadioMonitorRequest(NULL),
       
    35     mRadioControlRequest(NULL),
       
    36     mDataInitialized(false)
       
    37 {
       
    38     LOG_METHOD;
       
    39 }
       
    40 
       
    41 /*!
       
    42  Destructor 
       
    43  */
       
    44 FmRadioHsWidgetRadioServiceClient::~FmRadioHsWidgetRadioServiceClient()
       
    45 {
       
    46     LOG_METHOD;
       
    47     stopMonitoring();
       
    48 }
       
    49 
       
    50 /*!
       
    51  Close FM Radio application.
       
    52  
       
    53  */
       
    54 void FmRadioHsWidgetRadioServiceClient::doCloseFmRadio()
       
    55 {
       
    56     LOG_METHOD;
       
    57     QVariant commandArgument;
       
    58     commandArgument.setValue(static_cast<int>(RadioServiceCommand::PowerOff));
       
    59     doSendControlRequest(commandArgument, DoNotChange);
       
    60 }
       
    61 
       
    62 /*!
       
    63  Power on FM Radio application.
       
    64  This is used only when radio is in Closing state. This merely cancels the
       
    65  radio power off delay and unmutes the audio. Currently the starting of radio
       
    66  is done with startMonitoring(). 
       
    67  */
       
    68 void FmRadioHsWidgetRadioServiceClient::doPowerOnFmRadio()
       
    69 {
       
    70     LOG_METHOD;
       
    71     QVariant commandArgument;
       
    72     commandArgument.setValue(static_cast<int>(RadioServiceCommand::PowerOn));
       
    73     doSendControlRequest(commandArgument, DoNotChange);
       
    74 }
       
    75 
       
    76 
       
    77 /*!
       
    78  Bring FM Radio to foreground.
       
    79  
       
    80  */
       
    81 void FmRadioHsWidgetRadioServiceClient::doChangeFmRadioVisibility(
       
    82     const FmRadioVisibilty visibility)
       
    83 {
       
    84     LOG_METHOD;
       
    85     QVariant commandArgument;
       
    86     switch (visibility) {
       
    87     case ToForeground:
       
    88         LOG("ToForeground");
       
    89         commandArgument.setValue(static_cast<int>(RadioServiceCommand::Foreground));
       
    90         break;
       
    91     case ToBackground:
       
    92         LOG("ToBackground");
       
    93         commandArgument.setValue(static_cast<int>(RadioServiceCommand::Background));
       
    94     case DoNotChange:
       
    95     default:
       
    96         LOG("DoNotChange or default");
       
    97         break;
       
    98     }
       
    99     doSendControlRequest(commandArgument, visibility);
       
   100 }
       
   101 
       
   102 /*!
       
   103  Changing of FM Radio station.
       
   104  
       
   105  /param command Command to execute.
       
   106  */
       
   107 void FmRadioHsWidgetRadioServiceClient::doChangeFmRadioStation(
       
   108     const FmRadioStationChangeCommand command)
       
   109 {
       
   110     LOG_METHOD;
       
   111     QVariant commandArgument;
       
   112     switch (command) {
       
   113     case PreviousFavouriteStation:
       
   114         LOG("PreviousFavouriteChannel");
       
   115         commandArgument.setValue(static_cast<int>(RadioServiceCommand::Previous));
       
   116         break;
       
   117     case NextFavouriteStation:
       
   118         LOG("NextFavouriteChannel");
       
   119         commandArgument.setValue(static_cast<int>(RadioServiceCommand::Next));
       
   120         break;
       
   121     default:
       
   122         LOG("default");
       
   123         break;
       
   124     }
       
   125     doSendControlRequest(commandArgument, DoNotChange);
       
   126 }
       
   127 
       
   128 /*!
       
   129  Start FM Radio information listening.
       
   130  */
       
   131 void FmRadioHsWidgetRadioServiceClient::doSendControlRequest(
       
   132     const QVariant &argument, const FmRadioVisibilty visibility)
       
   133 {
       
   134     LOG_METHOD;
       
   135     if (!argument.isValid()) {
       
   136         return;
       
   137     }
       
   138     
       
   139     // If there is not mRadioControlRequest already.
       
   140     if (!mRadioControlRequest) {
       
   141         // Create it.
       
   142         createControlServiceRequest();
       
   143     }
       
   144     
       
   145     // Set argument list for request.
       
   146     QList<QVariant> arguments;
       
   147     arguments.append(argument);
       
   148     mRadioControlRequest->setArguments(arguments);
       
   149     
       
   150     prepareRequestInfo(mRadioControlRequest, visibility);
       
   151     
       
   152     LOG("Send request");
       
   153     bool res = mRadioControlRequest->send();
       
   154 
       
   155     if (!res) {
       
   156         LOG("Send failed");
       
   157         int error = mRadioControlRequest->lastError();
       
   158         handleRequestError(error);
       
   159     }
       
   160 }
       
   161 
       
   162 /*!
       
   163  Start FM Radio information listening.
       
   164  */
       
   165 void FmRadioHsWidgetRadioServiceClient::doSendMonitorRequest(
       
   166     const FmRadioVisibilty visibility)
       
   167 {
       
   168     LOG_METHOD;
       
   169     prepareRequestInfo(mRadioMonitorRequest, visibility);
       
   170     if (!mRequestPending) {
       
   171         LOG("Send request");
       
   172         mRequestPending = mRadioMonitorRequest->send();
       
   173     }
       
   174 }
       
   175 
       
   176 /*!
       
   177  Handle changes in FM Radio information texts.
       
   178  
       
   179  /param value
       
   180  */
       
   181 void FmRadioHsWidgetRadioServiceClient::handleFmRadioInformationChange(
       
   182     const QVariant &value)
       
   183 {
       
   184     LOG_METHOD;
       
   185     LEVEL2(LOG_SLOT_CALLER);
       
   186     // Request is not pending anymore.
       
   187     mRequestPending = false;
       
   188     // If first request was refresh operation.
       
   189     if (!mDataInitialized) {
       
   190         LOG("Set operation to KRadioServiceMonitorOperation");
       
   191         // Change the operation to the monitoring.
       
   192         mRadioMonitorRequest->setOperation(KRadioServiceMonitorOperation);
       
   193         // Data is now initialized.
       
   194         mDataInitialized = true;
       
   195     }
       
   196     // Request notifications again.
       
   197     startMonitoring(DoNotChange);
       
   198     
       
   199     // If valid and right kind of data was received.
       
   200     if ( value.isValid() && value.canConvert( QVariant::List ) ) {
       
   201         QVariantList notificationList = value.toList();
       
   202         // Iterate through the list.
       
   203         foreach ( const QVariant& variant, notificationList ) {
       
   204             // Extract notification data.
       
   205             RadioNotificationData notification = variant.value<RadioNotificationData>();
       
   206             // And it's type.
       
   207             const int notificationId = notification.mType;
       
   208             // Emit the new information.
       
   209             emit radioInformationChanged( notificationId, notification.mData );
       
   210         }
       
   211     }
       
   212 }
       
   213 
       
   214 /*!
       
   215  Handles request error.
       
   216  
       
   217  /param int Error value.
       
   218  */
       
   219 void FmRadioHsWidgetRadioServiceClient::handleRequestError(const int error)
       
   220 {
       
   221     LOG_METHOD;
       
   222     QString errorStr;
       
   223     QVariant var(FmRadioHsWidget::NotRunning);
       
   224     switch (error) {
       
   225     case XQService::ENoError:
       
   226         errorStr = "No error";
       
   227         break;
       
   228     case XQService::EConnectionError:
       
   229         errorStr = "Error in IPC Connection";
       
   230         break;
       
   231     case XQService::EConnectionClosed:
       
   232         errorStr = "IPC Connection is closed";
       
   233         stopMonitoring();
       
   234         handleFmRadioStateChange(var);
       
   235         break;
       
   236     case XQService::EServerNotFound:
       
   237         errorStr = "Can not find server";
       
   238         break;
       
   239     case XQService::EIPCError:
       
   240         errorStr = "Known IPC error defined by SDK";
       
   241         break;
       
   242     case XQService::EUnknownError:
       
   243         errorStr = "Unknown IPC error";
       
   244         break;
       
   245     case XQService::ERequestPending:
       
   246         errorStr = "Already pending request";
       
   247         break;
       
   248     default:
       
   249         errorStr = "default case";
       
   250         break;
       
   251     }
       
   252     LOG(GETSTRING(errorStr));
       
   253 }
       
   254 
       
   255 /*!
       
   256  Handle changes in FM Radio state.
       
   257  
       
   258  /param value New state of FM Radio.
       
   259  */
       
   260 void FmRadioHsWidgetRadioServiceClient::handleFmRadioStateChange(
       
   261     const QVariant &value)
       
   262 {
       
   263     LOG_METHOD;
       
   264     if (value.isValid()) {
       
   265         emit radioStateChanged(value);
       
   266     }
       
   267 }
       
   268 
       
   269 /*!
       
   270  Handle error.
       
   271  
       
   272  /param errorCode
       
   273  /param errorMessage
       
   274  */
       
   275 void FmRadioHsWidgetRadioServiceClient::handleError(const int errorCode,
       
   276     const QString &errorMessage)
       
   277 {
       
   278     Q_UNUSED(errorMessage)
       
   279     LOG_METHOD;
       
   280     LEVEL2(LOG_SLOT_CALLER);
       
   281     handleRequestError(errorCode);
       
   282 }
       
   283 
       
   284 /*!
       
   285  Creates control service request object.
       
   286  */
       
   287 void FmRadioHsWidgetRadioServiceClient::createControlServiceRequest()
       
   288 {
       
   289     LOG_METHOD;
       
   290     if (!mRadioControlRequest) {
       
   291         // Following commented code is for debugging when changing service and interface names.
       
   292         //QString fullInterfaceName = /*KRadioServiceName +"."+*/ KRadioServiceControlInterfaceName;
       
   293         /*
       
   294         QList<XQAiwInterfaceDescriptor> list;
       
   295         list = mApplicationManager.list(KRadioServiceName, fullInterfaceName, "");
       
   296         XQAiwInterfaceDescriptor interfaceDescriptor;
       
   297         foreach (XQAiwInterfaceDescriptor d, list)
       
   298             {
       
   299                 QString in = d.interfaceName();
       
   300                 QString sn = d.serviceName();
       
   301                 if (sn == KRadioServiceName && in == fullInterfaceName) {
       
   302                     interfaceDescriptor = d;
       
   303                 }
       
   304             }
       
   305         */
       
   306         /*
       
   307         mRadioMonitorRequest = mApplicationManager.create(interfaceDescriptor,
       
   308             KRadioServiceControlOperation, false);
       
   309         */
       
   310         
       
   311         LOG("Create request");
       
   312         mRadioControlRequest = mApplicationManager.create(
       
   313             KRadioServiceControlInterfaceName, KRadioServiceControlOperation,
       
   314             false);
       
   315         
       
   316         if (mRadioControlRequest) {
       
   317             LOG("Request created");
       
   318             // Request is synchronous.
       
   319             mRadioControlRequest->setSynchronous(true);
       
   320             // Request is not embedded.
       
   321             mRadioControlRequest->setEmbedded(false);
       
   322         }
       
   323     }
       
   324 }
       
   325 
       
   326 /*!
       
   327  Creates monitor service request object.
       
   328  */
       
   329 void FmRadioHsWidgetRadioServiceClient::createMonitorServiceRequest()
       
   330 {
       
   331     LOG_METHOD;
       
   332     if (!mRadioMonitorRequest) {
       
   333         // If data is not initialized, set operation to refresh,
       
   334         // otherwise to monitor operation.
       
   335 		QString operation = mDataInitialized ? KRadioServiceMonitorOperation
       
   336             : KRadioServiceRefreshOperation;
       
   337         QString fullInterfaceName = /*KRadioServiceName +"."+*/ KRadioServiceMonitorInterfaceName;
       
   338 
       
   339         // Following commented code is for debugging when changing service and interface names.
       
   340         /*
       
   341         QList<XQAiwInterfaceDescriptor> list;
       
   342         list = mApplicationManager.list(KRadioServiceName, fullInterfaceName, "");
       
   343         XQAiwInterfaceDescriptor interfaceDescriptor;
       
   344         foreach (XQAiwInterfaceDescriptor d, list)
       
   345             {
       
   346                 QString in = d.interfaceName();
       
   347                 QString sn = d.serviceName();
       
   348                 if (sn == KRadioServiceName && in == fullInterfaceName) {
       
   349                     interfaceDescriptor = d;
       
   350                 }
       
   351             }
       
   352         */
       
   353         /*
       
   354         mRadioMonitorRequest = mApplicationManager.create(interfaceDescriptor,
       
   355             operation, false);
       
   356         */
       
   357         
       
   358         LOG("Create request");
       
   359         mRadioMonitorRequest = mApplicationManager.create(
       
   360             KRadioServiceMonitorInterfaceName, operation, false);
       
   361         
       
   362         if (mRadioMonitorRequest) {
       
   363             LOG("Request created");
       
   364             // Connect request to handle it's completion.
       
   365             connect(mRadioMonitorRequest, SIGNAL(requestOk(const QVariant&)),
       
   366                 this, SLOT(handleFmRadioInformationChange(const QVariant&)));
       
   367             // Connect request to handle it's error.
       
   368             connect(mRadioMonitorRequest,
       
   369                 SIGNAL(requestError(int,const QString&)), this,
       
   370                 SLOT(handleError(int,const QString&)));
       
   371             
       
   372             // Request is asynchronous.
       
   373             mRadioMonitorRequest->setSynchronous(false);
       
   374             // Request is not embedded.
       
   375             mRadioMonitorRequest->setEmbedded(false);
       
   376         }
       
   377     }
       
   378 }
       
   379 
       
   380 /*!
       
   381  Start radio monitoring.
       
   382  */
       
   383 void FmRadioHsWidgetRadioServiceClient::startMonitoring(FmRadioVisibilty visibility)
       
   384 {
       
   385     LOG_METHOD;
       
   386     // If there is no mRadioMonitorRequest create it.
       
   387     if (!mRadioMonitorRequest) {
       
   388         createMonitorServiceRequest();
       
   389     }
       
   390     doSendMonitorRequest(visibility);
       
   391 }
       
   392 
       
   393 /*!
       
   394  Stops radio monitoring.
       
   395  */
       
   396 void FmRadioHsWidgetRadioServiceClient::stopMonitoring()
       
   397 {
       
   398     LOG_METHOD;
       
   399     // Delete the mRadioMonitorRequest. 
       
   400     if (mRadioMonitorRequest) {
       
   401         delete mRadioMonitorRequest;
       
   402         mRadioMonitorRequest = NULL;
       
   403         mRequestPending = false;
       
   404         mDataInitialized = false;
       
   405     }
       
   406 }
       
   407 
       
   408 /*!
       
   409  Prepares the visibility of the request.
       
   410  */
       
   411 void FmRadioHsWidgetRadioServiceClient::prepareRequestInfo(
       
   412     XQAiwRequest *request, const FmRadioVisibilty visibility)
       
   413 {
       
   414     LOG_METHOD;
       
   415     XQRequestInfo info;
       
   416     switch (visibility) {
       
   417     case ToForeground:
       
   418         info.setForeground(true);
       
   419         break;
       
   420     case ToBackground:
       
   421         info.setBackground(true);
       
   422         break;
       
   423     case DoNotChange:
       
   424     default:
       
   425         break;
       
   426     }
       
   427     if (request) {
       
   428         request->setInfo(info);
       
   429     }
       
   430 }