File: wizard.cpp

package info (click to toggle)
plasma-mobile 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,412 kB
  • sloc: xml: 38,474; cpp: 18,529; javascript: 139; sh: 82; makefile: 5
file content (122 lines) | stat: -rw-r--r-- 3,949 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
// SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later

#include "wizard.h"
#include "settings.h"
#include "utils.h"

#include <KPackage/PackageLoader>

#include <QFileInfo>
#include <QQmlComponent>

// TODO read distro provided config file
const QList<QString> WIZARD_MODULE_ORDER = {QStringLiteral("org.kde.plasma.mobileinitialstart.prepare"),
                                            QStringLiteral("org.kde.plasma.mobileinitialstart.time"),
                                            QStringLiteral("org.kde.plasma.mobileinitialstart.wifi"),
                                            QStringLiteral("org.kde.plasma.mobileinitialstart.cellular"),
                                            QStringLiteral("org.kde.plasma.mobileinitialstart.systemnavigation"),
                                            QStringLiteral("org.kde.plasma.mobileinitialstart.finished")};

Wizard::Wizard(QObject *parent, QQmlEngine *engine)
    : QObject{parent}
    , m_engine{engine}
{
}

void Wizard::load()
{
    if (!m_engine) {
        return;
    }

    qCDebug(LOGGING_CATEGORY) << "Loading initialstart packages...";

    // load initialstart packages
    const auto packages = KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/GenericQML"), QStringLiteral("plasma/mobileinitialstart"));
    for (auto &metaData : packages) {
        KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("KPackage/GenericQML"), QFileInfo(metaData.fileName()).path());
        if (!package.isValid()) {
            qCWarning(LOGGING_CATEGORY) << "initialstart package invalid:" << metaData.fileName();
            continue;
        }
        m_modulePackages.push_back({new KPluginMetaData{metaData}, package});
    }

    // sort modules by order
    std::sort(m_modulePackages.begin(), m_modulePackages.end(), [](const auto &lhs, const auto &rhs) {
        return WIZARD_MODULE_ORDER.indexOf(lhs.first->pluginId()) < WIZARD_MODULE_ORDER.indexOf(rhs.first->pluginId());
    });

    QQmlComponent *c = new QQmlComponent(m_engine, this);

    // load initialstart QML items
    for (const auto &[pluginMetadata, package] : m_modulePackages) {
        // load QML from kpackage
        c->loadUrl(package.fileUrl("mainscript"), QQmlComponent::PreferSynchronous);

        auto created = c->create(m_engine->rootContext());
        InitialStartModule *createdItem = qobject_cast<InitialStartModule *>(created);

        // print errors if there were issues loading
        if (!createdItem) {
            qCWarning(LOGGING_CATEGORY) << "Unable to load initialstart module:" << created;
            for (auto error : c->errors()) {
                qCWarning(LOGGING_CATEGORY) << error;
            }
            delete created;
            continue;
        }

        connect(createdItem, &InitialStartModule::availableChanged, this, &Wizard::determineAvailableModuleItems);
        m_moduleItems.push_back(createdItem);

        qCDebug(LOGGING_CATEGORY) << "Loaded initialstart module" << pluginMetadata->pluginId();
    }

    delete c;

    // Populate model
    determineAvailableModuleItems();
}

void Wizard::setTestingMode(bool testingMode)
{
    if (testingMode != m_testingMode) {
        m_testingMode = testingMode;
        Q_EMIT testingModeChanged();
    }
}

bool Wizard::testingMode()
{
    return m_testingMode;
}

QList<InitialStartModule *> Wizard::steps()
{
    return m_availableModuleItems;
}

int Wizard::stepsCount()
{
    return m_availableModuleItems.size();
}

void Wizard::wizardFinished()
{
    Settings::self()->setWizardFinished();
    QCoreApplication::quit();
}

void Wizard::determineAvailableModuleItems()
{
    m_availableModuleItems.clear();
    for (auto *moduleItem : m_moduleItems) {
        if (moduleItem->available()) {
            m_availableModuleItems.push_back(moduleItem);
        }
    }

    Q_EMIT stepsChanged();
}