FrameworkIntegration

appstream/main.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include <AppStreamQt/pool.h>
9#include <PackageKit/Daemon>
10#include <QCoreApplication>
11#include <QDebug>
12
13using namespace AppStream;
14
15int main(int argc, char **argv)
16{
17 QCoreApplication app(argc, argv);
18 Q_ASSERT(app.arguments().count() == 2);
19
20 const QUrl url(app.arguments().last());
21 Q_ASSERT(url.isValid());
22 Q_ASSERT(url.scheme() == QLatin1String("appstream"));
23
24 const QString componentName = url.host();
25 if (componentName.isEmpty()) {
26 qWarning() << "wrongly formatted URI" << url;
27 return 1;
28 }
29
30 Pool pool;
31 auto b = pool.load();
32 Q_ASSERT(b);
33 const auto components = pool.componentsById(componentName).toList();
34 if (components.isEmpty()) {
35 qWarning() << "couldn't find" << componentName;
36 return 1;
37 }
38
39 QStringList packages;
40 for (const auto &component : components) {
41 packages += component.packageNames();
42 }
43 packages.removeDuplicates();
44
45 if (packages.isEmpty()) {
46 qWarning() << "no packages to install";
47 return 1;
48 }
49
50 auto resolveTransaction = PackageKit::Daemon::global()->resolve(packages, PackageKit::Transaction::FilterArch);
51 Q_ASSERT(resolveTransaction);
52
54
55 QObject::connect(resolveTransaction,
56 &PackageKit::Transaction::package,
57 resolveTransaction,
58 [&pkgs](PackageKit::Transaction::Info info, const QString &packageID, const QString & /*summary*/) {
59 if (info == PackageKit::Transaction::InfoAvailable)
60 pkgs[PackageKit::Daemon::packageName(packageID)] = packageID;
61 qDebug() << "resolved package" << info << packageID;
62 });
63 QObject::connect(resolveTransaction, &PackageKit::Transaction::finished, resolveTransaction, [&app, &pkgs](PackageKit::Transaction::Exit status) {
64 if (status != PackageKit::Transaction::ExitSuccess) {
65 qWarning() << "resolve failed" << status;
67 return;
68 }
69 QStringList pkgids = pkgs.values();
70
71 if (pkgids.isEmpty()) {
72 qDebug() << "Nothing to install";
74 } else {
75 qDebug() << "installing..." << pkgids;
76 pkgids.removeDuplicates();
77 auto installTransaction = PackageKit::Daemon::global()->installPackages(pkgids);
78 QObject::connect(installTransaction, &PackageKit::Transaction::finished, &app, [](PackageKit::Transaction::Exit status) {
79 qDebug() << "install finished" << status;
80 QCoreApplication::exit(status == PackageKit::Transaction::ExitSuccess ? 0 : 1);
81 });
82 }
83 });
84 return app.exec();
85}
Q_SCRIPTABLE CaptureState status()
void exit(int returnCode)
QList< T > values() const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
qsizetype removeDuplicates()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:10:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.