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