phonebookui/pbkcommonui/src/cntextensionmanager.cpp
changeset 27 de1630741fbe
child 37 fd64c38c277d
equal deleted inserted replaced
25:76a2435edfd4 27:de1630741fbe
       
     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 <QPluginLoader>
       
    19 #include <QtGui>
       
    20 #include <cntuiextensionfactory.h>
       
    21 #include "cntextensionmanager.h"
       
    22 
       
    23 CntExtensionManager::CntExtensionManager( QObject* aParent ):
       
    24     mPluginsLoaded(false)
       
    25 {
       
    26     setParent(aParent);
       
    27 }
       
    28 
       
    29 CntExtensionManager::~CntExtensionManager()
       
    30 {
       
    31     unloadExtensionPlugins();
       
    32 }
       
    33 
       
    34 
       
    35 int CntExtensionManager::pluginCount()
       
    36 {
       
    37     loadExtensionPlugins();
       
    38     return mPlugins.count();
       
    39 }
       
    40     
       
    41 CntUiExtensionFactory* CntExtensionManager::pluginAt( int index)
       
    42 {
       
    43     loadExtensionPlugins();
       
    44     QPluginLoader* pluginLoader = mPlugins[index];
       
    45     QObject *plugin = pluginLoader->instance();
       
    46     if (plugin)
       
    47         {
       
    48             return qobject_cast<CntUiExtensionFactory*>(plugin);
       
    49         }
       
    50     return NULL;
       
    51 }
       
    52 
       
    53 CntUiExtensionFactory* CntExtensionManager::servicePlugin(int serviceId)
       
    54 {
       
    55     foreach(QPluginLoader* pluginLoader, mPlugins )
       
    56     {
       
    57         QObject *plugin = pluginLoader->instance();
       
    58         if (plugin)
       
    59         {
       
    60             CntUiExtensionFactory* interfacePlugin = qobject_cast<CntUiExtensionFactory*>(plugin);
       
    61             if(interfacePlugin && interfacePlugin->hasService(serviceId))
       
    62             {
       
    63                 return interfacePlugin;
       
    64             }
       
    65         }
       
    66     }
       
    67     return NULL;
       
    68 }
       
    69 
       
    70 void CntExtensionManager::loadExtensionPlugins()
       
    71 {
       
    72     if(mPluginsLoaded)
       
    73     {
       
    74         // Plugins already loaded, just return.
       
    75         return;
       
    76     }
       
    77     // TBD: extension should be loaded when they are first time needed.
       
    78     // This is needed due to performance impact on contacts startup.
       
    79     // Also framework should provide means to load new plugins
       
    80     // in runtime. It is very likely use case that new plugins will be
       
    81     // installed in runtime while contacts is running => new plugin should
       
    82     // be loaded when it is added to file system (assuming that this
       
    83     // extension API will be open for 3rd party plugins)
       
    84 
       
    85     QDir pluginsDir(QLibraryInfo::location(QLibraryInfo::PluginsPath));
       
    86     foreach (QString fileName, pluginsDir.entryList(QDir::Files))
       
    87         {
       
    88         // Create plugin loader
       
    89         QPluginLoader* pluginLoader = new QPluginLoader( 
       
    90                 pluginsDir.absoluteFilePath(fileName));
       
    91         if ( pluginLoader->load() )
       
    92             {
       
    93             QObject *plugin = pluginLoader->instance();
       
    94             if (plugin)
       
    95                 {
       
    96                 CntUiExtensionFactory* interface = qobject_cast<CntUiExtensionFactory*>(plugin);        
       
    97                 if ( interface )
       
    98                     {
       
    99                     mPlugins.append(pluginLoader);
       
   100                     }
       
   101                 }
       
   102             }
       
   103         // If plugin loader was not for our plugins, delete loader handle
       
   104         if ( !mPlugins.contains( pluginLoader ) )
       
   105             {
       
   106             pluginLoader->unload();
       
   107             delete pluginLoader;
       
   108             }
       
   109         }
       
   110     mPluginsLoaded = true;
       
   111 }
       
   112 
       
   113 void CntExtensionManager::unloadExtensionPlugins()
       
   114 {
       
   115     // Unload plugins and clear plugin array
       
   116     foreach (QPluginLoader* pluginLoader, mPlugins)
       
   117         {
       
   118         pluginLoader->unload();
       
   119         delete pluginLoader;
       
   120         }   
       
   121     mPlugins.clear();  
       
   122     mPluginsLoaded = false;  
       
   123 }
       
   124