messagingfw/msgsrvnstore/server/src/MSVARRAY.CPP
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 1998-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 "MSVARRAY.H"
       
    17 #include "MSVUIDS.H"
       
    18 #include "MSVPANIC.H"
       
    19 #include "cmsvdescriptionarray.h"
       
    20 
       
    21 const TInt KEntryArrayGranularity=32;
       
    22 const TInt KSortingMtmListGranularity=8;
       
    23 
       
    24 
       
    25 //**********************************
       
    26 // TKeyArrayFixPtr
       
    27 //**********************************
       
    28 
       
    29 EXPORT_C TAny* TKeyArrayFixPtr::At(TInt anIndex) const
       
    30 //
       
    31 // Key class for sorting on dereferenced pointers
       
    32 //
       
    33 	{
       
    34 	if (anIndex==KIndexPtr)
       
    35 		return((TUint8 *)((*((const TUint8 **)iPtr))+iKeyOffset)); 			
       
    36 	return((TAny *)((*((const TUint8 **)(iBase->Ptr(anIndex*iRecordLength).Ptr())))+iKeyOffset));
       
    37 	}
       
    38 
       
    39 
       
    40 //**********************************
       
    41 // CMsvEntryArray
       
    42 //**********************************
       
    43 
       
    44 EXPORT_C CMsvEntryArray* CMsvEntryArray::NewL(const CArrayFix<TUid>& aMtmList)
       
    45 	{
       
    46 	CMsvEntryArray* self = new(ELeave) CMsvEntryArray(aMtmList);
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 EXPORT_C CMsvEntryArray* CMsvEntryArray::NewLC(const CArrayFix<TUid>& aMtmList)
       
    51 	{
       
    52 	CMsvEntryArray* self = new(ELeave) CMsvEntryArray(aMtmList);
       
    53 	CleanupStack::PushL(self);
       
    54 	return self;
       
    55 	}
       
    56 
       
    57 CMsvEntryArray::CMsvEntryArray(const CArrayFix<TUid>& aMtmList)
       
    58 : CArrayPtrFlat<const TMsvEntry>(KEntryArrayGranularity), iOrigMtmList(aMtmList)
       
    59 	{}
       
    60 
       
    61 
       
    62 EXPORT_C CMsvEntryArray::~CMsvEntryArray()
       
    63 	{
       
    64 	delete iActualMtmList;
       
    65 	}
       
    66 
       
    67 
       
    68 // Subject based sorting is a special case because text such as Re: , Fwd: etc is removed
       
    69 // before the sort. This means email threads are now appear in sequence.
       
    70 // The original TMsvEntry SUBJECT string is preserved as the sort takes place on a temporary
       
    71 // array of CDescription.
       
    72 // Second part of the method is a bubble sort on DATE which deals with duplicates.
       
    73 // This means that email threads will be in DATE order, oldest date first. 
       
    74 void CMsvEntryArray::SubjectBasedSortL(TBool aReverse, const TDesC& aSubjectSkipString)
       
    75 	{
       
    76 	if(Count() < 2) // One or less no sort required
       
    77 		return;
       
    78 
       
    79 	// Create a temporary array of CDescription's and set the offset for the sort to iDescription
       
    80 	CMsvDescriptionArray* descriptionArray = new (ELeave) CMsvDescriptionArray();
       
    81 	CleanupStack::PushL(descriptionArray);
       
    82 	TKeyArrayFixPtr key(_FOFF(TMsvDescription,iDescription),ECmpCollated);
       
    83 	TInt count = Count(); // Use this count for both loops
       
    84 	// Loop through the class instance array and construct a CDescription array ready
       
    85 	// for the sort.
       
    86 	for(TInt i =0;i<count;++i)
       
    87 		{
       
    88 		TMsvDescription* modified = new (ELeave) TMsvDescription();
       
    89 		CleanupStack::PushL(modified);
       
    90 		modified->iEntry = const_cast<TMsvEntry*>(At(i)); // Pointer to the real TMsvEntry
       
    91 		modified->iDescription.Set(At(i)->iDescription);// Copy of its TPtrC (may be modified)
       
    92 		TPtrC ptr(modified->iDescription);
       
    93 		TInt offset(0);
       
    94 		// Perform the search and skip Re: Fwd: etc if found
       
    95 		// offset points to the start of the real subject string.
       
    96 		if((offset = FindSubjectStart(ptr,aSubjectSkipString)) != KErrNotFound) 
       
    97 			{
       
    98 			ptr.Set(ptr.Ptr()+offset,ptr.Length()-offset);
       
    99 			modified->iDescription.Set(ptr);
       
   100 			}
       
   101 		descriptionArray->InsertIsqAllowDuplicatesL(modified,key); // Insert into temporary array
       
   102 		CleanupStack::Pop(modified);
       
   103 		}
       
   104 	// Reverse before we do the duplicate sort
       
   105 	if(aReverse)
       
   106 		{
       
   107 		descriptionArray->ReverseOrder();
       
   108 		}
       
   109 	// Bubble sort individual email threads if any, on DATE
       
   110 	// Achieved in a single loop by resetting current to the beginning of the email thread if
       
   111 	// we are still bubble sorting the thread.
       
   112 	// Less complicated than creating new (N email threads) x CArrayFix derived classes just
       
   113 	// for its sort methods
       
   114 	TMsvDescription** current = &descriptionArray->At(0);
       
   115 	TMsvDescription** startSort(NULL); // Beginning of an email thread
       
   116 	TBool threadSorting(EFalse); // Continue bubble sorting a thread whilst this flag is set
       
   117 	// Loop exits when sorting is false and end of list
       
   118 	for(;;)
       
   119 		{
       
   120 		++current;
       
   121 		// IF not the end of list AND still in email thread 
       
   122 		if(current <= &descriptionArray->At(count-1) && (*current)->iDescription == (*(current-1))->iDescription) // Compare current and previous
       
   123 			{
       
   124 			if(startSort == NULL) // Duplicate
       
   125 				{
       
   126 				startSort = current-1; // Keep reference to the start of the email thread.
       
   127 				}
       
   128 			if((*(current-1))->iEntry->iDate < (*current)->iEntry->iDate) // Check the DATE
       
   129 				{
       
   130 				threadSorting = ETrue; // Set this flag so we loop through this thread again
       
   131 				// Perform the swap
       
   132 				TMsvDescription* temp = *(current-1);
       
   133 				*(current-1) = *current;
       
   134 				*current = temp;
       
   135 				}
       
   136 			}
       
   137 		else if(!threadSorting)
       
   138 			{
       
   139 			// (end of thread  OR end of list). Either continue to next email thread or finished the list 
       
   140 			if(current >= &descriptionArray->At(count-1))
       
   141 				{
       
   142 				break; // End of list
       
   143 				}
       
   144 			else
       
   145 				{
       
   146 				startSort = NULL; // Continue to next email thread, if any.
       
   147 				}
       
   148 			}
       
   149 		else
       
   150 			{
       
   151 			// End of thread but not yet sorted. 
       
   152 			// Reset the list pointer to the start of the email thread.
       
   153 			threadSorting = EFalse;
       
   154 			current=startSort;
       
   155 			}
       
   156 		}
       
   157 	// Reset the CMsvEntryArray class instance list then repopulate it with the bubble sorted one.
       
   158 	// The TMsvEntry's are pointers to the ones in the original list
       
   159 	Reset();
       
   160 	for(TInt i=0;i<descriptionArray->Count();++i)
       
   161 		{
       
   162 		AppendL(descriptionArray->At(i)->iEntry);
       
   163 		}
       
   164 	CleanupStack::PopAndDestroy(descriptionArray);
       
   165 	}
       
   166 
       
   167 // Return an offset of the real subject. Skips Re: Fwd: etc.
       
   168 // The string to skip is in the class instance iSubjectSkipString.
       
   169 // Returns a valid offset or KErrNotFound
       
   170 TInt CMsvEntryArray::FindSubjectStart(const TDesC& aSubject, const TDesC& aSubjectSkipString) const
       
   171 	{
       
   172 	// Set up temporary's for more readable code
       
   173 	// current moves along the descriptor
       
   174 	// offset is the index into the descriptor of the real subject. (Can be KErrNotFound)
       
   175 	TPtrC ptr(aSubject);
       
   176 	TInt offset(0);
       
   177 	TInt current(0);
       
   178 	TInt skipLength = aSubjectSkipString.Length();
       
   179 	do
       
   180 		{
       
   181 		current = ptr.FindF(aSubjectSkipString);
       
   182 		if(current != KErrNotFound)
       
   183 			{
       
   184 			// Found a match so increment past the skip string instance. (Could be more)
       
   185 			offset+= current + skipLength;
       
   186 			ptr.Set(aSubject.Ptr()+offset,aSubject.Length()-offset);
       
   187 			}
       
   188 		else if(offset == 0)
       
   189 			{
       
   190 			// None found
       
   191 			offset = KErrNotFound;
       
   192 			}
       
   193 		}while(current != KErrNotFound);
       
   194 	return offset;
       
   195 	}
       
   196 
       
   197 CMsvDescriptionArray::CMsvDescriptionArray() : CArrayPtrFlat<TMsvDescription>(8)
       
   198 	{
       
   199 	}
       
   200 
       
   201 CMsvDescriptionArray::~CMsvDescriptionArray()
       
   202 	{
       
   203 	ResetAndDestroy();
       
   204 	}
       
   205 
       
   206 // Reverse on CDescription array
       
   207 void CMsvDescriptionArray::ReverseOrder()
       
   208 	{
       
   209 	TInt c=Count()-1;
       
   210 	if (c>0)
       
   211 		{
       
   212 		TMsvDescription** low=&At(0);
       
   213 		TMsvDescription** high=low+c;
       
   214 		while (low<high)
       
   215 			{
       
   216 			TMsvDescription* t=*low;
       
   217 			*low++=*high;
       
   218 			*high--=t;
       
   219 			}
       
   220 		}
       
   221 	}
       
   222 
       
   223 
       
   224 
       
   225 EXPORT_C void CMsvEntryArray::SortL(TMsvSelectionOrdering aOrdering)
       
   226 //
       
   227 // sorts this array
       
   228 //
       
   229 	{
       
   230 	// If we're going to reverse the array first sort by id so sorting twice will give the same result
       
   231 	switch(aOrdering.Sorting())
       
   232 		{
       
   233 		case EMsvSortByDateReverse:
       
   234 		case EMsvSortByIdReverse:
       
   235 		case EMsvSortBySizeReverse:
       
   236 		case EMsvSortByDescriptionReverse:
       
   237 		case EMsvSortByDetailsReverse:
       
   238 			{
       
   239 			TKeyArrayFixPtr key = MessageSortKey(EMsvSortById);
       
   240 			User::LeaveIfError(Sort(key));
       
   241 			break;
       
   242 			}
       
   243 		default: // Not required - break
       
   244 			break;
       
   245 		}
       
   246 	if (Count() && (aOrdering.Sorting()!=EMsvSortByNone || aOrdering.GroupingOn()))
       
   247 		GroupL(EGroupByStandardFolders, aOrdering, (aOrdering.Sorting()!=EMsvSortByNone));
       
   248 	}
       
   249 
       
   250 void CMsvEntryArray::GroupL(TGroupCriterion aGroupCriterion,TMsvSelectionOrdering aOrdering,TBool aDoSort)
       
   251 //
       
   252 // This function works recursively, grouping and sorting the entry selection.  The 
       
   253 // 'sort' happens at the same time as the 1st 'group', then separate grouped arrays 
       
   254 // are grouped indidvidually and then merged together at the end. The order in which 
       
   255 // the grouping occurs is determined by the CMsvEntryArray::TGroupCriterion enum
       
   256 //	
       
   257 	{
       
   258 	TMsvSorting sortType=aOrdering.Sorting();
       
   259 	TKeyArrayFixPtr key=MessageSortKey(sortType);
       
   260 	
       
   261 	if (aGroupCriterion==EStopGrouping)
       
   262 		{  // if you haven't sorted yet
       
   263 		if (aDoSort)
       
   264 			{
       
   265 			// Subject based sorting requires a special algorithm. Only message entries are treated as other entries normally
       
   266 			// do not have a prefix like e.g. "re: " or "fwd: "
       
   267 			if(At(0)->iType == KUidMsvMessageEntry && (sortType == 	EMsvSortByDescription || sortType == EMsvSortByDescriptionReverse))
       
   268 				{
       
   269 				SubjectBasedSortL(sortType == EMsvSortByDescriptionReverse,aOrdering.SubjectSkipString());							
       
   270 				}
       
   271 			else
       
   272 				{
       
   273 				CMsvEntryArray* temp=CMsvEntryArray::NewLC(iOrigMtmList);
       
   274 				TInt count=Count();
       
   275 				if (count)
       
   276 					temp->InsertL(0,&(*(this))[0],count);
       
   277 				Reset();
       
   278 				const TMsvEntry** entry = &temp->At(0);
       
   279 				while (count--)
       
   280 					InsertIsqAllowDuplicatesL(*entry++, key); // Sorted
       
   281 				ReverseOrder(sortType);
       
   282 				CleanupStack::PopAndDestroy(); // temp
       
   283 				}
       
   284 			if (At(0)->iType == KUidMsvMessageEntry && (sortType == EMsvSortByDetails || sortType == EMsvSortByDetailsReverse))
       
   285 				{
       
   286 				DetailBasedSortL(); // Sort blocks of messages with matching details into newest first
       
   287 				}
       
   288 			}
       
   289 		else
       
   290 			{
       
   291 			// The aDoSort flag is not set, but we still need to do a subject
       
   292 			// based sort if this array contains only message entries, and we are
       
   293 			// sorting by description. Alternatively, we need to do a date based sort
       
   294 			// if this array contains only message entries and we are sorting by detail.
       
   295 			// In order to ensure the array contains only message entries, we
       
   296 			// check that we have previously grouped the entries by type which would
       
   297 			// have put all the message entries together in their own array.
       
   298 			if (Count() > 0 && At(0)->iType == KUidMsvMessageEntry && OkToGroup(EGroupByType, aOrdering))
       
   299 				{
       
   300 				if (sortType == EMsvSortByDescription || sortType == EMsvSortByDescriptionReverse)
       
   301 					{
       
   302 					SubjectBasedSortL(sortType == EMsvSortByDescriptionReverse,aOrdering.SubjectSkipString());
       
   303 					}
       
   304 				else if (sortType == EMsvSortByDetails || sortType == EMsvSortByDetailsReverse)
       
   305 					{
       
   306 					DetailBasedSortL(); // Sort blocks of messages with matching details into newest first
       
   307 					}
       
   308 				}
       
   309 			}
       
   310 		return;
       
   311 		}
       
   312 
       
   313 	if (OkToGroup(aGroupCriterion, aOrdering))
       
   314 		{
       
   315 		//
       
   316 		// Copy contents into temp and then put new grouped contents into 'this'
       
   317 		TInt count=Count();
       
   318 		if (count==0)
       
   319 			return; // nothing to do here
       
   320 		const TInt numberOfArrays=NumberOfArraysToSplitIntoL(aGroupCriterion);
       
   321 		if (numberOfArrays<1)  // cannot group on this so move on to next grouping
       
   322 			{
       
   323 			GroupL(TGroupCriterion(aGroupCriterion+1), aOrdering, aDoSort);
       
   324 			return;
       
   325 			}
       
   326 		CMsvEntryArray* temp;
       
   327 		if (iActualMtmList)
       
   328 			temp = CMsvEntryArray::NewLC(*iActualMtmList);
       
   329 		else
       
   330 			temp = CMsvEntryArray::NewLC(iOrigMtmList);
       
   331 		temp->InsertL(0,&(*(this))[0],count);
       
   332 		Reset();
       
   333 
       
   334 		//
       
   335 		// create the separate arrays for each group
       
   336 		CArrayFixFlat<CMsvEntryArray*>* arrays=new(ELeave) CArrayFixFlat<CMsvEntryArray*>(numberOfArrays);
       
   337 		CleanupStack::PushL(arrays);
       
   338 		for (TInt ii=0; ii<numberOfArrays; ii++)
       
   339 			{
       
   340 			if (iActualMtmList)
       
   341 				arrays->AppendL(CMsvEntryArray::NewLC(*iActualMtmList));
       
   342 			else
       
   343 				arrays->AppendL(CMsvEntryArray::NewLC(iOrigMtmList));
       
   344 			}
       
   345 
       
   346 		//
       
   347 		// split the selection into the correct group, 
       
   348 		// sorting aswell if needed and not doing standard folders
       
   349 		const TMsvEntry** entry = &temp->At(0);
       
   350 		if (!aDoSort || aGroupCriterion==EGroupByStandardFolders)
       
   351 			{
       
   352 			while (count--)
       
   353 				{
       
   354 				arrays->At(ArrayId(*entry,aGroupCriterion))->AppendL(*entry);
       
   355 				entry++;
       
   356 				}
       
   357 			}
       
   358 		else if (aGroupCriterion==EGroupByType)
       
   359 			{
       
   360 			TKeyArrayFixPtr folderKey = TKeyArrayFixPtr(_FOFF(TMsvEntry,iDetails),ECmpCollated);
       
   361 			while (count--)
       
   362 				{
       
   363 				if ((*entry)->iType==KUidMsvFolderEntry)
       
   364 					arrays->At(ArrayId(*entry, aGroupCriterion))->InsertIsqAllowDuplicatesL(*entry, folderKey);
       
   365 				else
       
   366 					arrays->At(ArrayId(*entry, aGroupCriterion))->InsertIsqAllowDuplicatesL(*entry, key);
       
   367 				entry++;
       
   368 				}
       
   369 			for (TInt jj=0; jj<numberOfArrays; jj++)
       
   370 				{
       
   371 				if (arrays->At(jj)->Count() && arrays->At(jj)->At(0)->iType!=KUidMsvFolderEntry)
       
   372 					arrays->At(jj)->ReverseOrder(sortType);
       
   373 				}
       
   374 			aDoSort=EFalse; 
       
   375 			}
       
   376 		else
       
   377 			{
       
   378 			while (count--)
       
   379 				{
       
   380 				arrays->At(ArrayId(*entry, aGroupCriterion))->InsertIsqAllowDuplicatesL(*entry, key); // Sorted
       
   381 				entry++;
       
   382 				}
       
   383 			for (TInt jj=0; jj<numberOfArrays; jj++)
       
   384 				arrays->At(jj)->ReverseOrder(sortType);
       
   385 			aDoSort=EFalse; 
       
   386 			}
       
   387 
       
   388 		
       
   389 		
       
   390 		
       
   391 		
       
   392 		//
       
   393 		// group further - but check that standard entries and grouped folders are not grouped anymore
       
   394 		if (aGroupCriterion==EGroupByStandardFolders)
       
   395 			{
       
   396 			__ASSERT_DEBUG(numberOfArrays==2, PanicServer(EMsvToManyGroups));
       
   397 			arrays->At(0)->GroupL(TGroupCriterion(aGroupCriterion+1), aOrdering, aDoSort);
       
   398 			}
       
   399 		else if (aGroupCriterion==EGroupByType)
       
   400 			{
       
   401 			for (TInt jj=0; jj<numberOfArrays; jj++)
       
   402 				if (arrays->At(jj)->Count() && arrays->At(jj)->At(0)->iType!=KUidMsvFolderEntry)
       
   403 					arrays->At(jj)->GroupL(TGroupCriterion(aGroupCriterion+1), aOrdering, aDoSort);
       
   404 			}
       
   405 		else 
       
   406 			{
       
   407 			for (TInt jj=0; jj<numberOfArrays; jj++)
       
   408 				arrays->At(jj)->GroupL(TGroupCriterion(aGroupCriterion+1), aOrdering, aDoSort);
       
   409 			}
       
   410 		
       
   411 		//
       
   412 		// merge the separate arrays into 'this'
       
   413 		for (TInt kk=0; kk<numberOfArrays; kk++)
       
   414 			{
       
   415 			count=arrays->At(kk)->Count();
       
   416 			if (count)
       
   417 				InsertL(0,&(*(arrays->At(kk)))[0],count);
       
   418 			}
       
   419 		CleanupStack::PopAndDestroy(numberOfArrays+2); // arrays contents + temp + arrays
       
   420 		}
       
   421 	else // move on to the next grouping
       
   422 		GroupL(TGroupCriterion(aGroupCriterion+1), aOrdering, aDoSort);
       
   423 	}
       
   424 
       
   425 
       
   426 TInt CMsvEntryArray::NumberOfArraysToSplitIntoL(TGroupCriterion aGroupCriterion)
       
   427 //
       
   428 // Returns the number of arrays the selection will be split into with this grouping method
       
   429 //
       
   430 	{
       
   431 	switch (aGroupCriterion)
       
   432 		{
       
   433 		case EGroupByType:
       
   434 			return 5;
       
   435 		case EGroupByStandardFolders:
       
   436 			return 2;
       
   437 		case EGroupByPriority:
       
   438 			return 3;
       
   439 		case EGroupByMtm:
       
   440 			{
       
   441 			// copy the orig mtm list
       
   442 			if (iActualMtmList==NULL)
       
   443 				iActualMtmList = new(ELeave) CArrayFixFlat<TUid>(KSortingMtmListGranularity);
       
   444 			else
       
   445 				iActualMtmList->Reset();
       
   446 
       
   447 			if (iOrigMtmList.Count())
       
   448 				iActualMtmList->InsertL(0, &iOrigMtmList.At(0), iOrigMtmList.Count());
       
   449 
       
   450 			// need to add any unknown MTM to the list 
       
   451 			TKeyArrayFix key = TKeyArrayFix(0, ECmpTUint);
       
   452 			for (TInt ii=0; ii<Count(); ii++)
       
   453 				{
       
   454 				TUid uid = At(ii)->iMtm;
       
   455 				TInt temp;
       
   456 				if (iActualMtmList->Find(uid, key, temp))
       
   457 					iActualMtmList->AppendL(uid);
       
   458 				}	
       
   459 			return iActualMtmList->Count();
       
   460 			}
       
   461 		default: // case EStopGrouping:
       
   462 			return 0;
       
   463 		}
       
   464 	}
       
   465 
       
   466 
       
   467 TBool CMsvEntryArray::OkToGroup(TGroupCriterion aGroupCriterion,TMsvSelectionOrdering aOrdering) const
       
   468 //
       
   469 // Returns true if the current grouping method is selected
       
   470 // 
       
   471 	{
       
   472 	switch (aGroupCriterion)
       
   473 		{
       
   474 		case EGroupByType:
       
   475 			return aOrdering.GroupByType();
       
   476 		case EGroupByStandardFolders:
       
   477 			return aOrdering.GroupStandardFolders();
       
   478 		case EGroupByPriority:
       
   479 			return aOrdering.GroupByPriority();
       
   480 		case EGroupByMtm:
       
   481 			return aOrdering.GroupByMtm();
       
   482 		default: //case EStopGrouping:
       
   483 			return EFalse;
       
   484 		}
       
   485 //	return EFalse;
       
   486 	}
       
   487 
       
   488 TInt CMsvEntryArray::ArrayId(const TMsvEntry* aEntry, TGroupCriterion aGroupCriterion) const
       
   489 //
       
   490 // Returns the id of the array to plave the entry into
       
   491 //
       
   492 	{
       
   493 	switch (aGroupCriterion)
       
   494 		{
       
   495 		case EGroupByType:
       
   496 			{
       
   497 			switch (aEntry->iType.iUid)
       
   498 				{
       
   499 				case KUidMsvAttachmentEntryValue:
       
   500 					return 1;
       
   501 				case KUidMsvMessageEntryValue:
       
   502 					return 2;
       
   503 				case KUidMsvFolderEntryValue:
       
   504 					return 3;
       
   505 				case KUidMsvServiceEntryValue:
       
   506 					return 4;
       
   507 				default:
       
   508 					return 0; // unknown types are grouped after attachments
       
   509 				}
       
   510 			}
       
   511 		case EGroupByPriority:
       
   512 			{
       
   513 			switch (aEntry->Priority())
       
   514 				{
       
   515 				case EMsvLowPriority:
       
   516 					return 0;
       
   517 				case EMsvMediumPriority:
       
   518 					return 1;
       
   519 				case EMsvHighPriority:
       
   520 					return 2;
       
   521 				default:
       
   522 					PanicServer(EMsvUnknownPriority);
       
   523 				}
       
   524 			}
       
   525 		case EGroupByMtm:
       
   526 			{
       
   527 			__ASSERT_DEBUG(iActualMtmList, PanicServer(EMsvMtmListNotDefined));
       
   528 			TInt index;
       
   529 			TKeyArrayFix key(0, ECmpTUint);
       
   530 			TInt error = iActualMtmList->Find(aEntry->iMtm, key, index);
       
   531 			__ASSERT_ALWAYS(error==KErrNone, PanicServer(EMsvUnknownMtm));
       
   532 			return iActualMtmList->Count() - index - 1;
       
   533 			}
       
   534 		case EGroupByStandardFolders:
       
   535 			return aEntry->StandardFolder() ? 1 : 0;
       
   536 		default:
       
   537 			return -1;
       
   538 		}
       
   539 	}
       
   540 
       
   541 
       
   542 TKeyArrayFixPtr CMsvEntryArray::MessageSortKey(TMsvSorting aSortType) const
       
   543 //
       
   544 // Return appropriate key for desired sort
       
   545 //
       
   546 	{
       
   547 	switch(aSortType)
       
   548 		{
       
   549 		case EMsvSortByDate:
       
   550 		case EMsvSortByDateReverse:
       
   551 			{
       
   552 			return TKeyArrayFixPtr(_FOFF(TMsvEntry,iDate),ECmpTInt64);
       
   553 			}
       
   554 		case EMsvSortBySize:
       
   555 		case EMsvSortBySizeReverse:
       
   556 			{
       
   557 			return TKeyArrayFixPtr(_FOFF(TMsvEntry,iSize),ECmpTUint);
       
   558 			}
       
   559 		case EMsvSortByDescription:
       
   560 		case EMsvSortByDescriptionReverse:
       
   561 			{
       
   562 			return TKeyArrayFixPtr(_FOFF(TMsvEntry,iDescription),ECmpCollated);
       
   563 			}
       
   564 		case EMsvSortByDetails:
       
   565 		case EMsvSortByDetailsReverse:
       
   566 			{
       
   567 			return TKeyArrayFixPtr(_FOFF(TMsvEntry,iDetails),ECmpCollated);
       
   568 			}
       
   569 		case EMsvSortById:
       
   570 		case EMsvSortByIdReverse:
       
   571 			{
       
   572 			return TKeyArrayFixPtr(_FOFF(TMsvEntry,iId),ECmpTInt32);
       
   573 			}
       
   574 		// case EMsvSortByNone:
       
   575 		default:
       
   576 			return TKeyArrayFixPtr(0,ECmpNormal);			
       
   577 		}
       
   578 	}
       
   579 
       
   580 void CMsvEntryArray::ReverseOrder(TMsvSorting aSortType)
       
   581 //
       
   582 // Reverse based in SortBy value
       
   583 //
       
   584 	{
       
   585 	switch(aSortType)
       
   586 		{
       
   587 		case EMsvSortByDateReverse:
       
   588 		case EMsvSortByIdReverse:
       
   589 		case EMsvSortBySizeReverse:
       
   590 		case EMsvSortByDescriptionReverse:
       
   591 		case EMsvSortByDetailsReverse:
       
   592 			ReverseOrder();
       
   593 			break;
       
   594 		default: // Not required - break
       
   595 			return;
       
   596 		}
       
   597 	}
       
   598 
       
   599 void CMsvEntryArray::ReverseOrder()
       
   600 //
       
   601 // Atomic selection reverse function
       
   602 //
       
   603 	{
       
   604 	TInt c=Count()-1;
       
   605 	if (c>0)
       
   606 		{
       
   607 		const TMsvEntry** low=&At(0);
       
   608 		const TMsvEntry** high=low+c;
       
   609 		while (low<high)
       
   610 			{
       
   611 			const TMsvEntry* t=*low;
       
   612 			*low++=*high;
       
   613 			*high--=t;
       
   614 			}
       
   615 		}
       
   616 	}
       
   617 
       
   618 void CMsvEntryArray::DetailBasedSortL()
       
   619 	{
       
   620 	// Find blocks of entries with matching details
       
   621 	TInt blockStart = 0;
       
   622 
       
   623 	while (blockStart < Count())
       
   624 		{
       
   625 		const TMsvEntry* firstEntry = At(blockStart);
       
   626 		TInt nextBlock = blockStart + 1;
       
   627 
       
   628 		// Find the start of the next block		
       
   629 		while (nextBlock < Count() && At(nextBlock)->iDetails == firstEntry->iDetails)
       
   630 			{
       
   631 			nextBlock++;
       
   632 			}
       
   633 		const TInt blockSize = nextBlock - blockStart;
       
   634 
       
   635 		// Sort the block by date, newest first
       
   636 		if (blockSize > 1)
       
   637 			{
       
   638 			TKeyArrayFixPtr key(_FOFF(TMsvEntry,iDate),ECmpTInt64);
       
   639 			CMsvEntryArray* temp = CMsvEntryArray::NewLC(iOrigMtmList);
       
   640 			const TMsvEntry** entry = &At(blockStart);
       
   641 
       
   642 			// Copy block entries in sequence to temporary array
       
   643 			for (TInt tempIndex = 0; tempIndex < blockSize; tempIndex++, entry++)
       
   644 				{
       
   645 				temp->InsertIsqAllowDuplicatesL(*entry, key);
       
   646 				}
       
   647 			// Reverse to give newest first
       
   648 			temp->ReverseOrder();
       
   649 
       
   650 			// Replace block with sorted entries
       
   651 			entry = &At(blockStart);
       
   652 			for (TInt tempIndex = 0; tempIndex < blockSize; tempIndex++, entry++)
       
   653 				{
       
   654 				*entry = temp->At(tempIndex);
       
   655 				}
       
   656 
       
   657 			CleanupStack::PopAndDestroy(temp);
       
   658 			}
       
   659 
       
   660 		// Move on to next block of entries
       
   661 		blockStart = nextBlock;
       
   662 		}
       
   663 	}
       
   664