Commit 0f823c0b authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Make it possible to choose which is the favorite flatpak source

Includes controls to raise and lower the preference of the different
flatpak backends available.

BUG: 388921
parent b34e0978
......@@ -68,6 +68,7 @@
class OurSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
public:
void classBegin() override {}
void componentComplete() override {
......
......@@ -29,7 +29,7 @@ DiscoverPage {
id: sourcesView
model: QSortFilterProxyModel {
filterRegExp: new RegExp(page.search, 'i')
sortRole: SourcesModelClass.SourceNameRole
dynamicSortFilter: false //We don't want to sort, as sorting can have some semantics on some backends
sourceModel: SourcesModel
}
currentIndex: -1
......@@ -145,6 +145,26 @@ DiscoverPage {
Keys.onReturnPressed: clicked()
actions: [
Kirigami.Action {
iconName: "go-up"
enabled: sourcesBackend.firstSourceId !== sourceId
visible: sourcesBackend.canMoveSources
onTriggered: {
var ret = sourcesBackend.moveSource(sourceId, -1)
if (!ret)
window.showPassiveNotification(i18n("Failed to increase '%1' preference", display))
}
},
Kirigami.Action {
iconName: "go-down"
enabled: sourcesBackend.lastSourceId !== sourceId
visible: sourcesBackend.canMoveSources
onTriggered: {
var ret = sourcesBackend.moveSource(sourceId, +1)
if (!ret)
window.showPassiveNotification(i18n("Failed to decrease '%1' preference", display))
}
},
Kirigami.Action {
iconName: "edit-delete"
tooltip: i18n("Delete the origin")
......
......@@ -78,3 +78,17 @@ QList<QAction*> DummySourcesBackend::actions() const
return QList<QAction*>() << m_testAction;
}
bool DummySourcesBackend::moveSource(const QString& sourceId, int delta)
{
int row = sourceForId(sourceId)->row();
auto prevRow = m_sources->takeRow(row);
Q_ASSERT(!prevRow.isEmpty());
const auto destRow = row + delta;
m_sources->insertRow(destRow, prevRow);
if (destRow == 0 || row == 0)
Q_EMIT firstSourceIdChanged();
if (destRow == m_sources->rowCount() - 1 || row == m_sources->rowCount() - 1)
Q_EMIT lastSourceIdChanged();
return true;
}
......@@ -36,6 +36,9 @@ public:
QList<QAction*> actions() const override;
bool supportsAdding() const override { return true; }
bool canMoveSources() const override { return true; }
bool moveSource(const QString & sourceId, int delta) override;
private:
QStandardItem* sourceForId(const QString& id) const;
......
......@@ -57,6 +57,7 @@
#include <QNetworkAccessManager>
#include <glib.h>
#include "FlatpakSourcesBackend.h"
DISCOVER_BACKEND_PLUGIN(FlatpakBackend)
......@@ -1074,7 +1075,7 @@ int FlatpakBackend::updatesCount() const
bool FlatpakBackend::flatpakResourceLessThan(AbstractResource* l, AbstractResource* r)
{
return (l->isInstalled() != r->isInstalled()) ? l->isInstalled()
: (l->origin() != r->origin()) ? l->origin() < r->origin()
: (l->origin() != r->origin()) ? m_sources->originIndex(l->origin()) < m_sources->originIndex(r->origin())
: l < r;
}
......
......@@ -23,6 +23,8 @@
#include "FlatpakResource.h"
#include "FlatpakBackend.h"
#include <KLocalizedString>
#include <KSharedConfig>
#include <KConfigGroup>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkReply>
......@@ -64,7 +66,19 @@ FlatpakSourcesBackend::FlatpakSourcesBackend(const QVector<FlatpakInstallation *
qWarning() << "Failed to list repositories from installation" << installation;
}
}
}
FlatpakSourcesBackend::~FlatpakSourcesBackend()
{
QStringList ids;
for (int i = 0, c = m_sources->rowCount(); i<c; ++i) {
auto it = m_sources->item(i);
ids << it->data(IdRole).toString();
}
auto conf = KSharedConfig::openConfig();
KConfigGroup group = conf->group("FlatpakSources");
group.writeEntry("Sources", ids);
}
QAbstractItemModel* FlatpakSourcesBackend::sources()
......@@ -103,7 +117,7 @@ bool FlatpakSourcesBackend::addSource(const QString &id)
return true;
}
bool FlatpakSourcesBackend::removeSource(const QString &id)
QStandardItem * FlatpakSourcesBackend::sourceById(const QString& id) const
{
QStandardItem* sourceIt = nullptr;
for (int i = 0, c = m_sources->rowCount(); i<c; ++i) {
......@@ -113,7 +127,12 @@ bool FlatpakSourcesBackend::removeSource(const QString &id)
break;
}
}
return sourceIt;
}
bool FlatpakSourcesBackend::removeSource(const QString &id)
{
auto sourceIt = sourceById(id);
if (sourceIt) {
FlatpakSourceItem *sourceItem = static_cast<FlatpakSourceItem*>(sourceIt);
g_autoptr(GCancellable) cancellable = g_cancellable_new();
......@@ -223,10 +242,54 @@ void FlatpakSourcesBackend::addRemote(FlatpakRemote *remote, FlatpakInstallation
it->setData(id, IdRole);
it->setFlatpakInstallation(installation);
m_sources->appendRow(it);
int idx = -1;
{
const auto conf = KSharedConfig::openConfig();
const KConfigGroup group = conf->group("FlatpakSources");
const auto ids = group.readEntry<QStringList>("Sources", QStringList());
const auto ourIdx = ids.indexOf(id);
if (ourIdx<0) { //If not present, we put it on top
idx = 0;
} else {
idx=0;
for(int c=m_sources->rowCount(); idx<c; ++idx) {
const auto compIt = m_sources->item(idx);
const int compIdx = ids.indexOf(compIt->data(IdRole).toString());
if (compIdx >= ourIdx) {
break;
}
}
}
}
m_sources->insertRow(idx, it);
if (m_sources->rowCount() == 1)
Q_EMIT firstSourceIdChanged();
Q_EMIT lastSourceIdChanged();
}
QString FlatpakSourcesBackend::idDescription()
{
return i18n("Flatpak repository URI (*.flatpakrepo)");
}
bool FlatpakSourcesBackend::moveSource(const QString& sourceId, int delta)
{
const auto row = sourceById(sourceId)->row();
auto prevRow = m_sources->takeRow(row);
Q_ASSERT(!prevRow.isEmpty());
const auto destRow = row + (delta>0? delta : delta);
m_sources->insertRow(destRow, prevRow);
if (destRow == 0 || row == 0)
Q_EMIT firstSourceIdChanged();
if (destRow == m_sources->rowCount() - 1 || row == m_sources->rowCount() - 1)
Q_EMIT lastSourceIdChanged();
return true;
}
int FlatpakSourcesBackend::originIndex(const QString& sourceId) const
{
return sourceById(sourceId)->row();
}
......@@ -32,8 +32,10 @@ extern "C" {
class FlatpakResource;
class FlatpakSourcesBackend : public AbstractSourcesBackend
{
Q_OBJECT
public:
explicit FlatpakSourcesBackend(const QVector<FlatpakInstallation *>& installations, AbstractResourcesBackend *parent);
~FlatpakSourcesBackend() override;
QAbstractItemModel* sources() override;
bool addSource(const QString &id) override;
......@@ -43,7 +45,13 @@ public:
bool supportsAdding() const override { return true; }
FlatpakRemote * installSource(FlatpakResource *resource);
bool canMoveSources() const override { return true; }
bool moveSource(const QString & sourceId, int delta) override;
int originIndex(const QString& sourceId) const;
private:
QStandardItem* sourceById(const QString & sourceId) const;
bool listRepositories(FlatpakInstallation *installation);
void addRemote(FlatpakRemote *remote, FlatpakInstallation *installation);
......
......@@ -20,6 +20,7 @@
#include "AbstractSourcesBackend.h"
#include "AbstractResourcesBackend.h"
#include <QAbstractItemModel>
AbstractSourcesBackend::AbstractSourcesBackend(AbstractResourcesBackend* parent)
: QObject(parent)
......@@ -32,3 +33,22 @@ AbstractResourcesBackend * AbstractSourcesBackend::resourcesBackend() const
return dynamic_cast<AbstractResourcesBackend*>(parent());
}
bool AbstractSourcesBackend::moveSource(const QString& sourceId, int delta)
{
Q_UNUSED(sourceId)
Q_UNUSED(delta)
return false;
}
QString AbstractSourcesBackend::firstSourceId() const
{
auto m = const_cast<AbstractSourcesBackend*>(this)->sources();
return m->index(0, 0).data(AbstractSourcesBackend::IdRole).toString();
}
QString AbstractSourcesBackend::lastSourceId() const
{
auto m = const_cast<AbstractSourcesBackend*>(this)->sources();
return m->index(m->rowCount()-1, 0).data(AbstractSourcesBackend::IdRole).toString();
}
......@@ -36,6 +36,9 @@ class DISCOVERCOMMON_EXPORT AbstractSourcesBackend : public QObject
Q_PROPERTY(QString idDescription READ idDescription CONSTANT)
Q_PROPERTY(QList<QAction*> actions READ actions CONSTANT)
Q_PROPERTY(bool supportsAdding READ supportsAdding CONSTANT)
Q_PROPERTY(bool canMoveSources READ canMoveSources CONSTANT)
Q_PROPERTY(QString firstSourceId READ firstSourceId NOTIFY firstSourceIdChanged)
Q_PROPERTY(QString lastSourceId READ lastSourceId NOTIFY lastSourceIdChanged)
public:
explicit AbstractSourcesBackend(AbstractResourcesBackend* parent);
~AbstractSourcesBackend() override;
......@@ -58,7 +61,15 @@ class DISCOVERCOMMON_EXPORT AbstractSourcesBackend : public QObject
AbstractResourcesBackend* resourcesBackend() const;
virtual bool canMoveSources() const { return false; }
Q_SCRIPTABLE virtual bool moveSource(const QString &sourceId, int delta);
QString firstSourceId() const;
QString lastSourceId() const;
Q_SIGNALS:
void firstSourceIdChanged();
void lastSourceIdChanged();
void passiveMessage(const QString &message);
};
......
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