psln/PslnFramework/src/PslnFWPluginHandler.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Base class for plugins containing other plugins.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "pslnfwpluginhandler.h"
       
    21 #include "PslnDebug.h"
       
    22 
       
    23 const TInt KPslnFWGeneralThemeViewLocation = 0;
       
    24 const TInt KPslnFWWallpaperViewLocation = 3;
       
    25 const TInt KPslnFWScreensaverViewLocation = 4;
       
    26 
       
    27 // ========================= MEMBER FUNCTIONS ================================
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CPslnFWPluginHandler::NewL()
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 EXPORT_C CPslnFWPluginHandler* CPslnFWPluginHandler::NewL(
       
    34     CAknViewAppUi* aAppUi, CArrayPtrFlat<CPslnFWPluginInterface>* aPluginArray )
       
    35     {
       
    36     PSLN_TRACE_DEBUG("CPslnFWPluginHandler::NewL");
       
    37     CPslnFWPluginHandler* self =
       
    38         new ( ELeave) CPslnFWPluginHandler( aAppUi, aPluginArray );
       
    39     return self;
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CPslnFWPluginHandler::CPslnFWPluginHandler()
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CPslnFWPluginHandler::CPslnFWPluginHandler(
       
    47     CAknViewAppUi* aAppUi, CArrayPtrFlat<CPslnFWPluginInterface>* aPluginArray )
       
    48     : iAppUi( aAppUi ), iPluginArray( aPluginArray )
       
    49     {
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Destructor.
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C CPslnFWPluginHandler::~CPslnFWPluginHandler()
       
    57     {
       
    58     PSLN_TRACE_DEBUG("CPslnFWPluginHandler::destructor");
       
    59     iImplInfoArray.ResetAndDestroy();
       
    60     // iPluginArray is not owned, and thus not deleted.
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Loads all found plugins.
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C void CPslnFWPluginHandler::LoadPluginsL(
       
    68     CArrayPtrFlat<CPslnFWPluginInterface>* aPluginArray )
       
    69     {
       
    70     PSLN_TRACE_DEBUG("CPslnFWPluginHandler::LoadPluginsL BEGIN");
       
    71     if ( iPluginArray )
       
    72         {
       
    73         iPluginArray = aPluginArray;
       
    74         }
       
    75     else
       
    76         {
       
    77         User::Leave( KErrArgument );
       
    78         }
       
    79 
       
    80     REComSession::ListImplementationsL(
       
    81         KPslnFWPluginInterfaceUid,
       
    82         iImplInfoArray );
       
    83 
       
    84     // Reset iterator:
       
    85     iImplInfoArrayIterator = 0;
       
    86 
       
    87     LoadNextPlugin();
       
    88     PSLN_TRACE_DEBUG("CPslnFWPluginHandler::LoadPluginsL END");
       
    89     }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // Loads one specific plugin.
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C CPslnFWPluginInterface* CPslnFWPluginHandler::LoadPluginL( const TUid aPluginId )
       
    96     {
       
    97     PSLN_TRACE_DEBUG("CPslnFWPluginHandler::LoadPluginL BEGIN");
       
    98     REComSession::ListImplementationsL(
       
    99         KPslnFWPluginInterfaceUid,
       
   100         iImplInfoArray );
       
   101 
       
   102     // Reset iterator:
       
   103     iImplInfoArrayIterator = 0;
       
   104 
       
   105     // Iterate through iImplInfoArray.
       
   106     for( ; iImplInfoArrayIterator < iImplInfoArray.Count();  )
       
   107         {
       
   108         CImplementationInformation* info =
       
   109             iImplInfoArray[ iImplInfoArrayIterator ];
       
   110         TUid implUid = info->ImplementationUid();
       
   111 
       
   112         if ( implUid.iUid == aPluginId.iUid )
       
   113             {
       
   114             // Create a plugin.
       
   115             CPslnFWPluginInterface* plugin = CPslnFWPluginInterface::NewL(
       
   116                 aPluginId,
       
   117                 iAppUi );
       
   118             // Ownership transferred.
       
   119             PSLN_TRACE_DEBUG("CPslnFWPluginHandler::LoadPluginL plugin found");
       
   120             return plugin;
       
   121             }
       
   122 
       
   123         iImplInfoArrayIterator++;
       
   124         }
       
   125     PSLN_TRACE_DEBUG("CPslnFWPluginHandler::LoadPluginL END");
       
   126     return NULL;
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // Constructor without array.
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C CPslnFWPluginHandler* CPslnFWPluginHandler::NewL( CAknViewAppUi* aAppUi )
       
   134     {
       
   135     PSLN_TRACE_DEBUG("CPslnFWPluginHandler::NewL 2");
       
   136     CPslnFWPluginHandler* self =
       
   137         new ( ELeave) CPslnFWPluginHandler( aAppUi, NULL );
       
   138     return self;
       
   139     }
       
   140 
       
   141 // ----------------------------------------------------------------------------
       
   142 // Iterate through iImplInfoArray. Load the plugin if it is eligible for
       
   143 // loading. Loaded plugin is added to iPluginArray.
       
   144 // ----------------------------------------------------------------------------
       
   145 //
       
   146 void CPslnFWPluginHandler::LoadNextPlugin()
       
   147     {
       
   148     // Iterate through iImplInfoArray.
       
   149     for( ; iImplInfoArrayIterator < iImplInfoArray.Count();  )
       
   150         {
       
   151         CImplementationInformation* info =
       
   152             iImplInfoArray[ iImplInfoArrayIterator ];
       
   153         TUid implUid = info->ImplementationUid();
       
   154 
       
   155         iImplInfoArrayIterator++;
       
   156 
       
   157         // Load plugin - if it fails, just go to next one.
       
   158         TRAP_IGNORE( AppendPluginL( implUid ) );
       
   159         }
       
   160     }
       
   161 
       
   162 // ----------------------------------------------------------------------------
       
   163 // Inserts plugin to plugin-array.
       
   164 // ----------------------------------------------------------------------------
       
   165 //
       
   166 void CPslnFWPluginHandler::AppendPluginL( const TUid& aImplUid )
       
   167     {
       
   168     // Create a plugin.
       
   169     CPslnFWPluginInterface* plugin = CPslnFWPluginInterface::NewL(
       
   170         aImplUid,
       
   171         iAppUi );
       
   172     CleanupStack::PushL ( plugin );
       
   173 
       
   174     CPslnFWPluginInterface::TPslnFWLocationType locType =
       
   175         CPslnFWPluginInterface::EPslnFWNotSpecified;
       
   176     TInt location = KErrNotFound;
       
   177     plugin->GetLocationTypeAndIndex( locType, location );
       
   178 
       
   179 
       
   180     // If plugin claims invalid location, OR location is not specified,
       
   181     // append to the end.
       
   182     if ( IsInvalidLocation( location ) ||
       
   183         locType == CPslnFWPluginInterface::EPslnFWNotSpecified )
       
   184         {
       
   185         if ( iPluginArray )
       
   186             {
       
   187             iPluginArray->AppendL( plugin );
       
   188             }
       
   189         }
       
   190     else
       
   191         {
       
   192         CPslnFWPluginInterface* arrayPlugin = NULL;
       
   193 
       
   194         CPslnFWPluginInterface::TPslnFWLocationType pluginLocType =
       
   195             CPslnFWPluginInterface::EPslnFWNotSpecified;
       
   196         TInt pluginLoc = KErrNotFound;
       
   197         TInt count = iPluginArray->Count();
       
   198 
       
   199         // Exception - first item, is always appended
       
   200         if ( count == 0 )
       
   201             {
       
   202             iPluginArray->AppendL( plugin );
       
   203             }
       
   204         else
       
   205             {
       
   206             // Loop through the plugin array, trying to find best location for the plugin.
       
   207             TBool breakLoop = EFalse;
       
   208             for ( TInt i = 0; i < count; i++ )
       
   209                 {
       
   210                 arrayPlugin = iPluginArray->At( i );
       
   211                 arrayPlugin->GetLocationTypeAndIndex( pluginLocType, pluginLoc );
       
   212                 if ( pluginLocType == CPslnFWPluginInterface::EPslnFWNotSpecified )
       
   213                     {
       
   214                     // There are no more valid locations, insert here
       
   215                     iPluginArray->InsertL( i, plugin );
       
   216                     breakLoop = ETrue;
       
   217                     }
       
   218                 if ( pluginLoc == location )
       
   219                     {
       
   220                     // Mismatch - plugin location already claimed.
       
   221                     // Append plugin to last position in the array.
       
   222                     iPluginArray->AppendL( plugin );
       
   223                     breakLoop = ETrue;
       
   224                     }
       
   225                 else if ( pluginLoc < location )
       
   226                     {
       
   227                     // Do nothing go to next inserted plugin
       
   228                     }
       
   229                 else
       
   230                     {
       
   231                     // This location is best for the new plugin.
       
   232                     iPluginArray->InsertL( i, plugin );
       
   233                     breakLoop = ETrue;
       
   234                     }
       
   235                 // If plugin has been inserted, or appended, break the loop.
       
   236                 if ( breakLoop )
       
   237                     {
       
   238                     i = count;
       
   239                     }
       
   240                 }
       
   241             }
       
   242         }
       
   243 
       
   244     // Transfer ownership to array.
       
   245     CleanupStack::Pop ( plugin );
       
   246     }
       
   247 
       
   248 // ----------------------------------------------------------------------------
       
   249 // Checks if the plugin tries to claim invalid location.
       
   250 // ----------------------------------------------------------------------------
       
   251 //
       
   252 TBool CPslnFWPluginHandler::IsInvalidLocation( const TInt& aLoc ) const
       
   253     {
       
   254     TBool retVal = ETrue;
       
   255     if ( aLoc == KPslnFWGeneralThemeViewLocation ||
       
   256          aLoc == KPslnFWWallpaperViewLocation ||
       
   257          aLoc == KPslnFWScreensaverViewLocation )
       
   258         {
       
   259         retVal = EFalse;
       
   260         }
       
   261     return retVal;
       
   262     }
       
   263 
       
   264 // End of File