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 { "libsuffix", I18N_NOOP("Compiled in library path suffix"), 0 }, 00022 { "localprefix", I18N_NOOP("Prefix in $HOME used to write files"), 0}, 00023 { "kde-version", I18N_NOOP("Compiled in version string for KDE libraries"), 0 }, 00024 { "types", I18N_NOOP("Available KDE resource types"), 0 }, 00025 { "path type", I18N_NOOP("Search path for resource type"), 0 }, 00026 { "userpath type", I18N_NOOP("User path: desktop|autostart|trash|document"), 0 }, 00027 { "install type", I18N_NOOP("Prefix to install resource files to"), 0}, 00028 { 0,0,0 } 00029 }; 00030 00031 bool _expandvars = false; 00032 00033 QString expandvars(const char *_input) 00034 { 00035 QString result = QString::fromLatin1(_input); 00036 if (!_expandvars) 00037 return result; 00038 00039 bool changed = false; 00040 int index = result.find("${prefix}"); 00041 if (index >= 0) { 00042 result = result.replace(index, 9, "/usr"); 00043 changed = true; 00044 } 00045 index = result.find("$(prefix)"); 00046 if (index >= 0) { 00047 result = result.replace(index, 9, "/usr"); 00048 changed = true; 00049 } 00050 index = result.find("${datadir}"); 00051 if (index >= 0) { 00052 result = result.replace(index, 10, "/usr/share"); 00053 changed = true; 00054 } 00055 index = result.find("$(datadir)"); 00056 if (index >= 0) { 00057 result = result.replace(index, 10, "/usr/share"); 00058 changed = true; 00059 } 00060 index = result.find("${exec_prefix}"); 00061 if (index >= 0) { 00062 result = result.replace(index, 14, "/usr"); 00063 changed = true; 00064 } 00065 index = result.find("$(exec_prefix)"); 00066 if (index >= 0) { 00067 result = result.replace(index, 14, "/usr"); 00068 changed = true; 00069 } 00070 index = result.find("${libdir}"); 00071 if (index >= 0) { 00072 result = result.replace(index, 9, "/usr/lib64"); 00073 changed = true; 00074 } 00075 index = result.find("$(libdir)"); 00076 if (index >= 0) { 00077 result = result.replace(index, 9, "/usr/lib64"); 00078 changed = true; 00079 } 00080 index = result.find("${includedir}"); 00081 if (index >= 0) { 00082 result = result.replace(index, 20, "/usr/include/kde"); 00083 changed = true; 00084 } 00085 index = result.find("$(includedir)"); 00086 if (index >= 0) { 00087 result = result.replace(index, 20, "/usr/include/kde"); 00088 changed = true; 00089 } 00090 index = result.find("${sysconfdir}"); 00091 if (index >= 0) { 00092 result = result.replace(index, 13, "/etc"); 00093 changed = true; 00094 } 00095 index = result.find("$(sysconfdir)"); 00096 if (index >= 0) { 00097 result = result.replace(index, 13, "/etc"); 00098 changed = true; 00099 } 00100 if (changed) 00101 return expandvars(result.latin1()); 00102 else 00103 return result; 00104 } 00105 00106 void printResult(const QString &s) 00107 { 00108 if (s.isEmpty()) 00109 printf("\n"); 00110 else 00111 printf("%s\n", s.local8Bit().data()); 00112 } 00113 00114 int main(int argc, char **argv) 00115 { 00116 KLocale::setMainCatalogue("kdelibs"); 00117 KAboutData about("kde-config", "kde-config", "1.0", description, KAboutData::License_GPL, "(C) 2000 Stephan Kulow"); 00118 KCmdLineArgs::init( argc, argv, &about); 00119 00120 KCmdLineArgs::addCmdLineOptions( options ); // Add my own options. 00121 00122 KInstance a("kde-config"); 00123 (void)KGlobal::dirs(); // trigger the creation 00124 (void)KGlobal::config(); 00125 00126 // Get application specific arguments 00127 KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); 00128 00129 _expandvars = args->isSet("expandvars"); 00130 00131 if (args->isSet("prefix")) 00132 { 00133 printResult(expandvars("/usr")); 00134 return 0; 00135 } 00136 00137 if (args->isSet("exec-prefix")) 00138 { 00139 printResult(expandvars("/usr")); 00140 return 0; 00141 } 00142 00143 if (args->isSet("libsuffix")) 00144 { 00145 QString tmp(KDELIBSUFF); 00146 tmp.remove('"'); 00147 printResult(expandvars(tmp.local8Bit())); 00148 return 0; 00149 } 00150 00151 if (args->isSet("localprefix")) 00152 { 00153 printResult(KGlobal::dirs()->localkdedir()); 00154 return 0; 00155 } 00156 00157 if (args->isSet("kde-version")) 00158 { 00159 printf("%s\n", KDE_VERSION_STRING); 00160 return 0; 00161 } 00162 00163 if (args->isSet("types")) 00164 { 00165 QStringList types = KGlobal::dirs()->allTypes(); 00166 types.sort(); 00167 const char *helptexts[] = { 00168 "apps", I18N_NOOP("Applications menu (.desktop files)"), 00169 "cgi", I18N_NOOP("CGIs to run from kdehelp"), 00170 "config", I18N_NOOP("Configuration files"), 00171 "data", I18N_NOOP("Where applications store data"), 00172 "exe", I18N_NOOP("Executables in $prefix/bin"), 00173 "html", I18N_NOOP("HTML documentation"), 00174 "icon", I18N_NOOP("Icons"), 00175 "kcfg", I18N_NOOP("Configuration description files"), 00176 "lib", I18N_NOOP("Libraries"), 00177 "include", I18N_NOOP("Includes/Headers"), 00178 "locale", I18N_NOOP("Translation files for KLocale"), 00179 "mime", I18N_NOOP("Mime types"), 00180 "module", I18N_NOOP("Loadable modules"), 00181 "qtplugins", I18N_NOOP("Qt plugins"), 00182 "services", I18N_NOOP("Services"), 00183 "servicetypes", I18N_NOOP("Service types"), 00184 "sound", I18N_NOOP("Application sounds"), 00185 "templates", I18N_NOOP("Templates"), 00186 "wallpaper", I18N_NOOP("Wallpapers"), 00187 "xdgdata-apps", I18N_NOOP("XDG Application menu (.desktop files)"), 00188 "xdgdata-dirs", I18N_NOOP("XDG Menu descriptions (.directory files)"), 00189 "xdgconf-menu", I18N_NOOP("XDG Menu layout (.menu files)"), 00190 "tmp", I18N_NOOP("Temporary files (specific for both current host and current user)"), 00191 "socket", I18N_NOOP("UNIX Sockets (specific for both current host and current user)"), 00192 0, 0 00193 }; 00194 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) 00195 { 00196 int index = 0; 00197 while (helptexts[index] && *it != helptexts[index]) { 00198 index += 2; 00199 } 00200 if (helptexts[index]) { 00201 printf("%s - %s\n", helptexts[index], i18n(helptexts[index+1]).local8Bit().data()); 00202 } else { 00203 printf("%s", i18n("%1 - unknown type\n").arg(*it).local8Bit().data()); 00204 } 00205 } 00206 return 0; 00207 } 00208 00209 QString type = args->getOption("path"); 00210 if (!type.isEmpty()) 00211 { 00212 printResult(KGlobal::dirs()->resourceDirs(type.latin1()).join(":")); 00213 return 0; 00214 } 00215 00216 type = args->getOption("userpath"); 00217 if (!type.isEmpty()) 00218 { 00219 if ( type == "desktop" ) 00220 printResult(KGlobalSettings::desktopPath()); 00221 else if ( type == "autostart" ) 00222 printResult(KGlobalSettings::autostartPath()); 00223 else if ( type == "trash" ) 00224 printResult(KGlobalSettings::trashPath()); 00225 else if ( type == "document" ) 00226 printResult(KGlobalSettings::documentPath()); 00227 else 00228 fprintf(stderr, "%s", i18n("%1 - unknown type of userpath\n").arg(type).local8Bit().data() ); 00229 return 0; 00230 } 00231 00232 type = args->getOption("install"); 00233 if (!type.isEmpty()) 00234 { 00235 const char *installprefixes[] = { 00236 "apps", "${datadir}/applnk", 00237 "config", "${datadir}/config", 00238 "kcfg", "${datadir}/config.kcfg", 00239 "data", "${datadir}/apps", 00240 "exe", "${exec_prefix}/bin", 00241 "html", "${datadir}/doc/HTML", 00242 "icon", "${datadir}/icons", 00243 "lib", "/usr/lib64", 00244 "module", "${libdir}/kde3", 00245 "qtplugins", "${libdir}/kde3/plugins", 00246 "locale", "${datadir}/locale", 00247 "mime", "${datadir}/mimelnk", 00248 "services", "${datadir}/services", 00249 "servicetypes", "${datadir}/servicetypes", 00250 "sound", "${datadir}/sounds", 00251 "templates", "${datadir}/templates", 00252 "wallpaper", "${datadir}/wallpapers", 00253 "xdgconf-menu", "${sysconfdir}/xdg/menus", 00254 "xdgdata-apps", "${datadir}/applications/kde", 00255 "xdgdata-dirs", "${datadir}/desktop-directories", 00256 "include", "/usr/include/kde", 00257 0, 0 00258 }; 00259 int index = 0; 00260 while (installprefixes[index] && type != installprefixes[index]) { 00261 index += 2; 00262 } 00263 if (installprefixes[index]) { 00264 printResult(expandvars(installprefixes[index+1])); 00265 } else { 00266 printResult("NONE"); // no i18n here as for scripts 00267 } 00268 } 00269 return 0; 00270 }