contentstorage/extsrc/cahandlerengine.cpp
changeset 61 8e5041d13c84
parent 60 f62f87b200ec
child 66 32469d7d46ff
equal deleted inserted replaced
60:f62f87b200ec 61:8e5041d13c84
     1 /*
       
     2  * Copyright (c) 2008 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:  ?Description
       
    15  *
       
    16  */
       
    17 
       
    18 #include "cahandlerengine.h"
       
    19 #include "cahandler.h"
       
    20 #include "cahandlerplugin.h"
       
    21 #include "cahandlerplugin.hrh"
       
    22 #include "cadef.h"
       
    23 #include "cainnerentry.h"
       
    24 
       
    25 // ======== LOCAL FUNCTIONS ========
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // Cleanupstack support method, calls RImplInfoPtrArray::ResetAndDestroy()
       
    29 // of the passed argument (array).
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 LOCAL_C void ResetAndDestroyArray( TAny* aArray )
       
    33     {
       
    34     ( (RImplInfoPtrArray* ) aArray )->ResetAndDestroy();
       
    35     }
       
    36 
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38 
       
    39 // ---------------------------------------------------------
       
    40 //
       
    41 // ---------------------------------------------------------
       
    42 //
       
    43 EXPORT_C CCaHandlerEngine::~CCaHandlerEngine()
       
    44     {
       
    45     iHandlers.ResetAndDestroy();
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------
       
    49 //
       
    50 // ---------------------------------------------------------
       
    51 //
       
    52 EXPORT_C CCaHandlerEngine* CCaHandlerEngine::NewL()
       
    53     {
       
    54     CCaHandlerEngine* self = NewLC();
       
    55     CleanupStack::Pop( self );
       
    56     return self;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------
       
    60 //
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 EXPORT_C CCaHandlerEngine* CCaHandlerEngine::NewLC()
       
    64     {
       
    65     CCaHandlerEngine* self = new ( ELeave ) CCaHandlerEngine();
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     return self;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------
       
    72 //
       
    73 // ---------------------------------------------------------
       
    74 //
       
    75 void CCaHandlerEngine::ConstructL()
       
    76     {
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------
       
    80 //
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 CCaHandlerEngine::CCaHandlerEngine()
       
    84     {
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------
       
    88 //
       
    89 // ---------------------------------------------------------
       
    90 //
       
    91 EXPORT_C void CCaHandlerEngine::HandleCommandL( CCaInnerEntry& aEntry,
       
    92         const TDesC8& aCommand )
       
    93     {
       
    94     // Currently, we load handlers on demand and never unload them.
       
    95     // As there aren't many types of items, this seems appropriate.
       
    96     CCaHandler* handler = FindHandler( aEntry.GetEntryTypeName() );
       
    97     if( !handler )
       
    98         {
       
    99         //TODO: need to change this
       
   100         if( aEntry.GetEntryTypeName() == KCaTypeApp()
       
   101                 || aEntry.GetEntryTypeName() == KCaTypeWidget() )
       
   102             {
       
   103             handler = LoadHandlerL( KCaTypeApp() );
       
   104             }
       
   105         else
       
   106             {
       
   107             handler = LoadHandlerL( aEntry.GetEntryTypeName() );
       
   108             }
       
   109         }
       
   110     if( handler )
       
   111         {
       
   112         handler->HandleCommandL( aEntry, aCommand );
       
   113         }
       
   114     else
       
   115         {
       
   116         User::Leave( KErrNotFound );
       
   117         }
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 CCaHandler* CCaHandlerEngine::FindHandler( const TDesC& aType )
       
   125     {
       
   126     for( TInt i = 0; i < iHandlers.Count(); i++ )
       
   127         {
       
   128         CCaHandler* handler = iHandlers[i];
       
   129         if( handler->SupportsType( aType ) )
       
   130             {
       
   131             return handler;
       
   132             }
       
   133         }
       
   134     return NULL;
       
   135     }
       
   136 
       
   137 // ---------------------------------------------------------
       
   138 //
       
   139 // ---------------------------------------------------------
       
   140 //
       
   141 CCaHandler* CCaHandlerEngine::LoadHandlerL( const TDesC& aType )
       
   142     {
       
   143     CCaHandlerPlugin* handler = NULL;
       
   144     TBuf8<KCaMaxTypeLen> type;
       
   145     type.Copy( aType );
       
   146     TEComResolverParams resolverParams;
       
   147     resolverParams.SetDataType( type );
       
   148     RImplInfoPtrArray implInfoArray;
       
   149     CleanupStack::PushL(
       
   150             TCleanupItem( ResetAndDestroyArray, &implInfoArray ) );
       
   151     REComSession::ListImplementationsL( TUid::Uid( HANDLER_IF_UID ),
       
   152             resolverParams, implInfoArray );
       
   153 
       
   154     __ASSERT_DEBUG( implInfoArray.Count() <= 1, User::Invariant() );
       
   155 
       
   156     if( implInfoArray.Count() != 0 )
       
   157         {
       
   158         TUid implUid = implInfoArray[0]->ImplementationUid();
       
   159         handler = CCaHandlerPlugin::NewL( implUid );
       
   160         CleanupStack::PushL( handler );
       
   161         // Here we check if this is the handler we need. Currently we only do
       
   162         // a sanity check for the handler (it should support the registered
       
   163         // types!), but later this can be extended with better support query.
       
   164         // E.g. support for type AND command, etc.
       
   165         if( handler->SupportsType( aType ) )
       
   166             {
       
   167             iHandlers.AppendL( handler );
       
   168             CleanupStack::Pop( handler );
       
   169             }
       
   170         else
       
   171             {
       
   172             CleanupStack::PopAndDestroy( handler );
       
   173             handler = NULL;
       
   174             }
       
   175         }
       
   176 
       
   177     CleanupStack::PopAndDestroy( &implInfoArray );
       
   178     return handler;
       
   179     }