File indexing completed on 2024-12-01 10:29:53

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "io.h"
0007 #include "ioplugin.h"
0008 
0009 #include <QJsonArray>
0010 #include <QJsonObject>
0011 #include <QPluginLoader>
0012 
0013 #include <optional>
0014 
0015 using namespace OSM;
0016 
0017 Q_IMPORT_PLUGIN(OSM_O5mIOPlugin)
0018 
0019 IOPluginInterface::~IOPluginInterface() = default;
0020 
0021 static std::optional<QStaticPlugin> pluginForMetaData(QLatin1String key, QStringView value)
0022 {
0023     const auto plugins = QPluginLoader::staticPlugins();
0024     for (const auto &plugin : plugins) {
0025         const auto md = plugin.metaData();
0026         if (md.value(QLatin1String("IID")).toString() != QLatin1String(OSMIOPluginInteraface_iid)) {
0027             continue;
0028         }
0029         const auto exts = md.value(QLatin1String("MetaData")).toObject().value(key).toArray();
0030         for (const auto &ext : exts) {
0031             if (value.endsWith(ext.toString(), Qt::CaseInsensitive)) {
0032                 return plugin;
0033             }
0034         }
0035     }
0036 
0037     return {};
0038 }
0039 
0040 std::unique_ptr<AbstractReader> IO::readerForFileName(QStringView fileName, OSM::DataSet *dataSet)
0041 {
0042     const auto plugin = pluginForMetaData(QLatin1String("fileExtensions"), fileName);
0043     if (plugin) {
0044         return qobject_cast<IOPluginInterface*>(plugin->instance())->createReader(dataSet);
0045     }
0046     return {};
0047 }
0048 
0049 std::unique_ptr<AbstractReader> IO::readerForMimeType(QStringView mimeType, OSM::DataSet* dataSet)
0050 {
0051     const auto plugin = pluginForMetaData(QLatin1String("mimetypes"), mimeType);
0052     if (plugin) {
0053         return qobject_cast<IOPluginInterface*>(plugin->instance())->createReader(dataSet);
0054     }
0055     return {};
0056 }
0057 
0058 std::unique_ptr<AbstractWriter> IO::writerForFileName(QStringView fileName)
0059 {
0060     const auto plugin = pluginForMetaData(QLatin1String("fileExtensions"), fileName);
0061     if (plugin) {
0062         return qobject_cast<IOPluginInterface*>(plugin->instance())->createWriter();
0063     }
0064     return {};
0065 }
0066 
0067 std::unique_ptr<AbstractWriter> IO::writerForMimeType(QStringView mimeType)
0068 {
0069     const auto plugin = pluginForMetaData(QLatin1String("mimeypes"), mimeType);
0070     if (plugin) {
0071         return qobject_cast<IOPluginInterface*>(plugin->instance())->createWriter();
0072     }
0073     return {};
0074 }