63
|
1 |
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
//
|
|
15 |
|
|
16 |
/**
|
|
17 |
@file
|
|
18 |
@internalComponent
|
|
19 |
@released
|
|
20 |
*/
|
|
21 |
|
|
22 |
#include "persistencelayerimpl.h"
|
|
23 |
#include "clplcontactproperties.h"
|
|
24 |
#include "cntfilesearch.h"
|
|
25 |
#include <bautils.h>
|
|
26 |
#include "persistencelayer.h"
|
|
27 |
|
|
28 |
const TInt KDriveNameWidth = 2; // e.g. "C:"
|
|
29 |
|
|
30 |
_LIT(KSqLiteFilePrefix, "SQLite__");
|
|
31 |
|
|
32 |
// Transaction constants (only used by CPplContactsFile)
|
|
33 |
_LIT(KStartTransaction,"BEGIN TRANSACTION;");
|
|
34 |
_LIT(KCommitTransaction,"COMMIT;");
|
|
35 |
_LIT(KRollbackTransaction,"ROLLBACK;");
|
|
36 |
|
|
37 |
/**
|
|
38 |
@SYMPatchable
|
|
39 |
@publishedPartner
|
|
40 |
@released
|
|
41 |
|
|
42 |
Patchable optional constant that overrides the cache size of the Contacts model databases set in the
|
|
43 |
SqlServer.cfg file in the SQL data cage.
|
|
44 |
If this value is left at 0, the cache size from the SqlServer.cfg file is used.
|
|
45 |
The cache size affects the size of the cache in Kb that opens and creates Contacts model files.
|
|
46 |
See SQLite documentation for more details.
|
|
47 |
|
|
48 |
The constant can be changed at ROM build time using patchdata OBY keyword.
|
|
49 |
*/
|
|
50 |
IMPORT_C extern const TInt KContactsModelSqliteDbCacheSize;
|
|
51 |
|
|
52 |
const TInt KCacheDataSize = 8; // The size in chars of patchable constant 'KContactsModelSqliteDbCacheSize' passed in
|
|
53 |
|
|
54 |
//configure string used to create and open sqlite database
|
|
55 |
_LIT8(KSqliteCacheSize, "cache_size=%d");
|
|
56 |
|
|
57 |
/**
|
|
58 |
CPplContactsFile NewL.
|
|
59 |
*/
|
|
60 |
CPplContactsFile* CPplContactsFile::NewL(CLplContactProperties& aProps, MContactDbObserver* aObserver)
|
|
61 |
{
|
|
62 |
CPplContactsFile* self = new (ELeave) CPplContactsFile(aProps, aObserver);
|
|
63 |
CleanupStack::PushL(self);
|
|
64 |
self->ConstructL();
|
|
65 |
CleanupStack::Pop(self);
|
|
66 |
return self;
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
CPplContactsFile ConstructL.
|
|
71 |
*/
|
|
72 |
void CPplContactsFile::ConstructL()
|
|
73 |
{
|
|
74 |
iItemManager = CPplContactItemManager::NewL(iDatabase, *this, iContactProperties, iIccContactStore);
|
|
75 |
iContactProperties.SetContactItemManagerL(*iItemManager);
|
|
76 |
|
|
77 |
iConfigureStr = NULL;
|
|
78 |
if(KContactsModelSqliteDbCacheSize > 0)
|
|
79 |
{
|
|
80 |
//Create configure string to be used when creating/opening database
|
|
81 |
iConfigureStr = HBufC8::NewL(KSqliteCacheSize().Length() + KCacheDataSize);
|
|
82 |
TPtr8 ptrConfigureStr = iConfigureStr->Des();
|
|
83 |
ptrConfigureStr.Format(KSqliteCacheSize(), KContactsModelSqliteDbCacheSize);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
|
|
88 |
/**
|
|
89 |
CPplContactsFile constructor.
|
|
90 |
*/
|
|
91 |
CPplContactsFile::CPplContactsFile(CLplContactProperties& aProps, MContactDbObserver* aObserver)
|
|
92 |
:
|
|
93 |
iActive(EFalse),
|
|
94 |
iDbObserver(aObserver),
|
|
95 |
iContactProperties(aProps),
|
|
96 |
iIccContactStore(aProps)
|
|
97 |
{
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
CPplContactsFile destructor.
|
|
102 |
*/
|
|
103 |
CPplContactsFile::~CPplContactsFile()
|
|
104 |
{
|
|
105 |
Close();
|
|
106 |
delete iItemManager;
|
|
107 |
delete iConfigureStr;
|
|
108 |
iSqlDatabaseObservers.Reset();
|
|
109 |
}
|
|
110 |
|
|
111 |
|
|
112 |
/**
|
|
113 |
Set the database observer for event propagation.
|
|
114 |
*/
|
|
115 |
void CPplContactsFile::RegisterDbObserver(MContactDbObserver& aDbObserver)
|
|
116 |
{
|
|
117 |
iDbObserver = &aDbObserver;
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
Utility method used to construct physical path for a given file name
|
|
122 |
|
|
123 |
@param aPhysicalFileName in/out paramter; file name to which physical path will be append
|
|
124 |
@param aSecuredFileName secured file name
|
|
125 |
*/
|
|
126 |
void CPplContactsFile::GetPhysicalFileNameL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName)
|
|
127 |
{
|
|
128 |
GetPhysicalPathL(aPhysicalFileName, aSecuredFileName);
|
|
129 |
aPhysicalFileName.Append(KSqLiteFilePrefix);
|
|
130 |
aPhysicalFileName.Append(aSecuredFileName.Right(aSecuredFileName.Length() - KDriveNameWidth));
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
Utility method used to construct physical path for a given file name
|
|
135 |
|
|
136 |
@param aPhysicalFileName in/out paramter; file name to which physical path will be append
|
|
137 |
@param aSecuredFileName secured file name
|
|
138 |
*/
|
|
139 |
void CPplContactsFile::GetPhysicalPathL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName)
|
|
140 |
{
|
|
141 |
const TUint16 KDriveDelimiter = ':';
|
|
142 |
|
|
143 |
// Check for the drive delimiter.
|
|
144 |
__ASSERT_ALWAYS(aSecuredFileName.Length() > 2 &&
|
|
145 |
aSecuredFileName[1] == KDriveDelimiter, User::Leave(KErrBadName));
|
|
146 |
|
|
147 |
// Check for malformed file name (path included explicitly).
|
|
148 |
__ASSERT_ALWAYS(aSecuredFileName.Locate('\\') == KErrNotFound, User::Leave(KErrBadName));
|
|
149 |
|
|
150 |
// Get the private path from the file session.
|
|
151 |
const TInt KMaxPrivatePathLength = 32;
|
|
152 |
TBuf<KMaxPrivatePathLength> privatePath;
|
|
153 |
|
|
154 |
LocalFsL();
|
|
155 |
iLocalFs.PrivatePath(privatePath);
|
|
156 |
|
|
157 |
aPhysicalFileName = aSecuredFileName.Left(KDriveNameWidth);
|
|
158 |
|
|
159 |
// Set current drive.
|
|
160 |
iDatabaseDrive = aPhysicalFileName;
|
|
161 |
|
|
162 |
aPhysicalFileName.Append(privatePath);
|
|
163 |
|
|
164 |
// Make sure private path exists.
|
|
165 |
TInt err = iLocalFs.MkDirAll(aPhysicalFileName);
|
|
166 |
if (err != KErrNone && err != KErrAlreadyExists)
|
|
167 |
{
|
|
168 |
User::Leave(err);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
Open the given database file.
|
|
174 |
*/
|
|
175 |
void CPplContactsFile::OpenL(const TDesC& aFileName, TBool aNotify)
|
|
176 |
{
|
|
177 |
// Contact databases are in the Contact model private directory.
|
|
178 |
TFileName fileName;
|
|
179 |
GetPhysicalFileNameL(fileName, aFileName);
|
|
180 |
|
|
181 |
iDatabase.Close();
|
|
182 |
iDatabase.OpenL(fileName, iConfigureStr);
|
|
183 |
|
|
184 |
iFileIsOpen = ETrue;
|
|
185 |
GenerateNotificationEventL(aNotify);
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
Utility method used to generate EContactDbObserverEventTablesOpened event
|
|
190 |
*/
|
|
191 |
void CPplContactsFile::GenerateNotificationEventL(TBool aNotify)
|
|
192 |
{
|
|
193 |
#if defined(__PROFILE_DEBUG__)
|
|
194 |
RDebug::Print(_L("[CNTMODEL] MTD: CPplContactsFile::GenerateNotificationEventL"));
|
|
195 |
#endif
|
|
196 |
|
|
197 |
// Generate an event - this event is generated if this was an explicit call from the
|
|
198 |
// server to opentables or recover database - ie A call on the contactsDatabase API
|
|
199 |
if (iDbObserver && aNotify)
|
|
200 |
{
|
|
201 |
TContactDbObserverEvent event;
|
|
202 |
event.iType = EContactDbObserverEventTablesOpened;
|
|
203 |
event.iContactId = 0;
|
|
204 |
event.iConnectionId = 0;
|
|
205 |
TRAP_IGNORE(iDbObserver->HandleDatabaseEventL(event));
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
|
|
210 |
/**
|
|
211 |
Open the database tables (a state change, no action on file necessary).
|
|
212 |
*/
|
|
213 |
void CPplContactsFile::OpenTablesL(TBool aNotify)
|
|
214 |
{
|
|
215 |
// Do nothing but notify
|
|
216 |
|
|
217 |
#if defined(__PROFILE_DEBUG__)
|
|
218 |
_LIT(KMessage, "[CNTMODEL] MTD: CPplContactsFile::OpenTablesL");
|
|
219 |
RDebug::Print(KMessage);
|
|
220 |
#endif
|
|
221 |
|
|
222 |
iFileIsOpen = ETrue;
|
|
223 |
GenerateNotificationEventL(aNotify);
|
|
224 |
}
|
|
225 |
|
|
226 |
|
|
227 |
/**
|
|
228 |
Close the database tables leaving the iDatabase itself open.
|
|
229 |
Despite the trailing 'L', this method cannot leave.
|
|
230 |
*/
|
|
231 |
void CPplContactsFile::CloseTablesL(TBool aNotify)
|
|
232 |
{
|
|
233 |
// Do nothing but notify
|
|
234 |
|
|
235 |
if (iDbObserver && aNotify) // Observer is optional.
|
|
236 |
{
|
|
237 |
// Notify observers that table is closed.
|
|
238 |
TContactDbObserverEvent event;
|
|
239 |
event.iType = EContactDbObserverEventTablesClosed;
|
|
240 |
event.iContactId = 0;
|
|
241 |
event.iConnectionId = 0;
|
|
242 |
TRAP_IGNORE(iDbObserver->HandleDatabaseEventL(event));
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
|
|
247 |
/**
|
|
248 |
File cleanup class used if creating database and its tables leaves.
|
|
249 |
*/
|
|
250 |
class TFileCleanup
|
|
251 |
{
|
|
252 |
public:
|
|
253 |
TFileCleanup(RSqlDatabase& aDb, RFs& aFs, const TDesC& aFileName) : iDb(aDb),iFs(aFs),iFileName(aFileName) {}
|
|
254 |
static void Cleanup(TAny* aSelf) { STATIC_CAST(TFileCleanup*,aSelf)->DoCleanup(); }
|
|
255 |
|
|
256 |
private:
|
|
257 |
void DoCleanup()
|
|
258 |
{
|
|
259 |
iDb.Close();
|
|
260 |
(void)iFs.Delete(iFileName);
|
|
261 |
}
|
|
262 |
|
|
263 |
public:
|
|
264 |
RSqlDatabase& iDb;
|
|
265 |
RFs& iFs;
|
|
266 |
const TDesC& iFileName;
|
|
267 |
};
|
|
268 |
|
|
269 |
|
|
270 |
/**
|
|
271 |
Create a new database.
|
|
272 |
*/
|
|
273 |
EXPORT_C void CPplContactsFile::CreateL(const TDesC& aFileName, TPlCreateMode aMode)
|
|
274 |
{
|
|
275 |
TFileName fileName;
|
|
276 |
GetPhysicalFileNameL(fileName, aFileName);
|
|
277 |
|
|
278 |
TUint attVal;
|
|
279 |
LocalFsL();
|
|
280 |
TInt err = iLocalFs.Att(fileName, attVal);
|
|
281 |
TBool fileExists = (err == KErrNone);
|
|
282 |
|
|
283 |
if (fileExists)
|
|
284 |
{
|
|
285 |
switch (aMode)
|
|
286 |
{
|
|
287 |
case EPlLeaveIfExist:
|
|
288 |
User::Leave(KErrAlreadyExists);
|
|
289 |
break;
|
|
290 |
|
|
291 |
case EPlOverwrite:
|
|
292 |
err = iLocalFs.Delete(fileName);
|
|
293 |
break;
|
|
294 |
}
|
|
295 |
}
|
|
296 |
|
|
297 |
// If the database is not created propertly delete the database file using
|
|
298 |
// the cleanup item.
|
|
299 |
TFileCleanup cleanupData(iDatabase, iLocalFs, fileName);
|
|
300 |
CleanupStack::PushL(TCleanupItem(TFileCleanup::Cleanup,&cleanupData));
|
|
301 |
|
|
302 |
if ((err != KErrNone) && (err != KErrNotFound))
|
|
303 |
{
|
|
304 |
User::LeaveIfError(err);
|
|
305 |
}
|
|
306 |
|
|
307 |
User::LeaveIfError(iDatabase.Create(fileName, iConfigureStr));
|
|
308 |
iItemManager->CreateTablesL();
|
|
309 |
iContactProperties.SystemTemplateManager().RecreateSystemTemplateL();
|
|
310 |
|
|
311 |
CleanupStack::Pop(); // The TCleanupItem.
|
|
312 |
|
|
313 |
iDatabase.Close();
|
|
314 |
}
|
|
315 |
|
|
316 |
|
|
317 |
/**
|
|
318 |
Delete the given database.
|
|
319 |
*/
|
|
320 |
void CPplContactsFile::DeleteL(const TDesC& aFileName)
|
|
321 |
{
|
|
322 |
TFileName fileName;
|
|
323 |
GetPhysicalFileNameL(fileName, aFileName);
|
|
324 |
|
|
325 |
User::LeaveIfError(iLocalFs.Delete(fileName));
|
|
326 |
iFileIsOpen = EFalse;
|
|
327 |
}
|
|
328 |
|
|
329 |
|
|
330 |
/**
|
|
331 |
Get a list of the contacts database on the given drive.
|
|
332 |
*/
|
|
333 |
CDesCArray* CPplContactsFile::ListL(TDriveUnit* aDriveUnit)
|
|
334 |
{
|
|
335 |
LocalFsL();
|
|
336 |
return CCntFileScanner::ListFilesL(iLocalFs, aDriveUnit);
|
|
337 |
}
|
|
338 |
|
|
339 |
|
|
340 |
/**
|
|
341 |
Close the database.
|
|
342 |
*/
|
|
343 |
void CPplContactsFile::Close(TBool aNotify)
|
|
344 |
{
|
|
345 |
// Close the resource which depends on iDatabase before it will be closed.
|
|
346 |
for (TInt i = 0; i < iSqlDatabaseObservers.Count(); i++ )
|
|
347 |
{
|
|
348 |
iSqlDatabaseObservers[i]->OnCloseL();
|
|
349 |
}
|
|
350 |
|
|
351 |
iDatabase.Close();
|
|
352 |
iFileIsOpen = EFalse;
|
|
353 |
|
|
354 |
REComSession::FinalClose(); // This line is necessary to make sure the plug-in is unloaded properly
|
|
355 |
iLocalFs.Close(); //we now use a local File Session
|
|
356 |
|
|
357 |
iIccContactStore.Close();
|
|
358 |
|
|
359 |
TRAP_IGNORE(CloseTablesL(aNotify) ); // CloseTablesL() cannot leave anyway but still
|
|
360 |
// trap in case implementation changes later
|
|
361 |
}
|
|
362 |
|
|
363 |
|
|
364 |
/**
|
|
365 |
Starts a new transaction if one is not currently active otherwise leave.
|
|
366 |
*/
|
|
367 |
void CPplContactsFile::StartTransactionL()
|
|
368 |
{
|
|
369 |
if (iActive)
|
|
370 |
{
|
|
371 |
User::Leave(KErrInUse);
|
|
372 |
}
|
|
373 |
User::LeaveIfError(iDatabase.Exec(KStartTransaction));
|
|
374 |
iIccContactStore.StartTransactionL();
|
|
375 |
iActive = ETrue;
|
|
376 |
}
|
|
377 |
|
|
378 |
|
|
379 |
/**
|
|
380 |
Commit all changes made since the transaction was started.
|
|
381 |
*/
|
|
382 |
void CPplContactsFile::CommitCurrentTransactionL(TUint aSessionId)
|
|
383 |
{
|
|
384 |
|
|
385 |
if (!iActive)
|
|
386 |
{
|
|
387 |
User::Leave(KErrNotReady);
|
|
388 |
}
|
|
389 |
iIccContactStore.CommitCurrentTransactionL(aSessionId);
|
|
390 |
User::LeaveIfError(iDatabase.Exec(KCommitTransaction));
|
|
391 |
iActive = EFalse;
|
|
392 |
}
|
|
393 |
|
|
394 |
|
|
395 |
/**
|
|
396 |
Rollback all changes made since the active the transaction was started.
|
|
397 |
*/
|
|
398 |
void CPplContactsFile::RollbackCurrentTransactionL(TUint aSessionId)
|
|
399 |
{
|
|
400 |
// Check that the database is open before rolling back.
|
|
401 |
if (iFileIsOpen && iActive)
|
|
402 |
{
|
|
403 |
iActive = EFalse;
|
|
404 |
iIccContactStore.RollbackCurrentTransaction(aSessionId);
|
|
405 |
User::LeaveIfError(iDatabase.Exec(KRollbackTransaction));
|
|
406 |
}
|
|
407 |
}
|
|
408 |
|
|
409 |
/**
|
|
410 |
Gets the size of the database file in bytes.
|
|
411 |
|
|
412 |
@return The size of the contact database.
|
|
413 |
*/
|
|
414 |
TInt CPplContactsFile::FileSize() const
|
|
415 |
{
|
|
416 |
return iDatabase.Size();
|
|
417 |
}
|
|
418 |
|
|
419 |
|
|
420 |
/**
|
|
421 |
Gets the current database drive. The database drive is the drive on which
|
|
422 |
the default contact database is located.
|
|
423 |
|
|
424 |
@param aDriveUnit On return, contains the database drive.
|
|
425 |
*/
|
|
426 |
void CPplContactsFile::DatabaseDrive(TDriveUnit& aDriveUnit)
|
|
427 |
{
|
|
428 |
aDriveUnit = iDatabaseDrive;
|
|
429 |
}
|
|
430 |
|
|
431 |
|
|
432 |
/**
|
|
433 |
A static method to determine if a given Contacts database exists.
|
|
434 |
|
|
435 |
@param aFileName The Contacts database to search for.
|
|
436 |
|
|
437 |
@return ETrue if the Contacts database is found, EFalse otherwise.
|
|
438 |
*/
|
|
439 |
TBool CPplContactsFile::DatabaseExistsL(const TDesC& aFileName)
|
|
440 |
{
|
|
441 |
TFileName fileName;
|
|
442 |
GetPhysicalFileNameL(fileName, aFileName);
|
|
443 |
LocalFsL();
|
|
444 |
return BaflUtils::FileExists(iLocalFs, fileName);
|
|
445 |
}
|
|
446 |
|
|
447 |
|
|
448 |
/**
|
|
449 |
* Add an observer for monitoring RSqlDatabase.
|
|
450 |
*
|
|
451 |
* @param aSqlDatabaseObserver The observer for monitoring RSqlDatabase.
|
|
452 |
*
|
|
453 |
* @return None
|
|
454 |
*/
|
|
455 |
void CPplContactsFile::AddSqlDBObserverL(
|
|
456 |
MLplSqlDatabaseObserver& aSqlDatabaseObserver )
|
|
457 |
{
|
|
458 |
iSqlDatabaseObservers.AppendL( &aSqlDatabaseObserver );
|
|
459 |
}
|
|
460 |
|
|
461 |
|
|
462 |
/**
|
|
463 |
* Remove an RSqlDatabase observer.
|
|
464 |
*
|
|
465 |
* @param aSqlDatabaseObserver The observer is to be removed.
|
|
466 |
*
|
|
467 |
* @return None
|
|
468 |
*/
|
|
469 |
void CPplContactsFile::RemoveSqlDBObserverL(
|
|
470 |
MLplSqlDatabaseObserver& aSqlDatabaseObserver )
|
|
471 |
{
|
|
472 |
TInt id = iSqlDatabaseObservers.Find( &aSqlDatabaseObserver );
|
|
473 |
if ( id != KErrNotFound )
|
|
474 |
{
|
|
475 |
iSqlDatabaseObservers.Remove( id );
|
|
476 |
}
|
|
477 |
}
|
|
478 |
|
|
479 |
|