| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. | 
|---|
| 4 | ** Contact: https://www.qt.io/licensing/ | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtGui 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 The Qt Company. For licensing terms | 
|---|
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
|---|
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | 
|---|
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
|---|
| 21 | ** packaging of this file. Please review the following information to | 
|---|
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
|---|
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
|---|
| 24 | ** | 
|---|
| 25 | ** GNU General Public License Usage | 
|---|
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
|---|
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
|---|
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
|---|
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
|---|
| 31 | ** included in the packaging of this file. Please review the following | 
|---|
| 32 | ** information to ensure the GNU General Public License requirements will | 
|---|
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
|---|
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** $QT_END_LICENSE$ | 
|---|
| 37 | ** | 
|---|
| 38 | ****************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | #include "private/qimagereaderwriterhelpers_p.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include <qjsonarray.h> | 
|---|
| 43 | #include <qmutex.h> | 
|---|
| 44 | #include <private/qfactoryloader_p.h> | 
|---|
| 45 |  | 
|---|
| 46 | QT_BEGIN_NAMESPACE | 
|---|
| 47 |  | 
|---|
| 48 | namespace QImageReaderWriterHelpers { | 
|---|
| 49 |  | 
|---|
| 50 | #ifndef QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 51 |  | 
|---|
| 52 | Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, | 
|---|
| 53 | (QImageIOHandlerFactoryInterface_iid, QLatin1String( "/imageformats"))) | 
|---|
| 54 | Q_GLOBAL_STATIC(QMutex, loaderMutex) | 
|---|
| 55 |  | 
|---|
| 56 | static void appendImagePluginFormats(QFactoryLoader *loader, | 
|---|
| 57 | QImageIOPlugin::Capability cap, | 
|---|
| 58 | QList<QByteArray> *result) | 
|---|
| 59 | { | 
|---|
| 60 | typedef QMultiMap<int, QString> PluginKeyMap; | 
|---|
| 61 | typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; | 
|---|
| 62 |  | 
|---|
| 63 | const PluginKeyMap keyMap = loader->keyMap(); | 
|---|
| 64 | const PluginKeyMapConstIterator cend = keyMap.constEnd(); | 
|---|
| 65 | int i = -1; | 
|---|
| 66 | QImageIOPlugin *plugin = nullptr; | 
|---|
| 67 | result->reserve(result->size() + keyMap.size()); | 
|---|
| 68 | for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) { | 
|---|
| 69 | if (it.key() != i) { | 
|---|
| 70 | i = it.key(); | 
|---|
| 71 | plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i)); | 
|---|
| 72 | } | 
|---|
| 73 | const QByteArray key = it.value().toLatin1(); | 
|---|
| 74 | if (plugin && (plugin->capabilities(nullptr, key) & cap) != 0) | 
|---|
| 75 | result->append(key); | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | static void appendImagePluginMimeTypes(QFactoryLoader *loader, | 
|---|
| 80 | QImageIOPlugin::Capability cap, | 
|---|
| 81 | QList<QByteArray> *result, | 
|---|
| 82 | QList<QByteArray> *resultKeys = nullptr) | 
|---|
| 83 | { | 
|---|
| 84 | QList<QJsonObject> metaDataList = loader->metaData(); | 
|---|
| 85 |  | 
|---|
| 86 | const int pluginCount = metaDataList.size(); | 
|---|
| 87 | for (int i = 0; i < pluginCount; ++i) { | 
|---|
| 88 | const QJsonObject metaData = metaDataList.at(i).value(QLatin1String( "MetaData")).toObject(); | 
|---|
| 89 | const QJsonArray keys = metaData.value(QLatin1String( "Keys")).toArray(); | 
|---|
| 90 | const QJsonArray mimeTypes = metaData.value(QLatin1String( "MimeTypes")).toArray(); | 
|---|
| 91 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i)); | 
|---|
| 92 | const int keyCount = keys.size(); | 
|---|
| 93 | for (int k = 0; k < keyCount; ++k) { | 
|---|
| 94 | const QByteArray key = keys.at(k).toString().toLatin1(); | 
|---|
| 95 | if (plugin && (plugin->capabilities(nullptr, key) & cap) != 0) { | 
|---|
| 96 | result->append(mimeTypes.at(k).toString().toLatin1()); | 
|---|
| 97 | if (resultKeys) | 
|---|
| 98 | resultKeys->append(key); | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | QSharedPointer<QFactoryLoader> pluginLoader() | 
|---|
| 105 | { | 
|---|
| 106 | loaderMutex()->lock(); | 
|---|
| 107 | return QSharedPointer<QFactoryLoader>(loader(), [](QFactoryLoader *) { | 
|---|
| 108 | loaderMutex()->unlock(); | 
|---|
| 109 | }); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | static inline QImageIOPlugin::Capability pluginCapability(Capability cap) | 
|---|
| 113 | { | 
|---|
| 114 | return cap == CanRead ? QImageIOPlugin::CanRead : QImageIOPlugin::CanWrite; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | #endif // QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 118 |  | 
|---|
| 119 | QList<QByteArray> supportedImageFormats(Capability cap) | 
|---|
| 120 | { | 
|---|
| 121 | QList<QByteArray> formats; | 
|---|
| 122 | formats.reserve(_qt_NumFormats); | 
|---|
| 123 | for (int i = 0; i < _qt_NumFormats; ++i) | 
|---|
| 124 | formats << _qt_BuiltInFormats[i].extension; | 
|---|
| 125 |  | 
|---|
| 126 | #ifndef QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 127 | appendImagePluginFormats(loader(), pluginCapability(cap), &formats); | 
|---|
| 128 | #endif // QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 129 |  | 
|---|
| 130 | std::sort(formats.begin(), formats.end()); | 
|---|
| 131 | formats.erase(std::unique(formats.begin(), formats.end()), formats.end()); | 
|---|
| 132 | return formats; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | QList<QByteArray> supportedMimeTypes(Capability cap) | 
|---|
| 136 | { | 
|---|
| 137 | QList<QByteArray> mimeTypes; | 
|---|
| 138 | mimeTypes.reserve(_qt_NumFormats); | 
|---|
| 139 | for (const auto &fmt : _qt_BuiltInFormats) | 
|---|
| 140 | mimeTypes.append(QByteArrayLiteral( "image/") + fmt.mimeType); | 
|---|
| 141 |  | 
|---|
| 142 | #ifndef QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 143 | appendImagePluginMimeTypes(loader(), pluginCapability(cap), &mimeTypes); | 
|---|
| 144 | #endif // QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 145 |  | 
|---|
| 146 | std::sort(mimeTypes.begin(), mimeTypes.end()); | 
|---|
| 147 | mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end()); | 
|---|
| 148 | return mimeTypes; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | QList<QByteArray> imageFormatsForMimeType(const QByteArray &mimeType, Capability cap) | 
|---|
| 152 | { | 
|---|
| 153 | QList<QByteArray> formats; | 
|---|
| 154 | if (mimeType.startsWith( "image/")) { | 
|---|
| 155 | const QByteArray type = mimeType.mid(sizeof( "image/") - 1); | 
|---|
| 156 | for (const auto &fmt : _qt_BuiltInFormats) { | 
|---|
| 157 | if (fmt.mimeType == type && !formats.contains(fmt.extension)) | 
|---|
| 158 | formats << fmt.extension; | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | #ifndef QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 163 | QList<QByteArray> mimeTypes; | 
|---|
| 164 | QList<QByteArray> keys; | 
|---|
| 165 | appendImagePluginMimeTypes(loader(), pluginCapability(cap), &mimeTypes, &keys); | 
|---|
| 166 | for (int i = 0; i < mimeTypes.size(); ++i) { | 
|---|
| 167 | if (mimeTypes.at(i) == mimeType) { | 
|---|
| 168 | const auto &key = keys.at(i); | 
|---|
| 169 | if (!formats.contains(key)) | 
|---|
| 170 | formats << key; | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 | #endif // QT_NO_IMAGEFORMATPLUGIN | 
|---|
| 174 |  | 
|---|
| 175 | return formats; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | } // QImageReaderWriterHelpers | 
|---|
| 179 |  | 
|---|
| 180 | QT_END_NAMESPACE | 
|---|
| 181 |  | 
|---|