harvester/composerplugins/imagecomposer/src/imagepresentobserver.cpp
changeset 0 c53acadfccc6
child 14 646a02f170b9
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:  image composer listener for setting pending image objects present
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "imagepresentobserver.h"
       
    20 #include "harvesterlog.h"
       
    21 #include "mdeconstants.h"
       
    22 #include "composerimageplugin.h"
       
    23 #include "mdcserializationbuffer.h"
       
    24 #include "mdsutils.h"
       
    25 #include "harvestercommon.h"
       
    26 
       
    27 using namespace MdeConstants;
       
    28 
       
    29 const TInt KBatchSize = 100;
       
    30 
       
    31 CImagePresentObserver::CImagePresentObserver() : 
       
    32 		CActive( KHarvesterPriorityComposerPlugin ),
       
    33 		iDefaultNamespace( NULL ),
       
    34 		iSession( NULL ),
       
    35 		iMdEHarvesterSession( NULL ),
       
    36 		iComposer( NULL ),
       
    37 		iPendingPckWrapper( NULL ),
       
    38 		iNextRequest( ERequestIdle ),
       
    39 		iPendingCount( 0 ),
       
    40 		iPendingBuffer( NULL ),
       
    41 		iStarted( EFalse )
       
    42 	{
       
    43 	// No implementation required
       
    44 	}
       
    45 
       
    46 CImagePresentObserver::~CImagePresentObserver()
       
    47 	{
       
    48 	if( iSession )
       
    49 	    {
       
    50 	    TRAP_IGNORE( iSession->RemoveObjectPresentObserverL( *this ) );
       
    51 	    }
       
    52 	
       
    53 	CleanUp();
       
    54 	}
       
    55 
       
    56 CImagePresentObserver* CImagePresentObserver::NewL( CMdESession* aSession, CComposerImagePlugin* aComposer, 
       
    57 		CMdEHarvesterSession* aMdEHarvesterSession )
       
    58 	{
       
    59 	WRITELOG( "CImagePresentObserver::NewL start" );
       
    60 	CImagePresentObserver* self = new (ELeave)CImagePresentObserver();
       
    61 	CleanupStack::PushL(self);
       
    62 	self->ConstructL( aSession, aComposer, aMdEHarvesterSession );
       
    63 	CleanupStack::Pop( self ); 
       
    64 	WRITELOG( "CImagePresentObserver::NewL end" );
       
    65 	return self;
       
    66 	}
       
    67 
       
    68 void CImagePresentObserver::ConstructL( CMdESession* aSession, CComposerImagePlugin* aComposer, 
       
    69 		CMdEHarvesterSession* aMdEHarvesterSession )
       
    70 	{
       
    71     WRITELOG( "CImagePresentObserver::ConstructL start" );
       
    72     User::LeaveIfNull( iSession = aSession );
       
    73     User::LeaveIfNull( iComposer = aComposer );
       
    74     
       
    75     iDefaultNamespace = &iSession->GetDefaultNamespaceDefL();
       
    76 
       
    77     iSession->AddObjectPresentObserverL( *this );
       
    78     iMdEHarvesterSession = aMdEHarvesterSession;
       
    79     
       
    80     iStarted = EFalse;
       
    81     
       
    82     CActiveScheduler::Add( this );
       
    83 
       
    84     WRITELOG( "CImagePresentObserver::ConstructL end" );
       
    85 	}
       
    86 
       
    87 
       
    88 void CImagePresentObserver::HandleObjectPresentNotification(CMdESession& /*aSession*/, 
       
    89 		TBool aPresent, const RArray<TItemId>& aObjectIdArray)
       
    90 	{
       
    91 	if ( aPresent )
       
    92 		{
       
    93 		if ( !iStarted )
       
    94 			{
       
    95 			CMdEObjectDef* imageObjDef = NULL;
       
    96 			TRAP_IGNORE( imageObjDef = &iDefaultNamespace->GetObjectDefL( 
       
    97 					MdeConstants::Image::KImageObject ));
       
    98 			
       
    99 			iPendingCount = iMdEHarvesterSession->GetPendingCount( imageObjDef );
       
   100 			SetNextRequest( ERequestWaitPendingCount );
       
   101 			iStarted = ETrue;
       
   102 			}
       
   103 		TInt count = aObjectIdArray.Count();
       
   104 		for (TInt i = 0; i < count; i++ )
       
   105 			{
       
   106 			iPresentObjectIds.Append( aObjectIdArray[i] );
       
   107 			}
       
   108 		}
       
   109 	}
       
   110 
       
   111 
       
   112 void CImagePresentObserver::RunL()
       
   113 	{
       
   114 	switch ( iNextRequest )
       
   115 		{
       
   116 		case ERequestIdle:
       
   117 			{
       
   118 			if( iComposeObjectIds.Count() > 0 )
       
   119 				{
       
   120 				iComposer->HandlePendingObjects( iComposeObjectIds );			
       
   121 				iComposeObjectIds.Reset();
       
   122 				}
       
   123 			iPendingObjectIds.Reset();
       
   124 			iPresentObjectIds.Reset();
       
   125 			delete iPendingBuffer;
       
   126 			iPendingBuffer = NULL;
       
   127 			iStarted = EFalse;
       
   128 			break;
       
   129 			}
       
   130 		
       
   131 		case ERequestWaitPendingCount:
       
   132 			{
       
   133 			if ( iPendingCount > 0 )
       
   134 				{
       
   135 				CMdEObjectDef& imageObjDef = iDefaultNamespace->GetObjectDefL( 
       
   136 						MdeConstants::Image::KImageObject );
       
   137 
       
   138 				iPendingBuffer = CMdCSerializationBuffer::NewL( iPendingCount * 
       
   139 						sizeof(TItemId) );
       
   140 			
       
   141 				const TInt result = iMdEHarvesterSession->GetPending( &imageObjDef,
       
   142 						iPendingCount, *iPendingBuffer );
       
   143 				
       
   144 				SetNextRequest( ERequestWaitPending );
       
   145 				}
       
   146 			else
       
   147 				{
       
   148 				SetNextRequest( ERequestIdle );
       
   149 				}
       
   150 			break;
       
   151 			}
       
   152 			
       
   153 		case ERequestWaitPending:
       
   154 			{
       
   155 			if ( iPendingBuffer )
       
   156 				{
       
   157 				DeserializeArrayL( iPendingBuffer->Buffer(), iPendingObjectIds );
       
   158 				}
       
   159 			
       
   160 			SetNextRequest( ERequestProcess );
       
   161 			break;
       
   162 			}
       
   163 		
       
   164 		case ERequestProcess:
       
   165 			{
       
   166 			if( iPresentObjectIds.Count() > 0 && iPendingObjectIds.Count() > 0 )
       
   167 				{
       
   168 				TItemId itemId = iPresentObjectIds[0];
       
   169 				iPresentObjectIds.Remove( 0 );
       
   170 				
       
   171 				TInt found = iPendingObjectIds.Find( itemId );
       
   172 				if ( found != KErrNotFound )
       
   173 					{
       
   174 					iPendingObjectIds.Remove( found );
       
   175 					iComposeObjectIds.Append( itemId );
       
   176 					}
       
   177 				}
       
   178 			else 
       
   179 				{
       
   180 				SetNextRequest( ERequestIdle );
       
   181 				
       
   182 				iPendingObjectIds.Compress();
       
   183 				iPresentObjectIds.Compress();
       
   184 				
       
   185 				break;
       
   186 				}
       
   187 
       
   188 			if( iComposeObjectIds.Count() > KBatchSize )
       
   189 				{
       
   190 				iComposer->HandlePendingObjects( iComposeObjectIds );		
       
   191 				iComposeObjectIds.Reset();
       
   192 				}
       
   193 			
       
   194 			SetNextRequest ( ERequestProcess );	
       
   195 			break;
       
   196 			}
       
   197 		}
       
   198 	}
       
   199 
       
   200 void CImagePresentObserver::DoCancel()
       
   201 	{
       
   202 	}
       
   203 
       
   204 TInt CImagePresentObserver::RunError( TInt /*aError*/ )
       
   205 	{
       
   206 	return KErrNone;
       
   207 	}
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // SetNextRequest
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CImagePresentObserver::SetNextRequest( TRequest aRequest )
       
   214     {
       
   215     iNextRequest = aRequest;
       
   216             
       
   217     if ( !IsActive() )
       
   218         {
       
   219         iStatus = KRequestPending;
       
   220         SetActive();
       
   221         TRequestStatus* ptrStatus = &iStatus;
       
   222         User::RequestComplete( ptrStatus, KErrNone );
       
   223         }
       
   224     }
       
   225 
       
   226 
       
   227 void CImagePresentObserver::CleanUp()
       
   228 	{
       
   229 	iPendingObjectIds.Close();
       
   230 	iPresentObjectIds.Close();
       
   231 	iComposeObjectIds.Close();
       
   232 	delete iPendingBuffer;
       
   233 	iPendingBuffer = NULL;
       
   234 	}
       
   235