controlpanel/src/cpframework/src/cppluginloader.cpp
changeset 68 13e71d907dc3
parent 40 6465d5bb863a
equal deleted inserted replaced
40:6465d5bb863a 68:13e71d907dc3
     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 #include <cppluginloader.h>
       
    18 #include <QString>
       
    19 #include <QDir>
       
    20 #include <QFileInfo>
       
    21 #include <QPluginLoader>
       
    22 #include <cpplugininterface.h>
       
    23 #include <cplauncherinterface.h>
       
    24 #include "cputility.h"
       
    25 #include <cplogger.h>
       
    26 
       
    27 
       
    28 /*!
       
    29     \class CpPluginLoader
       
    30     \brief The CpPluginLoader class loads a controlpanel plugin at run-time.
       
    31  */
       
    32 
       
    33 #ifdef WIN32
       
    34     #define PLUGINFILE_SUFFIX "dll"
       
    35 #else
       
    36     #define PLUGINFILE_SUFFIX "qtplugin"
       
    37 #endif
       
    38 
       
    39 template <typename INTERFACE>
       
    40 static INTERFACE* loadPluginInterface(const QString &pluginFile)
       
    41 {
       
    42     CPPERF_LOG( QLatin1String("Loading plugin: ") + pluginFile );
       
    43     
       
    44     QFileInfo fileInfo(pluginFile);
       
    45 
       
    46     if (!fileInfo.isAbsolute()) {
       
    47         QString fileName = fileInfo.fileName();
       
    48         if (fileInfo.suffix().compare(PLUGINFILE_SUFFIX,Qt::CaseInsensitive)) {
       
    49             fileName = fileInfo.baseName() + '.' + PLUGINFILE_SUFFIX;
       
    50         }
       
    51 
       
    52 		QStringList pluginDirs = CpUtility::pluginDirectories();
       
    53 		foreach(const QString &pluginDir,pluginDirs) {
       
    54 			fileInfo.setFile(pluginDir + fileName);
       
    55 			if (fileInfo.exists() && QLibrary::isLibrary(fileInfo.absoluteFilePath())) {
       
    56 			    CPPERF_LOG( QLatin1String("Valid plugin stub found: ") + fileInfo.absoluteFilePath() );
       
    57 				break;
       
    58 			}
       
    59 		}
       
    60     }
       
    61 
       
    62 	QPluginLoader loader(fileInfo.absoluteFilePath());
       
    63 	INTERFACE *plugin = qobject_cast<INTERFACE*> (loader.instance());
       
    64 	if (!plugin) {
       
    65 		loader.unload();
       
    66 	}
       
    67 	
       
    68 	CPPERF_LOG( QLatin1String("Load plugin ") + (plugin ? QLatin1String("succeed.") : QLatin1String("failed.")) );
       
    69     
       
    70     return plugin;
       
    71 }
       
    72 
       
    73 /*!
       
    74     load a CpPluginInterface by plugin file.
       
    75     the plugin file can either absoulte file path or only file name.
       
    76     acceptable format:
       
    77         sampleplugin
       
    78         sampleplugin.qtplugin
       
    79         sampleplugin.dll
       
    80         C:/resource/qt/plugins/controlpanel/sampleplugin.qtplugin
       
    81         C:/resource/qt/plugins/controlpanel/sampleplugin.dll
       
    82  */
       
    83 CpPluginInterface *CpPluginLoader::loadCpPluginInterface(const QString &pluginFile)
       
    84 {
       
    85     return ::loadPluginInterface<CpPluginInterface>(pluginFile);
       
    86 }
       
    87 
       
    88 /*!
       
    89     load a CpLauncherInterface by plugin file.
       
    90     the plugin file can either absoulte file path or only file name.
       
    91     acceptable format:
       
    92         sampleplugin
       
    93         sampleplugin.qtplugin
       
    94         sampleplugin.dll
       
    95         C:/resource/qt/plugins/controlpanel/sampleplugin.qtplugin
       
    96         C:/resource/qt/plugins/controlpanel/sampleplugin.dll
       
    97  */
       
    98 CpLauncherInterface *CpPluginLoader::loadCpLauncherInterface(const QString &pluginFile)
       
    99 {
       
   100     return ::loadPluginInterface<CpLauncherInterface>(pluginFile);    
       
   101 }
       
   102 
       
   103