1// -*- c++ -*-
2/*
3 * This file is part of the KDE libraries
4 *
5 * Copyright (c) 2008 David Jarvie <djarvie@kde.org>
6 * Copyright (c) 2006-2007 Christian Ehrlicher <Ch.Ehrlicher@gmx.de>
7 * Copyright (c) 2000-2007 Stephan Kulow <coolo@kde.org>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25
26#include <kcmdlineargs.h>
27#include <klocale.h>
28#include <kcomponentdata.h>
29#include <kstandarddirs.h>
30#include <kglobal.h>
31#include <kconfig.h>
32#include <qdir.h>
33#include <stdio.h>
34#include <kaboutdata.h>
35#include <kdeversion.h>
36#include <QDir>
37#include <QtCore/QLibraryInfo>
38#include <QtCore/QCoreApplication>
39#include <QProcess>
40#include <config.h>
41#include <config-prefix.h>
42#include <kconfiggroup.h>
43#include <kkernel_win.h>
44#include <kde_file.h>
45
46static void printResult(const QString &s)
47{
48 if (s.isEmpty())
49 printf("\n");
50 else {
51 QString path = QDir::toNativeSeparators( s );
52 printf("%s\n", path.toLocal8Bit().constData());
53 }
54}
55
56static QString readXdg( const char* type )
57{
58 QProcess proc;
59 proc.start( QString::fromLatin1("xdg-user-dir"), QStringList() << QString::fromLatin1(type) );
60 if (!proc.waitForStarted() || !proc.waitForFinished())
61 return QString();
62 return QString::fromLocal8Bit( proc.readAll()).trimmed();
63}
64
65int main(int argc, char **argv)
66{
67 QCoreApplication app(argc, argv);
68 KAboutData about("kde4-config", "kdelibs4", ki18n("kde4-config"), "1.0",
69 ki18n("A little program to output installation paths"),
70 KAboutData::License_GPL,
71 ki18n("(C) 2000 Stephan Kulow"));
72 KCmdLineArgs::init( argc, argv, &about);
73
74 KCmdLineOptions options;
75 options.add("expandvars", ki18n("Left for legacy support"));
76 options.add("prefix", ki18n("Compiled in prefix for KDE libraries"));
77 options.add("exec-prefix", ki18n("Compiled in exec_prefix for KDE libraries"));
78 options.add("libsuffix", ki18n("Compiled in library path suffix"));
79 options.add("localprefix", ki18n("Prefix in $HOME used to write files"));
80 options.add("kde-version", ki18n("Compiled in version string for KDE libraries"));
81 options.add("types", ki18n("Available KDE resource types"));
82 options.add("path type", ki18n("Search path for resource type"));
83 options.add("locate filename", ki18n("Find filename inside the resource type given to --path"));
84 options.add("userpath type", ki18n("User path: desktop|autostart|document"));
85 options.add("install type", ki18n("Prefix to install resource files to"));
86 options.add("qt-prefix", ki18n("Installation prefix for Qt"));
87 options.add("qt-binaries", ki18n("Location of installed Qt binaries"));
88 options.add("qt-libraries", ki18n("Location of installed Qt libraries"));
89 options.add("qt-plugins", ki18n("Location of installed Qt plugins"));
90 KCmdLineArgs::addCmdLineOptions( options ); // Add my own options.
91
92 KComponentData a(&about);
93 (void)KGlobal::dirs(); // trigger the creation
94 (void)KGlobal::config();
95
96 // Get application specific arguments
97 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
98
99 if (args->isSet("prefix"))
100 {
101 printResult(QFile::decodeName(KDEDIR));
102 return 0;
103 }
104
105 if (args->isSet("exec-prefix"))
106 {
107 printResult(QFile::decodeName(EXEC_INSTALL_PREFIX));
108 return 0;
109 }
110
111 if (args->isSet("libsuffix"))
112 {
113 QString tmp(QFile::decodeName(KDELIBSUFF));
114 tmp.remove(QLatin1Char('"'));
115 printResult(tmp);
116 return 0;
117 }
118
119 if (args->isSet("localprefix"))
120 {
121 printResult(KGlobal::dirs()->localkdedir());
122 return 0;
123 }
124
125 if (args->isSet("kde-version"))
126 {
127 printf("%s\n", KDE_VERSION_STRING);
128 return 0;
129 }
130
131 if (args->isSet("types"))
132 {
133 QStringList types = KGlobal::dirs()->allTypes();
134 types.sort();
135 const char *helptexts[] = {
136 "apps", I18N_NOOP("Applications menu (.desktop files)"),
137 "autostart", I18N_NOOP("Autostart directories"),
138 "cache", I18N_NOOP("Cached information (e.g. favicons, web-pages)"),
139 "cgi", I18N_NOOP("CGIs to run from kdehelp"),
140 "config", I18N_NOOP("Configuration files"),
141 "data", I18N_NOOP("Where applications store data"),
142 "emoticons", I18N_NOOP("Emoticons"),
143 "exe", I18N_NOOP("Executables in $prefix/bin"),
144 "html", I18N_NOOP("HTML documentation"),
145 "icon", I18N_NOOP("Icons"),
146 "kcfg", I18N_NOOP("Configuration description files"),
147 "lib", I18N_NOOP("Libraries"),
148 "include", I18N_NOOP("Includes/Headers"),
149 "locale", I18N_NOOP("Translation files for KLocale"),
150 "mime", I18N_NOOP("Mime types"),
151 "module", I18N_NOOP("Loadable modules"),
152 "pixmap", I18N_NOOP("Legacy pixmaps"),
153 "qtplugins", I18N_NOOP("Qt plugins"),
154 "services", I18N_NOOP("Services"),
155 "servicetypes", I18N_NOOP("Service types"),
156 "sound", I18N_NOOP("Application sounds"),
157 "templates", I18N_NOOP("Templates"),
158 "wallpaper", I18N_NOOP("Wallpapers"),
159 "xdgdata-apps", I18N_NOOP("XDG Application menu (.desktop files)"),
160 "xdgdata-dirs", I18N_NOOP("XDG Menu descriptions (.directory files)"),
161 "xdgdata-icon", I18N_NOOP("XDG Icons"),
162 "xdgdata-pixmap", I18N_NOOP("Legacy pixmaps"),
163 "xdgdata-mime", I18N_NOOP("XDG Mime Types"),
164 "xdgconf-menu", I18N_NOOP("XDG Menu layout (.menu files)"),
165 "xdgconf-autostart", I18N_NOOP("XDG autostart directory"),
166 "tmp", I18N_NOOP("Temporary files (specific for both current host and current user)"),
167 "socket", I18N_NOOP("UNIX Sockets (specific for both current host and current user)"),
168 0, 0
169 };
170 Q_FOREACH(const QString &type, types)
171 {
172 int index = 0;
173 while (helptexts[index] && type != QLatin1String(helptexts[index])) {
174 index += 2;
175 }
176 if (helptexts[index]) {
177 printf("%s - %s\n", helptexts[index], i18n(helptexts[index+1]).toLocal8Bit().constData());
178 } else {
179 printf("%s", i18n("%1 - unknown type\n", type).toLocal8Bit().constData());
180 }
181 }
182 return 0;
183 }
184
185 QString type = args->getOption("path");
186 if (!type.isEmpty())
187 {
188 QString fileName = args->getOption("locate");
189 if (!fileName.isEmpty())
190 {
191 QString result = KStandardDirs::locate(type.toLatin1(), fileName);
192 if (!result.isEmpty())
193 printf("%s\n", result.toLocal8Bit().constData());
194 return result.isEmpty() ? 1 : 0;
195 }
196
197 printResult(KGlobal::dirs()->resourceDirs(type.toLatin1()).join(QString(QChar::fromLatin1(KPATH_SEPARATOR))));
198 return 0;
199 }
200
201 type = args->getOption("userpath");
202 if (!type.isEmpty())
203 {
204 //code duplicated with KGlobalSettings::initPath()
205 if (type == QLatin1String("desktop"))
206 { // QDesktopServices is QtGui :-/
207 QString path = readXdg( "DESKTOP" );
208 if (path.isEmpty())
209 path = QDir::homePath() + QLatin1String("/Desktop");
210 path = QDir::cleanPath(path);
211 if (!path.endsWith(QLatin1Char('/')))
212 path.append(QLatin1Char('/'));
213 printResult(path);
214 }
215 else if (type == QLatin1String("autostart"))
216 {
217 KConfigGroup g( KGlobal::config(), "Paths" );
218 QString path = QDir::homePath() + QLatin1String("/Autostart/");
219 path = g.readPathEntry( "Autostart", path);
220 path = QDir::cleanPath( path );
221 if (!path.endsWith(QLatin1Char('/')))
222 path.append(QLatin1Char(QLatin1Char('/')));
223 printResult(path);
224
225 }
226 else if (type == QLatin1String("document"))
227 {
228 QString path = readXdg( "DOCUMENTS" );
229 if ( path.isEmpty())
230 path = QDir::homePath() + QLatin1String("/Documents");
231 path = QDir::cleanPath( path );
232 if (!path.endsWith(QLatin1Char('/')))
233 path.append(QLatin1Char('/'));
234 printResult(path);
235 }
236 else
237 fprintf(stderr, "%s", i18n("%1 - unknown type of userpath\n", type).toLocal8Bit().data() );
238 return 0;
239 }
240
241 type = args->getOption("install");
242 if (!type.isEmpty())
243 {
244 printResult( KGlobal::dirs()->installPath(type.toLocal8Bit()) );
245 }
246
247 if (args->isSet("qt-prefix"))
248 {
249 printResult(QLibraryInfo::location(QLibraryInfo::PrefixPath));
250 return 0;
251 }
252 if (args->isSet("qt-binaries"))
253 {
254 printResult(QLibraryInfo::location(QLibraryInfo::BinariesPath));
255 return 0;
256 }
257 if (args->isSet("qt-libraries"))
258 {
259 printResult(QLibraryInfo::location(QLibraryInfo::LibrariesPath));
260 return 0;
261 }
262 if (args->isSet("qt-plugins"))
263 {
264 printResult(QLibraryInfo::location(QLibraryInfo::PluginsPath));
265 return 0;
266 }
267 return 0;
268}
269