controlpanel/src/cpframework/src/cppluginloader.cpp
branchRCL_3
changeset 54 7e0eff37aedb
parent 53 8ee96d21d9bf
child 57 e78c61e77b1a
equal deleted inserted replaced
53:8ee96d21d9bf 54:7e0eff37aedb
     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 /*
       
    40  *  Load the root component object of the plugin from @pluginFile
       
    41  *  if @pluginFile is an absoulte file path, load it directly, if is a 
       
    42  *  file name, load the root component from path /resource/qt/plugins/controlpanel
       
    43 */
       
    44 static QObject* loadPluginInterface(const QString &pluginFile)
       
    45 {
       
    46     CPPERF_LOG( QLatin1String("Loading plugin: ") + pluginFile );
       
    47     
       
    48     QFileInfo fileInfo(pluginFile);
       
    49 
       
    50     // scan the plugin file from path /resource/qt/plugins/controlpanel
       
    51     if (!fileInfo.isAbsolute()) {
       
    52         QString fileName = fileInfo.fileName();
       
    53         if (fileInfo.suffix().compare(PLUGINFILE_SUFFIX,Qt::CaseInsensitive)) {
       
    54             fileName = fileInfo.baseName() + '.' + PLUGINFILE_SUFFIX;
       
    55         }
       
    56 
       
    57 		QStringList pluginDirs = CpUtility::pluginDirectories();
       
    58 		foreach(const QString &pluginDir,pluginDirs) {
       
    59 			fileInfo.setFile(pluginDir + fileName);
       
    60 			// found a valid plugin file.
       
    61 			if (fileInfo.exists() && QLibrary::isLibrary(fileInfo.absoluteFilePath())) {
       
    62 			    CPPERF_LOG( QLatin1String("Valid plugin stub found: ") + fileInfo.absoluteFilePath() );
       
    63 				break;
       
    64 			}
       
    65 		}
       
    66     }
       
    67 
       
    68 	QPluginLoader loader(fileInfo.absoluteFilePath());
       
    69 	QObject *plugin = loader.instance();
       
    70 	if (!plugin) {
       
    71 		loader.unload();
       
    72 	}
       
    73 	
       
    74 	CPPERF_LOG( QLatin1String("Load plugin ") + (plugin ? QLatin1String("succeed.") : QLatin1String("failed.")) );
       
    75     
       
    76     return plugin;
       
    77 }
       
    78 
       
    79 /*!
       
    80     load a CpPluginInterface by a controlpanel plugin file.
       
    81     the plugin file can either absoulte file path or only file name.
       
    82     acceptable format:
       
    83         sampleplugin
       
    84         sampleplugin.qtplugin
       
    85         sampleplugin.dll
       
    86         C:/resource/qt/plugins/controlpanel/sampleplugin.qtplugin
       
    87         C:/resource/qt/plugins/controlpanel/sampleplugin.dll
       
    88  */
       
    89 CpPluginInterface *CpPluginLoader::loadCpPluginInterface(const QString &pluginFile)
       
    90 {
       
    91     return qobject_cast<CpPluginInterface*>(::loadPluginInterface(pluginFile));
       
    92 }
       
    93 
       
    94 /*!
       
    95     load a CpLauncherInterface by a controlpanel plugin file.
       
    96     the plugin file can either absoulte file path or only file name.
       
    97     acceptable format:
       
    98         sampleplugin
       
    99         sampleplugin.qtplugin
       
   100         sampleplugin.dll
       
   101         C:/resource/qt/plugins/controlpanel/sampleplugin.qtplugin
       
   102         C:/resource/qt/plugins/controlpanel/sampleplugin.dll
       
   103  */
       
   104 CpLauncherInterface *CpPluginLoader::loadCpLauncherInterface(const QString &pluginFile)
       
   105 {
       
   106     return qobject_cast<CpLauncherInterface*>(::loadPluginInterface(pluginFile));  
       
   107 }
       
   108 
       
   109