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