author | hgs |
Mon, 23 Aug 2010 14:17:50 +0300 | |
changeset 44 | 0a3ad94fc71c |
parent 34 | bc10a61bd7d3 |
child 51 | bbebb0235466 |
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 |
||
34 | 36 |
static const QLatin1String DATABASE_NAME ( "radioplayhistory.db" ); |
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 |
|
63 |
#ifdef LOGGING_ENABLED |
|
64 |
# define GET_ERR( param ) GETSTRING( param.lastError().text() ) |
|
65 |
# define GET_ERR_PTR( param ) GETSTRING( param->lastError().text() ) |
|
66 |
#endif // LOGGING_ENABLED |
|
67 |
||
68 |
/*! |
|
69 |
* Static utility function to parse a frequency |
|
70 |
*/ |
|
71 |
static QString parseFrequency( const uint frequency ) |
|
72 |
{ |
|
73 |
QString loc = qtTrId( "txt_rad_dblist_val_l1_mhz" ); |
|
74 |
return loc.arg( RadioStation::parseFrequency( frequency ) ); |
|
75 |
} |
|
76 |
||
77 |
/*! |
|
78 |
* |
|
79 |
*/ |
|
80 |
RadioHistoryModelPrivate::RadioHistoryModelPrivate( RadioHistoryModel* model, |
|
81 |
RadioUiEngine& uiEngine ) : |
|
82 |
q_ptr( model ), |
|
83 |
mUiEngine( uiEngine ), |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
84 |
mRtItemClass( -1 ), |
24 | 85 |
mTopItemIsPlaying( false ), |
86 |
mShowDetails( true ), |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
87 |
mViewMode( ShowAll ) |
24 | 88 |
{ |
89 |
} |
|
90 |
||
91 |
/*! |
|
92 |
* |
|
93 |
*/ |
|
94 |
RadioHistoryModelPrivate::~RadioHistoryModelPrivate() |
|
95 |
{ |
|
96 |
if ( mDatabase && mDatabase->isOpen() ) { |
|
97 |
mDatabase->close(); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
/*! |
|
102 |
* |
|
103 |
*/ |
|
104 |
bool RadioHistoryModelPrivate::connectToDatabase() |
|
105 |
{ |
|
106 |
LOG_METHOD; |
|
107 |
QSqlDatabase db = QSqlDatabase::addDatabase( DATABASE_DRIVER ); |
|
108 |
if ( db.isValid() ) { |
|
109 |
mDatabase.reset( new QSqlDatabase( db ) ); |
|
110 |
mDatabase->setDatabaseName( DATABASE_NAME ); |
|
111 |
||
112 |
if ( !mDatabase->open() ) { |
|
113 |
LOG_FORMAT( "Failed to open database! error = %s", GET_ERR_PTR( mDatabase ) ); |
|
114 |
mDatabase.reset(); |
|
115 |
return false; |
|
116 |
} |
|
117 |
||
118 |
// Create the table if it does not exist |
|
119 |
if ( !mDatabase->tables().contains( HISTORY_TABLE ) ) { |
|
120 |
LOG( "RadioHistoryModelPrivate::connectToDatabase: Creating database tables." ); |
|
121 |
QSqlQuery query; |
|
122 |
if ( !query.exec( SQL_CREATE_TABLE ) ) { |
|
123 |
LOG_FORMAT( "Database creation failed! error = %s", GET_ERR( query ) ); |
|
124 |
mDatabase->close(); |
|
125 |
mDatabase.reset(); |
|
126 |
return false; |
|
127 |
} |
|
128 |
} |
|
129 |
} else { |
|
130 |
LOG_FORMAT( "Invalid database! error = %s", GET_ERR( db ) ); |
|
131 |
return false; |
|
132 |
} |
|
133 |
||
134 |
mQueryModel.reset( new QSqlQueryModel() ); |
|
135 |
setViewMode( ShowAll ); |
|
136 |
||
137 |
return mQueryModel->lastError().type() == QSqlError::NoError; |
|
138 |
} |
|
139 |
||
140 |
/*! |
|
141 |
* |
|
142 |
*/ |
|
143 |
void RadioHistoryModelPrivate::addItem( const QString& artist, |
|
144 |
const QString& title, |
|
145 |
const RadioStation& station, |
|
146 |
bool fromRds ) |
|
147 |
{ |
|
148 |
LOG_FORMAT( "RadioHistoryModelPrivate::addItem. Artist: %s, Title: %s", GETSTRING( artist ), GETSTRING( title ) ); |
|
149 |
||
150 |
if ( !mQueryModel ) { |
|
151 |
return; |
|
152 |
} |
|
153 |
||
154 |
mTopItemIsPlaying = true; |
|
155 |
||
156 |
QSqlQuery query = beginTransaction(); |
|
157 |
||
158 |
query.prepare( SQL_ADD_ITEM ); |
|
159 |
query.addBindValue( artist ); |
|
160 |
query.addBindValue( title ); |
|
161 |
query.addBindValue( station.name() ); |
|
162 |
query.addBindValue( static_cast<int>( station.frequency() / 1000 ) ); |
|
163 |
query.addBindValue( fromRds ); |
|
164 |
query.addBindValue( QDateTime::currentDateTime().toTime_t() ); |
|
165 |
||
166 |
commitTransaction( query, InsertRows, 0 ); |
|
167 |
} |
|
168 |
||
169 |
/*! |
|
170 |
* |
|
171 |
*/ |
|
172 |
int RadioHistoryModelPrivate::rowCount() const |
|
173 |
{ |
|
174 |
if ( !mQueryModel ) { |
|
175 |
return 0; |
|
176 |
} |
|
177 |
return mQueryModel->rowCount(); |
|
178 |
} |
|
179 |
||
180 |
/*! |
|
181 |
* |
|
182 |
*/ |
|
183 |
QVariant RadioHistoryModelPrivate::data( const int row, const int role ) const |
|
184 |
{ |
|
185 |
if ( mQueryModel->lastError().type() == QSqlError::NoError ) { |
|
186 |
||
187 |
QSqlRecord record = mQueryModel->record( row ); |
|
188 |
if ( role == Qt::DisplayRole ) { |
|
189 |
||
190 |
const QString artist = record.value( RadioHistoryValue::Artist ).toString(); |
|
191 |
const QString title = record.value( RadioHistoryValue::Title ).toString(); |
|
192 |
const QString station = record.value( RadioHistoryValue::Station ).toString(); |
|
193 |
const uint frequency = record.value( RadioHistoryValue::Frequency ).toUInt() * 1000; |
|
194 |
||
195 |
QStringList list; |
|
196 |
if ( mShowDetails ) { |
|
34 | 197 |
QString formatter = qtTrId( "txt_rad_dblist_1_2" ); |
198 |
LOG_FORMAT( "---formatter--- %s", GETSTRING( formatter ) ); |
|
199 |
formatter = "%1 - %2"; // TODO! |
|
200 |
||
201 |
const QString firstRow = QString( formatter ).arg( artist ).arg( title ); |
|
202 |
LOG_FORMAT( "---firstRow--- %s", GETSTRING( firstRow ) ); |
|
203 |
list.append( firstRow ); |
|
204 |
||
205 |
const uint timeInSecs = record.value( RadioHistoryValue::Time ).toUInt(); |
|
206 |
QDateTime dateTime; |
|
207 |
dateTime.setTime_t( timeInSecs ); |
|
208 |
||
209 |
QString time = dateTime.toString( Qt::SystemLocaleShortDate ); |
|
210 |
LOG_FORMAT( "---time--- %s", GETSTRING( time ) ); |
|
24 | 211 |
|
212 |
QString name = !station.isEmpty() ? station : parseFrequency( frequency ); |
|
34 | 213 |
LOG_FORMAT( "---name--- %s", GETSTRING( name ) ); |
214 |
const QString secondRow = QString( formatter ).arg( time ).arg( name ); |
|
215 |
LOG_FORMAT( "---secondRow--- %s", GETSTRING( secondRow ) ); |
|
216 |
||
217 |
list.append( secondRow ); |
|
24 | 218 |
} else { |
219 |
list.append( artist ); |
|
220 |
list.append( title ); |
|
221 |
} |
|
222 |
||
223 |
return list; |
|
224 |
} else if ( role == Qt::DecorationRole ) { |
|
225 |
QVariantList list; |
|
226 |
const bool tagged = record.value( RadioHistoryValue::Tagged ).toBool(); |
|
227 |
if ( tagged ) { |
|
228 |
list.append( mTaggedIcon ); |
|
229 |
} else { |
|
230 |
list.append( mNonTaggedIcon ); |
|
231 |
} |
|
232 |
return list; |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
return QVariant(); |
|
237 |
} |
|
238 |
||
239 |
/*! |
|
240 |
* |
|
241 |
*/ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
242 |
void RadioHistoryModelPrivate::removeAll( bool removeTagged ) |
24 | 243 |
{ |
244 |
if ( !mQueryModel ) { |
|
245 |
return; |
|
246 |
} |
|
247 |
||
248 |
QSqlQuery query = beginTransaction(); |
|
249 |
||
34 | 250 |
query.prepare( removeTagged ? SQL_CLEAR_TAGS : SQL_DELETE_ALL ); |
24 | 251 |
|
252 |
// Commented out because rowsRemoved() seems to crash HbListView |
|
253 |
// commitTransaction( query, RemoveRows, 0, rowCount() - 1 ); |
|
254 |
||
255 |
commitTransaction( query, NoOp, 0 ); |
|
256 |
q_ptr->reset(); |
|
257 |
} |
|
258 |
||
259 |
/*! |
|
260 |
* |
|
261 |
*/ |
|
262 |
void RadioHistoryModelPrivate::setViewMode( ViewMode mode ) |
|
263 |
{ |
|
264 |
if ( !mQueryModel ) { |
|
265 |
return; |
|
266 |
} |
|
267 |
||
268 |
mViewMode = mode; |
|
269 |
mQueryModel->setQuery( mode == ShowTagged ? SQL_SELECT_TAGGED : SQL_SELECT_ALL, *mDatabase ); |
|
270 |
} |
|
271 |
||
272 |
/*! |
|
273 |
* |
|
274 |
*/ |
|
275 |
void RadioHistoryModelPrivate::toggleTagging( const RadioHistoryItem& item, const int row ) |
|
276 |
{ |
|
277 |
QSqlQuery updateQuery = beginTransaction(); |
|
278 |
||
279 |
updateQuery.prepare( SQL_TOGGLE_TAG ); |
|
280 |
updateQuery.addBindValue( item.isTagged() ? 0 : 1 ); |
|
281 |
updateQuery.addBindValue( item.id() ); |
|
282 |
||
34 | 283 |
Operation operation = ChangeData; |
284 |
if ( mViewMode == ShowTagged && item.isTagged() ) { |
|
285 |
operation = RemoveRows; |
|
286 |
} |
|
287 |
commitTransaction( updateQuery, operation, row ); |
|
24 | 288 |
} |
289 |
||
290 |
/*! |
|
291 |
* |
|
292 |
*/ |
|
293 |
RadioHistoryItem RadioHistoryModelPrivate::itemAtIndex( const QModelIndex& index ) const |
|
294 |
{ |
|
295 |
LOG_METHOD; |
|
296 |
RadioHistoryItem item; |
|
297 |
QSqlRecord record = mQueryModel->record( index.row() ); |
|
298 |
item.data_ptr()->initFromRecord( record ); |
|
299 |
return item; |
|
300 |
} |
|
301 |
||
302 |
/*! |
|
303 |
* |
|
304 |
*/ |
|
305 |
void RadioHistoryModelPrivate::refreshModel() |
|
306 |
{ |
|
307 |
setViewMode( mViewMode ); |
|
308 |
} |
|
309 |
||
310 |
/*! |
|
311 |
* |
|
312 |
*/ |
|
313 |
QSqlQuery RadioHistoryModelPrivate::beginTransaction() |
|
314 |
{ |
|
315 |
LOG_METHOD; |
|
316 |
QSqlQuery newQuery( *mDatabase ); |
|
317 |
mDatabase->transaction(); |
|
318 |
return newQuery; |
|
319 |
} |
|
320 |
||
321 |
/*! |
|
322 |
* |
|
323 |
*/ |
|
324 |
void RadioHistoryModelPrivate::commitTransaction( QSqlQuery& query, Operation operation, int start, int end ) |
|
325 |
{ |
|
326 |
LOG_METHOD; |
|
327 |
if ( end == -1 ) { |
|
328 |
end = start; |
|
329 |
} |
|
330 |
||
331 |
bool success = false; |
|
332 |
Q_UNUSED( success ); |
|
333 |
if ( query.exec() ) { |
|
334 |
if ( operation == InsertRows ) { |
|
335 |
q_ptr->beginInsertRows( QModelIndex(), start, end ); |
|
336 |
} else if ( operation == RemoveRows ) { |
|
337 |
q_ptr->beginRemoveRows( QModelIndex(), start, end ); |
|
338 |
} |
|
339 |
||
340 |
success = mDatabase->commit(); |
|
341 |
LOG_ASSERT( success, LOG_FORMAT( "Commit failed! err: %s", GET_ERR_PTR( mDatabase ) ) ); |
|
342 |
||
343 |
refreshModel(); |
|
344 |
||
345 |
if ( operation == InsertRows ) { |
|
346 |
q_ptr->endInsertRows(); |
|
347 |
q_ptr->emitItemAdded(); |
|
348 |
} else if ( operation == RemoveRows ) { |
|
349 |
q_ptr->endRemoveRows(); |
|
350 |
} else if ( operation == ChangeData ) { |
|
351 |
q_ptr->reportChangedData( start ); |
|
352 |
} |
|
353 |
} else { |
|
354 |
LOG_FORMAT( "RadioHistoryModelPrivate::commitTransaction FAILED, rolling back: error = %s", GET_ERR( query ) ); |
|
355 |
success = mDatabase->rollback(); |
|
356 |
LOG_ASSERT( success, LOG_FORMAT( "Rollback failed! err: %s", GET_ERR_PTR( mDatabase ) ) ); |
|
357 |
} |
|
358 |
} |