harvester/monitorplugins/messageplugin/src/messagemonitorplugin.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2006-2009 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 "messagemonitorplugin.h"
       
    21 #include "harvesterlog.h"
       
    22 #include "mdesession.h"
       
    23 #include "clientkeywatcherkeys.h" 
       
    24 
       
    25 #include <smut.h>
       
    26 #include <mmsvattachmentmanager.h>	// MMsvAttachmentManager
       
    27 #include <msventry.h>	// CMsvEntry
       
    28 #include <msvfind.h>
       
    29 #include <msvuids.h>
       
    30 #include <mmsconst.h>
       
    31 #include <mtclreg.h>
       
    32 #include <smsclnt.h>
       
    33 #include <driveinfo.h>
       
    34 #include <PathInfo.h>
       
    35 
       
    36 // CONSTANTS
       
    37 #ifdef _DEBUG
       
    38 _LIT( KMsgMonitorHarvesterMsvPanic, "Message monitor harvester: Message server terminated" );
       
    39 #endif
       
    40 _LIT( KMsgURI, "%d.MSG" );
       
    41 
       
    42 const TInt KUriLength = 127;
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CMessageMonitorPlugin::NewL
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CMessageMonitorPlugin* CMessageMonitorPlugin::NewL()
       
    51 	{
       
    52 	CMessageMonitorPlugin* self = new ( ELeave ) CMessageMonitorPlugin();
       
    53 	CleanupStack::PushL( self );
       
    54 	self->ConstructL();
       
    55 	CleanupStack::Pop( self );
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CMessageMonitorPlugin::~CMessageMonitorPlugin
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CMessageMonitorPlugin::~CMessageMonitorPlugin() 
       
    64 	{
       
    65 	WRITELOG("ENTER ~CMessageMonitorPlugin");
       
    66     delete iMsvSession;
       
    67 	WRITELOG("END ~CMessageMonitorPlugin");	
       
    68 	}
       
    69 
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CMessageMonitorPlugin::StartMonitoring
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 TBool CMessageMonitorPlugin::StartMonitoring( MMonitorPluginObserver& aObserver, 
       
    76 											  CMdESession* aMdEClient, 
       
    77 											  CContextEngine* /*aCtxEngine*/,
       
    78 											  CHarvesterPluginFactory* /*aHarvesterPluginFactory*/ )
       
    79 	{
       
    80 	WRITELOG("ENTER CMessageMonitorPlugin::StartMonitoring");
       
    81 	
       
    82 	iMonitorPluginObserver = &aObserver;
       
    83 	iMdeSession = aMdEClient;
       
    84 	
       
    85 	if ( iMsvSession )
       
    86 		{
       
    87 		delete iMsvSession;
       
    88 		iMsvSession = NULL;
       
    89 		}
       
    90 	TInt error( KErrNone );
       
    91 	// Open a new session to the message server
       
    92 	TRAP( error, iMsvSession = CMsvSession::OpenSyncL( *this ) );
       
    93 	WRITELOG1("Open msgSession error: %d" , error );
       
    94 	
       
    95 	TRAP_IGNORE( CheckRestoreL() );
       
    96 		
       
    97 	TBool result = 	error ? EFalse : ETrue ;
       
    98 	WRITELOG("END CMessageMonitorPlugin::StartMonitoring");	
       
    99 		
       
   100 	return result;
       
   101 	}
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CMessageMonitorPlugin::CreateFileNameL
       
   105 // Filename flag can be changed to use cen rep!
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 void CMessageMonitorPlugin::CreateFileNameL() 
       
   109 	{
       
   110 	TInt drive ( 0 );
       
   111 	User::LeaveIfError( 
       
   112 			DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, drive ) );
       
   113 
       
   114     RFs fsSession;
       
   115     User::LeaveIfError( fsSession.Connect() );
       
   116   
       
   117     TChar driveLetter;
       
   118     fsSession.DriveToChar( drive, driveLetter );
       
   119 	
       
   120     iFileName.Append( driveLetter );
       
   121     iFileName.Append( KRestoreFile );  // result-> C:\\private\\200009F5\\restoredone
       
   122  
       
   123     fsSession.Close();
       
   124 	}
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CMessageMonitorPlugin::StopMonitoring
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 TBool CMessageMonitorPlugin::StopMonitoring()
       
   131 	{
       
   132 	WRITELOG("ENTER CMessageMonitorPlugin::StopMonitoring");
       
   133 	if( iMsvSession )
       
   134 		{
       
   135 	    delete iMsvSession;  
       
   136 		iMsvSession = NULL;
       
   137 		}
       
   138 	
       
   139 	WRITELOG("END CMessageMonitorPlugin::StopMonitoring");	
       
   140 	
       
   141 	return ETrue;
       
   142 	}
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CMessageMonitorPlugin::ResumeMonitoring
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 TBool CMessageMonitorPlugin::ResumeMonitoring( MMonitorPluginObserver& /*aObserver*/, 
       
   149 											   CMdESession* /*aMdEClient*/, 
       
   150 											   CContextEngine* /*aCtxEngine*/,
       
   151 											   CHarvesterPluginFactory* /*aHarvesterPluginFactory*/ )
       
   152 	{
       
   153 	WRITELOG("ENTER CMessageMonitorPlugin::ResumeMonitoring");
       
   154 	WRITELOG("END CMessageMonitorPlugin::ResumeMonitoring");
       
   155 	
       
   156 	return ETrue;
       
   157 	}
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CMessageMonitorPlugin::PauseMonitoring
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 TBool CMessageMonitorPlugin::PauseMonitoring()
       
   164 	{
       
   165 	WRITELOG("ENTER CMessageMonitorPlugin::PauseMonitoring");	
       
   166 	WRITELOG("END CMessageMonitorPlugin::PauseMonitoring");	
       
   167 	
       
   168 	return ETrue;
       
   169 	}
       
   170 
       
   171 void CMessageMonitorPlugin::CheckRestoreL()
       
   172 	{
       
   173 	RFs fs;
       
   174 	User::LeaveIfError( fs.Connect() );
       
   175 	CleanupClosePushL( fs );
       
   176 
       
   177 	RFile64 file;
       
   178 	CleanupClosePushL( file );
       
   179 	// only needed information is does file exist
       
   180 	TInt fileError = file.Open( fs, iFileName, EFileShareReadersOrWriters );
       
   181 	WRITELOG1("CMessageMonitorPlugin::CheckRestoreL - fileError: %d", fileError);
       
   182 	User::LeaveIfError( fileError );
       
   183 	
       
   184 	CleanupStack::PopAndDestroy( &file );
       
   185 	CleanupStack::PopAndDestroy( &fs );
       
   186 
       
   187    	iMessageScannerAO = CMessageScannerAO::NewL( iMsvSession, iMdeSession, 
       
   188    			iMonitorPluginObserver );
       
   189    	iMessageScannerAO->StartL();
       
   190 	}
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CMessageMonitorPlugin::CMessageMonitorPlugin
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 CMessageMonitorPlugin::CMessageMonitorPlugin() : CMonitorPlugin(), 
       
   197 												 iMonitorPluginObserver( NULL ),
       
   198 												 iMsvSession( NULL ),
       
   199 												 iMdeSession( NULL ),
       
   200 												 iMessageScannerAO( NULL )
       
   201 	{
       
   202 	WRITELOG("ENTER CMessageMonitorPlugin::CMessageMonitorPlugin");
       
   203 	WRITELOG("END CMessageMonitorPlugin::CMessageMonitorPlugin");		
       
   204 	}
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CMessageMonitorPlugin::ConstructL
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CMessageMonitorPlugin::ConstructL() 
       
   211 	{
       
   212 	WRITELOG("ENTER CMessageMonitorPlugin::ConstructL");
       
   213 	
       
   214 	CreateFileNameL();
       
   215 	
       
   216 	WRITELOG("END CMessageMonitorPlugin::ConstructL");
       
   217 	}
       
   218 	
       
   219 	
       
   220 // -----------------------------------------------------------------------------
       
   221 // CMessageMonitorPlugin::HandleSessionEventL
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CMessageMonitorPlugin::HandleSessionEventL( TMsvSessionEvent aEvent, 
       
   225 										  		 TAny* aArg1, 
       
   226 										  		 TAny* aArg2, 
       
   227 										  		 TAny* aArg3 )
       
   228 	{		
       
   229 	WRITELOG("ENTER CMessageMonitorPlugin::HandleSessionEventL");
       
   230 	WRITELOG1("TMsvSessionEvent aEvent: %d", aEvent );			
       
   231 		
       
   232 	switch( aEvent )
       
   233 		{
       
   234 		case EMsvEntriesCreated:
       
   235 		case EMsvEntriesChanged:
       
   236 			{
       
   237             CMsvEntrySelection* selection =  
       
   238                     		reinterpret_cast<CMsvEntrySelection*> ( aArg1 );
       
   239             TMsvId folderId = *( reinterpret_cast<TMsvId*> ( aArg2 ) );                    
       
   240 			HandleMsgCreatedChangedL( *selection, folderId, aEvent );		
       
   241 			break;
       
   242 			}		
       
   243 		case EMsvEntriesDeleted:
       
   244 			{
       
   245 		    CMsvEntrySelection* selection = 
       
   246                 reinterpret_cast<CMsvEntrySelection*> ( aArg1 );
       
   247             HandleMsgDeletedL( *selection );						
       
   248 			break;
       
   249 			}
       
   250 		case EMsvEntriesMoved:
       
   251 			{
       
   252 			// track move operations
       
   253 			// or sent folder
       
   254 			// ... extract the useful parameters
       
   255             if( aArg1 && aArg2 && aArg3 )
       
   256                 {
       
   257                 CMsvEntrySelection* selection = 
       
   258                     reinterpret_cast<CMsvEntrySelection*> ( aArg1 );
       
   259                 TMsvId folderId1 = *( reinterpret_cast<TMsvId*> ( aArg2 ) );
       
   260                 TMsvId folderId2 = *( reinterpret_cast<TMsvId*> ( aArg3 ) );
       
   261 		        HandleMsgMovedL( folderId1, folderId2, *selection );
       
   262                 }
       
   263 			break;
       
   264 			}			
       
   265 		case EMsvServerFailedToStart:			
       
   266 		case EMsvCloseSession:
       
   267 		case EMsvServerTerminated:
       
   268 			{
       
   269 			__ASSERT_DEBUG( EFalse, User::Panic( KMsgMonitorHarvesterMsvPanic, KErrServerTerminated ));
       
   270 			break;				
       
   271 			}			
       
   272 		case EMsvMtmGroupInstalled:
       
   273 		case EMsvMtmGroupDeInstalled:
       
   274 		case EMsvGeneralError: // not used after v5
       
   275 		case EMsvServerReady:
       
   276 		case EMsvCorruptedIndexRebuilt:
       
   277 		case EMsvMediaChanged:
       
   278 		case EMsvMediaUnavailable:
       
   279 		case EMsvMediaAvailable:
       
   280 		case EMsvMediaIncorrect:
       
   281 		case EMsvCorruptedIndexRebuilding:
       
   282 		default:
       
   283 			{
       
   284 			break;
       
   285 			}							
       
   286 		}	
       
   287 	WRITELOG("END CMessageMonitorPlugin::HandleSessionEventL");	
       
   288 		
       
   289 	}
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 // CMessageMonitorPlugin::HandleMsgMovedL
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CMessageMonitorPlugin::HandleMsgMovedL( const TMsvId aFolderId1, 
       
   296 											 const TMsvId aFolderId2, 
       
   297 								   			 const CMsvEntrySelection& aSelection )
       
   298 	{
       
   299 	
       
   300 	
       
   301 	
       
   302 	WRITELOG("ENTER CMessageMonitorPlugin::HandleMsgMovedL");
       
   303 	
       
   304 	TInt count( aSelection.Count() );
       
   305 	// cycle through every message in the CMsvEntrySelection
       
   306 	for ( TInt i=0; i < count; ++i )
       
   307 		{
       
   308 		TMsvId msgId = aSelection[i];
       
   309 		WRITELOG1("msgId: %d", msgId );					
       
   310 		
       
   311 		TMsvId service = 0;
       
   312         TMsvEntry entry;
       
   313 		TInt err = iMsvSession->GetEntry( msgId, service, entry );
       
   314 
       
   315         // if we have a message and it has been moved from the outbox to the sent 
       
   316         // items, and it is a valid message
       
   317         if ( ( !err ) &&
       
   318             ( KUidMsvMessageEntry == entry.iType ) &&
       
   319             ( KMsvSentEntryId == aFolderId1 ) &&
       
   320             ( KMsvGlobalOutBoxIndexEntryId == aFolderId2 ) && 
       
   321             ( IsValidMessageTypeL( entry, *iMsvSession ) ) )
       
   322             {            
       
   323             HBufC* uri = CreateUriL( entry.iMtm, msgId );
       
   324             CleanupStack::PushL( uri );
       
   325             CHarvesterData* hd = CHarvesterData::NewL( uri );
       
   326             CleanupStack::Pop( uri );
       
   327             hd->SetEventType( EHarvesterAdd );
       
   328             hd->SetTakeSnapshot( EFalse );
       
   329             hd->SetBinary( EFalse );
       
   330             iMonitorPluginObserver->MonitorEvent( hd );
       
   331             }          
       
   332         }
       
   333 	WRITELOG("END CMessageMonitorPlugin::HandleMsgMovedL");	        
       
   334 	}
       
   335 
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CMessageMonitorPlugin::HandleMsgCreatedChangedL
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 void CMessageMonitorPlugin::HandleMsgCreatedChangedL( const CMsvEntrySelection& aSelection,
       
   342                                           	   		  const TMsvId aFolderId,
       
   343                                           	          const TMsvSessionEvent aEvent )
       
   344 	{
       
   345 	WRITELOG("ENTER CMessageMonitorPlugin::HandleMsgCreatedChangedL");
       
   346 	
       
   347 	TInt count( aSelection.Count() );
       
   348 	
       
   349     // cycle through every message in the CMsvEntrySelection
       
   350 	for ( TInt i=0; i < count; ++i )
       
   351 		{
       
   352 		// extract the message server entry from the parameters
       
   353 		TMsvId msgId = aSelection[i];
       
   354 		WRITELOG1("msgId: %d", msgId );					
       
   355 		
       
   356         TMsvId service = 0;
       
   357         TMsvEntry entry;
       
   358 		TInt err = iMsvSession->GetEntry( msgId, service, entry );
       
   359 
       
   360 		WRITELOG1("HandleMsgCreatedChangedL error: %d ", err);		
       
   361 		
       
   362 		THarvesterEventType event = 
       
   363 						( aEvent == EMsvEntriesCreated ) || entry.Unread() ? 
       
   364 						  	  								 EHarvesterAdd : 
       
   365 															 EHarvesterEdit;
       
   366 		
       
   367         
       
   368         // ... we only want to process message entries, that are in the inbox
       
   369         // ... and are valid for adding to Mds.
       
   370         if ( ( !err ) && 
       
   371             ( KUidMsvMessageEntry == entry.iType ) && 
       
   372             ( KMsvGlobalInBoxIndexEntryId == aFolderId ) &&
       
   373             ( entry.Visible() && entry.Complete() && !entry.InPreparation() ) &&
       
   374             ( IsValidMessageTypeL( entry, *iMsvSession ) ) )
       
   375             {
       
   376             HBufC* uri = CreateUriL( entry.iMtm, msgId );    
       
   377             CleanupStack::PushL( uri );
       
   378             CHarvesterData* hd = CHarvesterData::NewL( uri );
       
   379             CleanupStack::Pop( uri );
       
   380             hd->SetEventType( event );
       
   381             hd->SetBinary( EFalse );
       
   382             hd->SetTakeSnapshot( EFalse );
       
   383 			iMonitorPluginObserver->MonitorEvent( hd );
       
   384             }
       
   385         }
       
   386 	WRITELOG("END CMessageMonitorPlugin::HandleMsgCreatedChangedL");	        
       
   387 	}
       
   388 	
       
   389 // -----------------------------------------------------------------------------
       
   390 // CMessageMonitorPlugin::HandleMsgDeletedL
       
   391 // -----------------------------------------------------------------------------
       
   392 //
       
   393 void CMessageMonitorPlugin::HandleMsgDeletedL( const CMsvEntrySelection& aSelection )
       
   394 	{
       
   395 	WRITELOG("ENTER CMessageMonitorPlugin::HandleMsgDeletedL");
       
   396 	
       
   397 	TInt count( aSelection.Count() );	
       
   398 	// cycle through every message in the CMsvEntrySelection
       
   399 	for( TInt i = 0; i < count; ++i )
       
   400 		{
       
   401 		TMsvId msgId = aSelection[i];	
       
   402 		WRITELOG1("msgId: %d", msgId );
       
   403 		HBufC* uri = CreateUriL( KUidMsgTypeSMS, msgId );
       
   404 		CleanupStack::PushL( uri );
       
   405 		RemoveObjectL( *uri );
       
   406 		CleanupStack::PopAndDestroy( uri );
       
   407 		}
       
   408 	WRITELOG("END CMessageMonitorPlugin::HandleMsgDeletedL");	
       
   409 	}	
       
   410 
       
   411 // ---------------------------------------------------------------------------
       
   412 // CHarvesterMessagePluginAO::RemoveObjectL
       
   413 // ---------------------------------------------------------------------------
       
   414 //
       
   415 void CMessageMonitorPlugin::RemoveObjectL( const TDesC& aUri )
       
   416 	{
       
   417     WRITELOG( "CMessageMonitorPlugin::RemoveObjectL enter" );
       
   418 	CMdEObject* mdeObject = iMdeSession->GetObjectL( aUri );
       
   419 
       
   420     // check if in mde
       
   421     if ( mdeObject )
       
   422     	{
       
   423     	TItemId objectId = mdeObject->Id();
       
   424 	    delete mdeObject;
       
   425 	    mdeObject = NULL;
       
   426 	    
       
   427 		TTime time( 0 );
       
   428 		CMdENamespaceDef& nsDef = iMdeSession->GetDefaultNamespaceDefL();
       
   429 		CMdEEventDef& eventDef = nsDef.GetEventDefL( MdeConstants::Events::KDeleted );
       
   430 	
       
   431 		iMdeSession->RemoveObjectL( aUri, &nsDef );
       
   432 		WRITELOG1( "aUri: %S", &aUri );							
       
   433 		time.UniversalTime();
       
   434 		CMdEEvent* event =  iMdeSession->NewEventL( eventDef, objectId, time,NULL,NULL );
       
   435 		
       
   436 		iMdeSession->AddEventL( *event );
       
   437 		delete event;
       
   438     	}
       
   439     
       
   440     WRITELOG( "CMessageMonitorPlugin::RemoveObjectL return" );
       
   441 	}
       
   442 
       
   443 // -----------------------------------------------------------------------------
       
   444 // CMessageMonitorPlugin::IsValidMessageTypeL
       
   445 // -----------------------------------------------------------------------------
       
   446 //	
       
   447 TBool CMessageMonitorPlugin::IsValidMessageTypeL( const TMsvEntry& aEntry,
       
   448                                                   CMsvSession& /*aSession*/)
       
   449 	{
       
   450 	WRITELOG("ENTER CMessageMonitorPlugin::IsValidMessageTypeL");
       
   451 	
       
   452     TBool isValidMsg( EFalse );
       
   453 
       
   454     // Mms message
       
   455 	if ( aEntry.iMtm.iUid == KUidMsgTypeMultimedia.iUid )
       
   456         {	
       
   457         isValidMsg = ETrue;
       
   458         }
       
   459     // check if this is a BIO message, if so exclude it from Hoover
       
   460     else if ( aEntry.iBioType != 0 )
       
   461         {
       
   462         isValidMsg = EFalse;
       
   463         }
       
   464     // else if we have a normal text based SMS it is valid    
       
   465 	else if ( aEntry.iMtm.iUid == KUidMsgTypeSMS.iUid )
       
   466 		{
       
   467 		isValidMsg = ETrue;
       
   468 		}
       
   469 	WRITELOG1( "isValidMsg: %d" , isValidMsg );					
       
   470 		
       
   471 	WRITELOG("END CMessageMonitorPlugin::IsValidMessageTypeL");		
       
   472         
       
   473 	return isValidMsg;
       
   474 	}	
       
   475 	
       
   476 // -----------------------------------------------------------------------------
       
   477 // CMessageMonitorPlugin::CreateUriL
       
   478 // -----------------------------------------------------------------------------
       
   479 //	
       
   480 HBufC* CMessageMonitorPlugin::CreateUriL( const TUid& /*aMsgTypeUid*/, const TMsvId aMsgId )
       
   481 	{
       
   482 	WRITELOG("ENTER CMessageMonitorPlugin::CreateUriL");
       
   483 	
       
   484     HBufC* uri = HBufC::NewL( KUriLength );
       
   485 	TPtr ptrUri = uri->Des();
       
   486 	ptrUri.Format( KMsgURI, aMsgId );	
       
   487 	
       
   488 	WRITELOG("END CMessageMonitorPlugin::CreateUriL");	
       
   489 		
       
   490 	return uri;
       
   491 	}
       
   492 
       
   493 // End Of File