Compile it.
[kdeartwork.git] / kscreensaver / kxsconfig / kxsrun.cpp
blob6f399fa17497731932343c495356ea1345558516
1 //-----------------------------------------------------------------------------
2 //
3 // KDE xscreensaver launcher
4 //
5 // Copyright (c) Martin R. Jones <mjones@kde.org> 1999
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public
9 // License as published by the Free Software Foundation;
10 // version 2 of the License.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; see the file COPYING. If not, write to
19 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21 #include <config-kxsconfig.h>
22 #include <config.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
28 #include <q3ptrlist.h>
29 #include <qfile.h>
30 #include <qfileinfo.h>
32 #include <kdebug.h>
33 #include <kapplication.h>
34 #include <kconfig.h>
35 #include <kstandarddirs.h>
36 #include <klocale.h>
37 #include <kcmdlineargs.h>
39 #include "kxsitem.h"
40 #include "kxsxml.h"
42 #define MAX_ARGS 30
44 template class Q3PtrList<KXSConfigItem>;
46 //===========================================================================
47 static const char appName[] = "kxsrun";
49 static const char description[] = I18N_NOOP("KDE X Screen Saver Launcher");
51 static const char version[] = "3.0.0";
53 static const KCmdLineOptions options[] =
55 {"+screensaver", I18N_NOOP("Filename of the screen saver to start"), 0},
56 {"+-- [options]", I18N_NOOP("Extra options to pass to the screen saver"), 0},
57 KCmdLineLastOption
60 int main(int argc, char *argv[])
62 KLocale::setMainCatalog("kxsconfig");
63 KCmdLineArgs::init(argc, argv, appName, I18N_NOOP("KXSRun"), description, version);
65 KCmdLineArgs::addCmdLineOptions(options);
67 KApplication app( false );
69 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
71 if ( !args->count() )
72 exit( 1 );
74 QString filename = args->arg(0);
75 QString configFile(filename);
77 // Get the config filename
78 int slash = filename.findRev('/');
79 if (slash >= 0)
80 configFile = filename.mid(slash+1);
82 QString exeName = configFile;
83 configFile += "rc";
85 // read configuration args
86 KConfig config(configFile);
88 Q3PtrList<KXSConfigItem> configItemList;
90 QString xmlFile = "/doesntexist";
91 #ifdef XSCREENSAVER_CONFIG_DIR
92 xmlFile = XSCREENSAVER_CONFIG_DIR;
93 #endif
94 xmlFile += "/" + exeName + ".xml";
95 if ( QFile::exists( xmlFile ) ) {
96 // We can use the xscreensaver xml config files.
97 KXSXml xmlParser(0);
98 xmlParser.parse(xmlFile);
99 configItemList = *xmlParser.items();
100 Q3PtrListIterator<KXSConfigItem> it( configItemList );
101 KXSConfigItem *item;
102 while ( (item = it.current()) != 0 ) {
103 ++it;
104 item->read( config );
106 } else {
107 // fall back to KDE's old config files.
108 int idx = 0;
109 while (true)
111 QString group = QString("Arg%1").arg(idx);
112 if (config.hasGroup(group)) {
113 config.setGroup(group);
114 QString type = config.readEntry("Type");
115 if (type == "Range") {
116 KXSRangeItem *rc = new KXSRangeItem(group, config);
117 configItemList.append(rc);
118 } else if (type == "DoubleRange") {
119 KXSDoubleRangeItem *rc = new KXSDoubleRangeItem(group, config);
120 configItemList.append(rc);
121 } else if (type == "Check") {
122 KXSBoolItem *cc = new KXSBoolItem(group, config);
123 configItemList.append(cc);
124 } else if (type == "DropList") {
125 KXSSelectItem *si = new KXSSelectItem(group, config);
126 configItemList.append(si);
128 } else {
129 break;
131 idx++;
135 // find the xscreensaver executable
136 //work around a KStandarDirs::findExe() "feature" where it looks in $KDEDIR/bin first no matter what and sometimes finds the wrong executable
137 QFileInfo checkExe;
138 QString saverdir = QString("%1/%2").arg(XSCREENSAVER_HACKS_DIR).arg(filename);
139 kDebug() << "saverdir is" << saverdir << endl;
140 QString exeFile;
141 checkExe.setFile(saverdir);
142 if (checkExe.exists() && checkExe.isExecutable() && checkExe.isFile())
144 exeFile = saverdir;
148 if (!exeFile.isEmpty()) {
149 char *sargs[MAX_ARGS];
150 sargs[0] = new char [strlen(filename.ascii())+1];
151 strcpy(sargs[0], filename.ascii());
153 // add the command line options
154 QString cmd;
155 unsigned int i;
156 for (i = 1; i < (unsigned)args->count(); i++)
157 cmd += " " + QString(args->arg(i));
159 // add the config options
160 KXSConfigItem *item;
161 for (item = configItemList.first(); item != 0; item = configItemList.next()) {
162 cmd += " " + item->command();
165 // put into char * array for execv
166 QString word;
167 int si = 1;
168 i = 0;
169 bool inQuotes = false;
170 while (i < cmd.length() && si < MAX_ARGS-1) {
171 word = "";
172 while ( cmd[i].isSpace() && i < cmd.length() ) i++;
173 while ( (!cmd[i].isSpace() || inQuotes) && i < cmd.length() ) {
174 if ( cmd[i] == '\"' ) {
175 inQuotes = !inQuotes;
176 } else {
177 word += cmd[i];
179 i++;
181 if (!word.isEmpty()) {
182 sargs[si] = new char [strlen(word.ascii())+1];
183 strcpy(sargs[si], word.ascii());
184 si++;
188 sargs[si] = 0;
190 // here goes...
191 execv(exeFile.ascii(), sargs);