summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/installercalculator.cpp
blob: 4c53824af360833ecbe18bd3ded1b5d008d8af01 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**************************************************************************
**
** Copyright (C) 2023 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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.
**
** $QT_END_LICENSE$
**
**************************************************************************/

#include "installercalculator.h"

#include "component.h"
#include "componentalias.h"
#include "componentmodel.h"
#include "packagemanagercore.h"
#include "settings.h"
#include <globals.h>

namespace QInstaller {

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::InstallerCalculator
    \internal
*/

InstallerCalculator::InstallerCalculator(PackageManagerCore *core, const AutoDependencyHash &autoDependencyComponentHash)
    : CalculatorBase(core)
    , m_autoDependencyComponentHash(autoDependencyComponentHash)
{
}

InstallerCalculator::~InstallerCalculator()
{
}

bool InstallerCalculator::solve()
{
    if (!solve(m_core->aliasesMarkedForInstallation()))
        return false;

    // Subtract components added by aliases
    QList<Component *> components = m_core->componentsMarkedForInstallation();
    for (auto *component : qAsConst(m_resolvedComponents))
        components.removeAll(component);

    return solve(components);
}

QString InstallerCalculator::resolutionText(Component *component) const
{
    const Resolution reason = resolutionType(component);
    switch (reason) {
        case Resolution::Automatic:
            return QCoreApplication::translate("InstallerCalculator",
                "Components added as automatic dependencies:");
        case Resolution::Dependent:
            return QCoreApplication::translate("InstallerCalculator", "Components added as "
                "dependency for \"%1\":").arg(referencedComponent(component));
        case Resolution::Resolved:
            return QCoreApplication::translate("InstallerCalculator",
                "Components that have resolved dependencies:");
        case Resolution::Selected:
            return QCoreApplication::translate("InstallerCalculator",
                "Selected components without dependencies:");
        case Resolution::Alias:
            return QCoreApplication::translate("InstallerCalculator",
                "Components selected by alias \"%1\":").arg(referencedComponent(component));
        default:
            Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid install resolution detected!");
    }
    return QString();
}

bool InstallerCalculator::solve(const QList<Component *> &components)
{
    if (components.isEmpty())
        return true;

    QList<Component*> notAppendedComponents; // for example components with unresolved dependencies
    for (Component *component : qAsConst(components)){
        if (!component)
            continue;
        if (m_toInstallComponentIds.contains(component->name())) {
            const QString errorMessage = recursionError(component);
            qCWarning(QInstaller::lcInstallerInstallLog).noquote() << errorMessage;
            m_errorString.append(errorMessage);
            Q_ASSERT_X(!m_toInstallComponentIds.contains(component->name()), Q_FUNC_INFO,
                qPrintable(errorMessage));
            return false;
        }

        if (component->currentDependencies().isEmpty())
            addComponentForInstall(component);
        else
            notAppendedComponents.append(component);
    }

    for (Component *component : qAsConst(notAppendedComponents)) {
        if (!solveComponent(component))
            return false;
    }

    // All regular dependencies are resolved. Now we are looking for auto depend on components.
    QSet<Component *> foundAutoDependOnList = autodependencyComponents();
    if (!foundAutoDependOnList.isEmpty())
        return solve(foundAutoDependOnList.values());

    return true;
}

bool InstallerCalculator::solve(const QList<ComponentAlias *> &aliases)
{
    if (aliases.isEmpty())
        return true;

    QList<ComponentAlias *> notAppendedAliases; // Aliases that require other aliases
    for (auto *alias : aliases) {
        if (!alias)
            continue;

        if (m_toInstallComponentAliases.contains(alias->name())) {
            const QString errorMessage = QCoreApplication::translate("InstallerCalculator",
                "Recursion detected, component alias \"%1\" already added.").arg(alias->name());
            qCWarning(QInstaller::lcInstallerInstallLog).noquote() << errorMessage;
            m_errorString.append(errorMessage);

            Q_ASSERT_X(!m_toInstallComponentAliases.contains(alias->name()), Q_FUNC_INFO,
                qPrintable(errorMessage));

            return false;
        }

        if (alias->aliases().isEmpty()) {
            if (!addComponentsFromAlias(alias))
                return false;
        } else {
            notAppendedAliases.append(alias);
        }
    }

    for (auto *alias : qAsConst(notAppendedAliases)) {
        if (!solveAlias(alias))
            return false;
    }

    return true;
}

void InstallerCalculator::addComponentForInstall(Component *component, const QString &version)
{
    if (!m_componentsForAutodepencencyCheck.contains(component))
        m_componentsForAutodepencencyCheck.append(component);

    if (!component->isInstalled(version) || (m_core->isUpdater() && component->isUpdateAvailable())) {
        m_resolvedComponents.append(component);
        m_toInstallComponentIds.insert(component->name());
    }
}

bool InstallerCalculator::addComponentsFromAlias(ComponentAlias *alias)
{
    QList<Component *> componentsToAdd;
    for (auto *component : alias->components()) {
        if (m_toInstallComponentIds.contains(component->name()))
            continue; // Already added

        componentsToAdd.append(component);
        // Updates the model, so that we also check the descendant
        // components when calculating components to install
        updateCheckState(component, Qt::Checked);
        insertResolution(component, Resolution::Alias, alias->name());
    }

    m_toInstallComponentAliases.insert(alias->name());
    return solve(componentsToAdd);
}

QString InstallerCalculator::recursionError(Component *component) const
{
    return QCoreApplication::translate("InstallerCalculator", "Recursion detected, component \"%1\" "
        "already added with reason: \"%2\"").arg(component->name(), resolutionText(component));
}

bool InstallerCalculator::updateCheckState(Component *component, Qt::CheckState state)
{
    ComponentModel *currentModel = m_core->isUpdater()
        ? m_core->updaterComponentModel()
        : m_core->defaultComponentModel();

    const QModelIndex &idx = currentModel->indexFromComponentName(component->treeName());
    return currentModel->setData(idx, state, Qt::CheckStateRole);
}

bool InstallerCalculator::solveComponent(Component *component, const QString &version)
{
    const QStringList dependenciesList = component->currentDependencies();
    QString requiredDependencyVersion = version;
    for (const QString &dependencyComponentName : dependenciesList) {
        // PackageManagerCore::componentByName returns 0 if dependencyComponentName contains a
        // version which is not available
        Component *dependencyComponent = m_core->componentByName(dependencyComponentName);
        if (!dependencyComponent) {
            const QString errorMessage = QCoreApplication::translate("InstallerCalculator",
                "Cannot find missing dependency \"%1\" for \"%2\".").arg(dependencyComponentName,
                component->name());
            qCWarning(QInstaller::lcInstallerInstallLog).noquote() << errorMessage;
            m_errorString.append(errorMessage);
            if (component->packageManagerCore()->settings().allowUnstableComponents()) {
                component->setUnstable(Component::UnstableError::MissingDependency, errorMessage);
                continue;
            } else {
                return false;
            }
        }
        //Check if component requires higher version than what might be already installed
        bool isUpdateRequired = false;
        QString requiredName;
        QString requiredVersion;
        PackageManagerCore::parseNameAndVersion(dependencyComponentName, &requiredName, &requiredVersion);
        if (!requiredVersion.isEmpty() &&
                !dependencyComponent->value(scInstalledVersion).isEmpty()) {
            static const QRegularExpression compEx(QLatin1String("^([<=>]+)(.*)$"));
            QRegularExpressionMatch match = compEx.match(dependencyComponent->value(scInstalledVersion));
            const QString installedVersion = match.hasMatch()
                    ? match.captured(2) : dependencyComponent->value(scInstalledVersion);

            match = compEx.match(requiredVersion);
            requiredVersion = match.hasMatch() ? match.captured(2) : requiredVersion;

            if (KDUpdater::compareVersion(requiredVersion, installedVersion) >= 1 ) {
                isUpdateRequired = true;
                requiredDependencyVersion = requiredVersion;
            }
        }
        //Check dependencies only if
        //- Dependency is not installed or update requested, nor newer version of dependency component required
        //- And dependency component is not already added for install
        //- And component is not already added for install, then dependencies are already resolved
        if (((!dependencyComponent->isInstalled() || dependencyComponent->updateRequested())
                || isUpdateRequired) && (!m_toInstallComponentIds.contains(dependencyComponent->name())
                && !m_toInstallComponentIds.contains(component->name()))) {
            if (m_visitedComponents.value(component).contains(dependencyComponent)) {
                const QString errorMessage = recursionError(component);
                qCWarning(QInstaller::lcInstallerInstallLog).noquote() << errorMessage;
                m_errorString = errorMessage;
                Q_ASSERT_X(!m_visitedComponents.value(component).contains(dependencyComponent),
                    Q_FUNC_INFO, qPrintable(errorMessage));
                return false;
            }
            m_visitedComponents[component].insert(dependencyComponent);
            // add needed dependency components to the next run
            insertResolution(dependencyComponent, Resolution::Dependent,
                component->name());

            if (!solveComponent(dependencyComponent, requiredDependencyVersion))
                return false;
        }
    }

    if (!m_toInstallComponentIds.contains(component->name())) {
        addComponentForInstall(component, requiredDependencyVersion);
        insertResolution(component, Resolution::Resolved);
    }
    return true;
}

bool InstallerCalculator::solveAlias(ComponentAlias *alias)
{
    for (auto *requiredAlias : alias->aliases()) {
        if (!solveAlias(requiredAlias))
            return false;
    }

    if (m_toInstallComponentAliases.contains(alias->name()))
        return true;

    return addComponentsFromAlias(alias);
}

QSet<Component *> InstallerCalculator::autodependencyComponents()
{
    // All regular dependencies are resolved. Now we are looking for auto depend on components.
    // m_componentsForAutodepencencyCheck is a list of recently calculated components to be installed
    // (normal components and regular dependencies components), and we check possible installable auto
    // dependency components based on that list.
    QSet<Component *> foundAutoDependOnList;
    for (const Component *component : qAsConst(m_componentsForAutodepencencyCheck)) {
        if (!m_autoDependencyComponentHash.contains(component->name())
                || (m_core->isUpdater() && !component->updateRequested()))
            continue;
        for (const QString& autoDependency : m_autoDependencyComponentHash.value(component->name())) {
            // If a components is already installed or is scheduled for installation, no need to check
            // for auto depend installation.
            if (m_toInstallComponentIds.contains(autoDependency)) {
                continue;
            }
            Component *autoDependComponent = m_core->componentByName(autoDependency);
            if (!autoDependComponent)
                continue;
            if ((!autoDependComponent->isInstalled()
                 || (m_core->isUpdater() && autoDependComponent->isUpdateAvailable()))
                && !m_toInstallComponentIds.contains(autoDependComponent->name())) {
                // One of the components autodependons is requested for install, check if there
                // are other autodependencies as well
                if (autoDependComponent->isAutoDependOn(m_toInstallComponentIds)) {
                    foundAutoDependOnList.insert(autoDependComponent);
                    insertResolution(autoDependComponent, Resolution::Automatic);
                }
            }
        }
    }
    m_componentsForAutodepencencyCheck.clear();
    return foundAutoDependOnList;
}

} // namespace QInstaller