summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntsrv/src/cntspeeddials.cpp
blob: 6bcc2ef60ab2687e23366cfae23779e6de507af7 (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
/*
* Copyright (c) 1997-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 "cntspeeddials.h"
#include <s32stor.h>
#include <s32file.h>
#include <collate.h>


const TInt KCntSpeedDialGranularity = 1;


CCntServerSpeedDialManager::CCntServerSpeedDialManager() : iSpeedDials(KCntSpeedDialGranularity)
	{
	}

CCntServerSpeedDialManager::~CCntServerSpeedDialManager()
	{
	iSpeedDials.ResetAndDestroy();
	iSpeedDials.Close();
	}

// returns the speeddial table for the database or if it doesn't exist creates a table
CCntServerSpeedDialTable& CCntServerSpeedDialManager::TableL(const TDesC& aDatabase)
	{
	TInt count = iSpeedDials.Count();
	TInt index = KErrNotFound;
	for (TInt i=0; i<count; i++)
		{
		CCntServerSpeedDialTable* table = iSpeedDials[i]; 
		if (FileNamesIdentical(aDatabase,table->Database()))
			{
			index = i;
			break;
			}
		}
	if (index==KErrNotFound) // the table doesn't exist yet
		{
		CCntServerSpeedDialTable* newTable = CCntServerSpeedDialTable::NewL(aDatabase);
		CleanupStack::PushL(newTable);
		User::LeaveIfError(iSpeedDials.Append(newTable));
		CleanupStack::Pop(newTable);
		index = count;
		}
	return *(iSpeedDials[index]);
	}

TBool CCntServerSpeedDialManager::DeleteTable(const TDesC& aDatabase)
	{
	const TInt count = iSpeedDials.Count();
	for (TInt i = 0; i < count; i++)
		{
		CCntServerSpeedDialTable* table = iSpeedDials[i]; 
		if (FileNamesIdentical(aDatabase,table->Database()))
			{
			iSpeedDials.Remove(i);
			delete table;
			// table has been deleted, cntmodel.ini file needs saving
			return ETrue;
			}
		}

	// no change
	return EFalse;
	}
void CCntServerSpeedDialManager::StoreL(CDictionaryFileStore& aStore) const
	{
	const TInt count = iSpeedDials.Count();
	if (count)
		{
		RDictionaryWriteStream stream;
		stream.AssignLC(aStore, KUidCntSpeedDialStream);
		stream.WriteInt32L(iSpeedDials.Count()); 
		for(TInt i=0; i<count; i++)
			{
			stream << *iSpeedDials[i];
			}
		stream.CommitL();
		CleanupStack::PopAndDestroy(); // stream
		}
	}

void CCntServerSpeedDialManager::RestoreL(CDictionaryFileStore& aStore)
	{
	if	(aStore.IsPresentL(KUidCntSpeedDialStream))
		{
		RDictionaryReadStream stream;
		stream.OpenLC(aStore, KUidCntSpeedDialStream);
		TInt count = stream.ReadInt32L();
		if (count <0)
			{
			User::LeaveIfError(KErrCorrupt);
			}
		iSpeedDials.ResetAndDestroy();
		TInt error(KErrNone) ;
		for(TInt i=0; i<count && (error == KErrNone); i++)
			{
			CCntServerSpeedDialTable* table = CCntServerSpeedDialTable::NewL(KNullDesC);
			CleanupStack::PushL(table);
			stream >> *table;
			error = iSpeedDials.Append(table);
			CleanupStack::Pop(table);
			}
		User::LeaveIfError(error);
		CleanupStack::PopAndDestroy(); // stream
		}
	}
	

CCntServerSpeedDialTable::CSpeedDial::CSpeedDial()
	{ 
	Reset();
	}
	
CCntServerSpeedDialTable::CSpeedDial::CSpeedDial(TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber) 
: iContactId(aContactId)
	{
	TPtr ptrNum = iPhoneNumber->Des();
	ptrNum = aPhoneNumber;
	}


CCntServerSpeedDialTable::CSpeedDial* CCntServerSpeedDialTable::CSpeedDial::NewL(TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber) 
	{
	CCntServerSpeedDialTable::CSpeedDial* self = new (ELeave) CSpeedDial(aContactId, aPhoneNumber);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
	}


void CCntServerSpeedDialTable::CSpeedDial::ConstructL()
	{
	iPhoneNumber = HBufC::NewL(KSpeedDialPhoneLength);
	}


void CCntServerSpeedDialTable::CSpeedDial::PhoneNumber(TSpeedDialPhoneNumber& aSpeedNumber) const 
	{
	if (iPhoneNumber == NULL)
		{
		aSpeedNumber.Zero();
		return;
		}
	TPtrC ptrNum = iPhoneNumber->Des();
	aSpeedNumber = ptrNum; 
	}

	
CCntServerSpeedDialTable::CSpeedDial::~CSpeedDial()
	{
	if (iPhoneNumber != NULL)
		{
		delete iPhoneNumber;
		}
	}

void CCntServerSpeedDialTable::CSpeedDial::InternalizeL(RReadStream& aStream)
	{
	iContactId = aStream.ReadInt32L();
	if (iPhoneNumber != NULL)
		{
		delete iPhoneNumber;
		iPhoneNumber = NULL;
		}
	iPhoneNumber = HBufC::NewL(aStream, KSpeedDialPhoneLength);
	}

void CCntServerSpeedDialTable::CSpeedDial::ExternalizeL(RWriteStream& aStream) const
	{
	aStream.WriteInt32L(iContactId);
	if (iPhoneNumber != NULL)
		{
		aStream << *iPhoneNumber;
		}
	else
		{
		aStream << KNullDesC;
		}
	}

void CCntServerSpeedDialTable::CSpeedDial::Reset()
	{
	iContactId = KNullContactId;
	if (iPhoneNumber != NULL)
		{
		delete iPhoneNumber;
		iPhoneNumber = NULL;
		}
	}

void CCntServerSpeedDialTable::CSpeedDial::SetL(TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber)
	{
	iContactId = aContactId;
	if (iPhoneNumber != NULL)
		{
		delete iPhoneNumber;
		iPhoneNumber = NULL;
		}
	iPhoneNumber = HBufC::NewL(KSpeedDialPhoneLength);
	TPtr phoneNum = iPhoneNumber->Des();
	phoneNum = aPhoneNumber;
	}

CCntServerSpeedDialTable* CCntServerSpeedDialTable::NewL(const TDesC& aDatabase)
	{
	CCntServerSpeedDialTable* self = new (ELeave) CCntServerSpeedDialTable();
	CleanupStack::PushL(self);
	self->ConstructL(aDatabase);
	CleanupStack::Pop(self);
	return self;
	}

CCntServerSpeedDialTable::~CCntServerSpeedDialTable()
	{
	delete iDatabaseFile;
	}

CCntServerSpeedDialTable::CCntServerSpeedDialTable()
	{
	}

void CCntServerSpeedDialTable::ConstructL(const TDesC& aDatabase)
	{
	iDatabaseFile = aDatabase.AllocL();
	}

TContactItemId CCntServerSpeedDialTable::SpeedDialContactItem(TInt aIndex, TDes& aPhoneNumber) const
	{
//	__ASSERT_ALWAYS(aIndex >= KCntMinSpeedDialIndex && aIndex <= KCntMaxSpeedDialIndex, User::Panic(KErrArgument));
	const CSpeedDial& item = iSpeedDialTable.At(aIndex - 1);
	TSpeedDialPhoneNumber speedNumber;
	item.PhoneNumber(speedNumber);
	aPhoneNumber = speedNumber;
	return item.ContactId();
	}

CArrayFix<TInt>* CCntServerSpeedDialTable::SpeedDialIndicesForContactIdLC(TContactItemId aContactId)
	{
	CArrayFixFlat<TInt>* indices = new(ELeave) CArrayFixFlat<TInt>(KCntMaxSpeedDialIndex);
	CleanupStack::PushL(indices);

	const TInt count = iSpeedDialTable.Count();
	for(TInt i=0; i<count; i++)
		{
		const CSpeedDial& item = iSpeedDialTable.At(i);
		if	(item.ContactId() == aContactId)
			indices->AppendL(i+1); // +1 to map onto the contacts speed dial indices of 1-9
		}
	return indices;
	}

void CCntServerSpeedDialTable::SetSpeedDialL(TInt aIndex, TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber)
	{
//	__ASSERT_ALWAYS(aIndex >= KCntMinSpeedDialIndex && aIndex <= KCntMaxSpeedDialIndex, CntServerUtils::Panic(ECntServerPanicSpeedDialIndexOutOfRange));
	CSpeedDial& item = iSpeedDialTable.At(aIndex - 1);
	item.SetL(aContactId, aPhoneNumber);	
	}

void CCntServerSpeedDialTable::InternalizeL(RReadStream& aStream)
	{
	HBufC* file = HBufC::NewLC(aStream, KMaxTInt);
	delete iDatabaseFile;
	iDatabaseFile = file;
	CleanupStack::Pop(file); 
	const TInt count = iSpeedDialTable.Count();
	for(TInt i=0; i<count; i++)
		{
		CSpeedDial& tableItem = iSpeedDialTable.At(i);
		tableItem.InternalizeL(aStream);
		}
	}

void CCntServerSpeedDialTable::ExternalizeL(RWriteStream& aStream) const
	{
	aStream << *iDatabaseFile;
	const TInt count = iSpeedDialTable.Count();
	for(TInt i=0; i<count; i++)
		{
		aStream << iSpeedDialTable.At(i);
		}
	}

TUid CCntServerSpeedDialManager::SpeedDialFieldUidFromSpeedDialPosition(TInt aSpeedDialPosition)
//
//	Static utility method to map a speed dial index onto a field uid.
//
	{
//	__ASSERT_ALWAYS(aSpeedDialPosition >= KCntMinSpeedDialIndex && aSpeedDialPosition <= KCntMaxSpeedDialIndex, Panic(ECntPanicInvalidSpeedDialIndex));

	TUid fieldTypeUid = KNullUid;
	switch (aSpeedDialPosition)
		{
	case 1:
		fieldTypeUid = KUidSpeedDialOne;
		break;
	case 2:
		fieldTypeUid = KUidSpeedDialTwo;
		break;
	case 3:
		fieldTypeUid = KUidSpeedDialThree;
		break;
	case 4:
		fieldTypeUid = KUidSpeedDialFour;
		break;
	case 5:
		fieldTypeUid = KUidSpeedDialFive;
		break;
	case 6:
		fieldTypeUid = KUidSpeedDialSix;
		break;
	case 7:
		fieldTypeUid = KUidSpeedDialSeven;
		break;
	case 8:
		fieldTypeUid = KUidSpeedDialEight;
		break;
	case 9:
		fieldTypeUid = KUidSpeedDialNine;
		break;
		}

	return fieldTypeUid; 
	}