Port to new KDecoration2::DecorationThemeProvider class

By supporting all the required attributes in the DecorationThemeMetaData class
the custom struct can be dropped.

Task: https://phabricator.kde.org/T14744
This commit is contained in:
Alexander Lohnau 2021-08-23 19:39:17 +02:00
parent 58c71de952
commit 08d3ceb024
4 changed files with 59 additions and 73 deletions

View file

@ -5,8 +5,9 @@
*/
#include "decorationmodel.h"
// KDecoration2
#include <KDecoration2/DecorationSettings>
#include <KDecoration2/Decoration>
#include <KDecoration2/DecorationSettings>
#include <KDecoration2/DecorationThemeProvider>
// KDE
#include <KPluginLoader>
#include <KPluginFactory>
@ -41,18 +42,18 @@ QVariant DecorationsModel::data(const QModelIndex &index, int role) const
if (!index.isValid() || index.column() != 0 || index.row() < 0 || index.row() >= int(m_plugins.size())) {
return QVariant();
}
const Data &d = m_plugins.at(index.row());
const KDecoration2::DecorationThemeMetaData &d = m_plugins.at(index.row());
switch (role) {
case Qt::DisplayRole:
return d.visibleName;
return d.visibleName();
case PluginNameRole:
return d.pluginName;
return d.pluginId();
case ThemeNameRole:
return d.themeName;
return d.themeName();
case ConfigurationRole:
return d.configuration;
return d.hasConfiguration();
case RecommendedBorderSizeRole:
return Utils::borderSizeToString(d.recommendedBorderSize);
return Utils::borderSizeToString(d.borderSize());
}
return QVariant();
}
@ -118,17 +119,13 @@ void DecorationsModel::init()
{
beginResetModel();
m_plugins.clear();
const auto plugins = KPluginLoader::findPlugins(s_pluginName);
const auto plugins = KPluginMetaData::findPlugins(s_pluginName);
for (const auto &info : plugins) {
KPluginLoader loader(info.fileName());
KPluginFactory *factory = loader.factory();
if (!factory) {
continue;
}
auto metadata = loader.metaData().value(QStringLiteral("MetaData")).toObject().value(s_pluginName);
Data data;
if (!metadata.isUndefined()) {
const auto decoSettingsMap = metadata.toObject().toVariantMap();
QScopedPointer<KDecoration2::DecorationThemeProvider> themeFinder(
KPluginFactory::instantiatePlugin<KDecoration2::DecorationThemeProvider>(info).plugin);
KDecoration2::DecorationThemeMetaData data;
if (themeFinder) {
const auto decoSettingsMap = info.rawData().value("org.kde.kdecoration2").toObject().toVariantMap();
const QString &kns = findKNewStuff(decoSettingsMap);
if (!kns.isEmpty() && !m_knsProviders.contains(kns)) {
m_knsProviders.append(kns);
@ -139,35 +136,20 @@ void DecorationsModel::init()
// We cannot list the themes
continue;
}
QScopedPointer<QObject> themeFinder(factory->create<QObject>(keyword));
if (themeFinder.isNull()) {
continue;
}
QVariant themes = themeFinder->property("themes");
if (!themes.isValid()) {
continue;
}
const auto themesMap = themes.toMap();
for (auto it = themesMap.begin(); it != themesMap.end(); ++it) {
Data d;
d.pluginName = info.pluginId();
d.themeName = it.value().toString();
d.visibleName = it.key();
QMetaObject::invokeMethod(themeFinder.data(), "hasConfiguration",
Q_RETURN_ARG(bool, d.configuration),
Q_ARG(QString, d.themeName));
m_plugins.emplace_back(std::move(d));
const auto themesList = themeFinder->themes();
for (const KDecoration2::DecorationThemeMetaData &data : themesList) {
m_plugins.emplace_back(data);
}
// it's a theme engine, we don't want to show this entry
continue;
}
data.configuration = isConfigureable(decoSettingsMap);
data.recommendedBorderSize = recommendedBorderSize(decoSettingsMap);
data.setHasConfiguration(isConfigureable(decoSettingsMap));
data.setBorderSize(recommendedBorderSize(decoSettingsMap));
}
data.pluginName = info.pluginId();
data.visibleName = info.name().isEmpty() ? info.pluginId() : info.name();
data.themeName = data.visibleName;
data.setVisibleName(info.name().isEmpty() ? info.pluginId() : info.name());
data.setPluginId(info.pluginId());
data.setThemeName(data.visibleName());
m_plugins.emplace_back(std::move(data));
}
@ -176,11 +158,9 @@ void DecorationsModel::init()
QModelIndex DecorationsModel::findDecoration(const QString &pluginName, const QString &themeName) const
{
auto it = std::find_if(m_plugins.cbegin(), m_plugins.cend(),
[pluginName, themeName](const Data &d) {
return d.pluginName == pluginName && d.themeName == themeName;
}
);
auto it = std::find_if(m_plugins.cbegin(), m_plugins.cend(), [pluginName, themeName](const KDecoration2::DecorationThemeMetaData &d) {
return d.pluginId() == pluginName && d.themeName() == themeName;
});
if (it == m_plugins.cend()) {
return QModelIndex();
}

View file

@ -8,6 +8,7 @@
#include "utils.h"
#include <KDecoration2/DecorationThemeProvider>
#include <QAbstractListModel>
namespace KDecoration2
@ -45,14 +46,7 @@ public Q_SLOTS:
void init();
private:
struct Data {
QString pluginName;
QString themeName;
QString visibleName;
bool configuration = false;
KDecoration2::BorderSize recommendedBorderSize = KDecoration2::BorderSize::Normal;
};
std::vector<Data> m_plugins;
std::vector<KDecoration2::DecorationThemeMetaData> m_plugins;
QStringList m_knsProviders;
};

View file

@ -49,10 +49,11 @@
K_PLUGIN_FACTORY_WITH_JSON(AuroraeDecoFactory,
"aurorae.json",
registerPlugin<Aurorae::Decoration>();
registerPlugin<Aurorae::ThemeFinder>(QStringLiteral("themes"));
registerPlugin<Aurorae::ConfigurationModule>(QStringLiteral("kcmodule"));
registerPlugin<Aurorae::ThemeProvider>();
registerPlugin<Aurorae::ConfigurationModule>();
)
namespace Aurorae
{
@ -600,28 +601,33 @@ KDecoration2::DecoratedClient *Decoration::clientPointer() const
return client().toStrongRef().data();
}
ThemeFinder::ThemeFinder(QObject *parent, const QVariantList &args)
: QObject(parent)
ThemeProvider::ThemeProvider(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
: KDecoration2::DecorationThemeProvider(parent, data, args)
, m_data(data)
{
Q_UNUSED(args)
init();
}
void ThemeFinder::init()
void ThemeProvider::init()
{
findAllQmlThemes();
findAllSvgThemes();
}
void ThemeFinder::findAllQmlThemes()
void ThemeProvider::findAllQmlThemes()
{
const auto offers = KPackage::PackageLoader::self()->findPackages(QStringLiteral("KWin/Decoration"), s_qmlPackageFolder);
for (const auto &offer : offers) {
m_themes.insert(offer.name(), offer.pluginId());
KDecoration2::DecorationThemeMetaData data;
data.setPluginId(m_data.pluginId());
data.setThemeName(offer.pluginId());
data.setVisibleName(offer.name());
data.setHasConfiguration(hasConfiguration(offer.pluginId()));
m_themes.append(data);
}
}
void ThemeFinder::findAllSvgThemes()
void ThemeProvider::findAllSvgThemes()
{
QStringList themes;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("aurorae/themes/"), QStandardPaths::LocateDirectory);
@ -649,14 +655,19 @@ void ThemeFinder::findAllSvgThemes()
name = packageName;
}
m_themes.insert(name, QString(QLatin1String("__aurorae__svg__") + packageName));
KDecoration2::DecorationThemeMetaData data;
data.setPluginId(m_data.pluginId());
data.setThemeName(QLatin1String("__aurorae__svg__") + packageName);
data.setVisibleName(name);
data.setHasConfiguration(hasConfiguration(data.themeName()));
m_themes.append(data);
}
}
static const QString s_configUiPath = QStringLiteral("kwin/decorations/%1/contents/ui/config.ui");
static const QString s_configXmlPath = QStringLiteral("kwin/decorations/%1/contents/config/main.xml");
bool ThemeFinder::hasConfiguration(const QString &theme) const
bool ThemeProvider::hasConfiguration(const QString &theme)
{
if (theme.startsWith(QLatin1String("__aurorae__svg__"))) {
return true;

View file

@ -7,10 +7,12 @@
#ifndef AURORAE_H
#define AURORAE_H
#include <KCModule>
#include <KDecoration2/Decoration>
#include <KDecoration2/DecorationThemeProvider>
#include <KPluginMetaData>
#include <QElapsedTimer>
#include <QVariant>
#include <KCModule>
class QQmlComponent;
class QQmlContext;
@ -78,25 +80,24 @@ private:
QElapsedTimer m_doubleClickTimer;
};
class ThemeFinder : public QObject
class ThemeProvider : public KDecoration2::DecorationThemeProvider
{
Q_OBJECT
Q_PROPERTY(QVariantMap themes READ themes)
public:
explicit ThemeFinder(QObject *parent = nullptr, const QVariantList &args = QVariantList());
explicit ThemeProvider(QObject *parent, const KPluginMetaData &data, const QVariantList &args);
QVariantMap themes() const {
QList<KDecoration2::DecorationThemeMetaData> themes() const override
{
return m_themes;
}
public Q_SLOTS:
bool hasConfiguration(const QString &theme) const;
private:
void init();
void findAllQmlThemes();
void findAllSvgThemes();
QVariantMap m_themes;
bool hasConfiguration(const QString &theme);
QList<KDecoration2::DecorationThemeMetaData> m_themes;
const KPluginMetaData m_data;
};
class ConfigurationModule : public KCModule