Commit 2a8cabcc authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Make the snap source less of a weird thing

This way we can make the sources page less of a weird monster.
parent ef66f3d0
This diff is collapsed.
......@@ -119,6 +119,13 @@ DiscoverPage
id: updatesView
currentIndex: -1
displaced: Transition {
YAnimator {
duration: Kirigami.Units.longDuration
easing.type: Easing.InOutQuad
}
}
footer: ColumnLayout {
anchors.right: parent.right
anchors.left: parent.left
......
......@@ -37,11 +37,32 @@
#include <QThread>
#include <QTimer>
#include <QAction>
#include <QStandardItemModel>
#include "utils.h"
DISCOVER_BACKEND_PLUGIN(SnapBackend)
class SnapSourcesBackend : public AbstractSourcesBackend
{
public:
explicit SnapSourcesBackend(AbstractResourcesBackend * parent) : AbstractSourcesBackend(parent), m_model(new QStandardItemModel) {
m_model->appendRow(new QStandardItem(i18n("Snap")));
}
QAbstractItemModel* sources() override { return m_model; }
bool addSource(const QString& /*id*/) override { return false; }
bool removeSource(const QString& /*id*/) override { return false;}
QString idDescription() override { return QStringLiteral("Snap"); }
QList<QAction*> actions() const override { return {}; }
bool supportsAdding() const override { return false; }
bool canMoveSources() const override { return false; }
private:
QStandardItemModel* const m_model;
};
SnapBackend::SnapBackend(QObject* parent)
: AbstractResourcesBackend(parent)
, m_updater(new StandardBackendUpdater(this))
......@@ -61,7 +82,7 @@ SnapBackend::SnapBackend(QObject* parent)
//make sure we populate the installed resources first
refreshStates();
SourcesModel::global()->addBackend(this);
SourcesModel::global()->addSourcesBackend(new SnapSourcesBackend(this));
}
int SnapBackend::updatesCount() const
......
......@@ -66,6 +66,7 @@ QHash<int, QByteArray> SourcesModel::roleNames() const
{
QHash<int, QByteArray> roles = KConcatenateRowsProxyModel::roleNames();
roles.insert(AbstractSourcesBackend::IdRole, "sourceId");
roles.insert(SourceNameRole, "sourceName");
roles.insert(SourcesBackend, "sourcesBackend");
roles.insert(ResourcesBackend, "resourcesBackend");
return roles;
......@@ -74,32 +75,13 @@ QHash<int, QByteArray> SourcesModel::roleNames() const
void SourcesModel::addSourcesBackend(AbstractSourcesBackend* sources)
{
auto backend = qobject_cast<AbstractResourcesBackend*>(sources->parent());
auto b = addBackend(backend);
if (!b)
return;
auto m = sources->sources();
m->setProperty(DisplayName, backend->displayName());
m->setProperty(SourcesBackendId, qVariantFromValue<QObject*>(sources));
b->setProperty(SourcesBackendId, qVariantFromValue<QObject*>(sources));
addSourceModel(m);
}
SourceBackendModel* SourcesModel::addBackend(AbstractResourcesBackend* backend)
{
Q_ASSERT(backend);
const auto inSourcesModel = "InSourcesModel";
if (backend->dynamicPropertyNames().contains(inSourcesModel))
return nullptr;
backend->setProperty(inSourcesModel, 1);
auto b = new SourceBackendModel(backend);
b->setProperty(DisplayName, backend->displayName());
addSourceModel(b);
return b;
}
const QAbstractItemModel * SourcesModel::modelAt(const QModelIndex& index) const
{
const auto sidx = mapToSource(index);
......@@ -119,4 +101,15 @@ QVariant SourcesModel::data(const QModelIndex& index, int role) const
}
}
AbstractSourcesBackend * SourcesModel::sourcesBackendByName(const QString& id) const
{
for(int i = 0, c = rowCount(); i < c; ++i) {
const auto idx = index(i, 0);
if (idx.data(SourceNameRole) == id) {
return qobject_cast<AbstractSourcesBackend *>(idx.data(SourcesBackend).value<QObject*>());
}
}
return nullptr;
}
#include "SourcesModel.moc"
......@@ -27,10 +27,6 @@
#include "discovercommon_export.h"
#include "AbstractSourcesBackend.h"
class QAction;
class AbstractResourcesBackend;
class SourceBackendModel;
class DISCOVERCOMMON_EXPORT SourcesModel : public KConcatenateRowsProxyModel
{
Q_OBJECT
......@@ -49,9 +45,10 @@ class DISCOVERCOMMON_EXPORT SourcesModel : public KConcatenateRowsProxyModel
QVariant data(const QModelIndex & index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
SourceBackendModel* addBackend(AbstractResourcesBackend* backend);
void addSourcesBackend(AbstractSourcesBackend* sources);
Q_SCRIPTABLE AbstractSourcesBackend* sourcesBackendByName(const QString &name) const;
private:
const QAbstractItemModel* modelAt(const QModelIndex& idx) const;
};
......
Markdown is supported
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