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

« back to all changes in this revision

Viewing changes to kscreensaver/krandom_screensaver/random.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
//
 
3
// Screen savers for KDE
 
4
//
 
5
// Copyright (c)  Martin R. Jones 1999
 
6
//
 
7
// This is an extremely simple program that starts a random screensaver.
 
8
//
 
9
 
 
10
#include <config-workspace.h>
 
11
 
 
12
#include <stdlib.h>
 
13
#include <stdio.h>
 
14
#include <time.h>
 
15
#include <unistd.h>
 
16
 
 
17
#include <QLayout>
 
18
#include <QCheckBox>
 
19
#include <QWidget>
 
20
//Added by qt3to4:
 
21
#include <QGridLayout>
 
22
 
 
23
#include <kapplication.h>
 
24
#include <kstandarddirs.h>
 
25
#include <kglobal.h>
 
26
#include <klocale.h>
 
27
#include <kdesktopfile.h>
 
28
#include <krandomsequence.h>
 
29
#include <kdebug.h>
 
30
#include <kcmdlineargs.h>
 
31
#include <kdialog.h>
 
32
#include <kconfig.h>
 
33
#include <kservice.h>
 
34
#include <kdeversion.h>
 
35
#include <kmacroexpander.h>
 
36
#include <kshell.h>
 
37
#include "kscreensaver_vroot.h"
 
38
#include "random.h"
 
39
#include <QX11Info>
 
40
#include <QFrame>
 
41
#include <kservicetypetrader.h>
 
42
 
 
43
void usage(char *name)
 
44
{
 
45
        puts(i18n("Usage: %1 [-setup] [args]\n"
 
46
                                "Starts a random screen saver.\n"
 
47
                                "Any arguments (except -setup) are passed on to the screen saver.", name ).toLocal8Bit().data());
 
48
}
 
49
 
 
50
static const char appName[] = "random";
 
51
 
 
52
static const char description[] = I18N_NOOP("Start a random KDE screen saver");
 
53
 
 
54
static QString exeFromActionGroup(const QList<KServiceAction>& actions, const char* name)
 
55
{
 
56
    foreach(const KServiceAction& action, actions) {
 
57
        if (action.name() == name)
 
58
            return action.exec();
 
59
    }
 
60
    return QString();
 
61
}
 
62
 
 
63
//----------------------------------------------------------------------------
 
64
 
 
65
int main(int argc, char *argv[])
 
66
{
 
67
        KCmdLineArgs::init(argc, argv, appName, "kscreensaver", ki18n("Random screen saver"), 
 
68
                KDE_VERSION_STRING, ki18n(description));
 
69
 
 
70
 
 
71
        KCmdLineOptions options;
 
72
 
 
73
        options.add("setup", ki18n("Setup screen saver"));
 
74
 
 
75
        options.add("window-id wid", ki18n("Run in the specified XWindow"));
 
76
 
 
77
        options.add("root", ki18n("Run in the root XWindow"));
 
78
 
 
79
        KCmdLineArgs::addCmdLineOptions(options);
 
80
 
 
81
        KApplication app;
 
82
 
 
83
        Window windowId = 0;
 
84
 
 
85
        KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
 
86
 
 
87
        if (args->isSet("setup"))
 
88
        {
 
89
                KRandomSetup setup;
 
90
                setup.exec();
 
91
                exit(0);
 
92
        }
 
93
 
 
94
        if (args->isSet("window-id"))
 
95
        {
 
96
                windowId = args->getOption("window-id").toInt();
 
97
        }
 
98
 
 
99
        if (args->isSet("root"))
 
100
        {
 
101
                QX11Info info;
 
102
                windowId = RootWindow(QX11Info::display(), info.screen());
 
103
        }
 
104
        args->clear();
 
105
        const KService::List lst = KServiceTypeTrader::self()->query( "ScreenSaver");
 
106
        KService::List availableSavers;
 
107
 
 
108
        KConfig type("krandom.kssrc", KConfig::NoGlobals);
 
109
        const KConfigGroup configGroup = type.group("Settings");
 
110
        const bool opengl = configGroup.readEntry("OpenGL", false);
 
111
        const bool manipulatescreen = configGroup.readEntry("ManipulateScreen", false);
 
112
        // TODO replace this with TryExec=fortune in the desktop files
 
113
        const bool fortune = !KStandardDirs::findExe("fortune").isEmpty();
 
114
        foreach( const KService::Ptr& service, lst ) {
 
115
            //QString file = KStandardDirs::locate("services", service->entryPath());
 
116
            //kDebug() << "Looking at " << file;
 
117
            const QString saverType = service->property("X-KDE-Type").toString();
 
118
            foreach (const QString &type, saverType.split(QLatin1Char(';'))) {
 
119
                //kDebug() << "saverTypes is "<< type;
 
120
                if (type == QLatin1String("ManipulateScreen")) {
 
121
                    if (!manipulatescreen)
 
122
                        goto fail;
 
123
                } else if (type == QLatin1String("OpenGL")) {
 
124
                    if (!opengl)
 
125
                        goto fail;
 
126
                } else if (type == QLatin1String("Fortune")) {
 
127
                    if (!fortune)
 
128
                        goto fail;
 
129
                }
 
130
            }
 
131
            availableSavers.append(service);
 
132
          fail: ;
 
133
        }
 
134
 
 
135
        KRandomSequence rnd;
 
136
        const int indx = rnd.getLong(availableSavers.count());
 
137
        const KService::Ptr service = availableSavers.at(indx);
 
138
        const QList<KServiceAction> actions = service->actions();
 
139
 
 
140
        QString cmd;
 
141
        if (windowId)
 
142
            cmd = exeFromActionGroup(actions, "InWindow");
 
143
        if (cmd.isEmpty() && windowId == 0)
 
144
            cmd = exeFromActionGroup(actions, "Root");
 
145
        if (cmd.isEmpty())
 
146
            cmd = service->exec();
 
147
 
 
148
    QHash<QChar, QString> keyMap;
 
149
    keyMap.insert('w', QString::number(windowId));
 
150
    const QStringList words = KShell::splitArgs(KMacroExpander::expandMacrosShellQuote(cmd, keyMap));
 
151
    if (!words.isEmpty()) {
 
152
        QString exeFile = KStandardDirs::findExe(words.first());
 
153
        if (!exeFile.isEmpty()) {
 
154
            char **sargs = new char *[words.size() + 1];
 
155
            int i = 0;
 
156
            for (; i < words.size(); i++)
 
157
                sargs[i] = qstrdup(words[i].toLocal8Bit().constData());
 
158
            sargs[i] = 0;
 
159
 
 
160
            execv(exeFile.toLocal8Bit(), sargs);
 
161
        }
 
162
    }
 
163
 
 
164
        // If we end up here then we couldn't start a saver.
 
165
        // If we have been supplied a window id or root window then blank it.
 
166
        QX11Info info;
 
167
        Window win = windowId ? windowId : RootWindow(QX11Info::display(), info.screen());
 
168
        XSetWindowBackground(QX11Info::display(), win,
 
169
                        BlackPixel(QX11Info::display(), info.screen()));
 
170
        XClearWindow(QX11Info::display(), win);
 
171
}
 
172
 
 
173
 
 
174
KRandomSetup::KRandomSetup( QWidget *parent, const char *name )
 
175
        : KDialog( parent )
 
176
{
 
177
  setObjectName( name );
 
178
  setModal( true );
 
179
  setCaption( i18n( "Setup Random Screen Saver" ) );
 
180
  setButtons( Ok | Cancel );
 
181
 
 
182
        QFrame *main = new QFrame( this );
 
183
  setMainWidget( main );
 
184
        QGridLayout *grid = new QGridLayout(main );
 
185
        grid->setSpacing( spacingHint() );
 
186
 
 
187
        openGL = new QCheckBox( i18n("Use OpenGL screen savers"), main );
 
188
        grid->addWidget(openGL, 0, 0);
 
189
 
 
190
        manipulateScreen = new QCheckBox(i18n("Use screen savers that manipulate the screen"), main);
 
191
        grid->addWidget(manipulateScreen, 1, 0);
 
192
 
 
193
        setMinimumSize( sizeHint() );
 
194
 
 
195
        KConfig config("krandom.kssrc", KConfig::NoGlobals);
 
196
        const KConfigGroup configGroup = config.group("Settings");
 
197
        openGL->setChecked(configGroup.readEntry("OpenGL", true));
 
198
        manipulateScreen->setChecked(configGroup.readEntry("ManipulateScreen", true));
 
199
 
 
200
  connect( this, SIGNAL( okClicked() ), SLOT( slotOk() ) );
 
201
}
 
202
 
 
203
void KRandomSetup::slotOk()
 
204
{
 
205
    KConfig config("krandom.kssrc");
 
206
    KConfigGroup configGroup = config.group("Settings");
 
207
    configGroup.writeEntry("OpenGL", openGL->isChecked());
 
208
    configGroup.writeEntry("ManipulateScreen", manipulateScreen->isChecked());
 
209
 
 
210
    accept();
 
211
}
 
212
 
 
213
#include "random.moc"