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

flatpak: Allow saving the new remotes sorting

Saves it to the installations, needs to be in a separate action because
it may trigger policykit dialogs.

It could make sense to design this in a different way.
parent be257ca3
......@@ -70,6 +70,11 @@ public:
QStandardItem::setData(value, role);
}
FlatpakRemote *remote() const
{
return m_remote;
}
private:
FlatpakInstallation *m_installation = nullptr;
FlatpakRemote *const m_remote;
......@@ -81,8 +86,13 @@ FlatpakSourcesBackend::FlatpakSourcesBackend(const QVector<FlatpakInstallation *
, m_preferredInstallation(installations.constFirst())
, m_sources(new QStandardItemModel(this))
, m_flathubAction(new DiscoverAction("flatpak-discover", i18n("Add Flathub"), this))
, m_saveAction(new DiscoverAction("dialog-ok-apply", i18n("Apply Changes"), this))
, m_noSourcesItem(new QStandardItem(QStringLiteral("-")))
{
m_saveAction->setVisible(false);
m_saveAction->setToolTip(i18n("Changes to the priority of these sources must be applied before they will take effect."));
connect(m_saveAction, &DiscoverAction::triggered, this, &FlatpakSourcesBackend::save);
m_flathubAction->setObjectName(QStringLiteral("flathub"));
m_flathubAction->setToolTip(i18n("Makes it possible to easily install the applications listed in https://flathub.org"));
connect(m_flathubAction, &DiscoverAction::triggered, this, [this]() {
......@@ -111,6 +121,22 @@ FlatpakSourcesBackend::~FlatpakSourcesBackend()
delete m_noSourcesItem;
}
void FlatpakSourcesBackend::save()
{
int last = INT_MIN;
for (int i = m_sources->rowCount() - 1; i >= 0; --i) {
auto it = m_sources->item(i);
const int prio = it->data(PrioRole).toInt();
if (prio <= last) {
FlatpakSourceItem *sourceItem = static_cast<FlatpakSourceItem *>(it);
flatpak_remote_set_prio(sourceItem->remote(), ++last);
flatpak_installation_modify_remote(sourceItem->flatpakInstallation(), sourceItem->remote(), nullptr, nullptr);
} else {
last = prio;
}
}
}
QAbstractItemModel *FlatpakSourcesBackend::sources()
{
return m_sources;
......@@ -275,7 +301,7 @@ bool FlatpakSourcesBackend::removeSource(const QString &id)
QVariantList FlatpakSourcesBackend::actions() const
{
return {QVariant::fromValue<QObject *>(m_flathubAction)};
return {QVariant::fromValue<QObject *>(m_flathubAction), QVariant::fromValue<QObject *>(m_saveAction)};
}
void FlatpakSourcesBackend::addRemote(FlatpakRemote *remote, FlatpakInstallation *installation)
......@@ -372,6 +398,7 @@ bool FlatpakSourcesBackend::moveSource(const QString &sourceId, int delta)
Q_EMIT firstSourceIdChanged();
if (destRow == m_sources->rowCount() - 1 || row == m_sources->rowCount() - 1)
Q_EMIT lastSourceIdChanged();
m_saveAction->setVisible(true);
return true;
}
......
......@@ -57,12 +57,14 @@ public:
void cancel() override;
void proceed() override;
void save();
void addRemote(FlatpakRemote *remote, FlatpakInstallation *installation);
private:
FlatpakInstallation *m_preferredInstallation;
QStandardItemModel *m_sources;
DiscoverAction *const m_flathubAction;
DiscoverAction *const m_saveAction;
QStandardItem *m_noSourcesItem;
QStack<std::function<void()>> m_proceedFunctions;
};
......
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