mpx/mpxviewframeworkqt/src/mpxviewpluginresolver_p.cpp
branchRCL_3
changeset 24 6c1dfe4da5dd
equal deleted inserted replaced
23:e42293e811d8 24:6c1dfe4da5dd
       
     1 /*
       
     2 * Copyright (c) 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 #include "mpxviewpluginresolver_p.h"    // MpxViewPluginResolverPrivate
       
    19 #include "mpxplugindata.h"
       
    20 #include "mpxplugindataparser.h"
       
    21 
       
    22 #include <mpxviewpluginqt.h>
       
    23 #include <QHash>
       
    24 
       
    25 const char gRequestedPluginInterfaceName [] = "org.nokia.mmdt.MpxViewPlugin/1.0";
       
    26 
       
    27 const int gNoneScore( 0 );
       
    28 const int gSupportedScore( 5 );
       
    29 const int gImplementationScore( 100 ); 
       
    30 const int gTypeScore( 1 );
       
    31 
       
    32 // ----------------------------------------------------------------------------
       
    33 // score
       
    34 // ----------------------------------------------------------------------------
       
    35 //
       
    36 static int score( int , const MpxPluginData& );
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 //MpxViewPluginResolverPrivate
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 MpxViewPluginResolverPrivate::MpxViewPluginResolverPrivate( 
       
    43         const QList<int>& request )
       
    44 :
       
    45 requestedPlugins( request )
       
    46 {
       
    47     //No implementation needed
       
    48 }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // ~MpxViewPluginResolverPrivate
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 MpxViewPluginResolverPrivate::~MpxViewPluginResolverPrivate()
       
    55 {
       
    56     //No implementation needed
       
    57 }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // resolvePlugin()
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 MpxViewPlugin* MpxViewPluginResolverPrivate::resolvePlugin()
       
    64 {
       
    65     MpxPluginViewInterface* retVal = 0;
       
    66     if( 0 < requestedPlugins.count() ){
       
    67         XQPluginLoader  loader;
       
    68         QList< XQPluginInfo > impls;
       
    69         if (loader.listImplementations(gRequestedPluginInterfaceName, impls)) {
       
    70             retVal = resolveAndLoadPlugins( impls );
       
    71         }
       
    72     }
       
    73     return (retVal!=0) ? retVal->viewPlugin() : 0;
       
    74 }
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // resolveAndLoadPlugins( const QList< XQPluginInfo >& impls );
       
    78 // ----------------------------------------------------------------------------
       
    79 //
       
    80 MpxPluginViewInterface* MpxViewPluginResolverPrivate::resolveAndLoadPlugins( 
       
    81         const QList< XQPluginInfo >& impls )
       
    82 {
       
    83     MpxPluginViewInterface* retVal = 0;
       
    84     
       
    85     //parse plugin info data 
       
    86     QHash<const XQPluginInfo*, MpxPluginData* > parsedInfo;
       
    87     QList< XQPluginInfo >::const_iterator iter( impls.begin() );
       
    88     MpxPluginDataParser parser;
       
    89     for( ; iter != impls.end(); ++iter ){
       
    90         if( 0 < ( *iter ).opaqueData().length() ){
       
    91             parser.parse( ( *iter ).opaqueData() );
       
    92             if( parser.data() ){
       
    93                 parsedInfo.insert( &( *iter ), parser.takeData() );
       
    94             }
       
    95         }
       
    96     }
       
    97     
       
    98     //look for optimal plugin
       
    99     QList<int> uids( requestedPlugins );
       
   100     QList<int>::iterator currentUidIter,
       
   101                          optimalUidIter;
       
   102     QHash<const XQPluginInfo*, MpxPluginData* >::iterator 
       
   103         currentInfoIter,
       
   104         optimalInfoIter;
       
   105     int currentPluginScore( 0 ),
       
   106         optimalPluginScore( 0 );
       
   107     
       
   108     do{
       
   109         //initialize iterators with invalid objects
       
   110         optimalInfoIter = parsedInfo.end();
       
   111         
       
   112         for( currentUidIter = uids.begin();
       
   113              uids.end() != currentUidIter;
       
   114              ++currentUidIter )
       
   115         {
       
   116             for( currentInfoIter = parsedInfo.begin();
       
   117                  parsedInfo.end() != currentInfoIter;
       
   118                  ++currentInfoIter )
       
   119             {
       
   120                 currentPluginScore = score( (*currentUidIter ), 
       
   121                                             (*(*currentInfoIter) ) );
       
   122                 if( gNoneScore < currentPluginScore ){
       
   123                     if( parsedInfo.end() == optimalInfoIter ){
       
   124                         //first plugin with no zero score. set as optimal
       
   125                         optimalPluginScore = currentPluginScore;
       
   126                         optimalInfoIter = currentInfoIter;
       
   127                         optimalUidIter = currentUidIter;
       
   128                     }
       
   129                     if( optimalPluginScore < currentPluginScore ||
       
   130                         ( ( currentPluginScore == optimalPluginScore ) &&
       
   131                           ( ( *currentInfoIter )->priority() > ( *optimalInfoIter )->priority() ) ) )
       
   132                     {
       
   133                         //current plugin fits better then "optimal" one
       
   134                         optimalPluginScore = currentPluginScore;
       
   135                         optimalInfoIter = currentInfoIter;
       
   136                         optimalUidIter = currentUidIter;
       
   137                     }
       
   138                 }
       
   139             }
       
   140         }
       
   141         if( parsedInfo.end() == optimalInfoIter ){
       
   142              //plugin was not found
       
   143              break;
       
   144          }
       
   145          XQPluginLoader loader( optimalInfoIter.key()->uid() );
       
   146          if( loader.load() ) {
       
   147              retVal = qobject_cast<MpxPluginViewInterface*>(loader.instance());
       
   148          }
       
   149          //remove already loaded plugin description
       
   150          delete ( *optimalInfoIter );
       
   151          parsedInfo.erase( optimalInfoIter );
       
   152          uids.erase( optimalUidIter );
       
   153     
       
   154     }while( NULL == retVal );
       
   155     uids.clear();
       
   156     
       
   157     //clear parsed data structures
       
   158     for( currentInfoIter = parsedInfo.begin();
       
   159          parsedInfo.end() != currentInfoIter;
       
   160          currentInfoIter = parsedInfo.erase( currentInfoIter ) ){
       
   161          delete ( *currentInfoIter );
       
   162     }
       
   163     return retVal;
       
   164 }
       
   165 // ----------------------------------------------------------------------------
       
   166 // score
       
   167 // ----------------------------------------------------------------------------
       
   168 //
       
   169 int score( int uid, const MpxPluginData& pluginData )
       
   170 {
       
   171     int retVal( gNoneScore );
       
   172     QList< int >::const_iterator iter( pluginData.supportedId().begin() );
       
   173     for( ; iter != pluginData.supportedId().end(); ++iter ){
       
   174         if( ( *iter ) == uid ){
       
   175             retVal += gSupportedScore;
       
   176         }
       
   177     }
       
   178     if( pluginData.id() == uid ){
       
   179         retVal += gImplementationScore; 
       
   180     }
       
   181     if( pluginData.type() == uid ){
       
   182         retVal += gTypeScore;
       
   183     }
       
   184     
       
   185     qDebug(QString("Score 0x%1: %2").arg(uid, 0, 16).arg(retVal).toLatin1().data());
       
   186     
       
   187     return retVal;
       
   188 }