phonebookengines_old/contactsmodel/tsrc/T_UTILS.CPP
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 1997-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 #include <e32test.h>
       
    17 #include <versit.h>
       
    18 #include <s32file.h>
       
    19 #include "T_UTILS.H"
       
    20 
       
    21 //
       
    22 // CCntTest
       
    23 //
       
    24 EXPORT_C CCntTest::~CCntTest()
       
    25 	{
       
    26 	delete iDb;
       
    27 	delete iCleanup;
       
    28 	iFs.Close();
       
    29 	// RTest object is created in each test harness and does a __UHEAP_MARK
       
    30 	// the Close() method does a __UHEAP_MARKEND and so must now be the last action
       
    31     iTest->Close();
       
    32 	}
       
    33 
       
    34 EXPORT_C void CCntTest::ConstructL(RTest &aTest, const TDesC &aDatabaseName)
       
    35 	{
       
    36 	iDatabaseName.Copy(aDatabaseName);
       
    37 	CActiveScheduler::Install(new(ELeave) CActiveScheduler);
       
    38 	iTest=&aTest;
       
    39 	iTest->Title();
       
    40     iCleanup=CTrapCleanup::New();
       
    41 	User::LeaveIfError(iFs.Connect());
       
    42 	}
       
    43 
       
    44 EXPORT_C void CCntTest::SelectDriveL()
       
    45 	{
       
    46 	TDriveList driveList;
       
    47 	iFs.DriveList(driveList);
       
    48 	driveList[25]=0;	// Get rid of Z:
       
    49 	TBuf<2> drive;
       
    50 	drive.Copy(_L("C:"));
       
    51 	TInt driveCount=0;
       
    52 	for(TInt loop0=0;loop0<driveList.Size();loop0++)
       
    53 		if (driveList[loop0])
       
    54 			driveCount++;
       
    55 	if (driveCount>1)
       
    56 		{
       
    57 		iTest->Printf(_L("Select drive for multi access test:\n"));
       
    58 		for(TInt loop=0;loop<driveList.Size();loop++)
       
    59 			if (driveList[loop])
       
    60 				iTest->Printf(_L("%c:\n"),loop+'A');
       
    61 		FOREVER
       
    62 			{
       
    63 			TChar key=iTest->Getch();
       
    64 			TInt index=key.GetLowerCase()-'a';
       
    65 			if (index>=0 && index<driveList.Size() && driveList[index])
       
    66 				{
       
    67 				drive[0]=(TText)(index+'A');
       
    68 				break;
       
    69 				}
       
    70 			}
       
    71 		}
       
    72 	TParse parse;
       
    73 	parse.Set(drive,&iDatabaseName,NULL);
       
    74 	iDatabaseName.Copy(parse.FullName());
       
    75 	}
       
    76 
       
    77 EXPORT_C void CCntTest::EndTestLib(TInt aErr)
       
    78 	{
       
    79     (*iTest)(aErr==KErrNone);
       
    80 
       
    81 	iTest->End();
       
    82 	(*iTest)(iFs.ResourceCount()==0);
       
    83 	delete CActiveScheduler::Current();
       
    84 	delete this;
       
    85 	}
       
    86 
       
    87 EXPORT_C CContactDatabase *CCntTest::CreateDatabaseL()
       
    88 	{
       
    89 	CloseDatabase();
       
    90 	iDb=CContactDatabase::ReplaceL(iDatabaseName);
       
    91 	return(iDb);
       
    92 	}
       
    93 
       
    94 EXPORT_C CContactDatabase* CCntTest::OpenDatabaseL()
       
    95 	{
       
    96 	CloseDatabase();
       
    97 	iDb=CContactDatabase::OpenL(iDatabaseName);
       
    98 	return(iDb);
       
    99 	}
       
   100 
       
   101 EXPORT_C void CCntTest::CloseDatabase()
       
   102 	{
       
   103 	delete iDb;
       
   104 	iDb=NULL;
       
   105 	}
       
   106 
       
   107 EXPORT_C void CCntTest::DeleteDatabaseL()
       
   108 	{
       
   109 	CContactDatabase::DeleteDatabaseL(iDatabaseName);	
       
   110 	}
       
   111 
       
   112 EXPORT_C void CCntTest::DeleteAllTemplateFieldsL()
       
   113 	{
       
   114 	CContactItemViewDef *viewDef=CContactItemViewDef::NewL(CContactItemViewDef::EMaskFields,CContactItemViewDef::EIncludeHiddenFields);
       
   115 	CContactItem *item=iDb->OpenContactLX(iDb->TemplateId(),*viewDef);
       
   116 	delete viewDef;
       
   117 	CleanupStack::PushL(item);
       
   118 	CContactItemFieldSet& fieldSet=item->CardFields();
       
   119 	while(fieldSet.Count())
       
   120 		fieldSet.Remove(0);
       
   121 	iDb->CommitContactL(*item);
       
   122 	CleanupStack::PopAndDestroy(2); // item,close
       
   123 	}
       
   124 
       
   125 EXPORT_C void CCntTest::TestField(CContactItemField &aField,TStorageType aType,TFieldType aFieldType,TUid aMapping)
       
   126 	{
       
   127 	(*iTest)(aField.StorageType()==aType);
       
   128 	(*iTest)(aField.ContentType().ContainsFieldType(aFieldType));
       
   129 	(*iTest)(aField.ContentType().Mapping()==aMapping);
       
   130 	}
       
   131 
       
   132 EXPORT_C void CCntTest::AdjustContactAccessCountL(TContactItemId aItemId, TInt aCount)
       
   133 	{
       
   134 	CContactItem *incItem=iDb->OpenContactLX(aItemId);
       
   135 	CleanupStack::PushL(incItem);
       
   136 	while(aCount>0)
       
   137 		{
       
   138 		incItem->IncAccessCount();
       
   139 		aCount--;
       
   140 		}
       
   141 	while(aCount<0)
       
   142 		{
       
   143 		incItem->DecAccessCount();
       
   144 		aCount++;
       
   145 		}
       
   146 	iDb->CommitContactL(*incItem);
       
   147 	CleanupStack::PopAndDestroy(2);	// incItem, Close(incItem)
       
   148 	}
       
   149 
       
   150 EXPORT_C void CCntTest::TestAccessCount(TContactItemId aItemId, TInt aCount)
       
   151 	{
       
   152 	CContactItem *item=iDb->ReadContactLC(aItemId);
       
   153 	(*iTest)(item->AccessCount()==aCount);
       
   154 	CleanupStack::PopAndDestroy();	// item
       
   155 	}
       
   156 
       
   157 EXPORT_C TBool CCntTest::CompareFields(CContactItemField &aField1, CContactItemField &aField2)
       
   158 	{
       
   159 	return(aField1.Label()==aField2.Label() &&
       
   160 			aField1.StorageType()==aField2.StorageType() &&
       
   161 			aField1.ContentType()==aField2.ContentType());
       
   162 	}
       
   163 
       
   164 EXPORT_C void CCntTest::CheckDeletedContact(TContactItemId aDeleteId)
       
   165 	{
       
   166 	CContactItem *deletedContact=iDb->ReadContactLC(aDeleteId);
       
   167 	(*iTest)(deletedContact->IsDeleted());
       
   168 	(*iTest)(deletedContact->CardFields().Count()==0);
       
   169 	TBuf<10> textDef;
       
   170 	iDb->ReadContactTextDefL(aDeleteId,textDef);
       
   171 	(*iTest)(textDef.Length()==0);
       
   172 	CleanupStack::PopAndDestroy();	// deletedContact
       
   173 	}
       
   174 
       
   175 EXPORT_C void CCntTest::CheckContactDoesNotExist(TContactItemId aNotExistId)
       
   176 	{
       
   177 	TRAPD(notExist,iDb->ReadContactLC(aNotExistId));
       
   178 	(*iTest)(notExist==KErrNotFound);
       
   179 	}
       
   180 
       
   181 EXPORT_C void CCntTest::DeleteContact(TContactItemId aDeleteMe)
       
   182 	{
       
   183 	TRAP_IGNORE(iDb->DeleteContactL(aDeleteMe));
       
   184 	}
       
   185 
       
   186 EXPORT_C HBufC *CCntTest::ContactUidLC(TContactItemId aId, TInt64 aMachineId)
       
   187 	{
       
   188 	CContactItem *contact=iDb->ReadContactLC(aId);
       
   189 	HBufC *uid=contact->UidStringL(aMachineId).AllocL();
       
   190 	CleanupStack::PopAndDestroy();	// deletedContact
       
   191 	CleanupStack::PushL(uid);
       
   192 	return(uid);
       
   193 	}
       
   194 
       
   195 EXPORT_C CVCardTestStore* CCntTest::ExportContactsLC(const CContactIdArray *aIds, TUint aFormatFlags, TVCardStoreType aType, TInt aFileNum)
       
   196 	{
       
   197 	CVCardTestStore* store=ExportContactsL(iDb,aIds,aFormatFlags, aType, &iFs, aFileNum);
       
   198 	CleanupStack::PushL(store); // outstream
       
   199 	return(store);
       
   200 	}
       
   201 
       
   202 EXPORT_C CVCardTestStore* CCntTest::ExportContactLC(TContactItemId aId, TUint aFormatFlags, TVCardStoreType aType, TInt aFileNum)
       
   203 	{
       
   204 	CContactIdArray *ids=CContactIdArray::NewLC();
       
   205 	ids->AddL(aId);
       
   206 	CVCardTestStore* store=ExportContactsLC(ids,aFormatFlags, aType, aFileNum);
       
   207 	CleanupStack::Pop(); // store
       
   208 	CleanupStack::PopAndDestroy(); // ids
       
   209 	CleanupStack::PushL(store);
       
   210 	return(store);
       
   211 	}
       
   212 
       
   213 EXPORT_C void CCntTest::ImportContacts1by1L(CVCardTestStore* aStore, TUint aFormatFlags)
       
   214 	{
       
   215 	RStoreReadStream instream;
       
   216 	instream.OpenLC(*aStore->Store(),aStore->StreamId());
       
   217 	TBool success=EFalse;
       
   218 	do
       
   219 		{
       
   220 		CArrayPtr<CContactItem>* items=iDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl),instream,success,aFormatFlags);
       
   221 		items->ResetAndDestroy();
       
   222 		delete items;
       
   223 		} while(success);
       
   224 	CleanupStack::PopAndDestroy(); // instream
       
   225 	}
       
   226 
       
   227 EXPORT_C CArrayPtr<CContactItem>* CCntTest::ImportContactsL(CVCardTestStore* aStore, TUint aFormatFlags)
       
   228 	{
       
   229 	return(::ImportContactsL(iDb,aStore,aFormatFlags));
       
   230 	}
       
   231 
       
   232 EXPORT_C CArrayPtr<CContactItem>* CCntTest::ImportContactsLC(CVCardTestStore* aStore, TUint aFormatFlags)
       
   233 	{
       
   234 	CArrayPtr<CContactItem>* items=ImportContactsL(aStore, aFormatFlags);
       
   235 	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,items));
       
   236 	return(items);
       
   237 	}
       
   238 
       
   239 class RTestSession : public RSessionBase
       
   240 	{
       
   241 public:
       
   242 	TInt CreateSession();
       
   243 	};
       
   244 
       
   245 TInt RTestSession::CreateSession()
       
   246 	{
       
   247 	TVersion version;
       
   248 	return(RSessionBase::CreateSession(_L("CntLockServer"),version,1));
       
   249 	}
       
   250 
       
   251 EXPORT_C TBool CCntTest::LockServerSessionExists(TBool aPause) const
       
   252 	{
       
   253 	if (aPause)
       
   254 		User::After(200000);
       
   255 	RTestSession session;
       
   256 	TInt sessionErr=session.CreateSession();
       
   257 	session.Close();
       
   258 	return(sessionErr==KErrNone);
       
   259 	}
       
   260 
       
   261 EXPORT_C void CCntTest::PrintfNewline() const
       
   262 	{
       
   263 	(*iTest).Printf(_L("\n"));
       
   264 	}
       
   265 
       
   266 EXPORT_C TInt CCntTest::LockServerProcessCount(TBool aPause, TBool aDisplayCount, TBool aDisplayList) const
       
   267 	{
       
   268 	if (aPause)
       
   269 		User::After(200000);
       
   270 	TInt count=0;
       
   271 	TFullName fullName;
       
   272 	TFindProcess find(_L("cntsrv*"));
       
   273 	if (aDisplayList)
       
   274 		iTest->Printf(_L("Lock server process list\n"));
       
   275 	while(find.Next(fullName)==KErrNone)
       
   276 		{
       
   277 		count++;
       
   278 		if (aDisplayList)
       
   279 			{
       
   280 			iTest->Printf(fullName);
       
   281 			PrintfNewline();
       
   282 			}
       
   283 		}
       
   284 	if (aDisplayCount)
       
   285 		{
       
   286 		iTest->Printf(_L("Process count=%d"),count);
       
   287 		PrintfNewline();
       
   288 		}
       
   289 	return(count);
       
   290 	}
       
   291 
       
   292 EXPORT_C void CCntTest::TestLockServerExists() const
       
   293 	{
       
   294 	(*iTest)(LockServerProcessCount(EFalse,EFalse,ETrue)==1);
       
   295 	}
       
   296 
       
   297 EXPORT_C void CCntTest::TestNoLockServer() const
       
   298 	{
       
   299 	TInt retries=0;
       
   300 	FOREVER
       
   301 		{
       
   302 		TInt count=LockServerProcessCount(EFalse,EFalse,ETrue);
       
   303 		if (count==0)
       
   304 			break;
       
   305 		if (retries==5)
       
   306 			{
       
   307 			iTest->Printf(_L("Error lock server count=%d\nPress 't' to try again\n"),count);
       
   308 			TChar key=iTest->Getch();
       
   309 			if (key=='t')
       
   310 				retries--;
       
   311 			else
       
   312 				(*iTest)(EFalse);
       
   313 			}
       
   314 		User::After(100000);
       
   315 		retries++;
       
   316 		}
       
   317 	}
       
   318 
       
   319 
       
   320 EXPORT_C void CCntTest::TestLockServerCloses() const
       
   321 	{
       
   322 	// Contacts Lock Server waits 5sec from last client until shutdown
       
   323 	TInt time = 0;
       
   324 	TInt count = LockServerProcessCount();
       
   325 	
       
   326 	while (count)
       
   327 		{
       
   328 		if (time > 55)
       
   329 			{ // 5.5 sec - too slow
       
   330 			(*iTest)(EFalse);
       
   331 			}
       
   332 		// wait a 1/10th second, then count again
       
   333 		User::After(100000);
       
   334 		time++;
       
   335 		count = LockServerProcessCount();
       
   336 		}
       
   337 	}
       
   338 	
       
   339 
       
   340 /*
       
   341 The RDebug::Profile APIs are removed from secure builds using
       
   342 the __SECURE_API__ macro.
       
   343 
       
   344 Although the APIs are present in EKA2, the EKA2 macro has been
       
   345 used because the implementation of the RDebug::Profile APIs 
       
   346 does not do anything in EKA2.
       
   347 
       
   348 In EKA2
       
   349 TCntProfileTls is stored in 'Thread Local Storage' and contains
       
   350 a TTime, used to store the time when ProfileStart() is called
       
   351 and a TCntProfile, used to store the time elapsed and to count
       
   352 the pairs of calls to ProfileStart() and ProfileEnd().
       
   353 Only 1 'Profile Bin' can be used in EKA2 with this approach.
       
   354 
       
   355 The 'Thread Local Storage' is stored on the heap so therfore
       
   356 if a call to ProfileStart() is made then it must be followed
       
   357 by a call to either ProfileReset() or ProfileResult() to ensure
       
   358 the memory is freed
       
   359 */
       
   360 
       
   361 EXPORT_C TInt CCntTest::ProfileResult(TCntProfile* aProfile,TInt,TInt)
       
   362 	{
       
   363 	TCntProfileTls* tls = (TCntProfileTls*)Dll::Tls();
       
   364 	if(!tls)
       
   365   		{
       
   366   		// The tls has not been created
       
   367   		// do nothing
       
   368   		}
       
   369   	else
       
   370   		{
       
   371   		*aProfile = tls->iProfile;
       
   372   		delete tls;
       
   373   		Dll::FreeTls();
       
   374   		}
       
   375 	return KErrNone;
       
   376 	}
       
   377 
       
   378 EXPORT_C TInt CCntTest::ProfileReset(TInt,TInt)
       
   379 	{
       
   380 	// Always exit with no tls allocated
       
   381 	TCntProfileTls* tls = (TCntProfileTls*)Dll::Tls();
       
   382 	if(!tls)
       
   383 		{
       
   384   		// The tls has not been created
       
   385   		// do nothing
       
   386 		}
       
   387 	else
       
   388 		{
       
   389   		delete tls;
       
   390 		Dll::FreeTls();
       
   391   		}
       
   392 	return KErrNone;
       
   393 	}
       
   394 
       
   395 EXPORT_C TInt CCntTest::ProfileStart(TInt)
       
   396 	{
       
   397 	TCntProfileTls* tls = (TCntProfileTls*)Dll::Tls();
       
   398 	if(!tls)
       
   399   		{
       
   400   		tls = new (ELeave) TCntProfileTls();
       
   401   		Dll::SetTls(tls);
       
   402   		tls->iProfile.iTime = 0;
       
   403   		tls->iProfile.iCount = 0;
       
   404   		}
       
   405 	++tls->iProfile.iCount;
       
   406 	tls->iTime.UniversalTime();
       
   407 	return KErrNone;
       
   408 	}
       
   409 
       
   410 EXPORT_C TInt CCntTest::ProfileEnd(TInt)
       
   411 	{
       
   412 	TCntProfileTls* tls = (TCntProfileTls*)Dll::Tls();
       
   413 	if(!tls)
       
   414   		{
       
   415   		// The tls has not been created
       
   416   		// do nothing
       
   417   		}
       
   418   	else
       
   419   		{
       
   420 		TTime myTime;
       
   421 		myTime.UniversalTime();
       
   422 		tls->iProfile.iTime += ((myTime).MicroSecondsFrom(tls->iTime)).Int64();
       
   423   		}
       
   424   	return KErrNone;
       
   425 	}
       
   426 
       
   427 /*
       
   428 The RDebug::Profile APIs are present and working on EKA1 (keep these for when 
       
   429 this functionality is reinstated for EKA2.
       
   430 
       
   431 
       
   432 
       
   433 EXPORT_C TInt CCntTest::ProfileResult(TCntProfile* aProfile,TInt aStart,TInt aCount)
       
   434 	{
       
   435 	TProfile* p = reinterpret_cast<TProfile*>(&aProfile[0]);
       
   436 	const TInt result = RDebug::ProfileResult(p,aStart,aCount);
       
   437 	return result;
       
   438 	}
       
   439 
       
   440 EXPORT_C TInt CCntTest::ProfileReset(TInt aStart,TInt aCount)
       
   441 	{
       
   442 	return RDebug::ProfileReset(aStart, aCount);
       
   443 	}
       
   444 
       
   445 EXPORT_C TInt CCntTest::ProfileStart(TInt aProfile)
       
   446 	{
       
   447 	return RDebug::ProfileStart(aProfile);
       
   448 	}
       
   449 
       
   450 EXPORT_C TInt CCntTest::ProfileEnd(TInt aProfile)
       
   451 	{
       
   452 	return RDebug::ProfileEnd(aProfile);
       
   453 	}
       
   454 */
       
   455 
       
   456 
       
   457 EXPORT_C TBool TestGroupStateL(CContactDatabase* aDb, TInt aGroupCount, TInt aGroupEntryCount)
       
   458 	{
       
   459 	CContactIdArray* groupIdList=aDb->GetGroupIdListL();
       
   460 	CleanupStack::PushL(groupIdList);
       
   461 	TInt groupCount=groupIdList->Count();
       
   462 	if (aGroupCount>=0 && groupCount!=aGroupCount)
       
   463 		return(EFalse);
       
   464 	TInt groupEntryCount=0;
       
   465 // Cross check that all contacts think they are in all the groups that think they contain the contact
       
   466 	for(TInt groupIndex=0;groupIndex<groupCount;groupIndex++)
       
   467 		{
       
   468 		TContactItemId groupId=(*groupIdList)[groupIndex];
       
   469 		CContactGroup *group=(CContactGroup *)aDb->ReadContactLC(groupId);
       
   470 		const CContactIdArray* itemsContained=group->ItemsContained();
       
   471 		TInt itemCount=itemsContained->Count();
       
   472 		groupEntryCount+=itemCount;
       
   473 		for (TInt itemLoop=0;itemLoop<itemCount;itemLoop++)
       
   474 			{
       
   475 			CContactItemPlusGroup *contact=(CContactItemPlusGroup*)aDb->ReadContactLC((*itemsContained)[itemLoop]);
       
   476 			CContactIdArray* groupsJoined=contact->GroupsJoinedLC();
       
   477 			TInt groupsJoinedLoop=0;
       
   478 			for (;groupsJoinedLoop<groupsJoined->Count();groupsJoinedLoop++)
       
   479 				{
       
   480 				if ((*groupsJoined)[groupsJoinedLoop]==groupId)
       
   481 					break;
       
   482 				}
       
   483 			if (groupsJoinedLoop==groupsJoined->Count())
       
   484 				return(EFalse);
       
   485 			CleanupStack::PopAndDestroy(2,contact);
       
   486 			}
       
   487 		CleanupStack::PopAndDestroy(group);
       
   488 		}
       
   489 	if (aGroupEntryCount>=0 && groupEntryCount!=aGroupEntryCount)
       
   490 		return(EFalse);
       
   491 	CleanupStack::PopAndDestroy(groupIdList);
       
   492 // Now cross check the other way, 
       
   493 	const CContactIdArray* contacts=aDb->SortedItemsL();
       
   494 	for(TInt contactIndex=0;contactIndex<contacts->Count();contactIndex++)
       
   495 		{
       
   496 		TContactItemId contactId=(*contacts)[contactIndex];
       
   497 		CContactItemPlusGroup *item=(CContactItemPlusGroup*)aDb->ReadContactLC(contactId);
       
   498 		CContactIdArray* groupsJoined=item->GroupsJoinedLC();
       
   499 		for (TInt groupsJoinedLoop=0;groupsJoinedLoop<groupsJoined->Count();groupsJoinedLoop++)
       
   500 			{
       
   501 			CContactGroup *group=(CContactGroup *)aDb->ReadContactLC((*groupsJoined)[groupsJoinedLoop]);
       
   502 			const CContactIdArray* itemsContained=group->ItemsContained();
       
   503 			TInt itemsCount=itemsContained->Count();
       
   504 			TInt itemLoop=0;
       
   505 			for (;itemLoop<itemsCount;itemLoop++)
       
   506 				{
       
   507 				if ((*itemsContained)[itemLoop]==contactId)
       
   508 					break;
       
   509 				}
       
   510 			if (itemLoop==itemsCount)
       
   511 				return(EFalse);
       
   512 			CleanupStack::PopAndDestroy(group);
       
   513 			}
       
   514 		CleanupStack::PopAndDestroy(2,item);
       
   515 		}
       
   516 	return(ETrue);
       
   517 	}
       
   518 
       
   519 //
       
   520 // Misc Util functions
       
   521 //
       
   522 
       
   523 EXPORT_C void CleanUpResetAndDestroy(TAny *aArray)
       
   524 	{
       
   525 	if (aArray)
       
   526 		{
       
   527 		CArrayPtr<CContactItem>* array=(CArrayPtr<CContactItem>*)aArray;
       
   528 		array->ResetAndDestroy();
       
   529 		delete array;
       
   530 		}
       
   531 	}
       
   532 
       
   533 EXPORT_C TPtrC FieldText(CContactItem& aItem,TUid aType)
       
   534 	{
       
   535 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   536 	const TInt pos=fieldSet.Find(aType);
       
   537 	if (pos==KErrNotFound || fieldSet[pos].StorageType()!=KStorageTypeText)
       
   538 		return _L("");
       
   539 	return fieldSet[pos].TextStorage()->Text();
       
   540 	}
       
   541 
       
   542 EXPORT_C void SetNameL(CContactItem& aItem,TUid aFieldType,TUid aMapping, const TDesC& aName, TBool aAddEntry)
       
   543 //
       
   544 // Set the contents of a text field, creating the field if required
       
   545 //
       
   546 	{
       
   547 	TInt pos=KErrNotFound;
       
   548 	if (!aAddEntry)
       
   549 		{
       
   550 		CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   551 		pos=fieldSet.Find(aFieldType);
       
   552 		if (pos!=KErrNotFound)
       
   553 			fieldSet[pos].TextStorage()->SetTextL(aName);
       
   554 		}
       
   555 	if (pos==KErrNotFound)
       
   556 		{
       
   557 		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,aFieldType);
       
   558 		field->SetMapping(aMapping);
       
   559 		field->TextStorage()->SetTextL(aName);
       
   560 		aItem.AddFieldL(*field);
       
   561 		CleanupStack::Pop(); // item
       
   562 		}
       
   563 	}
       
   564 
       
   565 EXPORT_C void SetUserFlags(CContactItemFieldSet& aFieldSet, TUint aFlags, TInt aIndex)
       
   566 	{
       
   567 	aFieldSet[aIndex].SetUserFlags(aFlags);
       
   568 	}
       
   569 
       
   570 EXPORT_C void SetUserFlags(CContactItemFieldSet& aFieldSet, TUint aFlags)
       
   571 	{
       
   572 	SetUserFlags(aFieldSet,aFlags,aFieldSet.Count()-1);
       
   573 	}
       
   574 
       
   575 EXPORT_C void SetUserFlags(CContactItem* aItem, TUint aFlags, TInt aIndex)
       
   576 	{
       
   577 	SetUserFlags(aItem->CardFields(),aFlags,aIndex);
       
   578 	}
       
   579 
       
   580 EXPORT_C void SetUserFlags(CContactItem* aItem, TUint aFlags)
       
   581 	{
       
   582 	SetUserFlags(aItem->CardFields(),aFlags);
       
   583 	}
       
   584 
       
   585 EXPORT_C void AddFieldL(CContactItemFieldSet* aFieldSet, TStorageType aType,TFieldType aFieldType,TUid aMapping)
       
   586 	{
       
   587 	CContactItemField* field=CContactItemField::NewLC(aType,aFieldType);
       
   588 	field->SetMapping(aMapping);
       
   589 	aFieldSet->AddL(*field);
       
   590 	CleanupStack::Pop(); // field
       
   591 	}
       
   592 
       
   593 EXPORT_C void AddFieldL(CContactItem* aItem, TStorageType aType,TFieldType aFieldType,TUid aMapping)
       
   594 	{
       
   595 	CContactItemField* field=CContactItemField::NewLC(aType,aFieldType);
       
   596 	field->SetMapping(aMapping);
       
   597 	aItem->AddFieldL(*field);
       
   598 	CleanupStack::Pop(); // field
       
   599 	}
       
   600 
       
   601 EXPORT_C void InsertFieldL(CContactItem& aItem, TInt aPos, TStorageType aType,TFieldType aFieldType,TUid aMapping)
       
   602 	{
       
   603 	CContactItemField* field=CContactItemField::NewLC(aType,aFieldType);
       
   604 	field->SetMapping(aMapping);
       
   605 	aItem.InsertFieldL(*field,aPos);
       
   606 	CleanupStack::Pop(); // field
       
   607 	}
       
   608 
       
   609 EXPORT_C void InsertFieldL(CContactItem& aItem, TInt aPos, TStorageType aType,TFieldType aFieldType,TUid aMapping, const TDesC& aText)
       
   610 	{
       
   611 	CContactItemField* field=CContactItemField::NewLC(aType,aFieldType);
       
   612 	field->SetMapping(aMapping);
       
   613 	aItem.InsertFieldL(*field,aPos);
       
   614 	field->TextStorage()->SetTextL(aText);
       
   615 	CleanupStack::Pop(); // field
       
   616 	}
       
   617 
       
   618 EXPORT_C TContactItemId AddContactL(CContactDatabase* aDb, TFieldType aFieldType,TUid aMapping, const TDesC &aText)
       
   619 	{
       
   620 	CContactCard *item=CContactCard::NewLC();
       
   621 	SetNameL(*item,aFieldType,aMapping,aText,ETrue);
       
   622 	TContactItemId id=aDb->AddNewContactL(*item);
       
   623 	CleanupStack::PopAndDestroy();	// item
       
   624 	return(id);
       
   625 	}
       
   626 
       
   627 EXPORT_C CVCardTestStore* ExportContactsL(CContactDatabase* aDb, const CContactIdArray *aIds, TUint aFormatFlags, TVCardStoreType aType, RFs *aFs, TInt aFileNum)
       
   628 	{
       
   629 	CVCardTestStore* store=CVCardTestStore::NewLC(aType,aFs,aFileNum);
       
   630 	RStoreWriteStream outstream;
       
   631 	TStreamId streamId=outstream.CreateLC(*store->Store());
       
   632 	TUid uid;
       
   633 	uid.iUid=KUidVCardConvDefaultImpl;
       
   634 	aDb->ExportSelectedContactsL(uid,*aIds,outstream,aFormatFlags);
       
   635 	outstream.CommitL();
       
   636 	store->SetStreamId(streamId);
       
   637 	store->Store()->CommitL();  	
       
   638 	CleanupStack::PopAndDestroy(); // outstream
       
   639 	CleanupStack::Pop(); // store
       
   640 	return(store);
       
   641 	}
       
   642 
       
   643 EXPORT_C CArrayPtr<CContactItem>* ImportContactsL(CContactDatabase* aDb, CVCardTestStore* aStore, TUint aFormatFlags)
       
   644 	{
       
   645 	RStoreReadStream instream;
       
   646 	instream.OpenLC(*aStore->Store(),aStore->StreamId());
       
   647 	TBool success=EFalse;
       
   648 	CArrayPtr<CContactItem>* items=aDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl),instream,success,aFormatFlags);
       
   649 	CleanupStack::PopAndDestroy(); // instream
       
   650 	return(items);
       
   651 	}
       
   652 
       
   653 EXPORT_C TBool CompareItemField(const CContactItemField &aItemField1, const CContactItemField &aItemField2, TBool aCheckStorage)
       
   654 	{
       
   655 	if (aItemField1.StorageType()!=aItemField2.StorageType())
       
   656 		return(EFalse);
       
   657 	TInt count=aItemField1.ContentType().FieldTypeCount();
       
   658 	if (count!=aItemField2.ContentType().FieldTypeCount())
       
   659 		return(EFalse);
       
   660 	for(TInt loop=0;loop<count;loop++)
       
   661 		if (!aItemField2.ContentType().ContainsFieldType(aItemField1.ContentType().FieldType(loop)))
       
   662 			return(EFalse);
       
   663 	if (aItemField1.StorageType()!=aItemField2.StorageType())
       
   664 		return(EFalse);
       
   665 	if (aItemField1.Label()!=aItemField2.Label())
       
   666 		return(EFalse);
       
   667 	if (aItemField1.IsHidden()!=aItemField2.IsHidden())
       
   668 		return(EFalse);
       
   669 	if (aItemField1.IsReadOnly()!=aItemField2.IsReadOnly())
       
   670 		return(EFalse);
       
   671 	if (aItemField1.IsDisabled()!=aItemField2.IsDisabled())
       
   672 		return(EFalse);
       
   673 	if (aItemField1.UserFlags()!=aItemField2.UserFlags())
       
   674 		return(EFalse);
       
   675 	if (aCheckStorage)
       
   676 		{
       
   677 		switch(aItemField1.StorageType())
       
   678 			{
       
   679 		case KStorageTypeText:
       
   680 			{
       
   681 			TPtrC f1=aItemField1.TextStorage()->Text();
       
   682 			TPtrC f2=aItemField2.TextStorage()->Text();
       
   683 			TInt len=f1.Length();
       
   684 			if (len==f2.Length()+1 && f1[0]==' ')
       
   685 				f1.Set(&f1[1],len-1);
       
   686 			//if (aItemField1.TextStorage()->Text()!=aItemField2.TextStorage()->Text())
       
   687 			if (f1!=f2)
       
   688 				return(EFalse);
       
   689 			break;
       
   690 			}
       
   691 		case KStorageTypeStore:
       
   692 			break;
       
   693 		case KStorageTypeContactItemId:
       
   694 			break;
       
   695 		case KStorageTypeDateTime:
       
   696 			if (aItemField1.DateTimeStorage()->Time()!=aItemField2.DateTimeStorage()->Time())
       
   697 				return(EFalse);
       
   698 			break;
       
   699 			}
       
   700 		}
       
   701 	return(ETrue);
       
   702 	}
       
   703 
       
   704 EXPORT_C TBool CompareItemFields(CContactItem *aItem1, CContactItem *aItem2, TBool aCheckStorage, TBool aTemplateLabelRemoved)
       
   705 	{
       
   706 	CContactItemFieldSet& fieldSet1 = aItem1->CardFields();
       
   707 	CContactItemFieldSet& fieldSet2 = aItem2->CardFields();
       
   708 	TInt count = fieldSet1.Count();
       
   709 	TInt check = aTemplateLabelRemoved?count-1:count;
       
   710 	if (check != fieldSet2.Count())
       
   711 	    {
       
   712 		return(EFalse);
       
   713 	    }
       
   714 	    
       
   715 	for(TInt loop=0;loop<count;loop++)
       
   716 		{
       
   717 		const CContactItemField &field1 = fieldSet1[loop];
       
   718 		if (aTemplateLabelRemoved && field1.ContentType().ContainsFieldType(KUidContactFieldTemplateLabel))
       
   719 		    {
       
   720 		    continue;
       
   721 		    }
       
   722 		
       
   723 		TBool found(EFalse);
       
   724 		for(TInt loop2 = 0; loop2 < count; loop2++)
       
   725 		    {
       
   726 		    found = CompareItemField(field1, fieldSet2[loop2], aCheckStorage);
       
   727 		    if(found) 
       
   728 		        {
       
   729 		        break;
       
   730 		        }
       
   731 		    }
       
   732 		    
       
   733 	    if(!found)    
       
   734 	        {
       
   735 	        return EFalse;
       
   736 	        }
       
   737 		}
       
   738 	return(ETrue);
       
   739 	}
       
   740 
       
   741 // This was originally created as the database files were going to reside in
       
   742 // the SQLite private directory. However, the file is now to be stored in the
       
   743 // contacts model private directory like the old DBMS file. So this is now a
       
   744 // bit redundant and perhaps could be removed at some point.
       
   745 _LIT(KSqlServerPrivateDir, "\\private\\10003A73\\");
       
   746 
       
   747 EXPORT_C HBufC* GetFullDbName(const TDesC& aDbName)
       
   748 	{
       
   749 	TParse parse;
       
   750 	parse.Set(aDbName, &KSqlServerPrivateDir(), 0);
       
   751     return parse.FullName().Alloc();    
       
   752 	}
       
   753 
       
   754 //
       
   755 // CVCardTestStore
       
   756 //
       
   757 
       
   758 EXPORT_C CVCardTestStore* CVCardTestStore::NewLC(TVCardStoreType aType, RFs *aFs, TInt aFileNum)
       
   759 	{
       
   760 	CVCardTestStore* store=CVCardTestStore::NewL(aType,aFs,aFileNum);
       
   761 	CleanupStack::PushL(store);
       
   762 	return(store);
       
   763 	}
       
   764 
       
   765 EXPORT_C CVCardTestStore* CVCardTestStore::NewL(TVCardStoreType aType, RFs *aFs, TInt aFileNum)
       
   766 	{
       
   767 	CVCardTestStore* store=new(ELeave) CVCardTestStore();
       
   768 	store->ConstructL(aType,aFs, aFileNum);
       
   769 	return(store);
       
   770 	}
       
   771 
       
   772 EXPORT_C CVCardTestStore::~CVCardTestStore()
       
   773 	{
       
   774 	delete iBufStore;
       
   775 	delete iFileStore;
       
   776 	}
       
   777 
       
   778 EXPORT_C void CVCardTestStore::ConstructL(TVCardStoreType aType, RFs *aFs, TInt aFileNum)
       
   779 	{
       
   780 	iType=aType;
       
   781 	switch(iType)
       
   782 		{
       
   783 		case KVCardStoreTypeBuf:
       
   784 			iBufStore=CBufStore::NewL(256);
       
   785 			break;
       
   786 		case KVCardStoreTypeFile:
       
   787 			{
       
   788 			aFs->MkDirAll(_L("c:\\vcards\\"));
       
   789 			TFileName fileName;
       
   790 			fileName.Format(_L("c:\\vcards\\VCard%04d"),aFileNum);
       
   791 			iFileStore=CDirectFileStore::ReplaceL(*aFs,fileName,EFileWrite);
       
   792 			iFileStore->SetTypeL(KDirectFileStoreLayoutUid);
       
   793 			}
       
   794 			break;
       
   795 		}
       
   796 	}
       
   797 
       
   798 EXPORT_C CStreamStore *CVCardTestStore::Store() const
       
   799 	{
       
   800 	CStreamStore *store=NULL;
       
   801 	switch(iType)
       
   802 		{
       
   803 		case KVCardStoreTypeBuf:
       
   804 			store=iBufStore;
       
   805 			break;
       
   806 		case KVCardStoreTypeFile:
       
   807 			store=iFileStore;
       
   808 			break;
       
   809 		}
       
   810 	return(store);
       
   811 	}
       
   812 
       
   813 //
       
   814 
       
   815 //
       
   816 // TCntPerfTimer
       
   817 //
       
   818 
       
   819 EXPORT_C TCntPerfTimer::TCntPerfTimer()
       
   820 	: iTickCount1(0), iTickCount2(0) 
       
   821 	{
       
   822 	}
       
   823 
       
   824 EXPORT_C void TCntPerfTimer::StartTimer()
       
   825 	{
       
   826 	iTickCount1 = User::NTickCount();
       
   827 	}
       
   828 
       
   829 EXPORT_C void TCntPerfTimer::StopTimer()
       
   830 	{
       
   831 	iTickCount2 = User::NTickCount();
       
   832 	}
       
   833 
       
   834 EXPORT_C void TCntPerfTimer::ResetTimer()
       
   835 	{
       
   836 	iTickCount1 = 0;
       
   837 	iTickCount2 = 0; 
       
   838 	}
       
   839 
       
   840 EXPORT_C TUint TCntPerfTimer::Result()
       
   841 	{
       
   842 	TInt tickPeriodMicroSeconds;
       
   843 	HAL::Get(HAL::ENanoTickPeriod, tickPeriodMicroSeconds);
       
   844 	TUint result(0);
       
   845 	if (iTickCount1 > 0 && iTickCount2 > iTickCount1)
       
   846 		{
       
   847  		result = (iTickCount2 - iTickCount1) * tickPeriodMicroSeconds;
       
   848 		}
       
   849 	return result;
       
   850 	}
       
   851 
       
   852 
       
   853 EXPORT_C TBool DbShouldBeInDamagedState(CContactDatabase* aDb)
       
   854 	{
       
   855 	TBool isDamaged(aDb->IsDamaged());
       
   856 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   857 	return isDamaged;
       
   858 #else
       
   859 	return ETrue;
       
   860 #endif
       
   861 	}
       
   862 
       
   863 EXPORT_C CContactItem* ReadContactFromDamagedDbLC(CContactDatabase* aDb, TContactItemId aContactId)
       
   864 	{
       
   865 	CContactItem* item(aDb->ReadContactLC(aContactId) );
       
   866 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   867 	return item;
       
   868 #else
       
   869 	User::Leave(KErrNotReady);
       
   870 	return NULL;
       
   871 #endif
       
   872 	}