summaryrefslogtreecommitdiffstats
path: root/tdescreensaver/kxsconfig/kxsrun.cpp
blob: 8c144c67b0cd4347e1939aa1dedc7ae4520c0c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//-----------------------------------------------------------------------------
//
// TDE xscreensaver launcher
//
// Copyright (c)  Martin R. Jones <mjones@kde.org> 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation;
// version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <tqptrlist.h>
#include <tqfile.h>
#include <tqfileinfo.h>

#include <kdebug.h>
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kstandarddirs.h>
#include <tdelocale.h>
#include <tdecmdlineargs.h>

#include "kxsitem.h"
#include "kxsxml.h"

#define MAX_ARGS  30

template class TQPtrList<KXSConfigItem>;

//===========================================================================
static const char appName[] = "kxsrun";

static const char description[] = I18N_NOOP("TDE X Screen Saver Launcher");

static const char version[] = "3.0.0";

static const TDECmdLineOptions options[] =
{
   {"+screensaver", I18N_NOOP("Filename of the screen saver to start"), 0},
   {"+-- [options]", I18N_NOOP("Extra options to pass to the screen saver"), 0},
   TDECmdLineLastOption
};

int main(int argc, char *argv[])
{
    TDELocale::setMainCatalogue("kxsconfig");
    TDECmdLineArgs::init(argc, argv, appName, I18N_NOOP("KXSRun"), description, version);

    TDECmdLineArgs::addCmdLineOptions(options);

    TDEApplication app( false, false );

    TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

    if ( !args->count() )
	exit( 1 );

    TQString filename = args->arg(0);
    TQString configFile(filename);

    // Get the config filename
    int slash = filename.findRev('/');
    if (slash >= 0)
	configFile = filename.mid(slash+1);

    TQString exeName = configFile;
    configFile += "rc";

    // read configuration args
    TDEConfig config(configFile);

    TQPtrList<KXSConfigItem> configItemList;

    TQString xmlFile = "/doesntexist";
    xmlFile += "/" + exeName + ".xml";
#ifdef XSCREENSAVER_CONFIG_DIRS
    TQFileInfo checkXml;
    TQStringList saverConfigPaths = TQStringList::split(':', XSCREENSAVER_CONFIG_DIRS);
    for (TQStringList::ConstIterator it = saverConfigPaths.begin(); it != saverConfigPaths.end(); ++it)
    {
        checkXml.setFile((*it) + "/" + exeName + ".xml");
        if (checkXml.exists())
        {
            xmlFile = checkXml.absFilePath();
            break;
        }
    }
#endif
    if ( TQFile::exists( xmlFile ) ) {
	// We can use the xscreensaver xml config files.
	KXSXml xmlParser(0);
	xmlParser.parse(xmlFile);
	configItemList = *xmlParser.items();
	TQPtrListIterator<KXSConfigItem> it( configItemList );
	KXSConfigItem *item;
	while ( (item = it.current()) != 0 ) {
	    ++it;
	    item->read( config );
	}
    } else {
	// fall back to TDE's old config files.
	int idx = 0;
	while (true)
	{
	    TQString group = TQString("Arg%1").arg(idx);
	    if (config.hasGroup(group)) {
		config.setGroup(group);
		TQString type = config.readEntry("Type");
		if (type == "Range") {
		    KXSRangeItem *rc = new KXSRangeItem(group, config);
		    configItemList.append(rc);
		} else if (type == "DoubleRange") {
		    KXSDoubleRangeItem *rc = new KXSDoubleRangeItem(group, config);
		    configItemList.append(rc);
		} else if (type == "Check") {
		    KXSBoolItem *cc = new KXSBoolItem(group, config);
		    configItemList.append(cc);
		} else if (type == "DropList") {
		    KXSSelectItem *si = new KXSSelectItem(group, config);
		    configItemList.append(si);
		}
	    } else {
		break;
	    }
	    idx++;
	}
    }

    // find the xscreensaver executable
    //work around a KStandarDirs::findExe() "feature" where it looks in $TDEDIR/bin first no matter what and sometimes finds the wrong executable
    TQString exeFile;
    TQFileInfo checkExe;
    TQStringList saverPaths = TQStringList::split(':', XSCREENSAVER_HACKS_DIRS);
    for (TQStringList::ConstIterator it = saverPaths.begin(); it != saverPaths.end(); ++it)
    {
        checkExe.setFile((*it) + "/" + filename);
        if (checkExe.exists() && checkExe.isExecutable() && checkExe.isFile())
        {
            exeFile = checkExe.absFilePath();
            kdDebug() << "saverdir is" << exeFile << endl;
            break;
        }
    }

    if (exeFile.isEmpty())
    {
        // Executable not present in XScreenSaver directory!
        // Try standard paths
        exeFile = TDEStandardDirs::findExe(filename);
    }

    if (!exeFile.isEmpty()) {
	char *sargs[MAX_ARGS];
	sargs[0] = new char [strlen(filename.ascii())+1];
	strcpy(sargs[0], filename.ascii());

	// add the command line options
	TQString cmd;
	unsigned int i;
	for (i = 1; i < (unsigned)args->count(); i++)
	    cmd += " " + TQString(args->arg(i));

	// add the config options
	KXSConfigItem *item;
	for (item = configItemList.first(); item != 0; item = configItemList.next()) {
	    cmd += " " + item->command();
	}

	// put into char * array for execv
	TQString word;
	int si = 1;
	i = 0;
	bool inQuotes = false;
	while (i < cmd.length() && si < MAX_ARGS-1) {
	    word = "";
	    while ( cmd[i].isSpace() && i < cmd.length() ) i++;
	    while ( (!cmd[i].isSpace() || inQuotes) && i < cmd.length() ) {
		if ( cmd[i] == '\"' ) {
		    inQuotes = !inQuotes;
		} else {
		    word += cmd[i];
		}
		i++;
	    }
	    if (!word.isEmpty()) {
		sargs[si] = new char [strlen(word.ascii())+1];
		strcpy(sargs[si], word.ascii());
		si++;
	    }
	}

	sargs[si] = 0;

	// here goes...
	execv(exeFile.ascii(), sargs);
    }
}