|
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 // ==================== MEMBER FUNCTIONS ====================== |
|
41 // Constructor |
|
42 IrServiceClient::IrServiceClient(QObject *aParent) : |
|
43 QObject(aParent), |
|
44 mIrAppInspector(NULL), |
|
45 mMonitorReqOngoing(false), |
|
46 mControlReqOngoing(false), |
|
47 mMonitorRequest(NULL), |
|
48 mRefreshRequest(NULL), |
|
49 mControlRequest(NULL) |
|
50 { |
|
51 mIrAppInspector = new IrAppInspector(this); |
|
52 |
|
53 QObject::connect(mIrAppInspector, SIGNAL(irRunningStatusChanged(IrAppInspector::IrRunningStatus)), |
|
54 this, SLOT(handleIrRunningStatusChanged(IrAppInspector::IrRunningStatus))); |
|
55 } |
|
56 |
|
57 // Destructor |
|
58 IrServiceClient::~IrServiceClient() |
|
59 { |
|
60 delete mMonitorRequest; |
|
61 mMonitorRequest = NULL; |
|
62 |
|
63 delete mRefreshRequest; |
|
64 mRefreshRequest = NULL; |
|
65 |
|
66 delete mControlRequest; |
|
67 mControlRequest = NULL; |
|
68 } |
|
69 |
|
70 bool IrServiceClient::startMonitoringIrState() |
|
71 { |
|
72 bool retVal = false; |
|
73 if (mIrAppInspector->isIrRunning()) |
|
74 { |
|
75 retVal = doSendRefreshRequest(); |
|
76 } |
|
77 else |
|
78 { |
|
79 if (isStationPlayed()) |
|
80 { |
|
81 initHsWidgetNoRunStopped(); |
|
82 } |
|
83 else |
|
84 { |
|
85 emit irStateChanged(IrAppState::NoRunInit); |
|
86 } |
|
87 } |
|
88 |
|
89 return mIrAppInspector->startInspectingIrRunningStatus() && retVal; |
|
90 } |
|
91 |
|
92 void IrServiceClient::stopMonitoringIrState() |
|
93 { |
|
94 delete mMonitorRequest; |
|
95 mMonitorRequest = NULL; |
|
96 |
|
97 delete mRefreshRequest; |
|
98 mRefreshRequest = NULL; |
|
99 } |
|
100 |
|
101 void IrServiceClient::initHsWidgetNoRunStopped() |
|
102 { |
|
103 emit irStateChanged(IrAppState::NoRunStopped); |
|
104 |
|
105 QString stationName; |
|
106 if (loadStationName(stationName)) |
|
107 { |
|
108 emit stationNameUpdated(stationName); |
|
109 } |
|
110 else |
|
111 { |
|
112 emit stationNameUpdated(QString("")); |
|
113 } |
|
114 |
|
115 emit stationLogoUpdated(loadStationLogoFlag()); |
|
116 } |
|
117 |
|
118 /****************************************************************** |
|
119 **************** Monitor Service Request ********************** |
|
120 *******************************************************************/ |
|
121 bool IrServiceClient::doSendMonitorRequest() |
|
122 { |
|
123 if(mMonitorReqOngoing) |
|
124 { |
|
125 return false; |
|
126 } |
|
127 |
|
128 if (NULL == mMonitorRequest |
|
129 &&!createMonitorServiceRequest()) |
|
130 { |
|
131 return false; |
|
132 } |
|
133 |
|
134 prepareRequestInfo(mMonitorRequest, DoNotChange); |
|
135 return mMonitorRequest->send(); |
|
136 } |
|
137 |
|
138 bool IrServiceClient::createMonitorServiceRequest() |
|
139 { |
|
140 bool retVal = false; |
|
141 |
|
142 if (NULL == mMonitorRequest) |
|
143 { |
|
144 mMonitorRequest = mApplicationManager.create(KIrServiceName, |
|
145 KIrServiceMonitorInterfaceName, |
|
146 KIrServiceMonitorOperation, |
|
147 false); |
|
148 |
|
149 if (mMonitorRequest) |
|
150 { |
|
151 retVal = true; |
|
152 |
|
153 connect(mMonitorRequest, SIGNAL(requestOk(const QVariant&)), |
|
154 this, SLOT(handleMonitorRequestOk(const QVariant&))); |
|
155 connect(mMonitorRequest, SIGNAL(requestError(int,const QString&)), |
|
156 this, SLOT(handleMonitorRequestError(int,const QString&))); |
|
157 |
|
158 mMonitorRequest->setEmbedded(false); |
|
159 mMonitorRequest->setSynchronous(false); |
|
160 } |
|
161 } |
|
162 else |
|
163 { |
|
164 retVal = true; |
|
165 } |
|
166 |
|
167 return retVal; |
|
168 } |
|
169 |
|
170 // ==================== Refresh Service Request =========================== |
|
171 bool IrServiceClient::doSendRefreshRequest() |
|
172 { |
|
173 if(mMonitorReqOngoing) |
|
174 { |
|
175 return false; |
|
176 } |
|
177 |
|
178 if (NULL == mRefreshRequest |
|
179 && !createRefreshServiceRequest()) |
|
180 { |
|
181 return false; |
|
182 } |
|
183 |
|
184 prepareRequestInfo(mRefreshRequest, DoNotChange); |
|
185 return mRefreshRequest->send(); |
|
186 } |
|
187 |
|
188 bool IrServiceClient::createRefreshServiceRequest() |
|
189 { |
|
190 bool retVal = false; |
|
191 |
|
192 if (NULL == mRefreshRequest) |
|
193 { |
|
194 mRefreshRequest = mApplicationManager.create(KIrServiceName, |
|
195 KIrServiceMonitorInterfaceName, |
|
196 KIrServiceRefreshOperation, |
|
197 false); |
|
198 |
|
199 if (mRefreshRequest) |
|
200 { |
|
201 retVal = true; |
|
202 |
|
203 connect(mRefreshRequest, SIGNAL(requestOk(const QVariant&)), |
|
204 this, SLOT(handleMonitorRequestOk(const QVariant&))); |
|
205 connect(mRefreshRequest, SIGNAL(requestError(int,const QString&)), |
|
206 this, SLOT(handleMonitorRequestError(int,const QString&))); |
|
207 |
|
208 mRefreshRequest->setEmbedded(false); |
|
209 mRefreshRequest->setSynchronous(false); |
|
210 } |
|
211 } |
|
212 else |
|
213 { |
|
214 retVal = true; |
|
215 } |
|
216 |
|
217 return retVal; |
|
218 } |
|
219 |
|
220 void IrServiceClient::handleMonitorRequestOk(const QVariant &aRetValue) |
|
221 { |
|
222 mMonitorReqOngoing = false; |
|
223 |
|
224 doSendMonitorRequest(); |
|
225 |
|
226 if (aRetValue.isValid() |
|
227 && aRetValue.canConvert(QVariant::List)) |
|
228 { |
|
229 QVariantList dataList = aRetValue.toList(); |
|
230 foreach (const QVariant& data, dataList) |
|
231 { |
|
232 if (data.canConvert<IrServiceData>()) |
|
233 { |
|
234 IrServiceData serviceData = data.value<IrServiceData>(); |
|
235 processNotificationData(serviceData); |
|
236 } |
|
237 } |
|
238 } |
|
239 } |
|
240 |
|
241 void IrServiceClient::handleMonitorRequestError(int aErrCode, const QString &aErrMessage) |
|
242 { |
|
243 mMonitorReqOngoing = false; |
|
244 |
|
245 Q_UNUSED(aErrCode); |
|
246 Q_UNUSED(aErrMessage); |
|
247 } |
|
248 |
|
249 /****************************************************************** |
|
250 **************** Control Service Request ********************** |
|
251 *******************************************************************/ |
|
252 bool IrServiceClient::bringIrForeground() |
|
253 { |
|
254 QVariant commandArgument(QVariant::Int); |
|
255 commandArgument.setValue((int)IrServiceCmd::Foreground); |
|
256 return doSendControlRequest(commandArgument, true, ToForeground); |
|
257 } |
|
258 |
|
259 bool IrServiceClient::launchIrNormally() |
|
260 { |
|
261 QVariant commandArgument(QVariant::Int); |
|
262 commandArgument.setValue((int)IrServiceCmd::LaunchNormally); |
|
263 return doSendControlRequest(commandArgument, true, ToForeground); |
|
264 } |
|
265 |
|
266 bool IrServiceClient::launchIrNowPlaying() |
|
267 { |
|
268 QVariant commandArgument(QVariant::Int); |
|
269 commandArgument.setValue((int)IrServiceCmd::LaunchNowPlayingView); |
|
270 return doSendControlRequest(commandArgument, false, ToBackground); |
|
271 } |
|
272 |
|
273 |
|
274 bool IrServiceClient::startPlaying() |
|
275 { |
|
276 QVariant commandArgument(QVariant::Int); |
|
277 commandArgument.setValue((int)IrServiceCmd::Play); |
|
278 return doSendControlRequest(commandArgument, true, DoNotChange); |
|
279 } |
|
280 |
|
281 |
|
282 bool IrServiceClient::stopPlaying() |
|
283 { |
|
284 QVariant commandArgument(QVariant::Int); |
|
285 commandArgument.setValue((int)IrServiceCmd::Stop); |
|
286 return doSendControlRequest(commandArgument, true, DoNotChange); |
|
287 } |
|
288 |
|
289 bool IrServiceClient::cancelLoading() |
|
290 { |
|
291 QVariant commandArgument(QVariant::Int); |
|
292 commandArgument.setValue((int)IrServiceCmd::Cancel); |
|
293 return doSendControlRequest(commandArgument, true, DoNotChange); |
|
294 } |
|
295 |
|
296 bool IrServiceClient::doSendControlRequest(const QVariant &aArgument, bool aIsSync, IrAppVisibilty aVisibility) |
|
297 { |
|
298 if(mControlReqOngoing) |
|
299 { |
|
300 return false; |
|
301 } |
|
302 |
|
303 if (NULL == mControlRequest && |
|
304 !createControlServiceRequest()) |
|
305 { |
|
306 return false; |
|
307 } |
|
308 |
|
309 QList<QVariant> arguments; |
|
310 arguments.append(aArgument); |
|
311 mControlRequest->setArguments(arguments); |
|
312 |
|
313 prepareRequestInfo(mControlRequest, aVisibility); |
|
314 mControlRequest->setSynchronous(aIsSync); |
|
315 return mControlRequest->send(); |
|
316 } |
|
317 |
|
318 bool IrServiceClient::createControlServiceRequest() |
|
319 { |
|
320 bool retVal = false; |
|
321 |
|
322 if (NULL == mControlRequest) |
|
323 { |
|
324 mControlRequest = mApplicationManager.create(KIrServiceName, |
|
325 KIrServiceControlInterfaceName, |
|
326 KIrServiceControlOperation, |
|
327 false); |
|
328 |
|
329 if (mControlRequest) |
|
330 { |
|
331 retVal = true; |
|
332 |
|
333 connect(mControlRequest, SIGNAL(requestOk(const QVariant&)), |
|
334 this, SLOT(handleControlRequestOk(const QVariant&))); |
|
335 connect(mControlRequest, SIGNAL(requestError(int,const QString&)), |
|
336 this, SLOT(handleControlRequestError(int,const QString&))); |
|
337 |
|
338 mControlRequest->setEmbedded(false); |
|
339 } |
|
340 } |
|
341 else |
|
342 { |
|
343 retVal = true; |
|
344 } |
|
345 |
|
346 return retVal; |
|
347 } |
|
348 |
|
349 void IrServiceClient::handleControlRequestOk(const QVariant &aRetValue) |
|
350 { |
|
351 mControlReqOngoing = false; |
|
352 |
|
353 if (aRetValue.isValid() |
|
354 && aRetValue.canConvert(QVariant::Int)) |
|
355 { |
|
356 if (IrServiceResult::Fail == aRetValue.toInt()) |
|
357 { |
|
358 emit controlFailed(); |
|
359 } |
|
360 } |
|
361 } |
|
362 |
|
363 void IrServiceClient::handleControlRequestError(int aErrCode, const QString &aErrMessage) |
|
364 { |
|
365 mControlReqOngoing = false; |
|
366 |
|
367 emit controlFailed(); |
|
368 |
|
369 Q_UNUSED(aErrCode); |
|
370 Q_UNUSED(aErrMessage); |
|
371 } |
|
372 /****************************************************************** |
|
373 ************************* Others ****************************** |
|
374 *******************************************************************/ |
|
375 // slot connected to signals from IrAppInspector, |
|
376 // used to monitor the startup / exit of IR |
|
377 void IrServiceClient::handleIrRunningStatusChanged(IrAppInspector::IrRunningStatus aNewStatus) |
|
378 { |
|
379 switch (aNewStatus) |
|
380 { |
|
381 case IrAppInspector::StartingUp : |
|
382 doSendRefreshRequest(); |
|
383 break; |
|
384 |
|
385 case IrAppInspector::Exiting : |
|
386 stopMonitoringIrState(); |
|
387 |
|
388 if (isStationPlayed()) |
|
389 { |
|
390 resetHsWidgetExitStopped(); |
|
391 } |
|
392 else |
|
393 { |
|
394 emit irStateChanged(IrAppState::NoRunInit); |
|
395 } |
|
396 break; |
|
397 |
|
398 default: |
|
399 break; |
|
400 } |
|
401 } |
|
402 |
|
403 void IrServiceClient::resetHsWidgetExitStopped() |
|
404 { |
|
405 emit irStateChanged(IrAppState::NoRunStopped); |
|
406 emit metaDataUpdated(QString("")); |
|
407 } |
|
408 |
|
409 // used to process service data sent from IR app. |
|
410 void IrServiceClient::processNotificationData(const IrServiceData &aServiceData) |
|
411 { |
|
412 QVariant data = aServiceData.mData; |
|
413 if (!data.isValid()) |
|
414 { |
|
415 return; |
|
416 } |
|
417 switch (aServiceData.mType) |
|
418 { |
|
419 case IrServiceNotification::StationName: |
|
420 if (data.canConvert(QVariant::String)) |
|
421 { |
|
422 emit stationNameUpdated(data.toString()); |
|
423 } |
|
424 break; |
|
425 |
|
426 case IrServiceNotification::MetaData: |
|
427 if (data.canConvert(QVariant::String)) |
|
428 { |
|
429 emit metaDataUpdated(data.toString()); |
|
430 } |
|
431 break; |
|
432 |
|
433 case IrServiceNotification::StationLogo: |
|
434 if (data.canConvert(QVariant::Bool)) |
|
435 { |
|
436 emit stationLogoUpdated(data.toBool()); |
|
437 } |
|
438 break; |
|
439 |
|
440 case IrServiceNotification::IrState: |
|
441 if (data.canConvert(QVariant::Int)) |
|
442 { |
|
443 emit irStateChanged((IrAppState::Type)data.toInt()); |
|
444 } |
|
445 break; |
|
446 |
|
447 default: |
|
448 break; |
|
449 } |
|
450 |
|
451 } |
|
452 |
|
453 // prepare the request send to IR app, including control request, refresh request, monitor request. |
|
454 void IrServiceClient::prepareRequestInfo(XQAiwRequest *aRequest, IrAppVisibilty aVisibility) |
|
455 { |
|
456 if (NULL == aRequest) |
|
457 { |
|
458 return; |
|
459 } |
|
460 |
|
461 XQRequestInfo reqInfo; |
|
462 switch (aVisibility) |
|
463 { |
|
464 case ToForeground: |
|
465 reqInfo.setForeground(true); |
|
466 break; |
|
467 case ToBackground: |
|
468 reqInfo.setBackground(true); |
|
469 break; |
|
470 case DoNotChange: |
|
471 break; |
|
472 default: |
|
473 break; |
|
474 } |
|
475 |
|
476 aRequest->setInfo(reqInfo); |
|
477 } |
|
478 |
|
479 bool IrServiceClient::isStationPlayed() |
|
480 { |
|
481 QSettings settings(KIrSettingOrganization, KIrSettingApplicaton); |
|
482 QString stationName = settings.value(KIrSettingStationName,KIrDefaultStationName).toString(); |
|
483 return stationName != KIrDefaultStationName; |
|
484 } |
|
485 |
|
486 bool IrServiceClient::loadStationLogoFlag() |
|
487 { |
|
488 QSettings settings(KIrSettingOrganization, KIrSettingApplicaton); |
|
489 return settings.value(KIrSettingStationLogoAvailable,false).toBool(); |
|
490 } |
|
491 |
|
492 bool IrServiceClient::loadStationName(QString &aStationName) |
|
493 { |
|
494 QSettings settings(KIrSettingOrganization, KIrSettingApplicaton); |
|
495 aStationName = settings.value(KIrSettingStationName,KIrDefaultStationName).toString(); |
|
496 if(aStationName != KIrDefaultStationName) |
|
497 { |
|
498 return true; |
|
499 } |
|
500 else |
|
501 { |
|
502 return false; |
|
503 } |
|
504 } |
|
505 |
|
506 Q_IMPLEMENT_USER_METATYPE(IrServiceData) |
|
507 |