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