File indexing completed on 2024-04-28 13:41:41

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2010-2020 Harald Sitter <sitter@kde.org>
0003 
0004 #include "DebugRepoEnabler.h"
0005 
0006 #include "BuildConfig.h"
0007 #include "Debug.h"
0008 
0009 void DebugRepoEnabler::run()
0010 {
0011     m_busy = true;
0012     Q_EMIT changed();
0013 
0014     AppStream::Pool pool;
0015     if (!pool.load()) {
0016         m_busy = false;
0017         m_error = QLatin1String("Failed to load appstream pool - ") + pool.lastError();
0018         Q_EMIT changed();
0019         return;
0020     }
0021 
0022     const QString idsString = QString::fromLatin1(DEBUG_REPO_APPSTREAM_IDS);
0023     if (idsString.isEmpty()) {
0024         qCDebug(INSTALLER) << "Skipping DebugRepoEnabler because DEBUG_REPO_APPSTREAM_IDS is empty";
0025         m_busy = false;
0026         m_installed = true;
0027         Q_EMIT changed();
0028         return;
0029     }
0030 
0031     const QStringList ids = idsString.split(QLatin1Char(';'));
0032     qDebug() << ids << ids.count();
0033     Q_ASSERT_X(ids.count() <= 1, Q_FUNC_INFO, "multiple ids are currently not supported - patches welcome");
0034 
0035     QList<AppStream::Component> components;
0036     for (const auto &id : ids) {
0037         const auto matchedComponents = pool.componentsById(id);
0038         components += matchedComponents;
0039 
0040         Q_ASSERT(components.count() == 1); // ensure distros use valid ids
0041         const auto &component = matchedComponents.at(0);
0042         Q_ASSERT(component.isValid()); // we've seen crash reports that indicated invalid components, unclear why. verify them for now.
0043         qWarning() << component.toString();
0044         qWarning() << component.packageNames();
0045         Q_ASSERT(component.kind() == AppStream::Component::KindRepository);
0046     }
0047 
0048     // TODO: for actual multi-id support the class would need various changes vis a vis tracking what is and isn't
0049     const auto &component = components.at(0);
0050 
0051     // Quick and dirty mapping of components into blobs so we can pass them to qml.
0052     m_components.reserve(components.size());
0053     for (const auto &component : components) {
0054         m_components << QJsonObject
0055         {
0056             {QStringLiteral("id"), component.id()},
0057             {QStringLiteral("name"), component.name()},
0058             {QStringLiteral("summary"), component.summary()}
0059         };
0060     }
0061     Q_EMIT changed();
0062 
0063     auto transaction = PackageKit::Daemon::resolve(component.packageNames(), PackageKit::Transaction::FilterArch);
0064     connect(transaction,
0065             &PackageKit::Transaction::package,
0066             this,
0067             [this](PackageKit::Transaction::Info info, const QString &packageID, const QString &summary) {
0068                 Q_UNUSED(summary);
0069                 m_packageIDs.push_back(packageID);
0070                 m_found = true;
0071                 m_installed = (info == PackageKit::Transaction::InfoInstalled);
0072                 Q_EMIT changed();
0073             });
0074     connect(transaction,
0075             &PackageKit::Transaction::finished,
0076             this,
0077             [this](PackageKit::Transaction::Exit status, uint runtime) {
0078                 Q_UNUSED(status);
0079                 Q_UNUSED(runtime);
0080                 m_busy = false;
0081                 Q_EMIT changed();
0082             });
0083 }
0084 
0085 void DebugRepoEnabler::install()
0086 {
0087     m_busy = true;
0088     Q_EMIT changed();
0089 
0090     auto transaction = PackageKit::Daemon::installPackages(m_packageIDs);
0091     connect(transaction,
0092             &PackageKit::Transaction::errorCode,
0093             this,
0094             [this](PackageKit::Transaction::Error error, const QString &details) {
0095                 Q_UNUSED(error);
0096                 m_error = details;
0097                 Q_EMIT changed();
0098             });
0099     connect(
0100         transaction, &PackageKit::Transaction::finished, this, [this](PackageKit::Transaction::Exit status, uint) {
0101             m_installed = (status == PackageKit::Transaction::ExitSuccess);
0102             auto transaction = PackageKit::Daemon::refreshCache(false);
0103             connect(transaction,
0104                     &PackageKit::Transaction::finished,
0105                     this,
0106                     [this](PackageKit::Transaction::Exit status, uint runtime) {
0107                         Q_UNUSED(status);
0108                         Q_UNUSED(runtime);
0109                         m_busy = false;
0110                         Q_EMIT changed();
0111                     });
0112     });
0113 }