File indexing completed on 2024-11-17 04:55:44

0001 /*
0002  *   SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  *   SPDX-FileCopyrightText: 2021 Mariam Fahmy Sobhy <mariamfahmy66@gmail.com>
0004  *
0005  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include "RpmOstreeSourcesBackend.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QDebug>
0012 
0013 #include <ostree-repo.h>
0014 #include <ostree.h>
0015 
0016 RpmOstreeSourcesBackend::RpmOstreeSourcesBackend(AbstractResourcesBackend *parent)
0017     : AbstractSourcesBackend(parent)
0018     , m_model(new QStandardItemModel(this))
0019 {
0020     g_autoptr(GFile) path = g_file_new_for_path("/ostree/repo");
0021     g_autoptr(OstreeRepo) repo = ostree_repo_new(path);
0022     if (repo == NULL) {
0023         qInfo() << "rpm-ostree-backend: Could not find ostree repo:" << path;
0024         return;
0025     }
0026 
0027     g_autoptr(GError) err = NULL;
0028     gboolean res = ostree_repo_open(repo, NULL, &err);
0029     if (!res) {
0030         qInfo() << "rpm-ostree-backend: Could not open ostree repo:" << path;
0031         return;
0032     }
0033 
0034     guint remote_count = 0;
0035     char **remotes = ostree_repo_remote_list(repo, &remote_count);
0036     for (guint r = 0; r < remote_count; ++r) {
0037         auto remote = new QStandardItem(QString::fromUtf8(remotes[r]));
0038 
0039         char *url = NULL;
0040         res = ostree_repo_remote_get_url(repo, remotes[r], &url, &err);
0041         if (res) {
0042             remote->setData(QString::fromUtf8(url), Qt::ToolTipRole);
0043             free(url);
0044         } else {
0045             qWarning() << "rpm-ostree-backend: Could not get the URL for ostree remote:" << remotes[r];
0046         }
0047 
0048         m_model->appendRow(remote);
0049     }
0050 
0051     for (guint r = 0; r < remote_count; ++r) {
0052         free(remotes[r]);
0053     }
0054     free(remotes);
0055 }
0056 
0057 QAbstractItemModel *RpmOstreeSourcesBackend::sources()
0058 {
0059     return m_model;
0060 }
0061 
0062 bool RpmOstreeSourcesBackend::addSource(const QString &)
0063 {
0064     return false;
0065 }
0066 
0067 bool RpmOstreeSourcesBackend::removeSource(const QString &)
0068 {
0069     return false;
0070 }
0071 
0072 QString RpmOstreeSourcesBackend::idDescription()
0073 {
0074     return i18n("ostree remotes");
0075 }
0076 
0077 QVariantList RpmOstreeSourcesBackend::actions() const
0078 {
0079     return {};
0080 }
0081 
0082 bool RpmOstreeSourcesBackend::supportsAdding() const
0083 {
0084     return false;
0085 }
0086 
0087 bool RpmOstreeSourcesBackend::canMoveSources() const
0088 {
0089     return false;
0090 }