summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/integration/testimpexvcard/src/testgenericimportstep.cpp
blob: b260808f7d9eda112cf46ca29ff846341694a97d (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
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* 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 "testgenericimportstep.h"
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
#include "cntdb_internal.h"
#endif

CTestGenericImportStep::~CTestGenericImportStep()
/**
 * Destructor
 */
	{
	}

CTestGenericImportStep::CTestGenericImportStep()
/**
 * Constructor
 */
	{
	// **MUST** call SetTestStepName in the constructor as the controlling
	// framework uses the test step name immediately following construction to set
	// up the step's unique logging ID.
	SetTestStepName(KTestGenericImportStep);
	}

TVerdict CTestGenericImportStep::doTestStepPreambleL()
/**
 * @return - TVerdict code
 * Override of base class virtual
 */
	{
	INFO_PRINTF1(_L("Start Import of vCards"));
	iScheduler = new (ELeave) CActiveScheduler;

	CActiveScheduler::Install(iScheduler);
	SetTestStepResult(EPass);
	return TestStepResult();
	}

TVerdict CTestGenericImportStep::doTestStepL()
/**
 * @return - TVerdict code
 * Override of base class pure virtual
 */
	{
	ImportContactsL();
	return TestStepResult();
	}

TVerdict CTestGenericImportStep::doTestStepPostambleL()
/**
 * @return - TVerdict code
 * Override of base class virtual
 */
	{
	CActiveScheduler::Install(NULL);
	delete iScheduler;
	iScheduler = NULL;
	INFO_PRINTF1(_L("Completed Import of vCards "));
	return TestStepResult();
	}

void CTestGenericImportStep::ImportContactsL()
	{
	// Retreive the file name to be Imported
   	RFs fsSession;
	RFileReadStream readStream;
	CleanupClosePushL(readStream);
	
	// connect to file system
	User::LeaveIfError(fsSession.Connect());
	CleanupClosePushL(fsSession);
	
	GetInputFromIni();
	
	// Open the the file from which the contact data has to be imported
   	User::LeaveIfError(readStream.Open(fsSession, iImportFile, EFileRead));
   	
   	CContactDatabase *dBase = NULL;
   	CArrayPtr<CContactItem>* item = NULL;

	dBase = CContactDatabase::ReplaceL(iDatabaseFile);
	CleanupStack::PushL(dBase);

	INFO_PRINTF1(_L("Importing Contact....."));
	TBool success = EFalse;
	
	// Importing contact item into vCard format
	
	if ( iOOM != KNullDesC )
		{
		TInt tryCount = 1;
 		TInt err = KErrNone;
 		for ( ;; )
 			{
 			__UHEAP_SETFAIL(RHeap::EDeterministic, tryCount);
 			TRAP(err, item = dBase->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), readStream, success, CContactDatabase::EDefault));
 			if ( err==KErrNone )
 				{
 				__UHEAP_RESET;
 				INFO_PRINTF1(_L("OOM testing of CContactDatabase::ImportContactsL Api is done"));
 				INFO_PRINTF2(_L("Total number of contacts imported is %d "), item->Count());
 				break;
 				}
 				
 			if ( err != KErrNoMemory )
 				{
 				__UHEAP_RESET;
 				SetTestStepResult(EFail);
 				break;
 				}
 			tryCount++;
 			}
 			__UHEAP_RESET;
 			dBase->RecoverL();
		}
	else
		{
		if(iEnableImportPBAP)
			{
			// Import with KUidPBAPVCardConvImpl - leaves with KErrNotSupported, as there is no support for PBAP import
			ImportWithPBAP(dBase,readStream,success);
			}
		else
			{
			if(iNumberOfContacts>0)
				{
				TStreamPos pos = 0;
				// Read the contact item one by one into the stream and import 
				for(;;)
					{
					// Importing the contact from readStream
					CArrayPtr<CContactItem>* contactItem = dBase->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), readStream, success, CContactDatabase::EImportSingleContact);
					
					TRAPD(completionState,pos = readStream.Source()->TellL(MStreamBuf::ERead));
					if(completionState!=KErrNone || dBase->CountL() >= iNumberOfContacts)
						{
						break;	
						}
					contactItem->ResetAndDestroy();
					}
				}
			else
				{
				item = dBase->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), readStream, success, CContactDatabase::EDefault);
				}	
			}
		}

	INFO_PRINTF2(_L("Total number of contacts imported is %d "), dBase->CountL());
	
	delete item;
	readStream.Close();
    
    // Cleanup
    CleanupStack::PopAndDestroy(dBase);
    CleanupStack::PopAndDestroy(&fsSession);
    CleanupStack::PopAndDestroy(&readStream);
    fsSession.Close();
	}

// Gets the input from ini file	
void::CTestGenericImportStep::GetInputFromIni()
	{
	iEnableImportPBAP = EFalse;
	GetStringFromConfig(ConfigSection(), KImportFile, iImportFile);
	// Database to Replace
	GetStringFromConfig(ConfigSection(), KDatabaseFile, iDatabaseFile);
	// Number of contacts to import
	GetIntFromConfig(ConfigSection(), KNumberOfContacts, iNumberOfContacts);
	GetStringFromConfig(ConfigSection(),KOOM,iOOM);	
	GetBoolFromConfig(ConfigSection(),KEnableImportPBAP,iEnableImportPBAP);	
	}

/** Import with KUidPBAPVCardConvImpl, which leaves with KErrNotSupported
@param	aDBase The database to import
@param	aReadStream Import stream
@param  aSuccess states the success of import on return
*/
void CTestGenericImportStep::ImportWithPBAP(CContactDatabase* aDBase, RFileReadStream& aReadStream, TBool& aSuccess)
	{
	CArrayPtr<CContactItem>* item = NULL;
	TRAPD(err, item = aDBase->ImportContactsL(TUid::Uid(KUidPBAPVCardConvImpl),aReadStream, aSuccess, CContactDatabase::EImportSingleContact));		
	
	if(err == KErrNotSupported )
		{
		SetTestStepResult(EPass);
		}
	else
		{
		item->ResetAndDestroy();
		SetTestStepResult(EFail);
		SetTestStepError(err);
		}
	}