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: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "t_radiodataparser.h" |
|
19 |
|
20 const int KThousand = 1000; |
|
21 const int KMillion = KThousand * KThousand; |
|
22 |
|
23 const QString KDataFile = "./settings.xml"; |
|
24 |
|
25 const QString KXmlMilliseconds = "ms"; |
|
26 const QString KXmlSeconds = "s"; |
|
27 const QString KXmlMinutes = "m"; |
|
28 const QString KXmlRadioSettings = "RadioSettings"; |
|
29 const QString KXmlMaxVolume = "maxvolume"; |
|
30 const QString KXmlFrequencyStepSize = "FrequencyStepSize"; |
|
31 const QString KXmlRegion = "region"; |
|
32 const QString KXmlMinFrequency = "MinFrequency"; |
|
33 const QString KXmlMaxFrequency = "MaxFrequency"; |
|
34 |
|
35 const QString KXmlRadioStations = "RadioStations"; |
|
36 const QString KXmlSetting = "setting"; |
|
37 const QString KXmlStation = "Station"; |
|
38 const QString KXmlName = "name"; |
|
39 const QString KXmlValue = "value"; |
|
40 const QString KXmlFrequency = "frequency"; |
|
41 const QString KXmlBooleanTrue = "TRUE"; |
|
42 const QString KXmlRds = "Rds"; |
|
43 const QString KXmlRdsItem = "RdsItem"; |
|
44 const QString KXmlInterval = "SendInterval"; |
|
45 const QString KXmlType = "type"; |
|
46 const QString KXmlCount = "count"; |
|
47 const QString KXmlDelay = "delay"; |
|
48 |
|
49 const QString KXmlRdsGroup = "RdsGroup"; |
|
50 const QString KXmlRdsPsName = "PsName"; |
|
51 const QString KXmlRdsPiCode = "PiCode"; |
|
52 const QString KXmlRdsRt = "RT"; |
|
53 const QString KXmlRdsRtPlus = "RtPlus"; |
|
54 const QString KXmlRdsRtPlusItem = "RtPlusItem"; |
|
55 const QString KXmlRdsRtPlusTag = "RtPlusTag"; |
|
56 const QString KXmlRdsRtPlusTagDelay = "delay"; |
|
57 const QString KXmlRdsRtPlusTagArtist = "artist"; |
|
58 const QString KXmlRdsRtPlusTagTitle = "title"; |
|
59 const QString KXmlRdsPty = "Pty"; |
|
60 |
|
61 /*! |
|
62 * |
|
63 */ |
|
64 static bool isEqual( const QString& first, const QString& second ) |
|
65 { |
|
66 return first.compare( second, Qt::CaseInsensitive ) == 0; |
|
67 } |
|
68 |
|
69 |
|
70 /*! |
|
71 * |
|
72 */ |
|
73 T_RadioDataParser::T_RadioDataParser() : |
|
74 mReader( new QXmlSimpleReader() ) |
|
75 { |
|
76 } |
|
77 |
|
78 /*! |
|
79 * |
|
80 */ |
|
81 T_RadioDataParser::~T_RadioDataParser() |
|
82 { |
|
83 } |
|
84 |
|
85 /*! |
|
86 * |
|
87 */ |
|
88 bool T_RadioDataParser::parse() |
|
89 { |
|
90 QFile file( KDataFile ); |
|
91 if ( file.exists() ) { |
|
92 mSource.reset( new QXmlInputSource( &file ) ); |
|
93 mReader->setContentHandler( this );; |
|
94 mReader->setErrorHandler( this ); |
|
95 |
|
96 return mReader->parse( mSource.data() ); |
|
97 } else { |
|
98 mErrorString = QString( "Data file %1 not found!" ).arg( file.fileName() ); |
|
99 } |
|
100 return false; |
|
101 } |
|
102 |
|
103 /*! |
|
104 * \reimp |
|
105 */ |
|
106 bool T_RadioDataParser::startDocument() |
|
107 { |
|
108 return true; |
|
109 } |
|
110 |
|
111 /*! |
|
112 * \reimp |
|
113 */ |
|
114 bool T_RadioDataParser::endDocument() |
|
115 { |
|
116 return true; |
|
117 } |
|
118 |
|
119 /*! |
|
120 * \reimp |
|
121 */ |
|
122 bool T_RadioDataParser::startElement( const QString& namespaceURI, |
|
123 const QString& localName, |
|
124 const QString& qName, |
|
125 const QXmlAttributes& atts ) |
|
126 { |
|
127 if ( isEqual( localName, KXmlRadioSettings ) ) |
|
128 { |
|
129 handleStartRadioSettings( atts ); |
|
130 } |
|
131 else if ( isEqual( localName, KXmlSetting ) ) |
|
132 { |
|
133 handleStartSetting( atts ); |
|
134 } |
|
135 else if ( isEqual( localName, KXmlRadioStations ) ) |
|
136 { |
|
137 // handleStartRdsData( atts ); |
|
138 } |
|
139 else if ( isEqual( localName, KXmlStation ) ) |
|
140 { |
|
141 handleStartStation( atts ); |
|
142 } |
|
143 else if ( isEqual( localName, KXmlRdsGroup ) ) |
|
144 { |
|
145 handleStartRdsGroup( atts ); |
|
146 } |
|
147 else if ( isEqual( localName, KXmlRds ) ) |
|
148 { |
|
149 handleStartRdsData( atts ); |
|
150 } |
|
151 |
|
152 return true; |
|
153 } |
|
154 |
|
155 /*! |
|
156 * \reimp |
|
157 */ |
|
158 bool T_RadioDataParser::endElement( const QString& namespaceURI, const QString& localName, const QString& qName ) |
|
159 { |
|
160 if ( isEqual( localName, KXmlRadioSettings ) ) |
|
161 { |
|
162 handleEndRadioSettings(); |
|
163 } |
|
164 else if ( isEqual( localName, KXmlSetting ) ) |
|
165 { |
|
166 handleEndSetting(); |
|
167 } |
|
168 else if ( isEqual( localName, KXmlStation ) ) |
|
169 { |
|
170 handleEndStation(); |
|
171 } |
|
172 else if ( isEqual( localName, KXmlRds ) ) |
|
173 { |
|
174 handleEndRdsData(); |
|
175 } |
|
176 return true; |
|
177 } |
|
178 |
|
179 /*! |
|
180 * \reimp |
|
181 */ |
|
182 bool T_RadioDataParser::characters( const QString& ch ) |
|
183 { |
|
184 return true; |
|
185 } |
|
186 |
|
187 /*! |
|
188 * \reimp |
|
189 */ |
|
190 bool T_RadioDataParser::error( const QXmlParseException& exception ) |
|
191 { |
|
192 mErrorString = QString( "Line: %1, Column: %2, Msg: %3" ) |
|
193 .arg( exception.lineNumber() ) |
|
194 .arg( exception.columnNumber() ) |
|
195 .arg( exception.message() ); |
|
196 return true; |
|
197 } |
|
198 |
|
199 /*! |
|
200 * \reimp |
|
201 */ |
|
202 bool T_RadioDataParser::fatalError( const QXmlParseException& exception ) |
|
203 { |
|
204 mErrorString = QString( "Error in data XML. Line: %1, Column: %2, Msg: %3" ) |
|
205 .arg( exception.lineNumber() ) |
|
206 .arg( exception.columnNumber() ) |
|
207 .arg( exception.message() ); |
|
208 return false; |
|
209 } |
|
210 |
|
211 /*! |
|
212 * \reimp |
|
213 */ |
|
214 QString T_RadioDataParser::errorString() const |
|
215 { |
|
216 return mErrorString; |
|
217 } |
|
218 |
|
219 /*! |
|
220 * |
|
221 */ |
|
222 void T_RadioDataParser::handleStartRadioSettings( const QXmlAttributes& atts ) |
|
223 { |
|
224 Q_UNUSED( atts ); |
|
225 RadioData::EngineSettings* engineSettings = new RadioData::EngineSettings; |
|
226 pushToSettingStack( engineSettings, mSettingStack ); |
|
227 } |
|
228 |
|
229 /*! |
|
230 * |
|
231 */ |
|
232 void T_RadioDataParser::handleEndRadioSettings() |
|
233 { |
|
234 RadioData::EngineSettings* engineSettings |
|
235 = static_cast<RadioData::EngineSettings*>( popFromSettingStack( mSettingStack ) ); |
|
236 if ( engineSettings ) |
|
237 { |
|
238 mEngineSettings = *engineSettings; |
|
239 delete engineSettings; |
|
240 engineSettings = 0; |
|
241 } |
|
242 } |
|
243 |
|
244 /*! |
|
245 * |
|
246 */ |
|
247 void T_RadioDataParser::handleStartSetting( const QXmlAttributes& atts ) |
|
248 { |
|
249 RadioData::SettingHolder* holder = new RadioData::SettingHolder; |
|
250 |
|
251 RadioData::Setting* parentHolder = topOfSettingStack( mSettingHolderStack ); |
|
252 if ( parentHolder ) { |
|
253 static_cast<RadioData::SettingHolder*>( parentHolder )->mChildren.append( holder ); |
|
254 } |
|
255 |
|
256 pushToSettingStack( holder, mSettingHolderStack ); |
|
257 |
|
258 for ( int i = 0; i < atts.count(); ++i ) |
|
259 { |
|
260 QString attrName = atts.localName( i ); |
|
261 |
|
262 if ( isEqual( attrName, KXmlName ) ) |
|
263 { |
|
264 holder->mName = atts.value( i ); |
|
265 } |
|
266 else if ( isEqual( attrName, KXmlValue ) == 0 ) |
|
267 { |
|
268 holder->mValue = atts.value( i ); |
|
269 } |
|
270 } |
|
271 } |
|
272 |
|
273 /*! |
|
274 * |
|
275 */ |
|
276 void T_RadioDataParser::handleEndSetting() |
|
277 { |
|
278 QScopedPointer<RadioData::SettingHolder> holder( |
|
279 static_cast<RadioData::SettingHolder*>( popFromSettingStack( mSettingHolderStack ) ) ); |
|
280 if ( mSettingHolderStack.count() == 0 ) |
|
281 { |
|
282 if ( RadioData::Setting* setting = topOfSettingStack( mSettingStack ) ) |
|
283 { |
|
284 setting->setValue( *holder, *this ); |
|
285 } |
|
286 } |
|
287 } |
|
288 |
|
289 /*! |
|
290 * |
|
291 */ |
|
292 void T_RadioDataParser::handleStartStation( const QXmlAttributes& atts ) |
|
293 { |
|
294 RadioData::Station* station = new RadioData::Station; |
|
295 pushToSettingStack( station, mSettingStack ); |
|
296 } |
|
297 |
|
298 /*! |
|
299 * |
|
300 */ |
|
301 void T_RadioDataParser::handleEndStation() |
|
302 { |
|
303 RadioData::Station* station = static_cast<RadioData::Station*>( popFromSettingStack( mSettingStack ) ); |
|
304 mStations.append( station ); |
|
305 } |
|
306 |
|
307 /*! |
|
308 * |
|
309 */ |
|
310 void T_RadioDataParser::handleStartRdsGroup( const QXmlAttributes& atts ) |
|
311 { |
|
312 |
|
313 } |
|
314 |
|
315 /*! |
|
316 * |
|
317 */ |
|
318 void T_RadioDataParser::handleEndRdsGroup() |
|
319 { |
|
320 |
|
321 } |
|
322 |
|
323 /*! |
|
324 * |
|
325 */ |
|
326 void T_RadioDataParser::handleStartRdsData( const QXmlAttributes& atts ) |
|
327 { |
|
328 RadioData::RdsItem* rds = new RadioData::RdsItem; |
|
329 pushToSettingStack( rds, mSettingStack ); |
|
330 } |
|
331 |
|
332 /*! |
|
333 * |
|
334 */ |
|
335 void T_RadioDataParser::handleEndRdsData() |
|
336 { |
|
337 QScopedPointer<RadioData::RdsItem> rds( |
|
338 static_cast<RadioData::RdsItem*>( popFromSettingStack( mSettingStack ) ) ); |
|
339 RadioData::Station* station = dynamic_cast<RadioData::Station*>( topOfSettingStack( mSettingStack ) ); |
|
340 if ( rds && station ) |
|
341 { |
|
342 station->mRdsArray.append( rds.take() ); |
|
343 } |
|
344 } |
|
345 |
|
346 /*! |
|
347 * |
|
348 */ |
|
349 void T_RadioDataParser::pushToSettingStack( RadioData::Setting* setting, RadioData::SettingArray& array ) |
|
350 { |
|
351 array.append( setting ); |
|
352 } |
|
353 |
|
354 /*! |
|
355 * |
|
356 */ |
|
357 RadioData::Setting* T_RadioDataParser::topOfSettingStack( RadioData::SettingArray& array ) |
|
358 { |
|
359 if ( array.count() > 0 ) |
|
360 { |
|
361 return array[ array.count() - 1 ]; |
|
362 } |
|
363 |
|
364 return 0; |
|
365 } |
|
366 |
|
367 /*! |
|
368 * |
|
369 */ |
|
370 RadioData::Setting* T_RadioDataParser::popFromSettingStack( RadioData::SettingArray& array ) |
|
371 { |
|
372 const int lastIndex = array.count() - 1; |
|
373 RadioData::Setting* setting = array[lastIndex]; |
|
374 array.removeAt( lastIndex ); |
|
375 return setting; |
|
376 } |
|
377 |
|
378 /*! |
|
379 * |
|
380 */ |
|
381 int T_RadioDataParser::parseInt( const QString& string, int defaultVal ) |
|
382 { |
|
383 bool ok = false; |
|
384 int ret = string.toInt( &ok ); |
|
385 if ( ok ) { |
|
386 return ret; |
|
387 } |
|
388 return defaultVal; |
|
389 } |
|
390 |
|
391 /*! |
|
392 * |
|
393 */ |
|
394 int T_RadioDataParser::parseTime( const QString& string, int defaultVal ) |
|
395 { |
|
396 if ( isEqual( string.right( 2 ), KXmlMilliseconds ) ) { |
|
397 QString temp = string.mid( 0, string.length() - 2 ); |
|
398 return parseInt( temp, defaultVal ); |
|
399 } else if ( isEqual( string.right( 1 ), KXmlSeconds ) ) { |
|
400 QString temp = string.mid( 0, string.length() - 1 ); |
|
401 return parseInt( temp, defaultVal ) * KThousand; |
|
402 } else if ( isEqual( string.right( 1 ), KXmlMinutes ) ) { |
|
403 QString temp = string.mid( 0, string.length() - 1 ); |
|
404 return parseInt( temp, defaultVal ) * KMillion; |
|
405 } else { |
|
406 // Default time is seconds |
|
407 return parseInt( string, defaultVal ) * KThousand; |
|
408 } |
|
409 } |
|
410 |
|
411 |
|
412 RadioData::RdsGroup::RdsGroup( const QXmlAttributes& atts, T_RadioDataParser& parser ) : |
|
413 mInterval( 0 ), |
|
414 mCount( 0 ) |
|
415 { |
|
416 for ( int i = 0; i < atts.count(); ++i ) { |
|
417 QString attrName = atts.localName( i ); |
|
418 |
|
419 if ( isEqual( attrName, KXmlInterval ) ) |
|
420 { |
|
421 mInterval = parser.parseTime( atts.value( i ), 0 ); |
|
422 } |
|
423 else if ( isEqual( attrName, KXmlCount ) == 0 ) |
|
424 { |
|
425 mCount = parser.parseInt( atts.value( i ), 0 ); |
|
426 } |
|
427 } |
|
428 |
|
429 } |
|
430 |
|
431 RadioData::RdsGroup::~RdsGroup() |
|
432 { |
|
433 |
|
434 } |
|
435 |
|
436 RadioData::Station::Station() : |
|
437 mFrequency( 0 ) |
|
438 { |
|
439 } |
|
440 |
|
441 RadioData::Station::~Station() |
|
442 { |
|
443 qDeleteAll( mRdsArray ); |
|
444 mRdsArray.clear(); |
|
445 } |
|
446 |
|
447 void RadioData::Station::setValue( SettingHolder& holder, T_RadioDataParser& parser ) |
|
448 { |
|
449 if ( isEqual( holder.mName, KXmlFrequency ) ) |
|
450 { |
|
451 int valueInt = 0; |
|
452 parser.parseInt( holder.mValue, valueInt ); |
|
453 mFrequency = static_cast<uint>( valueInt ); |
|
454 } |
|
455 } |
|
456 |
|
457 |
|
458 RadioData::RdsItem::RdsItem() : |
|
459 mCurrentRtPlusIndex( 0 ), |
|
460 mType( RadioData::RdsItem::Unknown ) |
|
461 { |
|
462 } |
|
463 |
|
464 RadioData::RdsItem::RtPlusHolder::RtPlusHolder() : |
|
465 mDelay( 0 ), |
|
466 mRtClass( 0 ) |
|
467 { |
|
468 } |
|
469 |
|
470 RadioData::RdsItem::~RdsItem() |
|
471 { |
|
472 qDeleteAll( mRtPlusItems ); |
|
473 mRtPlusItems.clear(); |
|
474 } |
|
475 |
|
476 void RadioData::RdsItem::setValue( SettingHolder& holder, T_RadioDataParser& parser ) |
|
477 { |
|
478 if ( holder.mChildren.count() == 0 ) |
|
479 { |
|
480 if ( isEqual( holder.mName, KXmlInterval ) ) |
|
481 { |
|
482 mInterval = parser.parseInt( holder.mValue, 0 ); |
|
483 } |
|
484 else if ( isEqual( holder.mName, KXmlType ) ) |
|
485 { |
|
486 if ( isEqual( holder.mValue, KXmlRdsPsName ) ) |
|
487 { |
|
488 mType = RadioData::RdsItem::RdsPsName; |
|
489 } |
|
490 else if ( isEqual( holder.mValue, KXmlRdsRt ) ) |
|
491 { |
|
492 mType = RadioData::RdsItem::RdsRadioText; |
|
493 } |
|
494 else if ( isEqual( holder.mValue, KXmlRdsRt ) ) |
|
495 { |
|
496 mType = RadioData::RdsItem::RdsRadioText; |
|
497 } |
|
498 else if ( isEqual( holder.mValue, KXmlRdsRtPlus ) ) |
|
499 { |
|
500 mType = RadioData::RdsItem::RdsRadioTextPlus; |
|
501 } |
|
502 else if ( isEqual( holder.mValue, KXmlRdsPty ) ) |
|
503 { |
|
504 mType = RadioData::RdsItem::RdsPty; |
|
505 } |
|
506 if ( isEqual( holder.mValue, KXmlRdsPiCode ) ) |
|
507 { |
|
508 mType = RadioData::RdsItem::RdsPiCode; |
|
509 } |
|
510 } |
|
511 else if ( isEqual( holder.mName, KXmlRdsItem ) ) |
|
512 { |
|
513 mRdsItems.append( holder.mValue ); |
|
514 } |
|
515 } |
|
516 else |
|
517 { |
|
518 initChildren( holder, parser ); |
|
519 } |
|
520 } |
|
521 |
|
522 void RadioData::RdsItem::initChildren( SettingHolder& holder, T_RadioDataParser& parser ) |
|
523 { |
|
524 mRdsItems.append( holder.mValue ); |
|
525 |
|
526 for ( int i = 0; i < holder.mChildren.count(); ++i ) |
|
527 { |
|
528 RtPlusHolder* rtPlusHolder = new RtPlusHolder; |
|
529 mRtPlusItems.append( rtPlusHolder ); |
|
530 |
|
531 SettingHolder* child = holder.mChildren[i]; |
|
532 if ( isEqual( child->mName, KXmlRdsRtPlusTag ) ) |
|
533 { |
|
534 rtPlusHolder->mRtClass = RadioData::RdsItem::Title; |
|
535 if ( isEqual( child->mValue, KXmlRdsRtPlusTagArtist ) ) |
|
536 { |
|
537 rtPlusHolder->mRtClass = RadioData::RdsItem::Artist; |
|
538 } |
|
539 } |
|
540 |
|
541 for ( int j = 0; j < child->mChildren.count(); ++j ) |
|
542 { |
|
543 SettingHolder* grandChild = child->mChildren[j]; |
|
544 if ( isEqual( grandChild->mName, KXmlRdsRtPlusTagDelay ) ) |
|
545 { |
|
546 parser.parseInt( grandChild->mValue, rtPlusHolder->mDelay ); |
|
547 } |
|
548 else if ( isEqual( grandChild->mName, KXmlRdsItem ) ) |
|
549 { |
|
550 rtPlusHolder->mRtItem = grandChild->mValue; |
|
551 } |
|
552 } |
|
553 } |
|
554 } |
|
555 |
|
556 RadioData::SettingHolder::SettingHolder() |
|
557 { |
|
558 } |
|
559 |
|
560 RadioData::SettingHolder::~SettingHolder() |
|
561 { |
|
562 qDeleteAll( mChildren ); |
|
563 mChildren.clear(); |
|
564 } |
|
565 |
|
566 RadioData::EngineSettings::EngineSettings() : |
|
567 mMaxVolume( 0 ), |
|
568 mFrequencyStepSize( 0 ), |
|
569 mRegionId( 0 ), |
|
570 mMinFrequency( 0 ), |
|
571 mMaxFrequency( 0 ) |
|
572 { |
|
573 } |
|
574 |
|
575 void RadioData::EngineSettings::setValue( SettingHolder& holder, T_RadioDataParser& parser ) |
|
576 { |
|
577 if ( isEqual( holder.mName, KXmlMaxVolume ) ) |
|
578 { |
|
579 parser.parseInt( holder.mValue, mMaxVolume ); |
|
580 } |
|
581 else if ( isEqual( holder.mName, KXmlFrequencyStepSize ) ) |
|
582 { |
|
583 parser.parseInt( holder.mValue, mFrequencyStepSize ); |
|
584 } |
|
585 else if ( isEqual( holder.mName, KXmlRegion ) ) |
|
586 { |
|
587 parser.parseInt( holder.mValue, mRegionId ); |
|
588 } |
|
589 else if ( isEqual( holder.mName, KXmlMinFrequency ) ) |
|
590 { |
|
591 int valueInt = 0; |
|
592 parser.parseInt( holder.mValue, valueInt ); |
|
593 mMinFrequency = static_cast<uint>( valueInt ); |
|
594 } |
|
595 else if ( isEqual( holder.mName, KXmlMaxFrequency ) ) |
|
596 { |
|
597 int valueInt = 0; |
|
598 parser.parseInt( holder.mValue, valueInt ); |
|
599 mMaxFrequency = static_cast<uint>( valueInt ); |
|
600 } |
|
601 } |
|