File: AppStreamIntegration.cpp

package info (click to toggle)
plasma-discover 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,288 kB
  • sloc: cpp: 30,576; xml: 2,710; python: 311; sh: 5; makefile: 5
file content (124 lines) | stat: -rw-r--r-- 4,718 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 *   SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
 *
 *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

#include "AppStreamIntegration.h"

#include <AppStreamQt/systeminfo.h>
#include <AppStreamQt/utils.h>
#include <AppStreamQt/version.h>
#include <KConfigGroup>
#include <KSharedConfig>
#include <QDebug>

using namespace Qt::StringLiterals;

AppStreamIntegration *AppStreamIntegration::global()
{
    static AppStreamIntegration *var = nullptr;
    if (!var) {
        var = new AppStreamIntegration;
    }

    return var;
}

std::optional<AppStream::Release> AppStreamIntegration::getDistroUpgrade(AppStream::Pool *pool, std::optional<QString> IdOverride)
{
    QString distroId;
    if (IdOverride) {
        distroId = *IdOverride;
    } else {
        distroId = AppStream::SystemInfo::currentDistroComponentId();
    }

    // Look at releases to see if we have a new major version available.
    const auto distroComponents = pool->componentsById(distroId);
    if (distroComponents.isEmpty()) {
        qWarning() << "AppStreamIntegration: No distro component found for" << distroId;
        return std::nullopt;
    }

    // Settings to control if pre-release and development releases are offered
    // for updates. Those options are not exposed to users via a graphical
    // interface and must be manually set in the configuration file.
    //
    // For example for Fedora:
    // - the development release is always Rawhide
    // - the pre-release is a version that has been branched from Rawhide but is
    //   not stable yet. For example Fedora 40 Beta
    //
    // In Appstream metadata terms:
    // - Development releases are specified as KindSnapshot
    // - Pre-releases are specified as KindDevelopment
    KConfigGroup settings(KSharedConfig::openConfig(QStringLiteral("discoverrc")), QStringLiteral("DistroUpgrade"));
    bool allowPreRelease = settings.readEntry<bool>("AllowPreRelease", false);
    bool allowDevelopmentRelease = settings.readEntry<bool>("AllowDevelopmentRelease", false);

    QString currentVersion = osRelease()->versionId();

    // skip if current version is unknown
    if (currentVersion.isEmpty()) {
        qWarning() << "AppStreamIntegration: Cannot detect current distro version for" << distroId;
        return std::nullopt;
    }

    std::optional<AppStream::Release> nextRelease;
    std::optional<AppStream::Release> developmentRelease;
    const QDateTime now = QDateTime::currentDateTimeUtc();
    for (const AppStream::Component &dc : distroComponents) {
        const auto releases = dc.releasesPlain().entries();
        for (const auto &r : releases) {
            // Keep the development release for later only if requested
            if (r.kind() == AppStream::Release::KindSnapshot) {
                if (allowDevelopmentRelease) {
                    developmentRelease = r;
                }
                continue;
            }

            // Skip releases that are not stable
            if (r.kind() != AppStream::Release::KindStable) {
                // Skip pre-releases if not requested
                if (!(r.kind() == AppStream::Release::KindDevelopment && allowPreRelease)) {
                    continue;
                }
            }

            // Skip stable and development releases with a start date in the future if we did not enable pre-releases
            if (r.timestamp() > now && !allowPreRelease) {
                continue;
            }

            // Look at the potentially new version
            const QString newVersion = r.version();
            if (AppStream::Utils::vercmpSimple(newVersion, currentVersion) > 0) {
                if (!nextRelease) {
                    // No other newer version found yet so let's pick this one
                    nextRelease = r;
                    qInfo() << "Found new major release:" << newVersion;
                } else if (AppStream::Utils::vercmpSimple(nextRelease->version(), newVersion) > 0) {
                    // We only offer updating to the very next major release so
                    // we pick the smallest of all the newest versions
                    nextRelease = r;
                    qInfo() << "Found a closer new major release:" << newVersion;
                }
            }
        }
    }

    // If:
    // - we did not find a major upcoming release
    // - we are allowed to look at development releases
    // - we found a development release
    // then offer it.
    if (!nextRelease && allowDevelopmentRelease && developmentRelease) {
        nextRelease = developmentRelease;
    }

    return nextRelease;
}

#include "moc_AppStreamIntegration.cpp"