harvesterplugins/messaging/src/cmessagemonitor.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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "cmessagemonitor.h"
       
    21 #include "harvesterserverlogger.h"
       
    22 
       
    23 #include <smut.h>
       
    24 #include <mmsvattachmentmanager.h>	// MMsvAttachmentManager
       
    25 #include <msventry.h>	// CMsvEntry
       
    26 #include <msvuids.h>
       
    27 #include <mmsconst.h>
       
    28 #include <mtclreg.h>
       
    29 #include <smsclnt.h>
       
    30 #include <senduiconsts.h>
       
    31 
       
    32 
       
    33 // DEFINES
       
    34 #define INDEXING_QUEUE_MAX 100 // Maximum number of messages in the queue
       
    35 #define INDEXING_DELAY 5000000 // Nano seconds to delay the monitored messages 
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CMessageMonitor::NewL
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CMessageMonitor* CMessageMonitor::NewL(  CMessagePlugin& aMessagePlugin, CMsvSession& aSession )
       
    44 	{
       
    45 	CMessageMonitor* self = new ( ELeave ) CMessageMonitor( aMessagePlugin, aSession );
       
    46 	CleanupStack::PushL( self );
       
    47 	self->ConstructL();
       
    48 	CleanupStack::Pop( self );
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CMessageMonitor::CMessageMonitor
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CMessageMonitor::CMessageMonitor( CMessagePlugin& aMessagePlugin, CMsvSession& aSession )
       
    57 	: CActive(CActive::EPriorityStandard),
       
    58 	iState(EStateNone),
       
    59 	iMsvSession(aSession),
       
    60 	iMessagePlugin(aMessagePlugin)
       
    61 	{
       
    62 	CPIXLOGSTRING("ENTER CMessageMonitor::CMessageMonitor");
       
    63 
       
    64 	CActiveScheduler::Add(this);
       
    65 
       
    66 	CPIXLOGSTRING("END CMessageMonitor::CMessageMonitor");
       
    67 	}
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CMessageMonitor::ConstructL
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 void CMessageMonitor::ConstructL() 
       
    74 	{
       
    75 	CPIXLOGSTRING("ENTER CMessageMonitor::ConstructL");
       
    76 
       
    77 	// Create timer 
       
    78 	User::LeaveIfError(iTimer.CreateLocal());
       
    79 	
       
    80 	CPIXLOGSTRING("END CMessageMonitor::ConstructL");
       
    81 	}
       
    82 	
       
    83 
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CMessageMonitor::~CMessageMonitor
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 CMessageMonitor::~CMessageMonitor() 
       
    90 	{
       
    91 	CPIXLOGSTRING("ENTER ~CMessageMonitor");
       
    92 	Cancel();
       
    93 	iMessages.Close();
       
    94 	iTimer.Close();
       
    95 	CPIXLOGSTRING("END ~CMessageMonitor");
       
    96 	}
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CMessageMonitor::HandleMsgMovedL
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CMessageMonitor::HandleMsgMovedL( const TMsvId aNewFolderId, 
       
   103 									   const TMsvId /* aOldFolderId */, 
       
   104 								   	   const CMsvEntrySelection& aSelection )
       
   105 	{
       
   106 	CPIXLOGSTRING("ENTER CMessageMonitor::HandleMsgMovedL");
       
   107 	
       
   108 	const TInt count( aSelection.Count() );
       
   109 	// cycle through every message in the CMsvEntrySelection
       
   110 	for ( TInt i=0; i < count; ++i )
       
   111 		{
       
   112 		TMsvId msgId = aSelection[i];
       
   113 		CPIXLOGSTRING2("msgId: %d", msgId );
       
   114         MessageItemL( msgId, ECPixUpdateAction, aNewFolderId );
       
   115 		}
       
   116 	CPIXLOGSTRING("END CMessageMonitor::HandleMsgMovedL");
       
   117 	}
       
   118 
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CMessageMonitor::0
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CMessageMonitor::HandleMsgCreatedChangedL( const CMsvEntrySelection& aSelection,
       
   125                                           	    const TMsvId aFolderId,
       
   126                                           	    const MMsvSessionObserver::TMsvSessionEvent /*aEvent*/ )
       
   127 	{
       
   128 	CPIXLOGSTRING("ENTER CMessageMonitor::HandleMsgCreatedChangedL");	
       
   129 	const TInt count( aSelection.Count() );
       
   130 	//MMsvSessionObserver::TMsvSessionEvent theEvent = aEvent;
       
   131     // cycle through every message in the CMsvEntrySelection
       
   132 	for ( TInt i=0; i < count; ++i )
       
   133 		{
       
   134 		// extract the message server entry from the parameters
       
   135 		TMsvId msgId = aSelection[i];
       
   136 		CPIXLOGSTRING2("msgId: %d", msgId );							
       
   137         TMsvId service = 0;
       
   138         TMsvEntry entry;
       
   139         const TInt err = iMsvSession.GetEntry( msgId, service, entry );
       
   140         if ( ( !err ) && 
       
   141             ( KUidMsvMessageEntry == entry.iType ) && 
       
   142             ( entry.Visible() && entry.Complete() && !entry.InPreparation() ) &&
       
   143             ( iMessagePlugin.CalculateMessageType( entry ) != EMsgTypeInvalid ) )
       
   144             {
       
   145             CPIXLOGSTRING("CMessageMonitor::HandleMsgCreatedChangedL Called # MonitorEvent #");
       
   146             MessageItemL( msgId, ECPixUpdateAction, aFolderId );
       
   147             }   
       
   148         CPIXLOGSTRING2(" Folder TYPE %x", aFolderId );	     
       
   149         CPIXLOGSTRING2(" Entry TYPE %x", entry.iType );	           
       
   150         CPIXLOGSTRING2(" Entry VISIBLE %d", entry.Visible() );	          
       
   151         CPIXLOGSTRING2(" Entry COMPLETE %d", entry.Complete() );
       
   152         CPIXLOGSTRING2(" Entry INPREPARATION %d", entry.InPreparation() );
       
   153         CPIXLOGSTRING2(" Message TYPE %d", iMessagePlugin.CalculateMessageType( entry ));
       
   154         }
       
   155 	CPIXLOGSTRING("END CMessageMonitor::HandleMsgCreatedChangedL");	        
       
   156 	}
       
   157 	
       
   158 // -----------------------------------------------------------------------------
       
   159 // CMessageMonitor::HandleMsgDeletedL
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CMessageMonitor::HandleMsgDeletedL( const CMsvEntrySelection& aSelection )
       
   163 	{
       
   164 	CPIXLOGSTRING("ENTER CMessageMonitor::HandleMsgDeletedL");
       
   165 	
       
   166 	const TInt count( aSelection.Count() );
       
   167 	// cycle through every message in the CMsvEntrySelection
       
   168 	for( TInt i = 0; i < count; ++i )
       
   169 		{
       
   170 		TMsvId msgId = aSelection[i];	
       
   171 		CPIXLOGSTRING2("msgId: %d", msgId );								
       
   172 		MessageItemL( msgId, ECPixRemoveAction, NULL);
       
   173 		}
       
   174 	CPIXLOGSTRING("END CMessageMonitor::HandleMsgDeletedL");	
       
   175 	}	
       
   176 
       
   177 
       
   178 void CMessageMonitor::OverwriteOrAddToQuereL(TMsvId aMsvId, TCPixActionType aAction, TMsvId aFolderId)
       
   179 {
       
   180 	// Overwrite or add the index to the queue
       
   181 	TMessageRecord message;
       
   182 	message.iMsvId = aMsvId;
       
   183 	message.iAction = aAction;
       
   184 	message.iFolderId = aFolderId;
       
   185 	for (TInt i=0; i<iMessages.Count(); i++)
       
   186 	{
       
   187 		if (iMessages[i].iMsvId==aMsvId)
       
   188 		{
       
   189 			// Older version found
       
   190 			iMessages[i] = message;
       
   191 			return;
       
   192 		}
       
   193 	}
       
   194 	
       
   195 	// older not found, append
       
   196 	iMessages.AppendL(message);
       
   197 }
       
   198 
       
   199 void CMessageMonitor::MessageItemL(TMsvId aMsvId, TCPixActionType aAction, TMsvId aFolderId)
       
   200 	{	
       
   201 		// Overwrite or add the index to the queue
       
   202 		OverwriteOrAddToQuereL(aMsvId, aAction, aFolderId);
       
   203 			
       
   204 		// Check the size against maximum queue size
       
   205 		if (iMessages.Count() > INDEXING_QUEUE_MAX)
       
   206 		{
       
   207 			// Maximum is exceeded, force the write immediately
       
   208 			if (iState == EStateWaiting)
       
   209 			{
       
   210 				iTimer.Cancel(); // RunL will be called with iStatus of KErrCancelled
       
   211 			}
       
   212 			else if (iState == EStateNone)
       
   213 			{
       
   214 		        SetActive();
       
   215 		        TRequestStatus* status = &iStatus;
       
   216 		        User::RequestComplete(status, KErrNone); // RunL will be called with iStatus of KErrNone
       
   217 			}
       
   218 		}
       
   219 		else
       
   220 		{
       
   221 			// Maximum is not exceeded, keep waiting
       
   222 			if (iState == EStateNone)
       
   223 			{
       
   224 				iState = EStateWaiting;
       
   225 				iTimer.After(iStatus, INDEXING_DELAY); // Wait 5 seconds before putting this to index
       
   226 				SetActive();
       
   227 			}
       
   228 		}
       
   229 	}
       
   230 
       
   231 void CMessageMonitor::RunL()
       
   232 	{
       
   233 		// Index the current queue	
       
   234 		while (iMessages.Count()>0)
       
   235 		{
       
   236 			TMessageRecord message = iMessages[0];
       
   237 			iMessages.Remove(0);
       
   238 			iMessagePlugin.MessageItemL(message.iMsvId, message.iAction, message.iFolderId);
       
   239 		}
       
   240 		
       
   241 		// Everything is indexed no need to be waiting anymore
       
   242 		iState = EStateNone;
       
   243 	}
       
   244 
       
   245 void CMessageMonitor::DoCancel()
       
   246 	{
       
   247 		iTimer.Cancel();
       
   248 		iState = EStateNone;
       
   249 	}
       
   250 
       
   251 TInt CMessageMonitor::RunError(TInt /* aError */)
       
   252 	{
       
   253 		// TODO handle indexing errors here
       
   254 		return KErrNone; // Don't panic
       
   255 	}
       
   256 
       
   257 // End Of File
       
   258