prospective fix for crashes induced by an invalidated iterator.
[kdelibs.git] / kdecore / kde-config.cpp.in
blobd72798f84f8b8428a5193d27dde3f47cc9d122e8
1 // -*- c++ -*-
3 #include <kcmdlineargs.h>
4 #include <klocale.h>
5 #include <kcomponentdata.h>
6 #include <kstandarddirs.h>
7 #include <kglobal.h>
8 #include <kconfig.h>
9 #include <qdir.h>
10 #include <stdio.h>
11 #include <kaboutdata.h>
12 #include <kdeversion.h>
13 #include <QDir>
14 #include <config.h>
15 #include <kconfiggroup.h>
17 #ifdef Q_WS_WIN
18 #include <win32_utils.h>
19 #endif
21 static const char *description = I18N_NOOP("A little program to output installation paths");
23 static KCmdLineOptions options[] =
25 { "expandvars", I18N_NOOP("expand ${prefix} and ${exec_prefix} in output"), 0 },
26 { "prefix", I18N_NOOP("Compiled in prefix for KDE libraries"), 0 },
27 { "exec-prefix", I18N_NOOP("Compiled in exec_prefix for KDE libraries"), 0 },
28 { "libsuffix", I18N_NOOP("Compiled in library path suffix"), 0 },
29 { "localprefix", I18N_NOOP("Prefix in $HOME used to write files"), 0},
30 { "version", I18N_NOOP("Compiled in version string for KDE libraries"), 0 },
31 { "types", I18N_NOOP("Available KDE resource types"), 0 },
32 { "path type", I18N_NOOP("Search path for resource type"), 0 },
33 { "locate filename", I18N_NOOP("Find filename inside the resource type given to --path"), 0},
34 { "userpath type", I18N_NOOP("User path: desktop|autostart|document"), 0 },
35 { "install type", I18N_NOOP("Prefix to install resource files to"), 0},
36 { 0,0,0 }
39 bool _expandvars = false;
41 QString expandvars(const char *_input)
43 QString result = QLatin1String(_input);
44 if (!_expandvars)
45 return result;
47 bool changed = false;
48 int index = result.indexOf("${prefix}");
49 if (index >= 0) {
50 result = result.replace(index, 9, "@prefix@");
51 changed = true;
53 index = result.indexOf("$(prefix)");
54 if (index >= 0) {
55 result = result.replace(index, 9, "@prefix@");
56 changed = true;
58 index = result.indexOf("${datadir}");
59 if (index >= 0) {
60 result = result.replace(index, 10, "@datadir@");
61 changed = true;
63 index = result.indexOf("$(datadir)");
64 if (index >= 0) {
65 result = result.replace(index, 10, "@datadir@");
66 changed = true;
68 index = result.indexOf("${exec_prefix}");
69 if (index >= 0) {
70 result = result.replace(index, 14, "@exec_prefix@");
71 changed = true;
73 index = result.indexOf("$(exec_prefix)");
74 if (index >= 0) {
75 result = result.replace(index, 14, "@exec_prefix@");
76 changed = true;
78 index = result.indexOf("${libdir}");
79 if (index >= 0) {
80 result = result.replace(index, 9, "@libdir@");
81 changed = true;
83 index = result.indexOf("$(libdir)");
84 if (index >= 0) {
85 result = result.replace(index, 9, "@libdir@");
86 changed = true;
88 index = result.indexOf("${includedir}");
89 if (index >= 0) {
90 result = result.replace(index, 20, "@includedir@");
91 changed = true;
93 index = result.indexOf("$(includedir)");
94 if (index >= 0) {
95 result = result.replace(index, 20, "@includedir@");
96 changed = true;
98 index = result.indexOf("${sysconfdir}");
99 if (index >= 0) {
100 result = result.replace(index, 13, "@sysconfdir@");
101 changed = true;
103 index = result.indexOf("$(sysconfdir)");
104 if (index >= 0) {
105 result = result.replace(index, 13, "@sysconfdir@");
106 changed = true;
108 if (changed)
109 return expandvars(result.toLatin1());
110 else
111 return result;
114 void printResult(const QString &s)
116 if (s.isEmpty())
117 printf("\n");
118 else {
119 QString path = QDir::convertSeparators( s );
120 printf("%s\n", path.toLocal8Bit().constData());
124 int main(int argc, char **argv)
126 KLocale::setMainCatalog("kdelibs");
127 KAboutData about("kde4-config", "kde4-config", "1.0", description, KAboutData::License_GPL, "(C) 2000 Stephan Kulow");
128 KCmdLineArgs::init( argc, argv, &about);
130 KCmdLineArgs::addCmdLineOptions( options ); // Add my own options.
132 KComponentData a("kde4-config");
133 (void)KGlobal::dirs(); // trigger the creation
134 (void)KGlobal::config();
136 // Get application specific arguments
137 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
139 _expandvars = args->isSet("expandvars");
141 if (args->isSet("prefix"))
143 printResult(expandvars("@prefix@"));
144 return 0;
147 if (args->isSet("exec-prefix"))
149 printResult(expandvars("@exec_prefix@"));
150 return 0;
153 if (args->isSet("libsuffix"))
155 QString tmp(KDELIBSUFF);
156 tmp.remove('"');
157 printResult(expandvars(tmp.toLocal8Bit()));
158 return 0;
161 if (args->isSet("localprefix"))
163 printResult(KGlobal::dirs()->localkdedir());
164 return 0;
167 if (args->isSet("version"))
169 printf("%s\n", KDE_VERSION_STRING);
170 return 0;
173 if (args->isSet("types"))
175 QStringList types = KGlobal::dirs()->allTypes();
176 types.sort();
177 const char *helptexts[] = {
178 "apps", I18N_NOOP("Applications menu (.desktop files)"),
179 "cgi", I18N_NOOP("CGIs to run from kdehelp"),
180 "config", I18N_NOOP("Configuration files"),
181 "data", I18N_NOOP("Where applications store data"),
182 "emoticons", I18N_NOOP("Emoticons"),
183 "exe", I18N_NOOP("Executables in $prefix/bin"),
184 "html", I18N_NOOP("HTML documentation"),
185 "icon", I18N_NOOP("Icons"),
186 "kcfg", I18N_NOOP("Configuration description files"),
187 "lib", I18N_NOOP("Libraries"),
188 "include", I18N_NOOP("Includes/Headers"),
189 "locale", I18N_NOOP("Translation files for KLocale"),
190 "mime", I18N_NOOP("Mime types"),
191 "module", I18N_NOOP("Loadable modules"),
192 "pixmap", I18N_NOOP("Legacy pixmaps"),
193 "qtplugins", I18N_NOOP("Qt plugins"),
194 "services", I18N_NOOP("Services"),
195 "servicetypes", I18N_NOOP("Service types"),
196 "sound", I18N_NOOP("Application sounds"),
197 "templates", I18N_NOOP("Templates"),
198 "wallpaper", I18N_NOOP("Wallpapers"),
199 "xdgdata-apps", I18N_NOOP("XDG Application menu (.desktop files)"),
200 "xdgdata-dirs", I18N_NOOP("XDG Menu descriptions (.directory files)"),
201 "xdgdata-icon", I18N_NOOP("XDG Icons"),
202 "xdgdata-pixmaps", I18N_NOOP("Legacy pixmaps"),
203 "xdgdata-mime", I18N_NOOP("XDG Mime Types"),
204 "xdgconf-menu", I18N_NOOP("XDG Menu layout (.menu files)"),
205 "tmp", I18N_NOOP("Temporary files (specific for both current host and current user)"),
206 "socket", I18N_NOOP("UNIX Sockets (specific for both current host and current user)"),
207 0, 0
209 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
211 int index = 0;
212 while (helptexts[index] && *it != helptexts[index]) {
213 index += 2;
215 if (helptexts[index]) {
216 printf("%s - %s\n", helptexts[index], i18n(helptexts[index+1]).toLocal8Bit().data());
217 } else {
218 printf("%s", i18n("%1 - unknown type\n", *it).toLocal8Bit().data());
221 return 0;
224 QString type = args->getOption("path");
225 if (!type.isEmpty())
227 QString fileName = args->getOption("locate");
228 if (!fileName.isEmpty())
230 QString result = KStandardDirs::locate(type.toLatin1(), fileName);
231 if (!result.isEmpty())
232 printf("%s\n", result.toLocal8Bit().constData());
233 return result.isEmpty() ? 1 : 0;
236 printResult(KGlobal::dirs()->resourceDirs(type.toLatin1()).join(QString(KPATH_SEPARATOR)));
237 return 0;
240 type = args->getOption("userpath");
241 if (!type.isEmpty())
243 //code duplicated with KGlobalSettings::initPath()
244 if ( type == "desktop" )
246 KConfigGroup g( KGlobal::config(), "Paths" );
247 QString path=QDir::homePath() + "/Desktop/";
248 path=g.readPathEntry( "Desktop", path);
249 path=QDir::cleanPath( path );
250 if ( !path.endsWith("/") )
251 path.append(QLatin1Char('/'));
252 printResult(path);
254 else if ( type == "autostart" )
256 KConfigGroup g( KGlobal::config(), "Paths" );
257 QString path=QDir::homePath() + "/Autostart/";
258 path=g.readPathEntry( "Autostart", path);
259 path=QDir::cleanPath( path );
260 if ( !path.endsWith("/") )
261 path.append(QLatin1Char('/'));
262 printResult(path);
265 else if ( type == "document" )
267 KConfigGroup g( KGlobal::config(), "Paths" );
268 #ifdef Q_WS_WIN
269 QString path=getWin32ShellFoldersPath("Personal");
270 #else
271 QString path=QDir::homePath();
272 #endif
273 path=g.readPathEntry( "Desktop", path);
274 path=QDir::cleanPath( path );
275 if ( !path.endsWith("/") )
276 path.append(QLatin1Char('/'));
277 printResult(path);
279 else
280 fprintf(stderr, "%s", i18n("%1 - unknown type of userpath\n", type).toLocal8Bit().data() );
281 return 0;
284 type = args->getOption("install");
285 if (!type.isEmpty())
287 const char *installprefixes[] = {
288 "apps", "@kde_appsdir@",
289 "config", "@kde_confdir@",
290 "kcfg", "@kde_kcfgdir@",
291 "data", "@kde_datadir@",
292 "exe", "@kde_bindir@",
293 "html", "@kde_htmldir@",
294 "icon", "@kde_icondir@",
295 "lib", "@libdir@",
296 "module", "@kde_moduledir@",
297 "qtplugins", "@kde_moduledir@/plugins",
298 "locale", "@kde_locale@",
299 "mime", "@kde_mimedir@",
300 "services", "@kde_servicesdir@",
301 "servicetypes", "@kde_servicetypesdir@",
302 "sound", "@kde_sounddir@",
303 "templates", "@kde_templatesdir@",
304 "wallpaper", "@kde_wallpaperdir@",
305 "xdgconf-menu", "@xdg_menudir@",
306 "xdgdata-apps", "@xdg_appsdir@",
307 "xdgdata-dirs", "@xdg_directorydir@",
308 "include", "@includedir@",
309 0, 0
311 int index = 0;
312 while (installprefixes[index] && type != installprefixes[index]) {
313 index += 2;
315 if (installprefixes[index]) {
316 printResult(expandvars(installprefixes[index+1]));
317 } else {
318 printResult("NONE"); // no i18n here as for scripts
321 return 0;