kdecore Library API Documentation

kde-config.cpp

00001 // -*- c++ -*-
00002 
00003 #include <kcmdlineargs.h>
00004 #include <klocale.h>
00005 #include <kinstance.h>
00006 #include <kstandarddirs.h>
00007 #include <kglobal.h>
00008 #include <kglobalsettings.h>
00009 #include <stdio.h>
00010 #include <kaboutdata.h>
00011 #include <config.h>
00012 #include <kapplication.h>
00013 
00014 static const char *description = I18N_NOOP("A little program to output installation paths");
00015 
00016 static KCmdLineOptions options[] =
00017 {
00018     { "expandvars", I18N_NOOP("expand ${prefix} and ${exec_prefix} in output"), 0 },
00019     { "prefix",    I18N_NOOP("Compiled in prefix for KDE libraries"), 0 },
00020     { "exec-prefix", I18N_NOOP("Compiled in exec_prefix for KDE libraries"), 0 },
00021     { "localprefix", I18N_NOOP("Prefix in $HOME used to write files"), 0},
00022     { "version",   I18N_NOOP("Compiled in version string for KDE libraries"), 0 },
00023     { "types",     I18N_NOOP("Available KDE resource types"), 0 },
00024     { "path type", I18N_NOOP("Search path for resource type"), 0 },
00025     { "userpath type", I18N_NOOP("User path: desktop|autostart|trash|document"), 0 },
00026     { "install type", I18N_NOOP("Prefix to install resource files to"), 0},
00027     { 0,0,0 }
00028 };
00029 
00030 bool _expandvars = false;
00031 
00032 QString expandvars(const char *_input)
00033 {
00034     QString result = QString::fromLatin1(_input);
00035     if (!_expandvars)
00036         return result;
00037 
00038     bool changed = false;
00039     int index = result.find("${prefix}");
00040     if (index >= 0) {
00041         result = result.replace(index, 9, "/usr");
00042         changed = true;
00043     }
00044     index = result.find("${exec_prefix}");
00045     if (index >= 0) {
00046         result = result.replace(index, 14, "/usr");
00047         changed = true;
00048     }
00049     index = result.find("$(exec_prefix)");
00050     if (index >= 0) {
00051         result = result.replace(index, 14, "/usr");
00052         changed = true;
00053     }
00054     index = result.find("$(prefix");
00055     if (index >= 0) {
00056         result = result.replace(index, 9, "/usr");
00057         changed = true;
00058     }
00059     if (changed)
00060         return expandvars(result.latin1());
00061     else
00062         return result;
00063 }
00064 
00065 int main(int argc, char **argv)
00066 {
00067     KLocale::setMainCatalogue("kdelibs");
00068     KAboutData about("kde-config", "kde-config", "1.0", description, KAboutData::License_GPL, "(C) 2000 Stephan Kulow");
00069     KCmdLineArgs::init( argc, argv, &about);
00070 
00071     KCmdLineArgs::addCmdLineOptions( options ); // Add my own options.
00072 
00073     KInstance a("kde-config");
00074     (void)KGlobal::dirs(); // trigger the creation
00075     (void)KGlobal::config();
00076 
00077     // Get application specific arguments
00078     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00079 
00080     _expandvars = args->isSet("expandvars");
00081 
00082     if (args->isSet("prefix"))
00083     {
00084         printf("%s\n", expandvars("/usr").latin1());
00085         return 0;
00086     }
00087 
00088     if (args->isSet("exec-prefix"))
00089     {
00090         printf("%s\n", expandvars("/usr").latin1());
00091         return 0;
00092     }
00093 
00094     if (args->isSet("localprefix"))
00095     {
00096         printf("%s\n", KGlobal::dirs()->localkdedir().latin1());
00097         return 0;
00098     }
00099 
00100     if (args->isSet("version"))
00101     {
00102         printf("%s\n", KDE_VERSION_STRING);
00103         return 0;
00104     }
00105 
00106     if (args->isSet("types"))
00107     {
00108         QStringList types = KGlobal::dirs()->allTypes();
00109         types.sort();
00110         const char *helptexts[] = {
00111             "apps", I18N_NOOP("Applications menu (.desktop files)"),
00112             "cgi", I18N_NOOP("CGIs to run from kdehelp"),
00113             "config", I18N_NOOP("Configuration files"),
00114             "data", I18N_NOOP("Where applications store data"),
00115             "exe", I18N_NOOP("Executables in $prefix/bin"),
00116             "html", I18N_NOOP("HTML documentation"),
00117             "icon", I18N_NOOP("Icons"),
00118             "lib", I18N_NOOP("Libraries"),
00119             "locale", I18N_NOOP("Translation files for KLocale"),
00120             "mime", I18N_NOOP("Mime types"),
00121             "services", I18N_NOOP("Services"),
00122             "servicetypes", I18N_NOOP("Service types"),
00123             "sound", I18N_NOOP("Application sounds"),
00124             "templates", I18N_NOOP("Templates"),
00125             "wallpaper", I18N_NOOP("Wallpapers"),
00126             "tmp", I18N_NOOP("Temporary files (specfic for both current host and current user)"),
00127             "socket", I18N_NOOP("UNIX Sockets (specific for both current host and current user)"),
00128             0, 0
00129         };
00130         for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
00131         {
00132             int index = 0;
00133             while (helptexts[index] && *it != helptexts[index]) {
00134                 index += 2;
00135             }
00136             if (helptexts[index]) {
00137                 printf("%s - %s\n", helptexts[index], i18n(helptexts[index+1]).local8Bit().data());
00138             } else {
00139                 printf(i18n("%1 - unknown type\n").arg(*it).local8Bit().data());
00140             }
00141         }
00142         return 0;
00143     }
00144 
00145     QString type = args->getOption("path");
00146     if (!type.isEmpty())
00147     {
00148         printf("%s\n", KGlobal::dirs()->resourceDirs(type.latin1()).
00149                join(":").latin1());
00150         return 0;
00151     }
00152 
00153     type = args->getOption("userpath");
00154     if (!type.isEmpty())
00155     {
00156         if ( type == "desktop" )
00157             printf("%s\n", KGlobalSettings::desktopPath().local8Bit().data() );
00158         else if ( type == "autostart" )
00159             printf("%s\n", KGlobalSettings::autostartPath().local8Bit().data() );
00160         else if ( type == "trash" )
00161             printf("%s\n", KGlobalSettings::trashPath().local8Bit().data() );
00162         else if ( type == "document" )
00163             printf("%s\n", KGlobalSettings::documentPath().local8Bit().data() );
00164         else
00165             printf(i18n("%1 - unknown type of userpath\n").arg(type).local8Bit().data() );
00166         return 0;
00167     }
00168 
00169     type = args->getOption("install");
00170     if (!type.isEmpty())
00171     {
00172         const char *installprefixes[] = {
00173             "apps",   "${prefix}/share/applnk",
00174             "config", "${prefix}/share/config",
00175             "data",   "${prefix}/share/apps",
00176             "exe",    "${exec_prefix}/bin",
00177             "html",   "${prefix}/share/doc/HTML",
00178             "icon",   "${prefix}/share/icons",
00179             "lib",    "/usr/lib",
00180             "locale", "${prefix}/share/locale",
00181             "mime",   "${prefix}/share/mimelnk",
00182             "services", "${prefix}/share/services",
00183             "servicetypes", "${prefix}/share/servicetypes",
00184             "sound", "${prefix}/share/sounds",
00185             "templates", "${prefix}/share/templates",
00186             "wallpaper", "${prefix}/share/wallpapers",
00187             0, 0
00188         };
00189         int index = 0;
00190         while (installprefixes[index] && type != installprefixes[index]) {
00191             index += 2;
00192         }
00193         if (installprefixes[index]) {
00194             printf("%s\n", expandvars(installprefixes[index+1]).latin1());
00195         } else {
00196             printf("NONE\n"); // no i18n here as for scripts
00197         }
00198     }
00199     return 0;
00200 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:40 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001