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.
14 * if kreadconfig --group KDE --key macStyle --type bool; then
15 * echo "We're using Mac-Style menus."
17 * echo "We're using normal menus."
20 * TRASH=`kreadconfig --group Paths --key Trash`
21 * if test -n "$TRASH"; then
22 * mv someFile "$TRASH"
28 #include <kconfiggroup.h>
30 #include <kapplication.h>
31 #include <kcmdlineargs.h>
33 #include <kaboutdata.h>
35 int main(int argc
, char **argv
)
37 KAboutData
aboutData("kreadconfig", 0, ki18n("KReadConfig"),
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();
61 KCmdLineArgs::usage();
65 KComponentData
inst(&aboutData
);
69 bool configMustDeleted
= false;
71 konfig
= KGlobal::config().data();
74 konfig
= new KConfig( file
, KConfig::NoGlobals
);
75 configMustDeleted
=true;
77 KConfigGroup cfgGroup
= konfig
->group(group
);
80 bool def
=(dflt
=="true" || dflt
=="on" || dflt
=="yes" || dflt
=="1");
81 bool retValue
= !cfgGroup
.readEntry(key
, def
);
82 if ( configMustDeleted
)
85 } else if((type
=="num") || (type
=="int")) {
86 int retValue
= cfgGroup
.readEntry(key
, dflt
.toInt());
87 if ( configMustDeleted
)
90 } else if (type
=="path"){
91 fprintf(stdout
, "%s\n", cfgGroup
.readPathEntry(key
, dflt
).toLocal8Bit().data());
92 if ( configMustDeleted
)
96 /* Assume it's a string... */
97 fprintf(stdout
, "%s\n", cfgGroup
.readEntry(key
, dflt
).toLocal8Bit().data());
98 if ( configMustDeleted
)