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