containmentconfigview.cpp 8.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 *   Copyright 2013 Marco Martin <mart@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "currentcontainmentactionsmodel.h"
#include "containmentconfigview.h"
22
#include "shellcorona.h"
23
#include "config-workspace.h"
24

Nicolas Fella's avatar
Nicolas Fella committed
25
#include <QDBusConnection>
26
27
28
29
30
#include <QDebug>
#include <QDir>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQmlComponent>
Nicolas Fella's avatar
Nicolas Fella committed
31
#include <QStandardPaths>
32

Nicolas Fella's avatar
Nicolas Fella committed
33
34
35
36
37
38
#include <KConfigLoader>
#include <KDeclarative/KDeclarative>
#include <KDeclarative/ConfigPropertyMap>
#include <KLocalizedString>
#include <KPackage/Package>
#include <KPackage/PackageLoader>
39
40
#include <Plasma/Corona>
#include <Plasma/PluginLoader>
Marco Martin's avatar
Marco Martin committed
41
#include <Plasma/ContainmentActions>
Nicolas Fella's avatar
Nicolas Fella committed
42
#include <PlasmaQuick/ConfigModel>
Marco Martin's avatar
Marco Martin committed
43

44
45
46
47
48
49
50
51
52
53
class WallpaperConfigModel: public PlasmaQuick::ConfigModel
{
    Q_OBJECT
public:
    WallpaperConfigModel(QObject *parent);
public Q_SLOTS:
    void repopulate();
};


54
55
56
//////////////////////////////ContainmentConfigView
ContainmentConfigView::ContainmentConfigView(Plasma::Containment *cont, QWindow *parent)
    : ConfigView(cont, parent),
57
      m_containment(cont)
58
59
{
    qmlRegisterType<QAbstractItemModel>();
60
    rootContext()->setContextProperty(QStringLiteral("configDialog"), this);
61
62
    setCurrentWallpaper(cont->containment()->wallpaper());

63
    KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Wallpaper"));
64
65
66
67
68
69
70
71
72
73
74
75
76
    pkg.setPath(m_containment->wallpaper());
    KConfigGroup cfg = m_containment->config();
    cfg = KConfigGroup(&cfg, "Wallpaper");

    syncWallpaperObjects();
}

ContainmentConfigView::~ContainmentConfigView()
{
}

void ContainmentConfigView::init()
{
77
    setSource(m_containment->corona()->kPackage().fileUrl("containmentconfigurationui"));
78
79
}

Marco Martin's avatar
Marco Martin committed
80
PlasmaQuick::ConfigModel *ContainmentConfigView::containmentActionConfigModel()
81
82
{
    if (!m_containmentActionConfigModel) {
Marco Martin's avatar
Marco Martin committed
83
        m_containmentActionConfigModel = new PlasmaQuick::ConfigModel(this);
84

Nicolas Fella's avatar
Nicolas Fella committed
85
        const KPluginInfo::List actions = Plasma::PluginLoader::self()->listContainmentActionsInfo(QString());
86

87
        KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Generic"));
88

Nicolas Fella's avatar
Nicolas Fella committed
89
        for (const KPluginInfo &info : actions) {
Laurent Montel's avatar
Laurent Montel committed
90
            pkg.setDefaultPackageRoot(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral(PLASMA_RELATIVE_DATA_INSTALL_DIR "/containmentactions"), QStandardPaths::LocateDirectory));
91
            m_containmentActionConfigModel->appendCategory(info.icon(), info.name(), pkg.filePath("ui", QStringLiteral("config.qml")), info.pluginName());
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        }

    }
    return m_containmentActionConfigModel;
}

QAbstractItemModel *ContainmentConfigView::currentContainmentActionsModel()
{
    if (!m_currentContainmentActionsModel) {
        m_currentContainmentActionsModel = new CurrentContainmentActionsModel(m_containment, this);
    }
    return m_currentContainmentActionsModel;
}

106
107
QString ContainmentConfigView::containmentPlugin() const
{
108
    return m_containment->pluginMetaData().pluginId();
109
110
111
112
113
114
115
116
117
118
119
120
}

void ContainmentConfigView::setContainmentPlugin(const QString &plugin)
{
    if (plugin.isEmpty() || containmentPlugin() == plugin) {
        return;
    }

    m_containment = static_cast<ShellCorona *>(m_containment->corona())->setContainmentTypeForScreen(m_containment->screen(), plugin);
    emit containmentPluginChanged();
}

Marco Martin's avatar
Marco Martin committed
121
PlasmaQuick::ConfigModel *ContainmentConfigView::wallpaperConfigModel()
122
123
{
    if (!m_wallpaperConfigModel) {
124
125
        m_wallpaperConfigModel = new WallpaperConfigModel(this);
        QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/KPackage/Plasma/Wallpaper"), QStringLiteral("org.kde.plasma.kpackage"), QStringLiteral("packageInstalled"),
126
127
128
            m_wallpaperConfigModel, SLOT(repopulate()));
        QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/KPackage/Plasma/Wallpaper"), QStringLiteral("org.kde.plasma.kpackage"), QStringLiteral("packageUpdated"),
            m_wallpaperConfigModel, SLOT(repopulate()));
129
130
        QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/KPackage/Plasma/Wallpaper"), QStringLiteral("org.kde.plasma.kpackage"), QStringLiteral("packageUninstalled"),
            m_wallpaperConfigModel, SLOT(repopulate()));
131
132
133
134
    }
    return m_wallpaperConfigModel;
}

135
136
137
138
139
PlasmaQuick::ConfigModel *ContainmentConfigView::containmentPluginsConfigModel()
{
    if (!m_containmentPluginsConfigModel) {
        m_containmentPluginsConfigModel = new PlasmaQuick::ConfigModel(this);

140
        KPluginInfo::List actions = Plasma::PluginLoader::self()->listContainmentsOfType(QStringLiteral("Desktop"));
141
142
143
144
145
146
147
148
149

        foreach (const KPluginInfo &info, actions) {
            m_containmentPluginsConfigModel->appendCategory(info.icon(), info.name(), QString(), info.pluginName());
        }

    }
    return m_containmentPluginsConfigModel;
}

150
KDeclarative::ConfigPropertyMap *ContainmentConfigView::wallpaperConfiguration() const
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{
    return m_currentWallpaperConfig;
}

QString ContainmentConfigView::currentWallpaper() const
{
    return m_currentWallpaper;
}

void ContainmentConfigView::setCurrentWallpaper(const QString &wallpaper)
{
    if (m_currentWallpaper == wallpaper) {
        return;
    }

    delete m_ownWallpaperConfig;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
167
    m_ownWallpaperConfig = nullptr;
168
169
170
171
172
173

    if (m_containment->wallpaper() == wallpaper) {
        syncWallpaperObjects();
    } else {

        //we have to construct an independent ConfigPropertyMap when we want to configure wallpapers that are not the current one
174
        KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Generic"));
Laurent Montel's avatar
Laurent Montel committed
175
        pkg.setDefaultPackageRoot(QStringLiteral(PLASMA_RELATIVE_DATA_INSTALL_DIR "/wallpapers"));
176
        pkg.setPath(wallpaper);
177
        QFile file(pkg.filePath("config", QStringLiteral("main.xml")));
178
179
        KConfigGroup cfg = m_containment->config();
        cfg = KConfigGroup(&cfg, "Wallpaper");
180
        cfg = KConfigGroup(&cfg, wallpaper);
Marco Martin's avatar
Marco Martin committed
181
        m_currentWallpaperConfig = m_ownWallpaperConfig = new KDeclarative::ConfigPropertyMap(new KConfigLoader(cfg, &file), this);
182
183
184
185
186
187
188
189
190
191
192
    }

    m_currentWallpaper = wallpaper;
    emit currentWallpaperChanged();
    emit wallpaperConfigurationChanged();
}

void ContainmentConfigView::applyWallpaper()
{
    m_containment->setWallpaper(m_currentWallpaper);

Marco Martin's avatar
Marco Martin committed
193
194
195
    syncWallpaperObjects();

    if (m_currentWallpaperConfig && m_ownWallpaperConfig) {
196
        for (const auto &key : m_ownWallpaperConfig->keys()) {
197
198
199
            auto value = m_ownWallpaperConfig->value(key);
            m_currentWallpaperConfig->insert(key, value);
            m_currentWallpaperConfig->valueChanged(key, value);
Marco Martin's avatar
Marco Martin committed
200
201
202
        }
    }

203
    delete m_ownWallpaperConfig;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
204
    m_ownWallpaperConfig = nullptr;
205
206
207
208
209
210
211

    emit wallpaperConfigurationChanged();
}

void ContainmentConfigView::syncWallpaperObjects()
{
    QObject *wallpaperGraphicsObject = m_containment->property("wallpaperGraphicsObject").value<QObject *>();
212
213
214
215

    if (!wallpaperGraphicsObject) {
        return;
    }
216
    rootContext()->setContextProperty(QStringLiteral("wallpaper"), wallpaperGraphicsObject);
217
218

    //FIXME: why m_wallpaperGraphicsObject->property("configuration").value<ConfigPropertyMap *>() doesn't work?
219
    m_currentWallpaperConfig = static_cast<KDeclarative::ConfigPropertyMap *>(wallpaperGraphicsObject->property("configuration").value<QObject *>());
220
221
}

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
WallpaperConfigModel::WallpaperConfigModel(QObject *parent)
    :PlasmaQuick::ConfigModel(parent)
{
    repopulate();
}

void WallpaperConfigModel::repopulate()
{
    clear();
    for (const KPluginMetaData &m : KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/Wallpaper"))) {
        KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Wallpaper"), m.pluginId());
        if (!pkg.isValid()) {
            continue;
        }
        appendCategory(pkg.metadata().iconName(), pkg.metadata().name(), pkg.fileUrl("ui", QStringLiteral("config.qml")).toString(), m.pluginId());
    }
}

#include "containmentconfigview.moc"