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