39 ** |
39 ** |
40 ****************************************************************************/ |
40 ****************************************************************************/ |
41 |
41 |
42 #include "qsensorpluginloader_p.h" |
42 #include "qsensorpluginloader_p.h" |
43 #include <QtCore/qcoreapplication.h> |
43 #include <QtCore/qcoreapplication.h> |
44 #include <QtCore/qpluginloader.h> |
|
45 #include <QtCore/qdir.h> |
44 #include <QtCore/qdir.h> |
46 #include <QtCore/qdebug.h> |
45 #include <QtCore/qdebug.h> |
47 |
46 |
48 #include "qsensorplugin.h" |
47 #include "qsensorplugin.h" |
49 |
48 |
54 { |
53 { |
55 m_location = location + QLatin1String("/"); |
54 m_location = location + QLatin1String("/"); |
56 load(); |
55 load(); |
57 } |
56 } |
58 |
57 |
|
58 QSensorPluginLoader::~QSensorPluginLoader() |
|
59 { |
|
60 Q_FOREACH (QPluginLoader *loader, m_loaders) { |
|
61 bool ok = loader->unload(); |
|
62 if (!ok) qWarning() << "Cannot unload" << loader->fileName(); |
|
63 delete loader; |
|
64 } |
|
65 } |
|
66 |
59 void QSensorPluginLoader::load() |
67 void QSensorPluginLoader::load() |
60 { |
68 { |
61 if (!m_plugins.isEmpty()) |
69 if (!m_plugins.isEmpty()) |
62 return; |
70 return; |
63 |
71 |
64 QStringList paths = QCoreApplication::libraryPaths(); |
72 QStringList paths = QCoreApplication::libraryPaths(); |
65 |
73 |
66 Q_FOREACH (QString const &path, paths) { |
74 Q_FOREACH (QString const &path, paths) { |
67 QString pluginPathName(path + m_location); |
75 QString pluginPathName(path + m_location); |
68 QDir pluginDir(pluginPathName); |
76 QDir pluginDir(pluginPathName); |
69 |
77 |
70 if (!pluginDir.exists()) |
78 if (!pluginDir.exists()) |
71 continue; |
79 continue; |
72 |
80 |
73 Q_FOREACH (QString pluginLib, pluginDir.entryList(QDir::Files)) { |
81 Q_FOREACH (QString pluginLib, pluginDir.entryList(QDir::Files)) { |
74 QPluginLoader loader(pluginPathName + pluginLib); |
82 QPluginLoader *loader = new QPluginLoader(pluginPathName + pluginLib); |
75 |
83 |
76 QObject *o = loader.instance(); |
84 QObject *o = loader->instance(); |
77 if (o != 0 && o->qt_metacast(m_iid) != 0) { |
85 if (o != 0 && o->qt_metacast(m_iid) != 0) { |
78 QSensorPluginInterface *p = qobject_cast<QSensorPluginInterface*>(o); |
86 QSensorPluginInterface *p = qobject_cast<QSensorPluginInterface*>(o); |
79 if (p != 0) { |
87 if (p != 0) { |
80 m_plugins << p; |
88 m_plugins << p; |
|
89 m_loaders << loader; |
|
90 } else { |
|
91 loader->unload(); |
|
92 delete loader; |
81 } |
93 } |
82 |
94 |
83 continue; |
95 continue; |
84 } else { |
96 } else { |
85 qWarning() << "QSensorPluginLoader: Failed to load plugin: " << pluginLib << loader.errorString(); |
97 qWarning() << "QSensorPluginLoader: Failed to load plugin: " << pluginLib << loader->errorString(); |
86 } |
98 } |
87 delete o; |
99 delete o; |
88 loader.unload(); |
100 loader->unload(); |
|
101 delete loader; |
89 } |
102 } |
90 } |
103 } |
91 } |
104 } |
92 |
105 |
93 QTM_END_NAMESPACE |
106 QTM_END_NAMESPACE |