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