kcm.cpp 5.63 KB
Newer Older
1
2
3
/* This file is part of the KDE Project
   Copyright (c) 2014 Marco Martin <mart@kde.org>
   Copyright (c) 2014 Vishesh Handa <me@vhanda.in>
4
   Copyright (c) 2019 Cyril Rossi <cyril.rossi@enioka.com>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kcm.h"

#include <KPluginFactory>
#include <KPluginLoader>
#include <KAboutData>
26
27
#include <KLocalizedString>

28
29
30
31
32
#include <QStandardPaths>
#include <QProcess>
#include <QStandardItemModel>
#include <QDir>

33
#include <KPackage/PackageLoader>
34

35
36
#include <KNewStuff3/KNS3/DownloadDialog>

37
38
#include "splashscreensettings.h"

39
K_PLUGIN_FACTORY_WITH_JSON(KCMSplashScreenFactory, "kcm_splashscreen.json", registerPlugin<KCMSplashScreen>();)
40

41
KCMSplashScreen::KCMSplashScreen(QObject* parent, const QVariantList& args)
42
    : KQuickAddons::ManagedConfigModule(parent, args)
43
44
    , m_settings(new SplashScreenSettings(this))
    , m_model(new QStandardItemModel(this))
45
{
46
    qmlRegisterType<SplashScreenSettings>();
47
    qmlRegisterType<QStandardItemModel>();
48

49
    KAboutData* about = new KAboutData(QStringLiteral("kcm_splashscreen"), i18n("Splash Screen"),
50
51
                                       QStringLiteral("0.1"), QString(), KAboutLicense::LGPL);
    about->addAuthor(i18n("Marco Martin"), QString(), QStringLiteral("mart@kde.org"));
52
53
54
55
56
    setAboutData(about);
    setButtons(Help | Apply | Default);

    QHash<int, QByteArray> roles = m_model->roleNames();
    roles[PluginNameRole] = "pluginName";
57
    roles[ScreenshotRole] = "screenshot";
58
    roles[DescriptionRole] = "description";
59
    m_model->setItemRoleNames(roles);
60
    loadModel();
61
62
}

63
QList<KPackage::Package> KCMSplashScreen::availablePackages(const QString &component)
64
{
65
    QList<KPackage::Package> packages;
66
    QStringList paths;
67
    const QStringList dataPaths = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
68
69

    for (const QString &path : dataPaths) {
70
        QDir dir(path + QStringLiteral("/plasma/look-and-feel"));
71
72
73
74
        paths << dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
    }

    for (const QString &path : paths) {
75
        KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LookAndFeel"));
76
        pkg.setPath(path);
77
        pkg.setFallbackPackage(KPackage::Package());
78
79
80
81
82
83
84
85
        if (component.isEmpty() || !pkg.filePath(component.toUtf8()).isEmpty()) {
            packages << pkg;
        }
    }

    return packages;
}

86
SplashScreenSettings *KCMSplashScreen::splashScreenSettings() const
87
{
88
    return m_settings;
89
90
}

91
QStandardItemModel *KCMSplashScreen::splashModel() const
92
{
93
    return m_model;
94
95
}

96
97
void KCMSplashScreen::getNewClicked()
{
98
    KNS3::DownloadDialog dialog("ksplash.knsrc", nullptr);
99
100
    if (dialog.exec()) {
        KNS3::Entry::List list = dialog.changedEntries();
Laurent Montel's avatar
Laurent Montel committed
101
        if (!list.isEmpty()) {
102
            loadModel();
103
104
105
106
        }
    }
}

107
void KCMSplashScreen::loadModel()
108
109
110
{
    m_model->clear();

111
112
    const QList<KPackage::Package> pkgs = availablePackages(QStringLiteral("splashmainscript"));
    for (const KPackage::Package &pkg : pkgs) {
113
        QStandardItem* row = new QStandardItem(pkg.metadata().name());
114
        row->setData(pkg.metadata().pluginId(), PluginNameRole);
115
        row->setData(pkg.filePath("previews", QStringLiteral("splash.png")), ScreenshotRole);
116
        row->setData(pkg.metadata().description(), DescriptionRole);
117
118
        m_model->appendRow(row);
    }
119
    m_model->sort(0 /*column*/);
120
121
122
123
124
125

    QStandardItem* row = new QStandardItem(i18n("None"));
    row->setData("None", PluginNameRole);
    row->setData(i18n("No splash screen will be shown"), DescriptionRole);
    m_model->insertRow(0, row);

126
127
128
129
130
    if (-1 == pluginIndex(m_settings->theme())) {
        defaults();
    }

    emit m_settings->themeChanged();
131
132
}

133
134
void KCMSplashScreen::save()
{
135
    m_settings->setEngine(m_settings->theme() == QStringLiteral("None") ? QStringLiteral("none") : QStringLiteral("KSplashQML"));
136
    ManagedConfigModule::save();
137
138
}

139
int KCMSplashScreen::pluginIndex(const QString &pluginName) const
140
{
141
142
143
    const auto results = m_model->match(m_model->index(0, 0), PluginNameRole, pluginName);
    if (results.count() == 1) {
        return results.first().row();
144
    }
145

146
147
148
    return -1;
}

149
150
151
152
153
bool KCMSplashScreen::testing() const
{
    return m_testProcess;
}

154
155
void KCMSplashScreen::test(const QString &plugin)
{
156
    if (plugin.isEmpty() || plugin == QLatin1String("None") || m_testProcess) {
157
158
159
        return;
    }

160
161
    m_testProcess = new QProcess(this);
    connect(m_testProcess, &QProcess::errorOccurred, this, [this](QProcess::ProcessError error) {
162
        Q_UNUSED(error)
163
164
165
166
        emit testingFailed();
    });
    connect(m_testProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,
        [this](int exitCode, QProcess::ExitStatus exitStatus) {
167
168
        Q_UNUSED(exitCode)
        Q_UNUSED(exitStatus)
169
170
171
172
173
174
175
176

        m_testProcess->deleteLater();
        m_testProcess = nullptr;
        emit testingChanged();
    });

    emit testingChanged();
    m_testProcess->start(QStringLiteral("ksplashqml"), {plugin, QStringLiteral("--test")});
177
178
179
}

#include "kcm.moc"