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