// ---------------------------------------------------------------------------
// Loops over the requested messages noticing the ends of the message array.
// The implementation is somewhat more complex than the corresponding NextL()
// because the messages are processed in the order they are inserted to the
// result array.
// ---------------------------------------------------------------------------
//
TBool CIpsPlgMsgIterator::PreviousL(
    TInt aEnd,
    CMsvEntrySelection* aMessageEntries,
    TUint aCount,
    RPointerArray<CFSMailMessage>& aMessages)
    {
    FUNC_LOG;
    __ASSERT_DEBUG( ( aEnd < aMessageEntries->Count() ), 
        User::Panic( KIpsPlgPanicCategory, EIpsPlgInvalidMessageIndex ) );
     
    TInt i = aEnd;
    TInt counter( 0 );
    CFSMailMessage* fsMsg; 
    
    while (( counter < aCount ) && ( i >= 0 ) )
        {
        const TMsvEmailEntry& entry( 
            iFolderEntry->ChildDataL( aMessageEntries->At(i) ) );
        if ( ( EDisconnectedDeleteOperation != entry.DisconnectedOperation() ) &&
        	(( entry.iMtm != KSenduiMtmImap4Uid )  || !entry.DeletedIMAP4Flag() ) &&	
   	 		 ( entry.iType == KUidMsvMessageEntry ) )
            {
            fsMsg = iMsgMapper->GetMailMessageL( iMailboxId, entry, 
                iRequestedDetails );
            aMessages.InsertL( fsMsg, 0 );
            counter++;
            }
        i--;
        }
    
    return ( i > 0 );
    }
// -----------------------------------------------------------------------------
// AppendItemsToArrayL
// append aSourceArray items to aDestArray
// -----------------------------------------------------------------------------
//
void AppendItemsToArrayL( const TArray<MCLFItem*>& aSourceArray,
                          RPointerArray<MCLFItem>& aDestArray,
                          TCLFUndefinedItemPosition aPosition,
                          TInt& aItemIndex,
                          TInt& aLastAddedItemCount )
{
    if( aPosition == ECLFSortingStyleUndefinedFirst )
    {
        aItemIndex = aItemIndex - aLastAddedItemCount;
    }

    aLastAddedItemCount = aSourceArray.Count();
    for( TInt i = 0 ; i < aLastAddedItemCount ; ++i )
    {
        aDestArray.InsertL( aSourceArray[i], aItemIndex );
        ++aItemIndex;
    }
}
void CGlxMediaListsTestCollectionPlugin::InsertItemL(const TDesC& aFileName, 
	              const TDesC& aTitle, 
	              RPointerArray<CItem>& aDatabase, 
	              TTime aDateTime , 
	              TInt  aFileSize ,
	              const TDesC& aDrive ) 
	{
	CItem* item = new (ELeave) CItem();
	CleanupStack::PushL(item);
	item->iId = TGlxMediaId(iItemAddedId++);
	item->SetFilenameL(aFileName);
	item->SetTitleL(aTitle);
	item->iDateTime = aDateTime;
	item->iFileSize = aFileSize;
	item->SetDriveL(aDrive);
	TInt databaseCount = aDatabase.Count();
	TInt position = Math::Random() % (databaseCount + 1);
	aDatabase.InsertL(item, position); 
	CleanupStack::Pop(); // item	
	}
/** list all the child part to this part 
    /note This method leaves with KErrNotSupported if it is NOT multi-part.
*/
EXPORT_C void CMsgStoreMessagePart::ChildPartsL( RPointerArray<CMsgStoreMessagePart>& aParts )
{
	
	TPartsArray partsArray( iContext, iMailBoxId, aParts );
	
	iContext.iSession.ChildrenPropertiesL( iId,               // aId
	                                       iParentId,         // aParentId
	                                       EMsgStorePartBits, // aContainerType
	                                       EFalse,            // aQuickProperties
	                                       EFalse,            // aRecursive	                                        	                                        
	                                       partsArray );	
	
	// make sure body come before attachments
	TInt count = aParts.Count(); 
	if ( count > 1 )
		{
		for ( TInt i = 0 ; i < count; i++ )
			{
			CMsgStoreMessagePart* part = aParts[i];
			TUint index = 0;
			if ( part->FindProperty( KMsgStorePropertyContentType, index ) )
				{
				const TDesC& contentType = part->PropertyValueDesL( index );
				if ( contentType == KFSMailContentTypeMultipartAlternative )
					{
						if( i > 0 )  
							{
							//body is not the first child, move it to the first place
							aParts.Remove(i);
							aParts.InsertL( part, 0 );
							}
						break;
					}
				}
			}
		}
}