harvesterplugins/messaging/src/cmessageharvester.cpp
changeset 2 208a4ba3894c
parent 0 ccd0fd43f247
child 3 6832643895f7
equal deleted inserted replaced
0:ccd0fd43f247 2:208a4ba3894c
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Harvester message plugin
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDES 
       
    20 
       
    21 #include <e32base.h>
       
    22 #include <mtclreg.h>
       
    23 #include <mmsclient.h>
       
    24 #include <smsclnt.h>
       
    25 #include <smuthdr.h>
       
    26 #include <smsclnt.h>
       
    27 #include <smut.h>
       
    28 #include <gsmuelem.h>
       
    29 #include <msventry.h>
       
    30 #include <msvuids.h>
       
    31 #include <mmsconst.h>
       
    32 #include "cmessageharvester.h"
       
    33 
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // CMessageHarvester::NewL
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CMessageHarvester* CMessageHarvester::NewL(  CMessagePlugin& aMessagePlugin, CMsvSession& aSession )
       
    42 	{
       
    43 	CMessageHarvester* self = new (ELeave) CMessageHarvester(  aMessagePlugin, aSession );
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // CMessageHarvester::~CMessageHarvester
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CMessageHarvester::~CMessageHarvester()
       
    52 	{   
       
    53 	Cancel();  
       
    54     delete iChildren;
       
    55     iToBeSearchedFolders.Close();
       
    56 	}
       
    57 	  
       
    58 // ---------------------------------------------------------------------------
       
    59 // CMessageHarvester::CMessageHarvester
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CMessageHarvester::CMessageHarvester( CMessagePlugin& aMessagePlugin, CMsvSession& aSession  ) :
       
    63 	CActive(CActive::EPriorityIdle),
       
    64 	iMessagePlugin( aMessagePlugin ),
       
    65 	iMsvSession( aSession ),
       
    66 	iState(EStateIdle)
       
    67 	{
       
    68 	CActiveScheduler::Add( this );
       
    69 	}
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CMessageHarvester::StartHarvesting
       
    73 // ---------------------------------------------------------------------------
       
    74 //	
       
    75 void CMessageHarvester::StartHarvestingL()
       
    76 	{
       
    77 	// First, get message folders
       
    78 	GetMessageFoldersL();
       
    79 	
       
    80 	// Then, harvest them
       
    81 	iState = EStateNextFolder;
       
    82 	HandleNextRequest();
       
    83 	}
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CMessageHarvester::AddFolderWithSubFoldersL
       
    87 // ---------------------------------------------------------------------------
       
    88 //		
       
    89 void CMessageHarvester::AddFolderWithSubFoldersL( const TMsvId& aFolderId )
       
    90 	{
       
    91 	// Add a folder to the search list
       
    92 	User::LeaveIfError(iToBeSearchedFolders.Append(aFolderId));	
       
    93 	CMsvEntry* folderEntry = CMsvEntry::NewL(iMsvSession, aFolderId, TMsvSelectionOrdering(KMsvGroupByStandardFolders, EMsvSortByDetails, EFalse));
       
    94 	CleanupStack::PushL(folderEntry);	
       
    95 	ListSubFoldersL(*folderEntry);
       
    96 	CleanupStack::PopAndDestroy(folderEntry);
       
    97 	}
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CMessageHarvester::ListSubFoldersL
       
   101 // ---------------------------------------------------------------------------
       
   102 //	
       
   103 void CMessageHarvester::ListSubFoldersL(  const CMsvEntry& aFolderEntry ) 
       
   104 	{
       
   105 	// Only list "folder" children
       
   106     CMsvEntrySelection* children = aFolderEntry.ChildrenL();
       
   107 	CleanupStack::PushL( children );
       
   108 
       
   109 	for ( TInt i=0; i < children->Count(); i++ )
       
   110 		{
       
   111 		TMsvId& childId = ( *children )[i];
       
   112 		const TMsvEntry& childEntry ( aFolderEntry.ChildDataL( childId ) );
       
   113 		if ( ( childEntry.iType == KUidMsvServiceEntry ) || 
       
   114     		 ( childEntry.iType == KUidMsvFolderEntry ) )
       
   115 		    {
       
   116 		    // Add this folder to search list
       
   117             AddFolderWithSubFoldersL( childId );		
       
   118 		    }
       
   119 		}
       
   120 	CleanupStack::PopAndDestroy();  // children
       
   121 	}
       
   122 	
       
   123 // ---------------------------------------------------------------------------
       
   124 // CMessageHarvester::GetMessageFoldersL
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 void CMessageHarvester::GetMessageFoldersL()
       
   128     {
       
   129     // Get list of folders
       
   130     CMsvEntry* rootEntry;
       
   131     rootEntry = CMsvEntry::NewL( iMsvSession, KMsvRootIndexEntryIdValue,
       
   132     TMsvSelectionOrdering( KMsvGroupByStandardFolders, EMsvSortByDetails, EFalse ) );
       
   133     CleanupStack::PushL( rootEntry );
       
   134 	ListSubFoldersL( *rootEntry);
       
   135 	CleanupStack::PopAndDestroy( rootEntry );
       
   136     }
       
   137 
       
   138 			
       
   139 // ---------------------------------------------------------------------------
       
   140 // CMessageHarvester::GetNextFolderL
       
   141 // ---------------------------------------------------------------------------
       
   142 //		
       
   143 void CMessageHarvester::GetNextFolderL()
       
   144     {
       
   145     if ( iToBeSearchedFolders.Count() > 0 )
       
   146         
       
   147     	{
       
   148     	// Open the new folder
       
   149         CMsvEntry* folder = CMsvEntry::NewL(
       
   150 	                            iMsvSession, 
       
   151 	                            iToBeSearchedFolders[0],
       
   152 	                            TMsvSelectionOrdering( KMsvGroupByStandardFolders, EMsvSortByDetails ));
       
   153         
       
   154         iCurrentFolder = iToBeSearchedFolders[0];
       
   155         
       
   156         // Get the message items from the folder
       
   157         CleanupStack::PushL(folder);        
       
   158         iChildren = folder->ChildrenWithTypeL( KUidMsvMessageEntry );
       
   159         CleanupStack::PopAndDestroy( folder );
       
   160         
       
   161         // Remove current folder from the queue
       
   162         iToBeSearchedFolders.Remove(0);
       
   163         iState = EStateNextMessage;
       
   164         HandleNextRequest();
       
   165         }
       
   166     else
       
   167         {
       
   168         // No more folders in the queue
       
   169         iState = EStateIdle;
       
   170         HandleNextRequest();      
       
   171         }
       
   172     }
       
   173 
       
   174 			
       
   175 // ---------------------------------------------------------------------------
       
   176 // CMessageHarvester::GetMessageId
       
   177 // ---------------------------------------------------------------------------
       
   178 //		    
       
   179 void CMessageHarvester::GetMessageIdL()
       
   180     { 
       
   181     if (iChildren && iChildren->Count() > 0)
       
   182         {
       
   183         // Take first item from list of message items, and give it for indexing
       
   184         iMessagePlugin.MessageItemL((*iChildren)[0], ECPixAddAction, iCurrentFolder);
       
   185         
       
   186         // Delete the item from list
       
   187         iChildren->Delete( 0 );   
       
   188         if ( iChildren->Count() > 0 )
       
   189             {
       
   190             // Get the next message
       
   191             iState = EStateNextMessage;
       
   192             HandleNextRequest();      
       
   193             }
       
   194         else 
       
   195             {
       
   196             // No messages in this folder
       
   197             iState = EStateNextFolder;
       
   198             HandleNextRequest();      
       
   199             }
       
   200         }
       
   201     else 
       
   202         {
       
   203         // Current folder dosen't contain any message items so change folder
       
   204         iState = EStateNextFolder;
       
   205         HandleNextRequest();      
       
   206         }
       
   207     }
       
   208       
       
   209 // -----------------------------------------------------------------------------
       
   210 // CMessageHarvester::DoCancel
       
   211 // -----------------------------------------------------------------------------
       
   212 //   
       
   213 void CMessageHarvester::DoCancel()
       
   214 	{
       
   215 	}
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CMessageHarvester::RunL
       
   219 // -----------------------------------------------------------------------------
       
   220 //   
       
   221 void CMessageHarvester::RunL()	
       
   222 	{
       
   223 	
       
   224 	 // Simple Round-Robin scheduling.
       
   225     Deque();
       
   226     CActiveScheduler::Add( this );
       
   227     
       
   228 	switch (iState)
       
   229 		{
       
   230 	       case EStateIdle:
       
   231 	            {
       
   232 	            // Delete current folder items
       
   233 	            delete iChildren; 
       
   234 	            iChildren = NULL;
       
   235 	            
       
   236 	            // Started idling, close the folders
       
   237 	            iToBeSearchedFolders.Reset();
       
   238 
       
   239 	            // Update harvester status
       
   240 	            iMessagePlugin.HarvestingCompleted(KErrNone);	            
       
   241 	            break; 
       
   242 	    	    }    
       
   243 		    case EStateNextMessage:
       
   244 		        {
       
   245 		        // Get the next message
       
   246 		        GetMessageIdL();	
       
   247 		        break;	        
       
   248 		        }
       
   249 	        case EStateNextFolder:
       
   250 	            {
       
   251 	            // Delete current folder items
       
   252 	            delete iChildren; 
       
   253 	            iChildren = NULL;
       
   254 	            
       
   255 	            // Start harvesting folders. 
       
   256 	            GetNextFolderL();	         
       
   257 				break;
       
   258 	            }
       
   259 		}
       
   260 	}
       
   261 	         				
       
   262 // -----------------------------------------------------------------------------
       
   263 // CMessageHarvester::RunError
       
   264 // -----------------------------------------------------------------------------
       
   265 //   
       
   266 TInt CMessageHarvester::RunError( TInt aError )	
       
   267 	{
       
   268 	iState = EStateIdle;
       
   269     iMessagePlugin.HarvestingCompleted(aError);
       
   270 	return KErrNone;
       
   271 	}
       
   272 	
       
   273 // ---------------------------------------------------------------------------
       
   274 // SetNextRequest
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 void CMessageHarvester::HandleNextRequest()
       
   278     {
       
   279     if (!IsActive())
       
   280         {
       
   281         SetActive();
       
   282         TRequestStatus* status = &iStatus;
       
   283         User::RequestComplete( status, KErrNone );
       
   284         }
       
   285     }
       
   286 
       
   287 
       
   288 // End of File