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

« back to all changes in this revision

Viewing changes to plasma/generic/tools/plasmoidviewer/main.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 2007 Frerich Raabe <raabe@kde.org>
 
3
 * Copyright 2007-2008 Aaron Seigo <aseigo@kde.org>
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer.
 
11
 * 2. Redistributions in binary form must reproduce the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer in the
 
13
 *    documentation and/or other materials provided with the distribution.
 
14
 *
 
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 
16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
17
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
18
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
19
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
20
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
21
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
22
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
25
 */
 
26
 
 
27
#include "fullview.h"
 
28
 
 
29
#include <iostream>
 
30
 
 
31
#include <QPixmapCache>
 
32
 
 
33
#include <KApplication>
 
34
#include <KAboutData>
 
35
#include <KAction>
 
36
#include <KCmdLineArgs>
 
37
#include <KLocale>
 
38
#include <KStandardAction>
 
39
 
 
40
// for --list
 
41
#include <Plasma/Applet>
 
42
#include <Plasma/Theme>
 
43
#include <Plasma/AccessManager>
 
44
#include <Plasma/AuthorizationManager>
 
45
#include <Plasma/Wallpaper>
 
46
 
 
47
using namespace Plasma;
 
48
 
 
49
static const char description[] = I18N_NOOP("Run Plasma widgets in their own window");
 
50
 
 
51
class RemotePlasmoidWatcher : public QObject
 
52
{
 
53
Q_OBJECT
 
54
 
 
55
public:
 
56
    RemotePlasmoidWatcher(AccessManager *manager)
 
57
        : QObject(manager)
 
58
    {
 
59
        kDebug();
 
60
        connect(manager, SIGNAL(remoteAppletAnnounced(Plasma::PackageMetadata)),
 
61
                this, SLOT(slotServiceAdded(Plasma::PackageMetadata)));
 
62
        connect(manager, SIGNAL(remoteAppletUnannounced(Plasma::PackageMetadata)),
 
63
                this, SLOT(slotServiceRemoved(Plasma::PackageMetadata)));
 
64
    }
 
65
 
 
66
    ~RemotePlasmoidWatcher() {}
 
67
 
 
68
public Q_SLOTS:
 
69
    void slotServiceAdded(Plasma::PackageMetadata metadata) {
 
70
        std::cout << "New service published:" << std::endl;
 
71
        std::cout << metadata.remoteLocation().prettyUrl().toLocal8Bit().data() << std::endl;
 
72
        std::cout << metadata.name().toLocal8Bit().data() << " - "
 
73
                  << metadata.description().toLocal8Bit().data() << std::endl;
 
74
    }
 
75
 
 
76
    void slotServiceRemoved(Plasma::PackageMetadata metadata) {
 
77
        std::cout << "Service removed:" << std::endl;
 
78
        std::cout << metadata.remoteLocation().prettyUrl().toLocal8Bit().data() << std::endl;
 
79
    }
 
80
};
 
81
 
 
82
void listPlugins(const KPluginInfo::List & plugins)
 
83
{
 
84
    int maxLen = 0;
 
85
    QMap<QString, QString> applets;
 
86
    foreach (const KPluginInfo &info, plugins) {
 
87
        if (info.property("NoDisplay").toBool()) {
 
88
            continue;
 
89
        }
 
90
 
 
91
        int len = info.pluginName().length();
 
92
        if (len > maxLen) {
 
93
            maxLen = len;
 
94
        }
 
95
 
 
96
        QString name = info.pluginName();
 
97
        QString comment = info.comment();
 
98
 
 
99
        if (comment.isEmpty()) {
 
100
            comment = i18n("No description available");
 
101
        }
 
102
 
 
103
        applets.insert(name, comment);
 
104
    }
 
105
 
 
106
    QMap<QString, QString>::const_iterator it;
 
107
    for (it = applets.constBegin(); it != applets.constEnd(); it++) {
 
108
        QString applet("%1 - %2");
 
109
 
 
110
        applet = applet.arg(it.key().leftJustified(maxLen, ' ')).arg(it.value());
 
111
        std::cout << applet.toLocal8Bit().data() << std::endl;
 
112
    }
 
113
}
 
114
 
 
115
int main(int argc, char **argv)
 
116
{
 
117
    KAboutData aboutData("plasmoidviewer", 0, ki18n("Plasma Widget Viewer"),
 
118
                         "1.0", ki18n(description), KAboutData::License_BSD,
 
119
                         ki18n("2007-2008, Frerich Raabe"));
 
120
    aboutData.setProgramIconName("plasma");
 
121
    aboutData.addAuthor(ki18n("Frerich Raabe"),
 
122
                         ki18n("Original author"),
 
123
                        "raabe@kde.org");
 
124
 
 
125
    KCmdLineArgs::init(argc, argv, &aboutData);
 
126
 
 
127
    KCmdLineOptions options;
 
128
    options.add("c");
 
129
    options.add("containment <name>", ki18n("Name of the containment plugin"), "null");
 
130
    options.add("f");
 
131
    options.add("formfactor <name>", ki18nc("Do not translate horizontal, vertical, mediacenter nor planar", "The formfactor to use (horizontal, vertical, mediacenter or planar)"), "planar");
 
132
    options.add("list", ki18n("Displays a list of known applets"));
 
133
    options.add("list-wallpapers", ki18n("Displays a list of known wallpapers"));
 
134
    options.add("list-containments", ki18n("Displays a list of known containments"));
 
135
    options.add("l");
 
136
    options.add("location <name>", ki18nc("Do not translate floating, desktop, fullscreen, top, bottom, left nor right", "The location constraint to start the Containment with (floating, desktop, fullscreen, top, bottom, left, right)"), "floating");
 
137
    options.add("p");
 
138
    options.add("pixmapcache <size>", ki18n("The size in KB to set the pixmap cache to"));
 
139
    options.add("s");
 
140
    options.add("screenshot", ki18n("Takes a screenshot of the widget and saves it the working directory as <pluginname>.png"));
 
141
    options.add("screenshot-all", ki18n("Takes a screenshot of each widget and saves it the working directory as <pluginname>.png"));
 
142
    options.add("t");
 
143
    options.add("theme <name>", ki18n("Desktop SVG theme to use"));
 
144
    options.add("w");
 
145
    options.add("wallpaper <name>", ki18n("Name of the wallpaper plugin. Requires a containment plugin to be specified."), QByteArray());
 
146
    options.add("+applet", ki18n("Name of applet to view; may refer to the plugin name or be a path "
 
147
                                "(absolute or relative) to a package. If not provided, then an "
 
148
                                "attempt is made to load a package from the current directory."));
 
149
    options.add("+[args]", ki18n("Optional arguments of the applet to add"));
 
150
    options.add("list-remote", ki18n("List zeroconf announced remote widgets"));
 
151
    KCmdLineArgs::addCmdLineOptions(options);
 
152
 
 
153
    KApplication app;
 
154
 
 
155
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs() ;
 
156
 
 
157
    if (args->isSet("list")) {
 
158
        listPlugins(Plasma::Applet::listAppletInfo());
 
159
        return 0;
 
160
    }
 
161
 
 
162
    if (args->isSet("list-wallpapers")) {
 
163
        listPlugins(Plasma::Wallpaper::listWallpaperInfo());
 
164
        return 0;
 
165
    }
 
166
 
 
167
    if (args->isSet("list-containments")) {
 
168
        listPlugins(Plasma::Containment::listContainments());
 
169
        return 0;
 
170
    }
 
171
 
 
172
    QString pluginName;
 
173
    if (args->count() > 0) {
 
174
        pluginName = args->arg(0);
 
175
    }
 
176
 
 
177
    QString formfactor = args->getOption("formfactor");
 
178
    kDebug() << "setting FormFactor to" << args->getOption("formfactor");
 
179
 
 
180
    QString location = args->getOption("location");
 
181
    kDebug() << "setting Location to" << args->getOption("location");
 
182
 
 
183
    QString containment = args->getOption("containment");
 
184
    kDebug() << "setting containment to" << containment;
 
185
 
 
186
    if (args->isSet("theme")) {
 
187
        QString theme = args->getOption("theme");
 
188
        Plasma::Theme::defaultTheme()->setUseGlobalSettings(false);
 
189
        Plasma::Theme::defaultTheme()->setThemeName(theme);
 
190
        kDebug() << "setting theme to" << theme;
 
191
    }
 
192
 
 
193
    QString wallpaper;
 
194
    if (args->isSet("wallpaper")) {
 
195
        wallpaper = args->getOption("wallpaper");
 
196
        kDebug() << "setting wallpaper to" << wallpaper;
 
197
    }
 
198
 
 
199
    if (args->isSet("pixmapcache")) {
 
200
        kDebug() << "setting pixmap cache to" << args->getOption("pixmapcache").toInt();
 
201
        QPixmapCache::setCacheLimit(args->getOption("pixmapcache").toInt());
 
202
    }
 
203
 
 
204
    QVariantList appletArgs;
 
205
    for (int i = 1; i < args->count(); ++i) {
 
206
        appletArgs << args->arg(i);
 
207
    }
 
208
    kDebug() << "setting auth policy";
 
209
    Plasma::AuthorizationManager::self()->setAuthorizationPolicy(Plasma::AuthorizationManager::PinPairing);
 
210
 
 
211
    FullView view(formfactor, location);
 
212
 
 
213
    if (args->isSet("list-remote")) {
 
214
        kDebug() << "list remote...";
 
215
        /**
 
216
        QList<KUrl> list = AccessManager::self()->remotePlasmoids();
 
217
        foreach (const KUrl &url, list) {
 
218
            std::cout << url.prettyUrl().toLocal8Bit().data() << std::endl;
 
219
        }
 
220
        */
 
221
        new RemotePlasmoidWatcher(AccessManager::self());
 
222
    } else if (args->isSet("screenshot-all")) {
 
223
        view.show();
 
224
        view.screenshotAll();
 
225
    } else {
 
226
        kDebug() << "just load applet";
 
227
        view.addApplet(pluginName, containment, wallpaper, appletArgs);
 
228
        view.show();
 
229
    }
 
230
 
 
231
 
 
232
    QAction *action = KStandardAction::quit(&app, SLOT(quit()), &view);
 
233
    view.addAction(action);
 
234
 
 
235
    return app.exec();
 
236
}
 
237
 
 
238
#include "main.moc"
 
239