phonebookengines/contactsmodel/cntsrv/src/CIniFileManager.cpp
branchRCL_3
changeset 63 f4a778e096c2
parent 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 // Copyright (c) 2005-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 
       
    23 #include "CIniFileManager.h"
       
    24 #include "CCntDbManagerController.h"
       
    25 #include "CCntBackupRestoreAgent.h"
       
    26 #include "CntCurrentItemMap.h"
       
    27 #include "CntSpeedDials.h"
       
    28 
       
    29 // uncomment this for debug logging
       
    30 //#define INIFILE_DEBUG_LOG
       
    31 
       
    32 
       
    33 
       
    34 const TUid KContactsIniUid = { 0x10009099 };
       
    35 const TUid KContactsCurrentDatabaseUid = { 0x1000909A };
       
    36 const TUid KUidContactsDefaultDatabaseLocation = { 0x1020380A };
       
    37 
       
    38 const TInt KIniFileSaveDelay = 100000;
       
    39 
       
    40 /**
       
    41 First phase constructor.
       
    42 */
       
    43 CIniFileManager::CIniFileManager(RFs& aFs, CCntDbManagerController& aDbMgrCtrlr)
       
    44 	: CTimer(CActive::EPriorityStandard), iFs(aFs), iDbMgrCtrlr(aDbMgrCtrlr), iBackupFlag(ENoSaveRestore)
       
    45 	{
       
    46 	CActiveScheduler::Add(this);
       
    47 	}
       
    48 
       
    49 
       
    50 /**
       
    51 Object factory method.
       
    52 */
       
    53 CIniFileManager* CIniFileManager::NewL(RFs& aFs, CCntDbManagerController& aDbMgrCtrlr)
       
    54 	{
       
    55 	CIniFileManager* IniFileManager = new (ELeave) CIniFileManager(aFs, aDbMgrCtrlr);
       
    56 	CleanupStack::PushL(IniFileManager);
       
    57 	IniFileManager->ConstructL();
       
    58 	CleanupStack::Pop(IniFileManager);
       
    59 	return IniFileManager;
       
    60 	}
       
    61 
       
    62 
       
    63 /**
       
    64 Second phase constructor.
       
    65 */
       
    66 void CIniFileManager::ConstructL()
       
    67 	{
       
    68 	CTimer::ConstructL();
       
    69 	iSpeedDialManager = new(ELeave) CCntServerSpeedDialManager();
       
    70 	iCurrentItemMap = new(ELeave) CCntServerCurrentItemMap();
       
    71 	iCurrentDb = KNullDesC;
       
    72 	// Restore the current database if possible. If this fails, the 
       
    73 	// current database is set to KNullDesC (i.e. zero'd)
       
    74 	RequestRestoreIniFileSettingsL();
       
    75 	}
       
    76 
       
    77 
       
    78 CIniFileManager::~CIniFileManager()
       
    79 	{
       
    80 	if (iFs.Handle()!=0)
       
    81 		{
       
    82 		TRAP_IGNORE(SaveIniFileSettingsL(ESaveAll,EFalse));
       
    83 		}
       
    84 	delete iSpeedDialManager;
       
    85 	delete iCurrentItemMap;
       
    86 	}
       
    87 
       
    88 
       
    89 /**
       
    90 Update/create a map entry for the current item associated with a database file.
       
    91 */	
       
    92 void CIniFileManager::SetCurrentItemForDatabaseL(const TDesC& aDatabase, TContactItemId aContactId)
       
    93 	{
       
    94 	// Does the specified database contain a current item already?
       
    95 	TInt index = KErrNotFound;
       
    96 	if	(iCurrentItemMap->EntryAvailable(aDatabase, index))
       
    97 		{
       
    98 		// Yes, already exists, so update it....
       
    99 		iCurrentItemMap->UpdateEntryL(index, aContactId); 
       
   100 		}
       
   101 	else
       
   102 		{
       
   103 		// No, so create a new entry to represent this database
       
   104 		iCurrentItemMap->AddEntryL(aDatabase, aContactId);
       
   105 		}
       
   106 	}
       
   107 
       
   108 		
       
   109 void CIniFileManager::RemoveCurrentItemL(const TDesC& aDatabase)
       
   110 	{
       
   111 	SetCurrentItemForDatabaseL(aDatabase, KNullContactId);
       
   112 	}
       
   113 
       
   114 
       
   115 void CIniFileManager::SetCurrentItemL(const TDesC& aDatabase, TContactItemId aNewCurrentItem)
       
   116 	{
       
   117 	SetCurrentItemForDatabaseL(aDatabase, aNewCurrentItem);
       
   118 	}
       
   119 
       
   120 
       
   121 /**
       
   122 Returns the current item associated with the specified database.
       
   123 */	
       
   124 TContactItemId CIniFileManager::CurrentItem(const TDesC& aDatabase) const
       
   125 	{
       
   126 	return iCurrentItemMap->CurrentItem(aDatabase);
       
   127 	}
       
   128 
       
   129 
       
   130 const TDesC& CIniFileManager::CurrentDb() const
       
   131 	{ 
       
   132 	return iCurrentDb; 
       
   133 	}
       
   134 
       
   135 	
       
   136 void CIniFileManager::SetCurrentDb(const TDesC& aDb)
       
   137 	{ 
       
   138 	iCurrentDb = aDb; 
       
   139 	}
       
   140 
       
   141 
       
   142 CCntServerSpeedDialManager& CIniFileManager::SpeedDialManager()
       
   143 	{ 
       
   144 	return *iSpeedDialManager;
       
   145 	}
       
   146 	
       
   147 
       
   148 void CIniFileManager::ScheduleSaveIniFileSettings(TInt aSaveFlags, TBool aReplace)
       
   149 	{
       
   150 	// make sure all requested writes are saved
       
   151 	iSaveType |= aSaveFlags;
       
   152 	iReplace = aReplace;
       
   153 
       
   154 	// make sure change isn't due to Internalize
       
   155 	if (iBackupFlag != EIsRestoring)
       
   156 		{
       
   157 		iBackupFlag = ERequestSave;
       
   158 		//iNumberOfAttemptedRetries = 0;
       
   159 
       
   160 		// set the time to RunL if not already set to run
       
   161 		// check that no Backup or Restore is in progress, 
       
   162 		// For Backup And Restore
       
   163 		if (!(iDbMgrCtrlr.BackupRestoreAgent().BackupInProgress()) &&
       
   164 			!(iDbMgrCtrlr.BackupRestoreAgent().RestoreInProgress()))
       
   165 			{
       
   166 			if (!IsActive())
       
   167 				{
       
   168 #ifdef INIFILE_DEBUG_LOG
       
   169 				RDebug::Print(_L("\n[CNTMODEL] CIniFileManager::ScheduleSaveIniFileSettings(aSaveFlags = %i, aReplace %i)\r\n"),aSaveFlags, aReplace);
       
   170 #endif
       
   171 				After(KIniFileSaveDelay);	
       
   172 				}
       
   173 			}
       
   174 		}
       
   175 	}
       
   176 
       
   177 
       
   178 void CIniFileManager::SaveIniFileSettingsIfRequestedL()
       
   179 	{
       
   180 	if (iBackupFlag == ERequestSave)
       
   181 		{
       
   182 		// Don't RunL
       
   183 		Cancel();
       
   184 
       
   185 		SaveIniFileSettingsL(iSaveType, iReplace);
       
   186 		iBackupFlag = ENoSaveRestore;
       
   187 		}
       
   188 	}
       
   189 
       
   190 
       
   191 void CIniFileManager::RequestRestoreIniFileSettingsL()
       
   192 	{
       
   193 	if (!(iDbMgrCtrlr.BackupRestoreAgent().RestoreInProgress()))
       
   194 		{
       
   195 		TInt ret = StartRestoreIniFileSettings();
       
   196 		switch (ret)
       
   197 			{
       
   198 		case KErrNone:
       
   199 			break;
       
   200 		// If file does not exist create ini file immediately.
       
   201 		case KErrPathNotFound:
       
   202 			SaveIniFileSettingsL(ESaveAll, EFalse);
       
   203 			break;
       
   204 		// If file is damaged replace ini file.
       
   205 		case KErrEof:
       
   206 			SaveIniFileSettingsL(ESaveAll, ETrue);
       
   207 			break;
       
   208 		default:
       
   209 #if	defined(_DEBUG)
       
   210 			User::Leave(KErrNotSupported);
       
   211 #endif
       
   212 			break;  //Would not build in UREL without it
       
   213 			}
       
   214 		}
       
   215 	}
       
   216 
       
   217 	
       
   218 TInt CIniFileManager::StartRestoreIniFileSettings()
       
   219 	{
       
   220 	iBackupFlag = EIsRestoring;
       
   221 	TInt err;
       
   222 	TRAP(err,RestoreIniFileSettingsL());
       
   223 	iBackupFlag = ENoSaveRestore;
       
   224 	return err ;
       
   225 	}
       
   226 
       
   227 
       
   228 void CIniFileManager::GetIniFileNameL(TDes& aFileName, TBool aIncPrivatePath)
       
   229 	{
       
   230 	if (aIncPrivatePath)
       
   231 		{
       
   232 		User::LeaveIfError(iFs.PrivatePath(aFileName));
       
   233 		}
       
   234 	// Drive name goes before the path.
       
   235 	aFileName.Insert(0,TDriveUnit(EDriveC).Name());
       
   236 	// Filename goes after the path.
       
   237 	aFileName.Append(KContactsIniFileName);
       
   238 	}
       
   239 
       
   240 
       
   241 /**
       
   242 Restore server-wide settings from the contacts ini file.
       
   243 */
       
   244 void CIniFileManager::RestoreIniFileSettingsL()
       
   245 	{
       
   246 	// Initial location for Default database
       
   247 	SetDefaultDatabaseDrive(TDriveUnit(EDriveC),EFalse);
       
   248 
       
   249 	TFileName iniFile;
       
   250 	GetIniFileNameL(iniFile);
       
   251 	CDictionaryFileStore* iniStore = IniFileStoreLC(iniFile);
       
   252 	
       
   253 	// Load settings
       
   254 	iCurrentItemMap->RestoreL(*iniStore);
       
   255 	iSpeedDialManager->RestoreL(*iniStore);
       
   256 
       
   257 	// Current database
       
   258 	RestoreCurrentDatabaseL(*iniStore);
       
   259 
       
   260 	// Default database location
       
   261 	RestoreDefaultDbDriveL(*iniStore);
       
   262 
       
   263 	// Tidy up
       
   264 	CleanupStack::PopAndDestroy(iniStore);
       
   265 	}
       
   266 
       
   267 	
       
   268 void CIniFileManager::SetDefaultDatabaseDrive(TDriveUnit aDriveUnit, TBool aDriveSet)
       
   269 	{
       
   270 	iDefaultDriveUnit = aDriveUnit;
       
   271 	iDatabaseDriveSet = aDriveSet;
       
   272 	}
       
   273 
       
   274 	
       
   275 /**
       
   276 Restore the current database filename from the ini file.
       
   277 */
       
   278 void CIniFileManager::RestoreCurrentDatabaseL(CDictionaryFileStore& aStore)
       
   279 	{
       
   280 	if	(aStore.IsPresentL(KContactsCurrentDatabaseUid))
       
   281 		{
       
   282 		RDictionaryReadStream stream;
       
   283 		stream.OpenLC(aStore, KContactsCurrentDatabaseUid);
       
   284 		stream >> iCurrentDb;
       
   285 		CleanupStack::PopAndDestroy(); // stream
       
   286 		}
       
   287 	}
       
   288 
       
   289 	
       
   290 /**
       
   291 Restore the current database filename from the ini file.
       
   292 */
       
   293 void CIniFileManager::RestoreDefaultDbDriveL(CDictionaryFileStore& aStore)
       
   294 	{
       
   295 	if	(aStore.IsPresentL(KUidContactsDefaultDatabaseLocation))
       
   296 		{
       
   297 		TDriveUnit driveUnit;
       
   298 		RDictionaryReadStream stream;
       
   299 
       
   300 		stream.OpenLC(aStore, KUidContactsDefaultDatabaseLocation);
       
   301 		driveUnit = stream.ReadInt32L();
       
   302 		CleanupStack::PopAndDestroy(); // stream
       
   303 
       
   304 		SetDefaultDatabaseDrive(driveUnit);
       
   305 		iDbMgrCtrlr.RestoreDatabaseDrive(driveUnit); // restore drive setting in DBManagerController
       
   306 		}
       
   307 	}
       
   308 
       
   309 
       
   310 void CIniFileManager::RetryStoreOperation()
       
   311 	{
       
   312 	if (iBackupFlag == ERequestSave && !IsActive())
       
   313 		{
       
   314 		// can RunL now
       
   315 		After(0);
       
   316 		}
       
   317 	}
       
   318 
       
   319 
       
   320 void CIniFileManager::RunL()
       
   321 	{
       
   322 	const TInt error = iStatus.Int();
       
   323 
       
   324 #ifdef INIFILE_DEBUG_LOG
       
   325 	RDebug::Print(_L("[CNTMODEL] CIniFileManager::RunL() - error %i, iBackupFlag %i\r\n"), error, iBackupFlag);
       
   326 #endif
       
   327 
       
   328 	// operation isn't cancelled
       
   329 	if	(error == KErrNone && iBackupFlag == ERequestSave)
       
   330 		{
       
   331 		SaveIniFileSettingsL(iSaveType, iReplace);
       
   332 		iBackupFlag = ENoSaveRestore;
       
   333 		}
       
   334 	}
       
   335 
       
   336 
       
   337 TInt CIniFileManager::RunError(TInt aError)
       
   338    	{
       
   339 #ifdef INIFILE_DEBUG_LOG
       
   340 	RDebug::Print(_L("[CNTMODEL] CIniFileManager::RunError(aError = %i)\r\n"), aError);
       
   341 #else
       
   342 	aError = aError; // remove compiler warning for unreferenced parameter
       
   343 #endif
       
   344 	// cannot do anything about the error
       
   345 	// - unless it occurred during a Backup, when it will already be retried
       
   346 
       
   347 	return KErrNone;
       
   348 	}
       
   349 
       
   350 	
       
   351 /**
       
   352 Save server-wide settings to the contacts model ini file.
       
   353 */
       
   354 void CIniFileManager::SaveIniFileSettingsL(TInt aSaveFlags, TBool aReplace)
       
   355 	{
       
   356 	TFileName	iniFile;
       
   357 	GetIniFileNameL(iniFile);
       
   358 	iFs.MkDirAll(iniFile);
       
   359 
       
   360 	if (aReplace)
       
   361 		{
       
   362 		iFs.Delete(iniFile);
       
   363 		}
       
   364 	
       
   365 	// Get the ini store
       
   366 	CDictionaryFileStore* iniStore = IniFileStoreLC(iniFile);
       
   367 
       
   368 	// Save settings
       
   369 	if ((aSaveFlags & ESaveCurrentItem) && iCurrentItemMap)
       
   370 		{
       
   371 		iCurrentItemMap->StoreL(*iniStore);
       
   372 		}
       
   373 
       
   374 	if ((aSaveFlags & ESaveSpeedDials) && iSpeedDialManager)
       
   375 		{
       
   376 		iSpeedDialManager->StoreL(*iniStore);
       
   377 		}
       
   378 
       
   379 	// Current database
       
   380 	if (aSaveFlags & ESaveCurrentDatabase)
       
   381 		{
       
   382 		StoreCurrentDatabaseL(*iniStore);
       
   383 		}
       
   384 
       
   385 	// Default database location
       
   386 	if (aSaveFlags & ESaveDefaultDbDrive)
       
   387 		{
       
   388 		StoreDefaultDbDriveL(*iniStore);
       
   389 		}
       
   390 
       
   391 	// Commit
       
   392 	iniStore->CommitL();
       
   393 
       
   394 	// Tidy up
       
   395 	CleanupStack::PopAndDestroy(); // iniStore
       
   396 	}
       
   397 
       
   398 
       
   399 /**
       
   400 Return a pointer to the contacts ini file.
       
   401 */
       
   402 CDictionaryFileStore* CIniFileManager::IniFileStoreLC(TFileName& aIniFile)
       
   403 	{
       
   404 	return CDictionaryFileStore::OpenLC(iFs, aIniFile, KContactsIniUid);
       
   405 	}
       
   406 
       
   407 	
       
   408 /**
       
   409 Save the current database filename to the ini file.
       
   410 */
       
   411 void CIniFileManager::StoreCurrentDatabaseL(CDictionaryFileStore& aStore)
       
   412 	{
       
   413 	RDictionaryWriteStream stream;
       
   414 	stream.AssignLC(aStore, KContactsCurrentDatabaseUid);
       
   415 	stream << iCurrentDb;
       
   416 	stream.CommitL();
       
   417 	CleanupStack::PopAndDestroy(); // stream
       
   418 	}
       
   419 
       
   420 
       
   421 /**
       
   422 Save the default database drive to the ini file.
       
   423 */
       
   424 void CIniFileManager::StoreDefaultDbDriveL(CDictionaryFileStore& aStore)
       
   425 	{
       
   426 	if (iDatabaseDriveSet)
       
   427 		{
       
   428 		RDictionaryWriteStream stream;
       
   429 		stream.AssignLC(aStore, KUidContactsDefaultDatabaseLocation);
       
   430 		stream.WriteInt32L(iDefaultDriveUnit);
       
   431 		stream.CommitL();
       
   432 		CleanupStack::PopAndDestroy(); // stream
       
   433 		}
       
   434 	}
       
   435 
       
   436 
       
   437 TBool& CIniFileManager::DatabaseDriveSet()
       
   438 	{
       
   439 	return iDatabaseDriveSet;
       
   440 	}