File indexing completed on 2024-05-12 05:28:50

0001 /*
0002  *   SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "AppStreamIntegration.h"
0008 
0009 #include <AppStreamQt/systeminfo.h>
0010 #include <AppStreamQt/utils.h>
0011 #include <AppStreamQt/version.h>
0012 #include <KConfigGroup>
0013 #include <KSharedConfig>
0014 #include <QDebug>
0015 
0016 using namespace Qt::StringLiterals;
0017 
0018 AppStreamIntegration *AppStreamIntegration::global()
0019 {
0020     static AppStreamIntegration *var = nullptr;
0021     if (!var) {
0022         var = new AppStreamIntegration;
0023     }
0024 
0025     return var;
0026 }
0027 
0028 std::optional<AppStream::Release> AppStreamIntegration::getDistroUpgrade(AppStream::Pool *pool)
0029 {
0030     QString distroId = AppStream::SystemInfo::currentDistroComponentId();
0031 
0032     // Look at releases to see if we have a new major version available.
0033     const auto distroComponents = pool->componentsById(distroId);
0034     if (distroComponents.isEmpty()) {
0035         qWarning() << "AppStreamIntegration: No distro component found for" << distroId;
0036         return std::nullopt;
0037     }
0038 
0039     KConfigGroup settings(KSharedConfig::openConfig(QStringLiteral("discoverrc")), u"DistroUpgrade"_s);
0040     bool allowPreRelease = settings.readEntry<bool>("AllowPreRelease", false);
0041 
0042     QString currentVersion = osRelease()->versionId();
0043     std::optional<AppStream::Release> nextRelease;
0044     for (const AppStream::Component &dc : distroComponents) {
0045 #if ASQ_CHECK_VERSION(1, 0, 0)
0046         const auto releases = dc.releasesPlain().entries();
0047 #else
0048         const auto releases = dc.releases();
0049 #endif
0050         for (const auto &r : releases) {
0051             // Only look at stable releases unless requested
0052             if (! (r.kind() == AppStream::Release::KindStable ||
0053                   (r.kind() == AppStream::Release::KindDevelopment && allowPreRelease))) {
0054                 continue;
0055             }
0056 
0057             // Let's look at this potentially new verson
0058             const QString newVersion = r.version();
0059             if (AppStream::Utils::vercmpSimple(newVersion, currentVersion) > 0) {
0060                 if (!nextRelease) {
0061                     // No other newer version found yet so let's pick this one
0062                     nextRelease = r;
0063                     qInfo() << "Found new major release:" << newVersion;
0064                 } else if (AppStream::Utils::vercmpSimple(nextRelease->version(), newVersion) > 0) {
0065                     // We only offer updating to the very next major release so
0066                     // we pick the smallest of all the newest versions
0067                     nextRelease = r;
0068                     qInfo() << "Found a closer new major release:" << newVersion;
0069                 }
0070             }
0071         }
0072     }
0073 
0074     return nextRelease;
0075 }