Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kreadconfig / kreadconfig.cpp
blob915c0967bc609700507a242a03f83b13a910247f
1 /* Read KConfig() entries - for use in shell scripts.
2 * (c) 2001 Red Hat, Inc.
3 * Programmed by Bernhard Rosenkraenzer <bero@redhat.com>
5 * If --type is specified as bool, the return value is 0 if the value
6 * is set, 1 if it isn't set. There is no output.
8 * If --type is specified as num, the return value matches the value
9 * of the key. There is no output.
11 * If --type is not set, the value of the key is simply printed to stdout.
13 * Usage examples:
14 * if kreadconfig --group KDE --key macStyle --type bool; then
15 * echo "We're using Mac-Style menus."
16 * else
17 * echo "We're using normal menus."
18 * fi
20 * TRASH=`kreadconfig --group Paths --key Trash`
21 * if test -n "$TRASH"; then
22 * mv someFile "$TRASH"
23 * else
24 * rm someFile
25 * fi
27 #include <kconfig.h>
28 #include <kconfiggroup.h>
29 #include <kglobal.h>
30 #include <kapplication.h>
31 #include <kcmdlineargs.h>
32 #include <klocale.h>
33 #include <kaboutdata.h>
34 #include <stdio.h>
35 int main(int argc, char **argv)
37 KAboutData aboutData("kreadconfig", 0, ki18n("KReadConfig"),
38 "1.0.1",
39 ki18n("Read KConfig entries - for use in shell scripts"),
40 KAboutData::License_GPL,
41 ki18n("(c) 2001 Red Hat, Inc."));
42 aboutData.addAuthor(ki18n("Bernhard Rosenkraenzer"), KLocalizedString(), "bero@redhat.com");
43 KCmdLineArgs::init(argc, argv, &aboutData);
45 KCmdLineOptions options;
46 options.add("file <file>", ki18n("Use <file> instead of global config"));
47 options.add("group <group>", ki18n("Group to look in"), "KDE");
48 options.add("key <key>", ki18n("Key to look for"));
49 options.add("default <default>", ki18n("Default value"));
50 options.add("type <type>", ki18n("Type of variable"));
51 KCmdLineArgs::addCmdLineOptions(options);
52 KCmdLineArgs *args=KCmdLineArgs::parsedArgs();
54 QString group=args->getOption("group");
55 QString key=args->getOption("key");
56 QString file=args->getOption("file");
57 QString dflt=args->getOption("default");
58 QString type=args->getOption("type").toLower();
60 if (key.isNull()) {
61 KCmdLineArgs::usage();
62 return 1;
65 KComponentData inst(&aboutData);
66 KGlobal::config();
68 KConfig *konfig;
69 bool configMustDeleted = false;
70 if (file.isEmpty())
71 konfig = KGlobal::config().data();
72 else
74 konfig = new KConfig( file, KConfig::NoGlobals );
75 configMustDeleted=true;
77 KConfigGroup cfgGroup = konfig->group(group);
78 if(type=="bool") {
79 dflt=dflt.toLower();
80 bool def=(dflt=="true" || dflt=="on" || dflt=="yes" || dflt=="1");
81 bool retValue = !cfgGroup.readEntry(key, def);
82 if ( configMustDeleted )
83 delete konfig;
84 return retValue;
85 } else if((type=="num") || (type=="int")) {
86 int retValue = cfgGroup.readEntry(key, dflt.toInt());
87 if ( configMustDeleted )
88 delete konfig;
89 return retValue;
90 } else if (type=="path"){
91 fprintf(stdout, "%s\n", cfgGroup.readPathEntry(key, dflt).toLocal8Bit().data());
92 if ( configMustDeleted )
93 delete konfig;
94 return 0;
95 } else {
96 /* Assume it's a string... */
97 fprintf(stdout, "%s\n", cfgGroup.readEntry(key, dflt).toLocal8Bit().data());
98 if ( configMustDeleted )
99 delete konfig;
100 return 0;