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

0001 /*
0002  *   SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *   SPDX-FileCopyrightText: 2018 Abhijeet Sharma <sharma.abhijeet2096@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 "FwupdSourcesBackend.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QString>
0012 
0013 class FwupdSourcesModel : public QStandardItemModel
0014 {
0015     Q_OBJECT
0016 public:
0017     FwupdSourcesModel(FwupdSourcesBackend *backend)
0018         : QStandardItemModel(backend)
0019         , m_backend(backend)
0020     {
0021     }
0022 
0023     bool setData(const QModelIndex &index, const QVariant &value, int role) override
0024     {
0025         auto item = itemFromIndex(index);
0026         if (!item)
0027             return false;
0028 
0029         FwupdRemote *remote = fwupd_client_get_remote_by_id(m_backend->backend->client,
0030                                                             item->data(AbstractSourcesBackend::IdRole).toString().toUtf8().constData(),
0031                                                             nullptr,
0032                                                             nullptr);
0033         switch (role) {
0034         case Qt::CheckStateRole: {
0035             if (value == Qt::Checked) {
0036                 m_backend->m_currentItem = item;
0037                 if (fwupd_remote_has_flag(remote, FWUPD_REMOTE_FLAG_APPROVAL_REQUIRED)) {
0038                     QString eulaText = i18n("The remote %1 require that you accept their license:\n %2",
0039                                             QString::fromUtf8(fwupd_remote_get_title(remote)),
0040                                             QString::fromUtf8(fwupd_remote_get_agreement(remote)));
0041                     Q_EMIT m_backend->proceedRequest(i18n("Review EULA"), eulaText);
0042                 } else {
0043                     m_backend->proceed();
0044                 }
0045             } else if (value.toInt() == Qt::Unchecked) {
0046                 g_autoptr(GError) error = nullptr;
0047                 if (fwupd_client_modify_remote(m_backend->backend->client, fwupd_remote_get_id(remote), "Enabled", "false", nullptr, &error))
0048                     item->setCheckState(Qt::Unchecked);
0049                 else
0050                     qWarning() << "could not disable remote" << remote << error->message;
0051             }
0052             return true;
0053         }
0054         }
0055         return false;
0056     }
0057 
0058 private:
0059     FwupdSourcesBackend *const m_backend;
0060 };
0061 
0062 FwupdSourcesBackend::FwupdSourcesBackend(AbstractResourcesBackend *parent)
0063     : AbstractSourcesBackend(parent)
0064     , backend(qobject_cast<FwupdBackend *>(parent))
0065     , m_sources(new FwupdSourcesModel(this))
0066 {
0067     populateSources();
0068 }
0069 
0070 void FwupdSourcesBackend::populateSources()
0071 {
0072     g_autoptr(GError) error = nullptr;
0073     g_autoptr(GPtrArray) remotes = fwupd_client_get_remotes(backend->client, nullptr, &error);
0074     if (!remotes) {
0075         qWarning() << "could not list fwupd remotes" << error->message;
0076         return;
0077     }
0078 
0079     for (uint i = 0; i < remotes->len; i++) {
0080         FwupdRemote *remote = (FwupdRemote *)g_ptr_array_index(remotes, i);
0081         if (fwupd_remote_get_kind(remote) == FWUPD_REMOTE_KIND_LOCAL)
0082             continue;
0083         const QString id = QString::fromUtf8(fwupd_remote_get_id(remote));
0084         if (id.isEmpty())
0085             continue;
0086 
0087         QStandardItem *it = new QStandardItem(id);
0088         it->setData(id, AbstractSourcesBackend::IdRole);
0089         it->setData(QVariant(QString::fromUtf8(fwupd_remote_get_title(remote))), Qt::ToolTipRole);
0090         it->setCheckable(true);
0091         it->setCheckState(fwupd_remote_has_flag(remote, FWUPD_REMOTE_FLAG_ENABLED) ? Qt::Checked : Qt::Unchecked);
0092         m_sources->appendRow(it);
0093     }
0094 }
0095 
0096 QAbstractItemModel *FwupdSourcesBackend::sources()
0097 {
0098     return m_sources;
0099 }
0100 
0101 bool FwupdSourcesBackend::addSource(const QString &id)
0102 {
0103     qWarning() << "Fwupd Error: Custom Addition of Sources Not Allowed"
0104                << "Remote-ID" << id;
0105     return false;
0106 }
0107 
0108 bool FwupdSourcesBackend::removeSource(const QString &id)
0109 {
0110     qWarning() << "Fwupd Error: Removal of Sources Not Allowed"
0111                << "Remote-ID" << id;
0112     return false;
0113 }
0114 
0115 QVariantList FwupdSourcesBackend::actions() const
0116 {
0117     return {};
0118 }
0119 
0120 void FwupdSourcesBackend::cancel()
0121 {
0122     FwupdRemote *remote =
0123         fwupd_client_get_remote_by_id(backend->client, m_currentItem->data(AbstractSourcesBackend::IdRole).toString().toUtf8().constData(), nullptr, nullptr);
0124     m_currentItem->setCheckState(fwupd_remote_has_flag(remote, FWUPD_REMOTE_FLAG_ENABLED) ? Qt::Checked : Qt::Unchecked);
0125 
0126     m_currentItem = nullptr;
0127 }
0128 
0129 void FwupdSourcesBackend::proceed()
0130 {
0131     const QString id = m_currentItem->data(AbstractSourcesBackend::IdRole).toString();
0132     FwupdRemote *remote = fwupd_client_get_remote_by_id(backend->client, id.toUtf8().constData(), nullptr, nullptr);
0133     g_autoptr(GError) error = nullptr;
0134     if (fwupd_client_modify_remote(backend->client, fwupd_remote_get_id(remote), "Enabled", "true", nullptr, &error))
0135         m_currentItem->setData(Qt::Checked, Qt::CheckStateRole);
0136     else
0137         Q_EMIT passiveMessage(i18n("Could not enable remote %1: %2", id, QString::fromUtf8(error ? error->message : "")));
0138 
0139     m_currentItem = nullptr;
0140 }
0141 
0142 #include "FwupdSourcesBackend.moc"