summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/componentalias.cpp
blob: 955d715fd6ff0039651736904f98807b49e828b5 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**************************************************************************
**
** 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 "componentalias.h"

#include "constants.h"
#include "globals.h"
#include "packagemanagercore.h"
#include "updater.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

namespace QInstaller {

static const QStringList scPossibleElements {
    scName,
    scDisplayName,
    scDescription,
    scVersion,
    scVirtual,
    scRequiredComponents,
    scRequiredAliases,
    scOptionalComponents,
    scOptionalAliases,
    scReleaseDate
};

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::AliasSource
    \brief Describes a source for alias declarations.
*/

/*!
    \enum QInstaller::AliasSource::SourceFileFormat

    This enum type holds the possible file formats for alias source:

    \value  Unknown
            Invalid or unknown file format.
    \value  Xml
            XML file format.
    \value  Json
            JSON file format.
*/

/*!
    Constructs an alias source with empty information.
*/
AliasSource::AliasSource()
    : priority(-1)
{}

/*!
    Constructs an alias source with source file format \a aFormat, filename \a aFilename, and priority \a aPriority.
*/
AliasSource::AliasSource(SourceFileFormat aFormat, const QString &aFilename, int aPriority)
    : format(aFormat)
    , filename(aFilename)
    , priority(aPriority)
{}

/*!
    Copy-constructs an alias source from \a other.
*/
AliasSource::AliasSource(const AliasSource &other)
{
    format = other.format;
    filename = other.filename;
    priority = other.priority;
}

/*!
    Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
hashValue qHash(const AliasSource &key, hashValue seed)
{
    return qHash(key.filename, seed) ^ key.priority;
}

/*!
    Returns \c true if \a lhs and \a rhs are equal; otherwise returns \c false.
*/
bool operator==(const AliasSource &lhs, const AliasSource &rhs)
{
    return lhs.filename == rhs.filename && lhs.priority == rhs.priority && lhs.format == rhs.format;
}

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::AliasFinder
    \brief Creates component alias objects from parsed alias source files, based
           on version and source priorities.
*/

/*!
    Constructs a new alias finder with \a core as the package manager instance.
*/
AliasFinder::AliasFinder(PackageManagerCore *core)
    : m_core(core)
{
}

/*!
    Destroys the finder and cleans unreleased results.
*/
AliasFinder::~AliasFinder()
{
    clear();
}

/*!
    Runs the finder. Parses the alias source files and creates component alias
    objects based on the parsed data. Same alias may be declared in multiple source
    files, thus source priority and version information is used to decide which
    source is used for creating the alias object.

    Any previous results are cleared when calling this.

    Returns \c true if at least one alias was found, \c false otherwise.
*/
bool AliasFinder::run()
{
    clear();

    if (m_sources.isEmpty())
        return false;

    // 1. Parse source files
    for (auto &source : qAsConst(m_sources)) {
        if (source.format == AliasSource::SourceFileFormat::Unknown) {
            qCWarning(QInstaller::lcInstallerInstallLog)
                << "Unknown alias source format for file:" << source.filename;
            continue;
        }
        if (source.format == AliasSource::SourceFileFormat::Xml)
            parseXml(source);
        else if (source.format == AliasSource::SourceFileFormat::Json)
            parseJson(source);
    }

    // 2. Create aliases based on priority & version
    for (auto &data : qAsConst(m_aliasData)) {
        const QString name = data.value(scName).toString();
        const Resolution resolution = checkPriorityAndVersion(data);
        if (resolution == Resolution::KeepExisting)
            continue;

        if (resolution == Resolution::RemoveExisting)
            delete m_aliases.take(name);

        ComponentAlias *alias = new ComponentAlias(m_core);
        AliasData::const_iterator it;
        for (it = data.cbegin(); it != data.cend(); ++it) {
            if (it.value().canConvert<QString>())
                alias->setValue(it.key(), it.value().toString());
        }
        m_aliases.insert(name, alias);
    }

    return !m_aliases.isEmpty();
}

/*!
    Returns a list of the found aliases.
*/
QList<ComponentAlias *> AliasFinder::aliases() const
{
    return m_aliases.values();
}

/*!
    Sets the alias sources to look alias information from to \a sources.
*/
void AliasFinder::setAliasSources(const QSet<AliasSource> &sources)
{
    clear();
    m_sources = sources;
}

/*!
    Clears the results of the finder.
*/
void AliasFinder::clear()
{
    qDeleteAll(m_aliases);

    m_aliases.clear();
    m_aliasData.clear();
}

/*!
    Reads an XML file specified by \a filename, and constructs a variant map of
    the data for each alias.

    Returns \c true on success, \c false otherwise.
*/
bool AliasFinder::parseXml(AliasSource source)
{
    QFile file(source.filename);
    if (!file.open(QIODevice::ReadOnly)) {
        qCWarning(QInstaller::lcInstallerInstallLog)
            << "Cannot open alias definition for reading:" << file.errorString();
        return false;
    }

    QString error;
    int errorLine;
    int errorColumn;

    QDomDocument doc;
    if (!doc.setContent(&file, &error, &errorLine, &errorColumn)) {
        qCWarning(QInstaller::lcInstallerInstallLog)
            << "Cannot read alias definition document:" << error
            << "line:" << errorLine << "column:" << errorColumn;
        return false;
    }
    file.close();

    const QDomElement root = doc.documentElement();
    const QDomNodeList children = root.childNodes();

    for (int i = 0; i < children.count(); ++i) {
        const QDomElement el = children.at(i).toElement();
        const QString tag = el.tagName();
        if (el.isNull() || tag != scAlias) {
            qCWarning(lcInstallerInstallLog) << "Unexpected element name:" << tag;
            continue;
        }

        AliasData data;
        data.insert(QLatin1String("source"), QVariant::fromValue(source));

        const QDomNodeList c2 = el.childNodes();
        for (int j = 0; j < c2.count(); ++j) {
            const QDomElement el2 = c2.at(j).toElement();
            const QString tag2 = el2.tagName();
            if (!scPossibleElements.contains(tag2)) {
                qCWarning(lcInstallerInstallLog) << "Unexpected element name:" << tag2;
                continue;
            }
            data.insert(tag2, el2.text());
        }

        m_aliasData.insert(data.value(scName).toString(), data);
    }

    return true;
}

/*!
    Reads a JSON file specified by \a source, and constructs a variant map of
    the data for each alias.

    Returns \c true on success, \c false otherwise.
*/
bool AliasFinder::parseJson(AliasSource source)
{
    QFile file(source.filename);
    if (!file.open(QIODevice::ReadOnly)) {
        qCWarning(QInstaller::lcInstallerInstallLog)
            << "Cannot open alias definition for reading:" << file.errorString();
        return false;
    }

    const QByteArray jsonData = file.readAll();
    const QJsonDocument doc(QJsonDocument::fromJson(jsonData));
    const QJsonObject docJsonObject = doc.object();

    const QJsonArray aliases = docJsonObject.value(QLatin1String("alias-packages")).toArray();
    for (auto &it : aliases) {
        AliasData data;
        data.insert(QLatin1String("source"), QVariant::fromValue(source));

        QJsonObject aliasObj = it.toObject();
        for (const auto &key : aliasObj.keys()) {
            if (!scPossibleElements.contains(key)) {
                qCWarning(lcInstallerInstallLog) << "Unexpected element name:" << key;
                continue;
            }

            const QJsonValue jsonValue = aliasObj.value(key);
            if (key == scRequiredComponents || key == scRequiredAliases
                    || key == scOptionalComponents || key == scOptionalAliases) {
                const QJsonArray requirements = jsonValue.toArray();
                QString requiresString;

                for (const auto &it2 : requirements) {
                    requiresString.append(it2.toString());
                    if (it2 != requirements.last())
                        requiresString.append(QLatin1Char(','));
                }

                data.insert(key, requiresString);
            } else if (key == scVirtual) {
                data.insert(key, QVariant(jsonValue.toBool()))->toString();
            } else {
                data.insert(key, jsonValue.toString());
            }
        }

        m_aliasData.insert(data.value(scName).toString(), data);
    }

    return true;
}

/*!
    Checks whether \a data should be used for creating a new alias object,
    based on version and source priority.

    If an alias of the same name exists, always use the one with the higher
    version. If the new alias has the same version but a higher
    priority, use the new new alias. Otherwise keep the already existing alias.

    Returns the resolution of the check.
*/
AliasFinder::Resolution AliasFinder::checkPriorityAndVersion(const AliasData &data) const
{
    for (const auto &existingData : m_aliasData.values(data.value(scName).toString())) {
        if (existingData == data)
            continue;

        const int versionMatch = KDUpdater::compareVersion(data.value(scVersion).toString(),
            existingData.value(scVersion).toString());

        const AliasSource newSource = data.value(QLatin1String("source")).value<AliasSource>();
        const AliasSource oldSource = existingData.value(QLatin1String("source")).value<AliasSource>();

        if (versionMatch > 0) {
            // new alias has higher version, use
            qCDebug(QInstaller::lcDeveloperBuild).nospace() << "Remove Alias 'Name: "
                << data.value(scName).toString() << ", Version: " << existingData.value(scVersion).toString()
                << ", Source: " << oldSource.filename
                << "' found an alias with higher version 'Name: "
                << data.value(scName).toString() << ", Version: " << data.value(scVersion).toString()
                << ", Source: " << newSource.filename << "'";

            return Resolution::RemoveExisting;
        }

        if ((versionMatch == 0) && (newSource.priority > oldSource.priority)) {
            // new alias version equals but priority is higher, use
            qCDebug(QInstaller::lcDeveloperBuild).nospace() << "Remove Alias 'Name: "
                << data.value(scName).toString() << ", Priority: " << oldSource.priority
                << ", Source: " << oldSource.filename
                << "' found an alias with higher priority 'Name: "
                << data.value(scName).toString() << ", Priority: " << newSource.priority
                << ", Source: " << newSource.filename << "'";

            return Resolution::RemoveExisting;
        }

        return Resolution::KeepExisting; // otherwise keep existing
    }

    return Resolution::AddNew;
}

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::ComponentAlias
    \brief The ComponentAlias class represents an alias for single or multiple components.
*/

/*!
    \enum QInstaller::ComponentAlias::UnstableError

    This enum type holds the possible reasons for marking an alias unstable:

    \value  ReferenceToUnstable
            Alias requires another alias that is marked unstable.
    \value  MissingComponent
            Alias requires a component that is missing.
    \value  UnselectableComponent
            Alias requires a component that cannot be selected.
    \value  MissingAlias
            Alias requires another alias that is missing.
    \value  ComponentNameConfict
            Alias has a name that conflicts with a name of a component
*/

/*!
    Constructs a new component alias with \a core as the package manager instance.
*/
ComponentAlias::ComponentAlias(PackageManagerCore *core)
    : m_core(core)
    , m_selected(false)
    , m_unstable(false)
{
}

/*!
    Destructs the alias.
*/
ComponentAlias::~ComponentAlias()
{
}

/*!
    Returns the name of the alias.
*/
QString ComponentAlias::name() const
{
    return m_variables.value(scName);
}

/*!
    Returns the display name of the alias.
*/
QString ComponentAlias::displayName() const
{
    return m_variables.value(scDisplayName);
}

/*!
    Returns the description text of the alias.
*/
QString ComponentAlias::description() const
{
    return m_variables.value(scDescription);
}

/*!
    Returns the version of the alias.
*/
QString ComponentAlias::version() const
{
    return m_variables.value(scVersion);
}

/*!
    Returns \c true if the alias is virtual, \c false otherwise.

    Virtual aliases are aliases that cannot be selected by the
    user, and are invisible. They can be required by other aliases however.
*/
bool ComponentAlias::isVirtual() const
{
    return m_variables.value(scVirtual, scFalse).toLower() == scTrue;
}

/*!
    Returns \c true if the alias is selected for installation, \c false otherwise.
*/
bool ComponentAlias::isSelected() const
{
    return m_selected;
}

/*!
    Sets the selection state of the alias to \a selected. The selection
    does not have an effect if the alias is unselectable.
*/
void ComponentAlias::setSelected(bool selected)
{
    if (selected && (isUnstable() || isVirtual()))
        return;

    m_selected = selected;
}

/*!
    Returns the list of components required by this alias, or an
    empty list if this alias does not require any components.
*/
QList<Component *> ComponentAlias::components()
{
    if (m_components.isEmpty()) {
        const QStringList componentList = QInstaller::splitStringWithComma(
            m_variables.value(scRequiredComponents));

        const QStringList optionalComponentList = QInstaller::splitStringWithComma(
            m_variables.value(scOptionalComponents));

        addRequiredComponents(componentList, false);
        addRequiredComponents(optionalComponentList, true);
    }

    return m_components;
}

/*!
    Returns the list of other aliases required by this alias, or an
    empty list if this alias does not require any other aliases.
*/
QList<ComponentAlias *> ComponentAlias::aliases()
{
    if (m_aliases.isEmpty()) {
        const QStringList aliasList = QInstaller::splitStringWithComma(
            m_variables.value(scRequiredAliases));

        const QStringList optionalAliasList = QInstaller::splitStringWithComma(
            m_variables.value(scOptionalAliases));

        addRequiredAliases(aliasList, false);
        addRequiredAliases(optionalAliasList, true);
    }

    return m_aliases;
}

/*!
    Returns the value specified by \a key, with an optional default value \a defaultValue.
*/
QString ComponentAlias::value(const QString &key, const QString &defaultValue) const
{
    return m_variables.value(key, defaultValue);
}

/*!
    Sets the value specified by \a key to \a value. If the value exists already,
    it is replaced with the new value.
*/
void ComponentAlias::setValue(const QString &key, const QString &value)
{
    const QString normalizedValue = m_core->replaceVariables(value);
    if (m_variables.value(key) == normalizedValue)
        return;

    m_variables[key] = normalizedValue;
}

/*!
    Returns all keys for the component alias values.
*/
QStringList ComponentAlias::keys() const
{
    return m_variables.keys();
}

/*!
    Returns \c true if the alias is marked unstable, \c false otherwise.
*/
bool ComponentAlias::isUnstable() const
{
    return m_unstable;
}

/*!
    Sets the alias unstable with \a error, and a \a message describing the error.
*/
void ComponentAlias::setUnstable(UnstableError error, const QString &message)
{
    setSelected(false);
    m_unstable = true;

    const QMetaEnum metaEnum = QMetaEnum::fromType<ComponentAlias::UnstableError>();
    emit m_core->unstableComponentFound(
        QLatin1String(metaEnum.valueToKey(error)), message, name());
}

/*!
    \internal

    Adds the \a aliases to the list of required aliases by this alias. If \a optional
    is \c true, missing alias references are ignored.
*/
void ComponentAlias::addRequiredAliases(const QStringList &aliases, const bool optional)
{
    for (const auto &aliasName : aliases) {
        ComponentAlias *alias = m_core->aliasByName(aliasName);
        if (!alias) {
            if (optional)
                continue;

            const QString error = QLatin1String("No required alias found by name: ") + aliasName;
            qCWarning(lcInstallerInstallLog) << error;

            setUnstable(UnstableError::MissingAlias, error);
            continue;
        }

        if (alias->isUnstable()) {
            const QString error = QLatin1String("Alias requires another alias "
                                                "that is marked unstable: ") + aliasName;
            qCWarning(lcInstallerInstallLog) << error;

            setUnstable(UnstableError::ReferenceToUnstable, error);
            continue;
        }

        m_aliases.append(alias);
    }
}

/*!
    \internal

    Adds the \a components to the list of required components by this alias. If \a optional
    is \c true, missing component references are ignored.
*/
void ComponentAlias::addRequiredComponents(const QStringList &components, const bool optional)
{
    for (const auto &componentName : components) {
        Component *component = m_core->componentByName(componentName);
        if (!component) {
            if (optional)
                continue;

            const QString error = QLatin1String("No required component found by name: ")
                                  + componentName;
            qCWarning(lcInstallerInstallLog) << error;

            setUnstable(UnstableError::MissingComponent, error);
            continue;
        }

        if (component->isUnstable() || !component->isCheckable()) {
            const QString error = QLatin1String("Alias requires component that is uncheckable or unstable: ")
                                  + componentName;
            qCWarning(lcInstallerInstallLog) << error;

            setUnstable(UnstableError::UnselectableComponent, error);
            continue;
        }

        m_components.append(component);
    }
}

} // namespace QInstaller