Qt 4.8
qpluginloader.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
59 
145  : QObject(parent), d(0), did_load(false)
146 {
147 }
148 
163  : QObject(parent), d(0), did_load(false)
164 {
165  setFileName(fileName);
166 }
167 
177 {
178  if (d)
179  d->release();
180 }
181 
203 {
204  if (!load())
205  return 0;
206  if (!d->inst && d->instance)
207  d->inst = d->instance();
208  return d->inst.data();
209 }
210 
222 {
223  if (!d || d->fileName.isEmpty())
224  return false;
225  if (did_load)
226  return d->pHnd && d->instance;
227  if (!d->isPlugin())
228  return false;
229  did_load = true;
230  return d->loadPlugin();
231 }
232 
233 
251 {
252  if (did_load) {
253  did_load = false;
254  return d->unload();
255  }
256  if (d) // Ouch
257  d->errorString = tr("The plugin was not loaded.");
258  return false;
259 }
260 
267 {
268  return d && d->pHnd && d->instance;
269 }
270 
293 {
294 #if defined(QT_SHARED)
295  QLibrary::LoadHints lh;
296  if (d) {
297  lh = d->loadHints;
298  d->release();
299  d = 0;
300  did_load = false;
301  }
302 
303 #if defined(Q_OS_SYMBIAN)
304  // In Symbian we actually look for plugin stub, so modify the filename
305  // to make canonicalFilePath find the file, if .dll is specified.
306  QFileInfo fi(fileName);
307 
308  if (fi.suffix() == QLatin1String("dll")) {
309  QString stubName = fileName;
310  stubName.chop(3);
311  stubName += QLatin1String("qtplugin");
312  fi = QFileInfo(stubName);
313  }
314 
315  QString fn = fi.canonicalFilePath();
316  // If not found directly, check also all the available drives
317  if (!fn.length()) {
318  QString stubPath(fi.fileName().length() ? fi.absoluteFilePath() : QString());
319  if (stubPath.length() > 1) {
320  if (stubPath.at(1).toAscii() == ':')
321  stubPath.remove(0,2);
322  QFileInfoList driveList(QDir::drives());
323  RFs rfs = qt_s60GetRFs();
324  foreach(const QFileInfo& drive, driveList) {
325  QString testFilePath(drive.absolutePath() + stubPath);
326  testFilePath = QDir::cleanPath(testFilePath);
327  // Use native Symbian code to check for file existence, because checking
328  // for file from under non-existent protected dir like E:/private/<uid> using
329  // QFile::exists causes platform security violations on most apps.
330  QString nativePath = QDir::toNativeSeparators(testFilePath);
331  TPtrC ptr(qt_QString2TPtrC(nativePath));
332  TUint attributes;
333  TInt err = rfs.Att(ptr, attributes);
334  if (err == KErrNone) {
335  fn = testFilePath;
336  break;
337  }
338  }
339  }
340  }
341 
342 #else
343  QString fn = QFileInfo(fileName).canonicalFilePath();
344 #endif
345 
347  d->loadHints = lh;
348  if (fn.isEmpty())
349  d->errorString = QLibrary::tr("The shared library was not found.");
350 #else
351  if (qt_debug_component()) {
352  qWarning("Cannot load %s into a statically linked Qt library.",
353  (const char*)QFile::encodeName(fileName));
354  }
355  Q_UNUSED(fileName);
356 #endif
357 }
358 
360 {
361  if (d)
362  return d->fileName;
363  return QString();
364 }
365 
375 {
376  return (!d || d->errorString.isEmpty()) ? tr("Unknown error") : d->errorString;
377 }
378 
380 Q_GLOBAL_STATIC(StaticInstanceFunctionList, staticInstanceFunctionList)
381 
382 
400 {
401  if (!d) {
402  d = QLibraryPrivate::findOrCreate(QString()); // ugly, but we need a d-ptr
403  d->errorString.clear();
404  }
405  d->loadHints = loadHints;
406 }
407 
408 QLibrary::LoadHints QPluginLoader::loadHints() const
409 {
410  if (!d) {
411  QPluginLoader *that = const_cast<QPluginLoader *>(this);
412  that->d = QLibraryPrivate::findOrCreate(QString()); // ugly, but we need a d-ptr
413  that->d->errorString.clear();
414  }
415  return d->loadHints;
416 }
417 
428 {
429  staticInstanceFunctionList()->append(function);
430 }
431 
437 {
438  QObjectList instances;
439  StaticInstanceFunctionList *functions = staticInstanceFunctionList();
440  if (functions) {
441  for (int i = 0; i < functions->count(); ++i)
442  instances.append((*functions)[i]());
443  }
444  return instances;
445 }
446 
448 
449 #endif // QT_NO_LIBRARY
double d
Definition: qnumeric_p.h:62
bool isPlugin(QSettings *settings=0)
Definition: qlibrary.cpp:669
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString errorString() const
Returns a text string with the description of the last error that occurred.
QLibrary::LoadHints loadHints() const
bool load()
Loads the plugin and returns true if the plugin was loaded successfully; otherwise returns false...
QWeakPointer< QObject > inst
Definition: qlibrary_p.h:94
static QLibraryPrivate * findOrCreate(const QString &fileName, const QString &version=QString())
Definition: qlibrary.cpp:438
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QtPluginInstanceFunction instance
Definition: qlibrary_p.h:95
QString fileName() const
Returns the name of the file, excluding the path.
Definition: qfileinfo.cpp:726
~QPluginLoader()
Destroys the QPluginLoader object.
QString absoluteFilePath() const
Returns an absolute path including the file name.
Definition: qfileinfo.cpp:534
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction(QtPluginInstanceFunction function)
Registers the given function with the plugin loader.
QList< QtPluginInstanceFunction > StaticInstanceFunctionList
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
T * data() const
static QObjectList staticInstances()
Returns a list of static plugin instances (root components) held by the plugin loader.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QObject *(* QtPluginInstanceFunction)()
Definition: qplugin.h:62
QString fileName
Definition: qlibrary_p.h:83
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
QString canonicalFilePath() const
Returns the canonical path including the file name, i.e.
Definition: qfileinfo.cpp:551
Q_CORE_EXPORT void qWarning(const char *,...)
void setFileName(const QString &fileName)
const T * ptr(const T &t)
static QFileInfoList drives()
Returns a list of the root directories on this system.
Definition: qdir.cpp:1812
static QString cleanPath(const QString &path)
Removes all multiple directory separators "/" and resolves any "."s or ".."s found in the path...
Definition: qdir.cpp:2082
HINSTANCE pHnd
Definition: qlibrary_p.h:81
void setLoadHints(QLibrary::LoadHints loadHints)
QString errorString
Definition: qlibrary_p.h:99
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QString suffix() const
Returns the suffix of the file.
Definition: qfileinfo.cpp:834
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
bool loadPlugin()
Definition: qlibrary.cpp:517
bool isLoaded() const
Returns true if the plugin is loaded; otherwise returns false.
QLibraryPrivate * d
Definition: qpluginloader.h:89
QObject * instance()
Returns the root component object of the plugin.
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
QString fileName() const
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
QPluginLoader(QObject *parent=0)
Constructs a plugin loader with the given parent.
QLibrary::LoadHints loadHints
Definition: qlibrary_p.h:100
bool unload()
Unloads the plugin and returns true if the plugin could be unloaded; otherwise returns false...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QString absolutePath() const
Returns a file&#39;s path absolute path.
Definition: qfileinfo.cpp:577
bool qt_debug_component()
Definition: qlibrary.cpp:1319
The QLibrary class loads shared libraries at runtime.
Definition: qlibrary.h:62
The QPluginLoader class loads a plugin at run-time.
Definition: qpluginloader.h:62