|
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 <cppluginplatinterface.h> |
|
24 #include "cpbasepath.h" |
|
25 #include "cputility.h" |
|
26 #include <cplogger.h> |
|
27 |
|
28 |
|
29 /*! |
|
30 \class CpPluginLoader |
|
31 \brief The CpPluginLoader class loads a controlpanel plugin at run-time. |
|
32 */ |
|
33 |
|
34 #ifdef WIN32 |
|
35 #define PLUGINFILE_SUFFIX "dll" |
|
36 #else |
|
37 #define PLUGINFILE_SUFFIX "qtplugin" |
|
38 #endif |
|
39 |
|
40 template <typename PLUGIN> |
|
41 static PLUGIN* loadPlugin(const QString &pluginFile) |
|
42 { |
|
43 QFileInfo fileInfo(pluginFile); |
|
44 |
|
45 if (!fileInfo.isAbsolute()) { |
|
46 QString fileName = fileInfo.fileName(); |
|
47 if (fileInfo.suffix().compare(PLUGINFILE_SUFFIX,Qt::CaseInsensitive)) { |
|
48 fileName = fileInfo.baseName() + '.' + PLUGINFILE_SUFFIX; |
|
49 } |
|
50 |
|
51 QStringList pluginDirs = CpUtility::pluginDirectories(); |
|
52 foreach(const QString &pluginDir,pluginDirs) { |
|
53 fileInfo.setFile(pluginDir + fileName); |
|
54 if (fileInfo.exists() && QLibrary::isLibrary(fileInfo.absoluteFilePath())) { |
|
55 break; |
|
56 } |
|
57 } |
|
58 } |
|
59 |
|
60 QPluginLoader loader(fileInfo.absoluteFilePath()); |
|
61 PLUGIN *plugin = qobject_cast<PLUGIN*> (loader.instance()); |
|
62 if (!plugin) { |
|
63 loader.unload(); |
|
64 } |
|
65 |
|
66 return plugin; |
|
67 } |
|
68 |
|
69 /*! |
|
70 load a controlpanel plugin by plugin file. |
|
71 the plugin file can either absoulte file path or only file name. |
|
72 acceptable format: |
|
73 sampleplugin |
|
74 sampleplugin.qtplugin |
|
75 sampleplugin.dll |
|
76 C:/resource/qt/plugins/controlpanel/sampleplugin.qtplugin |
|
77 C:/resource/qt/plugins/controlpanel/sampleplugin.dll |
|
78 */ |
|
79 |
|
80 CpPluginInterface *CpPluginLoader::loadCpPlugin(const QString &pluginFile) |
|
81 { |
|
82 return loadPlugin<CpPluginInterface>(pluginFile); |
|
83 } |
|
84 |
|
85 |
|
86 /*! |
|
87 \deprecated CpPluginPlatInterface *CpPluginLoader::loadPlatCpPlugin(const QString &) is deprecated. |
|
88 please use CpPluginInterface to implement controlpanel plugin and use CpPluginLoader::loadCpPlugin(const QString &) to load the plugin. |
|
89 */ |
|
90 |
|
91 CpPluginPlatInterface *CpPluginLoader::loadPlatCpPlugin(const QString &pluginFile) |
|
92 { |
|
93 return loadPlugin<CpPluginPlatInterface>(pluginFile); |
|
94 } |
|
95 |
|
96 |