author | hgs |
Tue, 05 Oct 2010 09:31:22 +0300 | |
changeset 54 | a8ba0c289b44 |
parent 51 | bbebb0235466 |
child 57 | 21be958eb3ce |
permissions | -rw-r--r-- |
24 | 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 |
// System includes |
|
19 |
#include <QStringList> |
|
20 |
||
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
21 |
// User includes |
24 | 22 |
#include "radiostationmodel.h" |
23 |
#include "radiostationmodel_p.h" |
|
24 |
#include "radiopresetstorage.h" |
|
25 |
#include "radioenginewrapper.h" |
|
26 |
#include "radiouiengine.h" |
|
27 |
#include "radiouiengine_p.h" |
|
28 |
#include "radiostation.h" |
|
29 |
#include "radiostation_p.h" |
|
30 |
#include "radiologger.h" |
|
31 |
||
32 |
/*! |
|
33 |
* |
|
34 |
*/ |
|
35 |
static QString parseLine( const RadioStation& station ) |
|
36 |
{ |
|
37 |
QString line = ""; |
|
38 |
||
39 |
QString name = station.name(); |
|
40 |
if ( !name.isEmpty() ) |
|
41 |
{ |
|
42 |
line.append( name.trimmed() ); |
|
47 | 43 |
} else { |
44 |
const QString parsedFrequency = qtTrId( "txt_rad_dblist_l1_mhz" ).arg( RadioStation::parseFrequency( station.frequency() ) ); |
|
45 |
line.append( parsedFrequency ); |
|
24 | 46 |
} |
47 |
||
48 |
LOG_FORMAT( "RadioStationModel: Returning line %s", GETSTRING(line) ); |
|
49 |
return line; |
|
50 |
} |
|
51 |
||
52 |
/*! |
|
53 |
* |
|
54 |
*/ |
|
55 |
RadioStationModel::RadioStationModel( RadioUiEnginePrivate& uiEngine ) : |
|
56 |
QAbstractListModel( &uiEngine.api() ), |
|
57 |
d_ptr( new RadioStationModelPrivate( this, uiEngine ) ) |
|
58 |
{ |
|
59 |
} |
|
60 |
||
61 |
/*! |
|
62 |
* |
|
63 |
*/ |
|
64 |
RadioStationModel::~RadioStationModel() |
|
65 |
{ |
|
66 |
} |
|
67 |
||
68 |
/*! |
|
69 |
* |
|
70 |
*/ |
|
71 |
Qt::ItemFlags RadioStationModel::flags ( const QModelIndex& index ) const |
|
72 |
{ |
|
73 |
Qt::ItemFlags flags = QAbstractListModel::flags( index ); |
|
74 |
flags |= Qt::ItemIsEditable; |
|
75 |
return flags; |
|
76 |
} |
|
77 |
||
78 |
/*! |
|
79 |
* |
|
80 |
*/ |
|
81 |
int RadioStationModel::rowCount( const QModelIndex& parent ) const |
|
82 |
{ |
|
83 |
Q_UNUSED( parent ); |
|
84 |
Q_D( const RadioStationModel ); |
|
85 |
const int count = d->mStations.keys().count(); |
|
86 |
return count; |
|
87 |
} |
|
88 |
||
89 |
/*! |
|
90 |
* Checks the given station and emits signals based on what member variables had been changed |
|
91 |
*/ |
|
92 |
QVariant RadioStationModel::data( const QModelIndex& index, int role ) const |
|
93 |
{ |
|
94 |
if ( !index.isValid() ) { |
|
95 |
return QVariant(); |
|
96 |
} |
|
97 |
||
98 |
Q_D( const RadioStationModel ); |
|
99 |
if ( role == Qt::DisplayRole ) { |
|
100 |
RadioStation station = stationAt( index.row() ); |
|
101 |
QString firstLine = parseLine( station ); |
|
47 | 102 |
QString name = station.name(); |
103 |
||
104 |
if ( !name.isEmpty() ) { |
|
105 |
if ( currentStation().frequency() == station.frequency() ) { |
|
106 |
if ( d->mDetailLevel.testFlag( RadioStationModel::ShowGenre ) ) { |
|
107 |
QStringList list; |
|
108 |
list.append( firstLine ); |
|
109 |
QString genre = " "; // Empty space so that the listbox generates the second row |
|
110 |
if ( station.genre() != -1 ) { |
|
111 |
genre = d->mUiEngine.api().genreToString( station.genre(), GenreTarget::StationsList ); |
|
112 |
} |
|
113 |
list.append( genre ); |
|
114 |
||
115 |
return list; |
|
116 |
} |
|
117 |
} else { |
|
118 |
QStringList list; |
|
119 |
list.append( firstLine ); |
|
51 | 120 |
list.append( qtTrId( "txt_rad_dblist_l1_mhz2" ).arg( RadioStation::parseFrequency( station.frequency() ) ) ); |
47 | 121 |
return list; |
24 | 122 |
} |
47 | 123 |
} else { |
124 |
if ( currentStation().frequency() != station.frequency() ) { |
|
125 |
QStringList list; |
|
126 |
list.append( firstLine ); |
|
51 | 127 |
list.append( " " ); |
47 | 128 |
return list; |
129 |
} else { |
|
130 |
QStringList list; |
|
131 |
list.append( firstLine ); |
|
132 |
QString genre = " "; // Empty space so that the listbox generates the second row |
|
133 |
if ( station.genre() != -1 ) { |
|
134 |
genre = d->mUiEngine.api().genreToString( station.genre(), GenreTarget::StationsList ); |
|
135 |
} |
|
136 |
list.append( genre ); |
|
137 |
return list; |
|
138 |
} |
|
24 | 139 |
} |
140 |
||
141 |
return firstLine; |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
142 |
} else if ( role == RadioRole::RadioStationRole ) { |
24 | 143 |
QVariant variant; |
144 |
variant.setValue( stationAt( index.row() ) ); |
|
145 |
return variant; |
|
146 |
} else if ( role == Qt::DecorationRole && |
|
147 |
d->mDetailLevel.testFlag( RadioStationModel::ShowIcons ) ) { |
|
148 |
RadioStation station = stationAt( index.row() ); |
|
149 |
QVariantList list; |
|
150 |
if ( station.isFavorite() && !d->mFavoriteIcon.isNull() ) { |
|
151 |
list.append( d->mFavoriteIcon ); |
|
51 | 152 |
} else if ( !station.isFavorite() && !d->mNonFavoriteIcon.isNull() ) { |
153 |
list.append( d->mNonFavoriteIcon ); |
|
154 |
}else { |
|
24 | 155 |
list.append( QIcon() ); |
156 |
} |
|
157 |
if ( currentStation().frequency() == station.frequency() && !d->mNowPlayingIcon.isNull() ) { |
|
158 |
list.append( d->mNowPlayingIcon ); |
|
159 |
} |
|
160 |
return list; |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
161 |
} else if ( role == RadioRole::IsFavoriteRole ) { |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
162 |
QVariant variant; |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
163 |
variant.setValue( stationAt( index.row() ).isFavorite() ); |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
164 |
return variant; |
24 | 165 |
} |
166 |
||
167 |
return QVariant(); |
|
168 |
} |
|
169 |
||
170 |
/*! |
|
171 |
* Checks the given station and emits signals based on what member variables had been changed |
|
172 |
*/ |
|
173 |
bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role ) |
|
174 |
{ |
|
175 |
Q_UNUSED( index ); |
|
176 |
||
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
177 |
if ( role == RadioRole::ToggleFavoriteRole ) { |
24 | 178 |
const uint frequency = value.toUInt(); |
179 |
RadioStation station; |
|
180 |
if ( findFrequency( frequency, station ) ) { |
|
181 |
setFavoriteByPreset( station.presetIndex(), !station.isFavorite() ); |
|
182 |
} else { |
|
183 |
setFavoriteByFrequency( frequency, true ); |
|
184 |
} |
|
185 |
||
186 |
return true; |
|
187 |
} |
|
188 |
||
189 |
return false; |
|
190 |
} |
|
191 |
||
192 |
/*! |
|
193 |
* Called by the engine to initialize the list with given amount of presets |
|
194 |
*/ |
|
195 |
void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper ) |
|
196 |
{ |
|
197 |
Q_D( RadioStationModel ); |
|
198 |
d->mPresetStorage = storage; |
|
199 |
d->mWrapper = wrapper; |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
200 |
|
24 | 201 |
int index = d->mPresetStorage->firstPreset(); |
34 | 202 |
LOG_FORMAT( "RadioStationModel::initialize: presetCount: %d, firstIndex: %d", |
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
203 |
d->mPresetStorage->presetCount(), index ); |
24 | 204 |
|
205 |
while ( index >= 0 ) { |
|
206 |
RadioStation station; |
|
207 |
||
34 | 208 |
RadioStationIf* stationInterface = static_cast<RadioStationIf*>( station.data_ptr() ); |
209 |
if ( d->mPresetStorage->readPreset( index, *stationInterface ) ) { |
|
210 |
if ( station.isValid() && d->mWrapper->isFrequencyValid( station.frequency() ) ) { |
|
36 | 211 |
|
38 | 212 |
#ifdef INIT_STATIONS_WITH_DUMMY_RT |
213 |
station.setGenre( GenreEurope::RdsChildrensProgrammes ); |
|
214 |
if ( index % 3 == 0 ) { |
|
215 |
station.setName( "Radio Rock" ); |
|
216 |
station.setRadioText( "Now playing: <font color='cyan'>The Presidents of the United States of America</font> - <font color='cyan'>Dune Buggy and diipa daapa jhkjhui erjlkej rewjtl</font>" ); |
|
217 |
} else if ( index % 2 == 0 ) { |
|
218 |
station.setName( "Radio Rock" ); |
|
219 |
} else { |
|
220 |
station.setDynamicPsText( "DYN PS" ); |
|
221 |
} |
|
222 |
#endif // INIT_STATIONS_WITH_DUMMY_RT |
|
223 |
||
36 | 224 |
// Check if the station seems to send RDS or not. |
225 |
// Note that radiotext is not checked because it is not saved to cenrep |
|
226 |
// TODO: Consider saving this state flag to cenrep |
|
227 |
if ( ( station.hasName() && !station.isRenamed() ) || station.hasUrl() ) { |
|
228 |
static_cast<RadioStationIf*>( station.data_ptr() )->setStationHasSentRds( true ); |
|
229 |
} |
|
230 |
||
24 | 231 |
d->mStations.insert( station.frequency(), station ); |
232 |
} else { |
|
34 | 233 |
LOG( "RadioStationModel::initialize: Invalid station!" ); |
234 |
LOG_FORMAT( "Invalid station freq: %d", station.frequency() ); |
|
24 | 235 |
} |
236 |
} |
|
237 |
||
238 |
index = d->mPresetStorage->nextPreset( index ); |
|
239 |
} |
|
240 |
||
241 |
d->setCurrentStation( d->mWrapper->currentFrequency() ); |
|
242 |
||
243 |
wrapper->addObserver( d ); |
|
244 |
} |
|
245 |
||
246 |
/*! |
|
247 |
* Sets the icons to be used in the lists |
|
248 |
*/ |
|
51 | 249 |
void RadioStationModel::setIcons( const QIcon& favoriteIcon, const QIcon& nonFavoriteIcon, const QIcon& nowPlayingIcon ) |
24 | 250 |
{ |
251 |
Q_D( RadioStationModel ); |
|
252 |
d->mFavoriteIcon = favoriteIcon; |
|
51 | 253 |
d->mNonFavoriteIcon = nonFavoriteIcon; |
24 | 254 |
d->mNowPlayingIcon = nowPlayingIcon; |
255 |
} |
|
256 |
||
257 |
/*! |
|
258 |
* Returns a reference to the station handler interface |
|
259 |
*/ |
|
260 |
RadioStationHandlerIf& RadioStationModel::stationHandlerIf() |
|
261 |
{ |
|
262 |
Q_D( RadioStationModel ); |
|
263 |
return *d; |
|
264 |
} |
|
265 |
||
266 |
/*! |
|
267 |
* Returns a reference to the underlying QList so that it can be easily looped |
|
268 |
*/ |
|
269 |
const Stations& RadioStationModel::list() const |
|
270 |
{ |
|
271 |
Q_D( const RadioStationModel ); |
|
272 |
return d->mStations; |
|
273 |
} |
|
274 |
||
275 |
/*! |
|
276 |
* Returns the station at the given index. |
|
277 |
*/ |
|
278 |
RadioStation RadioStationModel::stationAt( int index ) const |
|
279 |
{ |
|
280 |
// Get the value from the keys list instead of directly accessing the values list |
|
281 |
// because QMap may have added a default-constructed value to the values list |
|
282 |
Q_D( const RadioStationModel ); |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
283 |
if ( index >= 0 && index < d->mStations.keys().count() ) { |
24 | 284 |
uint frequency = d->mStations.keys().at( index ); |
285 |
return d->mStations.value( frequency ); |
|
286 |
} |
|
287 |
return RadioStation(); |
|
288 |
} |
|
289 |
||
290 |
/*! |
|
291 |
* Finds a station by frequency |
|
292 |
*/ |
|
34 | 293 |
bool RadioStationModel::findFrequency( uint frequency, RadioStation& station, FindCriteria::Criteria criteria ) const |
24 | 294 |
{ |
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
295 |
Q_D( const RadioStationModel ); |
34 | 296 |
|
297 |
if ( criteria == FindCriteria::IncludeManualStation && d->mCurrentStation->frequency() == frequency ) { |
|
298 |
station = *d->mCurrentStation; |
|
299 |
return true; |
|
300 |
} |
|
301 |
||
24 | 302 |
if ( d->mStations.contains( frequency ) ) { |
303 |
station = d->mStations.value( frequency ); |
|
304 |
return true; |
|
305 |
} |
|
306 |
return false; |
|
307 |
} |
|
308 |
||
309 |
/*! |
|
34 | 310 |
* Convenience function to find a radio station. |
311 |
*/ |
|
312 |
RadioStation RadioStationModel::findStation( uint frequency, FindCriteria::Criteria criteria ) const |
|
313 |
{ |
|
314 |
RadioStation station; |
|
315 |
findFrequency( frequency, station, criteria ); // Return value ignored |
|
316 |
return station; |
|
317 |
} |
|
318 |
||
319 |
/*! |
|
24 | 320 |
* Finds a station by preset index |
321 |
*/ |
|
322 |
int RadioStationModel::findPresetIndex( int presetIndex ) |
|
323 |
{ |
|
324 |
Q_D( RadioStationModel ); |
|
325 |
int index = 0; |
|
326 |
foreach( const RadioStation& tempStation, d->mStations ) { |
|
327 |
if ( tempStation.presetIndex() == presetIndex ) { |
|
328 |
return index; |
|
329 |
} |
|
330 |
++index; |
|
331 |
} |
|
332 |
||
333 |
return RadioStation::NotFound; |
|
334 |
} |
|
335 |
||
336 |
/*! |
|
337 |
* Finds a station by preset index |
|
338 |
*/ |
|
339 |
int RadioStationModel::findPresetIndex( int presetIndex, RadioStation& station ) |
|
340 |
{ |
|
341 |
Q_D( RadioStationModel ); |
|
342 |
const int index = findPresetIndex( presetIndex ); |
|
343 |
if ( index != RadioStation::NotFound ) { |
|
344 |
station = d->mStations.values().at( index ); |
|
345 |
} |
|
346 |
return index; |
|
347 |
} |
|
348 |
||
349 |
/*! |
|
350 |
* Finds the closest station from the given frequency |
|
351 |
*/ |
|
352 |
RadioStation RadioStationModel::findClosest( const uint frequency, StationSkip::Mode mode ) |
|
353 |
{ |
|
354 |
Q_D( RadioStationModel ); |
|
355 |
const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite; |
|
356 |
const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite; |
|
357 |
QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values(); |
|
358 |
||
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
359 |
if ( list.isEmpty() ) { |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
360 |
return RadioStation(); |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
361 |
} |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
362 |
|
24 | 363 |
// Find the previous and next station from current frequency |
364 |
RadioStation previous; |
|
365 |
RadioStation next; |
|
366 |
foreach( const RadioStation& station, list ) { |
|
367 |
const uint testFreq = station.frequency(); |
|
368 |
if ( testFreq == frequency ) { |
|
369 |
continue; |
|
370 |
} |
|
371 |
||
372 |
if ( testFreq > frequency ) { |
|
373 |
next = station; |
|
374 |
break; |
|
375 |
} |
|
376 |
previous = station; |
|
377 |
} |
|
378 |
||
379 |
// Check if we need to loop around |
|
380 |
if ( findNext && !next.isValid() ) { |
|
381 |
next = list.first(); |
|
382 |
} else if ( !findNext && !previous.isValid() ) { |
|
383 |
previous = list.last(); |
|
384 |
} |
|
385 |
||
386 |
return findNext ? next : previous; |
|
387 |
} |
|
388 |
||
389 |
/*! |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
390 |
* Checks if the model contains the given frequency |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
391 |
*/ |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
392 |
bool RadioStationModel::contains( const uint frequency ) const |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
393 |
{ |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
394 |
RadioStation unused; |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
395 |
return findFrequency( frequency, unused ); |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
396 |
} |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
397 |
|
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
398 |
/*! |
24 | 399 |
* Removes a station by frequency |
400 |
*/ |
|
401 |
void RadioStationModel::removeByFrequency( uint frequency ) |
|
402 |
{ |
|
403 |
RadioStation station; |
|
404 |
if ( findFrequency( frequency, station ) ) { |
|
405 |
removeStation( station ); |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
/*! |
|
410 |
* Removes a station by preset index |
|
411 |
*/ |
|
412 |
void RadioStationModel::removeByPresetIndex( int presetIndex ) |
|
413 |
{ |
|
414 |
RadioStation station; |
|
415 |
const int index = findPresetIndex( presetIndex, station ); |
|
416 |
if ( index >= 0 ) { |
|
417 |
removeStation( station ); |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
/*! |
|
422 |
* Removes the given station |
|
423 |
*/ |
|
424 |
void RadioStationModel::removeStation( const RadioStation& station ) |
|
425 |
{ |
|
426 |
Q_D( RadioStationModel ); |
|
427 |
const uint frequency = station.frequency(); |
|
428 |
if ( d->mStations.contains( frequency ) ) { |
|
429 |
||
430 |
// If we are removing the current station, copy its data to the current station pointer |
|
431 |
// to keep all of the received RDS data still available. They will be discarded when |
|
432 |
// the user tunes to another frequency, but they are available if the user decides to add it back. |
|
433 |
if ( d->mCurrentStation->frequency() == frequency ) { |
|
434 |
*d->mCurrentStation = station; |
|
435 |
} |
|
436 |
||
437 |
// Copy the station to a temporary variable that can be used as signal parameter |
|
438 |
RadioStation tempStation = station; |
|
439 |
||
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
440 |
const int row = indexFromFrequency( tempStation.frequency() ); |
24 | 441 |
beginRemoveRows( QModelIndex(), row, row ); |
442 |
||
443 |
d->mPresetStorage->deletePreset( tempStation.presetIndex() ); |
|
444 |
d->mStations.remove( frequency ); |
|
445 |
||
446 |
d->mCurrentStation = NULL; |
|
447 |
d->setCurrentStation( d->mWrapper->currentFrequency() ); |
|
448 |
||
449 |
endRemoveRows(); |
|
450 |
} |
|
451 |
} |
|
54 | 452 |
/*! |
453 |
* Removes stations based on model indices |
|
454 |
*/ |
|
455 |
void RadioStationModel::removeByModelIndices( QModelIndexList& indices, bool removefavorite ) |
|
456 |
{ |
|
457 |
// List needs to be sorted and indices needs to go throught from largest to smallest. |
|
458 |
// This is for keeping QmodelIndexing in sync after begin- and endremoverows, which |
|
459 |
// are needed for each item separately |
|
460 |
qSort( indices ); |
|
461 |
QModelIndexList::const_iterator iter = indices.constEnd(); |
|
462 |
QModelIndexList::const_iterator begin = indices.constBegin(); |
|
463 |
RadioStation station; |
|
464 |
while( iter != begin ) { |
|
465 |
iter--; |
|
466 |
station = stationAt( (*iter).row() ); |
|
467 |
if( removefavorite ) { |
|
468 |
setFavoriteByPreset( station.presetIndex(), false ); |
|
469 |
} else { |
|
470 |
removeStation( station ); |
|
471 |
} |
|
472 |
} |
|
473 |
} |
|
24 | 474 |
|
475 |
/*! |
|
476 |
* Public slot |
|
477 |
* Removes all stations |
|
478 |
*/ |
|
479 |
void RadioStationModel::removeAll( RemoveMode mode ) |
|
480 |
{ |
|
481 |
Q_D( RadioStationModel ); |
|
482 |
if ( d->mStations.count() == 0 ) { |
|
483 |
return; |
|
484 |
} |
|
485 |
||
486 |
if ( mode == RemoveAll ) { |
|
487 |
beginRemoveRows( QModelIndex(), 0, rowCount() - 1 ); |
|
488 |
||
489 |
// Preset utility deletes all presets with index -1 |
|
490 |
bool success = d->mPresetStorage->deletePreset( -1 ); |
|
491 |
Q_UNUSED( success ); |
|
492 |
RADIO_ASSERT( success, "FMRadio", "Failed to remove station" ); |
|
493 |
||
494 |
d->mStations.clear(); |
|
495 |
d->mCurrentStation = NULL; |
|
496 |
d->setCurrentStation( d->mWrapper->currentFrequency() ); |
|
497 |
||
498 |
endRemoveRows(); |
|
499 |
} else { |
|
500 |
foreach( const RadioStation& station, d->mStations ) { |
|
501 |
||
502 |
if ( mode == RemoveLocalStations ) { |
|
503 |
if ( station.isType( RadioStation::LocalStation ) && !station.isFavorite() ) { |
|
504 |
removeStation( station ); |
|
505 |
} |
|
506 |
} else { |
|
507 |
if ( station.isFavorite() ) { |
|
508 |
RadioStation newStation( station ); |
|
509 |
newStation.setFavorite( false ); |
|
510 |
saveStation( newStation ); |
|
511 |
} |
|
512 |
} |
|
513 |
} |
|
514 |
} |
|
515 |
} |
|
516 |
||
517 |
/*! |
|
518 |
* Adds a new station to the list |
|
519 |
*/ |
|
520 |
void RadioStationModel::addStation( const RadioStation& station ) |
|
521 |
{ |
|
522 |
Q_D( RadioStationModel ); |
|
523 |
const int newIndex = findUnusedPresetIndex(); |
|
34 | 524 |
LOG_FORMAT( "RadioStationModel::addStation: Adding station to index %d", newIndex ); |
24 | 525 |
|
526 |
RadioStation newStation = station; |
|
527 |
newStation.setPresetIndex( newIndex ); |
|
34 | 528 |
newStation.unsetType( RadioStation::ManualStation ); |
24 | 529 |
|
530 |
// We have to call beginInsertRows() BEFORE the addition is actually done so we must figure out where |
|
531 |
// the new station will go in the sorted frequency order |
|
532 |
int row = 0; |
|
533 |
const int count = rowCount(); |
|
534 |
if ( count > 1 ) { |
|
535 |
Stations::const_iterator iter = d->mStations.upperBound( newStation.frequency() ); |
|
536 |
if ( d->mStations.contains( iter.key() ) ) { |
|
537 |
row = d->mStations.keys().indexOf( iter.key() ); |
|
538 |
} else { |
|
539 |
row = count; |
|
540 |
} |
|
541 |
} else if ( count == 1 ) { |
|
542 |
uint existingFreq = d->mStations.keys().first(); |
|
543 |
if ( station.frequency() > existingFreq ) { |
|
544 |
row = 1; |
|
545 |
} |
|
546 |
} |
|
547 |
||
548 |
beginInsertRows( QModelIndex(), row, row ); |
|
549 |
||
550 |
d->doSaveStation( newStation ); |
|
551 |
||
552 |
d->setCurrentStation( d->mWrapper->currentFrequency() ); |
|
553 |
||
554 |
endInsertRows(); |
|
555 |
||
34 | 556 |
// Not all UI components listen to rowsInserted() signal so emit the favorite signal |
557 |
if ( newStation.isFavorite() ) { |
|
558 |
emit favoriteChanged( *d->mCurrentStation ); |
|
559 |
} |
|
24 | 560 |
} |
561 |
||
562 |
/*! |
|
563 |
* Saves the given station. It is expected to already exist in the list |
|
564 |
*/ |
|
565 |
void RadioStationModel::saveStation( RadioStation& station ) |
|
566 |
{ |
|
567 |
Q_D( RadioStationModel ); |
|
568 |
const bool stationHasChanged = station.hasChanged(); |
|
569 |
RadioStation::Change changeFlags = station.changeFlags(); |
|
570 |
station.resetChangeFlags(); |
|
571 |
||
34 | 572 |
if ( station.isType( RadioStation::ManualStation ) ) { |
24 | 573 |
|
34 | 574 |
d->mManualStation = station; |
24 | 575 |
emitChangeSignals( station, changeFlags ); |
576 |
||
577 |
} else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) { |
|
578 |
||
579 |
d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) ); |
|
580 |
d->setCurrentStation( d->mWrapper->currentFrequency() ); |
|
581 |
||
582 |
emitChangeSignals( station, changeFlags ); |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
/*! |
|
587 |
* Finds number of favorite stations |
|
588 |
*/ |
|
589 |
int RadioStationModel::favoriteCount() |
|
590 |
{ |
|
591 |
Q_D( const RadioStationModel ); |
|
592 |
return d->favorites().count(); |
|
593 |
} |
|
594 |
||
595 |
/*! |
|
47 | 596 |
* Finds number of local stations |
597 |
*/ |
|
598 |
int RadioStationModel::localCount() |
|
599 |
{ |
|
600 |
Q_D( const RadioStationModel ); |
|
601 |
return d->locals().count(); |
|
602 |
} |
|
603 |
||
604 |
/*! |
|
24 | 605 |
* Changes the favorite status of a station by its frequency. If the station does |
606 |
* not yet exist, it is added. |
|
607 |
*/ |
|
608 |
void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite ) |
|
609 |
{ |
|
610 |
Q_D( RadioStationModel ); |
|
611 |
if ( d->mWrapper->isFrequencyValid( frequency ) ) { |
|
34 | 612 |
LOG_FORMAT( "RadioStationModel::setFavoriteByFrequency, frequency: %d", frequency ); |
24 | 613 |
RadioStation station; |
614 |
if ( findFrequency( frequency, station ) ) { // Update existing preset |
|
615 |
if ( station.isFavorite() != favorite ) { |
|
616 |
station.setFavorite( favorite ); |
|
617 |
saveStation( station ); |
|
618 |
} |
|
619 |
} else if ( favorite ) { // Add new preset if setting as favorite |
|
620 |
RadioStation newStation; |
|
621 |
if ( d->mCurrentStation->frequency() == frequency ) { |
|
622 |
newStation = *d->mCurrentStation; |
|
623 |
} else { |
|
624 |
LOG( "CurrentStation frequency mismatch!" ); |
|
625 |
newStation.setFrequency( frequency ); |
|
626 |
} |
|
627 |
||
628 |
newStation.setType( RadioStation::LocalStation | RadioStation::Favorite ); |
|
629 |
||
630 |
// Emit the signals only after adding the preset and reinitializing the current station |
|
631 |
// because the UI will probably query the current station in its slots that get called. |
|
632 |
addStation( newStation ); |
|
633 |
} |
|
634 |
} |
|
635 |
} |
|
636 |
||
637 |
/*! |
|
638 |
* Changes the favorite status of a station by its preset index |
|
639 |
*/ |
|
640 |
void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite ) |
|
641 |
{ |
|
34 | 642 |
LOG_FORMAT( "RadioStationModel::setFavoriteByPreset, presetIndex: %d", presetIndex ); |
24 | 643 |
RadioStation station; |
644 |
if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) { |
|
645 |
station.setFavorite( favorite ); |
|
646 |
saveStation( station ); |
|
647 |
} |
|
648 |
} |
|
649 |
||
650 |
/*! |
|
651 |
* Renames a station by its preset index |
|
652 |
*/ |
|
653 |
void RadioStationModel::renameStation( int presetIndex, const QString& name ) |
|
654 |
{ |
|
34 | 655 |
LOG_FORMAT( "RadioStationModel::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) ); |
24 | 656 |
RadioStation station; |
657 |
if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) { |
|
47 | 658 |
station.setUserDefinedName( name.left(15) ); // Only 15 characters allowed |
24 | 659 |
saveStation( station ); |
660 |
} |
|
661 |
} |
|
662 |
||
663 |
/*! |
|
664 |
* |
|
665 |
*/ |
|
666 |
void RadioStationModel::setFavorites( const QModelIndexList& favorites ) |
|
667 |
{ |
|
668 |
foreach ( const QModelIndex& index, favorites ) { |
|
669 |
RadioStation station = stationAt( index.row() ); |
|
670 |
RADIO_ASSERT( station.isValid() , "RadioStationModel::setFavorites", "invalid RadioStation"); |
|
671 |
setFavoriteByPreset( station.presetIndex(), true ); |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
/*! |
|
676 |
* Returns the currently tuned station |
|
677 |
*/ |
|
678 |
RadioStation& RadioStationModel::currentStation() |
|
679 |
{ |
|
680 |
Q_D( RadioStationModel ); |
|
681 |
return *d->mCurrentStation; |
|
682 |
} |
|
683 |
||
684 |
/*! |
|
685 |
* Returns the currently tuned station |
|
686 |
*/ |
|
687 |
const RadioStation& RadioStationModel::currentStation() const |
|
688 |
{ |
|
689 |
Q_D( const RadioStationModel ); |
|
690 |
return *d->mCurrentStation; |
|
691 |
} |
|
692 |
||
693 |
/*! |
|
694 |
* Sets the model detail level |
|
695 |
*/ |
|
696 |
void RadioStationModel::setDetail( Detail level ) |
|
697 |
{ |
|
698 |
Q_D( RadioStationModel ); |
|
699 |
d->mDetailLevel = level; |
|
700 |
} |
|
701 |
||
702 |
/*! |
|
703 |
* Returns a list of radio stations in the given frequency range |
|
704 |
*/ |
|
705 |
QList<RadioStation> RadioStationModel::stationsInRange( uint minFrequency, uint maxFrequency ) |
|
706 |
{ |
|
707 |
Q_D( RadioStationModel ); |
|
708 |
QList<RadioStation> stations; |
|
709 |
foreach( const RadioStation& station, d->mStations ) { |
|
710 |
if ( station.frequency() >= minFrequency && station.frequency() <= maxFrequency ) { |
|
711 |
stations.append( station ); |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
return stations; |
|
716 |
} |
|
717 |
||
718 |
/*! |
|
719 |
* Returns the model index corresponding to the given frequency |
|
720 |
*/ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
721 |
int RadioStationModel::indexFromFrequency( uint frequency ) |
24 | 722 |
{ |
723 |
RadioStation station; |
|
724 |
if ( findFrequency( frequency, station ) ) { |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
725 |
return findPresetIndex( station.presetIndex() ); |
24 | 726 |
} |
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
727 |
return -1; |
24 | 728 |
} |
729 |
||
730 |
/*! |
|
731 |
* Private slot |
|
732 |
* Timer timeout slot to indicate that the dynamic PS check has ended |
|
733 |
*/ |
|
734 |
void RadioStationModel::dynamicPsCheckEnded() |
|
735 |
{ |
|
736 |
Q_D( RadioStationModel ); |
|
737 |
LOG_TIMESTAMP( "Finished dynamic PS check." ); |
|
47 | 738 |
LOG("RadioStationModel::dynamicPsCheckEnded"); |
24 | 739 |
if ( d->mCurrentStation->psType() != RadioStation::Dynamic && !d->mCurrentStation->dynamicPsText().isEmpty() ) |
740 |
{ |
|
741 |
d->mCurrentStation->setPsType( RadioStation::Static ); |
|
742 |
d->mCurrentStation->setName( d->mCurrentStation->dynamicPsText() ); |
|
743 |
d->mCurrentStation->setDynamicPsText( "" ); |
|
744 |
saveStation( *d->mCurrentStation ); |
|
745 |
} |
|
746 |
} |
|
747 |
||
748 |
/*! |
|
749 |
* Checks the given station and emits signals based on what member variables had been changed |
|
750 |
*/ |
|
751 |
void RadioStationModel::emitChangeSignals( const RadioStation& station, RadioStation::Change flags ) |
|
752 |
{ |
|
753 |
if ( flags.testFlag( RadioStation::NameChanged ) || |
|
754 |
flags.testFlag( RadioStation::GenreChanged ) || |
|
755 |
flags.testFlag( RadioStation::UrlChanged ) || |
|
756 |
flags.testFlag( RadioStation::TypeChanged ) || |
|
757 |
flags.testFlag( RadioStation::PiCodeChanged ) ) { |
|
758 |
||
759 |
// Create a temporary RadioStation for the duration of the signal-slot processing |
|
760 |
// The receivers can ask the station what data has changed and update accordingly |
|
761 |
RadioStation tempStation( station ); |
|
762 |
tempStation.setChangeFlags( flags ); |
|
763 |
emit stationDataChanged( tempStation ); |
|
764 |
||
765 |
emitDataChanged( tempStation ); |
|
766 |
} |
|
767 |
||
768 |
if ( flags.testFlag( RadioStation::RadioTextChanged ) ) { |
|
769 |
emit radioTextReceived( station ); |
|
770 |
emitDataChanged( station ); |
|
771 |
} |
|
772 |
||
773 |
if ( flags.testFlag( RadioStation::DynamicPsChanged ) ) { |
|
774 |
emit dynamicPsChanged( station ); |
|
775 |
emitDataChanged( station ); |
|
776 |
} |
|
777 |
||
778 |
if ( flags.testFlag( RadioStation::FavoriteChanged ) && station.isValid() ) { |
|
779 |
emit favoriteChanged( station ); |
|
780 |
emitDataChanged( station ); |
|
781 |
} |
|
782 |
} |
|
783 |
||
784 |
/*! |
|
785 |
* |
|
786 |
*/ |
|
787 |
void RadioStationModel::emitDataChanged( const RadioStation& station ) |
|
788 |
{ |
|
789 |
const int row = findPresetIndex( station.presetIndex() ); |
|
790 |
QModelIndex top = index( row, 0, QModelIndex() ); |
|
791 |
QModelIndex bottom = index( row, 0, QModelIndex() ); |
|
792 |
emit dataChanged( top, bottom ); |
|
793 |
} |
|
794 |
||
795 |
/*! |
|
796 |
* Finds an unused preset index |
|
797 |
*/ |
|
798 |
int RadioStationModel::findUnusedPresetIndex() |
|
799 |
{ |
|
800 |
Q_D( RadioStationModel ); |
|
801 |
QList<int> indexes; |
|
802 |
foreach( const RadioStation& station, d->mStations ) { |
|
803 |
if ( station.isValid() ) { |
|
804 |
indexes.append( station.presetIndex() ); |
|
805 |
} |
|
806 |
} |
|
807 |
||
808 |
int index = 0; |
|
809 |
for ( ; indexes.contains( index ); ++index ) { |
|
810 |
// Nothing to do here |
|
811 |
} |
|
812 |
||
34 | 813 |
LOG_FORMAT( "RadioStationModel::findUnusedPresetIndex, index: %d", index ); |
24 | 814 |
return index; |
815 |
} |