contextengine/src/contextengineao.cpp
changeset 0 c53acadfccc6
child 3 6752808b2036
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2007-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 <sbdefs.h>
       
    20 #include <e32property.h>
       
    21 
       
    22 #include "contextengineao.h"
       
    23 #include "contextengine.h"
       
    24 #include "contextsnapshotitem.h"
       
    25 #include "harvesterlog.h"
       
    26 #include "mdsutils.h"
       
    27 #include "harvesterdata.h"
       
    28 #include "propertywatcher.h" 
       
    29 #include "harvestercommon.h"
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // Default constructor for first phase construction.
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CContextEngineAO::CContextEngineAO( 
       
    36 		MContextInitializationObserver* aInitObserver,
       
    37 		MContextSnapshotObserver* aSnapshotObserver ) : 
       
    38 		CActive( KHarvesterPriorityContextEngine ), 
       
    39 		iInitializationObserver( aInitObserver ), 
       
    40 		iContextSnapshotObserver ( aSnapshotObserver ),
       
    41 		iUninitializedPluginsCount( 0 ),
       
    42 		iPluginSnapshotsLacking( 0 ),
       
    43 		iItem( NULL ),
       
    44 	    iState( EInitialization ),
       
    45 	    iInitializationError( KErrNone ),
       
    46 	    iSnapshotError( KErrNone ),
       
    47 	    iMdESession( NULL ),
       
    48 	    iPropertyWatcher( NULL ),
       
    49 	    iLastPlugin( EFalse )
       
    50     {
       
    51     WRITELOG( "CContextEngineAO::CContextEngineAO" ); // DEBUG INFO
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // Standard NewL.
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CContextEngineAO* CContextEngineAO::NewL(
       
    59     MContextInitializationObserver* aInitObserver,
       
    60     MContextSnapshotObserver* aSnapshotObserver )
       
    61     {
       
    62     WRITELOG( "CContextEngineAO::NewL" ); // DEBUG INFO
       
    63 
       
    64     CContextEngineAO* self = new (ELeave) CContextEngineAO( aInitObserver, aSnapshotObserver );
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL();
       
    67     CleanupStack::Pop( self );
       
    68     return self;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // 2nd phase construction.
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void CContextEngineAO::ConstructL()
       
    76     {
       
    77     WRITELOG( "CContextEngineAO::ConstructL" ); // DEBUG INFO
       
    78 
       
    79     CActiveScheduler::Add( this );
       
    80 
       
    81     iPropertyWatcher = CPropertyWatcher::GetInstanceL();
       
    82       
       
    83   	// Want to listen when backup/restore starts.
       
    84   	// Calls NotifyKeyL when key's state has changed. 
       
    85   	iPropertyWatcher->ListenKeyChangesL( 
       
    86   			KUidSystemCategory,
       
    87   			conn::KUidBackupRestoreKey, this );
       
    88       
       
    89     
       
    90     if ( !iInitializationObserver )
       
    91         {
       
    92         InitializeL();
       
    93         iState = EReady;
       
    94         }
       
    95     else  // same thing asyncronously
       
    96         {
       
    97         iState = EInitialization;
       
    98         SetActive();
       
    99         TRequestStatus* status = &iStatus;
       
   100         User::RequestComplete( status, KErrNone ); // "kick-start" this AO
       
   101         }
       
   102     }
       
   103 // ---------------------------------------------------------------------------
       
   104 // Destructor.
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 CContextEngineAO::~CContextEngineAO()
       
   108     {
       
   109     WRITELOG( "CContextEngineAO::~CContextEngineAO" ); // DEBUG INFO
       
   110 
       
   111     Cancel();
       
   112 
       
   113     MdsUtils::CleanupPtrArray<CContextPlugin>( &iPlugins );
       
   114 	
       
   115     if( iPropertyWatcher )
       
   116 		{
       
   117 		
       
   118 	 	iPropertyWatcher->StopListeningKeyChanges( 
       
   119 	  			KUidSystemCategory,
       
   120 	  			conn::KUidBackupRestoreKey, this );
       
   121 		
       
   122 		iPropertyWatcher->Delete(); // Release connection to TLS object.
       
   123 		}
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // Set MdeSession for context engine plugins.
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void CContextEngineAO::SetMdeSession( CMdESession* aSession )
       
   131     {
       
   132     iMdESession = aSession;
       
   133 
       
   134     for ( TInt i = 0; i < iPlugins.Count(); ++i )
       
   135         {
       
   136         iPlugins[i]->SetMdeSession( *aSession );
       
   137         }
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // Start taking a context snapshot (for multiple objects).
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CContextEngineAO::StartSnapshot( CContextSnapshotItem* aItem )
       
   145     {
       
   146     WRITELOG( "CContextEngineAO::StartSnapshot" ); // DEBUG INFO
       
   147     iItem = aItem;
       
   148     if ( !aItem ) return;
       
   149     if ( !aItem->GetItem() && !aItem->GetItemArray() )
       
   150         {
       
   151         // no items to do the snapshot for... stop.
       
   152         aItem->GetObserver()->ContextSnapshotStatus( KErrNone );
       
   153         }
       
   154 
       
   155     iSnapshotError = KErrNone;
       
   156     iPluginSnapshotsLacking = iPlugins.Count(); // reset, no snapshots taken yet
       
   157 
       
   158     if ( iPluginSnapshotsLacking <= 0 )
       
   159     	{
       
   160     	iState = EReady;
       
   161     	iContextSnapshotObserver->ContextSnapshotStatus( KErrNone );    
       
   162     	}
       
   163     iLastPlugin = EFalse;
       
   164     
       
   165     iState = ESnapshot;
       
   166     SetActive();
       
   167     TRequestStatus* status = &iStatus;
       
   168     User::RequestComplete( status, KErrNone ); // "kick-start" this AO
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // Return loaded plugin count.
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 TInt CContextEngineAO::PluginCount()
       
   176     {
       
   177     return iPlugins.Count();
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // From CActive.
       
   182 // Start/continue taking a context snapshot (for multiple objects).
       
   183 // Ask a snapshot from one plugin at a time.
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 void CContextEngineAO::RunL()
       
   187 	{
       
   188 #ifdef _DEBUG
       
   189     WRITELOG1( "CContextEngineAO::RunL iStatus: %d", iStatus.Int() ); // DEBUG INFO
       
   190 #endif
       
   191 
       
   192     if ( iState == EInitialization )
       
   193         {
       
   194         InitializeL();
       
   195         }
       
   196     else if ( iState == ESnapshot )
       
   197         {
       
   198         if ( iStatus.Int() != KErrNone )
       
   199             {
       
   200             return;
       
   201             }
       
   202 
       
   203         if ( iPluginSnapshotsLacking > 0 )
       
   204             {
       
   205             TInt i = iPlugins.Count() - iPluginSnapshotsLacking;
       
   206             if( iPluginSnapshotsLacking == 1 )
       
   207                 {
       
   208                 iLastPlugin = ETrue;
       
   209                 }
       
   210             if ( iItem->GetItemArray() ) 
       
   211                 {
       
   212                 iPlugins[i]->ContextSnapshot( *this, *iItem->GetItemArray() );  
       
   213                 }                               // CContextEngineAO is instance of
       
   214             else                                // MContextPluginObserver
       
   215                 {
       
   216                 iPlugins[i]->ContextSnapshot( *this, *iItem->GetItem() );
       
   217                 }
       
   218             iPluginSnapshotsLacking--;
       
   219             }
       
   220         }
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // From CActive.
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 #ifdef _DEBUG
       
   228 TInt CContextEngineAO::RunError( TInt aError )
       
   229 #else
       
   230 TInt CContextEngineAO::RunError( TInt )
       
   231 #endif
       
   232     {
       
   233     WRITELOG1( "CContextEngineAO::RunError with error code: %d", aError ); // DEBUG INFO
       
   234     return KErrNone;
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // From CActive. 
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 void CContextEngineAO::DoCancel()
       
   242     {
       
   243     WRITELOG( "CContextEngineAO::DoCancel" ); // DEBUG INFO
       
   244 
       
   245     iPluginSnapshotsLacking = 0;
       
   246     iState = EReady;
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // From MContextPluginObserver.
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 void CContextEngineAO::PluginInitializationStatus( TInt aErrorCode )
       
   254     {
       
   255     WRITELOG1( "CContextEngineAO::PluginInitializationStatus with error code: %d", aErrorCode ); // DEBUG INFO
       
   256 
       
   257     if ( aErrorCode != KErrNone && iInitializationError != KErrNone )
       
   258         {
       
   259         iInitializationError = aErrorCode;
       
   260         }
       
   261 
       
   262     iUninitializedPluginsCount--;
       
   263     if ( iUninitializedPluginsCount <= 0 )
       
   264         {
       
   265         iState = EReady;
       
   266         // notify the client application by using MContextSnapshotObserver's
       
   267         // method ContextInitializationStatus()
       
   268         // Notice! Error code of the last plugin is returned...
       
   269         if ( iInitializationObserver )
       
   270             {
       
   271             iInitializationObserver->ContextInitializationStatus( iInitializationError );
       
   272             }
       
   273         }
       
   274     }
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 // From MContextPluginObserver.
       
   278 // ---------------------------------------------------------------------------
       
   279 //
       
   280 void CContextEngineAO::PluginSnapshotStatus( CHarvesterData* aHD )
       
   281     {
       
   282     TInt errorCode = aHD->ErrorCode();
       
   283     WRITELOG1( "CContextEngineAO::PluginSnapshotStatus with error code: %d", errorCode ); // DEBUG INFO
       
   284 
       
   285     if ( errorCode != KErrNone && iSnapshotError != KErrNone )
       
   286         {
       
   287         aHD->SetErrorCode( errorCode );
       
   288         }
       
   289     
       
   290     if( iLastPlugin )
       
   291         {
       
   292         // Notice! Error code of the last plugin is returned...
       
   293         iContextSnapshotObserver->ContextSnapshotStatus( aHD );
       
   294         }
       
   295 
       
   296     if ( iPluginSnapshotsLacking <= 0 )
       
   297         {
       
   298         iState = EReady;
       
   299         }
       
   300     else 
       
   301         {  // do this again for the next plugin/harvest data object
       
   302         if( !IsActive() )
       
   303             {
       
   304             SetActive();
       
   305             TRequestStatus* status = &iStatus;
       
   306             User::RequestComplete( status, KErrNone ); // "kick-start" this AO
       
   307             }
       
   308         }
       
   309     }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // Called by CBlacklistBackupSubscriberAO when
       
   313 // Backup&Restore is backing up or restoring.
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 void CContextEngineAO::BackupRestoreStart()
       
   317     {
       
   318     // stop context plugins
       
   319     WRITELOG( "CContextEngineAO::BackupRestoreStart" );
       
   320  
       
   321     iPlugins.ResetAndDestroy(); // remove any existing plugins, just in case
       
   322     }
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // Called by CBlacklistBackupSubscriberAO when
       
   326 // Backup&Restore has finished backup or restore.
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 void CContextEngineAO::BackupRestoreReady()
       
   330     {
       
   331     // restart plugins
       
   332     WRITELOG( "CContextEngineAO::BackupRestoreReady" );
       
   333     
       
   334     TRAP_IGNORE( InitializeL() );
       
   335     
       
   336     if ( iMdESession )
       
   337     	{
       
   338     	SetMdeSession( iMdESession );
       
   339     	}
       
   340     
       
   341     iState = EReady;
       
   342     
       
   343     }
       
   344 
       
   345 // private methods
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // Load context plugins to the "iPlugins" array.
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 void CContextEngineAO::LoadPluginsL()
       
   352     {
       
   353     WRITELOG( "CContextEngineAO::LoadPluginsL" ); // DEBUG INFO
       
   354 
       
   355     if ( iPlugins.Count() > 0 )
       
   356         {
       
   357         iPlugins.ResetAndDestroy(); // remove any existing plugins, just in case
       
   358         }
       
   359 
       
   360     // read info about all implementations into infoArray
       
   361     RImplInfoPtrArray infoArray;
       
   362     TCleanupItem cleanupItem( MdsUtils::CleanupEComArray, &infoArray );
       
   363     CleanupStack::PushL( cleanupItem );
       
   364     
       
   365     CContextPlugin::ListImplementationsL( infoArray );
       
   366 
       
   367     CContextPlugin* plugin = NULL;
       
   368     TInt err = KErrNone;
       
   369     for ( TInt i = 0; i < infoArray.Count(); i++ )
       
   370         {
       
   371         TUid uid = infoArray[i]->ImplementationUid(); 
       
   372         TRAP( err, plugin = CContextPlugin::NewL( uid ) ); // create the plug-ins
       
   373         if ( err == KErrNone && plugin )
       
   374             {
       
   375             CleanupStack::PushL( plugin );
       
   376             iPlugins.AppendL( plugin );        // and add them to an array
       
   377             CleanupStack::Pop( plugin );
       
   378             }
       
   379         }
       
   380 
       
   381 	CleanupStack::PopAndDestroy( &infoArray ); // infoArray, results in a call to CleanupEComArray		
       
   382     }
       
   383 
       
   384 // ---------------------------------------------------------------------------
       
   385 // Initialize this object. Thus load and initialize plugins.
       
   386 // ---------------------------------------------------------------------------
       
   387 //
       
   388 void CContextEngineAO::InitializeL()
       
   389     {
       
   390     WRITELOG( "CContextEngineAO::InitializeL" ); // DEBUG INFO
       
   391 
       
   392     iInitializationError = KErrNone;
       
   393     LoadPluginsL();
       
   394     iUninitializedPluginsCount = iPlugins.Count(); // set this so we can later be sure that all
       
   395                                                    // plug-ins are initialized.
       
   396     if ( iUninitializedPluginsCount <= 0 ) // if no plugins were loaded
       
   397         {
       
   398         if ( iInitializationObserver )
       
   399             {
       
   400             iInitializationObserver->ContextInitializationStatus( KErrNone );
       
   401             }
       
   402         }
       
   403 
       
   404     const TInt KCount = iUninitializedPluginsCount;
       
   405     for ( TInt i = 0; i < KCount; i++ )
       
   406         {
       
   407         iPlugins[i]->Init( *this ); // CContextEngineAO is instance of MContextPluginObserver
       
   408         }
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CRestoreWatcher::NotifyKeyL
       
   413 // CPropertyWatcher's callback. From 
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 void CContextEngineAO::NotifyKeyL( 
       
   417 		const TInt aKeyValue,
       
   418 		const TUid aPropertyCategory,
       
   419 		const TUint aKey )
       
   420 	{
       
   421 	WRITELOG("CContextEngineAO::NotifyKeyL() - begin");
       
   422 	
       
   423 	if( aPropertyCategory == KUidSystemCategory &&
       
   424 			aKey == conn::KUidBackupRestoreKey ) 
       
   425 		{
       
   426 		HandleBackupRestoreKeyAction( aKeyValue );
       
   427 		}
       
   428 	
       
   429 	
       
   430 	WRITELOG("CContextEngineAO::NotifyKeyL() - end");
       
   431 	}
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CContextEngineAO::HandleBackupKeyAction
       
   435 // When user starts restore sets flag on.
       
   436 // -----------------------------------------------------------------------------
       
   437 //
       
   438 void CContextEngineAO::HandleBackupRestoreKeyAction( const TUint aKeyValue )
       
   439 	{	
       
   440 	WRITELOG("CRestoreWatcher::HandleBackupKeyAction() - begin");
       
   441 	
       
   442 	TInt backupStateValue = aKeyValue;
       
   443 	backupStateValue &= conn::KBURPartTypeMask;
       
   444 	
       
   445 	switch ( backupStateValue )
       
   446 		{
       
   447 		case conn::EBURBackupFull:
       
   448 		case conn::EBURBackupPartial:
       
   449 		case conn::EBURRestoreFull:
       
   450 		case conn::EBURRestorePartial:
       
   451 			{
       
   452 			BackupRestoreStart();
       
   453 			break;
       
   454 			}
       
   455 		
       
   456 		case conn::EBURNormal:
       
   457 		case conn::EBURUnset:
       
   458 		default:
       
   459 			{
       
   460 			// backup or restore is completed, so resume normal operation.
       
   461        		BackupRestoreReady();
       
   462 			}
       
   463 		}	
       
   464 	
       
   465 	WRITELOG("CRestoreWatcher::HandleBackupKeyAction() - end");
       
   466 	}
       
   467