summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntsrv/src/ccntbackuprestoreagent.cpp
blob: 5204574540b712a13fb94ece3e0d5a24e4adf79e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


#include "connect/sbdefs.h" // for conn::eburnormal etc.
#include <utf.h> // for cnvutfconverter::convertfromunicodetoutf8().

#include "ccntbackuprestoreagent.h"
#include "cinifilemanager.h"
#include "ccntdbmanagercontroller.h"
#include "ccntserver.h" // for kcntnullconnectionid.
#include "ccntlogger.h"


const TInt CCntBackupRestoreAgent::KMaxFileNameLength = 256;

_LIT(KSqLiteFilePrefix, "SQLite__");

/** Object factory method for CCntBackupRestoreAgent.
*/
CCntBackupRestoreAgent* CCntBackupRestoreAgent::NewL(RFs& aFs, CCntDbManagerController& aDbMgrCtrlr)
	{
	CCntBackupRestoreAgent* self = new(ELeave) CCntBackupRestoreAgent(aFs, aDbMgrCtrlr);
	return self;
	}


/** Destructor. 
*/
CCntBackupRestoreAgent::~CCntBackupRestoreAgent()
	{
	Cancel();
	iBackupRestoreNotification.Close();
	}


/** Process any initial notification and start monitoring for changes in the
	Backup/Restore property used by the SBEngine component.
*/
void CCntBackupRestoreAgent::StartL()
	{
	TInt newState = 0;
	
	// Check to see if a Backup/Restore is currently in progress.
	if (iBackupRestoreNotification.Get(newState) != KErrNotFound)
		{
		ProcessInitialStateL(newState);
		}

	SetActive();
	}


/** First phase default constructor.
*/
CCntBackupRestoreAgent::CCntBackupRestoreAgent(RFs& aFs, CCntDbManagerController& aDbMgrCtrlr)
	: CActive(CActive::EPriorityStandard), iFs(aFs), iDbMgrCtrlr(aDbMgrCtrlr)
	{
	CActiveScheduler::Add(this);

	iEvent.iType = EContactDbObserverEventNull;		// Changed as required.
	iEvent.iContactId =	KNullContactId;				// Always this value.
	iEvent.iConnectionId = KCntNullConnectionId;	// Always this value.

	// Attach and subscribe to the Backup/Restore property used by the SBEngine
	// component.
	iBackupRestoreNotification.Attach(KUidSystemCategory, conn::KUidBackupRestoreKey);
	iBackupRestoreNotification.Subscribe(iStatus);
	}


/** Indicates if a Backup operation is currently in progress.
	@return ETrue if operation in progress else EFalse.
*/
TBool CCntBackupRestoreAgent::BackupInProgress() const
	{
	return (iCurrentState & (conn::EBURBackupPartial | conn::EBURBackupFull));
	}


/** Indicates if a Restore operation is currently in progress.
	@return	ETrue if operation in progress else EFalse.
*/
TBool CCntBackupRestoreAgent::RestoreInProgress() const
	{
	return (iCurrentState & (conn::EBURRestorePartial | conn::EBURRestoreFull));
	}


/** Create a new XML backup registration file for the file aFileName.  The file
	will be created on the same drive as aFileName in the Contacts Model private
	data directory.

	@param aFileName File name including drive letter i.e. <drive>:<file>.
*/
void CCntBackupRestoreAgent::CreateBackupRegistrationFileL(const TDesC& aFileName)
	{
	_LIT8(KXmlFilePart1, "<?xml version=\"1.0\" standalone=\"yes\"?>\r\n"
		"<backup_registration>\r\n"
		"    <passive_backup base_backup_only=\"yes\">\r\n"
		"        <include_file name=\"");
	_LIT8(KXmlFilePart2, "\"/>\r\n"
		"    </passive_backup>\r\n"
		"</backup_registration>\r\n");
		
	// Find out the drive held in aFileName.  If a drive letter is not present
	// then the method leaves.
	TParsePtrC parseFileName(aFileName);
	User::LeaveIfError(parseFileName.DrivePresent());
	
	TPath privatePath;
	User::LeaveIfError(iFs.PrivatePath(privatePath));
	User::LeaveIfError(iFs.SetSessionPath(parseFileName.Drive()));
	User::LeaveIfError(iFs.SetSessionPath(privatePath));

	// If the path does not exist create it.
	TInt err = iFs.MkDirAll(privatePath);

	if (err != KErrAlreadyExists && err != KErrNone)
		{
		User::Leave(err);
		}

	HBufC* newFileName = CreateBackupRegistrationFileNameLC(parseFileName.NameAndExt());
	TPtr newFileNamePtr(newFileName->Des());

	// Create registration file.
	RFile file;
	CleanupClosePushL(file);

	err = file.Create(iFs, *newFileName, EFileWrite);

	// Registration file already exists so we're done.
	if (err == KErrAlreadyExists)
		{
		CleanupStack::PopAndDestroy(2, newFileName); // file, newFileName
		return;
		}

	if (err)
		{
		User::Leave(err);
		}
		
	HBufC* nameAndExt = NULL;
	nameAndExt = HBufC::NewLC(KSqLiteFilePrefix().Length() + parseFileName.NameAndExt().Length());
    if (parseFileName.NameAndExt().Compare(KContactsIniFileName) != 0) 
        {
        nameAndExt->Des().Append(KSqLiteFilePrefix);
        }                                             
	nameAndExt->Des().Append(parseFileName.NameAndExt());

	// Convert filename and extension to UTF8 before writing to file.
	HBufC8* fileNameAndExt8 = HBufC8::NewLC(nameAndExt->Length());
	TPtr8 pFileNameAndExt8(fileNameAndExt8->Des());
	
	User::LeaveIfError(CnvUtfConverter::ConvertFromUnicodeToUtf8(pFileNameAndExt8,
		*nameAndExt));

	// Write data into file.
	User::LeaveIfError(file.Write(KXmlFilePart1()));
	User::LeaveIfError(file.Write(pFileNameAndExt8));
	User::LeaveIfError(file.Write(KXmlFilePart2()));
	User::LeaveIfError(file.Flush());

	CleanupStack::PopAndDestroy(4, newFileName); // fileNameAndExt8, file, nameAndExt, newFileName
	}


/** Delete the XML backup registration file associated with the filename.  The
	registration file will be found on the same drive as aFileName.

	@param aFileName File name including drive letter i.e. <drive>:<file>.
*/
void CCntBackupRestoreAgent::DeleteBackupRegistrationFileL(const TDesC& aFileName)
	{
	TParsePtrC parseFileName(aFileName);
	
	// Find out the drive held in aFileName.  If a drive letter is not present
	// then the method leaves.
	User::LeaveIfError(parseFileName.DrivePresent());
	
	TPath privatePath;
	User::LeaveIfError(iFs.PrivatePath(privatePath));
	User::LeaveIfError(iFs.SetSessionPath(parseFileName.Drive()));
	User::LeaveIfError(iFs.SetSessionPath(privatePath));

	HBufC* regFileName = CreateBackupRegistrationFileNameLC(parseFileName.NameAndExt());
	TPtrC regFileNamePtr(regFileName->Des());

	// Check if registration file exists and if so delete it.
	TEntry entry;
	TInt err = iFs.Entry(regFileNamePtr, entry);

	if((err == KErrNone) && !entry.IsDir())
		{
		User::LeaveIfError(iFs.Delete(regFileNamePtr));
		}

	CleanupStack::PopAndDestroy(regFileName);
	}


/** Handle change in value of the property KUidBackupRestoreKey (used to notify
	subscribers of Backup/Restore events).
	@see CActive
*/
void CCntBackupRestoreAgent::RunL()
	{
	// Resubscribe before dealing with the current notification.
	iBackupRestoreNotification.Subscribe(iStatus);
	SetActive();

	TInt newState = 0;
	
	// Flag updated.  Decide what to do in ReceivedNotificationL().
	if (iBackupRestoreNotification.Get(newState) != KErrNotFound)
		{
		ReceivedNotificationL(newState);
		}
	}


/** Recover if RunL() leaves.
	@see CActive
*/
TInt CCntBackupRestoreAgent::RunError(TInt /*aError*/)
	{
	
	DEBUG_PRINT1(__VERBOSE_DEBUG__,_L("[CNTMODEL] CCntBackupRestoreAgent::RunError(): RunL() has left!\n")); 

	return (KErrNone);
	}


/**
	@see CActive
*/
void CCntBackupRestoreAgent::DoCancel()
	{
	iBackupRestoreNotification.Cancel();
	}


/** Determines what operation is starting or has completed and calls the
	relevant observer function.

	@param aNewState Flags indicating what the new Backup/Restore state is:

	1. No Backup/Restore is taking place = EBURNormal | ENoBackup		
	2. Backup operation starting = (EBURBackupFull || EBURBackupPartial) &&
	   EBackupBase
	3. Restore operation starting = (EBURRestoreFull || EBURRestorePartial) &&
	   EBackupBase
*/
void CCntBackupRestoreAgent::ReceivedNotificationL(TInt aNewState)
	{
	// We are starting a a new operation...
	// Was there a previous Restore which failed to complete?
	if (!(aNewState & (conn::EBURNormal | conn::EBURUnset)) && RestoreInProgress())
		{
		iCurrentState = aNewState;
		RestoreCompletedL(KErrAbort);
		}
	
	// A Base Backup or Restore taking place.
	if (aNewState & (conn::EBURBackupPartial | conn::EBURBackupFull) )
		{			
		// Backup is taking place (either partial or full) 
		iCurrentState = aNewState;
		BackupBeginningL();
		}
	else if (aNewState & (conn::EBURRestorePartial | conn::EBURRestoreFull)) 
		{
		// Restore is taking place (either partial or full)
		iCurrentState = aNewState;
		RestoreBeginningL();
		}
	else if (aNewState & (conn::EBURNormal | conn::EBURUnset))
		{
		// The state has changed to no Backup/Restore.  Decide which operation
		// has just completed.
		if (BackupInProgress())
			{
			iCurrentState = aNewState;
			BackupCompletedL();
			}
		else if (RestoreInProgress())
			{
			iCurrentState = aNewState;
			RestoreCompletedL(KErrNone);
			}		
		}
	}


void CCntBackupRestoreAgent::ProcessInitialStateL(TInt aNewState)
	{
	// A Base Backup or Restore is taking place.
	if (aNewState & (conn::EBURBackupPartial | conn::EBURBackupFull) )
		{
		// Backup is taking place (either partial or full).
		BackupBeginningL();
		}
	else if (aNewState & (conn::EBURRestorePartial | conn::EBURRestoreFull)) 
		{
		// Restore is taking place (either partial or full).
		RestoreBeginningL();
		}
	iCurrentState = aNewState;
	}


void CCntBackupRestoreAgent::BackupBeginningL()
	{
	iEvent.iType = EContactDbObserverEventBackupBeginning;
	iDbMgrCtrlr.HandleBackupRestoreEventL(iEvent);
	iDbMgrCtrlr.IniFileManager().SaveIniFileSettingsIfRequestedL();
	}


void CCntBackupRestoreAgent::BackupCompletedL()
	{
	iEvent.iType = EContactDbObserverEventBackupRestoreCompleted;
	iDbMgrCtrlr.HandleBackupRestoreEventL(iEvent);
	// In case Store has been waiting for backup to finish.  Now attempt to
	// perform deferred operation.
	iDbMgrCtrlr.IniFileManager().RetryStoreOperation();
	}


void CCntBackupRestoreAgent::RestoreBeginningL()
	{
	iEvent.iType = EContactDbObserverEventRestoreBeginning;
	iDbMgrCtrlr.HandleBackupRestoreEventL(iEvent);
	iDbMgrCtrlr.IniFileManager().SaveIniFileSettingsIfRequestedL();
	}


void CCntBackupRestoreAgent::RestoreCompletedL(TInt aRestoreResult)
	{
	if (aRestoreResult == KErrNone)
		{
		iDbMgrCtrlr.IniFileManager().StartRestoreIniFileSettings();
		iEvent.iType = EContactDbObserverEventBackupRestoreCompleted;
		iDbMgrCtrlr.HandleBackupRestoreEventL(iEvent);
		}
	else
		{
		iEvent.iType = EContactDbObserverEventRestoreBadDatabase;
		iDbMgrCtrlr.HandleBackupRestoreEventL(iEvent);
		// A restore of the Contacts Model initialisation file failed so save
		// initialisation file.
		iDbMgrCtrlr.IniFileManager().ScheduleSaveIniFileSettings(CIniFileManager::ESaveAll);
		}
	}
	

/** Create an XML backup registration file name using aFileName.

	@param aFileName File name without drive letter.
*/
HBufC* CCntBackupRestoreAgent::CreateBackupRegistrationFileNameLC(const TDesC& aFileName)
	{	
	_LIT(KContactsBackupFileName, "backup_registration");
	_LIT(KContactsBackupFileExtension, ".xml");

	const TInt newFileNameLength = KContactsBackupFileName().Length() +	
	KSqLiteFilePrefix().Length() +
	aFileName.Length() + KContactsBackupFileExtension().Length();
	
	if (newFileNameLength > KMaxFileNameLength)
		{
		User::Leave(KErrArgument);
		}
		
	HBufC* newFileName = HBufC::NewL(newFileNameLength);
	CleanupStack::PushL(newFileName);

	TPtr newFileNamePtr(newFileName->Des());

	// New filename will be of the form backup_registration<aFileName>.xml.
	// If aFileName has an extension then the '.' will be replaced with '_'.
	newFileNamePtr.Append(KContactsBackupFileName);
	
	// if the it is the cntModel.ini file
	// then do not append it in the file name
	// this ensures that there is one file
	// with the name backup_registration.xml file
	// which is required for the Backup & Restore engine.
	if (aFileName.Compare(KContactsIniFileName) != 0) 
	    {
	    newFileNamePtr.Append(KSqLiteFilePrefix);
		newFileNamePtr.Append(aFileName);             
	    }                                             
	                                                  
	                                                  
	TInt dotPos = newFileNamePtr.LocateReverse('.');
	if (dotPos != KErrNotFound )
		{
		newFileNamePtr[dotPos] = '_';
		}
	newFileNamePtr.Append(KContactsBackupFileExtension);

	return newFileName;
	}