|
1 /* |
|
2 * Copyright (c) 2006 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: Encapsulates operations on a table in the db |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <mpxlog.h> |
|
21 #include "mpxharvesterdbtable.h" |
|
22 #include "mpxdbcommon.h" |
|
23 #include "mpxharvesterdbitem.h" |
|
24 |
|
25 const TInt KStringLength = 300; |
|
26 |
|
27 // --------------------------------------------------------------------------- |
|
28 // Constructor |
|
29 // --------------------------------------------------------------------------- |
|
30 // |
|
31 CMPXHarvesterDatabaseTable::CMPXHarvesterDatabaseTable( RDbStoreDatabase& aDb ) |
|
32 : iDb( aDb ), |
|
33 iState( EInvalidState ) |
|
34 { |
|
35 } |
|
36 |
|
37 // --------------------------------------------------------------------------- |
|
38 // Two-Phased Constructor |
|
39 // --------------------------------------------------------------------------- |
|
40 // |
|
41 CMPXHarvesterDatabaseTable* CMPXHarvesterDatabaseTable::NewLC( |
|
42 RDbStoreDatabase& aDb ) |
|
43 { |
|
44 CMPXHarvesterDatabaseTable* self = |
|
45 new( ELeave ) CMPXHarvesterDatabaseTable( aDb ); |
|
46 CleanupStack::PushL( self ); |
|
47 return self; |
|
48 } |
|
49 |
|
50 |
|
51 // --------------------------------------------------------------------------- |
|
52 // destructor |
|
53 // --------------------------------------------------------------------------- |
|
54 // |
|
55 CMPXHarvesterDatabaseTable::~CMPXHarvesterDatabaseTable() |
|
56 { |
|
57 iView.Close(); |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // Open the Table to list all files in the db |
|
62 // --------------------------------------------------------------------------- |
|
63 // |
|
64 void CMPXHarvesterDatabaseTable::OpenAllFilesTableL() |
|
65 { |
|
66 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenAllFilesTableL <---"); |
|
67 |
|
68 // Close old view just in case |
|
69 iView.Close(); |
|
70 |
|
71 // Open all files query |
|
72 // select * from files |
|
73 // |
|
74 TBuf<KDbMaxTableCreationSQLLength> query; |
|
75 query.Append( KSelectAll ); |
|
76 query.Append( KHarvesterMainTable ); |
|
77 |
|
78 // Open the view |
|
79 User::LeaveIfError( iView.Prepare( iDb, query ) ); |
|
80 User::LeaveIfError( iView.Evaluate() ); |
|
81 |
|
82 iState = EAllItems; |
|
83 |
|
84 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenAllFilesTableL --->"); |
|
85 } |
|
86 |
|
87 // --------------------------------------------------------------------------- |
|
88 // Open the Table to a specific directory ( drive:\\path ) |
|
89 // --------------------------------------------------------------------------- |
|
90 // |
|
91 void CMPXHarvesterDatabaseTable::OpenDirectoryL( const TDesC& aDirectory ) |
|
92 { |
|
93 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenDirectoryL <---"); |
|
94 |
|
95 // Close old view just in case |
|
96 iView.Close(); |
|
97 |
|
98 HBufC* buffer = HBufC::NewLC(KStringLength); // 255 file path + 45 for single quotes |
|
99 // magic, this would break if we have > |
|
100 // 45 single quotes in a file name. |
|
101 TParsePtrC parse( aDirectory ); |
|
102 TPtr ptr = buffer->Des(); |
|
103 FindAndReplaceSingleQuote( parse.DriveAndPath(), ptr ); |
|
104 |
|
105 // Open all files query |
|
106 // select * from files where filepath='aDirectory' |
|
107 // |
|
108 TBuf<KDbMaxTableCreationSQLLength> query; |
|
109 query += KSelectAll; |
|
110 query += KHarvesterMainTable; |
|
111 query += KWhere; |
|
112 query += KHarPathName; |
|
113 query += KLike; |
|
114 query += KItemBracket; |
|
115 query += ptr; |
|
116 query += KWildcard; |
|
117 query += KItemBracket; |
|
118 |
|
119 // Open the view |
|
120 TDbQuery sqlQuery( query, EDbCompareFolded ); |
|
121 User::LeaveIfError( iView.Prepare( iDb, sqlQuery ) ); |
|
122 User::LeaveIfError( iView.Evaluate() ); |
|
123 |
|
124 iState = EDirectory; |
|
125 CleanupStack::PopAndDestroy(buffer); |
|
126 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenDirectoryL --->"); |
|
127 } |
|
128 |
|
129 // --------------------------------------------------------------------------- |
|
130 // Open the Table to a specific song view |
|
131 // --------------------------------------------------------------------------- |
|
132 // |
|
133 void CMPXHarvesterDatabaseTable::OpenItemL( const TDesC& aSongName ) |
|
134 { |
|
135 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenItemL <---"); |
|
136 |
|
137 // Close old view just in case |
|
138 iView.Close(); |
|
139 |
|
140 HBufC* buffer = HBufC::NewLC(KStringLength); // 255 file path + 45 for single quotes |
|
141 // magic, this would break if we have > 45 single quotes in a file name. |
|
142 TPtr ptr = buffer->Des(); |
|
143 // Open all files query |
|
144 // select * from files where path='path' AND filename='aSongName' |
|
145 // |
|
146 TParsePtrC parse( aSongName ); |
|
147 TBuf<KDbMaxTableCreationSQLLength> query; |
|
148 query += KSelectAll; |
|
149 query += KHarvesterMainTable; |
|
150 query += KWhere; |
|
151 query += KHarFileName; |
|
152 query += KEquals; |
|
153 query += KItemBracket; |
|
154 ptr.Copy( KNullDesC ); |
|
155 FindAndReplaceSingleQuote( parse.NameAndExt(), ptr ); |
|
156 query += ptr; |
|
157 query += KItemBracket; |
|
158 query += KAnd; |
|
159 query += KHarPathName; |
|
160 query += KEquals; |
|
161 query += KItemBracket; |
|
162 ptr.Copy( KNullDesC ); |
|
163 FindAndReplaceSingleQuote( parse.DriveAndPath(), ptr ); |
|
164 query += ptr; |
|
165 query += KItemBracket; |
|
166 |
|
167 // Open the view |
|
168 TDbQuery sqlQuery( query, EDbCompareFolded ); |
|
169 User::LeaveIfError( iView.Prepare( iDb, sqlQuery ) ); |
|
170 User::LeaveIfError( iView.Evaluate() ); |
|
171 |
|
172 iState = EFile; |
|
173 CleanupStack::PopAndDestroy( buffer ); |
|
174 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenItemL --->"); |
|
175 } |
|
176 |
|
177 // --------------------------------------------------------------------------- |
|
178 // Open a table to list of files with DRM flag set. |
|
179 // --------------------------------------------------------------------------- |
|
180 // |
|
181 void CMPXHarvesterDatabaseTable::OpenDrmL() |
|
182 { |
|
183 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenDrmL <---"); |
|
184 |
|
185 // Close old view just in case |
|
186 iView.Close(); |
|
187 |
|
188 HBufC* buffer = HBufC::NewLC(KStringLength); // 255 file path + 45 for single quotes |
|
189 // magic, this would break if we have > 45 single quotes in a file name. |
|
190 TPtr ptr = buffer->Des(); |
|
191 // Open all files query |
|
192 // select * from files where path='path' AND filename='aSongName' |
|
193 // |
|
194 TBuf<KDbMaxTableCreationSQLLength> query; |
|
195 query += KSelectAll; |
|
196 query += KHarvesterMainTable; |
|
197 query += KWhere; |
|
198 query += KHarItemDRM; |
|
199 query += KEquals; |
|
200 query += KOn; |
|
201 |
|
202 // Open the view |
|
203 TDbQuery sqlQuery( query, EDbCompareFolded ); |
|
204 User::LeaveIfError( iView.Prepare( iDb, sqlQuery ) ); |
|
205 User::LeaveIfError( iView.Evaluate() ); |
|
206 |
|
207 iState = EDrm; |
|
208 CleanupStack::PopAndDestroy( buffer ); |
|
209 MPX_DEBUG1("CMPXHarvesterDatabaseTable::OpenDrmL --->"); |
|
210 } |
|
211 |
|
212 // --------------------------------------------------------------------------- |
|
213 // Delete the item pointed to at the current row |
|
214 // --------------------------------------------------------------------------- |
|
215 // |
|
216 void CMPXHarvesterDatabaseTable::DeleteItemL(TBool aEndTransaction) |
|
217 { |
|
218 MPX_DEBUG1("CMPXHarvesterDatabaseTable::DeleteItemL <---"); |
|
219 |
|
220 #ifdef __PRINTDB__ |
|
221 PrintItemsInTableL(); |
|
222 #endif //__PRINTDB__ |
|
223 |
|
224 if (!iDb.InTransaction()) |
|
225 { |
|
226 iDb.Begin(); |
|
227 } |
|
228 // Safe row update |
|
229 TBool found = iView.FirstL(); |
|
230 if( !found ) |
|
231 { |
|
232 if ( aEndTransaction ) |
|
233 { |
|
234 iDb.Commit(); |
|
235 } |
|
236 User::Leave(KErrNotFound); |
|
237 } |
|
238 iView.GetL(); |
|
239 |
|
240 TRAPD( err, iView.DeleteL() ); |
|
241 if( err != KErrNone ) |
|
242 { |
|
243 iView.Cancel(); |
|
244 if ( aEndTransaction ) |
|
245 { |
|
246 iDb.Commit(); |
|
247 } |
|
248 User::Leave( err ); |
|
249 } |
|
250 if ( aEndTransaction ) |
|
251 { |
|
252 iDb.Commit(); |
|
253 } |
|
254 // DB Compaction |
|
255 struct RDbDatabase::TSize size = iDb.Size(); |
|
256 const TInt waterMark = 60; |
|
257 if (size.iUsage <= waterMark) |
|
258 { |
|
259 MPX_DEBUG1( "Compacting DB" ); |
|
260 iDb.Compact(); |
|
261 } |
|
262 |
|
263 MPX_DEBUG1("CMPXHarvesterDatabaseTable::DeleteItemL --->"); |
|
264 } |
|
265 |
|
266 // --------------------------------------------------------------------------- |
|
267 // Return the number of rows in the current view |
|
268 // --------------------------------------------------------------------------- |
|
269 // |
|
270 TInt CMPXHarvesterDatabaseTable::CountL() |
|
271 { |
|
272 return iView.CountL(); |
|
273 } |
|
274 |
|
275 // --------------------------------------------------------------------------- |
|
276 // Add an item to the table |
|
277 // --------------------------------------------------------------------------- |
|
278 // |
|
279 void CMPXHarvesterDatabaseTable::AddItemL( const TDesC& aPath, |
|
280 const TDesC& aFileName, |
|
281 const TTime aLastModTime, |
|
282 const TInt aColId, |
|
283 const TBool aDRM ) |
|
284 { |
|
285 MPX_DEBUG1("CMPXHarvesterDatabaseTable::AddItemL <---"); |
|
286 |
|
287 // Add the item into the db |
|
288 iView.InsertL(); |
|
289 iView.SetColL( KHarPathNameColumn, aPath ); |
|
290 iView.SetColL( KHarFileNameColumn, aFileName ); |
|
291 iView.SetColL( KHarModTimeColumn, aLastModTime ); |
|
292 iView.SetColL( KHarColDBIDColumn, aColId ); |
|
293 iView.SetColL( KHarDRMColumn, (TUint) aDRM ); |
|
294 |
|
295 // Safe update to database |
|
296 TRAPD( err, iView.PutL() ); |
|
297 if( KErrNone != err ) |
|
298 { |
|
299 iView.Cancel(); |
|
300 User::Leave( err ); |
|
301 } |
|
302 |
|
303 #ifdef __PRINTDB__ |
|
304 PrintItemsInTableL(); |
|
305 #endif //__PRINTDB__ |
|
306 |
|
307 MPX_DEBUG1("CMPXHarvesterDatabaseTable::AddItemL --->"); |
|
308 } |
|
309 |
|
310 // --------------------------------------------------------------------------- |
|
311 // Update the current row with the updated info |
|
312 // --------------------------------------------------------------------------- |
|
313 // |
|
314 void CMPXHarvesterDatabaseTable::UpdateItemL( const TDesC& /*aPath*/, |
|
315 const TTime aLastModTime, |
|
316 const TInt aColId, |
|
317 const TBool aDrm, |
|
318 TBool aNewTransaction ) |
|
319 { |
|
320 // Safe row update |
|
321 // |
|
322 TBool newTransaction = aNewTransaction && !iDb.InTransaction(); |
|
323 if ( newTransaction ) |
|
324 { |
|
325 iDb.Begin(); |
|
326 } |
|
327 TBool valid = iView.FirstL(); |
|
328 if( valid ) |
|
329 { |
|
330 iView.UpdateL(); |
|
331 iView.SetColL( KHarModTimeColumn, aLastModTime ); |
|
332 iView.SetColL( KHarColDBIDColumn, aColId ); |
|
333 iView.SetColL( KHarDRMColumn, (TUint) aDrm ); |
|
334 } |
|
335 else |
|
336 { |
|
337 if( newTransaction ) |
|
338 { |
|
339 iDb.Commit(); |
|
340 } |
|
341 User::Leave( KErrNotFound ); |
|
342 } |
|
343 |
|
344 TRAPD( err, iView.PutL() ); |
|
345 if( err != KErrNone ) |
|
346 { |
|
347 iView.Cancel(); |
|
348 if ( newTransaction ) |
|
349 { |
|
350 iDb.Commit(); |
|
351 } |
|
352 User::Leave( err ); |
|
353 } |
|
354 if ( newTransaction ) |
|
355 { |
|
356 iDb.Commit(); |
|
357 } |
|
358 |
|
359 // DB Compaction |
|
360 struct RDbDatabase::TSize size = iDb.Size(); |
|
361 const TInt waterMark = 60; |
|
362 if (size.iUsage <= waterMark) |
|
363 { |
|
364 if ( newTransaction ) |
|
365 { |
|
366 MPX_DEBUG1( "Compacting DB" ); |
|
367 iDb.Compact(); |
|
368 } |
|
369 } |
|
370 } |
|
371 |
|
372 // --------------------------------------------------------------------------- |
|
373 // Update the current row with the updated info |
|
374 // --------------------------------------------------------------------------- |
|
375 // |
|
376 void CMPXHarvesterDatabaseTable::UpdateItemL( const TTime aLastModTime, |
|
377 const TInt aColId, |
|
378 TBool aNewTransaction, |
|
379 const TDesC& aPath ) |
|
380 { |
|
381 // Safe row update |
|
382 // |
|
383 TBool newTransaction = aNewTransaction && !iDb.InTransaction(); |
|
384 if( iView.FirstL() ) |
|
385 { |
|
386 if ( newTransaction ) |
|
387 { |
|
388 iDb.Begin(); |
|
389 } |
|
390 iView.UpdateL(); |
|
391 if ( aPath != KNullDesC ) |
|
392 { |
|
393 TParsePtrC parser( aPath ); |
|
394 iView.SetColL( KHarPathNameColumn, parser.DriveAndPath() ); |
|
395 iView.SetColL( KHarFileNameColumn, parser.NameAndExt() ); |
|
396 } |
|
397 iView.SetColL( KHarModTimeColumn, aLastModTime ); |
|
398 if ( aColId != KNullUid.iUid ) |
|
399 { |
|
400 iView.SetColL( KHarColDBIDColumn, aColId ); |
|
401 } |
|
402 } |
|
403 else |
|
404 { |
|
405 User::Leave( KErrNotFound ); |
|
406 } |
|
407 |
|
408 TRAPD( err, iView.PutL() ); |
|
409 if( err != KErrNone ) |
|
410 { |
|
411 iView.Cancel(); |
|
412 if ( newTransaction ) |
|
413 { |
|
414 iDb.Commit(); |
|
415 } |
|
416 |
|
417 User::Leave( err ); |
|
418 } |
|
419 if ( newTransaction ) |
|
420 { |
|
421 iDb.Commit(); |
|
422 } |
|
423 |
|
424 // DB Compaction |
|
425 struct RDbDatabase::TSize size = iDb.Size(); |
|
426 const TInt waterMark = 60; |
|
427 if (size.iUsage <= waterMark) |
|
428 { |
|
429 if ( newTransaction ) |
|
430 { |
|
431 MPX_DEBUG1( "Compacting DB" ); |
|
432 iDb.Compact(); |
|
433 } |
|
434 } |
|
435 } |
|
436 |
|
437 // --------------------------------------------------------------------------- |
|
438 // Local function to cleanup an array |
|
439 // --------------------------------------------------------------------------- |
|
440 // |
|
441 static void CleanupArray( TAny* item ) |
|
442 { |
|
443 ((RPointerArray<CMPXHarvesterDbItem>*) item )->ResetAndDestroy(); |
|
444 } |
|
445 |
|
446 // --------------------------------------------------------------------------- |
|
447 // Create an array representing the table that is generated |
|
448 // --------------------------------------------------------------------------- |
|
449 // |
|
450 RPointerArray<CMPXHarvesterDbItem>* CMPXHarvesterDatabaseTable::CreateTableRepresentationL() |
|
451 { |
|
452 MPX_DEBUG1("CMPXHarvesterDatabaseTable::CreateTableRepresentationL <---"); |
|
453 |
|
454 RPointerArray<CMPXHarvesterDbItem>* array = |
|
455 new (ELeave) RPointerArray<CMPXHarvesterDbItem>; |
|
456 CleanupStack::PushL( TCleanupItem( CleanupArray, array) ); |
|
457 |
|
458 for (iView.FirstL();iView.AtRow();iView.NextL()) |
|
459 { |
|
460 // Retrieve the row |
|
461 iView.GetL(); |
|
462 |
|
463 CMPXHarvesterDbItem* item = new(ELeave) CMPXHarvesterDbItem(); |
|
464 CleanupStack::PushL( item ); |
|
465 |
|
466 // Setup the item |
|
467 HBufC* filepath = ReadLongTextLC( KHarPathNameColumn ); |
|
468 HBufC* songname = ReadLongTextLC( KHarFileNameColumn ); |
|
469 item->iFile = HBufC::NewL( filepath->Length() + songname->Length() ); |
|
470 TPtr buf = item->iFile->Des(); |
|
471 buf.Append( *filepath ); |
|
472 buf.Append( *songname ); |
|
473 CleanupStack::PopAndDestroy( 2, filepath ); |
|
474 |
|
475 item->iLastModifiedTime = iView.ColTime( KHarModTimeColumn ); |
|
476 item->iColId = iView.ColInt( KHarColDBIDColumn ); |
|
477 item->iDrm = iView.ColUint( KHarDRMColumn ); |
|
478 |
|
479 User::LeaveIfError( array->InsertInOrderAllowRepeats( item, |
|
480 CMPXHarvesterDbItem::Compare ) ); |
|
481 CleanupStack::Pop( item ); |
|
482 } |
|
483 |
|
484 CleanupStack::Pop(); // cleanup |
|
485 MPX_DEBUG1("CMPXHarvesterDatabaseTable::CreateTableRepresentationL --->"); |
|
486 return array; |
|
487 } |
|
488 |
|
489 // --------------------------------------------------------------------------- |
|
490 // Read out the text at the current row and at a specific column |
|
491 // --------------------------------------------------------------------------- |
|
492 // |
|
493 HBufC* CMPXHarvesterDatabaseTable::ReadLongTextLC( TInt aColumn ) |
|
494 { |
|
495 TInt len = iView.ColLength( aColumn ); |
|
496 |
|
497 HBufC* buf = HBufC::NewLC(len); |
|
498 TPtr value( buf->Des() ); |
|
499 if ( len>0 ) |
|
500 { |
|
501 RDbColReadStream strm; |
|
502 strm.OpenLC(iView, aColumn ); |
|
503 strm.ReadL( value, len ); |
|
504 strm.Close(); |
|
505 CleanupStack::PopAndDestroy( &strm ); |
|
506 } |
|
507 return buf; |
|
508 } |
|
509 |
|
510 // --------------------------------------------------------------------------- |
|
511 // Fixes single quote query issues |
|
512 // --------------------------------------------------------------------------- |
|
513 // |
|
514 void CMPXHarvesterDatabaseTable::FindAndReplaceSingleQuote(const TDesC& aSrc, |
|
515 TDes& aTrg) |
|
516 { |
|
517 |
|
518 TPtrC ch; |
|
519 |
|
520 TInt srcLen = aSrc.Length(); |
|
521 |
|
522 for (TInt i = 0; i < srcLen; ++i) |
|
523 { |
|
524 ch.Set(&aSrc[i], 1); |
|
525 aTrg.Append(ch); |
|
526 if ( ch.CompareF(_L("'")) == 0) |
|
527 { |
|
528 aTrg.Append(ch); |
|
529 } |
|
530 } |
|
531 } |
|
532 |
|
533 #ifdef __PRINTDB__ |
|
534 // --------------------------------------------------------------------------- |
|
535 // Print out all items in the current table |
|
536 // --------------------------------------------------------------------------- |
|
537 // |
|
538 void CMPXHarvesterDatabaseTable::PrintItemsInTableL() |
|
539 { |
|
540 for (iView.FirstL();iView.AtRow();iView.NextL()) |
|
541 { |
|
542 // Retrieve the row |
|
543 iView.GetL(); |
|
544 |
|
545 // Print out each row |
|
546 HBufC* filepath = ReadLongTextLC( KHarPathNameColumn ); |
|
547 HBufC* songname = ReadLongTextLC( KHarFileNameColumn ); |
|
548 TTime time = iView.ColTime( KHarModTimeColumn ); |
|
549 MPX_DEBUG4("Filepath: %S Filename:%S Time %i", filepath, songname, time.Int64() ); |
|
550 CleanupStack::PopAndDestroy( 2, filepath ); |
|
551 } |
|
552 } |
|
553 #endif // __PRINTDB__ |
|
554 |