summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/integration/testimpexvcard/src/testimpexvcardsuitestepbase.cpp
blob: 766ddd8c39ba1f803b2c1a449a698dc1988aa20c (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (c) 2005-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: 
*
*/


#include "testimpexvcardsuitestepbase.h"
#include "testimpexvcardsuitedefs.h"

CTestImpExvCardSuiteStepBase::~CTestImpExvCardSuiteStepBase()
	{
	}

CTestImpExvCardSuiteStepBase::CTestImpExvCardSuiteStepBase()
	{
	}

void CTestImpExvCardSuiteStepBase::OpenVCFAndImportItemL(TBuf<80> aPath, RFs aFsSession, TInt aIndex)
	{
	User::LeaveIfError(aFsSession.Connect());
	CleanupClosePushL(aFsSession);
	RFileReadStream readVCFStream;
	
	// Format buf to hold relevant vcf file
	TBuf<100> buf;
	buf.Zero();
#ifdef __WINS__	
    buf.Append(_L("c:"));
#else
    TFileName processFileName = RProcess().FileName();
    TParsePtrC parse(processFileName);
    buf.Append(parse.Drive());
#endif 
	buf.Append(aPath);
	buf.AppendNum(aIndex);
	buf.Append(_L(".vcf"));
	
	// Open vcf file
	User::LeaveIfError(readVCFStream.Open(aFsSession, buf, EFileRead));
	
	TBuf<MAX_TEXT_MESSAGE> message;
	message.Format(KOpenVCFFile, &buf);
	INFO_PRINTF1(message);
	
	CleanupClosePushL(readVCFStream);
	
	// Imports the vCard
	ImportCardL(readVCFStream);
	
	CleanupStack::PopAndDestroy(); // pop readVCFStream
	aFsSession.Close();
	CleanupStack::PopAndDestroy(); // pop aFsSession
	}
	
void CTestImpExvCardSuiteStepBase::ImportCardL(RReadStream& aReadStream)
	{
	INFO_PRINTF1(_L("Importing vCard....."));
	
	CVCardToContactsAppConverter* converter=new(ELeave)	CVCardToContactsAppConverter;
	CleanupStack::PushL(converter); // converter

	CParserVCard* vCard= CParserVCard::NewL();
	CleanupStack::PushL(vCard);
	TInt err = KErrNone;
	TRAP(err, vCard->InternalizeL(aReadStream));
	User::LeaveIfError(err);
	
	CContactItem* contactItem = NULL;
		
	// the CContactVCardConverter::ELocalTime must be used, so the program expects a local time
	contactItem = converter->GetVCardAsContactItemLC(*vCard, CVCardToContactsAppConverter::EPreserveAllProperties, 0);
	
	// the time would be automatically converted to UTC by the production codes
	iTimeFromImport = (*contactItem).LastModified();
	
	CleanupStack::PopAndDestroy(3); // GetVCardAsContactItemLC, vCard & converter
	INFO_PRINTF1(_L("Imported to Contact Entry"));
	}
	
void CTestImpExvCardSuiteStepBase::OpenBDAYVCFAndImportItemL(TBuf<80> aPath,TInt aIndex)
	{
	// Format buf to hold relevant vcf file
	TFileName fileName;
#ifdef __WINS__
	fileName.Append(_L("c:"));
#else
	TFileName processFileName = RProcess().FileName();
	TParsePtrC parse(processFileName);
	fileName.Append(parse.Drive());
#endif
	fileName.Append(aPath);
	fileName.AppendNum(aIndex);
	fileName.Append(_L(".vcf"));
	
	RFs fs; 
	User::LeaveIfError(fs.Connect());  // push fs
	CleanupClosePushL(fs);
	RFileReadStream readVCFStream;
	User::LeaveIfError(readVCFStream.Open(fs, fileName, EFileRead)); // opens vcf file
	
	TBuf<MAX_TEXT_MESSAGE> message;
	message.Format(KOpenVCFFile, &fileName);
	INFO_PRINTF1(message);
	
	CleanupClosePushL(readVCFStream);   // readVCFStream

	ImportBDAYCardL(readVCFStream);
		
	CleanupStack::PopAndDestroy(); // pop readVCFStream
	fs.Close();
	CleanupStack::PopAndDestroy(); // pop fs
	}
	
void CTestImpExvCardSuiteStepBase::ImportBDAYCardL(RReadStream& aReadStream)
	{
	INFO_PRINTF1(_L("Importing vCard....."));
	

	CVCardToContactsAppConverter* converter=new(ELeave)	CVCardToContactsAppConverter;
	CleanupStack::PushL(converter); // converter

	CParserVCard* vCard= CParserVCard::NewL();
	CleanupStack::PushL(vCard);
	TInt err = KErrNone;
	TRAP(err, vCard->InternalizeL(aReadStream));
	User::LeaveIfError(err);
	
	CContactItem* contactItem = NULL;
	
	// set to local time
	contactItem = converter->GetVCardAsContactItemLC(*vCard, CVCardToContactsAppConverter::EPreserveAllProperties, CContactVCardConverter::ELocalTime);
	
	CContactItemFieldSet& cardFields = contactItem->CardFields();
	
	INFO_PRINTF1(_L("Looking for Birthday field in Contact entry....."));
	TInt pos = cardFields.Find(KUidContactFieldBirthday);
	
	if (pos==KErrNotFound)
		{
		INFO_PRINTF1(_L("Birthday field not found in Contact entry"));
		iBDayFromImport = NULL;
		}
	else
		{
		INFO_PRINTF1(_L("Birthday field found in Contact entry"));
		CContactItemField& itemField = cardFields[pos];
	
		CContactDateField* dateTimeField = itemField.DateTimeStorage();
		CleanupStack::PushL(dateTimeField);
	
		iBDayFromImport = dateTimeField->Time();
		
		CleanupStack::Pop();
		}
	
	INFO_PRINTF1(_L("Imported to Contact Entry"));
		
	CleanupStack::PopAndDestroy(3); 
	}
	
void CTestImpExvCardSuiteStepBase::ExportItemL(TBuf<80> aPath, TBool aAddBDay)
	{
	OpenDBL();
	
	TContactItemId itemId;
	RFs fsSession;
	RFileWriteStream fileStream;

	CContactIdArray* idArray = NULL;
	CContactCard* contactAdded = NULL;
	TTime now;
	
	User::LeaveIfError(fsSession.Connect());
	CleanupClosePushL(fsSession);
	
	idArray = CContactIdArray::NewL();

	CleanupStack::PushL(idArray);   // idArray

	fsSession.MkDirAll(aPath);
	User::LeaveIfError(fileStream.Replace(fsSession, aPath, EFileWrite));

	contactAdded = CContactCard::NewL();

	CleanupStack::PushL(contactAdded);   // contactAdded
	
	contactAdded->IncAccessCount();
	
	// ADD BDAY
	if (aAddBDay)
		{
		CContactItemFieldSet& fieldSet=contactAdded->CardFields();
		const TInt pos=fieldSet.Find(KUidContactFieldBirthday);
	
		if (pos!=KErrNotFound)
			{
			fieldSet[pos].DateTimeStorage()->SetTime(iBDayLocal);
			}
		else
			{
			CContactItemField* field=CContactItemField::NewLC(KStorageTypeDateTime,KUidContactFieldBirthday);
   			field->SetMapping(KUidContactFieldVCardMapBDAY);
			field->DateTimeStorage()->SetTime(iBDayLocal);
			contactAdded->AddFieldL(*field);
	
			CleanupStack::Pop(); // field
			}
		}
	
	SetNameL(*contactAdded, KUidContactFieldGivenName, _L("toby"), ETrue);
	SetNameL(*contactAdded, KUidContactFieldPhoneNumber, _L("6041233214"), ETrue);

	User::After(3000000);

	// There is no need to call SetLastModifed because
	// That function will be call by ExportSelectedContactsL below
	now.UniversalTime();
	iRecordedTime = now;
	
	INFO_PRINTF1(_L("Adding contact to Database....."));
	itemId = iDb->AddNewContactL(*contactAdded);
	INFO_PRINTF1(_L("Contact Added to Database"));
	CleanupStack::PopAndDestroy(contactAdded);  // contactAdded
	contactAdded = NULL;

	idArray->InsertL(0, itemId);

	INFO_PRINTF1(_L("Exporting Contact....."));
	iDb->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), *idArray, fileStream, CContactVCardConverter::EDefault, ETrue);
	INFO_PRINTF1(_L("Exported Contact"));
	fileStream.CommitL();
	fileStream.Close();
	
	CleanupStack::PopAndDestroy(idArray);  // idArray
	fsSession.Close();
	CleanupStack::PopAndDestroy();  // fsSession
	CloseDB();
	}
	
void CTestImpExvCardSuiteStepBase::ImportItemL(TBuf<80> aPath, TBool aImportBDay)
	{
	RFs fs; 
	User::LeaveIfError(fs.Connect());  // push fs
	CleanupClosePushL(fs);
	RFileReadStream readVCFStream;
	
	INFO_PRINTF1(_L("Opening exported vcf file to import, to check REV property"));
	
	User::LeaveIfError(readVCFStream.Open(fs, aPath, EFileRead));
	
	TBuf<MAX_TEXT_MESSAGE> message;
	message.Format(KOpenVCFFile, &KExportRevUTCFile);
	INFO_PRINTF1(message);
	
	CleanupClosePushL(readVCFStream);  // readVCFStream

	if (aImportBDay)
		{
		ImportBDAYCardL(readVCFStream);
		}
	else
		{
		ImportCardL(readVCFStream);
		}

	CleanupStack::PopAndDestroy();  // readVCFStream
	CleanupStack::PopAndDestroy(); // pop fs
	}

void CTestImpExvCardSuiteStepBase::OpenDBL()
	{
	if(iDb == NULL)
		{
		INFO_PRINTF1(_L("Opening Contacts Database....."));
		iDb = CContactDatabase::ReplaceL();
		INFO_PRINTF1(_L("Opened Contacts Database"));
		}
	}
	
void CTestImpExvCardSuiteStepBase::CloseDB()
	{
	delete iDb;
	iDb = NULL;
	}

void CTestImpExvCardSuiteStepBase::SetNameL(CContactItem& aItem,TUid aType,const TDesC& aName, TBool aAddField)
//
// Set the contents of a text field, creating the field if required
//
	{
	CContactItemFieldSet& fieldSet=aItem.CardFields();
	const TInt pos=fieldSet.Find(aType);
	if (!aAddField && pos!=KErrNotFound)
		{
		fieldSet[pos].TextStorage()->SetTextL(aName);
		}
	else
		{
		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,aType);
   		field->SetMapping(KUidContactFieldVCardMapUnusedN);
		field->TextStorage()->SetTextL(aName);
		aItem.AddFieldL(*field);
		CleanupStack::Pop(); // field
		}
	}
	
TTime CTestImpExvCardSuiteStepBase::FormatDateTime(const TDesC& aParamString)
	{
	// Format of buffer to construct a TTime is YYYYMMDD:HHMMSS (15 characters).
	// TTime uses zero-based values for month and day - which is confusing for scripting.
	// In our script, we use actual month and day numbers to make things simpler so we
	// must modify the string here to take account of this.
	TBuf<32> buf;
	TInt m, d;

	buf.Zero();
	buf.Copy(aParamString.Left(4));		// The year
	TLex lexMonth = aParamString.Mid(4, 2);
	lexMonth.Val(m);
	TLex lexDay = aParamString.Mid(6, 2);
	lexDay.Val(d);
	buf.AppendFormat(_L("%02d%02d"), m - 1, d - 1);	// The month and day
	buf.Append(aParamString.Right(7));

	return TTime(buf);
	}
	
TPtrC CTestImpExvCardSuiteStepBase::GetExpectedUTCFromIniL(TInt numberOfCases,TBuf<80> aConfig, TBool aExpectedBDay)
	{
	TPtrC expectedUTC;
	TPtrC expectedBDAY;
	
	TPtrC config = aConfig;
	HBufC *configval = HBufC::NewLC(config.Length()+2);
	
	TPtr configSection = configval->Des();
	
	configSection.Append(config);
	configSection.AppendNum(numberOfCases);
	
	if (aExpectedBDay)
		{
		GetStringFromConfig(configSection,KExpectedBDay,expectedBDAY);
		CleanupStack::PopAndDestroy(configval);
		return expectedBDAY;
		}
	else
		{
		GetStringFromConfig(configSection,KExpectedUTC,expectedUTC);
		CleanupStack::PopAndDestroy(configval);
		return expectedUTC;
		}
	}