1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qplatformdefs.h"
|
---|
43 |
|
---|
44 | #include "qplugin.h"
|
---|
45 | #include "qpluginloader.h"
|
---|
46 | #include <qfileinfo.h>
|
---|
47 | #include "qlibrary_p.h"
|
---|
48 | #include "qdebug.h"
|
---|
49 | #include "qdir.h"
|
---|
50 |
|
---|
51 | #if defined(Q_OS_SYMBIAN)
|
---|
52 | # include <f32file.h>
|
---|
53 | # include "private/qcore_symbian_p.h"
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #ifndef QT_NO_LIBRARY
|
---|
57 |
|
---|
58 | QT_BEGIN_NAMESPACE
|
---|
59 |
|
---|
60 | /*!
|
---|
61 | \class QPluginLoader
|
---|
62 | \reentrant
|
---|
63 | \brief The QPluginLoader class loads a plugin at run-time.
|
---|
64 |
|
---|
65 |
|
---|
66 | \ingroup plugins
|
---|
67 |
|
---|
68 | QPluginLoader provides access to a \l{How to Create Qt
|
---|
69 | Plugins}{Qt plugin}. A Qt plugin is stored in a shared library (a
|
---|
70 | DLL) and offers these benefits over shared libraries accessed
|
---|
71 | using QLibrary:
|
---|
72 |
|
---|
73 | \list
|
---|
74 | \o QPluginLoader checks that a plugin is linked against the same
|
---|
75 | version of Qt as the application.
|
---|
76 | \o QPluginLoader provides direct access to a root component object
|
---|
77 | (instance()), instead of forcing you to resolve a C function manually.
|
---|
78 | \endlist
|
---|
79 |
|
---|
80 | An instance of a QPluginLoader object operates on a single shared
|
---|
81 | library file, which we call a plugin. It provides access to the
|
---|
82 | functionality in the plugin in a platform-independent way. To
|
---|
83 | specify which plugin to load, either pass a file name in
|
---|
84 | the constructor or set it with setFileName().
|
---|
85 |
|
---|
86 | The most important functions are load() to dynamically load the
|
---|
87 | plugin file, isLoaded() to check whether loading was successful,
|
---|
88 | and instance() to access the root component in the plugin. The
|
---|
89 | instance() function implicitly tries to load the plugin if it has
|
---|
90 | not been loaded yet. Multiple instances of QPluginLoader can be
|
---|
91 | used to access the same physical plugin.
|
---|
92 |
|
---|
93 | Once loaded, plugins remain in memory until all instances of
|
---|
94 | QPluginLoader has been unloaded, or until the application
|
---|
95 | terminates. You can attempt to unload a plugin using unload(),
|
---|
96 | but if other instances of QPluginLoader are using the same
|
---|
97 | library, the call will fail, and unloading will only happen when
|
---|
98 | every instance has called unload(). Right before the unloading
|
---|
99 | happen, the root component will also be deleted.
|
---|
100 |
|
---|
101 | In order to speed up loading and validation of plugins, some of
|
---|
102 | the information that is collected during loading is cached in
|
---|
103 | persistent memory (through QSettings). For instance, the result
|
---|
104 | of a load operation (e.g. succeeded or failed) is stored in the
|
---|
105 | cache, so that subsequent load operations don't try to load an
|
---|
106 | invalid plugin. However, if the "last modified" timestamp of
|
---|
107 | a plugin has changed, the plugin's cache entry is invalidated
|
---|
108 | and the plugin is reloaded regardless of the values in the cache
|
---|
109 | entry. The cache entry is then updated with the new result of the
|
---|
110 | load operation.
|
---|
111 |
|
---|
112 | This also means that the timestamp must be updated each time the
|
---|
113 | plugin or any dependent resources (such as a shared library) is
|
---|
114 | updated, since the dependent resources might influence the result
|
---|
115 | of loading a plugin.
|
---|
116 |
|
---|
117 | See \l{How to Create Qt Plugins} for more information about
|
---|
118 | how to make your application extensible through plugins.
|
---|
119 |
|
---|
120 | Note that the QPluginLoader cannot be used if your application is
|
---|
121 | statically linked against Qt. In this case, you will also have to
|
---|
122 | link to plugins statically. You can use QLibrary if you need to
|
---|
123 | load dynamic libraries in a statically linked application.
|
---|
124 |
|
---|
125 | \note In Symbian the plugin stub files must be used whenever a
|
---|
126 | path to plugin is needed. For the purposes of loading plugins,
|
---|
127 | the stubs can be considered to have the same name as the actual
|
---|
128 | plugin binary. In practice they have ".qtplugin" extension
|
---|
129 | instead of ".dll", but this difference is handled transparently
|
---|
130 | by QPluginLoader and QLibrary to avoid need for Symbian specific
|
---|
131 | plugin handling in most Qt applications. Plugin stubs are needed
|
---|
132 | because Symbian Platform Security denies all access to the directory
|
---|
133 | where the actual plugin binaries are located.
|
---|
134 |
|
---|
135 | \sa QLibrary, {Plug & Paint Example}
|
---|
136 | */
|
---|
137 |
|
---|
138 | /*!
|
---|
139 | Constructs a plugin loader with the given \a parent.
|
---|
140 | */
|
---|
141 | QPluginLoader::QPluginLoader(QObject *parent)
|
---|
142 | : QObject(parent), d(0), did_load(false)
|
---|
143 | {
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*!
|
---|
147 | Constructs a plugin loader with the given \a parent that will
|
---|
148 | load the plugin specified by \a fileName.
|
---|
149 |
|
---|
150 | To be loadable, the file's suffix must be a valid suffix for a
|
---|
151 | loadable library in accordance with the platform, e.g. \c .so on
|
---|
152 | Unix, - \c .dylib on Mac OS X, and \c .dll on Windows. The suffix
|
---|
153 | can be verified with QLibrary::isLibrary().
|
---|
154 |
|
---|
155 | Note: In Symbian the \a fileName must point to plugin stub file.
|
---|
156 |
|
---|
157 | \sa setFileName()
|
---|
158 | */
|
---|
159 | QPluginLoader::QPluginLoader(const QString &fileName, QObject *parent)
|
---|
160 | : QObject(parent), d(0), did_load(false)
|
---|
161 | {
|
---|
162 | setFileName(fileName);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /*!
|
---|
166 | Destroys the QPluginLoader object.
|
---|
167 |
|
---|
168 | Unless unload() was called explicitly, the plugin stays in memory
|
---|
169 | until the application terminates.
|
---|
170 |
|
---|
171 | \sa isLoaded(), unload()
|
---|
172 | */
|
---|
173 | QPluginLoader::~QPluginLoader()
|
---|
174 | {
|
---|
175 | if (d)
|
---|
176 | d->release();
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*!
|
---|
180 | Returns the root component object of the plugin. The plugin is
|
---|
181 | loaded if necessary. The function returns 0 if the plugin could
|
---|
182 | not be loaded or if the root component object could not be
|
---|
183 | instantiated.
|
---|
184 |
|
---|
185 | If the root component object was destroyed, calling this function
|
---|
186 | creates a new instance.
|
---|
187 |
|
---|
188 | The root component, returned by this function, is not deleted when
|
---|
189 | the QPluginLoader is destroyed. If you want to ensure that the root
|
---|
190 | component is deleted, you should call unload() as soon you don't
|
---|
191 | need to access the core component anymore. When the library is
|
---|
192 | finally unloaded, the root component will automatically be deleted.
|
---|
193 |
|
---|
194 | The component object is a QObject. Use qobject_cast() to access
|
---|
195 | interfaces you are interested in.
|
---|
196 |
|
---|
197 | \sa load()
|
---|
198 | */
|
---|
199 | QObject *QPluginLoader::instance()
|
---|
200 | {
|
---|
201 | if (!load())
|
---|
202 | return 0;
|
---|
203 | if (d->instance)
|
---|
204 | return d->instance();
|
---|
205 | return 0;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /*!
|
---|
209 | Loads the plugin and returns true if the plugin was loaded
|
---|
210 | successfully; otherwise returns false. Since instance() always
|
---|
211 | calls this function before resolving any symbols it is not
|
---|
212 | necessary to call it explicitly. In some situations you might want
|
---|
213 | the plugin loaded in advance, in which case you would use this
|
---|
214 | function.
|
---|
215 |
|
---|
216 | \sa unload()
|
---|
217 | */
|
---|
218 | bool QPluginLoader::load()
|
---|
219 | {
|
---|
220 | if (!d || d->fileName.isEmpty())
|
---|
221 | return false;
|
---|
222 | if (did_load)
|
---|
223 | return d->pHnd && d->instance;
|
---|
224 | if (!d->isPlugin())
|
---|
225 | return false;
|
---|
226 | did_load = true;
|
---|
227 | return d->loadPlugin();
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /*!
|
---|
232 | Unloads the plugin and returns true if the plugin could be
|
---|
|
---|