harvester/monitorplugins/messageplugin/src/messagescannerao.cpp
changeset 0 c53acadfccc6
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 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 #include <msvstd.h>
       
    18 #include <msvfind.h>
       
    19 #include <msvapi.h>
       
    20 #include <msvids.h>
       
    21 #include "mdesession.h"
       
    22 #include "mdeconstants.h"
       
    23 #include "mdenamespacedef.h"
       
    24 #include "messagescannerao.h"
       
    25 #include "harvesterlog.h"
       
    26 #include "messagemonitorplugin.h"
       
    27 #include "clientkeywatcherkeys.h"
       
    28 
       
    29 CMessageScannerAO* CMessageScannerAO::NewL( CMsvSession* aMsvSession, 
       
    30 		CMdESession* aMdeSession,
       
    31 		MMonitorPluginObserver* aMonitorPluginObserver )
       
    32 	{
       
    33 	CMessageScannerAO* self = new (ELeave) CMessageScannerAO( 
       
    34 			aMsvSession, aMdeSession, aMonitorPluginObserver );
       
    35 	CleanupStack::PushL( self );
       
    36 	self->ConstructL();
       
    37 	CleanupStack::Pop( self );
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 CMessageScannerAO::CMessageScannerAO( CMsvSession* aMsvSession, 
       
    42 		CMdESession* aMdeSession, 
       
    43 		MMonitorPluginObserver* aMonitorPluginObserver ) :
       
    44 		CActive( CActive::EPriorityStandard ),
       
    45 		iFindOperation( NULL ), iObjectQuery( NULL )
       
    46 	{
       
    47 	CActiveScheduler::Add( this );
       
    48 	iMsvSession = aMsvSession;
       
    49 	iMdeSession = aMdeSession;
       
    50 	iMonitorPluginObserver = aMonitorPluginObserver;
       
    51 	iState = EScannerIdle;
       
    52 	}
       
    53 
       
    54 CMessageScannerAO::~CMessageScannerAO()
       
    55 	{
       
    56 	Cancel();
       
    57 	Cleanup();
       
    58 	}
       
    59 
       
    60 void CMessageScannerAO::StartL()
       
    61 	{
       
    62 	WRITELOG("CMessageScannerAO::Start()");
       
    63 	_LIT( KEmpty, "" );
       
    64 	iFindOperation = CMsvFindOperation::FindInChildrenL( *iMsvSession, KEmpty, 
       
    65 			KMsvRootIndexEntryId, KMsvMessagePartNone, iStatus );
       
    66 	SetActive();
       
    67 	iState = EScannerRunning;
       
    68 	WRITELOG("CMessageScannerAO::Start() - ends");
       
    69 	}
       
    70 		
       
    71 void CMessageScannerAO::ConstructL()
       
    72 	{
       
    73 	WRITELOG("CMessageScannerAO::ConstructL()");
       
    74 	User::LeaveIfError( iProperty.Attach( KPSRestoreWatcherCategory,
       
    75 			KPSRestoreWatcherClientsKey, EOwnerThread ) );
       
    76 	
       
    77 	TInt registeredClients = 0;
       
    78 	iProperty.Get( registeredClients );
       
    79 	TInt error = iProperty.Set( registeredClients + 1 );
       
    80 	
       
    81 	WRITELOG1("CMessageScannerAO::ConstructL() - error: %d", error);
       
    82 	}
       
    83 
       
    84 void CMessageScannerAO::RunL()
       
    85 	{
       
    86 	WRITELOG("CMessageScannerAO::RunL()");
       
    87 	if ( iStatus.Int() != KErrNone )
       
    88 		{
       
    89 #ifdef _DEBUG
       
    90 		WRITELOG1("CMessageScannerAO::RunL() . error %d", iStatus.Int());
       
    91 #endif
       
    92 		return;
       
    93 		}
       
    94 	
       
    95 	switch ( iState )
       
    96 		{
       
    97 		case EScannerRunning:
       
    98 			{
       
    99 			QueryAllMessagesL();
       
   100 			break;
       
   101 			}
       
   102 		case EScannerFinished:
       
   103 			{
       
   104 			Cleanup();
       
   105 			break;
       
   106 			}
       
   107 		}
       
   108 	}
       
   109 
       
   110 void CMessageScannerAO::DoCancel()
       
   111 	{
       
   112 	iFindOperation->Cancel();
       
   113 	iProperty.Cancel();
       
   114 	}
       
   115 
       
   116 #ifdef _DEBUG
       
   117 TInt CMessageScannerAO::RunError( TInt aError )
       
   118 #else
       
   119 TInt CMessageScannerAO::RunError( TInt /*aError*/ )
       
   120 #endif
       
   121 	{
       
   122 	WRITELOG1( "CMessageScannerAO::RunError - error: %d", aError );
       
   123 	return KErrNone;
       
   124 	}
       
   125 
       
   126 void CMessageScannerAO::QueryAllMessagesL()
       
   127 	{
       
   128 	WRITELOG("CMessageScannerAO::QueryAllMessagesL()");
       
   129 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   130 	CMdEObjectDef& messageDef = namespaceDef.GetObjectDefL( MdeConstants::Message::KMessageObject );
       
   131 	
       
   132 	iObjectQuery = iMdeSession->NewObjectQueryL( namespaceDef, messageDef, this );
       
   133 	iObjectQuery->SetResultMode( EQueryResultModeItem );
       
   134 	
       
   135 	iObjectQuery->FindL();
       
   136 	WRITELOG("CMessageScannerAO::QueryAllMessagesL() - ends");
       
   137 	}
       
   138 
       
   139 void CMessageScannerAO::HandleQueryNewResults( CMdEQuery& /*aQuery*/, TInt /*aFirstNewItemIndex*/,
       
   140 		TInt /*aNewItemCount*/ )
       
   141 	{
       
   142 	}
       
   143 
       
   144 void CMessageScannerAO::HandleQueryCompleted( CMdEQuery& aQuery, TInt /*aError*/ )
       
   145 	{
       
   146 	WRITELOG("CMessageScannerAO::HandleQueryCompleted()");
       
   147 	RArray<TMessage> messages;
       
   148 	TInt error = KErrNone;
       
   149 	
       
   150 	if ( aQuery.Count() > 0 )
       
   151 		{
       
   152 #ifdef _DEBUG
       
   153 		WRITELOG1("CMessageScannerAO::HandleQueryCompleted() - aQuery count %d", aQuery.Count());
       
   154 #endif
       
   155 		TRAP( error, ProcessMessagesL( aQuery, messages ) );
       
   156 		}
       
   157 
       
   158 	if ( error == KErrNone )
       
   159 		{
       
   160 		TRAP_IGNORE( ScanMessagesL( messages ) );
       
   161 		}
       
   162 	
       
   163 	messages.Close();
       
   164 	Unregister();
       
   165 	iState = EScannerFinished;
       
   166 	
       
   167 	SetActive();
       
   168 	TRequestStatus* pStatus = &iStatus;
       
   169     User::RequestComplete( pStatus, KErrNone );
       
   170 	}
       
   171 
       
   172 void CMessageScannerAO::ProcessMessagesL( CMdEQuery& aQuery, RArray<TMessage>& aMessages )
       
   173 	{
       
   174 	_LIT( KPeriod, "." );
       
   175 
       
   176 	CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   177 	CMdEObjectDef& messageDef = namespaceDef.GetObjectDefL( MdeConstants::Message::KMessageObject );
       
   178 	CMdEPropertyDef& creationDateDef = messageDef.GetPropertyDefL( 
       
   179 			MdeConstants::Object::KCreationDateProperty );
       
   180 
       
   181 	TInt count = aQuery.Count();
       
   182 	
       
   183 	for (TInt i = 0; i < count; i++ )
       
   184 		{
       
   185 		CMdEObject& object = static_cast<CMdEObject&>( aQuery.ResultItem( i ) );
       
   186 		TInt index = object.Uri().Find( KPeriod );
       
   187 		if ( index != KErrNotFound )
       
   188 			{
       
   189 			TLex16 lex( object.Uri().Left( index ) );
       
   190 			TMessage message;
       
   191 			message.iMdeId = object.Id();
       
   192 
       
   193 			CMdEProperty* creationDateProp = NULL;
       
   194 			object.Property( creationDateDef, creationDateProp, 0 );
       
   195 			message.iCreationDate = creationDateProp->TimeValueL();
       
   196 
       
   197 			TInt error = lex.Val( message.iMsvId );
       
   198 			if ( error == KErrNone )
       
   199 				{
       
   200 				aMessages.Append( message );
       
   201 				}
       
   202 			}
       
   203 		}
       
   204 	}
       
   205 
       
   206 void CMessageScannerAO::ScanMessagesL( RArray<TMessage>& aMessages )
       
   207 	{
       
   208 	WRITELOG("CMessageScannerAO::ScanMessagesL()");
       
   209 	const CMsvFindResultSelection& findResults = iFindOperation->GetFindResult();
       
   210 	CMdENamespaceDef& defaultNamespace = iMdeSession->GetDefaultNamespaceDefL();
       
   211 	TInt msvCount = findResults.Count();
       
   212 	
       
   213 	for ( TInt i = 0; i < msvCount; i++ )
       
   214 		{
       
   215 		WRITELOG1("CMessageScannerAO::ScanMessagesL() - MSV item %d", i);
       
   216 		TBool idMatched = EFalse;
       
   217 		for ( TInt j = 0; j < aMessages.Count(); j++ )
       
   218 			{
       
   219 			WRITELOG1("CMessageScannerAO::ScanMessagesL() - MDE item %d", j);
       
   220 			if ( findResults[i].iId == aMessages[j].iMsvId )
       
   221 				{
       
   222 				WRITELOG("CMessageScannerAO::ScanMessagesL() - id matches");
       
   223 				idMatched = ETrue;
       
   224 				CMsvEntry* e = iMsvSession->GetEntryL( findResults[i].iId );
       
   225 				if ( !e )
       
   226 					{
       
   227 					WRITELOG("CMessageScannerAO::ScanMessagesL() - message entry is NULL!?");
       
   228 					continue;
       
   229 					}
       
   230 				
       
   231 				CleanupStack::PushL( e );
       
   232 				const TMsvEntry& entry = e->Entry();
       
   233 				WRITELOG("CMessageScannerAO::ScanMessagesL() - comparing dates");
       
   234 				if ( entry.iDate != aMessages[j].iCreationDate )
       
   235 					{
       
   236 					WRITELOG("CMessageScannerAO::ScanMessagesL() - creation dates don't match");
       
   237 					// Harvest the message with EHarvesterEdit event type.
       
   238 					HBufC* uri = CMessageMonitorPlugin::CreateUriL( entry.iMtm, entry.Id() );
       
   239 					CleanupStack::PushL( uri );
       
   240 					CHarvesterData* hd = CHarvesterData::NewL( uri );
       
   241 					CleanupStack::Pop( uri );
       
   242 					hd->SetEventType( EHarvesterEdit );
       
   243 					hd->SetTakeSnapshot( EFalse );
       
   244 					hd->SetBinary( EFalse );
       
   245 					iMonitorPluginObserver->MonitorEvent( hd );
       
   246 					}
       
   247 				
       
   248 				CleanupStack::PopAndDestroy( e );
       
   249 				aMessages.Remove( j );
       
   250 				j--;
       
   251 				break;
       
   252 				}
       
   253 			}
       
   254 		
       
   255 		if ( !idMatched )
       
   256 			{
       
   257 			WRITELOG("CMessageScannerAO::ScanMessagesL() - harvesting new message");
       
   258 			// Harvest the message as new message.
       
   259 			CMsvEntry* e = iMsvSession->GetEntryL( findResults[i].iId );
       
   260 			if ( !e )
       
   261 				{
       
   262 				WRITELOG("CMessageScannerAO::ScanMessagesL() - message entry is NULL!?");
       
   263 				continue;
       
   264 				}
       
   265 			
       
   266 			CleanupStack::PushL( e );
       
   267 			const TMsvEntry& entry = e->Entry();
       
   268 			HBufC* uri = CMessageMonitorPlugin::CreateUriL( entry.iMtm, entry.Id() );
       
   269 			CleanupStack::PushL( uri );
       
   270 			CHarvesterData* hd = CHarvesterData::NewL( uri );
       
   271 			CleanupStack::Pop( uri );
       
   272 			hd->SetEventType( EHarvesterAdd );
       
   273 			hd->SetTakeSnapshot( EFalse );
       
   274 			hd->SetBinary( EFalse );
       
   275 			iMonitorPluginObserver->MonitorEvent( hd );
       
   276 			
       
   277 			CleanupStack::PopAndDestroy( e );
       
   278 			}
       
   279 		}
       
   280 	
       
   281 	// Remove extra messages in MdS db.
       
   282 	if ( aMessages.Count() > 0 )
       
   283 		{
       
   284 		WRITELOG1("CMessageScannerAO::ScanMessagesL() - removing extra messages from mde (%d)", 
       
   285 				aMessages.Count() );
       
   286 		RArray<TItemId> extraMessages;
       
   287 		CleanupClosePushL( extraMessages );
       
   288 		
       
   289 		for ( TInt i = 0; i < aMessages.Count(); i++ )
       
   290 			{
       
   291 			extraMessages.Append( aMessages[i].iMdeId );
       
   292 			}
       
   293 		
       
   294 		RArray<TItemId> results;
       
   295 		CleanupClosePushL( results );
       
   296 		iMdeSession->RemoveObjectsL( extraMessages, results, &defaultNamespace );
       
   297 		CleanupStack::PopAndDestroy( 2 ); // extramessages, results
       
   298 		}
       
   299 	}
       
   300 
       
   301 void CMessageScannerAO::Unregister()
       
   302 	{
       
   303 	TInt registeredClients = 0;
       
   304 	iProperty.Get( registeredClients );
       
   305 	WRITELOG1("CMessageScannerAO::Unregister() - registered clients: %d", registeredClients);
       
   306 	if ( registeredClients > 0 )
       
   307 		{
       
   308 		iProperty.Set( registeredClients - 1 );
       
   309 		}
       
   310 	}
       
   311 
       
   312 void CMessageScannerAO::Cleanup()
       
   313 	{
       
   314 	iProperty.Close();
       
   315 	delete iFindOperation;
       
   316 	iFindOperation = NULL;
       
   317 	delete iObjectQuery;
       
   318 	iObjectQuery = NULL;
       
   319 	}
       
   320