• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
kde-config.cpp
Go to the documentation of this file.
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 
46 static 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 
56 static 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 
65 int 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 }
i18n
QString i18n(const char *text)
Returns a localized version of a string.
Definition: klocalizedstring.h:630
KConfigGroup::readPathEntry
QString readPathEntry(const QString &pKey, const QString &aDefault) const
Reads a path.
Definition: kconfiggroup.cpp:780
KCmdLineArgs::addCmdLineOptions
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
Add options to your application.
Definition: kcmdlineargs.cpp:503
QDir::toNativeSeparators
QString toNativeSeparators(const QString &pathName)
QString::append
QString & append(QChar ch)
KCmdLineOptions::add
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
Add command line option, by providing its name, description, and possibly a default value...
Definition: kcmdlineargs.cpp:142
readXdg
static QString readXdg(const char *type)
Definition: kde-config.cpp:56
ki18n
KLocalizedString ki18n(const char *msg)
Creates localized string from a given message.
Definition: klocalizedstring.cpp:924
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Access parsed arguments.
Definition: kcmdlineargs.cpp:611
QCoreApplication
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
This function is just for convenience.
Definition: kstandarddirs.cpp:2085
kconfig.h
KCmdLineArgs
A class for command-line argument handling.
Definition: kcmdlineargs.h:281
KGlobal::dirs
KStandardDirs * dirs()
Returns the application standard dirs object.
QString::remove
QString & remove(int position, int n)
I18N_NOOP
#define I18N_NOOP(x)
I18N_NOOP marks a string to be translated without translating it.
Definition: klocalizedstring.h:51
klocale.h
KDE_VERSION_STRING
#define KDE_VERSION_STRING
Version of KDE as string, at compile time.
Definition: kdeversion.h.cmake:47
QDir::homePath
QString homePath()
KCmdLineArgs::isSet
bool isSet(const QByteArray &option) const
Read out a boolean option or check for the presence of string option.
Definition: kcmdlineargs.cpp:1496
KGlobal::config
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:139
kglobal.h
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
QProcess
QChar::fromLatin1
QChar fromLatin1(char c)
printResult
static void printResult(const QString &s)
Definition: kde-config.cpp:46
kcmdlineargs.h
QString::isEmpty
bool isEmpty() const
QByteArray::constData
const char * constData() const
QIODevice::readAll
QByteArray readAll()
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
KAboutData
This class is used to store information about a program.
Definition: kaboutdata.h:192
QString
QStringList
kkernel_win.h
QString::toLocal8Bit
QByteArray toLocal8Bit() const
QProcess::waitForStarted
bool waitForStarted(int msecs)
QLatin1Char
KConfigGroup
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:53
QDir::cleanPath
QString cleanPath(const QString &path)
QString::toLatin1
QByteArray toLatin1() const
QLatin1String
kstandarddirs.h
main
int main(int argc, char **argv)
Definition: kde-config.cpp:65
KCmdLineArgs::init
static void init(int argc, char **argv, const QByteArray &appname, const QByteArray &catalog, const KLocalizedString &programName, const QByteArray &version, const KLocalizedString &description=KLocalizedString(), StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
Initialize class.
Definition: kcmdlineargs.cpp:418
KAboutData::License_GPL
Definition: kaboutdata.h:203
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QStringList::sort
void sort()
KCmdLineArgs::getOption
QString getOption(const QByteArray &option) const
Read out a string option.
Definition: kcmdlineargs.cpp:1438
KStandardDirs::allTypes
QStringList allTypes() const
This function will return a list of all the types that KStandardDirs supports.
Definition: kstandarddirs.cpp:281
kaboutdata.h
kcomponentdata.h
QLibraryInfo::location
QString location(LibraryLocation loc)
KCmdLineOptions
Class that holds command line options.
Definition: kcmdlineargs.h:48
KComponentData
Per component data.
Definition: kcomponentdata.h:46
QProcess::start
void start(const QString &program, const QStringList &arguments, QFlags< QIODevice::OpenModeFlag > mode)
QFile::decodeName
QString decodeName(const QByteArray &localFileName)
kconfiggroup.h
QProcess::waitForFinished
bool waitForFinished(int msecs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal