phonebookengines_old/contactsmodel/src/CNTDEF.CPP
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     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 <cntdef.h>
       
    17 #include "CNTSTD.H"
       
    18 
       
    19 //
       
    20 // class CContactIdArray
       
    21 //
       
    22 
       
    23 EXPORT_C CContactIdArray* CContactIdArray::NewL()
       
    24 /** Allocates and constructs a new contact ID array. 
       
    25 
       
    26 @return Pointer to the newly created contact ID array. */
       
    27 	{ // static 
       
    28 	CContactIdArray* self=CContactIdArray::NewLC();
       
    29 	CleanupStack::Pop(); // self
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 EXPORT_C CContactIdArray* CContactIdArray::NewLC()
       
    34 /** Allocates and constructs a new contact ID array. 
       
    35 
       
    36 The pointer to the object is left on the cleanup stack.
       
    37 
       
    38 @return Pointer to the newly created contact ID array. */
       
    39 	{ // static 
       
    40 	CContactIdArray* self=new(ELeave) CContactIdArray;
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 EXPORT_C CContactIdArray* CContactIdArray::NewL(const CContactIdArray* aArray)
       
    47 /** Allocates and constructs a new contact ID array and then copies the contents of 
       
    48 aArray into it. 
       
    49 
       
    50 @param aArray Pointer to the contact ID array to copy.
       
    51 @return Pointer to the newly created contact ID array. */
       
    52 	{
       
    53 	CContactIdArray* self=CContactIdArray::NewLC(aArray);
       
    54 	CleanupStack::Pop(); // self
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 EXPORT_C CContactIdArray* CContactIdArray::NewLC(const CContactIdArray* aArray)
       
    59 /** Allocates and constructs a new contact ID array and then copies the contents of 
       
    60 aArray into it. 
       
    61 
       
    62 The pointer to the object is left on the cleanup stack.
       
    63 
       
    64 @param aArray Pointer to the contact ID array to copy.
       
    65 @return Pointer to the newly created contact ID array. */
       
    66 	{
       
    67 	CContactIdArray* self=CContactIdArray::NewLC();
       
    68 	self->CloneL(aArray);
       
    69 	return self;
       
    70 	}
       
    71 	
       
    72 /** Allocates and constructs a new contact ID array based on a RReadStream. 
       
    73 
       
    74 The pointer to the object is left on the cleanup stack.
       
    75 
       
    76 @param aStream RReadStream containing object to internalize.
       
    77 @return Pointer to the newly created contact ID array 
       
    78 @internalTechnology 
       
    79 */
       
    80 CContactIdArray* CContactIdArray::NewLC(RReadStream& aStream)
       
    81 	{
       
    82 	CContactIdArray* self=CContactIdArray::NewLC();
       
    83 	self->InternalizeL(aStream);	
       
    84 	return self;
       
    85 	}	
       
    86 
       
    87 void CContactIdArray::CloneL(const CContactIdArray* aArray)
       
    88 	{
       
    89 	const TInt arrayCount = aArray->Count();
       
    90 	for(TInt loop=0;loop<arrayCount;loop++)
       
    91 		AddL((*aArray)[loop]);
       
    92 	}
       
    93 
       
    94 CContactIdArray::CContactIdArray()
       
    95 	{}
       
    96 
       
    97 void CContactIdArray::ConstructL()
       
    98 	{
       
    99 	iIds=new(ELeave) CArrayFixFlat<TContactItemId>(4);
       
   100 	}
       
   101 
       
   102 EXPORT_C CContactIdArray::~CContactIdArray()
       
   103 /** The destructor frees all resources owned by the array, prior to its destruction. */
       
   104 	{
       
   105 	delete iIds;
       
   106 	}
       
   107 
       
   108 EXPORT_C TInt CContactIdArray::Find(TContactItemId aId) const
       
   109 /** Finds the index of the specified contact ID within the array.
       
   110 
       
   111 @param aId The contact ID to find.
       
   112 @return If the contact ID is found, its index within the array. KErrNotFound 
       
   113 if the ID is not found. */
       
   114 	{
       
   115 	TInt pos;
       
   116 	TKeyArrayFix key(0,ECmpTInt);
       
   117 	if (iIds->Find(aId,key,pos)==KErrNone)
       
   118 		return pos;
       
   119 	return KErrNotFound;
       
   120 	}
       
   121 
       
   122 EXPORT_C void CContactIdArray::AddL(TContactItemId aId)
       
   123 /** Appends a contact ID to the array.
       
   124 
       
   125 @param aId The contact ID to append to the array. */
       
   126 	{
       
   127 //	__ASSERT_DEBUG(Find(aId)<=KErrNotFound,Panic(ECntPanicDuplicateIds));
       
   128 	iIds->AppendL(aId);
       
   129 	}
       
   130 
       
   131 
       
   132 EXPORT_C void CContactIdArray::MoveL(TInt aOldIndex,TInt aNewIndex)
       
   133 /** Moves a contact ID within the array.
       
   134 
       
   135 Note: both indexes must be valid or a panic occurs.
       
   136 
       
   137 @param aOldIndex The index of the ID to move.
       
   138 @param aNewIndex The new index for the contact ID. */
       
   139 	{
       
   140 	/*
       
   141 	This function has unexpected behaviour. From the function prototype
       
   142 	one would expect this to provide simple swap functionality. However,
       
   143 	in fact, this function performs a slightly more complicated task: 
       
   144 	it actually deletes the item at aOldIndex and moves it *after* the 
       
   145 	item at aNewIndex. This behaviour is subtley different from a 'swap'
       
   146 	(see the example at the end of this comment).
       
   147 	
       
   148 	This means that the only time this kind of code could be used:-
       
   149 
       
   150 	const TContactItemId id = (*iIds)[aOldIndex];
       
   151 	(*iIds)[aOldIndex] = (*iIds)[aNewIndex];
       
   152 	(*iIds)[aNewIndex] = id;
       
   153 
       
   154 	is when Abs(aNewIndex - aOldIndex) = 1, which is rare in the simple
       
   155 	testing that has been performed.
       
   156 
       
   157 	An example of the function pre and post execution is below
       
   158 
       
   159 	CContactIdArray::MoveL(aOld = 2, aNew = 7)
       
   160 	CContactIdArray::MoveL - Before operation...
       
   161 	iIds[0] = 5
       
   162 	iIds[1] = 6
       
   163 	iIds[2] = 7
       
   164 	iIds[3] = 4
       
   165 	iIds[4] = 1
       
   166 	iIds[5] = 2
       
   167 	iIds[6] = 3
       
   168 	iIds[7] = 8
       
   169 
       
   170 	CContactIdArray::MoveL - After operation...
       
   171 	iIds[0] = 5
       
   172 	iIds[1] = 6
       
   173 	iIds[2] = 4
       
   174 	iIds[3] = 1
       
   175 	iIds[4] = 2
       
   176 	iIds[5] = 3
       
   177 	iIds[6] = 8
       
   178 	iIds[7] = 7
       
   179 	*/
       
   180 
       
   181 	if	(aOldIndex==aNewIndex)
       
   182 		return;
       
   183 
       
   184 	if	(aNewIndex>aOldIndex && aNewIndex<Count())
       
   185 		++aNewIndex;
       
   186 	TContactItemId id=(*iIds)[aOldIndex];
       
   187 	iIds->InsertL(aNewIndex,id);
       
   188 	if	(aOldIndex>aNewIndex)
       
   189 		++aOldIndex;
       
   190 	Remove(aOldIndex);
       
   191 	}
       
   192 
       
   193 EXPORT_C void CContactIdArray::ReverseOrder()
       
   194 /** Reverses the order of the whole array. */
       
   195 	{
       
   196 	TInt count=Count();
       
   197 	TInt end=count/2;
       
   198 	TInt index2=count-1;
       
   199 	for(TInt index=0;index<end;index++)
       
   200 		{
       
   201 		TContactItemId id=(*iIds)[index];
       
   202 		(*iIds)[index]=(*iIds)[index2];
       
   203 		(*iIds)[index2]=id;
       
   204 		index2--;
       
   205 		}
       
   206 	}
       
   207 
       
   208 EXPORT_C void CContactIdArray::InternalizeL(RReadStream& aStream)
       
   209 /** Internalises a CContactIdArray object from a read stream. The presence of this 
       
   210 function means that the standard templated operator>>() (defined in s32strm.h) 
       
   211 is available to internalise objects of this class.
       
   212 
       
   213 @param aStream Stream from which the object should be internalised. */
       
   214 	{
       
   215 	TContactItemId anitem;
       
   216 	TInt count;
       
   217 	count=aStream.ReadInt32L();
       
   218 	for (TInt i=0;i<count;i++)
       
   219 		{
       
   220 		aStream >> anitem;
       
   221 		iIds->AppendL(anitem);
       
   222 		}
       
   223 	}
       
   224 
       
   225 EXPORT_C void CContactIdArray::ExternalizeL(RWriteStream& aStream) const 
       
   226 /** Externalises a CContactIdArray object to a write stream. The presence of this 
       
   227 function means that the standard templated operator<<() (defined in s32strm.h) 
       
   228 is available to externalise objects of this class. 
       
   229 
       
   230 @param aStream Stream to which the object should be externalised. */
       
   231 	{
       
   232 	TInt count;
       
   233 	TContactItemId anitem;
       
   234 	count=iIds->Count();
       
   235 	aStream.WriteInt32L(count);
       
   236 	for (TInt i=0;i<count;i++)
       
   237 		{
       
   238 		anitem=(*iIds)[i];
       
   239 		aStream << anitem;
       
   240 		}
       
   241 	}
       
   242 	
       
   243 /* Sorts the array into ascending order.  
       
   244 This function is intended for internal use only.*/	
       
   245 void CContactIdArray::Sort()
       
   246 	{
       
   247 	TKeyArrayFix key(0,ECmpTInt);
       
   248 	iIds->Sort(key);     
       
   249 	}