Kirigami2

kirigamipluginfactory.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017 by Marco Martin <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "kirigamipluginfactory.h"
8 
9 #include <QDebug>
10 
11 #include <QDir>
12 #include <QQuickStyle>
13 #include <QPluginLoader>
14 
15 #include "styleselector_p.h"
16 #include "units.h"
17 
18 #include "loggingcategory.h"
19 
20 namespace Kirigami {
21 
22 KirigamiPluginFactory::KirigamiPluginFactory(QObject *parent)
23  : QObject(parent)
24 {
25 }
26 
27 KirigamiPluginFactory::~KirigamiPluginFactory() = default;
28 
29 KirigamiPluginFactory *KirigamiPluginFactory::findPlugin()
30 {
31  static KirigamiPluginFactory *pluginFactory = nullptr;
32 
33  //check for the plugin only once: it's an heavy operation
34  if (pluginFactory) {
35  return pluginFactory;
36  }
37 
38  static bool s_factoryChecked = false;
39 
40  if (!s_factoryChecked) {
41  s_factoryChecked = true;
42 
43  #ifdef KIRIGAMI_BUILD_TYPE_STATIC
44  for (QObject *staticPlugin : QPluginLoader::staticInstances()) {
45  KirigamiPluginFactory *factory = qobject_cast<KirigamiPluginFactory *>(staticPlugin);
46  if (factory) {
47  pluginFactory = factory;
48  }
49  }
50  #else
51  const auto libraryPaths = QCoreApplication::libraryPaths();
52  for (const QString &path : libraryPaths) {
53  #ifdef Q_OS_ANDROID
54  QDir dir(path);
55  #else
56  QDir dir(path + QStringLiteral("/kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/kirigami"));
57  #endif
58  const auto fileNames = dir.entryList(QDir::Files);
59 
60  for (const QString &fileName : fileNames) {
61 
62 #ifdef Q_OS_ANDROID
63  if (fileName.startsWith(QStringLiteral("libplugins_kf" QT_STRINGIFY(QT_VERSION_MAJOR) "_kirigami_")) && QLibrary::isLibrary(fileName)) {
64 #endif
65  // TODO: env variable?
66  if (!QQuickStyle::name().isEmpty() && fileName.contains(QQuickStyle::name())) {
67  QPluginLoader loader(dir.absoluteFilePath(fileName));
68  QObject *plugin = loader.instance();
69  // TODO: load actually a factory as plugin
70 
71  qCDebug(KirigamiLog) << "Loading style plugin from" << dir.absoluteFilePath(fileName);
72 
73  KirigamiPluginFactory *factory = qobject_cast<KirigamiPluginFactory *>(plugin);
74  if (factory) {
75  pluginFactory = factory;
76  break;
77  }
78  }
79 #ifdef Q_OS_ANDROID
80  }
81 #endif
82  }
83 
84  // Ensure we only load the first plugin from the first plugin location.
85  // If we do not break here, we may end up loading a different plugin
86  // in place of the first one.
87  if (pluginFactory) {
88  break;
89  }
90  }
91 #endif
92  }
93 
94  return pluginFactory;
95 }
96 
97 KirigamiPluginFactoryV2::KirigamiPluginFactoryV2(QObject *parent)
98  : KirigamiPluginFactory(parent)
99 {
100 }
101 
102 KirigamiPluginFactoryV2::~KirigamiPluginFactoryV2() = default;
103 }
104 
105 #include "moc_kirigamipluginfactory.cpp"
bool isLibrary(const QString &fileName)
QStringList libraryPaths()
QObjectList staticInstances()
QString name()
QObject * instance()
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.