phonebookengines/contactsmodel/cntsrv/src/CntSpeedDials.cpp
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 1997-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 //
       
    15 
       
    16 #include "CntSpeedDials.h"
       
    17 #include <s32stor.h>
       
    18 #include <s32file.h>
       
    19 #include <collate.h>
       
    20 
       
    21 
       
    22 const TInt KCntSpeedDialGranularity = 1;
       
    23 
       
    24 
       
    25 CCntServerSpeedDialManager::CCntServerSpeedDialManager() : iSpeedDials(KCntSpeedDialGranularity)
       
    26 	{
       
    27 	}
       
    28 
       
    29 CCntServerSpeedDialManager::~CCntServerSpeedDialManager()
       
    30 	{
       
    31 	iSpeedDials.ResetAndDestroy();
       
    32 	iSpeedDials.Close();
       
    33 	}
       
    34 
       
    35 // returns the speeddial table for the database or if it doesn't exist creates a table
       
    36 CCntServerSpeedDialTable& CCntServerSpeedDialManager::TableL(const TDesC& aDatabase)
       
    37 	{
       
    38 	TInt count = iSpeedDials.Count();
       
    39 	TInt index = KErrNotFound;
       
    40 	for (TInt i=0; i<count; i++)
       
    41 		{
       
    42 		CCntServerSpeedDialTable* table = iSpeedDials[i]; 
       
    43 		if (FileNamesIdentical(aDatabase,table->Database()))
       
    44 			{
       
    45 			index = i;
       
    46 			break;
       
    47 			}
       
    48 		}
       
    49 	if (index==KErrNotFound) // the table doesn't exist yet
       
    50 		{
       
    51 		CCntServerSpeedDialTable* newTable = CCntServerSpeedDialTable::NewL(aDatabase);
       
    52 		CleanupStack::PushL(newTable);
       
    53 		User::LeaveIfError(iSpeedDials.Append(newTable));
       
    54 		CleanupStack::Pop(newTable);
       
    55 		index = count;
       
    56 		}
       
    57 	return *(iSpeedDials[index]);
       
    58 	}
       
    59 
       
    60 TBool CCntServerSpeedDialManager::DeleteTable(const TDesC& aDatabase)
       
    61 	{
       
    62 	const TInt count = iSpeedDials.Count();
       
    63 	for (TInt i = 0; i < count; i++)
       
    64 		{
       
    65 		CCntServerSpeedDialTable* table = iSpeedDials[i]; 
       
    66 		if (FileNamesIdentical(aDatabase,table->Database()))
       
    67 			{
       
    68 			iSpeedDials.Remove(i);
       
    69 			delete table;
       
    70 			// table has been deleted, cntmodel.ini file needs saving
       
    71 			return ETrue;
       
    72 			}
       
    73 		}
       
    74 
       
    75 	// no change
       
    76 	return EFalse;
       
    77 	}
       
    78 void CCntServerSpeedDialManager::StoreL(CDictionaryFileStore& aStore) const
       
    79 	{
       
    80 	const TInt count = iSpeedDials.Count();
       
    81 	if (count)
       
    82 		{
       
    83 		RDictionaryWriteStream stream;
       
    84 		stream.AssignLC(aStore, KUidCntSpeedDialStream);
       
    85 		stream.WriteInt32L(iSpeedDials.Count()); 
       
    86 		for(TInt i=0; i<count; i++)
       
    87 			{
       
    88 			stream << *iSpeedDials[i];
       
    89 			}
       
    90 		stream.CommitL();
       
    91 		CleanupStack::PopAndDestroy(); // stream
       
    92 		}
       
    93 	}
       
    94 
       
    95 void CCntServerSpeedDialManager::RestoreL(CDictionaryFileStore& aStore)
       
    96 	{
       
    97 	if	(aStore.IsPresentL(KUidCntSpeedDialStream))
       
    98 		{
       
    99 		RDictionaryReadStream stream;
       
   100 		stream.OpenLC(aStore, KUidCntSpeedDialStream);
       
   101 		TInt count = stream.ReadInt32L();
       
   102 		if (count <0)
       
   103 			{
       
   104 			User::LeaveIfError(KErrCorrupt);
       
   105 			}
       
   106 		iSpeedDials.ResetAndDestroy();
       
   107 		TInt error(KErrNone) ;
       
   108 		for(TInt i=0; i<count && (error == KErrNone); i++)
       
   109 			{
       
   110 			CCntServerSpeedDialTable* table = CCntServerSpeedDialTable::NewL(KNullDesC);
       
   111 			CleanupStack::PushL(table);
       
   112 			stream >> *table;
       
   113 			error = iSpeedDials.Append(table);
       
   114 			CleanupStack::Pop(table);
       
   115 			}
       
   116 		User::LeaveIfError(error);
       
   117 		CleanupStack::PopAndDestroy(); // stream
       
   118 		}
       
   119 	}
       
   120 	
       
   121 
       
   122 CCntServerSpeedDialTable::CSpeedDial::CSpeedDial()
       
   123 	{ 
       
   124 	Reset();
       
   125 	}
       
   126 	
       
   127 CCntServerSpeedDialTable::CSpeedDial::CSpeedDial(TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber) 
       
   128 : iContactId(aContactId)
       
   129 	{
       
   130 	TPtr ptrNum = iPhoneNumber->Des();
       
   131 	ptrNum = aPhoneNumber;
       
   132 	}
       
   133 
       
   134 
       
   135 CCntServerSpeedDialTable::CSpeedDial* CCntServerSpeedDialTable::CSpeedDial::NewL(TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber) 
       
   136 	{
       
   137 	CCntServerSpeedDialTable::CSpeedDial* self = new (ELeave) CSpeedDial(aContactId, aPhoneNumber);
       
   138 	CleanupStack::PushL(self);
       
   139 	self->ConstructL();
       
   140 	CleanupStack::Pop();
       
   141 	return self;
       
   142 	}
       
   143 
       
   144 
       
   145 void CCntServerSpeedDialTable::CSpeedDial::ConstructL()
       
   146 	{
       
   147 	iPhoneNumber = HBufC::NewL(KSpeedDialPhoneLength);
       
   148 	}
       
   149 
       
   150 
       
   151 void CCntServerSpeedDialTable::CSpeedDial::PhoneNumber(TSpeedDialPhoneNumber& aSpeedNumber) const 
       
   152 	{
       
   153 	if (iPhoneNumber == NULL)
       
   154 		{
       
   155 		aSpeedNumber.Zero();
       
   156 		return;
       
   157 		}
       
   158 	TPtrC ptrNum = iPhoneNumber->Des();
       
   159 	aSpeedNumber = ptrNum; 
       
   160 	}
       
   161 
       
   162 	
       
   163 CCntServerSpeedDialTable::CSpeedDial::~CSpeedDial()
       
   164 	{
       
   165 	if (iPhoneNumber != NULL)
       
   166 		{
       
   167 		delete iPhoneNumber;
       
   168 		}
       
   169 	}
       
   170 
       
   171 void CCntServerSpeedDialTable::CSpeedDial::InternalizeL(RReadStream& aStream)
       
   172 	{
       
   173 	iContactId = aStream.ReadInt32L();
       
   174 	if (iPhoneNumber != NULL)
       
   175 		{
       
   176 		delete iPhoneNumber;
       
   177 		iPhoneNumber = NULL;
       
   178 		}
       
   179 	iPhoneNumber = HBufC::NewL(aStream, KSpeedDialPhoneLength);
       
   180 	}
       
   181 
       
   182 void CCntServerSpeedDialTable::CSpeedDial::ExternalizeL(RWriteStream& aStream) const
       
   183 	{
       
   184 	aStream.WriteInt32L(iContactId);
       
   185 	if (iPhoneNumber != NULL)
       
   186 		{
       
   187 		aStream << *iPhoneNumber;
       
   188 		}
       
   189 	else
       
   190 		{
       
   191 		aStream << KNullDesC;
       
   192 		}
       
   193 	}
       
   194 
       
   195 void CCntServerSpeedDialTable::CSpeedDial::Reset()
       
   196 	{
       
   197 	iContactId = KNullContactId;
       
   198 	if (iPhoneNumber != NULL)
       
   199 		{
       
   200 		delete iPhoneNumber;
       
   201 		iPhoneNumber = NULL;
       
   202 		}
       
   203 	}
       
   204 
       
   205 void CCntServerSpeedDialTable::CSpeedDial::SetL(TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber)
       
   206 	{
       
   207 	iContactId = aContactId;
       
   208 	if (iPhoneNumber != NULL)
       
   209 		{
       
   210 		delete iPhoneNumber;
       
   211 		iPhoneNumber = NULL;
       
   212 		}
       
   213 	iPhoneNumber = HBufC::NewL(KSpeedDialPhoneLength);
       
   214 	TPtr phoneNum = iPhoneNumber->Des();
       
   215 	phoneNum = aPhoneNumber;
       
   216 	}
       
   217 
       
   218 CCntServerSpeedDialTable* CCntServerSpeedDialTable::NewL(const TDesC& aDatabase)
       
   219 	{
       
   220 	CCntServerSpeedDialTable* self = new (ELeave) CCntServerSpeedDialTable();
       
   221 	CleanupStack::PushL(self);
       
   222 	self->ConstructL(aDatabase);
       
   223 	CleanupStack::Pop(self);
       
   224 	return self;
       
   225 	}
       
   226 
       
   227 CCntServerSpeedDialTable::~CCntServerSpeedDialTable()
       
   228 	{
       
   229 	delete iDatabaseFile;
       
   230 	}
       
   231 
       
   232 CCntServerSpeedDialTable::CCntServerSpeedDialTable()
       
   233 	{
       
   234 	}
       
   235 
       
   236 void CCntServerSpeedDialTable::ConstructL(const TDesC& aDatabase)
       
   237 	{
       
   238 	iDatabaseFile = aDatabase.AllocL();
       
   239 	}
       
   240 
       
   241 TContactItemId CCntServerSpeedDialTable::SpeedDialContactItem(TInt aIndex, TDes& aPhoneNumber) const
       
   242 	{
       
   243 //	__ASSERT_ALWAYS(aIndex >= KCntMinSpeedDialIndex && aIndex <= KCntMaxSpeedDialIndex, User::Panic(KErrArgument));
       
   244 	const CSpeedDial& item = iSpeedDialTable.At(aIndex - 1);
       
   245 	TSpeedDialPhoneNumber speedNumber;
       
   246 	item.PhoneNumber(speedNumber);
       
   247 	aPhoneNumber = speedNumber;
       
   248 	return item.ContactId();
       
   249 	}
       
   250 
       
   251 CArrayFix<TInt>* CCntServerSpeedDialTable::SpeedDialIndicesForContactIdLC(TContactItemId aContactId)
       
   252 	{
       
   253 	CArrayFixFlat<TInt>* indices = new(ELeave) CArrayFixFlat<TInt>(KCntMaxSpeedDialIndex);
       
   254 	CleanupStack::PushL(indices);
       
   255 
       
   256 	const TInt count = iSpeedDialTable.Count();
       
   257 	for(TInt i=0; i<count; i++)
       
   258 		{
       
   259 		const CSpeedDial& item = iSpeedDialTable.At(i);
       
   260 		if	(item.ContactId() == aContactId)
       
   261 			indices->AppendL(i+1); // +1 to map onto the contacts speed dial indices of 1-9
       
   262 		}
       
   263 	return indices;
       
   264 	}
       
   265 
       
   266 void CCntServerSpeedDialTable::SetSpeedDialL(TInt aIndex, TContactItemId aContactId, const TSpeedDialPhoneNumber& aPhoneNumber)
       
   267 	{
       
   268 //	__ASSERT_ALWAYS(aIndex >= KCntMinSpeedDialIndex && aIndex <= KCntMaxSpeedDialIndex, CntServerUtils::Panic(ECntServerPanicSpeedDialIndexOutOfRange));
       
   269 	CSpeedDial& item = iSpeedDialTable.At(aIndex - 1);
       
   270 	item.SetL(aContactId, aPhoneNumber);	
       
   271 	}
       
   272 
       
   273 void CCntServerSpeedDialTable::InternalizeL(RReadStream& aStream)
       
   274 	{
       
   275 	HBufC* file = HBufC::NewLC(aStream, KMaxTInt);
       
   276 	delete iDatabaseFile;
       
   277 	iDatabaseFile = file;
       
   278 	CleanupStack::Pop(file); 
       
   279 	const TInt count = iSpeedDialTable.Count();
       
   280 	for(TInt i=0; i<count; i++)
       
   281 		{
       
   282 		CSpeedDial& tableItem = iSpeedDialTable.At(i);
       
   283 		tableItem.InternalizeL(aStream);
       
   284 		}
       
   285 	}
       
   286 
       
   287 void CCntServerSpeedDialTable::ExternalizeL(RWriteStream& aStream) const
       
   288 	{
       
   289 	aStream << *iDatabaseFile;
       
   290 	const TInt count = iSpeedDialTable.Count();
       
   291 	for(TInt i=0; i<count; i++)
       
   292 		{
       
   293 		aStream << iSpeedDialTable.At(i);
       
   294 		}
       
   295 	}
       
   296 
       
   297 TUid CCntServerSpeedDialManager::SpeedDialFieldUidFromSpeedDialPosition(TInt aSpeedDialPosition)
       
   298 //
       
   299 //	Static utility method to map a speed dial index onto a field uid.
       
   300 //
       
   301 	{
       
   302 //	__ASSERT_ALWAYS(aSpeedDialPosition >= KCntMinSpeedDialIndex && aSpeedDialPosition <= KCntMaxSpeedDialIndex, Panic(ECntPanicInvalidSpeedDialIndex));
       
   303 
       
   304 	TUid fieldTypeUid = KNullUid;
       
   305 	switch (aSpeedDialPosition)
       
   306 		{
       
   307 	case 1:
       
   308 		fieldTypeUid = KUidSpeedDialOne;
       
   309 		break;
       
   310 	case 2:
       
   311 		fieldTypeUid = KUidSpeedDialTwo;
       
   312 		break;
       
   313 	case 3:
       
   314 		fieldTypeUid = KUidSpeedDialThree;
       
   315 		break;
       
   316 	case 4:
       
   317 		fieldTypeUid = KUidSpeedDialFour;
       
   318 		break;
       
   319 	case 5:
       
   320 		fieldTypeUid = KUidSpeedDialFive;
       
   321 		break;
       
   322 	case 6:
       
   323 		fieldTypeUid = KUidSpeedDialSix;
       
   324 		break;
       
   325 	case 7:
       
   326 		fieldTypeUid = KUidSpeedDialSeven;
       
   327 		break;
       
   328 	case 8:
       
   329 		fieldTypeUid = KUidSpeedDialEight;
       
   330 		break;
       
   331 	case 9:
       
   332 		fieldTypeUid = KUidSpeedDialNine;
       
   333 		break;
       
   334 		}
       
   335 
       
   336 	return fieldTypeUid; 
       
   337 	}
       
   338