File indexing completed on 2024-05-12 15:42:37

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 by Marco Martin <mart@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "kirigamipluginfactory.h"
0008 
0009 #include <QDebug>
0010 
0011 #include <QDir>
0012 #include <QQuickStyle>
0013 #include <QPluginLoader>
0014 
0015 #include "styleselector_p.h"
0016 #include "units.h"
0017 
0018 #include "loggingcategory.h"
0019 
0020 namespace Kirigami {
0021 
0022 KirigamiPluginFactory::KirigamiPluginFactory(QObject *parent)
0023     : QObject(parent)
0024 {
0025 }
0026 
0027 KirigamiPluginFactory::~KirigamiPluginFactory() = default;
0028 
0029 KirigamiPluginFactory *KirigamiPluginFactory::findPlugin()
0030 {
0031     static KirigamiPluginFactory *pluginFactory = nullptr;
0032 
0033     //check for the plugin only once: it's an heavy operation
0034     if (pluginFactory) {
0035         return pluginFactory;
0036     }
0037 
0038     static bool s_factoryChecked = false;
0039 
0040     if (!s_factoryChecked) {
0041         s_factoryChecked = true;
0042 
0043         #ifdef KIRIGAMI_BUILD_TYPE_STATIC
0044         for (QObject *staticPlugin : QPluginLoader::staticInstances()) {
0045             KirigamiPluginFactory *factory = qobject_cast<KirigamiPluginFactory *>(staticPlugin);
0046             if (factory) {
0047                 pluginFactory = factory;
0048             }
0049         }
0050         #else
0051         const auto libraryPaths = QCoreApplication::libraryPaths();
0052         for (const QString &path : libraryPaths) {
0053             #ifdef Q_OS_ANDROID
0054             QDir dir(path);
0055             #else
0056             QDir dir(path + QStringLiteral("/kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/kirigami"));
0057             #endif
0058             const auto fileNames = dir.entryList(QDir::Files);
0059 
0060             for (const QString &fileName : fileNames) {
0061 
0062 #ifdef Q_OS_ANDROID
0063                 if (fileName.startsWith(QStringLiteral("libplugins_kf" QT_STRINGIFY(QT_VERSION_MAJOR) "_kirigami_")) && QLibrary::isLibrary(fileName)) {
0064 #endif
0065                     // TODO: env variable?
0066                     if (!QQuickStyle::name().isEmpty() && fileName.contains(QQuickStyle::name())) {
0067                         QPluginLoader loader(dir.absoluteFilePath(fileName));
0068                         QObject *plugin = loader.instance();
0069                         // TODO: load actually a factory as plugin
0070 
0071                         qCDebug(KirigamiLog) << "Loading style plugin from" << dir.absoluteFilePath(fileName);
0072 
0073                         KirigamiPluginFactory *factory = qobject_cast<KirigamiPluginFactory *>(plugin);
0074                         if (factory) {
0075                             pluginFactory = factory;
0076                             break;
0077                         }
0078                     }
0079 #ifdef Q_OS_ANDROID
0080                 }
0081 #endif
0082             }
0083 
0084             // Ensure we only load the first plugin from the first plugin location.
0085             // If we do not break here, we may end up loading a different plugin
0086             // in place of the first one.
0087             if (pluginFactory) {
0088                 break;
0089             }
0090         }
0091 #endif
0092     }
0093 
0094     return pluginFactory;
0095 }
0096 
0097 KirigamiPluginFactoryV2::KirigamiPluginFactoryV2(QObject *parent)
0098     : KirigamiPluginFactory(parent)
0099 {
0100 }
0101 
0102 KirigamiPluginFactoryV2::~KirigamiPluginFactoryV2() = default;
0103 }
0104 
0105 #include "moc_kirigamipluginfactory.cpp"