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