pimprotocols/phonebooksync/Server/Phonebook.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2002-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 // Contains method implementations for Phonebook Synchroniser's CPhoneBook
       
    15 // class which stores the parameters for each phonebook, including the
       
    16 // Look-Up Table, Phonebook store handle and flags for the synchroniser
       
    17 // engine.
       
    18 // This class can be accessed from either the server side or engine side.
       
    19 // 
       
    20 //
       
    21 
       
    22 /**
       
    23  @file
       
    24  @internalComponent
       
    25 */
       
    26 
       
    27 #include "phbksync.h"
       
    28 #include "Phonebook.h"
       
    29 #include "PhonebookManager.h"
       
    30 #include "SyncContactICCEntry.h"
       
    31 #include "phbksyncsvr.h"
       
    32 
       
    33 
       
    34 /**
       
    35  *  Granularity of the Look Up Table.
       
    36  */
       
    37 const TInt KLookUpTableGranularity = 16;
       
    38 
       
    39 
       
    40 /**
       
    41  *  Default constructor for a Look-Up Table entry.
       
    42  */
       
    43 CPhoneBook::TLookUpTableEntry::TLookUpTableEntry()
       
    44   : iContactId(KNullContactId),
       
    45     iSlotState(ESlotEmpty)
       
    46 	{
       
    47 	// NOP
       
    48 	} // CPhoneBook::TLookUpTableEntry::TLookUpTableEntry
       
    49 
       
    50 
       
    51 /**
       
    52  *  Static factory method to create a CPhoneBook object.
       
    53  *
       
    54  *  @param aPhonebookUid  UID of the phonebook.
       
    55  */
       
    56 CPhoneBook* CPhoneBook::NewL(TUid aPhonebookUid)
       
    57 	{
       
    58 	CPhoneBook*  phonebook = new(ELeave) CPhoneBook(aPhonebookUid);
       
    59 	CleanupStack::PushL(phonebook);
       
    60 	phonebook->ConstructL();
       
    61 	CleanupStack::Pop(phonebook);
       
    62 
       
    63 	return phonebook;
       
    64 	} // CPhoneBook::NewL
       
    65 
       
    66 
       
    67 /**
       
    68  *  Standard constructor.
       
    69  *
       
    70  *  @param aPhonebookUid  UID of the phonebook.
       
    71  */
       
    72 CPhoneBook::CPhoneBook(TUid aPhonebookUid)
       
    73   : iPhonebookUid(aPhonebookUid),
       
    74     iSyncMode(RPhoneBookSession::EManual),
       
    75     iPhBkInfoRetrievedResult(KErrNotReady),
       
    76     iSyncState(RPhoneBookSession::EUnsynchronised),
       
    77     iLastSyncError(KErrNone),
       
    78     iTemplateId(KNullContactId),
       
    79     iGroupId(KNullContactId),
       
    80     iContactFieldsV3(),
       
    81     iPhonebookStore(),
       
    82     iPhBkInfoV5(),
       
    83 	iLookUpTable(KLookUpTableGranularity)
       
    84 	{
       
    85 	iPhBkInfoV5.iIdentity.Copy(KPhonebookIdUnknown);
       
    86 	} // CPhoneBook::CPhoneBook
       
    87 
       
    88 
       
    89 /**
       
    90  *  Standard destructor.
       
    91  */
       
    92 CPhoneBook::~CPhoneBook()
       
    93 	{
       
    94 	// NOP
       
    95 	} // CPhoneBook::~CPhoneBook
       
    96 
       
    97 
       
    98 /**
       
    99  *  Second phase constructor.
       
   100  */
       
   101 void CPhoneBook::ConstructL()
       
   102 	{
       
   103 	// NOP
       
   104 	} // CPhoneBook::ConstructL
       
   105 
       
   106 
       
   107 /**
       
   108  *  Changes the size of the look-up table so that it contains the specified
       
   109  *  number of elements.
       
   110  *
       
   111  *  @param aSize  New desired size of the table.
       
   112  */
       
   113 void CPhoneBook::SetLookUpTableSizeL(TInt aSize)
       
   114 	{
       
   115 	TInt  currentSize(iLookUpTable.Count());
       
   116 
       
   117     //
       
   118 	// Are we increasing or decreasing the table size???
       
   119 	//
       
   120 	if (currentSize < aSize)
       
   121 		{
       
   122 		//
       
   123 		// Expand the table...
       
   124 		//
       
   125 		TLookUpTableEntry  blankEntry;
       
   126 
       
   127 		for (TInt index = currentSize;  index < aSize;  index++)
       
   128 			{
       
   129 			iLookUpTable.AppendL(blankEntry);
       
   130 			}
       
   131 		}
       
   132 	else if (currentSize > aSize)
       
   133 		{
       
   134 		//
       
   135 		// Delete any entries that come after 'aSize', e.g. ensure only
       
   136 		// 'aSize' number of entries remain.  This is basically deleting
       
   137 		// from the end to the middle.
       
   138 		//
       
   139 		for (TInt index = currentSize - 1;  index >= aSize;  index--)
       
   140 			{
       
   141 			iLookUpTable.Delete(index);
       
   142 			}
       
   143 		}
       
   144 	} // CPhoneBook::SetLookUpTableSizeL
       
   145 
       
   146 
       
   147 /**
       
   148  *  Clears the look-up table so that it contains only default values.
       
   149  */
       
   150 void CPhoneBook::ClearLookUpTable()
       
   151 	{
       
   152 	TInt  currentSize(iLookUpTable.Count());
       
   153 
       
   154 	//
       
   155 	// Initilise the table...
       
   156 	//
       
   157 	TLookUpTableEntry  blankEntry;
       
   158 
       
   159 	for (TInt index = 0;  index < currentSize;  index++)
       
   160 		{
       
   161 		iLookUpTable[index] = blankEntry;
       
   162 		}
       
   163 	} // CPhoneBook::ClearLookUpTable
       
   164 
       
   165 
       
   166 /**
       
   167  *  Check whether an entry with the given Contacts UID exists in the
       
   168  *  look-up-table and whether that entry has already been flagged as
       
   169  *  being used.
       
   170  *
       
   171  *  @param aContactId  Contacts ICC entry UID
       
   172  *
       
   173  *  @return KErrNone if entry found, otherwise KErrNotFound.
       
   174  */
       
   175 TInt CPhoneBook::IsEntryInTable(TContactItemId aContactId) const
       
   176 	{
       
   177 	TInt  totalEntries(iLookUpTable.Count());
       
   178 
       
   179 	for(TInt index = 0;  index < totalEntries ;  index++)
       
   180 		{
       
   181 		const TLookUpTableEntry&  entry = iLookUpTable.At(index);
       
   182 
       
   183 		if (entry.iContactId == aContactId)
       
   184 			{
       
   185 			if (entry.iSlotState == ESlotUsedAvailable  ||
       
   186 			    entry.iSlotState == ESlotHiddenAvailable)
       
   187 			    {
       
   188 				return KErrNone;
       
   189 				}
       
   190 			else if (entry.iSlotState == ESlotUsedNotAvailable  ||
       
   191 			         entry.iSlotState == ESlotHiddenNotAvailable)
       
   192 				{
       
   193 				return KErrAccessDenied;
       
   194 				}
       
   195 			}
       
   196 		}
       
   197 
       
   198 	return KErrNotFound;
       
   199 	} // CPhoneBook::IsEntryInTable
       
   200 
       
   201 
       
   202 /**
       
   203  *  Update the Contact UID parameter for a phonebook entry in the Look-Up
       
   204  *  Table. The entry is identified by the aSlotNum parameter.
       
   205  *
       
   206  *  @param aSlotNum     Phonebook entry slot number
       
   207  *  @param aContactId  Contacts ICC entry UID
       
   208  *
       
   209  *  @return KErrNone if entry found, otherwise KErrNotFound
       
   210  */
       
   211 TInt CPhoneBook::UpdateEntryInTable(TInt aSlotNum,
       
   212 									TContactItemId aContactId)
       
   213 	{
       
   214 	if (aSlotNum >= 1  &&  aSlotNum <= iLookUpTable.Count())
       
   215 		{
       
   216 		TLookUpTableEntry&  entry = iLookUpTable.At(aSlotNum-1);
       
   217 		
       
   218 		if (entry.iSlotState == ESlotUsedAvailable  &&
       
   219  		    (entry.iContactId == KNullContactId  ||
       
   220 		     entry.iContactId == KGoldenTemplateId))
       
   221 			{
       
   222 			entry.iContactId = aContactId;
       
   223 			return KErrNone;
       
   224 			}
       
   225 		}
       
   226 	
       
   227 	return KErrNotFound;
       
   228 	} // CPhoneBook::UpdateEntryInTable
       
   229 
       
   230 
       
   231 /**
       
   232  *  Update parameters for a phonebook entry in the Look-Up Table. The entry
       
   233  *  is identified by the aSlotNum parameter whereas the information to be
       
   234  *  updated is supplied by the aContactId & aSlotState parameters.
       
   235  *
       
   236  *  @param aSlotNum    Phonebook entry slot number
       
   237  *  @param aContactId  Contacts ICC entry UID
       
   238  *  @param aSlotState  Phonebook entry slot state
       
   239  *
       
   240  *  @return KErrNone if entry found, otherwise KErrNotFound
       
   241  */ 
       
   242 TInt CPhoneBook::UpdateEntryInTable(TInt aSlotNum,
       
   243 									TContactItemId aContactId,
       
   244 									TPhonebookSlotState aSlotState)
       
   245 	{
       
   246 	if (aSlotNum >= 1  &&  aSlotNum <= iLookUpTable.Count())
       
   247 		{
       
   248 		TLookUpTableEntry&  entry = iLookUpTable.At(aSlotNum-1);
       
   249 		
       
   250 		entry.iContactId = aContactId;
       
   251 		entry.iSlotState = aSlotState;
       
   252 
       
   253 		return KErrNone;
       
   254 		}
       
   255 
       
   256 	return KErrNotFound;
       
   257 	} // CPhoneBook::UpdateEntryInTable
       
   258 
       
   259 
       
   260 /**
       
   261  *  Return a Contacts UID for the given slot number.
       
   262  *
       
   263  *  @param aSlotNum  Slot Number of the entry.
       
   264  *
       
   265  *  @return Contacts UID if that entry found, otherwise KNullContactId. 
       
   266  */
       
   267 TContactItemId CPhoneBook::GetContactIdFromSlotNum(TInt aSlotNum) const
       
   268 	{
       
   269 	if (aSlotNum >= 1  &&  aSlotNum <= iLookUpTable.Count())
       
   270 		{
       
   271 		const TLookUpTableEntry&  entry = iLookUpTable.At(aSlotNum-1);
       
   272 		
       
   273 		return entry.iContactId;
       
   274 		}
       
   275 
       
   276 	return KNullContactId;
       
   277 	} // CPhoneBook::GetContactIdFromSlotNum
       
   278 
       
   279 
       
   280 /**
       
   281  *  Return a slot number for the given Contacts UID.
       
   282  *
       
   283  *  @param aContactId  Contacts UID of the entry.
       
   284  *
       
   285  *  @return Slot Number if that entry found, otherwise KErrNotFound.
       
   286  */
       
   287 TInt CPhoneBook::GetSlotNumFromContactId(TContactItemId aContactId) const
       
   288 	{
       
   289 	//
       
   290 	// Take care of some default values...
       
   291 	//
       
   292 	if (aContactId == KNullContactId  ||
       
   293 		aContactId == KGoldenTemplateId)
       
   294 		{
       
   295 		return KErrNotFound;
       
   296 		}
       
   297 	
       
   298 	//
       
   299 	// Search the table for the Contact UID...
       
   300 	//
       
   301 	TInt  totalEntries(iLookUpTable.Count());
       
   302 
       
   303 	for (TInt index = 0;  index < totalEntries;  index++)
       
   304  		{
       
   305 		const TLookUpTableEntry&  entry = iLookUpTable.At(index);
       
   306 
       
   307 		if (entry.iContactId == aContactId)
       
   308 			{
       
   309 			return index+1; // Slot numbers start from 1
       
   310 			}
       
   311 		}
       
   312 
       
   313 	return KErrNotFound;
       
   314 	} // CPhoneBook::GetSlotNumFromContactId
       
   315 
       
   316 
       
   317 /**
       
   318  *  Return the count of free slots for this phonebook. 
       
   319  *
       
   320  *  @return Number of slots free.
       
   321  */
       
   322 TInt CPhoneBook::GetNumFreeSlots() const
       
   323 	{
       
   324 	TInt  numFreeSlots(0);
       
   325 
       
   326 	//
       
   327 	// Scan through the table and count the empty slots...
       
   328 	//
       
   329 	TInt  totalEntries(iLookUpTable.Count());
       
   330 
       
   331 	for (TInt index = 0;  index < totalEntries;  index++)
       
   332  		{
       
   333 		const TLookUpTableEntry&  entry = iLookUpTable.At(index);
       
   334 
       
   335 		if (entry.iSlotState == ESlotEmpty)
       
   336 			{
       
   337 			numFreeSlots++;
       
   338 			}
       
   339 		}
       
   340 		
       
   341 	return numFreeSlots;
       
   342 	} // CPhoneBook::GetNumFreeSlots
       
   343 
       
   344 
       
   345 /**
       
   346  *  Return the first available slot from the look-up table.
       
   347  *
       
   348  *  @return Slot Number if an empty slot found, otherwise KErrNotFound.
       
   349  */
       
   350 TInt CPhoneBook::GetFirstEmptySlot() const
       
   351 	{
       
   352 	TInt  totalEntries(iLookUpTable.Count());
       
   353 
       
   354 	for (TInt index = 0;  index < totalEntries;  index++)
       
   355  		{
       
   356 		const TLookUpTableEntry&  entry = iLookUpTable.At(index);
       
   357 
       
   358 		if (entry.iSlotState == ESlotEmpty)
       
   359 			{
       
   360 			return index+1; // Slot numbers start from 1
       
   361 			}
       
   362 		}
       
   363 
       
   364 	return KErrNotFound;
       
   365 	} // CPhoneBook::GetFirstEmptySlot
       
   366 
       
   367 
       
   368 /**
       
   369  *  Return the slot numbers of all matching entries.
       
   370  *
       
   371  *  @param aSlotState  Slot state to search for.
       
   372  *  @param aEntries    RArray containing the slot numbers of all matching entries.
       
   373  */
       
   374 void CPhoneBook::GetMatchingEntries(TPhonebookSlotState aSlotState,
       
   375 									RArray<TInt>& aEntries) const
       
   376 	{
       
   377 	TInt  totalEntries(iLookUpTable.Count());
       
   378 
       
   379 	for (TInt index = 0;  index < totalEntries;  index++)
       
   380  		{
       
   381 		const TLookUpTableEntry&  entry = iLookUpTable.At(index);
       
   382 
       
   383 		if (entry.iSlotState == aSlotState)
       
   384 			{
       
   385 			aEntries.Append(index+1); // Slot numbers start from 1
       
   386 			}
       
   387 		}
       
   388 	} // CPhoneBook::GetMatchingEntries
       
   389 
       
   390 
       
   391 /**
       
   392  *  Output the Look-Up Table to the debug log file.
       
   393  *
       
   394  *  @note This function only works in debug mode.
       
   395  */
       
   396 void CPhoneBook::LogLookUpTable() const
       
   397 	{
       
   398 	LOGPARAMS2(_L8("LogLookUpTable(): Phonebook=0x%08x"), iPhonebookUid);
       
   399 	
       
   400 #ifdef _DEBUG
       
   401 	//
       
   402 	// Write the contents of the table...
       
   403 	//
       
   404 	TInt  tableSize(iLookUpTable.Count());
       
   405 
       
   406 	for (TInt index = 0;  index < tableSize;  index++)
       
   407 		{
       
   408 		const TLookUpTableEntry&  entry = iLookUpTable[index];
       
   409 		TBuf8<32>  slotStateStr;
       
   410 
       
   411 		switch (entry.iSlotState)
       
   412 			{
       
   413 			case ESlotUnconfirmed:
       
   414 				{
       
   415 				slotStateStr.Copy(_L8("Unconfirmed       "));
       
   416 				}
       
   417 				break;
       
   418 
       
   419 			case ESlotEmpty:
       
   420 				{
       
   421 				slotStateStr.Copy(_L8("Empty             "));
       
   422 				}
       
   423 				break;
       
   424 
       
   425 			case ESlotUsedAvailable:
       
   426 				{
       
   427 				slotStateStr.Copy(_L8("UsedAvailable     "));
       
   428 				}
       
   429 				break;
       
   430 
       
   431 			case ESlotUsedNotAvailable:
       
   432 				{
       
   433 				slotStateStr.Copy(_L8("UsedNotAvailable  "));
       
   434 				}
       
   435 				break;
       
   436 
       
   437 			case ESlotHiddenAvailable:
       
   438 				{
       
   439 				slotStateStr.Copy(_L8("HiddenAvailable   "));
       
   440 				}
       
   441 				break;
       
   442 
       
   443 			case ESlotHiddenNotAvailable:
       
   444 				{
       
   445 				slotStateStr.Copy(_L8("HiddenNotAvailable"));
       
   446 				}
       
   447 				break;
       
   448 
       
   449 			default:
       
   450 				{
       
   451 				slotStateStr.Copy(_L8("Illegal_Enum_value"));
       
   452 				}
       
   453 				break;
       
   454 			}
       
   455 
       
   456 		LOGPARAMS5(_L8("LUT[%03d]:  %3d  0x%08x  %S"), index, index+1,
       
   457 				   entry.iContactId, &slotStateStr);
       
   458 		}
       
   459 #endif
       
   460 	} // CPhoneBook::LogLookUpTable