|
1 // Copyright (c) 2008-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 // Summary: |
|
15 // A class for creating a CContactDatabase object with random data. |
|
16 // The contact items take the form of the following example: |
|
17 // [KUidContactFieldFamilyName] Fasiokljno <5-10 chars> |
|
18 // [KUidContactFieldGivenName] Liasidwq <5-10 chars> |
|
19 // [KUidContactFieldCompanyName] Xwjweiohj <5-10 chars> |
|
20 // [KUidContactFieldPhoneNumber] 9874234891 <10 digits> |
|
21 // Usage example: |
|
22 // |
|
23 |
|
24 // #include "dbcreator.h" |
|
25 // void MyFunctionL() |
|
26 // // create a database populated with 500 randomly created contact items |
|
27 // const TInt KNumContacts(500); |
|
28 // _LIT(KDbName, "C:mydb.cbd"); |
|
29 // CContactDatabase* db = DbCreator::CreateDbL(KDbName, KNumContacts); |
|
30 // CleanupStack::PushL(db); |
|
31 // // do something interesting... |
|
32 // CleanupStack::PopAndDestroy(db); |
|
33 // CContactDatabase::DeleteDatabaseL(KDbName); |
|
34 // Written by: |
|
35 // James Clarke |
|
36 // |
|
37 |
|
38 #include "dbcreator.h" |
|
39 |
|
40 // |
|
41 // DbCreator methods |
|
42 // |
|
43 |
|
44 CContactDatabase* DbCreator::CreateDbL(const TDesC &aFileName, TInt aNumContacts) |
|
45 { |
|
46 CContactDatabase* db = CContactDatabase::ReplaceL(aFileName); |
|
47 CleanupStack::PushL(db); |
|
48 PopulateDatabaseL(*db, aNumContacts); |
|
49 CleanupStack::Pop(); |
|
50 return db; |
|
51 } |
|
52 |
|
53 |
|
54 TUint DbCreator::RandomNum(TUint aMin, TUint aMax) |
|
55 { |
|
56 TInt64 seed(Math::Random()); |
|
57 return (static_cast<TUint>(Math::Rand(seed)) % ((aMax - aMin) + 1)) + aMin; |
|
58 } |
|
59 |
|
60 |
|
61 TChar DbCreator::RandomAlphaChar(TBool aIsUpper = EFalse) |
|
62 { |
|
63 const TInt KUppercaseA(65); |
|
64 const TInt KLowercaseA(97); |
|
65 const TInt KZOffset(25); |
|
66 TInt min = aIsUpper ? KUppercaseA : KLowercaseA; |
|
67 TInt max = min + KZOffset; |
|
68 return TChar(RandomNum(min, max)); |
|
69 } |
|
70 |
|
71 |
|
72 HBufC* DbCreator::RandomNameL() |
|
73 { |
|
74 const TInt KNameLen( RandomNum(5, 10) ); |
|
75 HBufC* name = HBufC::NewLC(KNameLen); |
|
76 TPtr namePtr = name->Des(); |
|
77 for (TInt i = 0; i < KNameLen; ++i) |
|
78 { |
|
79 namePtr.Append(RandomAlphaChar(i == 0)); |
|
80 } |
|
81 CleanupStack::Pop(name); |
|
82 return name; |
|
83 } |
|
84 |
|
85 |
|
86 HBufC* DbCreator::RandomPhoneNumL() |
|
87 { |
|
88 const TInt KAsciiZero(48); |
|
89 const TInt KTenDigits(10); |
|
90 HBufC* num = HBufC::NewLC(KTenDigits); |
|
91 TPtr numPtr = num->Des(); |
|
92 for (TInt i = 0; i < KTenDigits; ++i) |
|
93 { |
|
94 numPtr.Append( TChar(RandomNum(KAsciiZero, KAsciiZero + 9) ) ); |
|
95 } |
|
96 CleanupStack::Pop(num); |
|
97 return num; |
|
98 } |
|
99 |
|
100 |
|
101 void DbCreator::PopulateDatabaseL(CContactDatabase& aDb, TInt aNumContacts) |
|
102 { |
|
103 for(TInt i = 0; i < aNumContacts; ++i) |
|
104 { |
|
105 CContactCard* contact = CContactCard::NewLC(); |
|
106 |
|
107 // first name |
|
108 HBufC* fnameBuf = RandomNameL(); |
|
109 CleanupStack::PushL(fnameBuf); |
|
110 CContactItemField* fname = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName); |
|
111 TPtr fnamePtr = fnameBuf->Des(); |
|
112 fname->TextStorage()->SetTextL(fnamePtr); |
|
113 contact->AddFieldL(*fname); |
|
114 CleanupStack::Pop(fname); |
|
115 CleanupStack::PopAndDestroy(fnameBuf); |
|
116 |
|
117 // last name |
|
118 HBufC* lnameBuf = RandomNameL(); |
|
119 CleanupStack::PushL(lnameBuf); |
|
120 CContactItemField* lname = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName); |
|
121 TPtr lnamePtr = lnameBuf->Des(); |
|
122 lname->TextStorage()->SetTextL(lnamePtr); |
|
123 contact->AddFieldL(*lname); |
|
124 CleanupStack::Pop(lname); |
|
125 CleanupStack::PopAndDestroy(lnameBuf); |
|
126 |
|
127 // company name |
|
128 HBufC* cnameBuf = RandomNameL(); |
|
129 CleanupStack::PushL(cnameBuf); |
|
130 CContactItemField* cname = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldCompanyName); |
|
131 TPtr cnamePtr = cnameBuf->Des(); |
|
132 cname->TextStorage()->SetTextL(cnamePtr); |
|
133 contact->AddFieldL(*cname); |
|
134 CleanupStack::Pop(cname); |
|
135 CleanupStack::PopAndDestroy(cnameBuf); |
|
136 |
|
137 // phone number |
|
138 HBufC* numBuf = RandomPhoneNumL(); |
|
139 CleanupStack::PushL(numBuf); |
|
140 CContactItemField* num = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPhoneNumber); |
|
141 TPtr numPtr = numBuf->Des(); |
|
142 num->TextStorage()->SetTextL(numPtr); |
|
143 contact->AddFieldL(*num); |
|
144 CleanupStack::Pop(num); |
|
145 CleanupStack::PopAndDestroy(numBuf); |
|
146 |
|
147 aDb.AddNewContactL(*contact); |
|
148 CleanupStack::PopAndDestroy(contact); |
|
149 } |
|
150 } |
|
151 |