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