~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/plasmagenericshell/scripting/applet.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2009 Aaron Seigo <aseigo@kde.org>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License as
 
6
 *   published by the Free Software Foundation; either version 2, or
 
7
 *   (at your option) any later version.
 
8
 *
 
9
 *   This program is distributed in the hope that it will be useful,
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *   GNU General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "applet.h"
 
21
 
 
22
#include <QAction>
 
23
#include <QGraphicsLinearLayout>
 
24
 
 
25
#include <KService>
 
26
#include <KServiceTypeTrader>
 
27
 
 
28
#include <Plasma/Applet>
 
29
#include <Plasma/Containment>
 
30
#include <Plasma/Corona>
 
31
 
 
32
namespace WorkspaceScripting
 
33
{
 
34
 
 
35
class Applet::Private
 
36
{
 
37
public:
 
38
    Private()
 
39
        : configDirty(false),
 
40
          inWallpaperConfig(false),
 
41
          wallpaperConfigDirty(false)
 
42
    {
 
43
    }
 
44
 
 
45
    KConfigGroup configGroup;
 
46
    QStringList configGroupPath;
 
47
    KConfigGroup globalConfigGroup;
 
48
    QStringList globalConfigGroupPath;
 
49
    bool configDirty : 1;
 
50
    bool inWallpaperConfig : 1;
 
51
    bool wallpaperConfigDirty : 1;
 
52
};
 
53
 
 
54
Applet::Applet(QObject *parent)
 
55
    : QObject(parent),
 
56
      d(new Applet::Private)
 
57
{
 
58
}
 
59
 
 
60
Applet::~Applet()
 
61
{
 
62
    delete d;
 
63
}
 
64
 
 
65
void Applet::setCurrentConfigGroup(const QStringList &groupNames)
 
66
{
 
67
    Plasma::Applet *app = applet();
 
68
    if (!app) {
 
69
        d->configGroup = KConfigGroup();
 
70
        d->configGroupPath.clear();
 
71
        return;
 
72
    }
 
73
 
 
74
    d->configGroup = app->config();
 
75
    d->configGroupPath = groupNames;
 
76
 
 
77
    foreach (const QString &groupName, groupNames) {
 
78
        d->configGroup = KConfigGroup(&d->configGroup, groupName);
 
79
    }
 
80
 
 
81
    d->inWallpaperConfig = !groupNames.isEmpty() && groupNames.first() == "Wallpaper";
 
82
}
 
83
 
 
84
QStringList Applet::currentConfigGroup() const
 
85
{
 
86
    return d->configGroupPath;
 
87
}
 
88
 
 
89
QStringList Applet::configKeys() const
 
90
{
 
91
    if (d->configGroup.isValid()) {
 
92
        return d->configGroup.keyList();
 
93
    }
 
94
 
 
95
    return QStringList();
 
96
}
 
97
 
 
98
QStringList Applet::configGroups() const
 
99
{
 
100
    if (d->configGroup.isValid()) {
 
101
        return d->configGroup.groupList();
 
102
    }
 
103
 
 
104
    return QStringList();
 
105
}
 
106
 
 
107
QVariant Applet::readConfig(const QString &key, const QVariant &def) const
 
108
{
 
109
    if (d->configGroup.isValid()) {
 
110
        return d->configGroup.readEntry(key, def);
 
111
    } else {
 
112
        return QVariant();
 
113
    }
 
114
}
 
115
 
 
116
void Applet::writeConfig(const QString &key, const QVariant &value)
 
117
{
 
118
    if (d->configGroup.isValid()) {
 
119
        if (d->inWallpaperConfig) {
 
120
            d->wallpaperConfigDirty = true;
 
121
        }
 
122
 
 
123
        d->configGroup.writeEntry(key, value);
 
124
        d->configDirty = true;
 
125
    }
 
126
}
 
127
 
 
128
void Applet::setCurrentGlobalConfigGroup(const QStringList &groupNames)
 
129
{
 
130
    Plasma::Applet *app = applet();
 
131
    if (!app) {
 
132
        d->globalConfigGroup = KConfigGroup();
 
133
        d->globalConfigGroupPath.clear();
 
134
        return;
 
135
    }
 
136
 
 
137
    d->globalConfigGroup = app->globalConfig();
 
138
    d->globalConfigGroupPath = groupNames;
 
139
 
 
140
    foreach (const QString &groupName, groupNames) {
 
141
        d->globalConfigGroup = KConfigGroup(&d->globalConfigGroup, groupName);
 
142
    }
 
143
}
 
144
 
 
145
QStringList Applet::currentGlobalConfigGroup() const
 
146
{
 
147
    return d->globalConfigGroupPath;
 
148
}
 
149
 
 
150
QStringList Applet::globalConfigKeys() const
 
151
{
 
152
    if (d->globalConfigGroup.isValid()) {
 
153
        return d->globalConfigGroup.keyList();
 
154
    }
 
155
 
 
156
    return QStringList();
 
157
}
 
158
 
 
159
QStringList Applet::globalConfigGroups() const
 
160
{
 
161
    if (d->globalConfigGroup.isValid()) {
 
162
        return d->globalConfigGroup.groupList();
 
163
    }
 
164
 
 
165
    return QStringList();
 
166
}
 
167
 
 
168
QVariant Applet::readGlobalConfig(const QString &key, const QVariant &def) const
 
169
{
 
170
    if (d->globalConfigGroup.isValid()) {
 
171
        return d->globalConfigGroup.readEntry(key, def);
 
172
    } else {
 
173
        return QVariant();
 
174
    }
 
175
}
 
176
 
 
177
void Applet::writeGlobalConfig(const QString &key, const QVariant &value)
 
178
{
 
179
    if (d->globalConfigGroup.isValid()) {
 
180
        d->globalConfigGroup.writeEntry(key, value);
 
181
        d->configDirty = true;
 
182
    }
 
183
}
 
184
 
 
185
void Applet::reloadConfigIfNeeded()
 
186
{
 
187
    if (d->configDirty) {
 
188
        reloadConfig();
 
189
    }
 
190
}
 
191
 
 
192
void Applet::reloadConfig()
 
193
{
 
194
    Plasma::Applet *app = applet();
 
195
    if (app) {
 
196
        KConfigGroup cg = app->config();
 
197
 
 
198
        if (!app->isContainment()) {
 
199
            app->restore(cg);
 
200
        }
 
201
 
 
202
        app->configChanged();
 
203
 
 
204
        if (app->containment() && app->containment()->corona()) {
 
205
            app->containment()->corona()->requestConfigSync();
 
206
        }
 
207
 
 
208
        d->configDirty = false;
 
209
    }
 
210
}
 
211
 
 
212
QString Applet::version() const
 
213
{
 
214
    Plasma::Applet *app = applet();
 
215
    if (!app) {
 
216
        return QString();
 
217
    }
 
218
 
 
219
    QString type = app->pluginName();
 
220
    KService::List services = KServiceTypeTrader::self()->query("Plasma/Applet", "[X-KDE-PluginInfo-Name] == '" + type + "'");
 
221
    if (services.isEmpty()) {
 
222
        return QString();
 
223
    }
 
224
 
 
225
    KPluginInfo info(services.first());
 
226
    return info.version();
 
227
}
 
228
 
 
229
void Applet::setLocked(bool locked)
 
230
{
 
231
    Plasma::Applet *app = applet();
 
232
    if (!app) {
 
233
        return;
 
234
    }
 
235
 
 
236
    app->setImmutability(locked ? Plasma::UserImmutable : Plasma::Mutable);
 
237
}
 
238
 
 
239
bool Applet::locked() const
 
240
{
 
241
    Plasma::Applet *app = applet();
 
242
    if (!app) {
 
243
        return Plasma::Mutable;
 
244
    }
 
245
 
 
246
    return app->immutability() != Plasma::Mutable;
 
247
}
 
248
 
 
249
bool Applet::wallpaperConfigDirty() const
 
250
{
 
251
    return d->wallpaperConfigDirty;
 
252
}
 
253
 
 
254
Plasma::Applet *Applet::applet() const
 
255
{
 
256
    return 0;
 
257
}
 
258
 
 
259
}
 
260
 
 
261
#include "applet.moc"
 
262