aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/updateinfo/updateinfotools.h
blob: f664628966c894e054b7554bc841ae77ec088482 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/****************************************************************************
**
** Copyright (C) 2022 the Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include <utils/algorithm.h>
#include <utils/optional.h>

#include <QDomDocument>
#include <QList>
#include <QLoggingCategory>
#include <QRegularExpression>
#include <QVersionNumber>

Q_DECLARE_LOGGING_CATEGORY(updateLog)

struct Update
{
    QString name;
    QString version;

    bool operator==(const Update &other) const
    {
        return other.name == name && other.version == version;
    };
};

QList<Update> availableUpdates(const QString &updateXml)
{
    QDomDocument document;
    document.setContent(updateXml);
    if (document.isNull() || !document.firstChildElement().hasChildNodes())
        return {};
    QList<Update> result;
    const QDomNodeList updates = document.firstChildElement().elementsByTagName("update");
    for (int i = 0; i < updates.size(); ++i) {
        const QDomNode node = updates.item(i);
        if (node.isElement()) {
            const QDomElement element = node.toElement();
            if (element.hasAttribute("name"))
                result.append({element.attribute("name"), element.attribute("version")});
        }
    }
    return result;
}

struct QtPackage
{
    QString displayName;
    QVersionNumber version;
    bool installed;
    bool isPrerelease = false;

    bool operator==(const QtPackage &other) const
    {
        return other.installed == installed && other.isPrerelease == isPrerelease
               && other.version == version && other.displayName == displayName;
    }
};

QList<QtPackage> availableQtPackages(const QString &packageXml)
{
    QDomDocument document;
    document.setContent(packageXml);
    if (document.isNull() || !document.firstChildElement().hasChildNodes())
        return {};
    QList<QtPackage> result;
    const QDomNodeList packages = document.firstChildElement().elementsByTagName("package");
    for (int i = 0; i < packages.size(); ++i) {
        const QDomNode node = packages.item(i);
        if (node.isElement()) {
            const QDomElement element = node.toElement();
            if (element.hasAttribute("displayname") && element.hasAttribute("name")
                && element.hasAttribute("version")) {
                QtPackage package{element.attribute("displayname"),
                                  QVersionNumber::fromString(element.attribute("version")),
                                  element.hasAttribute("installedVersion")};
                // Heuristic: Prerelease if the name is not "Qt x.y.z"
                // (prereleases are named "Qt x.y.z-alpha" etc)
                package.isPrerelease = package.displayName
                                       != QString("Qt %1").arg(package.version.toString());
                result.append(package);
            }
        }
    }
    std::sort(result.begin(), result.end(), [](const QtPackage &p1, const QtPackage &p2) {
        return p1.version > p2.version;
    });
    return result;
}

// Expects packages to be sorted, high version first.
Utils::optional<QtPackage> highestInstalledQt(const QList<QtPackage> &packages)
{
    const auto highestInstalledIt = std::find_if(packages.cbegin(),
                                                 packages.cend(),
                                                 [](const QtPackage &p) { return p.installed; });
    if (highestInstalledIt == packages.cend()) // Qt not installed
        return {};
    return *highestInstalledIt;
}

// Expects packages to be sorted, high version first.
Utils::optional<QtPackage> qtToNagAbout(const QList<QtPackage> &allPackages,
                                        QVersionNumber *highestSeen)
{
    // Filter out any Qt prereleases
    const QList<QtPackage> packages = Utils::filtered(allPackages, [](const QtPackage &p) {
        return !p.isPrerelease;
    });
    if (packages.isEmpty())
        return {};
    const QtPackage highest = packages.constFirst();
    qCDebug(updateLog) << "Highest available (non-prerelease) Qt:" << highest.version;
    qCDebug(updateLog) << "Highest previously seen (non-prerelease) Qt:" << *highestSeen;
    // if the highestSeen version is null, we don't know if the Qt version is new, and better don't nag
    const bool isNew = !highestSeen->isNull() && highest.version > *highestSeen;
    if (highestSeen->isNull() || isNew)
        *highestSeen = highest.version;
    if (!isNew)
        return {};
    const Utils::optional<QtPackage> highestInstalled = highestInstalledQt(packages);
    qCDebug(updateLog) << "Highest installed Qt:"
                       << qPrintable(highestInstalled ? highestInstalled->version.toString()
                                                      : QString("none"));
    if (!highestInstalled) // don't nag if no Qt is installed at all
        return {};
    if (highestInstalled->version == highest.version)
        return {};
    return highest;
}

Q_DECLARE_METATYPE(Update)
Q_DECLARE_METATYPE(QtPackage)