author | hgs |
Tue, 12 Oct 2010 11:25:14 +0300 | |
changeset 56 | 04837bf3a628 |
parent 54 | a8ba0c289b44 |
child 57 | 21be958eb3ce |
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 <QSqlDatabase> |
|
20 |
#include <QFile> |
|
21 |
#include <QDateTime> |
|
22 |
#include <QVariant> |
|
23 |
#include <QStringList> |
|
24 |
#include <QSqlError> |
|
25 |
#include <QSqlQueryModel> |
|
26 |
#include <QSqlRecord> |
|
27 |
||
28 |
// User includes |
|
29 |
#include "radiohistorymodel_p.h" |
|
30 |
#include "radiohistorymodel.h" |
|
31 |
#include "radiohistoryitem.h" |
|
32 |
#include "radiohistoryitem_p.h" |
|
33 |
#include "radiostation.h" |
|
34 |
#include "radiologger.h" |
|
35 |
||
51 | 36 |
static const QLatin1String DATABASE_NAME ( "c:\\radioplayhistory.db" ); |
34 | 37 |
static const QLatin1String DATABASE_DRIVER ( "QSQLITE" ); |
38 |
static const QLatin1String HISTORY_TABLE ( "history" ); |
|
39 |
static const QLatin1String SQL_CREATE_TABLE ( "CREATE TABLE history (" |
|
40 |
"id INTEGER PRIMARY KEY AUTOINCREMENT, " |
|
41 |
"artist TEXT NOT NULL, " |
|
42 |
"title TEXT NOT NULL, " |
|
43 |
"station TEXT NOT NULL, " |
|
44 |
"frequency INTEGER NOT NULL, " |
|
45 |
"tagged INTEGER NOT NULL DEFAULT 0, " |
|
46 |
"fromRds INTEGER NOT NULL DEFAULT 1, " |
|
47 |
"time INTEGER NOT NULL)" ); |
|
24 | 48 |
|
34 | 49 |
static const QLatin1String SQL_ADD_ITEM ( "INSERT INTO history (artist,title,station,frequency,fromRds,time) " |
50 |
"VALUES ( ?,?,?,?,?,? )" ); |
|
24 | 51 |
|
34 | 52 |
static const QLatin1String SQL_SELECT_ALL ( "SELECT * FROM history ORDER BY id DESC" ); |
53 |
static const QLatin1String SQL_SELECT_TAGGED( "SELECT * FROM history WHERE tagged=1" );// ORDER BY id DESC"; |
|
24 | 54 |
|
34 | 55 |
static const QLatin1String SQL_DELETE_ALL ( "DELETE FROM history" ); |
56 |
static const QLatin1String SQL_DELETE_RECENT( "DELETE FROM history WHERE tagged=0" ); |
|
57 |
//static const QLatin1String SQL_DELETE_TAGGED = "DELETE FROM history WHERE tagged=1"; |
|
58 |
static const QLatin1String SQL_CLEAR_TAGS ( "UPDATE history SET tagged = 0 WHERE tagged = 1" ); |
|
24 | 59 |
|
34 | 60 |
//static static const QLatin1String SQL_FIND_ITEM_BY_ID( "SELECT * FROM history WHERE id = ?" ); |
61 |
static const QLatin1String SQL_TOGGLE_TAG ( "UPDATE history SET tagged = ? WHERE id = ?" ); |
|
24 | 62 |
|
54 | 63 |
static const QLatin1String SQL_DELETE_ITEM_FORMAT_STR ( "DELETE FROM history WHERE id = %1" ); |
64 |
static const QLatin1String SQL_REMOVE_TAG_FORMAT_STR ( "UPDATE history SET tagged = 0 WHERE id = %1" ); |
|
65 |
static const QLatin1String OR_ID_IS_FORMAT_STR (" OR id = %1"); |
|
66 |
||
67 |
static const int MAX_ID_COUNT_IN_QUERY = 5; |
|
68 |
||
24 | 69 |
#ifdef LOGGING_ENABLED |
70 |
# define GET_ERR( param ) GETSTRING( param.lastError().text() ) |
|
71 |
# define GET_ERR_PTR( param ) GETSTRING( param->lastError().text() ) |
|
72 |
#endif // LOGGING_ENABLED |
|
73 |
||
74 |
/*! |
|
75 |
* Static utility function to parse a frequency |
|
76 |
*/ |
|
77 |
static QString parseFrequency( const uint frequency ) |
|
78 |
{ |
|
79 |
QString loc = qtTrId( "txt_rad_dblist_val_l1_mhz" ); |
|
80 |
return loc.arg( RadioStation::parseFrequency( frequency ) ); |
|
81 |
} |
|
82 |
||
83 |
/*! |
|
84 |
* |
|
85 |
*/ |
|
86 |
RadioHistoryModelPrivate::RadioHistoryModelPrivate( RadioHistoryModel* model, |
|
87 |
RadioUiEngine& uiEngine ) : |
|
88 |
q_ptr( model ), |
|
89 |
mUiEngine( uiEngine ), |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
90 |
mRtItemClass( -1 ), |
24 | 91 |
mTopItemIsPlaying( false ), |
92 |
mShowDetails( true ), |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
93 |
mViewMode( ShowAll ) |
24 | 94 |
{ |
95 |
} |
|
96 |
||
97 |
/*! |
|
98 |
* |
|
99 |
*/ |
|
100 |
RadioHistoryModelPrivate::~RadioHistoryModelPrivate() |
|
101 |
{ |
|
102 |
if ( mDatabase && mDatabase->isOpen() ) { |
|
103 |
mDatabase->close(); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
/*! |
|
108 |
* |
|
109 |
*/ |
|
110 |
bool RadioHistoryModelPrivate::connectToDatabase() |
|
111 |
{ |
|
112 |
LOG_METHOD; |
|
113 |
QSqlDatabase db = QSqlDatabase::addDatabase( DATABASE_DRIVER ); |
|
114 |
if ( db.isValid() ) { |
|
115 |
mDatabase.reset( new QSqlDatabase( db ) ); |
|
116 |
mDatabase->setDatabaseName( DATABASE_NAME ); |
|
117 |
||
118 |
if ( !mDatabase->open() ) { |
|
119 |
LOG_FORMAT( "Failed to open database! error = %s", GET_ERR_PTR( mDatabase ) ); |
|
120 |
mDatabase.reset(); |
|
121 |
return false; |
|
122 |
} |
|
123 |
||
124 |
// Create the table if it does not exist |
|
125 |
if ( !mDatabase->tables().contains( HISTORY_TABLE ) ) { |
|
126 |
LOG( "RadioHistoryModelPrivate::connectToDatabase: Creating database tables." ); |
|
127 |
QSqlQuery query; |
|
128 |
if ( !query.exec( SQL_CREATE_TABLE ) ) { |
|
129 |
LOG_FORMAT( "Database creation failed! error = %s", GET_ERR( query ) ); |
|
130 |
mDatabase->close(); |
|
131 |
mDatabase.reset(); |
|
132 |
return false; |
|
133 |
} |
|
134 |
} |
|
135 |
} else { |
|
136 |
LOG_FORMAT( "Invalid database! error = %s", GET_ERR( db ) ); |
|
137 |
return false; |
|
138 |
} |
|
139 |
||
140 |
mQueryModel.reset( new QSqlQueryModel() ); |
|
141 |
setViewMode( ShowAll ); |
|
142 |
||
143 |
return mQueryModel->lastError().type() == QSqlError::NoError; |
|
144 |
} |
|
145 |
||
146 |
/*! |
|
147 |
* |
|
148 |
*/ |
|
149 |
void RadioHistoryModelPrivate::addItem( const QString& artist, |
|
150 |
const QString& title, |
|
151 |
const RadioStation& station, |
|
152 |
bool fromRds ) |
|
153 |
{ |
|
154 |
LOG_FORMAT( "RadioHistoryModelPrivate::addItem. Artist: %s, Title: %s", GETSTRING( artist ), GETSTRING( title ) ); |
|
155 |
||
156 |
if ( !mQueryModel ) { |
|
157 |
return; |
|
158 |
} |
|
159 |
||
160 |
mTopItemIsPlaying = true; |
|
161 |
||
162 |
QSqlQuery query = beginTransaction(); |
|
163 |
||
164 |
query.prepare( SQL_ADD_ITEM ); |
|
165 |
query.addBindValue( artist ); |
|
166 |
query.addBindValue( title ); |
|
167 |
query.addBindValue( station.name() ); |
|
168 |
query.addBindValue( static_cast<int>( station.frequency() / 1000 ) ); |
|
169 |
query.addBindValue( fromRds ); |
|
170 |
query.addBindValue( QDateTime::currentDateTime().toTime_t() ); |
|
171 |
||
172 |
commitTransaction( query, InsertRows, 0 ); |
|
173 |
} |
|
174 |
||
175 |
/*! |
|
176 |
* |
|
177 |
*/ |
|
178 |
int RadioHistoryModelPrivate::rowCount() const |
|
179 |
{ |
|
180 |
if ( !mQueryModel ) { |
|
181 |
return 0; |
|
182 |
} |
|
183 |
return mQueryModel->rowCount(); |
|
184 |
} |
|
185 |
||
186 |
/*! |
|
187 |
* |
|
188 |
*/ |
|
189 |
QVariant RadioHistoryModelPrivate::data( const int row, const int role ) const |
|
190 |
{ |
|
191 |
if ( mQueryModel->lastError().type() == QSqlError::NoError ) { |
|
192 |
||
193 |
QSqlRecord record = mQueryModel->record( row ); |
|
194 |
if ( role == Qt::DisplayRole ) { |
|
195 |
||
51 | 196 |
QString artist = record.value( RadioHistoryValue::Artist ).toString(); |
197 |
if ( artist.isEmpty() ) { |
|
198 |
artist = qtTrId( "txt_rad_dblist_unknown" ); |
|
199 |
} |
|
200 |
||
24 | 201 |
const QString title = record.value( RadioHistoryValue::Title ).toString(); |
202 |
const QString station = record.value( RadioHistoryValue::Station ).toString(); |
|
203 |
const uint frequency = record.value( RadioHistoryValue::Frequency ).toUInt() * 1000; |
|
204 |
||
205 |
QStringList list; |
|
206 |
if ( mShowDetails ) { |
|
34 | 207 |
QString formatter = qtTrId( "txt_rad_dblist_1_2" ); |
208 |
LOG_FORMAT( "---formatter--- %s", GETSTRING( formatter ) ); |
|
209 |
||
210 |
const QString firstRow = QString( formatter ).arg( artist ).arg( title ); |
|
211 |
LOG_FORMAT( "---firstRow--- %s", GETSTRING( firstRow ) ); |
|
212 |
list.append( firstRow ); |
|
213 |
||
214 |
const uint timeInSecs = record.value( RadioHistoryValue::Time ).toUInt(); |
|
215 |
QDateTime dateTime; |
|
216 |
dateTime.setTime_t( timeInSecs ); |
|
217 |
||
218 |
QString time = dateTime.toString( Qt::SystemLocaleShortDate ); |
|
219 |
LOG_FORMAT( "---time--- %s", GETSTRING( time ) ); |
|
24 | 220 |
|
221 |
QString name = !station.isEmpty() ? station : parseFrequency( frequency ); |
|
34 | 222 |
LOG_FORMAT( "---name--- %s", GETSTRING( name ) ); |
223 |
const QString secondRow = QString( formatter ).arg( time ).arg( name ); |
|
224 |
LOG_FORMAT( "---secondRow--- %s", GETSTRING( secondRow ) ); |
|
225 |
||
226 |
list.append( secondRow ); |
|
24 | 227 |
} else { |
228 |
list.append( artist ); |
|
229 |
list.append( title ); |
|
230 |
} |
|
231 |
||
232 |
return list; |
|
233 |
} else if ( role == Qt::DecorationRole ) { |
|
234 |
QVariantList list; |
|
235 |
const bool tagged = record.value( RadioHistoryValue::Tagged ).toBool(); |
|
236 |
if ( tagged ) { |
|
237 |
list.append( mTaggedIcon ); |
|
238 |
} else { |
|
239 |
list.append( mNonTaggedIcon ); |
|
240 |
} |
|
241 |
return list; |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
return QVariant(); |
|
246 |
} |
|
247 |
||
248 |
/*! |
|
249 |
* |
|
250 |
*/ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
251 |
void RadioHistoryModelPrivate::removeAll( bool removeTagged ) |
24 | 252 |
{ |
253 |
if ( !mQueryModel ) { |
|
254 |
return; |
|
255 |
} |
|
256 |
||
257 |
QSqlQuery query = beginTransaction(); |
|
258 |
||
34 | 259 |
query.prepare( removeTagged ? SQL_CLEAR_TAGS : SQL_DELETE_ALL ); |
24 | 260 |
|
261 |
// Commented out because rowsRemoved() seems to crash HbListView |
|
262 |
// commitTransaction( query, RemoveRows, 0, rowCount() - 1 ); |
|
263 |
||
264 |
commitTransaction( query, NoOp, 0 ); |
|
265 |
q_ptr->reset(); |
|
266 |
} |
|
267 |
||
268 |
/*! |
|
269 |
* |
|
270 |
*/ |
|
54 | 271 |
void RadioHistoryModelPrivate::removeByModelIndices( QModelIndexList& indices, bool removeTags ) |
272 |
{ |
|
273 |
if ( !mQueryModel ) { |
|
274 |
return; |
|
275 |
} |
|
276 |
QString sqlStr = ""; |
|
277 |
int rowIndex = -1; |
|
278 |
||
279 |
QSqlQuery query( *mDatabase ); |
|
280 |
mDatabase->transaction(); |
|
281 |
// List needs to be sorted and indices needs to go throught from largest to smallest. |
|
282 |
// This is for keeping QmodelIndexing in sync after begin- and endremoverows |
|
283 |
// calls when content is not yet actually removed. |
|
284 |
// Real removal happens in QSqlQuery::exec |
|
285 |
qSort(indices); |
|
286 |
QModelIndexList::const_iterator iter = indices.constEnd(); |
|
287 |
QModelIndexList::const_iterator begin = indices.constBegin(); |
|
288 |
for ( int counter = 1; iter != begin; ) { |
|
289 |
iter--; |
|
290 |
rowIndex = (*iter).row(); |
|
291 |
if( rowIndex > -1 ) { |
|
292 |
QSqlRecord record = mQueryModel->record(rowIndex); |
|
293 |
||
294 |
if( counter > 1 ) { |
|
295 |
sqlStr += QString( OR_ID_IS_FORMAT_STR ).arg(record.value("id").toInt()); |
|
296 |
} else { |
|
297 |
sqlStr = QString( removeTags ? SQL_REMOVE_TAG_FORMAT_STR |
|
298 |
: SQL_DELETE_ITEM_FORMAT_STR ).arg(record.value("id").toInt()); |
|
299 |
} |
|
300 |
// adding max MAX_ID_COUNT_IN_QUERY ids to Query |
|
301 |
if( counter == MAX_ID_COUNT_IN_QUERY ) { |
|
302 |
if( !prepareAndExec( query, sqlStr ) ) { |
|
303 |
// error, do not proceed |
|
304 |
break; |
|
305 |
} |
|
306 |
counter = 1; |
|
307 |
sqlStr = ""; |
|
308 |
} else { |
|
309 |
counter++; |
|
310 |
} |
|
311 |
q_ptr->beginRemoveRows( QModelIndex(), rowIndex, rowIndex ); |
|
312 |
q_ptr->endRemoveRows(); |
|
313 |
} |
|
314 |
} |
|
315 |
if( !query.lastError().isValid() && sqlStr.length() ) { |
|
316 |
prepareAndExec( query, sqlStr ); |
|
317 |
} |
|
318 |
if( query.lastError().isValid() ) { |
|
319 |
// in case of error, rollback everyhing and reset model |
|
320 |
mDatabase->rollback(); |
|
321 |
q_ptr->reset(); |
|
322 |
} else { |
|
323 |
mDatabase->commit(); |
|
324 |
refreshModel(); |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
/*! |
|
329 |
* |
|
330 |
*/ |
|
24 | 331 |
void RadioHistoryModelPrivate::setViewMode( ViewMode mode ) |
332 |
{ |
|
333 |
if ( !mQueryModel ) { |
|
334 |
return; |
|
335 |
} |
|
336 |
||
337 |
mViewMode = mode; |
|
338 |
mQueryModel->setQuery( mode == ShowTagged ? SQL_SELECT_TAGGED : SQL_SELECT_ALL, *mDatabase ); |
|
339 |
} |
|
340 |
||
341 |
/*! |
|
342 |
* |
|
343 |
*/ |
|
344 |
void RadioHistoryModelPrivate::toggleTagging( const RadioHistoryItem& item, const int row ) |
|
345 |
{ |
|
346 |
QSqlQuery updateQuery = beginTransaction(); |
|
347 |
||
348 |
updateQuery.prepare( SQL_TOGGLE_TAG ); |
|
349 |
updateQuery.addBindValue( item.isTagged() ? 0 : 1 ); |
|
350 |
updateQuery.addBindValue( item.id() ); |
|
34 | 351 |
Operation operation = ChangeData; |
352 |
if ( mViewMode == ShowTagged && item.isTagged() ) { |
|
353 |
operation = RemoveRows; |
|
354 |
} |
|
355 |
commitTransaction( updateQuery, operation, row ); |
|
24 | 356 |
} |
357 |
||
358 |
/*! |
|
359 |
* |
|
360 |
*/ |
|
361 |
RadioHistoryItem RadioHistoryModelPrivate::itemAtIndex( const QModelIndex& index ) const |
|
362 |
{ |
|
363 |
LOG_METHOD; |
|
364 |
RadioHistoryItem item; |
|
365 |
QSqlRecord record = mQueryModel->record( index.row() ); |
|
366 |
item.data_ptr()->initFromRecord( record ); |
|
367 |
return item; |
|
368 |
} |
|
369 |
||
370 |
/*! |
|
371 |
* |
|
372 |
*/ |
|
373 |
void RadioHistoryModelPrivate::refreshModel() |
|
374 |
{ |
|
375 |
setViewMode( mViewMode ); |
|
376 |
} |
|
377 |
||
378 |
/*! |
|
379 |
* |
|
380 |
*/ |
|
381 |
QSqlQuery RadioHistoryModelPrivate::beginTransaction() |
|
382 |
{ |
|
383 |
LOG_METHOD; |
|
384 |
QSqlQuery newQuery( *mDatabase ); |
|
385 |
mDatabase->transaction(); |
|
386 |
return newQuery; |
|
387 |
} |
|
388 |
||
389 |
/*! |
|
390 |
* |
|
391 |
*/ |
|
392 |
void RadioHistoryModelPrivate::commitTransaction( QSqlQuery& query, Operation operation, int start, int end ) |
|
393 |
{ |
|
394 |
LOG_METHOD; |
|
395 |
if ( end == -1 ) { |
|
396 |
end = start; |
|
397 |
} |
|
398 |
||
399 |
bool success = false; |
|
400 |
Q_UNUSED( success ); |
|
401 |
if ( query.exec() ) { |
|
402 |
if ( operation == InsertRows ) { |
|
403 |
q_ptr->beginInsertRows( QModelIndex(), start, end ); |
|
404 |
} else if ( operation == RemoveRows ) { |
|
405 |
q_ptr->beginRemoveRows( QModelIndex(), start, end ); |
|
406 |
} |
|
407 |
||
408 |
success = mDatabase->commit(); |
|
409 |
LOG_ASSERT( success, LOG_FORMAT( "Commit failed! err: %s", GET_ERR_PTR( mDatabase ) ) ); |
|
410 |
||
411 |
refreshModel(); |
|
412 |
||
413 |
if ( operation == InsertRows ) { |
|
414 |
q_ptr->endInsertRows(); |
|
415 |
q_ptr->emitItemAdded(); |
|
416 |
} else if ( operation == RemoveRows ) { |
|
417 |
q_ptr->endRemoveRows(); |
|
418 |
} else if ( operation == ChangeData ) { |
|
419 |
q_ptr->reportChangedData( start ); |
|
420 |
} |
|
421 |
} else { |
|
422 |
LOG_FORMAT( "RadioHistoryModelPrivate::commitTransaction FAILED, rolling back: error = %s", GET_ERR( query ) ); |
|
423 |
success = mDatabase->rollback(); |
|
424 |
LOG_ASSERT( success, LOG_FORMAT( "Rollback failed! err: %s", GET_ERR_PTR( mDatabase ) ) ); |
|
425 |
} |
|
426 |
} |
|
54 | 427 |
|
428 |
bool RadioHistoryModelPrivate::prepareAndExec( QSqlQuery& query, const QString& sqlStr ) |
|
429 |
{ |
|
430 |
bool isOk = true; |
|
431 |
isOk = query.prepare(sqlStr); |
|
432 |
||
433 |
if(isOk) { |
|
434 |
isOk = query.exec(); |
|
435 |
} |
|
436 |
return isOk; |
|
437 |
} |