34
|
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_ENTER;
|
|
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_ENTER;
|
|
94 |
createMonitorServiceRequest();
|
|
95 |
sendMonitorRequest(visibility);
|
|
96 |
}
|
|
97 |
|
|
98 |
/*!
|
|
99 |
Stops radio monitoring.
|
|
100 |
*/
|
|
101 |
void RadioHsWidgetRadioServiceClient::stopMonitoring()
|
|
102 |
{
|
|
103 |
LOG_METHOD_ENTER;
|
|
104 |
// Delete the mRadioMonitorRequest.
|
|
105 |
if (mRadioMonitorRequest) {
|
|
106 |
delete mRadioMonitorRequest;
|
|
107 |
mRadioMonitorRequest = NULL;
|
|
108 |
mRequestPending = false;
|
|
109 |
mDataInitialized = false;
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
/*!
|
|
114 |
Creates and returns RadioNotificationData.
|
|
115 |
This is needed because radionotificationdata.h cannot be
|
|
116 |
included on test code.
|
|
117 |
*/
|
|
118 |
QVariant RadioHsWidgetRadioServiceClient::createRadioNotificationData(int type, const QVariant& data)
|
|
119 |
{
|
|
120 |
LOG_METHOD_ENTER;
|
|
121 |
QVariant ret;
|
|
122 |
ret.setValue(RadioNotificationData(type, data));
|
|
123 |
return ret;
|
|
124 |
}
|
|
125 |
|
|
126 |
|
|
127 |
/*!
|
|
128 |
Handles request errors.
|
|
129 |
|
|
130 |
\param errorCode Code of the error type.
|
|
131 |
\param errorMessage Error message.
|
|
132 |
*/
|
|
133 |
void RadioHsWidgetRadioServiceClient::handleError(const int errorCode,
|
|
134 |
const QString &errorMessage)
|
|
135 |
{
|
|
136 |
LOG_METHOD_ENTER;
|
|
137 |
Q_UNUSED(errorMessage)
|
|
138 |
LEVEL2(LOG_SLOT_CALLER);
|
|
139 |
handleRequestError(errorCode);
|
|
140 |
}
|
|
141 |
|
|
142 |
/*!
|
|
143 |
Handle changes in FM Radio.
|
|
144 |
|
|
145 |
\param value List of information published by radio.
|
|
146 |
*/
|
|
147 |
void RadioHsWidgetRadioServiceClient::handleFmRadioChange(
|
|
148 |
const QVariant &value)
|
|
149 |
{
|
|
150 |
LOG_METHOD_ENTER;
|
|
151 |
LEVEL2(LOG_SLOT_CALLER);
|
|
152 |
// Request is not pending anymore.
|
|
153 |
mRequestPending = false;
|
|
154 |
// If first request was refresh operation.
|
|
155 |
if (!mDataInitialized) {
|
|
156 |
LOG("Set operation to KRadioServiceMonitorOperation");
|
|
157 |
// Change the operation to the monitoring.
|
|
158 |
mRadioMonitorRequest->setOperation(RADIO_MONITOR_SERVICE_OPERATION);
|
|
159 |
// Data is now initialized.
|
|
160 |
mDataInitialized = true;
|
|
161 |
}
|
|
162 |
// Request notifications again.
|
|
163 |
startMonitoring(FmRadio::VisibiltyDoNotChange);
|
|
164 |
|
|
165 |
// If valid and right kind of data was received.
|
|
166 |
if (value.canConvert(QVariant::List) ) {
|
|
167 |
QVariantList notificationList = value.toList();
|
|
168 |
// Iterate through the list.
|
|
169 |
foreach (const QVariant& variant, notificationList) {
|
|
170 |
// Extract notification data.
|
|
171 |
RadioNotificationData notification = variant.value<RadioNotificationData>();
|
|
172 |
// And it's type.
|
|
173 |
const int notificationId = notification.mType;
|
|
174 |
// And the data
|
|
175 |
const QVariant notificationData = notification.mData;
|
|
176 |
// If the data is valid.
|
|
177 |
if (notificationData.isValid()) {
|
|
178 |
// Notify the observer about the new information.
|
|
179 |
mParent.handleRadioInformationChange(notificationId, notificationData);
|
|
180 |
}
|
|
181 |
}
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
/*!
|
|
186 |
Handles request error.
|
|
187 |
|
|
188 |
\param error Identifies the error.
|
|
189 |
*/
|
|
190 |
void RadioHsWidgetRadioServiceClient::handleRequestError(const int error)
|
|
191 |
{
|
|
192 |
LOG_METHOD;
|
|
193 |
QString errorStr;
|
|
194 |
switch (error) {
|
|
195 |
case XQService::ENoError:
|
|
196 |
errorStr = "No error";
|
|
197 |
break;
|
|
198 |
case XQService::EConnectionError:
|
|
199 |
errorStr = "Error in IPC Connection";
|
|
200 |
break;
|
|
201 |
case XQService::EConnectionClosed:
|
|
202 |
errorStr = "IPC Connection is closed";
|
|
203 |
stopMonitoring();
|
|
204 |
mParent.handleRadioStateChange(FmRadio::StateNotRunning);
|
|
205 |
break;
|
|
206 |
case XQService::EServerNotFound:
|
|
207 |
errorStr = "Can not find server";
|
|
208 |
break;
|
|
209 |
case XQService::EIPCError:
|
|
210 |
errorStr = "Known IPC error defined by SDK";
|
|
211 |
break;
|
|
212 |
case XQService::EUnknownError:
|
|
213 |
errorStr = "Unknown IPC error";
|
|
214 |
break;
|
|
215 |
case XQService::ERequestPending:
|
|
216 |
errorStr = "Already pending request";
|
|
217 |
break;
|
|
218 |
default:
|
|
219 |
errorStr = "default case at error";
|
|
220 |
break;
|
|
221 |
}
|
|
222 |
LOG(GETSTRING(errorStr));
|
|
223 |
}
|
|
224 |
|
|
225 |
/*!
|
|
226 |
Creates control service request object.
|
|
227 |
*/
|
|
228 |
void RadioHsWidgetRadioServiceClient::createControlServiceRequest()
|
|
229 |
{
|
|
230 |
LOG_METHOD_ENTER;
|
|
231 |
if (!mRadioControlRequest) {
|
|
232 |
LOG("Create request");
|
|
233 |
mRadioControlRequest = mApplicationManager.create(
|
|
234 |
RADIO_CONTROL_SERVICE, RADIO_CONTROL_SERVICE_OPERATION, false);
|
|
235 |
|
|
236 |
if (mRadioControlRequest) {
|
|
237 |
LOG("Request created");
|
|
238 |
// Connect request to handle it's error.
|
|
239 |
Radio::connect(mRadioControlRequest,
|
|
240 |
SIGNAL(requestError(int,const QString&)), this,
|
|
241 |
SLOT(handleError(int,const QString&)));
|
|
242 |
|
|
243 |
// Request is synchronous.
|
|
244 |
mRadioControlRequest->setSynchronous(true);
|
|
245 |
// Request is not embedded.
|
|
246 |
mRadioControlRequest->setEmbedded(false);
|
|
247 |
}
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
/*!
|
|
252 |
Creates monitor service request object.
|
|
253 |
*/
|
|
254 |
void RadioHsWidgetRadioServiceClient::createMonitorServiceRequest()
|
|
255 |
{
|
|
256 |
LOG_METHOD_ENTER;
|
|
257 |
if (!mRadioMonitorRequest) {
|
|
258 |
// If data is not initialized, set operation to refresh,
|
|
259 |
// otherwise to monitor operation.
|
|
260 |
QString operation = mDataInitialized ? RADIO_MONITOR_SERVICE_OPERATION
|
|
261 |
: RADIO_MONITOR_SERVICE_REFRESH_OPERATION;
|
|
262 |
|
|
263 |
LOG("Create request");
|
|
264 |
mRadioMonitorRequest = mApplicationManager.create(
|
|
265 |
RADIO_MONITOR_SERVICE, operation, false);
|
|
266 |
|
|
267 |
if (mRadioMonitorRequest) {
|
|
268 |
LOG("Request created");
|
|
269 |
// Connect request to handle it's completion.
|
|
270 |
Radio::connect(mRadioMonitorRequest, SIGNAL(requestOk(const QVariant&)),
|
|
271 |
this, SLOT(handleFmRadioChange(const QVariant&)));
|
|
272 |
// Connect request to handle it's error.
|
|
273 |
Radio::connect(mRadioMonitorRequest,
|
|
274 |
SIGNAL(requestError(int,const QString&)), this,
|
|
275 |
SLOT(handleError(int,const QString&)));
|
|
276 |
|
|
277 |
// Request is asynchronous.
|
|
278 |
mRadioMonitorRequest->setSynchronous(false);
|
|
279 |
// Request is not embedded.
|
|
280 |
mRadioMonitorRequest->setEmbedded(false);
|
|
281 |
}
|
|
282 |
}
|
|
283 |
}
|
|
284 |
|
|
285 |
/*!
|
|
286 |
Sends the control request containing the command.
|
|
287 |
|
|
288 |
\param argument Contains the command.
|
|
289 |
\param visibility Desired visibility for the radio after the request.
|
|
290 |
*/
|
|
291 |
void RadioHsWidgetRadioServiceClient::sendControlRequest(
|
|
292 |
const QVariant &argument,
|
|
293 |
const FmRadio::VisibiltyAfterRequest visibility)
|
|
294 |
{
|
|
295 |
LOG_METHOD_ENTER;
|
|
296 |
if (!argument.isValid()) {
|
|
297 |
return;
|
|
298 |
}
|
|
299 |
|
|
300 |
// If there is not mRadioControlRequest already.
|
|
301 |
if (!mRadioControlRequest) {
|
|
302 |
// Create it.
|
|
303 |
createControlServiceRequest();
|
|
304 |
}
|
|
305 |
|
|
306 |
// Set argument list for request.
|
|
307 |
QVariantList arguments;
|
|
308 |
arguments.append(argument);
|
|
309 |
mRadioControlRequest->setArguments(arguments);
|
|
310 |
|
|
311 |
prepareRequestInfo(mRadioControlRequest, visibility);
|
|
312 |
|
|
313 |
LOG("Send request");
|
|
314 |
bool res = mRadioControlRequest->send();
|
|
315 |
|
|
316 |
/*
|
|
317 |
if (!res) {
|
|
318 |
LOG("Send failed");
|
|
319 |
int error = mRadioControlRequest->lastError();
|
|
320 |
handleRequestError(error);
|
|
321 |
}
|
|
322 |
*/
|
|
323 |
}
|
|
324 |
|
|
325 |
/*!
|
|
326 |
Sends the monitor request.
|
|
327 |
|
|
328 |
\param visibility Desired visibility for the radio after the request.
|
|
329 |
*/
|
|
330 |
void RadioHsWidgetRadioServiceClient::sendMonitorRequest(
|
|
331 |
const FmRadio::VisibiltyAfterRequest visibility)
|
|
332 |
{
|
|
333 |
LOG_METHOD_ENTER;
|
|
334 |
prepareRequestInfo(mRadioMonitorRequest, visibility);
|
|
335 |
if (!mRequestPending) {
|
|
336 |
LOG("Send request");
|
|
337 |
mRequestPending = mRadioMonitorRequest->send();
|
|
338 |
}
|
|
339 |
}
|
|
340 |
|
|
341 |
/*!
|
|
342 |
Prepares the visibility of the request.
|
|
343 |
|
|
344 |
\param request Request to prepare.
|
|
345 |
\param visibility Desired visibility for the radio after the request.
|
|
346 |
*/
|
|
347 |
void RadioHsWidgetRadioServiceClient::prepareRequestInfo(
|
|
348 |
XQAiwRequest *request,
|
|
349 |
const FmRadio::VisibiltyAfterRequest visibility)
|
|
350 |
{
|
|
351 |
LOG_METHOD_ENTER;
|
|
352 |
XQRequestInfo info;
|
|
353 |
if (visibility == FmRadio::VisibiltyToForeground) {
|
|
354 |
info.setForeground(true);
|
|
355 |
} else if (visibility == FmRadio::VisibiltyToBackground) {
|
|
356 |
info.setBackground(true);
|
|
357 |
}
|
|
358 |
if (request) {
|
|
359 |
request->setInfo(info);
|
|
360 |
}
|
|
361 |
}
|