summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/dbcreator.cpp
blob: b52fe8ea23f29c9f36ff9eca8bea10e014b55113 (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
/*
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* 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: 
* Summary:
* A class for creating a CContactDatabase object with random data.
* The contact items take the form of the following example:
* [KUidContactFieldFamilyName]	Fasiokljno	<5-10 chars>
* [KUidContactFieldGivenName]		Liasidwq	<5-10 chars>
* [KUidContactFieldCompanyName]	Xwjweiohj	<5-10 chars>	
* [KUidContactFieldPhoneNumber]	9874234891	<10 digits>
* Usage example:
*
*/


#include "dbcreator.h"

//////////////////////////////////////////////////////////////////////////////////////////////////
// DbCreator methods
//////////////////////////////////////////////////////////////////////////////////////////////////

CContactDatabase* DbCreator::CreateDbL(const TDesC &aFileName, TInt aNumContacts)
	{
	CContactDatabase* db = CContactDatabase::ReplaceL(aFileName);
	CleanupStack::PushL(db);
	PopulateDatabaseL(*db, aNumContacts);
	CleanupStack::Pop();
	return db;
	}


TUint DbCreator::RandomNum(TUint aMin, TUint aMax)
    {
	TInt64 seed(Math::Random());
	return (static_cast<TUint>(Math::Rand(seed)) % ((aMax - aMin) + 1)) + aMin;
	}


TChar DbCreator::RandomAlphaChar(TBool aIsUpper = EFalse)
    {
	const TInt KUppercaseA(65);
	const TInt KLowercaseA(97);
	const TInt KZOffset(25);
	TInt min = aIsUpper ? KUppercaseA : KLowercaseA;
	TInt max = min + KZOffset;
	return TChar(RandomNum(min, max));
	}


HBufC* DbCreator::RandomNameL()
    {
	const TInt KNameLen( RandomNum(5, 10) );
	HBufC* name = HBufC::NewLC(KNameLen);
	TPtr namePtr = name->Des();
	for (TInt i = 0; i < KNameLen; ++i)
		{
		namePtr.Append(RandomAlphaChar(i == 0));		
		}
	CleanupStack::Pop(name);
	return name;
	}


HBufC* DbCreator::RandomPhoneNumL()
    {
	const TInt KAsciiZero(48);
	const TInt KTenDigits(10);
	HBufC* num = HBufC::NewLC(KTenDigits);
	TPtr numPtr = num->Des();
	for (TInt i = 0; i < KTenDigits; ++i)
		{
		numPtr.Append( TChar(RandomNum(KAsciiZero, KAsciiZero + 9) ) );		
		}
	CleanupStack::Pop(num);
	return num;
	}


void DbCreator::PopulateDatabaseL(CContactDatabase& aDb, TInt aNumContacts)
    {
	for(TInt i = 0; i < aNumContacts; ++i)
		{
		CContactCard* contact = CContactCard::NewLC();

		// first name
		HBufC* fnameBuf = RandomNameL();
		CleanupStack::PushL(fnameBuf);
		CContactItemField* fname = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
		TPtr fnamePtr = fnameBuf->Des();
		fname->TextStorage()->SetTextL(fnamePtr);
		contact->AddFieldL(*fname);
		CleanupStack::Pop(fname);
		CleanupStack::PopAndDestroy(fnameBuf);
		
		// last name
		HBufC* lnameBuf = RandomNameL();
		CleanupStack::PushL(lnameBuf);
		CContactItemField* lname = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
		TPtr lnamePtr = lnameBuf->Des();
		lname->TextStorage()->SetTextL(lnamePtr);
		contact->AddFieldL(*lname);
		CleanupStack::Pop(lname);
		CleanupStack::PopAndDestroy(lnameBuf);

		// company name
		HBufC* cnameBuf = RandomNameL();
		CleanupStack::PushL(cnameBuf);
		CContactItemField* cname = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldCompanyName);
		TPtr cnamePtr = cnameBuf->Des();
		cname->TextStorage()->SetTextL(cnamePtr);
		contact->AddFieldL(*cname);
		CleanupStack::Pop(cname);
		CleanupStack::PopAndDestroy(cnameBuf);

		// phone number
		HBufC* numBuf = RandomPhoneNumL();
		CleanupStack::PushL(numBuf);
		CContactItemField* num = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPhoneNumber);
		TPtr numPtr = numBuf->Des();
		num->TextStorage()->SetTextL(numPtr);
		contact->AddFieldL(*num);
		CleanupStack::Pop(num);
		CleanupStack::PopAndDestroy(numBuf);

		aDb.AddNewContactL(*contact);
		CleanupStack::PopAndDestroy(contact);
		}
	}