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 <QString> |
|
20 #include <QSettings> |
|
21 #include <QDataStream> |
|
22 |
|
23 // User includes |
|
24 #include "radiopresetstorage.h" |
|
25 #include "radiopresetstorage_win32_p.h" |
|
26 #include "radiostationif.h" |
|
27 |
|
28 const QString KKeyBookKeeping = "BookKeeping"; |
|
29 const QString KKeyBase = "Preset-"; |
|
30 |
|
31 /*! |
|
32 * |
|
33 */ |
|
34 static QString makeKey( int index ) { |
|
35 QString key = KKeyBase; |
|
36 key.append( index ); |
|
37 return key; |
|
38 } |
|
39 |
|
40 /*! |
|
41 * |
|
42 */ |
|
43 Preset::Preset() : |
|
44 mFrequency( 0 ), |
|
45 mRenamedByUser( false ), |
|
46 mGenre( -1 ), |
|
47 mPiCode( -1 ), |
|
48 mFavorite( false ), |
|
49 mLocalStation( false ) |
|
50 { |
|
51 } |
|
52 |
|
53 /*! |
|
54 * |
|
55 */ |
|
56 Preset::~Preset() |
|
57 { |
|
58 } |
|
59 |
|
60 /*! |
|
61 * |
|
62 */ |
|
63 void Preset::externalize( QDataStream& outStream ) |
|
64 { |
|
65 outStream << mFrequency; |
|
66 outStream << mName; |
|
67 outStream << mRenamedByUser; |
|
68 outStream << mGenre; |
|
69 outStream << mUrl; |
|
70 outStream << mPiCode; |
|
71 outStream << mFavorite; |
|
72 outStream << mLocalStation; |
|
73 } |
|
74 |
|
75 /*! |
|
76 * |
|
77 */ |
|
78 void Preset::internalize( QDataStream& inStream ) |
|
79 { |
|
80 inStream >> mFrequency; |
|
81 inStream >> mName; |
|
82 inStream >> mRenamedByUser; |
|
83 inStream >> mGenre; |
|
84 inStream >> mUrl; |
|
85 inStream >> mPiCode; |
|
86 inStream >> mFavorite; |
|
87 inStream >> mLocalStation; |
|
88 } |
|
89 |
|
90 /*! |
|
91 * |
|
92 */ |
|
93 RadioPresetStorage::RadioPresetStorage() : |
|
94 d_ptr( new RadioPresetStoragePrivate() ) |
|
95 { |
|
96 Q_D( RadioPresetStorage ); |
|
97 d->init(); |
|
98 } |
|
99 |
|
100 /*! |
|
101 * |
|
102 */ |
|
103 RadioPresetStorage::~RadioPresetStorage() |
|
104 { |
|
105 } |
|
106 |
|
107 /*! |
|
108 * |
|
109 */ |
|
110 int RadioPresetStorage::maxNumberOfPresets() const |
|
111 { |
|
112 return 100; |
|
113 } |
|
114 |
|
115 /*! |
|
116 * |
|
117 */ |
|
118 int RadioPresetStorage::presetCount() const |
|
119 { |
|
120 Q_D( const RadioPresetStorage ); |
|
121 return d->mBookKeeping.count(); |
|
122 } |
|
123 |
|
124 /*! |
|
125 * |
|
126 */ |
|
127 int RadioPresetStorage::firstPreset() const |
|
128 { |
|
129 Q_D( const RadioPresetStorage ); |
|
130 if ( d->mBookKeeping.count() > 0 ) { |
|
131 return d->mBookKeeping.at( 0 ); |
|
132 } |
|
133 return -1; |
|
134 } |
|
135 |
|
136 /*! |
|
137 * |
|
138 */ |
|
139 int RadioPresetStorage::nextPreset( int fromIndex ) const |
|
140 { |
|
141 Q_D( const RadioPresetStorage ); |
|
142 int index = d->mBookKeeping.indexOf( fromIndex ) + 1; |
|
143 if ( index > 0 && index < d->mBookKeeping.count() ) { |
|
144 return d->mBookKeeping.at( index ); |
|
145 } |
|
146 return -1; |
|
147 } |
|
148 |
|
149 /*! |
|
150 * |
|
151 */ |
|
152 bool RadioPresetStorage::deletePreset( int presetIndex ) |
|
153 { |
|
154 Q_D( RadioPresetStorage ); |
|
155 if ( presetIndex > -1 ) { |
|
156 int index = d->mBookKeeping.indexOf( presetIndex ); |
|
157 if ( index > -1 ) { |
|
158 d->mBookKeeping.remove( index, 1 ); |
|
159 d->mSettings->remove( makeKey( presetIndex ) ); |
|
160 d->removeIndex( presetIndex ); |
|
161 return true; |
|
162 } |
|
163 } else if ( presetIndex == -1 ) { |
|
164 for ( int i = d->mBookKeeping.count() - 1; i >= 0; --i ) { |
|
165 d->mSettings->remove( makeKey( d->mBookKeeping.at( i ) ) ); |
|
166 } |
|
167 d->mBookKeeping.clear(); |
|
168 return true; |
|
169 } |
|
170 return false; |
|
171 } |
|
172 |
|
173 /*! |
|
174 * |
|
175 */ |
|
176 bool RadioPresetStorage::savePreset( const RadioStationIf& station ) |
|
177 { |
|
178 Preset preset; |
|
179 preset.mFrequency = station.frequency(); |
|
180 preset.mName = station.name(); |
|
181 preset.mRenamedByUser = station.isRenamedByUser() ; |
|
182 preset.mGenre = station.genre(); |
|
183 preset.mUrl = station.url(); |
|
184 preset.mPiCode = station.piCode(); |
|
185 preset.mFavorite = station.isFavorite(); |
|
186 preset.mLocalStation = station.isLocalStation(); |
|
187 |
|
188 QByteArray array; |
|
189 QDataStream outStream( &array, QIODevice::WriteOnly ); |
|
190 preset.externalize( outStream ); |
|
191 |
|
192 Q_D( RadioPresetStorage ); |
|
193 d->mSettings->setValue( makeKey( station.presetIndex() ), array ); |
|
194 |
|
195 d->addIndex( station.presetIndex() ); |
|
196 |
|
197 return true; |
|
198 } |
|
199 |
|
200 /*! |
|
201 * |
|
202 */ |
|
203 bool RadioPresetStorage::readPreset( int index, RadioStationIf& station ) |
|
204 { |
|
205 Q_D( RadioPresetStorage ); |
|
206 QByteArray array = d->mSettings->value( makeKey( index ) ).toByteArray(); |
|
207 if ( !array.isEmpty() ) { |
|
208 Preset preset; |
|
209 |
|
210 QDataStream inStream( array ); |
|
211 preset.internalize( inStream ); |
|
212 |
|
213 station.setPresetIndex( index ); |
|
214 station.setFrequency( preset.mFrequency ); |
|
215 station.setName( preset.mName ); |
|
216 station.setRenamedByUser( preset.mRenamedByUser ); |
|
217 station.setGenre( preset.mGenre ); |
|
218 station.setUrl( preset.mUrl ); |
|
219 station.setPiCode( preset.mPiCode ); |
|
220 station.setFavorite( preset.mFavorite ); |
|
221 station.setLocalStation( preset.mLocalStation ); |
|
222 |
|
223 return true; |
|
224 } |
|
225 return false; |
|
226 } |
|
227 |
|
228 /*! |
|
229 * |
|
230 */ |
|
231 RadioPresetStoragePrivate::RadioPresetStoragePrivate() |
|
232 { |
|
233 } |
|
234 |
|
235 /*! |
|
236 * |
|
237 */ |
|
238 RadioPresetStoragePrivate::~RadioPresetStoragePrivate() |
|
239 { |
|
240 } |
|
241 |
|
242 /*! |
|
243 * |
|
244 */ |
|
245 bool RadioPresetStoragePrivate::init() |
|
246 { |
|
247 mSettings.reset( new QSettings( "Nokia", "QtFmRadio" ) ); |
|
248 readBookKeeping(); |
|
249 return true; |
|
250 } |
|
251 |
|
252 /*! |
|
253 * |
|
254 */ |
|
255 void RadioPresetStoragePrivate::readBookKeeping() |
|
256 { |
|
257 QVariant value = mSettings->value( KKeyBookKeeping ); |
|
258 if ( value.isValid() ) { |
|
259 mBookKeeping = value.toByteArray(); |
|
260 } |
|
261 } |
|
262 |
|
263 /*! |
|
264 * |
|
265 */ |
|
266 void RadioPresetStoragePrivate::saveBookKeeping() |
|
267 { |
|
268 mSettings->setValue( KKeyBookKeeping, mBookKeeping ); |
|
269 } |
|
270 |
|
271 /*! |
|
272 * |
|
273 */ |
|
274 void RadioPresetStoragePrivate::removeIndex( int index ) |
|
275 { |
|
276 int pos = mBookKeeping.indexOf( index ); |
|
277 if ( pos > -1 ) { |
|
278 mBookKeeping.remove( pos, 1 ); |
|
279 saveBookKeeping(); |
|
280 } |
|
281 } |
|
282 |
|
283 /*! |
|
284 * |
|
285 */ |
|
286 void RadioPresetStoragePrivate::addIndex( int index ) |
|
287 { |
|
288 int pos = mBookKeeping.indexOf( index ); |
|
289 if ( pos == -1 ) { |
|
290 mBookKeeping.append( index ); |
|
291 saveBookKeeping(); |
|
292 } |
|
293 } |
|