Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / workspace / kscreensaver / krandom_screensaver / random.cpp
blob998414e5dcb7dfc18f39bd9477abbee5546804a9
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 //
10 #include <config-workspace.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <time.h>
15 #include <unistd.h>
17 #include <QTextStream>
18 #include <QLayout>
19 #include <QCheckBox>
20 #include <QWidget>
21 //Added by qt3to4:
22 #include <QGridLayout>
24 #include <kapplication.h>
25 #include <kstandarddirs.h>
26 #include <kglobal.h>
27 #include <klocale.h>
28 #include <kdesktopfile.h>
29 #include <krandomsequence.h>
30 #include <kdebug.h>
31 #include <kcmdlineargs.h>
32 #include <kdialog.h>
33 #include <kconfig.h>
34 #include <kservice.h>
35 #include "kscreensaver_vroot.h"
36 #include "random.h"
37 #include <QX11Info>
38 #include <QFrame>
39 #include <kservicetypetrader.h>
40 #define MAX_ARGS 20
42 void usage(char *name)
44 puts(i18n("Usage: %1 [-setup] [args]\n"
45 "Starts a random screen saver.\n"
46 "Any arguments (except -setup) are passed on to the screen saver.", name ).toLocal8Bit().data());
49 static const char appName[] = "random";
51 static const char description[] = I18N_NOOP("Start a random KDE screen saver");
53 static const char version[] = "2.0.0";
55 static QString exeFromActionGroup(const QList<KServiceAction>& actions, const char* name)
57 foreach(const KServiceAction& action, actions) {
58 if (action.name() == name)
59 return action.exec();
61 return QString();
64 //----------------------------------------------------------------------------
66 int main(int argc, char *argv[])
68 KCmdLineArgs::init(argc, argv, appName, "kscreensaver", ki18n("Random screen saver"), version, ki18n(description));
71 KCmdLineOptions options;
73 options.add("setup", ki18n("Setup screen saver"));
75 options.add("window-id wid", ki18n("Run in the specified XWindow"));
77 options.add("root", ki18n("Run in the root XWindow"));
79 KCmdLineArgs::addCmdLineOptions(options);
81 KApplication app;
83 Window windowId = 0;
85 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
87 if (args->isSet("setup"))
89 KRandomSetup setup;
90 setup.exec();
91 exit(0);
94 if (args->isSet("window-id"))
96 windowId = args->getOption("window-id").toInt();
99 if (args->isSet("root"))
101 QX11Info info;
102 windowId = RootWindow(QX11Info::display(), info.screen());
104 args->clear();
105 const KService::List lst = KServiceTypeTrader::self()->query( "ScreenSaver");
106 KService::List availableSavers;
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 if (saverType.isEmpty()) { // no X-KDE-Type defined so must be OK
119 availableSavers.append(service);
120 } else {
121 const QStringList saverTypes = saverType.split( ";");
122 for (QStringList::ConstIterator it = saverTypes.begin(); it != saverTypes.end(); ++it ) {
123 kDebug() << "saverTypes is "<< *it;
124 if (*it == "ManipulateScreen") {
125 if (manipulatescreen) {
126 availableSavers.append(service);
128 } else if (*it == "OpenGL") {
129 if (opengl) {
130 availableSavers.append(service);
132 } else if (*it == "Fortune") {
133 if (fortune) {
134 availableSavers.append(service);
141 KRandomSequence rnd;
142 const int indx = rnd.getLong(availableSavers.count());
143 const KService::Ptr service = availableSavers.at(indx);
144 const QList<KServiceAction> actions = service->actions();
146 QString cmd;
147 if (windowId)
148 cmd = exeFromActionGroup(actions, "InWindow");
149 if (cmd.isEmpty() && windowId == 0)
150 cmd = exeFromActionGroup(actions, "Root");
151 if (cmd.isEmpty())
152 cmd = service->exec();
154 QTextStream ts(&cmd, QIODevice::ReadOnly);
155 QString word;
156 ts >> word;
157 QString exeFile = KStandardDirs::findExe(word);
159 if (!exeFile.isEmpty())
161 char *sargs[MAX_ARGS];
162 sargs[0] = new char [strlen(word.toAscii())+1];
163 strcpy(sargs[0], word.toAscii());
165 int i = 1;
166 while (!ts.atEnd() && i < MAX_ARGS-1)
168 ts >> word;
169 if (word == "%w")
171 word = word.setNum(windowId);
174 sargs[i] = new char [strlen(word.toAscii())+1];
175 strcpy(sargs[i], word.toAscii());
176 kDebug() << "word is " << word.toAscii();
178 i++;
181 sargs[i] = 0;
183 execv(exeFile.toAscii(), sargs);
186 // If we end up here then we couldn't start a saver.
187 // If we have been supplied a window id or root window then blank it.
188 QX11Info info;
189 Window win = windowId ? windowId : RootWindow(QX11Info::display(), info.screen());
190 XSetWindowBackground(QX11Info::display(), win,
191 BlackPixel(QX11Info::display(), info.screen()));
192 XClearWindow(QX11Info::display(), win);
196 KRandomSetup::KRandomSetup( QWidget *parent, const char *name )
197 : KDialog( parent )
199 setObjectName( name );
200 setModal( true );
201 setCaption( i18n( "Setup Random Screen Saver" ) );
202 setButtons( Ok | Cancel );
203 showButtonSeparator( true );
205 QFrame *main = new QFrame( this );
206 setMainWidget( main );
207 QGridLayout *grid = new QGridLayout(main );
208 grid->setSpacing( spacingHint() );
210 openGL = new QCheckBox( i18n("Use OpenGL screen savers"), main );
211 grid->addWidget(openGL, 0, 0);
213 manipulateScreen = new QCheckBox(i18n("Use screen savers that manipulate the screen"), main);
214 grid->addWidget(manipulateScreen, 1, 0);
216 setMinimumSize( sizeHint() );
218 KConfig config("krandom.kssrc", KConfig::NoGlobals);
219 const KConfigGroup configGroup = config.group("Settings");
220 openGL->setChecked(configGroup.readEntry("OpenGL", true));
221 manipulateScreen->setChecked(configGroup.readEntry("ManipulateScreen", true));
223 connect( this, SIGNAL( okClicked() ), SLOT( slotOk() ) );
226 void KRandomSetup::slotOk()
228 KConfig config("krandom.kssrc");
229 KConfigGroup configGroup = config.group("Settings");
230 configGroup.writeEntry("OpenGL", openGL->isChecked());
231 configGroup.writeEntry("ManipulateScreen", manipulateScreen->isChecked());
233 accept();
236 #include "random.moc"