|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qfactoryloader_p.h" |
|
43 |
|
44 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) |
|
45 #include "qfactoryinterface.h" |
|
46 #include "qmap.h" |
|
47 #include <qdir.h> |
|
48 #include <qsettings.h> |
|
49 #include <qdebug.h> |
|
50 #include "qmutex.h" |
|
51 #include "qplugin.h" |
|
52 #include "qpluginloader.h" |
|
53 #include "private/qobject_p.h" |
|
54 #include "private/qcoreapplication_p.h" |
|
55 |
|
56 QT_BEGIN_NAMESPACE |
|
57 |
|
58 Q_GLOBAL_STATIC(QList<QFactoryLoader *>, qt_factory_loaders) |
|
59 |
|
60 Q_GLOBAL_STATIC_WITH_ARGS(QMutex, qt_factoryloader_mutex, (QMutex::Recursive)) |
|
61 |
|
62 class QFactoryLoaderPrivate : public QObjectPrivate |
|
63 { |
|
64 Q_DECLARE_PUBLIC(QFactoryLoader) |
|
65 public: |
|
66 QFactoryLoaderPrivate(){} |
|
67 ~QFactoryLoaderPrivate(); |
|
68 mutable QMutex mutex; |
|
69 QByteArray iid; |
|
70 QList<QLibraryPrivate*> libraryList; |
|
71 QMap<QString,QLibraryPrivate*> keyMap; |
|
72 QStringList keyList; |
|
73 QString suffix; |
|
74 Qt::CaseSensitivity cs; |
|
75 QStringList loadedPaths; |
|
76 |
|
77 void unloadPath(const QString &path); |
|
78 }; |
|
79 |
|
80 QFactoryLoaderPrivate::~QFactoryLoaderPrivate() |
|
81 { |
|
82 for (int i = 0; i < libraryList.count(); ++i) |
|
83 libraryList.at(i)->release(); |
|
84 } |
|
85 |
|
86 QFactoryLoader::QFactoryLoader(const char *iid, |
|
87 const QString &suffix, |
|
88 Qt::CaseSensitivity cs) |
|
89 : QObject(*new QFactoryLoaderPrivate) |
|
90 { |
|
91 moveToThread(QCoreApplicationPrivate::mainThread()); |
|
92 Q_D(QFactoryLoader); |
|
93 d->iid = iid; |
|
94 d->cs = cs; |
|
95 d->suffix = suffix; |
|
96 |
|
97 |
|
98 QMutexLocker locker(qt_factoryloader_mutex()); |
|
99 update(); |
|
100 qt_factory_loaders()->append(this); |
|
101 } |
|
102 |
|
103 |
|
104 |
|
105 void QFactoryLoader::update() |
|
106 { |
|
107 #ifdef QT_SHARED |
|
108 Q_D(QFactoryLoader); |
|
109 QStringList paths = QCoreApplication::libraryPaths(); |
|
110 QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); |
|
111 for (int i = 0; i < paths.count(); ++i) { |
|
112 const QString &pluginDir = paths.at(i); |
|
113 // Already loaded, skip it... |
|
114 if (d->loadedPaths.contains(pluginDir)) |
|
115 continue; |
|
116 d->loadedPaths << pluginDir; |
|
117 |
|
118 QString path = pluginDir + d->suffix; |
|
119 if (!QDir(path).exists(QLatin1String("."))) |
|
120 continue; |
|
121 |
|
122 QStringList plugins = QDir(path).entryList(QDir::Files); |
|
123 QLibraryPrivate *library = 0; |
|
124 for (int j = 0; j < plugins.count(); ++j) { |
|
125 QString fileName = QDir::cleanPath(path + QLatin1Char('/') + plugins.at(j)); |
|
126 if (qt_debug_component()) { |
|
127 qDebug() << "QFactoryLoader::QFactoryLoader() looking at" << fileName; |
|
128 } |
|
129 library = QLibraryPrivate::findOrCreate(QFileInfo(fileName).canonicalFilePath()); |
|
130 if (!library->isPlugin(&settings)) { |
|
131 if (qt_debug_component()) { |
|
132 qDebug() << library->errorString; |
|
133 qDebug() << " not a plugin"; |
|
134 } |
|
135 library->release(); |
|
136 continue; |
|
137 } |
|
138 QString regkey = QString::fromLatin1("Qt Factory Cache %1.%2/%3:/%4") |
|
139 .arg((QT_VERSION & 0xff0000) >> 16) |
|
140 .arg((QT_VERSION & 0xff00) >> 8) |
|
141 .arg(QLatin1String(d->iid)) |
|
142 .arg(fileName); |
|
143 QStringList reg, keys; |
|
144 reg = settings.value(regkey).toStringList(); |
|
145 if (reg.count() && library->lastModified == reg[0]) { |
|
146 keys = reg; |
|
147 keys.removeFirst(); |
|
148 } else { |
|
149 if (!library->loadPlugin()) { |
|
150 if (qt_debug_component()) { |
|
151 qDebug() << library->errorString; |
|
152 qDebug() << " could not load"; |
|
153 } |
|
154 library->release(); |
|
155 continue; |
|
156 } |
|
157 QObject *instance = library->instance(); |
|
158 if (!instance) |
|
159 // ignore plugins that have a valid signature but cannot be loaded. |
|
160 continue; |
|
161 QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instance); |
|
162 if (instance && factory && instance->qt_metacast(d->iid)) |
|
163 keys = factory->keys(); |
|
164 if (keys.isEmpty()) |
|
165 library->unload(); |
|
166 reg.clear(); |
|
167 reg << library->lastModified; |
|
168 reg += keys; |
|
169 settings.setValue(regkey, reg); |
|
170 } |
|
171 if (qt_debug_component()) { |
|
172 qDebug() << "keys" << keys; |
|
173 } |
|
174 |
|
175 if (keys.isEmpty()) { |
|
176 library->release(); |
|
177 continue; |
|
178 } |
|
179 d->libraryList += library; |
|
180 for (int k = 0; k < keys.count(); ++k) { |
|
181 // first come first serve, unless the first |
|
182 // library was built with a future Qt version, |
|
183 // whereas the new one has a Qt version that fits |
|
184 // better |
|
185 QString key = keys.at(k); |
|
186 if (!d->cs) |
|
187 key = key.toLower(); |
|
188 QLibraryPrivate *previous = d->keyMap.value(key); |
|
189 if (!previous || (previous->qt_version > QT_VERSION && library->qt_version <= QT_VERSION)) { |
|
190 d->keyMap[key] = library; |
|
191 d->keyList += keys.at(k); |
|
192 } |
|
193 } |
|
194 } |
|
195 } |
|
196 #else |
|
197 Q_D(QFactoryLoader); |
|
198 if (qt_debug_component()) { |
|
199 qDebug() << "QFactoryLoader::QFactoryLoader() ignoring" << d->iid |
|
200 << "since plugins are disabled in static builds"; |
|
201 } |
|
202 #endif |
|
203 } |
|
204 |
|
205 QFactoryLoader::~QFactoryLoader() |
|
206 { |
|
207 QMutexLocker locker(qt_factoryloader_mutex()); |
|
208 qt_factory_loaders()->removeAll(this); |
|
209 } |
|
210 |
|
211 QStringList QFactoryLoader::keys() const |
|
212 { |
|
213 Q_D(const QFactoryLoader); |
|
214 QMutexLocker locker(&d->mutex); |
|
215 QStringList keys = d->keyList; |
|
216 QObjectList instances = QPluginLoader::staticInstances(); |
|
217 for (int i = 0; i < instances.count(); ++i) |
|
218 if (QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instances.at(i))) |
|
219 if (instances.at(i)->qt_metacast(d->iid)) |
|
220 keys += factory->keys(); |
|
221 return keys; |
|
222 } |
|
223 |
|
224 QObject *QFactoryLoader::instance(const QString &key) const |
|
225 { |
|
226 Q_D(const QFactoryLoader); |
|
227 QMutexLocker locker(&d->mutex); |
|
228 QObjectList instances = QPluginLoader::staticInstances(); |
|
229 for (int i = 0; i < instances.count(); ++i) |
|
230 if (QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instances.at(i))) |
|
231 if (instances.at(i)->qt_metacast(d->iid) && factory->keys().contains(key, Qt::CaseInsensitive)) |
|
232 return instances.at(i); |
|
233 |
|
234 QString lowered = d->cs ? key : key.toLower(); |
|
235 if (QLibraryPrivate* library = d->keyMap.value(lowered)) { |
|
236 if (library->instance || library->loadPlugin()) { |
|
237 if (QObject *obj = library->instance()) { |
|
238 if (obj && !obj->parent()) |
|
239 obj->moveToThread(QCoreApplicationPrivate::mainThread()); |
|
240 return obj; |
|
241 } |
|
242 } |
|
243 } |
|
244 return 0; |
|
245 } |
|
246 |
|
247 void QFactoryLoader::refreshAll() |
|
248 { |
|
249 QMutexLocker locker(qt_factoryloader_mutex()); |
|
250 QList<QFactoryLoader *> *loaders = qt_factory_loaders(); |
|
251 for (QList<QFactoryLoader *>::const_iterator it = loaders->constBegin(); |
|
252 it != loaders->constEnd(); ++it) { |
|
253 (*it)->update(); |
|
254 } |
|
255 } |
|
256 |
|
257 QT_END_NAMESPACE |
|
258 |
|
259 #endif // QT_NO_LIBRARY |