File indexing completed on 2024-10-13 03:35:59

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include <AppStreamQt/pool.h>
0009 #include <PackageKit/Daemon>
0010 #include <QCoreApplication>
0011 #include <QDebug>
0012 
0013 using namespace AppStream;
0014 
0015 int main(int argc, char **argv)
0016 {
0017     QCoreApplication app(argc, argv);
0018     Q_ASSERT(app.arguments().count() == 2);
0019 
0020     const QUrl url(app.arguments().last());
0021     Q_ASSERT(url.isValid());
0022     Q_ASSERT(url.scheme() == QLatin1String("appstream"));
0023 
0024     const QString componentName = url.host();
0025     if (componentName.isEmpty()) {
0026         qWarning() << "wrongly formatted URI" << url;
0027         return 1;
0028     }
0029 
0030     Pool pool;
0031     auto b = pool.load();
0032     Q_ASSERT(b);
0033     const auto components = pool.componentsById(componentName).toList();
0034     if (components.isEmpty()) {
0035         qWarning() << "couldn't find" << componentName;
0036         return 1;
0037     }
0038 
0039     QStringList packages;
0040     for (const auto &component : components) {
0041         packages += component.packageNames();
0042     }
0043     packages.removeDuplicates();
0044 
0045     if (packages.isEmpty()) {
0046         qWarning() << "no packages to install";
0047         return 1;
0048     }
0049 
0050     auto resolveTransaction = PackageKit::Daemon::global()->resolve(packages, PackageKit::Transaction::FilterArch);
0051     Q_ASSERT(resolveTransaction);
0052 
0053     QHash<QString, QString> pkgs;
0054 
0055     QObject::connect(resolveTransaction,
0056                      &PackageKit::Transaction::package,
0057                      resolveTransaction,
0058                      [&pkgs](PackageKit::Transaction::Info info, const QString &packageID, const QString & /*summary*/) {
0059                          if (info == PackageKit::Transaction::InfoAvailable)
0060                              pkgs[PackageKit::Daemon::packageName(packageID)] = packageID;
0061                          qDebug() << "resolved package" << info << packageID;
0062                      });
0063     QObject::connect(resolveTransaction, &PackageKit::Transaction::finished, resolveTransaction, [&app, &pkgs](PackageKit::Transaction::Exit status) {
0064         if (status != PackageKit::Transaction::ExitSuccess) {
0065             qWarning() << "resolve failed" << status;
0066             QCoreApplication::exit(1);
0067             return;
0068         }
0069         QStringList pkgids = pkgs.values();
0070 
0071         if (pkgids.isEmpty()) {
0072             qDebug() << "Nothing to install";
0073             QCoreApplication::exit(0);
0074         } else {
0075             qDebug() << "installing..." << pkgids;
0076             pkgids.removeDuplicates();
0077             auto installTransaction = PackageKit::Daemon::global()->installPackages(pkgids);
0078             QObject::connect(installTransaction, &PackageKit::Transaction::finished, &app, [](PackageKit::Transaction::Exit status) {
0079                 qDebug() << "install finished" << status;
0080                 QCoreApplication::exit(status == PackageKit::Transaction::ExitSuccess ? 0 : 1);
0081             });
0082         }
0083     });
0084     return app.exec();
0085 }