phonebookengines/contactsmodel/cntplsql/src/pplcontactitemmanager.cpp
branchRCL_3
changeset 20 f4a778e096c2
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 // Copyright (c) 2007-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 "pplcontactitemmanager.h"
       
    23 #include "cntsqlprovider.h"
       
    24 #include "dbsqlconstants.h"
       
    25 #include "cntpersistenceutility.h"
       
    26 #include <cntdef.h>
       
    27 #include <sqldb.h>
       
    28 #include <cntdb.h>
       
    29 
       
    30 /**
       
    31 Creates a concrete CPplContactItemManager object 
       
    32 
       
    33 @param 	aDatabase reference to RSqlDatabase
       
    34 		aTransactionManager reference to MLplTransactionManager (provided by CPlContactsFile)
       
    35 @return concrete CPplContactItemManager object
       
    36 @leave KErrNoMemory, an out of memory occured
       
    37 */		
       
    38 CPplContactItemManager* CPplContactItemManager::NewL(RSqlDatabase& aDatabase, MLplTransactionManager& aTransactionManager, CLplContactProperties&  aContactProperties, RPplIccContactStore& aIccContactStore)
       
    39 	{
       
    40 	CPplContactItemManager* self = new (ELeave) CPplContactItemManager(aDatabase, aTransactionManager, aContactProperties, aIccContactStore);
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	CleanupStack::Pop(self);
       
    44 	return self;	
       
    45 	}
       
    46 
       
    47 /**
       
    48 Class destructor. Deletes holded CCntSqlStatement
       
    49 
       
    50 */		
       
    51 CPplContactItemManager::~CPplContactItemManager()
       
    52 	{
       
    53 	delete iSelectStatement;	
       
    54 	delete iContactTable;
       
    55 	delete iCommAddrTable;	
       
    56 	delete iGroupTable;
       
    57 	delete iPreferencePersistor;
       
    58 	}
       
    59 	
       
    60 /**
       
    61 Class constructor
       
    62 
       
    63 @param aDatabase reference to RSqlDatabase
       
    64 @param aTransactionManager reference to transaction manager
       
    65 @param aContactProperties reference to contact properties
       
    66 */	
       
    67 CPplContactItemManager::CPplContactItemManager(RSqlDatabase& aDatabase, MLplTransactionManager& aTransactionManager, CLplContactProperties&  aContactProperties, RPplIccContactStore& aIccContactStore) :
       
    68 	iDatabase(aDatabase),
       
    69 	iTransactionManager(aTransactionManager),
       
    70 	iContactProperties(aContactProperties),
       
    71 	iIccContactStore(aIccContactStore)
       
    72 	{
       
    73 	}
       
    74 	
       
    75 /**
       
    76 Add the given contact item to the database. Forward the call to CPplTableBase
       
    77 based classes representing the tables in the contact database.
       
    78 
       
    79 @param 	aItem The contact item to be added to the database. 
       
    80 @param 	aSessionId The ID of the session that issued the request.  Used to
       
    81 		prevent Phonebook Synchroniser deadlock.
       
    82 
       
    83 @return Contact item ID of the contact added to the database.
       
    84 */
       
    85 TContactItemId CPplContactItemManager::CreateL(CContactItem& aItem, TUint aSessionId)
       
    86 	{
       
    87 	TBool controlTransaction = !(iTransactionManager.IsTransactionActive());
       
    88 	
       
    89 	TBool compressedGuid=EFalse;
       
    90 
       
    91 	// If needed generate a gui for the current contact item
       
    92 	if (aItem.Guid() == TPtrC(KNullDesC))
       
    93 		{
       
    94 		iPreferencePersistor->SetGuidL(aItem, compressedGuid);
       
    95 		}
       
    96 
       
    97 	if (compressedGuid)
       
    98 		{
       
    99 		aItem.SetHasCompressedGuid(compressedGuid);
       
   100 		}
       
   101 
       
   102 	if (aItem.Type() == KUidContactICCEntry) 
       
   103 		{
       
   104 		const TInt ret = iContactProperties.ContactSynchroniserL(aSessionId).ValidateWriteContact(static_cast<CContactICCEntry&>(aItem));
       
   105 		User::LeaveIfError(ret);	
       
   106 		}
       
   107 	
       
   108 	if(controlTransaction)
       
   109 		{
       
   110 		StartTransactionL(aSessionId);
       
   111 		}
       
   112 		
       
   113 	iContactTable->CreateInDbL(aItem);	
       
   114 	iGroupTable->CreateInDbL(aItem);	
       
   115 	iCommAddrTable->CreateInDbL(aItem);
       
   116 
       
   117    	TContactItemId groupId = iIccContactStore.CreateInDbL(aItem, aSessionId);
       
   118    	if(groupId != KNullContactId)
       
   119    	    {
       
   120    	    //Every ICC entry is added to a special group, created by the Phonebook
       
   121         //Synchroniser server during the initial synchronisation with the Contacts Model.
       
   122 		CContactItemViewDef* itemDef = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EMaskHiddenFields);
       
   123 		itemDef->AddL(KUidContactFieldMatchAll);
       
   124 
       
   125 		// Add ICC entry to the group.
       
   126 		CContactGroup* grp = static_cast<CContactGroup*>(ReadLC(groupId, *itemDef, EPlAllInfo, aSessionId));
       
   127 		
       
   128 		grp->AddContactL(aItem.Id());
       
   129 		UpdateL(*grp, aSessionId);
       
   130 
       
   131 		CleanupStack::PopAndDestroy(2, itemDef); // grp
       
   132    	    }
       
   133 
       
   134 	if(controlTransaction)
       
   135 		{
       
   136 		CommitTransactionL();
       
   137 		}
       
   138 	
       
   139 	return aItem.Id();	
       
   140 	}
       
   141 	
       
   142 /**
       
   143 Reads the contact item from the database using the given contact item ID.
       
   144 
       
   145 @param aItemId The Id number of the contact to be read.
       
   146 @param aView Specifies the fields to be read.
       
   147 @param aInfoToRead not used 
       
   148 @param aSessionId The ID of the session that issued the request.  This is used
       
   149 to prevent Phonebook Synchroniser deadlock.
       
   150 @param aIccOpenCheck Specifies if validation with the Phonebook Synchroniser is
       
   151 needed for this contact.
       
   152 
       
   153 @leave 	KErrArgument 	if the itemID can't be set within select statement
       
   154 @leave  KErrNotFound 	if a contact item with aItemId does not exist within contact database
       
   155 @leave  KSqlErrBusy 	the database file is locked; thrown if RSqlStatement::Next() 
       
   156 						returns this error
       
   157 @leave  KErrNoMemory 	an out of memory condition has occurred - the statement
       
   158                         will be reset;thrown if RSqlStatement::Next() returns this error
       
   159 @leave  KSqlErrGeneral	a run-time error has occured - this function must not
       
   160                         be called again;thrown if RSqlStatement::Next() returns this error
       
   161 @leave	KSqlErrMisuse	this function has been called after a previous call
       
   162                         returned KSqlAtEnd or KSqlErrGeneral.thrown if RSqlStatement::Next() 
       
   163                         returns this error
       
   164 @leave  KSqlErrStmtExpired	the SQL statement has expired (if new functions or
       
   165                             collating sequences have been registered or if an
       
   166                             authorizer function has been added or changed);
       
   167                             thrown if RSqlStatement::Next() returns this error
       
   168 
       
   169 @return CContactItem created from reading the database tables.
       
   170 */	
       
   171 CContactItem* CPplContactItemManager::ReadLC(TContactItemId aItemId, const CContactItemViewDef& aView, TInt aInfoToRead, TUint aSessionId, TBool aIccOpenCheck) const
       
   172 	{	
       
   173 	CContactTemplate* sysTemplate = NULL;
       
   174 	if (aItemId != KGoldenTemplateId) 
       
   175 		{
       
   176 		sysTemplate = const_cast<CContactTemplate*>(&iContactProperties.SystemTemplateL());
       
   177 		}
       
   178 	
       
   179 	RSqlStatement selectStmt;
       
   180 	CleanupClosePushL(selectStmt);
       
   181 	User::LeaveIfError(selectStmt.Prepare(iDatabase, iSelectStatement->SqlStringL()));
       
   182 	
       
   183 	TInt err = selectStmt.BindInt(KFirstParam, aItemId);
       
   184 	
       
   185 	if(err != KErrNone)
       
   186 		{
       
   187 		User::Leave(KErrArgument);	
       
   188 		}
       
   189 	
       
   190 	CContactItem* item = NULL;
       
   191 	TUid type(KNullUid);
       
   192 	
       
   193 	if((err = selectStmt.Next()) == KSqlAtRow)
       
   194 		{
       
   195 		TInt contactId = selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactId));
       
   196 		TInt templateId = selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactTemplateId));
       
   197 		TInt typeFlags = selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactTypeFlags));
       
   198 		type = TCntPersistenceUtility::TypeFlagsToContactTypeUid(typeFlags); 
       
   199 		item = CContactItem::NewLC(type);
       
   200 		item->SetId(contactId);
       
   201 		TPtrC guid = selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactGuidString));
       
   202 		item->SetUidStringL(guid);
       
   203 		TInt attr = (typeFlags & EContactAttributes_Mask) >> EContactAttributes_Shift; 
       
   204 		item->SetAttributes(attr);
       
   205 		item->SetTemplateRefId(templateId);
       
   206 		item->SetLastModified(TTime(selectStmt.ColumnInt64(iSelectStatement->ParameterIndex(KContactLastModified)))); 
       
   207 		item->SetCreationDate(TTime(selectStmt.ColumnInt64(iSelectStatement->ParameterIndex(KContactCreationDate))));
       
   208 		item->SetAccessCount(selectStmt.ColumnInt(iSelectStatement->ParameterIndex(KContactAccessCount)));
       
   209 			
       
   210 		RArray<TPtrC> fastAccessFields;
       
   211 		CleanupClosePushL(fastAccessFields);
       
   212 		fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactFirstName)));
       
   213 		fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactLastName)));
       
   214 		fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactCompanyName)));
       
   215 		fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactFirstNamePrn)));
       
   216 		fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactLastNamePrn)));
       
   217 		fastAccessFields.AppendL(selectStmt.ColumnTextL(iSelectStatement->ParameterIndex(KContactCompanyNamePrn)));
       
   218 				
       
   219 		//set first name, last name, company name, first name pronunciation, last name pronunciation, company name pronunciation
       
   220 		for (TInt fieldNum = item->CardFields().Count() - 1; fieldNum>=0; --fieldNum) 
       
   221 			{
       
   222 			CContactItemField& textField = (item->CardFields())[fieldNum];
       
   223 			const TInt nameFieldIndex = NameFieldIndex(textField);
       
   224 
       
   225 			// Check if field is first name, last name, company name,
       
   226 			// first name pronunciation, last name pronunciation, company name pronunciation.
       
   227 			if (nameFieldIndex != KErrNotFound)
       
   228 				{
       
   229 				HBufC* text = HBufC::NewLC(fastAccessFields[nameFieldIndex].Size());
       
   230 				text->Des() = fastAccessFields[nameFieldIndex];
       
   231 				textField.TextStorage()->SetText(text);
       
   232 				CleanupStack::PopAndDestroy(text);
       
   233 				}
       
   234 			}
       
   235 			
       
   236 		CleanupStack::PopAndDestroy(&fastAccessFields);		
       
   237 		}
       
   238 	else
       
   239 		{
       
   240 		if(err == KSqlAtEnd)
       
   241 			{
       
   242 			User::Leave(KErrNotFound);	// Select statement did not return any row		
       
   243 			}
       
   244 		else
       
   245 			{
       
   246 			User::Leave(err); 	// Otherwise selectStmt.Next() generated a sql error
       
   247 			}	
       
   248 		
       
   249 		}
       
   250 
       
   251 	if (!(item->IsDeleted()) || type == KUidContactCardTemplate)
       
   252 		{
       
   253 		TCntPersistenceUtility::ReadBlobL(*item, aView, sysTemplate, iDatabase);
       
   254 		}
       
   255     
       
   256     //Fill in group content.
       
   257     static_cast<CPplGroupsTable*>(iGroupTable)->ReadL(*item);
       
   258     
       
   259     //Validate checking ICC entry.
       
   260     iIccContactStore.ReadL(*item, aInfoToRead, aSessionId, aIccOpenCheck);
       
   261     
       
   262 	CleanupStack::Pop(item);
       
   263 	CleanupStack::PopAndDestroy(&selectStmt);
       
   264 	CleanupStack::PushL(item);
       
   265 	
       
   266 	return item;
       
   267 	}
       
   268 	
       
   269 /** 
       
   270 Updates the given contact item in the database.Forward the call to CPplTableBase
       
   271 based classes representing the tables in the contact database.
       
   272 
       
   273 @param aItem The contact to be updated with all the changes (received from the
       
   274 user).
       
   275 @param aSessionId The ID of the session that issued the request.  Used to
       
   276 prevent Phonebook Synchroniser deadlock.
       
   277 */	
       
   278 void CPplContactItemManager::UpdateL(CContactItem& aItem, TUint aSessionId, TBool /*aSpeeDailUpdate*/)
       
   279 	{
       
   280 	TBool controlTransaction = !(iTransactionManager.IsTransactionActive());
       
   281 	
       
   282 	if (aItem.Type() == KUidContactICCEntry) 
       
   283 		{
       
   284         //Fakely validating read right to ICC entry to keep BC.
       
   285 		const TInt ret = iContactProperties.ContactSynchroniserL(aSessionId).ValidateContact(MContactSynchroniser::ERead, aItem.Id());
       
   286 		User::LeaveIfError(ret);	
       
   287 		}
       
   288 		
       
   289 	if(controlTransaction)
       
   290 		{
       
   291 		StartTransactionL(aSessionId);
       
   292 		}
       
   293 
       
   294 	iIccContactStore.UpdateL(aItem, aSessionId);
       
   295 	iContactTable->UpdateL(aItem);	
       
   296 	iGroupTable->UpdateL(aItem);	
       
   297 	iCommAddrTable->UpdateL(aItem);
       
   298 
       
   299 	if(controlTransaction)
       
   300 		{
       
   301 		CommitTransactionL();
       
   302 		}
       
   303 		
       
   304 	// If the item is the System template then delete it from the template
       
   305 	// manager.  The template can later be recreated from the table.
       
   306 	if (aItem.Id() == KGoldenTemplateId)
       
   307 		{
       
   308 		iContactProperties.SystemTemplateManager().DeleteTemplate();
       
   309 		}
       
   310 	}
       
   311 
       
   312 /**
       
   313 Deletes the given contact from the database.Forward the call to CPplTableBase
       
   314 based classes representing the tables in the contact database. In low disk condition
       
   315 checks if it can gain access to reserver database space and perform the deletion. If 
       
   316 this is not possible a KErrDiskFull will be thrown.
       
   317 
       
   318 @param aItemId The contact ID of the contact item to be deleted.
       
   319 @param aSessionId The ID of the session that issued the request.  Used to
       
   320 prevent Phonebook Synchroniser deadlock.
       
   321 
       
   322 @leave KErrDiskFull if a full disk error appears
       
   323 
       
   324 @return The item which was read from the database before it was deleted.   This
       
   325 object can be used by, for example, the CLplAnalyserProxy::DeleteLC() method to
       
   326 create a notification message.
       
   327 */	
       
   328 CContactItem* CPplContactItemManager::DeleteLC(TContactItemId  aItemId, TUint aSessionId, TCntSendEventAction /*aEventType*/)
       
   329 	{
       
   330 	TBool controlTransaction = !(iTransactionManager.IsTransactionActive());
       
   331 
       
   332 	CContactItemViewDef* viewDef = CContactItemViewDef::NewL(CContactItemViewDef::EIncludeFields, CContactItemViewDef::EIncludeHiddenFields);
       
   333 	CleanupStack::PushL(viewDef);
       
   334 
       
   335 	if(controlTransaction)
       
   336 		{
       
   337 		StartTransactionL(aSessionId);
       
   338 		}
       
   339 
       
   340 	TBool lowDisk = EFalse;
       
   341 	CContactItem* savedContactItem = static_cast<CPplContactTable*>(iContactTable)->DeleteLC(aItemId, lowDisk);
       
   342 	if(lowDisk) 
       
   343 		{
       
   344 		CleanupStack::PopAndDestroy(savedContactItem); // this was returned from the first call, but not deleted in the db
       
   345 		TInt err = iDatabase.GetReserveAccess();
       
   346 		if(err != KErrNotFound)
       
   347 			{
       
   348 			User::Leave(KErrDiskFull);	
       
   349 			}
       
   350 		
       
   351 		lowDisk = EFalse;
       
   352 		
       
   353 		savedContactItem = static_cast<CPplContactTable*>(iContactTable)->DeleteLC(aItemId, lowDisk);
       
   354 		if(lowDisk)
       
   355 			{
       
   356 			iDatabase.FreeReservedSpace();
       
   357 			User::Leave(KErrDiskFull);	
       
   358 			}
       
   359 		iDatabase.FreeReservedSpace();		
       
   360 		}
       
   361 
       
   362 	lowDisk = EFalse;	
       
   363 	iGroupTable->DeleteL(*savedContactItem, lowDisk);	
       
   364 	if(lowDisk) 
       
   365 		{
       
   366 		DeleteInLowDiskConditionL(iGroupTable, savedContactItem);
       
   367 		}
       
   368 
       
   369 	lowDisk = EFalse;
       
   370 	iCommAddrTable->DeleteL(*savedContactItem, lowDisk);	
       
   371 	if(lowDisk) 
       
   372 		{
       
   373 		DeleteInLowDiskConditionL(iCommAddrTable, savedContactItem);
       
   374 		}
       
   375     
       
   376     //Fake checking read access to ICCEntry store to keep BC.
       
   377     iIccContactStore.ReadL(*savedContactItem, EPlGroupMembershipInfo, aSessionId, EFalse);    
       
   378     
       
   379 	//Don't need to check low disk state for ICC synchronizer.
       
   380 	iIccContactStore.DeleteL(*savedContactItem, aSessionId);
       
   381 
       
   382 	if(controlTransaction)
       
   383 		{
       
   384 		CommitTransactionL();  	// Pops item
       
   385 		}
       
   386 		
       
   387 	CleanupStack::Pop();		// Pops item or transaction
       
   388 	CleanupStack::PopAndDestroy(viewDef);	
       
   389 	CleanupStack::PushL(savedContactItem);
       
   390 
       
   391 	return savedContactItem;
       
   392 	
       
   393 	}
       
   394 	
       
   395 /**
       
   396 Perform a deletion in low disk condition
       
   397 
       
   398 @param aTable CPplTableBase on which the deletion will be done
       
   399 @param aContactItem the contact item to be deleted
       
   400 
       
   401 @leave KErrDiskFull if the deletion can't be done a KerrDiskFull will be throw
       
   402 */
       
   403 void CPplContactItemManager::DeleteInLowDiskConditionL(CPplTableBase* aTable, CContactItem* aContactItem)
       
   404 	{
       
   405 	TInt err = iDatabase.GetReserveAccess();
       
   406 	if(err != KErrNotFound)
       
   407 		{
       
   408 		User::Leave(KErrDiskFull);	
       
   409 		}
       
   410 		
       
   411 	TBool lowDisk = EFalse;
       
   412 	aTable->DeleteL(*aContactItem, lowDisk);
       
   413 	if(lowDisk)
       
   414 		{
       
   415 		iDatabase.FreeReservedSpace();
       
   416 		User::Leave(KErrDiskFull);	
       
   417 		}
       
   418 	iDatabase.FreeReservedSpace();		
       
   419 	}
       
   420 	
       
   421 /**
       
   422 Change the type of the given contact item to the given type.Forward the call to CPplTableBase
       
   423 based classes representing the tables in the contact database.
       
   424 
       
   425 @param aItemId Contact item ID whose type is to be changed.
       
   426 @param aNewType New type for contact item.
       
   427 */	
       
   428 void CPplContactItemManager::ChangeTypeL(TContactItemId aItemId, TUid aNewType)
       
   429 	{
       
   430 	static_cast<CPplContactTable*>(iContactTable)->ChangeTypeL(aItemId, aNewType);
       
   431 	}
       
   432 
       
   433 /** 
       
   434 Does nothing: the CLplAnalyserProxy class sets the connetion ID.
       
   435 */
       
   436 void CPplContactItemManager::SetConnectionId(TInt /*aConnectionId*/)
       
   437 	{
       
   438 	}
       
   439 	
       
   440 /**
       
   441 Second phase constructor for CPplContactItemManager object.
       
   442 Sets the CCntSqlStatement to be used for reading contact item.
       
   443 
       
   444 */
       
   445 void CPplContactItemManager::ConstructL()
       
   446 	{
       
   447 	TCntSqlStatementType statementType(ESelect, KSqlContactTableName);
       
   448 	
       
   449 	iSelectStatement = TSqlProvider::GetSqlStatementL(statementType);
       
   450 	
       
   451 	// also prepare everything in CCntSqlStatement
       
   452 	// First add columns from contact table
       
   453 	iSelectStatement->SetParamL(KContactId, KSpace);
       
   454 	iSelectStatement->SetParamL(KContactTemplateId, KSpace);
       
   455 	iSelectStatement->SetParamL(KContactTypeFlags, KSpace);
       
   456 	iSelectStatement->SetParamL(KContactAccessCount, KSpace);
       
   457 	iSelectStatement->SetParamL(KContactCreationDate, KSpace);	
       
   458 	iSelectStatement->SetParamL(KContactLastModified, KSpace);
       
   459 	iSelectStatement->SetParamL(KContactGuidString, KSpace);
       
   460 	iSelectStatement->SetParamL(KContactFirstName, KSpace);
       
   461 	iSelectStatement->SetParamL(KContactLastName, KSpace);
       
   462 	iSelectStatement->SetParamL(KContactCompanyName, KSpace);
       
   463 	iSelectStatement->SetParamL(KContactFirstNamePrn, KSpace);
       
   464 	iSelectStatement->SetParamL(KContactLastNamePrn, KSpace);
       
   465 	iSelectStatement->SetParamL(KContactCompanyNamePrn, KSpace);
       
   466 	
       
   467 	HBufC* condition = HBufC::NewLC(KWhereStringEqualsStringFormatText().Size() + KContactId().Size());
       
   468 	TPtr ptrCondition = condition->Des();
       
   469 	ptrCondition.Format(KWhereStringEqualsStringFormatText, &KContactId, &KVar);
       
   470 	
       
   471 	// Set condition
       
   472 	iSelectStatement->SetConditionL(ptrCondition);
       
   473 	
       
   474 	CleanupStack::PopAndDestroy( condition ); 
       
   475 
       
   476 	// construct tables
       
   477 	iContactTable = CPplContactTable::NewL(iDatabase, iContactProperties);
       
   478 	iCommAddrTable = CPplCommAddrTable::NewL(iDatabase, iContactProperties);
       
   479 	iGroupTable = CPplGroupsTable::NewL(iDatabase);
       
   480 	iPreferencePersistor = CPplPreferencesPersistor::NewL(iDatabase);
       
   481 	}
       
   482 
       
   483 /**
       
   484 Cleanup operation. If one of the crud operation fails, this method will be called
       
   485 to rollback the sql transaction
       
   486 
       
   487 */
       
   488 void CPplContactItemManager::CleanupOperationRollbackL(TAny* aPplContactItemanager)
       
   489 	{
       
   490 	CPplContactItemManager* contactItemManager = static_cast<CPplContactItemManager*>(aPplContactItemanager);
       
   491 	contactItemManager->iTransactionManager.RollbackCurrentTransactionL(contactItemManager->iSessionId);
       
   492 	}
       
   493 	
       
   494 /**
       
   495 Utility method used to identify if the provided CContactItemField is first name, last name, 
       
   496 company name, first name pronunciation, last name pronunciation or company name pronunciation 
       
   497 Method used to fill information when a CContactItem is read from contact database
       
   498 
       
   499 @param aNameField reference to a constant CContactItemField for which the index will be returned
       
   500 
       
   501 */	
       
   502 TInt CPplContactItemManager::NameFieldIndex(const CContactItemField& aNameField) const
       
   503 	{
       
   504 	// For all the name fields...
       
   505 	for (TInt nameFieldNum=0; nameFieldNum < sizeof(KFastAccessFieldUids)/sizeof(TInt); ++nameFieldNum)
       
   506 		{
       
   507 		if (aNameField.ContentType().ContainsFieldType(TUid::Uid(KFastAccessFieldUids[nameFieldNum])))
       
   508 			{
       
   509 			return nameFieldNum;
       
   510 			}
       
   511 		}
       
   512 	return KErrNotFound;
       
   513 	}
       
   514 	
       
   515 /**
       
   516 Utility method used to start a transaction in database
       
   517 
       
   518 @param aSessionId id of the session
       
   519 */
       
   520 void CPplContactItemManager::StartTransactionL(TUint aSessionId)
       
   521 	{
       
   522 	iTransactionManager.StartTransactionL();
       
   523 	
       
   524 	iSessionId = aSessionId;
       
   525 	CleanupStack::PushL(TCleanupItem(CleanupOperationRollbackL, this));		
       
   526 	}
       
   527 	
       
   528 /**
       
   529 Utility method used to commit a transaction
       
   530 */
       
   531 void CPplContactItemManager::CommitTransactionL()
       
   532 	{
       
   533 	iTransactionManager.CommitCurrentTransactionL(iSessionId);
       
   534 		
       
   535 	CleanupStack::Pop(); //CleanupOperationRollbackL		
       
   536 	}
       
   537 	
       
   538 /**
       
   539 Utility method used to create tables in a newly create database
       
   540 */	
       
   541 void CPplContactItemManager::CreateTablesL()
       
   542 	{
       
   543 	TBool controlTransaction = !(iTransactionManager.IsTransactionActive());
       
   544 	
       
   545 	if(controlTransaction)
       
   546 		{
       
   547 		StartTransactionL(0);
       
   548 		}
       
   549 		
       
   550 	iContactTable->CreateTableL();	
       
   551 	iGroupTable->CreateTableL();	
       
   552 	iCommAddrTable->CreateTableL();
       
   553 	iPreferencePersistor->CreateTableL();
       
   554 	
       
   555 	if(controlTransaction)
       
   556 		{
       
   557 		CommitTransactionL();
       
   558 		}			
       
   559 	}
       
   560 	
       
   561 /**
       
   562 Utility method used to check if there is any record in the contact database
       
   563 
       
   564 @return ETrue if the contact database is empty and EFalse otherwise
       
   565 */	
       
   566 TBool CPplContactItemManager::IsDatabaseEmptyL()
       
   567 	{
       
   568 	return static_cast<CPplContactTable*>(iContactTable)->IsTableEmptyL();
       
   569 	}
       
   570 
       
   571 CContactIdArray* CPplContactItemManager::MatchPhoneNumberL(const TDesC& aNumber, TInt aMatchLengthFromRight)
       
   572 	{
       
   573 	// Call comm address table
       
   574 	if (aMatchLengthFromRight == KBestMatchingPhoneNumbers)
       
   575 	    {
       
   576 	    return  static_cast<CPplCommAddrTable*>(iCommAddrTable)->BestMatchingPhoneNumberL(aNumber);
       
   577 	    }
       
   578 	else
       
   579 	    {
       
   580 	    return  static_cast<CPplCommAddrTable*>(iCommAddrTable)->MatchPhoneNumberL(aNumber, aMatchLengthFromRight);
       
   581 	    }
       
   582 	}
       
   583 
       
   584 /**
       
   585 Utility method used to retrieve an array of card template ids
       
   586 
       
   587 @return reference to a CContactIdArray containing the card template ids
       
   588 */
       
   589 CContactIdArray& CPplContactItemManager::CardTemplateIdsL()
       
   590 	{
       
   591 	// Call contact table
       
   592 	return  static_cast<CPplContactTable*>(iContactTable)->CardTemplateIdsL();	
       
   593 	}
       
   594 
       
   595 /**
       
   596 Utility method used to retrieve a reference to persistence layer preference persistor
       
   597 */
       
   598 CPplPreferencesPersistor& CPplContactItemManager::PreferencesPersitor()  
       
   599 	{
       
   600 	return *iPreferencePersistor;
       
   601 	}
       
   602 
       
   603 /**
       
   604 Utility method used to retrieve own card contact id
       
   605 */
       
   606 TContactItemId CPplContactItemManager::OwnCardIdL()
       
   607 	{
       
   608 	// Call contact table
       
   609 	return  static_cast<CPplContactTable*>(iContactTable)->OwnCardIdL();		
       
   610 	}
       
   611 	
       
   612 /**
       
   613 Utility method used to set own card id
       
   614 */	
       
   615 void CPplContactItemManager::SetOwnCardIdL(TContactItemId aId)
       
   616 	{	
       
   617 	// Call contact table
       
   618 	static_cast<CPplContactTable*>(iContactTable)->SetOwnCardIdL(aId);		
       
   619 	}
       
   620 
       
   621 /**
       
   622 Utility class. Returns an array with ids of all groups in contact database
       
   623 */
       
   624 CContactIdArray* CPplContactItemManager::GroupIdListL()
       
   625 	{
       
   626 	CContactIdArray* idArray = CContactIdArray::NewLC();
       
   627 
       
   628 	HBufC* selectString = HBufC::NewLC(KOneTypeField().Length() + KSqlContactTableName().Length() + KContactTypeFlags().Length() + 3); 
       
   629 	TPtr ptrSelectString = selectString->Des();
       
   630 	ptrSelectString.Format(KOneTypeField, &KContactId, &KSqlContactTableName, &KContactTypeFlags, EContactType_Shift, EContactTypeFlags_Group);
       
   631 
       
   632 	RSqlStatement selectStatement;
       
   633 	CleanupClosePushL(selectStatement);
       
   634 	
       
   635 	User::LeaveIfError(selectStatement.Prepare(iDatabase, ptrSelectString));
       
   636 	const TInt KIdx = iSelectStatement->ParameterIndex(KContactId);
       
   637 	
       
   638 	TInt err;
       
   639 	while((err = selectStatement.Next()) == KSqlAtRow)
       
   640 		{
       
   641 		idArray->AddL(selectStatement.ColumnInt(KIdx));	
       
   642 		}
       
   643 
       
   644 	if(err != KSqlAtEnd)
       
   645 		{
       
   646 		User::Leave(err);
       
   647 		}
       
   648 
       
   649     CleanupStack::PopAndDestroy(&selectStatement);
       
   650 	CleanupStack::PopAndDestroy(selectString);
       
   651 	CleanupStack::Pop(idArray);
       
   652 	
       
   653 	return idArray;
       
   654 	}
       
   655 
       
   656 /**
       
   657 Utility method used to rthe prefered card template id
       
   658 */
       
   659 TInt CPplContactItemManager::CardTemplatePrefIdL() const
       
   660 	{
       
   661 	return iPreferencePersistor->PreferredCardTemplateIdL();
       
   662 	}
       
   663 
       
   664 /**
       
   665 Utility method used to set the prefered card template id
       
   666 */
       
   667 void CPplContactItemManager::SetCardTemplatePrefIdL(TInt aCardTemplatePrefId)
       
   668 	{
       
   669 	iPreferencePersistor->SetPreferredCardTemplateIdL(aCardTemplatePrefId);
       
   670 	}