widgetmodel/alfwidgetmodel/src/alfwidgetenvextension.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     1 /*
       
     2 * Copyright (c) 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:  Extensions can be added to AlfEnv through this class.
       
    15                Currently resourcepool,widgetfactory can be added as extensions to AlfEnv.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <alf/alfenv.h>
       
    21 #include <alf/alfenvobject.h>
       
    22 #include <alf/alfresourcepool.h>
       
    23 
       
    24 #include <osn/osnnew.h>
       
    25 #include <alf/alfwidgetexception.h>
       
    26 #include <alf/alfwidgetenvextension.h>
       
    27 
       
    28 #include <alf/alfwidgetfactoryloader.h>
       
    29 #include <alf/ialfwidgetfactory.h>
       
    30 namespace Alf
       
    31     {
       
    32     const int KAlfExtensionResourcePoolUid( 89 ); // Bogus id for resource 
       
    33                                                   // pool extension
       
    34     
       
    35     const int KAlfExtensionWidgetFactoryUid( 106 ); // Bogus id for widget
       
    36                                                     // factory extension
       
    37     
       
    38     // Env extension for the resource pool
       
    39     class AlfExtensionResourcePoolContainer : public MAlfEnvObject
       
    40         {
       
    41     public:        
       
    42 
       
    43         AlfExtensionResourcePoolContainer( ResourcePool* aResourcePool) :
       
    44         mResourcePool(aResourcePool )
       
    45             {
       
    46             }
       
    47 
       
    48         virtual void Release()
       
    49             {
       
    50             delete this;
       
    51             }
       
    52 
       
    53         ~AlfExtensionResourcePoolContainer()
       
    54             {
       
    55             delete mResourcePool;
       
    56             }
       
    57         // Owned
       
    58         ResourcePool* mResourcePool;
       
    59         };
       
    60         
       
    61 
       
    62     // Env extension for the widget factory
       
    63     class AlfExtensionWidgetFactory : public MAlfEnvObject
       
    64         {
       
    65     public:        
       
    66 
       
    67         AlfExtensionWidgetFactory( 
       
    68             AlfWidgetFactoryLoader* amWidgetFactoryLoader,
       
    69             IAlfWidgetFactory* aWidgetFactory) :
       
    70         mWidgetFactoryLoader( amWidgetFactoryLoader ),
       
    71         mWidgetFactory( aWidgetFactory )
       
    72             {
       
    73             }
       
    74 
       
    75         virtual void Release()
       
    76             {
       
    77             delete this;
       
    78             }
       
    79 
       
    80         ~AlfExtensionWidgetFactory()
       
    81             {
       
    82             delete mWidgetFactoryLoader;
       
    83             }
       
    84         // Owned
       
    85         AlfWidgetFactoryLoader* mWidgetFactoryLoader;
       
    86         // Not owned
       
    87         IAlfWidgetFactory* mWidgetFactory;
       
    88         };    
       
    89     
       
    90 // ---------------------------------------------------------------------------
       
    91 // Initializes environment with resource pool and factory extensions
       
    92 // ---------------------------------------------------------------------------
       
    93 //    
       
    94 OSN_EXPORT void AlfWidgetEnvExtension::initializeEnvironment(CAlfEnv& aEnv)
       
    95     {
       
    96 	AlfExtensionResourcePoolContainer* poolContainer = 
       
    97         static_cast<AlfExtensionResourcePoolContainer*>(
       
    98             aEnv.Extension( KAlfExtensionResourcePoolUid));
       
    99     if ( poolContainer == NULL)
       
   100         {
       
   101         // Initialize the env with resource pool extension
       
   102         initializeEnvironmentForResourcePool(aEnv);
       
   103         }
       
   104 	AlfExtensionWidgetFactory* widgetFactoryExtension = 
       
   105         static_cast<AlfExtensionWidgetFactory*>(
       
   106             aEnv.Extension( KAlfExtensionWidgetFactoryUid));
       
   107     if ( widgetFactoryExtension == NULL)
       
   108         {
       
   109         // Initialize the env with widget factory extension
       
   110         initializeEnvironmentForFactory(aEnv);
       
   111         }
       
   112     }
       
   113 // ---------------------------------------------------------------------------
       
   114 // Initializes environment with Theme Manager extension
       
   115 // ---------------------------------------------------------------------------
       
   116 //   
       
   117 void AlfWidgetEnvExtension::initializeEnvironmentForTheme(CAlfEnv& /*aEnv*/ )
       
   118     {
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // Initializes environment with resource pool extension
       
   123 // ---------------------------------------------------------------------------
       
   124 //        
       
   125 void AlfWidgetEnvExtension::initializeEnvironmentForResourcePool(CAlfEnv& aEnv)
       
   126     {
       
   127     auto_ptr<ResourcePool> pool(new(EMM) ResourcePool(
       
   128                                              aEnv.TextureManager(), NULL));
       
   129     auto_ptr<AlfExtensionResourcePoolContainer> poolContainer(
       
   130         new(EMM) AlfExtensionResourcePoolContainer( pool.release()));
       
   131     int err = aEnv.AddExtension( KAlfExtensionResourcePoolUid,
       
   132                        (MAlfEnvObject*)(poolContainer.get()));
       
   133     if ( err != KErrNone)
       
   134         {
       
   135         ALF_THROW(AlfWidgetException, ECommonError, "Failed to add AlfEnv extension");
       
   136         }
       
   137     else
       
   138         {
       
   139         poolContainer.release();
       
   140         }   
       
   141     }
       
   142 // ---------------------------------------------------------------------------
       
   143 // Returns the reference for Resource Pool extension object. 
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 OSN_EXPORT ResourcePool& AlfWidgetEnvExtension::resourcePool( CAlfEnv& aEnv)
       
   147     {
       
   148     Alf::AlfExtensionResourcePoolContainer* poolContainer = 
       
   149         static_cast<Alf::AlfExtensionResourcePoolContainer*>(aEnv.Extension( KAlfExtensionResourcePoolUid));
       
   150     if ( poolContainer == NULL)
       
   151         {
       
   152         initializeEnvironment(aEnv);
       
   153         }
       
   154     //re-fetch the pool
       
   155     poolContainer = 
       
   156         static_cast<Alf::AlfExtensionResourcePoolContainer*>(aEnv.Extension( KAlfExtensionResourcePoolUid));
       
   157     return *(poolContainer->mResourcePool);
       
   158 }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // Initializes environment with factory extension
       
   162 // ---------------------------------------------------------------------------
       
   163 //    
       
   164 void AlfWidgetEnvExtension::initializeEnvironmentForFactory(CAlfEnv& aEnv)
       
   165     {
       
   166     auto_ptr<AlfWidgetFactoryLoader> widgetFactoryLoader( 
       
   167                                          new (EMM) AlfWidgetFactoryLoader() );
       
   168     
       
   169     IAlfWidgetFactory* widgetFactory = NULL;
       
   170     widgetFactory = widgetFactoryLoader->loadWidgetFactory(aEnv);
       
   171     if(!widgetFactory)
       
   172         {
       
   173         ALF_THROW(AlfWidgetException, ECommonError, "Failed to create widget factory");
       
   174         }
       
   175     auto_ptr<AlfExtensionWidgetFactory> widgetFactoryExtension(
       
   176         new(EMM) AlfExtensionWidgetFactory( widgetFactoryLoader.release(),
       
   177                      widgetFactory));
       
   178     int err = aEnv.AddExtension( KAlfExtensionWidgetFactoryUid,
       
   179                        (MAlfEnvObject*)(widgetFactoryExtension.get()));
       
   180     if ( err != 0)
       
   181         {
       
   182         ALF_THROW(AlfWidgetException, ECommonError, "Failed to add AlfEnv extension");
       
   183         }
       
   184     else
       
   185         {
       
   186     	widgetFactoryExtension.release();
       
   187         }        
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // Returns the reference for Theme Manager extension object. 
       
   192 // ---------------------------------------------------------------------------
       
   193 //    
       
   194 OSN_EXPORT AlfThemeManagerUtil& AlfWidgetEnvExtension::themeManagerUtil( CAlfEnv& /*aEnv*/)
       
   195     {
       
   196     // Depricated
       
   197     ALF_THROW(AlfWidgetException, EInvalidArgument, "Depricated Module");
       
   198     }
       
   199 
       
   200     
       
   201 // ---------------------------------------------------------------------------
       
   202 //Access to the widget factory
       
   203 // ---------------------------------------------------------------------------
       
   204 //        
       
   205 OSN_EXPORT IAlfWidgetFactory& AlfWidgetEnvExtension::widgetFactory( 
       
   206     CAlfEnv& aEnv)
       
   207     {
       
   208     AlfExtensionWidgetFactory* widgetFactoryExtension = 
       
   209         static_cast<AlfExtensionWidgetFactory*>(
       
   210             aEnv.Extension( KAlfExtensionWidgetFactoryUid));
       
   211     if ( widgetFactoryExtension == NULL)
       
   212         {
       
   213         initializeEnvironmentForFactory(aEnv);
       
   214         }
       
   215     //re-fetch the widget factory extension
       
   216     widgetFactoryExtension = 
       
   217         static_cast<AlfExtensionWidgetFactory*>(
       
   218             aEnv.Extension( KAlfExtensionWidgetFactoryUid));
       
   219     return *(widgetFactoryExtension->mWidgetFactory);
       
   220     }    
       
   221 
       
   222     } // Alf