From 4e4403d69c60ea6429b3ad5e7898a84b1dc92be5 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 8 May 2012 13:14:09 +0200 Subject: No longer use deprecated methods for plugin loading. Change-Id: I19c66b1c41ea4dd236726c86d7d071b210ec9244 Reviewed-by: Lars Knoll --- src/gui/accessible/qaccessible.cpp | 23 +++------ src/gui/accessible/qplatformaccessibility_qpa.cpp | 22 +++++--- src/gui/image/qimagereader.cpp | 59 +++++++++++++--------- src/gui/image/qimagewriter.cpp | 54 +++++++++++++++----- src/gui/image/qpicture.cpp | 14 +++-- src/gui/kernel/qgenericpluginfactory_qpa.cpp | 19 ++++--- src/gui/kernel/qplatformintegrationfactory_qpa.cpp | 36 ++++++------- src/gui/kernel/qplatformthemefactory_qpa.cpp | 34 ++++++------- 8 files changed, 153 insertions(+), 108 deletions(-) (limited to 'src/gui') diff --git a/src/gui/accessible/qaccessible.cpp b/src/gui/accessible/qaccessible.cpp index af75693ca2..b84ecefd73 100644 --- a/src/gui/accessible/qaccessible.cpp +++ b/src/gui/accessible/qaccessible.cpp @@ -570,35 +570,28 @@ QAccessible::RootObjectHandler QAccessible::installRootObjectHandler(RootObjectH QAccessibleInterface *QAccessible::queryAccessibleInterface(QObject *object) { accessibility_active = true; - QAccessibleInterface *iface = 0; if (!object) return 0; const QMetaObject *mo = object->metaObject(); while (mo) { - const QLatin1String cn(mo->className()); + const QString cn = QLatin1String(mo->className()); for (int i = qAccessibleFactories()->count(); i > 0; --i) { InterfaceFactory factory = qAccessibleFactories()->at(i - 1); - iface = factory(cn, object); - if (iface) + if (QAccessibleInterface *iface = factory(cn, object)) return iface; } #ifndef QT_NO_LIBRARY - QAccessibleFactoryInterface *factory = qobject_cast(loader()->instance(cn)); - if (factory) { - iface = factory->create(cn, object); - if (iface) - return iface; - } + if (QAccessibleInterface * iface = qLoadPlugin1(loader(), cn, object)) + return iface; #endif mo = mo->superClass(); } - if (!iface) { - if (object == qApp) - iface = new QAccessibleApplication; - } - return iface; + if (object == qApp) + return new QAccessibleApplication; + + return 0; } /*! diff --git a/src/gui/accessible/qplatformaccessibility_qpa.cpp b/src/gui/accessible/qplatformaccessibility_qpa.cpp index 7ea5a5d2c8..271f324000 100644 --- a/src/gui/accessible/qplatformaccessibility_qpa.cpp +++ b/src/gui/accessible/qplatformaccessibility_qpa.cpp @@ -112,15 +112,21 @@ void QPlatformAccessibility::initialize() isInit = true; // ### not atomic #ifndef QT_NO_LIBRARY - const QStringList l = bridgeloader()->keys(); - for (int i = 0; i < l.count(); ++i) { - if (QAccessibleBridgeFactoryInterface *factory = - qobject_cast(bridgeloader()->instance(l.at(i)))) { - QAccessibleBridge * bridge = factory->create(l.at(i)); - if (bridge) { - bridges()->append(bridge); - } + typedef QMultiMap PluginKeyMap; + typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; + + const PluginKeyMap keyMap = bridgeloader()->keyMap(); + QAccessibleBridgeFactoryInterface *factory = 0; + int i = -1; + const PluginKeyMapConstIterator cend = keyMap.constEnd(); + for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) { + if (it.key() != i) { + i = it.key(); + factory = qobject_cast(bridgeloader()->instance(i)); } + if (factory) + if (QAccessibleBridge *bridge = factory->create(it.value())) + bridges()->append(bridge); } #endif } diff --git a/src/gui/image/qimagereader.cpp b/src/gui/image/qimagereader.cpp index fe14a45dcb..02c01f9866 100644 --- a/src/gui/image/qimagereader.cpp +++ b/src/gui/image/qimagereader.cpp @@ -224,15 +224,18 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device, QImageIOHandler *handler = 0; #ifndef QT_NO_LIBRARY + typedef QMultiMap PluginKeyMap; + typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; + // check if we have plugins that support the image format QFactoryLoader *l = loader(); - QStringList keys = l->keys(); + const PluginKeyMap keyMap = l->keyMap(); #endif QByteArray suffix; #ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler( device =" << (void *)device << ", format =" << format << ")," - << keys.size() << "plugins available: " << keys; + << keyMap.values().size() << "plugins available: " << keyMap.values(); #endif #ifndef QT_NO_LIBRARY @@ -246,7 +249,7 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device, qDebug() << "QImageReader::createReadHandler: device is a file:" << file->fileName(); #endif if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) { - int index = keys.indexOf(QString::fromLatin1(suffix)); + const int index = keyMap.key(QString::fromLatin1(suffix), -1); if (index != -1) { #ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: suffix recognized; the" @@ -269,13 +272,16 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device, // check if the plugin that claims support for this format can load // from this device with this format. const qint64 pos = device ? device->pos() : 0; - QImageIOPlugin *plugin = qobject_cast(l->instance(QString::fromLatin1(suffix))); - if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) { - handler = plugin->create(device, testFormat); + const int index = keyMap.key(QString::fromLatin1(suffix), -1); + if (index != -1) { + QImageIOPlugin *plugin = qobject_cast(l->instance(index)); + if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) { + handler = plugin->create(device, testFormat); #ifdef QIMAGEREADER_DEBUG - qDebug() << "QImageReader::createReadHandler: using the" << suffix - << "plugin"; + qDebug() << "QImageReader::createReadHandler: using the" << suffix + << "plugin"; #endif + } } if (device && !device->isSequential()) device->seek(pos); @@ -287,9 +293,10 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device, const qint64 pos = device ? device->pos() : 0; if (autoDetectImageFormat) { - for (int i = 0; i < keys.size(); ++i) { + const int keyCount = keyMap.keys().size(); + for (int i = 0; i < keyCount; ++i) { if (i != suffixPluginIndex) { - QImageIOPlugin *plugin = qobject_cast(l->instance(keys.at(i))); + QImageIOPlugin *plugin = qobject_cast(l->instance(i)); if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) { #ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: the" << keys.at(i) << "plugin can read this format"; @@ -300,12 +307,15 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device, } } } else { - QImageIOPlugin *plugin = qobject_cast(l->instance(QLatin1String(testFormat))); - if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) { + const int testIndex = keyMap.key(QLatin1String(testFormat), -1); + if (testIndex != -1) { + QImageIOPlugin *plugin = qobject_cast(l->instance(testIndex)); + if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) { #ifdef QIMAGEREADER_DEBUG - qDebug() << "QImageReader::createReadHandler: the" << testFormat << "plugin can read this format"; + qDebug() << "QImageReader::createReadHandler: the" << testFormat << "plugin can read this format"; #endif - handler = plugin->create(device, testFormat); + handler = plugin->create(device, testFormat); + } } } if (device && !device->isSequential()) @@ -363,9 +373,10 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device, if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) { // check if any of our plugins recognize the file from its contents. const qint64 pos = device ? device->pos() : 0; - for (int i = 0; i < keys.size(); ++i) { + const int keyCount = keyMap.keys().size(); + for (int i = 0; i < keyCount; ++i) { if (i != suffixPluginIndex) { - QImageIOPlugin *plugin = qobject_cast(l->instance(keys.at(i))); + QImageIOPlugin *plugin = qobject_cast(l->instance(i)); if (plugin && plugin->capabilities(device, QByteArray()) & QImageIOPlugin::CanRead) { handler = plugin->create(device, testFormat); #ifdef QIMAGEREADER_DEBUG @@ -1413,6 +1424,12 @@ QByteArray QImageReader::imageFormat(QIODevice *device) return format; } +#ifndef QT_NO_LIBRARY +void supportedImageHandlerFormats(QFactoryLoader *loader, + QImageIOPlugin::Capability cap, + QSet *result); +#endif + /*! Returns the list of image formats supported by QImageReader. @@ -1442,6 +1459,7 @@ QByteArray QImageReader::imageFormat(QIODevice *device) \sa setFormat(), QImageWriter::supportedImageFormats(), QImageIOPlugin */ + QList QImageReader::supportedImageFormats() { QSet formats; @@ -1449,14 +1467,7 @@ QList QImageReader::supportedImageFormats() formats << _qt_BuiltInFormats[i].extension; #ifndef QT_NO_LIBRARY - QFactoryLoader *l = loader(); - QStringList keys = l->keys(); - - for (int i = 0; i < keys.count(); ++i) { - QImageIOPlugin *plugin = qobject_cast(l->instance(keys.at(i))); - if (plugin && plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanRead) - formats << keys.at(i).toLatin1(); - } + supportedImageHandlerFormats(loader(), QImageIOPlugin::CanRead, &formats); #endif // QT_NO_LIBRARY QList sortedFormats; diff --git a/src/gui/image/qimagewriter.cpp b/src/gui/image/qimagewriter.cpp index 8395f9d216..43a93d75c2 100644 --- a/src/gui/image/qimagewriter.cpp +++ b/src/gui/image/qimagewriter.cpp @@ -136,9 +136,12 @@ static QImageIOHandler *createWriteHandlerHelper(QIODevice *device, QImageIOHandler *handler = 0; #ifndef QT_NO_LIBRARY + typedef QMultiMap PluginKeyMap; + typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; + // check if any plugins can write the image QFactoryLoader *l = loader(); - QStringList keys = l->keys(); + const PluginKeyMap keyMap = l->keyMap(); int suffixPluginIndex = -1; #endif @@ -149,7 +152,7 @@ static QImageIOHandler *createWriteHandlerHelper(QIODevice *device, if (QFile *file = qobject_cast(device)) { if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) { #ifndef QT_NO_LIBRARY - int index = keys.indexOf(QString::fromLatin1(suffix)); + const int index = keyMap.key(QString::fromLatin1(suffix), -1); if (index != -1) suffixPluginIndex = index; #endif @@ -163,9 +166,12 @@ static QImageIOHandler *createWriteHandlerHelper(QIODevice *device, if (suffixPluginIndex != -1) { // when format is missing, check if we can find a plugin for the // suffix. - QImageIOPlugin *plugin = qobject_cast(l->instance(QString::fromLatin1(suffix))); - if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite)) - handler = plugin->create(device, suffix); + const int index = keyMap.key(QString::fromLatin1(suffix), -1); + if (index != -1) { + QImageIOPlugin *plugin = qobject_cast(l->instance(index)); + if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite)) + handler = plugin->create(device, suffix); + } } #endif // QT_NO_LIBRARY @@ -210,8 +216,9 @@ static QImageIOHandler *createWriteHandlerHelper(QIODevice *device, #ifndef QT_NO_LIBRARY if (!testFormat.isEmpty()) { - for (int i = 0; i < keys.size(); ++i) { - QImageIOPlugin *plugin = qobject_cast(l->instance(keys.at(i))); + const int keyCount = keyMap.keys().size(); + for (int i = 0; i < keyCount; ++i) { + QImageIOPlugin *plugin = qobject_cast(l->instance(i)); if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) { delete handler; handler = plugin->create(device, testFormat); @@ -647,6 +654,31 @@ bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const return d->handler->supportsOption(option); } + +#ifndef QT_NO_LIBRARY +void supportedImageHandlerFormats(QFactoryLoader *loader, + QImageIOPlugin::Capability cap, + QSet *result) +{ + typedef QMultiMap PluginKeyMap; + typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; + + const PluginKeyMap keyMap = loader->keyMap(); + const PluginKeyMapConstIterator cend = keyMap.constEnd(); + int i = -1; + QImageIOPlugin *plugin = 0; + for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) { + if (it.key() != i) { + i = it.key(); + plugin = qobject_cast(loader->instance(i)); + } + const QByteArray key = it.value().toLatin1(); + if (plugin && (plugin->capabilities(0, key) & cap) != 0) + result->insert(key); + } +} +#endif // QT_NO_LIBRARY + /*! Returns the list of image formats supported by QImageWriter. @@ -696,13 +728,7 @@ QList QImageWriter::supportedImageFormats() #endif #ifndef QT_NO_LIBRARY - QFactoryLoader *l = loader(); - QStringList keys = l->keys(); - for (int i = 0; i < keys.count(); ++i) { - QImageIOPlugin *plugin = qobject_cast(l->instance(keys.at(i))); - if (plugin && (plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanWrite) != 0) - formats << keys.at(i).toLatin1(); - } + supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats); #endif // QT_NO_LIBRARY QList sortedFormats; diff --git a/src/gui/image/qpicture.cpp b/src/gui/image/qpicture.cpp index 8bb9f211e8..0544d3d431 100644 --- a/src/gui/image/qpicture.cpp +++ b/src/gui/image/qpicture.cpp @@ -1400,14 +1400,20 @@ Q_GLOBAL_STATIC(QPHList, pictureHandlers) void qt_init_picture_plugins() { #ifndef QT_NO_LIBRARY + typedef QMultiMap PluginKeyMap; + typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; + static QBasicMutex mutex; QMutexLocker locker(&mutex); static QFactoryLoader loader(QPictureFormatInterface_iid, QStringLiteral("/pictureformats")); - QStringList keys = loader.keys(); - for (int i = 0; i < keys.count(); ++i) - if (QPictureFormatInterface *format = qobject_cast(loader.instance(keys.at(i)))) - format->installIOHandler(keys.at(i)); + + const PluginKeyMap keyMap = loader.keyMap(); + const PluginKeyMapConstIterator cend = keyMap.constEnd(); + for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) { + if (QPictureFormatInterface *format = qobject_cast(loader.instance(it.key()))) + format->installIOHandler(it.value()); + } #endif } diff --git a/src/gui/kernel/qgenericpluginfactory_qpa.cpp b/src/gui/kernel/qgenericpluginfactory_qpa.cpp index d4a7a9c20e..90da16f868 100644 --- a/src/gui/kernel/qgenericpluginfactory_qpa.cpp +++ b/src/gui/kernel/qgenericpluginfactory_qpa.cpp @@ -79,12 +79,12 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, */ QObject *QGenericPluginFactory::create(const QString& key, const QString &specification) { - QString driver = key.toLower(); + const QString driver = key.toLower(); #if !defined(Q_OS_WIN32) || defined(QT_MAKEDLL) #ifndef QT_NO_LIBRARY - if (QGenericPluginFactoryInterface *factory = qobject_cast(loader()->instance(driver))) - return factory->create(driver, specification); + if (QObject *object = qLoadPlugin1(loader(), driver, specification)) + return object; #endif #endif return 0; @@ -101,11 +101,14 @@ QStringList QGenericPluginFactory::keys() #if !defined(Q_OS_WIN32) || defined(QT_MAKEDLL) #ifndef QT_NO_LIBRARY - QStringList plugins = loader()->keys(); - for (int i = 0; i < plugins.size(); ++i) { - if (!list.contains(plugins.at(i))) - list += plugins.at(i); - } + typedef QMultiMap PluginKeyMap; + typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; + + const PluginKeyMap keyMap = loader()->keyMap(); + const PluginKeyMapConstIterator cend = keyMap.constEnd(); + for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) + if (!list.contains(it.value())) + list += it.value(); #endif //QT_NO_LIBRARY #endif //QT_MAKEDLL return list; diff --git a/src/gui/kernel/qplatformintegrationfactory_qpa.cpp b/src/gui/kernel/qplatformintegrationfactory_qpa.cpp index 2092f2d0ba..3b05742f09 100644 --- a/src/gui/kernel/qplatformintegrationfactory_qpa.cpp +++ b/src/gui/kernel/qplatformintegrationfactory_qpa.cpp @@ -43,6 +43,7 @@ #include #include "private/qfactoryloader_p.h" #include "qmutex.h" +#include "qdir.h" #include "qguiapplication.h" #include "qdebug.h" @@ -58,26 +59,20 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, directLoader, QPlatformIntegration *QPlatformIntegrationFactory::create(const QString& key, const QString &platformPluginPath) { - QPlatformIntegration *ret = 0; QStringList paramList = key.split(QLatin1Char(':')); - QString platform = paramList.takeFirst().toLower(); + const QString platform = paramList.takeFirst().toLower(); #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) // Try loading the plugin from platformPluginPath first: if (!platformPluginPath.isEmpty()) { QCoreApplication::addLibraryPath(platformPluginPath); - if (QPlatformIntegrationFactoryInterface *factory = - qobject_cast(directLoader()->instance(platform))) - ret = factory->create(key, paramList); - - if (ret) + if (QPlatformIntegration *ret = qLoadPlugin1(directLoader(), platform, paramList)) return ret; } - if (QPlatformIntegrationFactoryInterface *factory = qobject_cast(loader()->instance(platform))) - ret = factory->create(platform, paramList); + if (QPlatformIntegration *ret = qLoadPlugin1(loader(), platform, paramList)) + return ret; #endif - - return ret; + return 0; } /*! @@ -86,23 +81,28 @@ QPlatformIntegration *QPlatformIntegrationFactory::create(const QString& key, co \sa create() */ + QStringList QPlatformIntegrationFactory::keys(const QString &platformPluginPath) { #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) QStringList list; - if (!platformPluginPath.isEmpty()) { QCoreApplication::addLibraryPath(platformPluginPath); - foreach (const QString &key, directLoader()->keys()) { - list += key + QString(QLatin1String(" (from %1)")).arg(platformPluginPath); + list = directLoader()->keyMap().values(); + if (!list.isEmpty()) { + const QString postFix = QStringLiteral(" (from ") + + QDir::toNativeSeparators(platformPluginPath) + + QLatin1Char(')'); + const QStringList::iterator end = list.end(); + for (QStringList::iterator it = list.begin(); it != end; ++it) + (*it).append(postFix); } } - - list += loader()->keys(); + list.append(loader()->keyMap().values()); + return list; #else - QStringList list; + return QStringList(); #endif - return list; } QT_END_NAMESPACE diff --git a/src/gui/kernel/qplatformthemefactory_qpa.cpp b/src/gui/kernel/qplatformthemefactory_qpa.cpp index 8294591c4b..c809a7fba3 100644 --- a/src/gui/kernel/qplatformthemefactory_qpa.cpp +++ b/src/gui/kernel/qplatformthemefactory_qpa.cpp @@ -41,6 +41,7 @@ #include #include +#include #include "private/qfactoryloader_p.h" #include "qmutex.h" @@ -58,26 +59,20 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, directLoader, QPlatformTheme *QPlatformThemeFactory::create(const QString& key, const QString &platformPluginPath) { - QPlatformTheme *ret = 0; QStringList paramList = key.split(QLatin1Char(':')); - QString platform = paramList.takeFirst().toLower(); + const QString platform = paramList.takeFirst().toLower(); #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) // Try loading the plugin from platformPluginPath first: if (!platformPluginPath.isEmpty()) { QCoreApplication::addLibraryPath(platformPluginPath); - if (QPlatformThemeFactoryInterface *factory = - qobject_cast(directLoader()->instance(platform))) - ret = factory->create(key, paramList); - - if (ret) + if (QPlatformTheme *ret = qLoadPlugin1(directLoader(), platform, paramList)) return ret; } - if (QPlatformThemeFactoryInterface *factory = qobject_cast(loader()->instance(platform))) - ret = factory->create(platform, paramList); + if (QPlatformTheme *ret = qLoadPlugin1(loader(), platform, paramList)) + return ret; #endif - - return ret; + return 0; } /*! @@ -93,16 +88,21 @@ QStringList QPlatformThemeFactory::keys(const QString &platformPluginPath) if (!platformPluginPath.isEmpty()) { QCoreApplication::addLibraryPath(platformPluginPath); - foreach (const QString &key, directLoader()->keys()) { - list += key + QString(QLatin1String(" (from %1)")).arg(platformPluginPath); + list += directLoader()->keyMap().values(); + if (!list.isEmpty()) { + const QString postFix = QStringLiteral(" (from ") + + QDir::toNativeSeparators(platformPluginPath) + + QLatin1Char(')'); + const QStringList::iterator end = list.end(); + for (QStringList::iterator it = list.begin(); it != end; ++it) + (*it).append(postFix); } } - - list += loader()->keys(); + list += loader()->keyMap().values(); + return list; #else - QStringList list; + return QStringList(); #endif - return list; } QT_END_NAMESPACE -- cgit v1.2.3