File indexing completed on 2024-04-28 09:40:58

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2017-2020 Harald Sitter <sitter@kde.org>
0003 
0004 #include "Installer.h"
0005 
0006 #include <QApplication>
0007 #include <QDebug>
0008 
0009 #include <PackageKit/Daemon>
0010 #include <KLocalizedString>
0011 
0012 #include "Debug.h"
0013 #include "FileResolver.h"
0014 
0015 Installer::Installer(const QStringList &files)
0016 {
0017     for (const auto &file : files) {
0018         m_files.push_back(std::make_shared<File>(file));
0019     }
0020 }
0021 
0022 QList<QObject *> Installer::fileQObjects() const
0023 {
0024     QList<QObject *> list;
0025     list.reserve(m_files.size());
0026     for (const auto &file : std::as_const(m_files)) {
0027         list << file.get();
0028     }
0029     return list;
0030 }
0031 
0032 void Installer::resolve()
0033 {
0034     for (const auto &file : std::as_const(m_files)) {
0035         auto resolver = new FileResolver(file, this);
0036         ++m_pendingResolvers;
0037         connect(resolver, &FileResolver::finished, this, [this, file] {
0038             sender()->deleteLater();
0039             qCDebug(INSTALLER) << "RESOLVED" << file->packageID() << file->debugPackageID() << m_pendingResolvers;
0040             if (!file->debugPackageID().isEmpty() && !file->isDebugPackageInstalled()) {
0041                 m_debugPackageIDs << file->debugPackageID();
0042             }
0043             if (--m_pendingResolvers <= 0)  {
0044                 prepareInstall();
0045             }
0046         });
0047         resolver->resolve();
0048     }
0049 }
0050 
0051 void Installer::prepareInstall()
0052 {
0053     std::sort(m_debugPackageIDs.begin(), m_debugPackageIDs.end());
0054     auto last = std::unique(m_debugPackageIDs.begin(), m_debugPackageIDs.end());
0055     m_debugPackageIDs.erase(last, m_debugPackageIDs.end());
0056 
0057     if (m_debugPackageIDs.isEmpty()) {
0058         m_error = i18nc("@label", "Failed to find any suitable debug packages or they are all already installed.");
0059         Q_EMIT changed();
0060     }
0061 
0062     m_ready = true;
0063     Q_EMIT changed();
0064 }
0065 
0066 void Installer::install() {
0067     m_installing = true;
0068     Q_EMIT changed();
0069 
0070     qDebug() << "DO INSTALL" << m_debugPackageIDs;
0071 
0072     auto transaction = PackageKit::Daemon::installPackages(m_debugPackageIDs);
0073     connect(transaction,
0074             &PackageKit::Transaction::errorCode,
0075             this,
0076             [&](PackageKit::Transaction::Error, const QString &details) {
0077                 m_error = details;
0078                 Q_EMIT changed();
0079             });
0080     connect(transaction, &PackageKit::Transaction::finished, this, [&](PackageKit::Transaction::Exit status, uint) {
0081         m_installing = false;
0082         m_installed = true;
0083         Q_EMIT changed();
0084 
0085         switch (status) {
0086         case PackageKit::Transaction::ExitSuccess:
0087             for (const auto &id : std::as_const(m_debugPackageIDs)) {
0088                 for (const auto &file : std::as_const(m_files)) {
0089                     if (id == file->debugPackageID()) {
0090                         file->setDebugPackageInstalled();
0091                     }
0092                 }
0093             }
0094             return;
0095         default:
0096             qCDebug(INSTALLER) << "unexpected exit status" << status;
0097             return;
0098         }
0099     });
0100 }