File indexing completed on 2024-05-05 16:48:34

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
0003    Copyright     2007       David Faure <faure@kde.org>
0004    Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "KReportJsonTrader_p.h"
0023 #include "kreport_debug.h"
0024 #include "config-kreport.h"
0025 
0026 #include <QPluginLoader>
0027 #include <QJsonObject>
0028 #include <QJsonArray>
0029 #include <QDirIterator>
0030 #include <QCoreApplication>
0031 
0032 Q_GLOBAL_STATIC(KReportJsonTrader, KReportJsonTrader_instance)
0033 
0034 class Q_DECL_HIDDEN KReportJsonTrader::Private
0035 {
0036 public:
0037     Private() : pluginPathFound(false)
0038     {
0039     }
0040     bool pluginPathFound;
0041     QStringList pluginPaths;
0042 };
0043 
0044 // ---
0045 
0046 KReportJsonTrader::KReportJsonTrader()
0047     : d(new Private)
0048 {
0049     Q_ASSERT(!KReportJsonTrader_instance.exists());
0050 }
0051 
0052 KReportJsonTrader::~KReportJsonTrader()
0053 {
0054     delete d;
0055 }
0056 
0057 KReportJsonTrader* KReportJsonTrader::self()
0058 {
0059     return KReportJsonTrader_instance;
0060 }
0061 
0062 static QList<QPluginLoader *> findPlugins(const QString &path, const QString &servicetype,
0063                                           const QString &mimetype)
0064 {
0065     QList<QPluginLoader*> list;
0066     QDirIterator dirIter(path, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
0067     while (dirIter.hasNext()) {
0068         dirIter.next();
0069         if (dirIter.fileInfo().isFile()) {
0070             QPluginLoader *loader = new QPluginLoader(dirIter.filePath());
0071             QJsonObject json = loader->metaData().value(QLatin1String("MetaData")).toObject();
0072 
0073             if (json.isEmpty()) {
0074                 kreportDebug() << dirIter.filePath() << "has no json!";
0075             }
0076             if (!json.isEmpty()) {
0077                 QJsonObject pluginData = json.value(QLatin1String("KPlugin")).toObject();
0078                 if (!pluginData.value(QLatin1String("ServiceTypes")).toArray()
0079                         .contains(QJsonValue(servicetype)))
0080                 {
0081                     continue;
0082                 }
0083 
0084                 if (!mimetype.isEmpty()) {
0085                     QStringList mimeTypes = json.value(QLatin1String("X-KDE-ExtraNativeMimeTypes"))
0086                             .toString().split(QLatin1Char(','));
0087                     mimeTypes += json.value(QLatin1String("MimeType")).toString().split(QLatin1Char(';'));
0088                     mimeTypes += json.value(QLatin1String("X-KDE-NativeMimeType")).toString();
0089                     if (! mimeTypes.contains(mimetype)) {
0090                         continue;
0091                     }
0092                 }
0093                 list.append(loader);
0094             }
0095         }
0096     }
0097     return list;
0098 }
0099 
0100 QList<QPluginLoader *> KReportJsonTrader::query(const QString &servicetype,
0101                                                 const QString &mimetype)
0102 {
0103     if (!d->pluginPathFound) {
0104         QStringList searchDirs;
0105         searchDirs += QCoreApplication::libraryPaths();
0106         foreach(const QString &dir, searchDirs) {
0107             //kreportDebug() << dir;
0108             const QString possiblePath(dir + QLatin1Char('/') + QLatin1String(KREPORT_BASE_NAME_LOWER));
0109             if (QDir(possiblePath).exists()) {
0110                 d->pluginPaths += possiblePath;
0111             }
0112         }
0113         d->pluginPathFound = true;
0114     }
0115 
0116     QList<QPluginLoader *> list;
0117     foreach(const QString &path, d->pluginPaths) {
0118         list += findPlugins(path, servicetype, mimetype);
0119     }
0120     return list;
0121 }