mpx/viewframework/viewutility/src/mpxviewutilityimp.cpp
changeset 0 a2952bb97e68
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Implementation of view utility
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <eikenv.h>
       
    21 #include <eikapp.h>
       
    22 #include <eikappui.h>
       
    23 #include <centralrepository.h>
       
    24 #include <mpxprivatecrkeys.h>
       
    25 
       
    26 #include "mpxviewutilitycommanddef.h"
       
    27 #include "mpxviewhistory.h"
       
    28 #include "mpxviewutilityhelper.h"
       
    29 #include "mpxviewutilityimp.h"
       
    30 #include "mpxviewpluginhandler.h"
       
    31 #include "mpxlog.h"
       
    32 
       
    33 // CONSTANTS
       
    34 _LIT( KMPXDelimiter, ";" );
       
    35 const TInt KMPXMaxHistoryLength( 255 );
       
    36 
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // C++ default constructor can NOT contain any code, that
       
    41 // might leave.
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CMPXViewUtilityImp::CMPXViewUtilityImp()
       
    45     {
       
    46     // Do nothing
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // By default Symbian 2nd phase constructor is private.
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 void CMPXViewUtilityImp::ConstructL()
       
    54     {
       
    55     iPluginManager = CMPXViewPluginHandler::NewL( this, *this );
       
    56     iCurrentView = CMPXViewHistory::NewL();
       
    57     ConstructDefaultViewHistoryL();
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // Two-phased constructor.
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CMPXViewUtilityImp* CMPXViewUtilityImp::NewL()
       
    65     {
       
    66     CMPXViewUtilityImp* self = new ( ELeave ) CMPXViewUtilityImp();
       
    67     CleanupStack::PushL( self );
       
    68     self->ConstructL();
       
    69     CleanupStack::Pop( self );
       
    70 
       
    71     return self;
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // Destructor
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CMPXViewUtilityImp::~CMPXViewUtilityImp()
       
    79     {
       
    80     delete iPreviousView;
       
    81     delete iCurrentView;
       
    82     delete iDefaultView;
       
    83     delete iPluginManager;
       
    84     iDefaultHistoryPath.ResetAndDestroy();
       
    85     iCurrentHistoryPath.ResetAndDestroy();
       
    86 
       
    87     iObservers.Reset();
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Retrieves view utility from TLS. Creates if it's not there.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 MMPXViewUtility* CMPXViewUtilityImp::UtilityL()
       
    95     {
       
    96     MPX_FUNC( "CMPXViewUtilityImp::UtilityL" );
       
    97     MMPXViewUtility* utility = reinterpret_cast<MMPXViewUtility*>( Dll::Tls() );
       
    98     if ( !utility )
       
    99         {
       
   100         utility = CMPXViewUtilityImp::NewL();
       
   101         StoreViewUtility( utility );
       
   102         }
       
   103 
       
   104     static_cast<CMPXViewUtilityImp*>( utility )->iRefCount++;
       
   105     return utility;
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // Store view utility to TLS.
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CMPXViewUtilityImp::StoreViewUtility( MMPXViewUtility* aUtility )
       
   113     {
       
   114     Dll::SetTls( aUtility );
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // From MMPXViewUtility
       
   119 // This method must be called when the client no longer needs this instance.
       
   120 // This object will be destroyed when no one is using it.
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 void CMPXViewUtilityImp::Close()
       
   124     {
       
   125     ASSERT( iRefCount > 0 );
       
   126     MPX_FUNC_EX( "CMPXViewUtilityImp::Close" );
       
   127     MPX_DEBUG2( "CMPXViewUtilityImp::Close ref count = %d", iRefCount );
       
   128     if ( --iRefCount == 0 )
       
   129         {
       
   130         MPX_DEBUG1( "CMPXViewUtilityImp::Close deleting this" );
       
   131         // last client released
       
   132         StoreViewUtility( NULL );
       
   133         delete this;
       
   134         }
       
   135     }
       
   136 
       
   137 // ---------------------------------------------------------------------------
       
   138 // From MMPXViewUtility
       
   139 // Activate view by resolving passed uid.
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void CMPXViewUtilityImp::ActivateViewL(
       
   143     const TUid& aUid,
       
   144     const TDesC* aParam )
       
   145     {
       
   146     RArray<TUid> array;
       
   147     CleanupClosePushL( array );
       
   148     array.AppendL( aUid );
       
   149     ActivateViewL( array, EFalse, EFalse, aParam );
       
   150     CleanupStack::PopAndDestroy( &array );
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // From MMPXViewUtility
       
   155 // Activate view by resolving passed uids.
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void CMPXViewUtilityImp::ActivateViewL(
       
   159     const RArray<TUid>& aUids,
       
   160     const TDesC* aParam )
       
   161     {
       
   162     ActivateViewL( aUids, EFalse, EFalse, aParam );
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // From MMPXViewUtility
       
   167 // Activate view as default view, does not bring to foreground.
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 void CMPXViewUtilityImp::SetAsDefaultViewL( const TUid& aUid )
       
   171     {
       
   172     RArray<TUid> array;
       
   173     CleanupClosePushL( array );
       
   174     array.AppendL( aUid );
       
   175     SetAsDefaultViewL( array );
       
   176     CleanupStack::PopAndDestroy( &array );
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // From MMPXViewUtility
       
   181 // Activate view by resolving passed uids.
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 void CMPXViewUtilityImp::SetAsDefaultViewL( const RArray<TUid>& aUids )
       
   185     {
       
   186     TInt count = iCurrentHistoryPath.Count();
       
   187     if ( count > 0 )
       
   188         {
       
   189         User::Leave( KErrAlreadyExists );
       
   190         }
       
   191     else
       
   192         {
       
   193         TUid pluginType( KNullUid );
       
   194         TUid pluginImpUid( KNullUid );
       
   195         CMPXViewPlugin* plugin(
       
   196             iPluginManager->ResolvePluginL( aUids, pluginType, pluginImpUid ) );
       
   197 
       
   198         MPX_DEBUG4( "CMPXViewUtilityImp::SetAsDefaultViewL adding type: 0x%x, impUid: 0x%x, address: 0x%x to current view history",
       
   199             pluginType.iUid, pluginImpUid.iUid, plugin );
       
   200         CMPXViewHistory* viewHistory = CMPXViewHistory::NewL( aUids, pluginType, pluginImpUid, plugin );
       
   201         iCurrentHistoryPath.Append( viewHistory ); // iCurrentHistoryPath takes ownership
       
   202         delete iCurrentView;
       
   203         iCurrentView = NULL;
       
   204         iCurrentView = CMPXViewHistory::NewL( viewHistory );
       
   205         delete iDefaultView;
       
   206         iDefaultView = NULL;
       
   207         iDefaultView = CMPXViewHistory::NewL( viewHistory );
       
   208 
       
   209         NotifyObservers( ActiveViewType(), PreviousViewType() );
       
   210 
       
   211         MPX_DEBUG2( "CMPXViewUtilityImp::SetAsDefaultViewL activating 0x%x as default view", pluginType.iUid );
       
   212         TRAPD( err, plugin->SetAsDefaultViewL() );
       
   213         if ( err != KErrNone )
       
   214             {
       
   215             MPX_DEBUG2( "CMPXViewUtilityImp::SetAsDefaultViewL error duing SetAsDefaultViewL, leaving with code %d", err );
       
   216             // error during activation, clean up view history
       
   217             MarkViewAsClosedL( plugin );
       
   218             User::Leave( err );
       
   219             }
       
   220         else if ( ActiveViewType() != pluginType )
       
   221             {
       
   222             // notify observers again if the view type has changed
       
   223             // possibily because ActivateViewL of the plugin is sync
       
   224             // and the plugin exited already
       
   225             MPX_DEBUG1( "CMPXViewUtilityImp::SetAsDefaultViewL notifying observers again" );
       
   226             NotifyObservers( ActiveViewType(), PreviousViewType() );
       
   227             }
       
   228         else
       
   229             {
       
   230             // nothing to do
       
   231             }
       
   232         }
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // From MMPXViewUtility
       
   237 // Activate previous view.
       
   238 // ---------------------------------------------------------------------------
       
   239 //
       
   240 void CMPXViewUtilityImp::ActivatePreviousViewL()
       
   241     {
       
   242     // if can't find default history in cenrep, don't use default history
       
   243     if ( iCurrentHistoryPath.Count() > 1 )
       
   244         {
       
   245         TBool jump( EFalse );
       
   246         // previousViewType contains a list of view types that needs to be checked
       
   247         TUid previousViewType = KNullUid;
       
   248         if ( iDefaultHistoryPath.Count() > 0 )
       
   249             {
       
   250             // default path exist, check that
       
   251             TUid currentViewType(
       
   252                 iCurrentHistoryPath[ iCurrentHistoryPath.Count() - 1 ]->iPluginType );
       
   253             TInt count( iDefaultHistoryPath.Count() );
       
   254 
       
   255             // loop starts at one, since there's no previous view in index 0
       
   256             for ( TInt i = 1; i < count && previousViewType == KNullUid; i++ )
       
   257                 {
       
   258                 if ( iDefaultHistoryPath[ i ]->iPluginType == currentViewType )
       
   259                     {
       
   260                     previousViewType = iDefaultHistoryPath[ i - 1 ]->iPluginType;
       
   261                     }
       
   262                 }
       
   263 
       
   264             if ( previousViewType != KNullUid )
       
   265                 {
       
   266                 jump = ETrue;
       
   267                 // if previousViewType exist in iCurrentHistoryPath, it's not a jump
       
   268                 count = iCurrentHistoryPath.Count();
       
   269                 for ( TInt i = 0; ( ( i < count ) && ( jump ) ); i++ )
       
   270                     {
       
   271                     if ( iCurrentHistoryPath[ i ]->iPluginType == previousViewType )
       
   272                         {
       
   273                         jump = EFalse;
       
   274                         }
       
   275                     }
       
   276                 MPX_DEBUG2( "CMPXViewUtilityImp::ActivatePreviousViewL activating jump = %d", jump );
       
   277                 }
       
   278 #ifdef _DEBUG
       
   279             else
       
   280                 {
       
   281                 // current UID doesn't exist in default path, just follow the current path
       
   282                 MPX_DEBUG1( "CMPXViewUtilityImp::ActivatePreviousViewL activating current UID doesn't exist in default path" );
       
   283                 }
       
   284 #endif // _DEBUG
       
   285             }
       
   286 #ifdef _DEBUG
       
   287         else
       
   288             {
       
   289             // default path doesn't exist, just use current history path
       
   290             MPX_DEBUG1( "CMPXViewUtilityImp::ActivatePreviousViewL activating without default history" );
       
   291             }
       
   292 #endif // _DEBUG
       
   293         RArray<TUid> array;
       
   294         CleanupClosePushL( array );
       
   295         TInt lastIndex( iCurrentHistoryPath.Count() - 1 );
       
   296         TBool updatePreviousView( EFalse );
       
   297         TBool activatingPreviousView( EFalse );
       
   298         UpdatePreviousViewInfoL( lastIndex );
       
   299         if ( jump )
       
   300             {
       
   301             if ( previousViewType != KNullUid )
       
   302                 {
       
   303                 MPX_DEBUG2( "CMPXViewUtilityImp::ActivatePreviousViewL activating 0x%x jumped", previousViewType.iUid );
       
   304                 updatePreviousView = ETrue;
       
   305                 array.AppendL( previousViewType );
       
   306                 }
       
   307             }
       
   308         else
       
   309             {
       
   310             TInt count = iCurrentHistoryPath[ lastIndex - 1 ]->iUids.Count();
       
   311             MPX_DEBUG2( "CMPXViewUtilityImp::ActivatePreviousViewL activating not jumped iUid.Count %d", count );
       
   312             for ( TInt i = 0; i < count; i++ )
       
   313                 {
       
   314                 TUid u = iCurrentHistoryPath[ lastIndex - 1 ]->iUids[ i ];
       
   315                 MPX_DEBUG2( "CMPXViewUtilityImp::ActivatePreviousViewL activating not jumped, appending 0x%x", u.iUid );
       
   316                 array.AppendL( u );
       
   317                 }
       
   318             activatingPreviousView = ETrue;
       
   319             }
       
   320         // last index is current view, previous view is at last index - 1
       
   321         delete iCurrentHistoryPath[ lastIndex ];
       
   322         iCurrentHistoryPath.Remove( lastIndex );
       
   323         iCurrentHistoryPath.Compress();
       
   324         ActivateViewL( array, activatingPreviousView, updatePreviousView );
       
   325         CleanupStack::PopAndDestroy( &array );
       
   326         }
       
   327     else
       
   328         {
       
   329         // no history
       
   330         User::Leave( KErrNotFound );
       
   331         }
       
   332     }
       
   333 
       
   334 // ---------------------------------------------------------------------------
       
   335 // From MMPXViewUtility
       
   336 // Pre-load view plug-ins that have KMPXVpFeaturePreLoaded flag set.
       
   337 // ---------------------------------------------------------------------------
       
   338 //
       
   339 void CMPXViewUtilityImp::PreLoadViewsL()
       
   340     {
       
   341     MPX_FUNC( "CMPXViewUtilityImp::PreLoadViewsL" );
       
   342 	PreLoadViewsL( KNullUid );
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------------------------
       
   346 // From MMPXViewUtility
       
   347 // Pre-load view plug-ins that have KMPXVpFeaturePreLoaded flag set and also by resolving UID
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 void CMPXViewUtilityImp::PreLoadViewsL( const TUid& aUid )
       
   351     {
       
   352     MPX_DEBUG2( "CMPXViewUtilityImp::PreLoadViewsL aUid: 0x%x", aUid );
       
   353     RArray<TUid> array;
       
   354     CleanupClosePushL( array );
       
   355 
       
   356     // Get list of plug-ins that need to be pre-loaded
       
   357    	iPluginManager->GetPreLoadPluginsL( array, aUid );
       
   358 
       
   359     for ( TInt i = array.Count(); --i >= 0; )
       
   360         {
       
   361         MPX_DEBUG2( "CMPXViewUtilityImp::PreLoadViewsL loading UID: 0x%x", array[i] );
       
   362         CMPXViewPlugin* plugin = iPluginManager->PluginL( array[i] );
       
   363         plugin->CreateViewL();
       
   364         }
       
   365 
       
   366     CleanupStack::PopAndDestroy( &array );
       
   367     }
       
   368 
       
   369 // ---------------------------------------------------------------------------
       
   370 // From MMPXViewUtility
       
   371 // Pre-load view plug-in by resolving passed uid.
       
   372 // ---------------------------------------------------------------------------
       
   373 //
       
   374 void CMPXViewUtilityImp::PreLoadViewL( const TUid& aUid )
       
   375     {
       
   376     TUid pluginType( KNullUid );
       
   377     TUid pluginImpUid( KNullUid );
       
   378     CMPXViewPlugin* plugin(
       
   379         iPluginManager->ResolvePluginL( aUid, pluginType, pluginImpUid ) );
       
   380     plugin->CreateViewL();
       
   381     }
       
   382 
       
   383 // ---------------------------------------------------------------------------
       
   384 // From MMPXViewUtility
       
   385 // Get a reference to view plugin manager.
       
   386 // ---------------------------------------------------------------------------
       
   387 //
       
   388 MMPXViewPluginManager& CMPXViewUtilityImp::ViewPluginManager()
       
   389     {
       
   390     return *iPluginManager;
       
   391     }
       
   392 
       
   393 // ---------------------------------------------------------------------------
       
   394 // From MMPXViewUtility
       
   395 // Constructs default view history
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 TUid CMPXViewUtilityImp::ConstructDefaultViewHistoryL()
       
   399     {
       
   400     iDefaultHistoryPath.ResetAndDestroy();
       
   401     delete iDefaultView;
       
   402     iDefaultView = NULL;
       
   403 
       
   404     CRepository* cenRepository = CRepository::NewLC( KCRUidMPXSettings );
       
   405     TBool isEmbedded = CEikonEnv::Static()->StartedAsServerApp();
       
   406     MPX_DEBUG2( "CMPXViewUtilityImp::ConstructDefaultViewHistoryL isEmbedded: %d", isEmbedded );
       
   407 
       
   408     // check if embeded mode, if embeded mode use host application uid
       
   409     // CAiwGenericParamList
       
   410     // if not embeded mode, use app uid
       
   411 
       
   412     // get default paths
       
   413     TUid settingsUid( KNullUid );
       
   414     if ( isEmbedded )
       
   415         {
       
   416         // TO-DO: get host UID
       
   417         }
       
   418     else
       
   419         {
       
   420         CEikAppUi* appUi = CEikonEnv::Static()->EikAppUi();
       
   421         if ( appUi )
       
   422             {
       
   423             CEikApplication* app = appUi->Application();
       
   424             if ( app )
       
   425                 {
       
   426                 settingsUid = app->AppDllUid();
       
   427                 }
       
   428             }
       
   429         }
       
   430     TInt ret ( KErrGeneral );
       
   431     HBufC* defaultPath = HBufC::NewLC( KMPXMaxHistoryLength );
       
   432     MPX_DEBUG2( "CMPXViewUtilityImp::ConstructDefaultViewHistoryL trying to read default history from uid 0x%x", settingsUid.iUid );
       
   433     TPtr dPathPtr = defaultPath->Des();
       
   434     ret = cenRepository->Get( settingsUid.iUid, dPathPtr );
       
   435     if ( ret == KErrNone )
       
   436         {
       
   437         TPtrC defaultPathPtr( *defaultPath );
       
   438         TInt nextLoc = defaultPathPtr.Find( KMPXDelimiter );
       
   439         TBool defaultSet( EFalse );
       
   440         while ( nextLoc != KErrNotFound )
       
   441             {
       
   442             TPtrC currentPath = defaultPathPtr.Left( nextLoc );
       
   443             AddToDefaultViewHistoryL( currentPath );
       
   444             if ( !defaultSet && iDefaultHistoryPath.Count() == 1 )
       
   445                 {
       
   446                 // first entry is the default view
       
   447                 iDefaultView = CMPXViewHistory::NewL( iDefaultHistoryPath[0] );
       
   448                 defaultSet = ETrue;
       
   449                 }
       
   450             defaultPathPtr.Set( defaultPathPtr.Mid( nextLoc + 1 ) );
       
   451             nextLoc = defaultPathPtr.Find( KMPXDelimiter );
       
   452             }
       
   453         if ( defaultPathPtr.Length() > 0 )
       
   454             {
       
   455             AddToDefaultViewHistoryL( defaultPathPtr );
       
   456             }
       
   457         }
       
   458     else
       
   459         {
       
   460         // get default view
       
   461         HBufC* defaultView = HBufC::NewLC( KMPXMaxHistoryLength );
       
   462         TPtr dViewPtr = defaultView->Des();
       
   463         ret = cenRepository->Get( KMPXVUCurrentDefaultView, dViewPtr );
       
   464         if ( ret == KErrNone )
       
   465             {
       
   466             TPtrC defaultViewPtr( *defaultView );
       
   467             if ( defaultViewPtr.Length() > 0 )
       
   468                 {
       
   469                 TInt nextPos( 0 );
       
   470                 TInt err(
       
   471                     CMPXViewUtilityHelper::ExtractNum( defaultViewPtr, nextPos, ret ) );
       
   472                 if ( err == KErrNone )
       
   473                     {
       
   474                     iDefaultView = CMPXViewHistory::NewL();
       
   475                     iDefaultView->iPluginType = TUid::Uid( ret );
       
   476                     if ( iDefaultHistoryPath.Count() > 0 )
       
   477                         {
       
   478                         TRAP( ret, AddToDefaultViewHistoryL( defaultViewPtr, 0 ) );
       
   479                         }
       
   480                     }
       
   481 
       
   482                 if ( iDefaultHistoryPath.Count() == 0 && err != KErrNone )
       
   483                     {
       
   484                     ret = err;
       
   485                     }
       
   486                 }
       
   487             }
       
   488         CleanupStack::PopAndDestroy( defaultView );
       
   489         }
       
   490     CleanupStack::PopAndDestroy( defaultPath );
       
   491 
       
   492     // clean up duplicate entries
       
   493     TInt count( iDefaultHistoryPath.Count() );
       
   494     TUid previousUid( KNullUid );
       
   495     for ( TInt i = count - 1; i >= 0; i-- )
       
   496         {
       
   497         if ( iDefaultHistoryPath[ i ]->iPluginType == previousUid )
       
   498             {
       
   499             MPX_DEBUG3( "CMPXViewUtilityImp::ConstructDefaultViewHistoryL removing duplicate entry 0x%x at %d", iDefaultHistoryPath[ i ]->iPluginType.iUid, i );
       
   500             // duplicate entry, remove
       
   501             delete iDefaultHistoryPath[ i ];
       
   502             iDefaultHistoryPath.Remove( i );
       
   503             }
       
   504         else
       
   505             {
       
   506             previousUid = iDefaultHistoryPath[ i ]->iPluginType;
       
   507             }
       
   508         }
       
   509     iDefaultHistoryPath.Compress();
       
   510     MPX_DEBUG2( "CMPXViewUtilityImp::ConstructDefaultViewHistoryL after clean up, default path contains %d elements", iDefaultHistoryPath.Count() );
       
   511 
       
   512     CleanupStack::PopAndDestroy( cenRepository );
       
   513     return DefaultViewUid();
       
   514     }
       
   515 
       
   516 // ---------------------------------------------------------------------------
       
   517 // Push default history to view history
       
   518 // ---------------------------------------------------------------------------
       
   519 //
       
   520 void CMPXViewUtilityImp::PushDefaultHistoryL()
       
   521     {
       
   522     MPX_FUNC( "CMPXViewUtilityImp::PushDefaultHistoryL" );
       
   523     TInt count( iDefaultHistoryPath.Count() );
       
   524     iCurrentHistoryPath.ResetAndDestroy();
       
   525     delete iPreviousView;
       
   526     iPreviousView = NULL;
       
   527     if ( count > 0 )
       
   528         {
       
   529         for ( TInt i = 0; i < count; i++ )
       
   530             {
       
   531             CMPXViewHistory* viewHistory = CMPXViewHistory::NewL();
       
   532             CleanupStack::PushL( viewHistory );
       
   533             TUid pluginType( iDefaultHistoryPath[ i ]->iPluginType );
       
   534             viewHistory->iPluginType = pluginType;
       
   535             viewHistory->iUids.Append( pluginType );
       
   536             viewHistory->iPlugin = NULL;
       
   537             iCurrentHistoryPath.Append( viewHistory );
       
   538             CleanupStack::Pop( viewHistory );
       
   539             }
       
   540         }
       
   541 #ifdef _DEBUG
       
   542     else
       
   543         {
       
   544         MPX_DEBUG1( "CMPXViewUtilityImp::PushDefaultHistoryL default history is not defined, FAILED" );
       
   545         }
       
   546 #endif
       
   547     }
       
   548 
       
   549 // ---------------------------------------------------------------------------
       
   550 // Gets default view's UID
       
   551 // ---------------------------------------------------------------------------
       
   552 //
       
   553 TUid CMPXViewUtilityImp::DefaultViewUid()
       
   554     {
       
   555 #ifdef _DEBUG
       
   556     if ( iDefaultView )
       
   557         {
       
   558         MPX_DEBUG2( "CMPXViewUtilityImp::DefaultViewUid = 0x%x", iDefaultView->iPluginType.iUid );
       
   559         }
       
   560 #endif // _DEBUG
       
   561     return iDefaultView ? iDefaultView->iPluginType : KNullUid;
       
   562     }
       
   563 
       
   564 // ---------------------------------------------------------------------------
       
   565 // Returns the current history depth
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 TInt CMPXViewUtilityImp::ViewHistoryDepth()
       
   569     {
       
   570 #ifdef _DEBUG
       
   571     TInt count( iCurrentHistoryPath.Count() );
       
   572     for ( TInt i = 0; i < count; i++ )
       
   573         {
       
   574         TUid currentPluginType = iCurrentHistoryPath[ i ]->iPluginType;
       
   575         CMPXViewPlugin* plugin = iCurrentHistoryPath[ i ]->iPlugin;
       
   576         MPX_DEBUG4( "CMPXViewUtilityImp::ViewHistoryDepth (%d) type = 0x%x, address = 0x%x", i, currentPluginType.iUid, plugin );
       
   577         }
       
   578 #endif
       
   579     return iCurrentHistoryPath.Count();
       
   580     }
       
   581 
       
   582 // ---------------------------------------------------------------------------
       
   583 // Returns the last activated view type
       
   584 // ---------------------------------------------------------------------------
       
   585 //
       
   586 TUid CMPXViewUtilityImp::PreviousViewType()
       
   587     {
       
   588 #ifdef _DEBUG
       
   589     if ( iPreviousView )
       
   590         {
       
   591         MPX_DEBUG2( "CMPXViewUtilityImp::PreviousViewType = 0x%x", iPreviousView->iPluginType.iUid );
       
   592         }
       
   593 #endif // _DEBUG
       
   594     return iPreviousView ? iPreviousView->iPluginType : KNullUid;
       
   595     }
       
   596 
       
   597 // ---------------------------------------------------------------------------
       
   598 // Returns the current activated view type
       
   599 // ---------------------------------------------------------------------------
       
   600 //
       
   601 TUid CMPXViewUtilityImp::ActiveViewType()
       
   602     {
       
   603 #ifdef _DEBUG
       
   604     if ( iCurrentView )
       
   605         {
       
   606         MPX_DEBUG2( "CMPXViewUtilityImp::ActiveViewType = 0x%x", iCurrentView->iPluginType.iUid );
       
   607         }
       
   608 #endif // _DEBUG
       
   609     return iCurrentView ? iCurrentView->iPluginType : KNullUid;
       
   610     }
       
   611 
       
   612 // ---------------------------------------------------------------------------
       
   613 // Returns the current activated view type
       
   614 // ---------------------------------------------------------------------------
       
   615 //
       
   616 TUid CMPXViewUtilityImp::ActiveViewImplementationUid()
       
   617     {
       
   618 #ifdef _DEBUG
       
   619     MPX_DEBUG1( "-->CMPXViewUtilityImp::ActiveViewImplementationUid" );
       
   620     if( iCurrentView &&
       
   621         ( iCurrentView->iPluginImplementationUid != KNullUid ) )
       
   622         {
       
   623         MPX_DEBUG2( "CMPXViewUtilityImp::ActiveViewImplementationUid = 0x%x",
       
   624             iCurrentView->iPluginImplementationUid.iUid );
       
   625         }
       
   626     else
       
   627         {
       
   628         MPX_DEBUG1( "CMPXViewUtilityImp::ActiveViewImplementationUid - active view implementation uid not resolved yet" );
       
   629         }
       
   630     MPX_DEBUG1( "<--CMPXViewUtilityImp::ActiveViewImplementationUid" );
       
   631 #endif // _DEBUG
       
   632     return iCurrentView ? iCurrentView->iPluginImplementationUid : KNullUid;
       
   633     }
       
   634 
       
   635 // ---------------------------------------------------------------------------
       
   636 // Add observer.
       
   637 // ---------------------------------------------------------------------------
       
   638 //
       
   639 void CMPXViewUtilityImp::AddObserverL(
       
   640     MMPXViewActivationObserver* aObserver )
       
   641     {
       
   642     if ( aObserver )
       
   643         {
       
   644         iObservers.AppendL( aObserver );
       
   645         }
       
   646     }
       
   647 
       
   648 // ---------------------------------------------------------------------------
       
   649 // Remove observer.
       
   650 // ---------------------------------------------------------------------------
       
   651 //
       
   652 void CMPXViewUtilityImp::RemoveObserver(
       
   653     MMPXViewActivationObserver* aObserver )
       
   654     {
       
   655     TInt index = iObservers.Find( aObserver );
       
   656     ASSERT( index >= 0 );
       
   657     if ( index >= 0 )
       
   658         {
       
   659         iObservers.Remove( index );
       
   660         }
       
   661     }
       
   662 
       
   663 // ---------------------------------------------------------------------------
       
   664 // Handle custom commands.
       
   665 // ---------------------------------------------------------------------------
       
   666 //
       
   667 void CMPXViewUtilityImp::HandleCustomCommand(
       
   668     const TUid& aUid,
       
   669     const TInt aCommand,
       
   670     const TDesC8& aData )
       
   671     {
       
   672     MPX_FUNC( "CMPXViewUtilityImp::HandleCustomCommand" );
       
   673     if ( aUid.iUid == KMPXViewUtilityCustomCommand )
       
   674         {
       
   675         switch ( aCommand )
       
   676             {
       
   677             case EMPXViewUtilCmdViewActivated:
       
   678                 {
       
   679                 // TO-DO
       
   680                 break;
       
   681                 }
       
   682             case EMPXViewUtilCmdViewDeActivated:
       
   683                 {
       
   684                 MPX_DEBUG1( "CMPXViewUtilityImp::HandleCustomCommand EMPXViewUtilCmdViewDeActivated" );
       
   685                 // aData contains the pointer to the view
       
   686                 if ( aData.Length() > 0 )
       
   687                     {
       
   688                     TPckgBuf<TInt> buf( KErrNotFound );
       
   689                     buf.Copy( aData );
       
   690                     TInt viewPtr = buf();
       
   691                     MPX_DEBUG2( "CMPXViewUtilityImp::HandleCustomCommand EMPXViewUtilCmdViewDeActivated address = 0x%x", viewPtr );
       
   692                     CMPXViewPlugin* piPtr = reinterpret_cast<CMPXViewPlugin*>( viewPtr );
       
   693                     TRAP_IGNORE( MarkViewAsClosedL( piPtr ) );
       
   694                     }
       
   695                 break;
       
   696                 }
       
   697             default:
       
   698                 {
       
   699                 break;
       
   700                 }
       
   701             }
       
   702         }
       
   703     }
       
   704 
       
   705 // ---------------------------------------------------------------------------
       
   706 // CMPXViewUtilityImp::HandlePluginHandlerEvent
       
   707 // ---------------------------------------------------------------------------
       
   708 //
       
   709 void CMPXViewUtilityImp::HandlePluginHandlerEvent(
       
   710     TPluginHandlerEvents aEvent,
       
   711     const TUid& aPluginUid,
       
   712     TInt aLoaded,
       
   713     TInt aData)
       
   714     {
       
   715     MPX_FUNC("CMPXViewUtilityImp::HandlePluginHandlerEvent");
       
   716 
       
   717     switch (aEvent)
       
   718         {
       
   719         case EPluginAdd:
       
   720             {
       
   721             NotifyViewUpdate(aPluginUid, MMPXViewActivationObserver::EViewPluginAdd,
       
   722                 aLoaded, aData);
       
   723             break;
       
   724             }
       
   725         case EPluginUpdateStart:
       
   726             {
       
   727             NotifyViewUpdate(aPluginUid, MMPXViewActivationObserver::EViewPluginUpdate,
       
   728                 aLoaded, aData);
       
   729             break;
       
   730             }
       
   731         case EPluginRemove:
       
   732             {
       
   733             NotifyViewUpdate(aPluginUid, MMPXViewActivationObserver::EViewPluginRemove,
       
   734                 aLoaded, aData);
       
   735             break;
       
   736             }
       
   737         case EPluginUpdateEnd:
       
   738             {
       
   739             // do nothing
       
   740             break;
       
   741             }
       
   742         }
       
   743     }
       
   744 
       
   745 // ---------------------------------------------------------------------------
       
   746 // Activate view by resolving passed uids.
       
   747 // ---------------------------------------------------------------------------
       
   748 //
       
   749 void CMPXViewUtilityImp::ActivateViewL(
       
   750     const RArray<TUid>& aUids,
       
   751     TBool aIsActivatingPreviousView,
       
   752     TBool aUpdatedPreviousView,
       
   753     const TDesC* aParam )
       
   754     {
       
   755     TUid pluginType( KNullUid );
       
   756     TUid pluginImpUid( KNullUid );
       
   757     CMPXViewPlugin* plugin(
       
   758         iPluginManager->ResolvePluginL( aUids, pluginType, pluginImpUid ) );
       
   759     CMPXViewPlugin* currentPlugin( NULL );
       
   760 
       
   761     // if the resolved view is already activated
       
   762     TInt count = iCurrentHistoryPath.Count();
       
   763     TUid currentPluginType( KNullUid );
       
   764     if ( count > 0 )
       
   765         {
       
   766         currentPluginType = iCurrentHistoryPath[ count - 1 ]->iPluginType;
       
   767         currentPlugin = iCurrentHistoryPath[ count - 1 ]->iPlugin;
       
   768         }
       
   769 
       
   770     if ( ( currentPluginType != pluginType ) || ( currentPlugin != plugin ) ||
       
   771         ( aIsActivatingPreviousView ) )
       
   772         {
       
   773         if ( ( pluginType != KNullUid ) && ( !aIsActivatingPreviousView ) )
       
   774             {
       
   775             if ( !aUpdatedPreviousView )
       
   776                 {
       
   777                 if ( count > 0 )
       
   778                     {
       
   779                     UpdatePreviousViewInfoL( KErrNotFound );
       
   780                     }
       
   781                 }
       
   782 
       
   783             // check if the view has been activated before
       
   784             TInt index( FindCurrentHistoryEntryByTypeL( pluginType ) );
       
   785             if ( index != KErrNotFound )
       
   786                 {
       
   787                 // cleanup, count down
       
   788                 for ( TInt i = count - 1; i > index; i-- )
       
   789                     {
       
   790                     MPX_DEBUG2( "CMPXViewUtilityImp::ActivateViewL removing entry %d", i );
       
   791                     delete iCurrentHistoryPath[ i ];
       
   792                     iCurrentHistoryPath.Remove( i );
       
   793                     }
       
   794                 iCurrentHistoryPath.Compress();
       
   795 
       
   796                 // update the uids used to activate this view
       
   797                 CMPXViewHistory* vh( iCurrentHistoryPath[ index ] );
       
   798                 vh->iPlugin = plugin;
       
   799                 vh->iUids.Reset();
       
   800                 TInt uidCount( aUids.Count() );
       
   801                 for ( TInt i = 0; i < uidCount; i++ )
       
   802                     {
       
   803                     TUid u = aUids[ i ];
       
   804                     vh->iUids.Append( u );
       
   805                     }
       
   806                 }
       
   807             else
       
   808                 {
       
   809                 MPX_DEBUG3( "CMPXViewUtilityImp::ActivateViewL adding type: 0x%x, address: 0x%x to current view history", pluginType.iUid, plugin );
       
   810                 CMPXViewHistory* viewHistory = CMPXViewHistory::NewL();
       
   811                 CleanupStack::PushL( viewHistory );
       
   812                 TInt uidCount( aUids.Count() );
       
   813                 for ( TInt i = 0; i < uidCount; i++ )
       
   814                     {
       
   815                     TUid u = aUids[ i ];
       
   816                     viewHistory->iUids.Append( u );
       
   817                     }
       
   818                 viewHistory->iPlugin = plugin;
       
   819                 viewHistory->iPluginType = pluginType;
       
   820                 viewHistory->iPluginImplementationUid = pluginImpUid;
       
   821                 iCurrentHistoryPath.Append( viewHistory );
       
   822                 CleanupStack::Pop( viewHistory );
       
   823                 }
       
   824             }
       
   825         MPX_DEBUG2( "CMPXViewUtilityImp::ActivateViewL activating 0x%x", pluginType.iUid );
       
   826 
       
   827         if ( plugin != iCurrentView->iPlugin )
       
   828             {
       
   829             delete iCurrentView;
       
   830             iCurrentView = NULL;
       
   831             iCurrentView = CMPXViewHistory::NewL( aUids, pluginType, pluginImpUid, plugin );
       
   832             // Notify observers for view activation, ActivateViewL maybe sync
       
   833             // and won't return until the view is closed
       
   834             NotifyObservers( ActiveViewType(), PreviousViewType() );
       
   835             TRAPD( err, plugin->ActivateViewL( aParam ) );
       
   836             if ( err != KErrNone )
       
   837                 {
       
   838                 MPX_DEBUG2( "CMPXViewUtilityImp::ActivateViewL error duing ActivateViewL, leaving with code %d", err );
       
   839                 // error during view activation, clean up view history
       
   840                 MarkViewAsClosedL( plugin );
       
   841                 User::Leave( err );
       
   842                 }
       
   843             }
       
   844         else
       
   845             {
       
   846             MPX_DEBUG2( "CMPXViewUtilityImp::ActivateViewL view 0x%x already activated, ignoring", pluginType.iUid );
       
   847             }
       
   848         }
       
   849     else
       
   850         {
       
   851         MPX_DEBUG2( "CMPXViewUtilityImp::ActivateViewL view 0x%x already activated, ignoring", pluginType.iUid );
       
   852         }
       
   853     }
       
   854 
       
   855 // ---------------------------------------------------------------------------
       
   856 // Marks the view closed.  This does not actually close the view.
       
   857 // ---------------------------------------------------------------------------
       
   858 //
       
   859 void CMPXViewUtilityImp::MarkViewAsClosedL( CMPXViewPlugin* aPlugin )
       
   860     {
       
   861     MPX_DEBUG2( "CMPXViewUtilityImp::MarkViewAsClosedL called 0x%x", aPlugin );
       
   862     TInt count = iCurrentHistoryPath.Count();
       
   863     TBool removed( EFalse );
       
   864     // Count down, counting up and removing items is bad
       
   865     for ( TInt i = count - 1; i >= 0 && !removed ; i-- )
       
   866         {
       
   867         if ( iCurrentHistoryPath[ i ]->iPlugin == aPlugin )
       
   868             {
       
   869             MPX_DEBUG2( "CMPXViewUtilityImp::MarkViewAsClosedL removing location %d", i );
       
   870             UpdatePreviousViewInfoL( i );
       
   871 
       
   872             delete iCurrentView;
       
   873             iCurrentView = NULL;
       
   874             if ( i > 0 )
       
   875                 {
       
   876                 iCurrentView = CMPXViewHistory::NewL(
       
   877                     iCurrentHistoryPath[ ( i != count - 1 ) ? count - 1 : i - 1 ] );
       
   878                 }
       
   879 
       
   880             // match found, remove it from history
       
   881             delete iCurrentHistoryPath[ i ];
       
   882             iCurrentHistoryPath.Remove( i );
       
   883             removed = ETrue;
       
   884 
       
   885             // Notify observers for view activation, ActivateViewL maybe sync
       
   886             // and won't return until the view is closed
       
   887             NotifyObservers( ActiveViewType(), PreviousViewType() );
       
   888             }
       
   889         }
       
   890     if ( removed )
       
   891         {
       
   892         iCurrentHistoryPath.Compress();
       
   893         }
       
   894 #ifdef _DEBUG
       
   895     else
       
   896         {
       
   897         MPX_DEBUG1( "CMPXViewUtilityImp::MarkViewAsClosedL Nothing removed" );
       
   898         }
       
   899 #endif
       
   900     }
       
   901 
       
   902 // -----------------------------------------------------------------------------
       
   903 // Notify observers for view activation
       
   904 // -----------------------------------------------------------------------------
       
   905 //
       
   906 void CMPXViewUtilityImp::NotifyObservers(
       
   907     const TUid& aCurrentViewType,
       
   908     const TUid& aPreviousViewType )
       
   909     {
       
   910     MPX_FUNC( "CMPXViewUtilityImp::NotifyObservers" );
       
   911     MPX_DEBUG3( "CMPXViewUtilityImp::NotifyObservers current: 0x%x, previous: 0x%x", aCurrentViewType.iUid, aPreviousViewType.iUid );
       
   912     TInt count( iObservers.Count() );
       
   913     for ( TInt i = 0; i < count; i++ )
       
   914         {
       
   915         iObservers[i]->HandleViewActivation(
       
   916             aCurrentViewType,
       
   917             aPreviousViewType );
       
   918         }
       
   919     }
       
   920 
       
   921 // -----------------------------------------------------------------------------
       
   922 // Notify observers of a view update event
       
   923 // -----------------------------------------------------------------------------
       
   924 //
       
   925 void CMPXViewUtilityImp::NotifyViewUpdate(
       
   926     const TUid& aViewUid,
       
   927     MMPXViewActivationObserver::TViewUpdateEvent aUpdateEvent,
       
   928     TBool aLoaded,
       
   929     TInt aVersion /* = 0 */)
       
   930     {
       
   931     MPX_FUNC( "CMPXViewUtilityImp::NotifyViewUpdate" );
       
   932 
       
   933     TInt count(iObservers.Count());
       
   934     for (TInt i = 0; i < count; ++i)
       
   935         {
       
   936         iObservers[i]->HandleViewUpdate(aViewUid, aUpdateEvent, aLoaded, aVersion);
       
   937         }
       
   938     }
       
   939 
       
   940 // ---------------------------------------------------------------------------
       
   941 // Insert/Append to default view history array
       
   942 // ---------------------------------------------------------------------------
       
   943 //
       
   944 TInt CMPXViewUtilityImp::AddToDefaultViewHistoryL(
       
   945     const TDesC& aData, TInt aLoc )
       
   946     {
       
   947     TInt result( KErrGeneral );
       
   948     TInt nextPos( 0 );
       
   949     User::LeaveIfError( CMPXViewUtilityHelper::ExtractNum( aData, nextPos, result ) );
       
   950     CMPXViewHistory* viewHistory = CMPXViewHistory::NewL();
       
   951     CleanupStack::PushL( viewHistory );
       
   952     viewHistory->iPluginType.iUid = result;
       
   953     if ( aLoc == -1 )
       
   954         {
       
   955         MPX_DEBUG2( "CMPXViewUtilityImp::AddToDefaultViewHistoryL Appending 0x%x to default view history", result );
       
   956         User::LeaveIfError( iDefaultHistoryPath.Append( viewHistory ) );
       
   957         }
       
   958     else
       
   959         {
       
   960         MPX_DEBUG3( "CMPXViewUtilityImp::AddToDefaultViewHistoryL Inserting 0x%x to default view history at %d", result, aLoc );
       
   961         User::LeaveIfError( iDefaultHistoryPath.Insert( viewHistory, aLoc ) );
       
   962         }
       
   963     CleanupStack::Pop( viewHistory );
       
   964     return result;
       
   965     }
       
   966 
       
   967 // ---------------------------------------------------------------------------
       
   968 // Updates previous view info
       
   969 // ---------------------------------------------------------------------------
       
   970 //
       
   971 void CMPXViewUtilityImp::UpdatePreviousViewInfoL( TInt aIndex )
       
   972     {
       
   973     MPX_FUNC( "CMPXViewUtilityImp::UpdatePreviousViewInfoL" );
       
   974     delete iPreviousView;
       
   975     iPreviousView = NULL;
       
   976     if ( aIndex != KErrNotFound )
       
   977         {
       
   978         TInt currentHistoryCount = iCurrentHistoryPath.Count();
       
   979         if ( ( currentHistoryCount > aIndex ) && ( aIndex >= 0 ) )
       
   980             {
       
   981             iPreviousView = CMPXViewHistory::NewL( iCurrentHistoryPath[ aIndex ] );
       
   982             iPreviousView->iPlugin = NULL;
       
   983             MPX_DEBUG2( "CMPXViewUtilityImp::UpdatePreviousViewInfoL previous view type 0x%x", iPreviousView->iPluginType.iUid );
       
   984             }
       
   985         }
       
   986     else
       
   987         {
       
   988         delete iPreviousView;
       
   989         iPreviousView = NULL;
       
   990         iPreviousView = CMPXViewHistory::NewL( iCurrentView );
       
   991         }
       
   992     }
       
   993 
       
   994 // ---------------------------------------------------------------------------
       
   995 // Find if the view exist in current history
       
   996 // ---------------------------------------------------------------------------
       
   997 //
       
   998 TInt CMPXViewUtilityImp::FindCurrentHistoryEntryByTypeL( const TUid aType )
       
   999     {
       
  1000     TInt count = iCurrentHistoryPath.Count();
       
  1001     TInt index = KErrNotFound;
       
  1002     for ( TInt i = 0; i < count && index == KErrNotFound; i++ )
       
  1003         {
       
  1004         if ( iCurrentHistoryPath[ i ]->iPluginType == aType )
       
  1005             {
       
  1006             index = i;
       
  1007             }
       
  1008         }
       
  1009     return index;
       
  1010     }
       
  1011 
       
  1012 //  End of File