radiohswidget/src/radiohswidgetradioserviceclient.cpp
changeset 32 189d20c34778
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 "radiohswidgetradioserviceclient.h"
       
    23 #include "radiohswidget.h"
       
    24 #include "radionotificationdata.h"
       
    25 #include "radioservicedef.h"
       
    26 #include "radiologger.h"
       
    27 
       
    28 /*!
       
    29     \class RadioHsWidgetRadioServiceClient
       
    30     \brief Implementation of QtHighway based communicator with radio application.
       
    31 
       
    32     RadioHsWidgetRadioServiceClient implements monitoring of radio
       
    33     information and controlling of radio. This is done by service request
       
    34     through QtHighway.
       
    35 */
       
    36 
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38 
       
    39 /*!
       
    40     Constructs a profile reader which is a child of \a parent.
       
    41  */
       
    42 RadioHsWidgetRadioServiceClient::RadioHsWidgetRadioServiceClient(
       
    43     RadioHsWidget *parent) :
       
    44     QObject(parent),
       
    45     mParent(*parent),
       
    46     mRequestPending(false),
       
    47     mRadioMonitorRequest(NULL),
       
    48     mRadioControlRequest(NULL),
       
    49     mDataInitialized(false)
       
    50 {
       
    51     LOG_METHOD;
       
    52 }
       
    53 
       
    54 /*!
       
    55     Destructor 
       
    56  */
       
    57 RadioHsWidgetRadioServiceClient::~RadioHsWidgetRadioServiceClient()
       
    58 {
       
    59     LOG_METHOD;
       
    60     stopMonitoring();
       
    61 }
       
    62 
       
    63 /*!
       
    64     Command FM Radio station.
       
    65  
       
    66     \param command Command to request radio to perform.
       
    67  */
       
    68 void RadioHsWidgetRadioServiceClient::commandFmRadio(
       
    69     const RadioServiceCommand::CommandId command)
       
    70 {
       
    71     LOG_METHOD;
       
    72     QVariant commandArgument;
       
    73     commandArgument.setValue(static_cast<int>(command));
       
    74     FmRadio::VisibiltyAfterRequest visibility;
       
    75     if (command == RadioServiceCommand::Foreground) {
       
    76         visibility = FmRadio::VisibiltyToForeground;
       
    77     } else if (command == RadioServiceCommand::Background) {
       
    78         visibility = FmRadio::VisibiltyToBackground;
       
    79     }  else {
       
    80         visibility = FmRadio::VisibiltyDoNotChange;
       
    81     }
       
    82     sendControlRequest(commandArgument, visibility);
       
    83 }
       
    84 
       
    85 /*!
       
    86     Start radio monitoring.
       
    87     
       
    88     \param visibility Desired visibility for the radio after the request.
       
    89  */
       
    90 void RadioHsWidgetRadioServiceClient::startMonitoring(
       
    91     FmRadio::VisibiltyAfterRequest visibility)
       
    92 {
       
    93     LOG_METHOD;
       
    94     createMonitorServiceRequest();
       
    95     sendMonitorRequest(visibility);
       
    96 }
       
    97 
       
    98 /*!
       
    99     Stops radio monitoring.
       
   100  */
       
   101 void RadioHsWidgetRadioServiceClient::stopMonitoring()
       
   102 {
       
   103     LOG_METHOD;
       
   104     // Delete the mRadioMonitorRequest. 
       
   105     if (mRadioMonitorRequest) {
       
   106         delete mRadioMonitorRequest;
       
   107         mRadioMonitorRequest = NULL;
       
   108         mRequestPending = false;
       
   109         mDataInitialized = false;
       
   110     }
       
   111 }
       
   112 
       
   113 /*!
       
   114     Handles request errors.
       
   115  
       
   116     \param errorCode Code of the error type.
       
   117     \param errorMessage Error message.
       
   118  */
       
   119 void RadioHsWidgetRadioServiceClient::handleError(const int errorCode,
       
   120     const QString &errorMessage)
       
   121 {
       
   122     Q_UNUSED(errorMessage)
       
   123     LOG_METHOD;
       
   124     LEVEL2(LOG_SLOT_CALLER);
       
   125     handleRequestError(errorCode);
       
   126 }
       
   127 
       
   128 /*!
       
   129     Handle changes in FM Radio.
       
   130  
       
   131     \param value List of information published by radio.
       
   132  */
       
   133 void RadioHsWidgetRadioServiceClient::handleFmRadioChange(
       
   134     const QVariant &value)
       
   135 {
       
   136     LOG_METHOD;
       
   137     LEVEL2(LOG_SLOT_CALLER);
       
   138     // Request is not pending anymore.
       
   139     mRequestPending = false;
       
   140     // If first request was refresh operation.
       
   141     if (!mDataInitialized) {
       
   142         LOG("Set operation to KRadioServiceMonitorOperation");
       
   143         // Change the operation to the monitoring.
       
   144         mRadioMonitorRequest->setOperation(RADIO_MONITOR_SERVICE_OPERATION);
       
   145         // Data is now initialized.
       
   146         mDataInitialized = true;
       
   147     }
       
   148     // Request notifications again.
       
   149     startMonitoring(FmRadio::VisibiltyDoNotChange);
       
   150     
       
   151     // If valid and right kind of data was received.
       
   152     if (value.canConvert(QVariant::List) ) {
       
   153         QVariantList notificationList = value.toList();
       
   154         // Iterate through the list.
       
   155         foreach (const QVariant& variant, notificationList) {
       
   156             // Extract notification data.
       
   157             RadioNotificationData notification = variant.value<RadioNotificationData>();
       
   158             // And it's type.
       
   159             const int notificationId = notification.mType;
       
   160             // Notify the observer about the new information.
       
   161             mParent.handleRadioInformationChange(notificationId, notification.mData);
       
   162         }
       
   163     }
       
   164 }
       
   165 
       
   166 /*!
       
   167     Handles request error.
       
   168  
       
   169     \param error Identifies the error. 
       
   170  */
       
   171 void RadioHsWidgetRadioServiceClient::handleRequestError(const int error)
       
   172 {
       
   173     LOG_METHOD;
       
   174     QString errorStr;
       
   175     switch (error) {
       
   176     case XQService::ENoError:
       
   177         errorStr = "No error";
       
   178         break;
       
   179     case XQService::EConnectionError:
       
   180         errorStr = "Error in IPC Connection";
       
   181         break;
       
   182     case XQService::EConnectionClosed:
       
   183         errorStr = "IPC Connection is closed";
       
   184         stopMonitoring();
       
   185         mParent.handleRadioStateChange(FmRadio::StateNotRunning);
       
   186         break;
       
   187     case XQService::EServerNotFound:
       
   188         errorStr = "Can not find server";
       
   189         break;
       
   190     case XQService::EIPCError:
       
   191         errorStr = "Known IPC error defined by SDK";
       
   192         break;
       
   193     case XQService::EUnknownError:
       
   194         errorStr = "Unknown IPC error";
       
   195         break;
       
   196     case XQService::ERequestPending:
       
   197         errorStr = "Already pending request";
       
   198         break;
       
   199     default:
       
   200         errorStr = "default case at error";
       
   201         break;
       
   202     }
       
   203     LOG(GETSTRING(errorStr));
       
   204 }
       
   205 
       
   206 /*!
       
   207     Creates control service request object.
       
   208  */
       
   209 void RadioHsWidgetRadioServiceClient::createControlServiceRequest()
       
   210 {
       
   211     LOG_METHOD;
       
   212     if (!mRadioControlRequest) {
       
   213         // Following commented code is for debugging when changing service and interface names.
       
   214         //QString fullInterfaceName = /*KRadioServiceName +"."+*/ RADIO_CONTROL_SERVICE;
       
   215         /*
       
   216         QList<XQAiwInterfaceDescriptor> list;
       
   217         list = mApplicationManager.list(KRadioServiceName, fullInterfaceName, "");
       
   218         XQAiwInterfaceDescriptor interfaceDescriptor;
       
   219         foreach (XQAiwInterfaceDescriptor d, list)
       
   220             {
       
   221                 QString in = d.interfaceName();
       
   222                 QString sn = d.serviceName();
       
   223                 if (sn == KRadioServiceName && in == fullInterfaceName) {
       
   224                     interfaceDescriptor = d;
       
   225                 }
       
   226             }
       
   227         */
       
   228         /*
       
   229         mRadioMonitorRequest = mApplicationManager.create(interfaceDescriptor,
       
   230             KRadioServiceControlOperation, false);
       
   231         */
       
   232         
       
   233         LOG("Create request");
       
   234         mRadioControlRequest = mApplicationManager.create(
       
   235             RADIO_CONTROL_SERVICE, RADIO_CONTROL_SERVICE_OPERATION, false);
       
   236         
       
   237         if (mRadioControlRequest) {
       
   238             LOG("Request created");
       
   239             // Request is synchronous.
       
   240             mRadioControlRequest->setSynchronous(true);
       
   241             // Request is not embedded.
       
   242             mRadioControlRequest->setEmbedded(false);
       
   243         }
       
   244     }
       
   245 }
       
   246 
       
   247 /*!
       
   248     Creates monitor service request object.
       
   249  */
       
   250 void RadioHsWidgetRadioServiceClient::createMonitorServiceRequest()
       
   251 {
       
   252     LOG_METHOD;
       
   253     if (!mRadioMonitorRequest) {
       
   254         // If data is not initialized, set operation to refresh,
       
   255         // otherwise to monitor operation.
       
   256         QString operation = mDataInitialized ? RADIO_MONITOR_SERVICE_OPERATION
       
   257             : RADIO_MONITOR_SERVICE_REFRESH_OPERATION;
       
   258 
       
   259         // Following commented code is for debugging when changing service and interface names.
       
   260         //QString fullInterfaceName = RADIO_SERVICE +"."+ RADIO_MONITOR_SERVICE;
       
   261         /*
       
   262         QList<XQAiwInterfaceDescriptor> list;
       
   263         list = mApplicationManager.list(RADIO_SERVICE, RADIO_MONITOR_SERVICE, "");
       
   264         XQAiwInterfaceDescriptor interfaceDescriptor;
       
   265         foreach (XQAiwInterfaceDescriptor d, list)
       
   266             {
       
   267                 QString in = d.interfaceName();
       
   268                 QString sn = d.serviceName();
       
   269                 if (sn == RADIO_SERVICE && in == RADIO_MONITOR_SERVICE) {
       
   270                     interfaceDescriptor = d;
       
   271                 }
       
   272             }
       
   273                 
       
   274         
       
   275         mRadioMonitorRequest = mApplicationManager.create(interfaceDescriptor,
       
   276             operation, false);
       
   277         */
       
   278         
       
   279         LOG("Create request");
       
   280         mRadioMonitorRequest = mApplicationManager.create(
       
   281             RADIO_MONITOR_SERVICE, operation, false);
       
   282         
       
   283         if (mRadioMonitorRequest) {
       
   284             LOG("Request created");
       
   285             // Connect request to handle it's completion.
       
   286             Radio::connect(mRadioMonitorRequest, SIGNAL(requestOk(const QVariant&)),
       
   287                 this, SLOT(handleFmRadioChange(const QVariant&)));
       
   288             // Connect request to handle it's error.
       
   289             Radio::connect(mRadioMonitorRequest,
       
   290                 SIGNAL(requestError(int,const QString&)), this,
       
   291                 SLOT(handleError(int,const QString&)));
       
   292             
       
   293             // Request is asynchronous.
       
   294             mRadioMonitorRequest->setSynchronous(false);
       
   295             // Request is not embedded.
       
   296             mRadioMonitorRequest->setEmbedded(false);
       
   297         }
       
   298     }
       
   299 }
       
   300 
       
   301 /*!
       
   302     Sends the control request containing the command.
       
   303     
       
   304     \param argument Contains the command.
       
   305     \param visibility Desired visibility for the radio after the request.
       
   306  */
       
   307 void RadioHsWidgetRadioServiceClient::sendControlRequest(
       
   308     const QVariant &argument,
       
   309     const FmRadio::VisibiltyAfterRequest visibility)
       
   310 {
       
   311     LOG_METHOD;
       
   312     if (!argument.isValid()) {
       
   313         return;
       
   314     }
       
   315     
       
   316     // If there is not mRadioControlRequest already.
       
   317     if (!mRadioControlRequest) {
       
   318         // Create it.
       
   319         createControlServiceRequest();
       
   320     }
       
   321     
       
   322     // Set argument list for request.
       
   323     QVariantList arguments;
       
   324     arguments.append(argument);
       
   325     mRadioControlRequest->setArguments(arguments);
       
   326     
       
   327     prepareRequestInfo(mRadioControlRequest, visibility);
       
   328     
       
   329     LOG("Send request");
       
   330     bool res = mRadioControlRequest->send();
       
   331 
       
   332     if (!res) {
       
   333         LOG("Send failed");
       
   334         int error = mRadioControlRequest->lastError();
       
   335         handleRequestError(error);
       
   336     }
       
   337 }
       
   338 
       
   339 /*!
       
   340     Sends the monitor request.
       
   341     
       
   342     \param visibility Desired visibility for the radio after the request.
       
   343  */
       
   344 void RadioHsWidgetRadioServiceClient::sendMonitorRequest(
       
   345     const FmRadio::VisibiltyAfterRequest visibility)
       
   346 {
       
   347     LOG_METHOD;
       
   348     prepareRequestInfo(mRadioMonitorRequest, visibility);
       
   349     if (!mRequestPending) {
       
   350         LOG("Send request");
       
   351         mRequestPending = mRadioMonitorRequest->send();
       
   352     }
       
   353 }
       
   354 
       
   355 /*!
       
   356     Prepares the visibility of the request.
       
   357     
       
   358     \param request Request to prepare.
       
   359     \param visibility Desired visibility for the radio after the request.
       
   360  */
       
   361 void RadioHsWidgetRadioServiceClient::prepareRequestInfo(
       
   362     XQAiwRequest *request,
       
   363     const FmRadio::VisibiltyAfterRequest visibility)
       
   364 {
       
   365     LOG_METHOD;
       
   366     XQRequestInfo info;
       
   367     if (visibility == FmRadio::VisibiltyToForeground) {
       
   368         info.setForeground(true);
       
   369     } else if (visibility == FmRadio::VisibiltyToBackground) {
       
   370         info.setBackground(true);
       
   371     }
       
   372     if (request) {
       
   373         request->setInfo(info);
       
   374     }
       
   375 }