Commit bfef3f82 authored by Fushan Wen's avatar Fushan Wen Committed by Nate Graham
Browse files

wallpapers/potd: Use singleton to share data between QML files

After dataengine is gone, a singleton is required to share data.

CCBUG: 422934
parent b5f8a25e
......@@ -8,10 +8,23 @@
#include "potdprovidermodel.h"
#include <QQmlContext>
Q_GLOBAL_STATIC(PotdProviderModel, potdProviderModelSelf)
PotdProviderModel *self(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
QQmlEngine::setObjectOwnership(potdProviderModelSelf, QQmlEngine::CppOwnership);
return potdProviderModelSelf;
}
void PotdPlugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QByteArrayLiteral("org.kde.plasma.wallpapers.potd"));
qmlRegisterType<PotdProviderModel>(uri, 1, 0, "PotdProviderModel");
qmlRegisterSingletonType<PotdProviderModel>(uri, 1, 0, "PotdProviderModelInstance", self);
qmlRegisterUncreatableType<PotdProviderModel>(uri, 1, 0, "Global", QStringLiteral("Error: only enums"));
}
......@@ -92,26 +92,9 @@ void PotdProviderModel::loadPluginMetaData()
endResetModel();
}
KPluginMetaData PotdProviderModel::metadata(int index) const
int PotdProviderModel::currentIndex() const
{
if (index >= static_cast<int>(m_providers.size()) || index < 0) {
return m_providers.at(0);
}
return m_providers.at(index);
}
int PotdProviderModel::indexOfProvider(const QString &identifier) const
{
const auto it = std::find_if(m_providers.cbegin(), m_providers.cend(), [&identifier](const KPluginMetaData &metadata) {
return metadata.value(QStringLiteral("X-KDE-PlasmaPoTDProvider-Identifier")) == identifier;
});
if (it == m_providers.cend()) {
return -1;
}
return std::distance(m_providers.cbegin(), it);
return m_currentIndex;
}
bool PotdProviderModel::running() const
......@@ -158,7 +141,15 @@ void PotdProviderModel::setIdentifier(const QString &identifier)
}
m_identifier = identifier;
m_currentIndex = indexOfProvider(identifier);
const auto it = std::find_if(m_providers.cbegin(), m_providers.cend(), [&identifier](const KPluginMetaData &metadata) {
return metadata.value(QStringLiteral("X-KDE-PlasmaPoTDProvider-Identifier")) == identifier;
});
if (it == m_providers.cend()) {
m_currentIndex = -1;
} else {
m_currentIndex = std::distance(m_providers.cbegin(), it);
}
// Avoid flickering
if (const QString path = CachedProvider::identifierToPath(m_identifier); QFile::exists(path)) {
......@@ -169,6 +160,7 @@ void PotdProviderModel::setIdentifier(const QString &identifier)
updateSource();
Q_EMIT identifierChanged();
Q_EMIT currentIndexChanged();
}
QVariantList PotdProviderModel::arguments() const
......@@ -404,7 +396,8 @@ bool PotdProviderModel::updateSource(bool refresh)
return false;
}
const auto pluginResult = KPluginFactory::instantiatePlugin<PotdProvider>(metadata(m_currentIndex), this, m_args);
const KPluginMetaData &metadata = m_providers.at(m_currentIndex);
const auto pluginResult = KPluginFactory::instantiatePlugin<PotdProvider>(metadata, this, m_args);
if (pluginResult) {
connect(pluginResult.plugin, &PotdProvider::finished, this, &PotdProviderModel::slotFinished);
......
......@@ -32,6 +32,8 @@ class PotdProviderModel : public QAbstractListModel
Q_PROPERTY(QString identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged)
Q_PROPERTY(QVariantList arguments READ arguments WRITE setArguments NOTIFY argumentsChanged)
Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
/**
* Read-only properties that expose data from the provider.
*/
......@@ -88,8 +90,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
void loadPluginMetaData();
KPluginMetaData metadata(int index) const;
Q_INVOKABLE int indexOfProvider(const QString &identifier) const;
int currentIndex() const;
bool running() const;
void setRunning(bool flag);
......@@ -115,6 +116,7 @@ public:
Q_INVOKABLE void saveImage();
Q_SIGNALS:
void currentIndexChanged();
void runningChanged();
void identifierChanged();
void argumentsChanged();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment