harvester/server/src/ondemandao.cpp
changeset 0 c53acadfccc6
child 40 910a23996aa0
child 54 a3cc46f37772
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2008-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 #include "ondemandao.h"
       
    17 #include "mdeharvestersession.h"
       
    18 #include "harvesterdata.h"
       
    19 #include "harvesterlog.h"
       
    20 #include "harvesterplugin.h"
       
    21 #include "harvesterplugininfo.h"
       
    22 #include "harvesterpluginfactory.h"
       
    23 
       
    24 COnDemandAO::COnDemandAO() :
       
    25 	CActive( CActive::EPriorityUserInput )
       
    26 	{
       
    27 	WRITELOG("COnDemandAO::COnDemandAO");
       
    28 	}
       
    29 
       
    30 COnDemandAO* COnDemandAO::NewLC( CMdESession& aSession, MMonitorPluginObserver& aObserver,
       
    31 		CHarvesterPluginFactory& aPluginFactory, RPointerArray<CHarvesterData>* aPhArray )
       
    32 	{
       
    33 	WRITELOG("COnDemandAO::NewLC");
       
    34 	COnDemandAO* self = new ( ELeave ) COnDemandAO();
       
    35 	CleanupStack::PushL ( self);
       
    36 	self->ConstructL ( aSession, aObserver, aPluginFactory, aPhArray );
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 COnDemandAO* COnDemandAO::NewL( CMdESession& aSession, MMonitorPluginObserver& aObserver,
       
    41 		CHarvesterPluginFactory& aPluginFactory, RPointerArray<CHarvesterData>* aPhArray )
       
    42 	{
       
    43 	WRITELOG("COnDemandAO::NewL");
       
    44 	COnDemandAO* self = COnDemandAO::NewLC ( aSession, aObserver, aPluginFactory, aPhArray );
       
    45 	CleanupStack::Pop (); // self;
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 void COnDemandAO::ConstructL( CMdESession& aSession, MMonitorPluginObserver& aObserver,
       
    50 		CHarvesterPluginFactory& aPluginFactory, RPointerArray<CHarvesterData>* aPhArray )
       
    51 	{
       
    52 	WRITELOG("COnDemandAO::ConstructL");
       
    53 	iMdEHarvesterSession = CMdEHarvesterSession::NewL ( aSession);
       
    54 	iObserver = &aObserver;
       
    55 	iPluginFactory = &aPluginFactory;
       
    56 	iReadyPhArray = aPhArray;
       
    57 	CActiveScheduler::Add ( this ); // Add to scheduler
       
    58 	}
       
    59 
       
    60 COnDemandAO::~COnDemandAO()
       
    61 	{
       
    62 	WRITELOG("COnDemandAO::~COnDemandAO");
       
    63 	Cancel (); // Cancel any request, if outstanding
       
    64 	if( iMdEHarvesterSession )
       
    65 		{
       
    66 		delete iMdEHarvesterSession;
       
    67 		}
       
    68 	// Delete instance variables if any
       
    69 	}
       
    70 
       
    71 void COnDemandAO::DoCancel()
       
    72 	{
       
    73 	WRITELOG("COnDemandAO::DoCancel");
       
    74 	iMdEHarvesterSession->CancelHarvestingPrioritizationObserver ();
       
    75 	}
       
    76 
       
    77 void COnDemandAO::StartL()
       
    78 	{
       
    79 	WRITELOG("COnDemandAO::StartL");
       
    80 	// Cancel any request, just to be sure
       
    81 	Cancel ();
       
    82 	iMdEHarvesterSession->SetHarvestingPrioritizationChunkL ( 16384 );
       
    83 	WaitHarvestingRequest ();
       
    84 	}
       
    85 
       
    86 void COnDemandAO::RunL()
       
    87 	{
       
    88 	WRITELOG("COnDemandAO::RunL");
       
    89 	if( iStatus.Int() != KErrNotFound )
       
    90 		{
       
    91 		User::LeaveIfError ( iStatus.Int ());
       
    92 		
       
    93 		TInt count = iMdEHarvesterSession->HarvestingPrioritizationUriCountL();
       
    94 		if( count > 0 )
       
    95 			{
       
    96 			RPointerArray<CHarvesterPluginInfo>& pluginInfos = iPluginFactory->GetPluginInfos();
       
    97 			TInt plugins = pluginInfos.Count();
       
    98 
       
    99 			for (TInt i = count; --i >= 0;)
       
   100 				{
       
   101 				HBufC* uri = iMdEHarvesterSession->HarvestingPrioritizationUriL( i );
       
   102 				CleanupStack::PushL( uri );
       
   103 				TBool found = EFalse;
       
   104 
       
   105 				for ( TInt j = 0; j < plugins && !found; j++ )
       
   106 					{
       
   107 					RPointerArray<CHarvesterData>& queue = pluginInfos[j]->iQueue;
       
   108 					TInt queueSize = queue.Count();
       
   109 					for ( TInt k = 0; k < queueSize && !found; k++ )
       
   110 						{				
       
   111 						if ( queue[k]->Uri().CompareF( *uri ) == 0 )
       
   112 							{
       
   113 							WRITELOG2("COnDemandAO::RunL URI %S found in plugin %d queue", uri, j);
       
   114 							CHarvesterData* hd = queue[k];
       
   115 							hd->SetEventType( EHarvesterEdit );
       
   116 							hd->SetObjectType( EFastHarvest );
       
   117 							queue.Remove( k );
       
   118 							queue.Insert( hd, 0 );
       
   119 							found = ETrue;
       
   120 							}							
       
   121 						}
       
   122 					}
       
   123 
       
   124 				const TInt readyPhArraySize = iReadyPhArray->Count();
       
   125 				for ( TInt j = 0; j < readyPhArraySize && !found; j++ )
       
   126 					{
       
   127 					if ( (*iReadyPhArray)[j]->Uri().CompareF( *uri ) == 0 )
       
   128 						{
       
   129 						WRITELOG1("COnDemandAO::RunL URI %S found in ph array", uri);
       
   130 						CHarvesterData* hd = (*iReadyPhArray)[j];
       
   131 						hd->SetEventType( EHarvesterEdit );
       
   132 						hd->SetObjectType( EFastHarvest );
       
   133 						iReadyPhArray->Remove( j );
       
   134 						iReadyPhArray->Insert( hd, 0 );
       
   135 						found = ETrue;
       
   136 						}
       
   137 					}
       
   138 
       
   139 				if ( !found )
       
   140 					{
       
   141 					WRITELOG1("COnDemandAO::RunL URI %S wasn't found in placeholder queue.", uri);
       
   142 					
       
   143 					CMdESession& session = iMdEHarvesterSession->SessionRef();
       
   144 					TMdEObject object;
       
   145 					TRAPD( err, session.CheckObjectL( object, *uri ));
       
   146 					if ( err == KErrNone && object.Placeholder() )
       
   147 						{
       
   148 						CHarvesterData* hd = CHarvesterData::NewL( uri );
       
   149 						hd->SetEventType( EHarvesterEdit );
       
   150 						hd->SetObjectType( EFastHarvest );
       
   151 						iObserver->MonitorEvent( hd );
       
   152 						CleanupStack::Pop( uri );
       
   153 						}
       
   154 					else
       
   155 						{
       
   156 						CleanupStack::PopAndDestroy( uri );
       
   157 						}
       
   158 					}
       
   159 				else
       
   160 					{
       
   161 					CleanupStack::PopAndDestroy( uri );
       
   162 					}
       
   163 				}
       
   164 			}
       
   165 		}
       
   166 
       
   167 	WaitHarvestingRequest();
       
   168 	}
       
   169 
       
   170 TInt COnDemandAO::RunError( TInt /*aError*/ )
       
   171 	{
       
   172 	return KErrNone;
       
   173 	}
       
   174 
       
   175 void COnDemandAO::WaitHarvestingRequest()
       
   176 	{
       
   177 	WRITELOG("COnDemandAO::WaitHarvestingRequest");
       
   178 	iStatus = KRequestPending;
       
   179 	iMdEHarvesterSession->AddHarvestingPrioritizationObserver( iStatus );
       
   180 	// Tell scheduler a request is active
       
   181 	SetActive();
       
   182 	}