phonebookengines/contactsmodel/cntsrv/src/CntCurrentItemMap.cpp
branchRCL_3
changeset 63 f4a778e096c2
parent 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     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 "CntCurrentItemMap.h"
       
    17 #include <s32stor.h>
       
    18 #include <s32file.h>
       
    19 
       
    20 const TInt KCntCurrentItemMapGranularity = 3;
       
    21 
       
    22 
       
    23 CCntServerCurrentItemMap::CCntServerCurrentItemMap() : iCurrentItemMap(KCntCurrentItemMapGranularity)
       
    24 	{
       
    25 	}
       
    26 
       
    27 CCntServerCurrentItemMap::~CCntServerCurrentItemMap()
       
    28 	{
       
    29 	iCurrentItemMap.ResetAndDestroy();
       
    30 	iCurrentItemMap.Close();
       
    31 	}
       
    32 
       
    33 TContactItemId CCntServerCurrentItemMap::CurrentItem(const TDesC& aDatabaseFile) const
       
    34 	{
       
    35 	TInt index = KErrNotFound;
       
    36 	if	(EntryAvailable(aDatabaseFile, index))
       
    37 		return iCurrentItemMap[index]->ContactId();
       
    38 	return KNullContactId;
       
    39 	}
       
    40 
       
    41 TBool CCntServerCurrentItemMap::EntryAvailable(const TDesC& aDatabaseFile, TInt& aIndex) const
       
    42 	{
       
    43 	const TInt count = iCurrentItemMap.Count();
       
    44 	for(aIndex = 0; aIndex < count; aIndex++)
       
    45 		{
       
    46 		if (FileNamesIdentical(iCurrentItemMap[aIndex]->DatabaseFile(),aDatabaseFile))
       
    47 			return ETrue;
       
    48 		}
       
    49 	aIndex = KErrNotFound;
       
    50 	return EFalse;
       
    51 	}
       
    52 
       
    53 
       
    54 void CCntServerCurrentItemMap::UpdateEntryL(TInt aIndex, TContactItemId aId)
       
    55 	{
       
    56 	__ASSERT_ALWAYS(aIndex >= 0 && aIndex < iCurrentItemMap.Count(), User::Leave(KErrOverflow));
       
    57 	iCurrentItemMap[aIndex]->SetContactId(aId);
       
    58 	}
       
    59 
       
    60 	
       
    61 void CCntServerCurrentItemMap::AddEntryL(const TDesC& aDatabaseFile, TContactItemId aId)
       
    62 	{
       
    63 	CCntCurrentItemMapEntry* entry = new(ELeave) CCntCurrentItemMapEntry();
       
    64 	CleanupStack::PushL(entry);
       
    65 	entry->ConstructL(aDatabaseFile, aId);
       
    66 	User::LeaveIfError(iCurrentItemMap.Append(entry));
       
    67 	CleanupStack::Pop(); // entry
       
    68 	}
       
    69 
       
    70 
       
    71 
       
    72 void CCntServerCurrentItemMap::StoreL(CDictionaryFileStore& aStore) const
       
    73 	{
       
    74 	RDictionaryWriteStream stream;
       
    75 	stream.AssignLC(aStore, KUidCntCurrentItemStream);
       
    76 
       
    77 	const TInt count = iCurrentItemMap.Count();
       
    78 	stream.WriteInt32L(count);
       
    79 	for(TInt i=0; i<count; i++)
       
    80 		{
       
    81 		const CCntCurrentItemMapEntry* entry = iCurrentItemMap[i];
       
    82 		stream << *entry;
       
    83 		}
       
    84 
       
    85 	stream.CommitL();
       
    86 	CleanupStack::PopAndDestroy(); // stream
       
    87 	}
       
    88 
       
    89 
       
    90 void CCntServerCurrentItemMap::RestoreL(CDictionaryFileStore& aStore)
       
    91 	{
       
    92 	if	(aStore.IsPresentL(KUidCntCurrentItemStream))
       
    93 		{
       
    94 		RDictionaryReadStream stream;
       
    95 		stream.OpenLC(aStore, KUidCntCurrentItemStream);
       
    96 
       
    97 		const TInt count = stream.ReadInt32L();
       
    98 		for(TInt i=0; i<count; i++)
       
    99 			{
       
   100 			CCntCurrentItemMapEntry* entry = new(ELeave) CCntCurrentItemMapEntry();
       
   101 			CleanupStack::PushL(entry);
       
   102 			stream >> *entry;
       
   103 			User::LeaveIfError(iCurrentItemMap.Append(entry));
       
   104 			CleanupStack::Pop(); // entry
       
   105 			}
       
   106 
       
   107 		CleanupStack::PopAndDestroy(); // stream
       
   108 		}
       
   109 	}
       
   110 
       
   111 
       
   112 
       
   113 
       
   114 
       
   115 
       
   116 
       
   117 CCntServerCurrentItemMap::CCntCurrentItemMapEntry::CCntCurrentItemMapEntry()
       
   118 	{
       
   119 	}
       
   120 
       
   121 
       
   122 CCntServerCurrentItemMap::CCntCurrentItemMapEntry::~CCntCurrentItemMapEntry()
       
   123 	{
       
   124 	delete iDatabaseFile;
       
   125 	}
       
   126 
       
   127 
       
   128 void CCntServerCurrentItemMap::CCntCurrentItemMapEntry::ConstructL(const TDesC& aFileName, TContactItemId aContactId)
       
   129 	{
       
   130 	iDatabaseFile = aFileName.AllocL();
       
   131 	iContactId = aContactId;
       
   132 	}
       
   133 
       
   134 
       
   135 void CCntServerCurrentItemMap::CCntCurrentItemMapEntry::InternalizeL(RReadStream& aStream)
       
   136 	{
       
   137 	iDatabaseFile = HBufC::NewL(aStream, KMaxTInt);
       
   138 	iContactId = aStream.ReadInt32L();
       
   139 	}
       
   140 
       
   141 
       
   142 void CCntServerCurrentItemMap::CCntCurrentItemMapEntry::ExternalizeL(RWriteStream& aStream) const
       
   143 	{
       
   144 	aStream << *iDatabaseFile;
       
   145 	aStream.WriteInt32L(iContactId);
       
   146 	}
       
   147 
       
   148 
       
   149 
       
   150