1 | /* Read KConfig() entries - for use in shell scripts. |
2 | |
3 | Copyright (c) 2001 Red Hat, Inc. |
4 | |
5 | Programmed by Bernhard Rosenkraenzer <bero@redhat.com> |
6 | |
7 | This program is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU General Public License as |
9 | published by the Free Software Foundation; either version 2 of |
10 | the License, or (at your option) any later version. |
11 | |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | */ |
20 | |
21 | /* |
22 | * If --type is specified as bool, the return value is 0 if the value |
23 | * is set, 1 if it isn't set. There is no output. |
24 | * |
25 | * If --type is specified as num, the return value matches the value |
26 | * of the key. There is no output. |
27 | * |
28 | * If --type is not set, the value of the key is simply printed to stdout. |
29 | * |
30 | * Usage examples: |
31 | * if kreadconfig --group KDE --key macStyle --type bool; then |
32 | * echo "We're using Mac-Style menus." |
33 | * else |
34 | * echo "We're using normal menus." |
35 | * fi |
36 | * |
37 | * TRASH=`kreadconfig --group Paths --key Trash` |
38 | * if test -n "$TRASH"; then |
39 | * mv someFile "$TRASH" |
40 | * else |
41 | * rm someFile |
42 | * fi |
43 | */ |
44 | #include <kconfig.h> |
45 | #include <kconfiggroup.h> |
46 | #include <kglobal.h> |
47 | #include <kapplication.h> |
48 | #include <kcmdlineargs.h> |
49 | #include <klocale.h> |
50 | #include <kaboutdata.h> |
51 | #include <stdio.h> |
52 | int main(int argc, char **argv) |
53 | { |
54 | KAboutData aboutData("kreadconfig" , 0, ki18n("KReadConfig" ), |
55 | "1.0.1" , |
56 | ki18n("Read KConfig entries - for use in shell scripts" ), |
57 | KAboutData::License_GPL, |
58 | ki18n("(c) 2001 Red Hat, Inc." )); |
59 | aboutData.addAuthor(ki18n("Bernhard Rosenkraenzer" ), KLocalizedString(), "bero@redhat.com" ); |
60 | KCmdLineArgs::init(argc, argv, &aboutData); |
61 | |
62 | QCoreApplication app(argc, argv); |
63 | |
64 | KCmdLineOptions options; |
65 | options.add("file <file>" , ki18n("Use <file> instead of global config" )); |
66 | options.add("group <group>" , ki18n("Group to look in. Use repeatedly for nested groups." ), "KDE" ); |
67 | options.add("key <key>" , ki18n("Key to look for" )); |
68 | options.add("default <default>" , ki18n("Default value" )); |
69 | options.add("type <type>" , ki18n("Type of variable" )); |
70 | KCmdLineArgs::addCmdLineOptions(options); |
71 | KCmdLineArgs *args=KCmdLineArgs::parsedArgs(); |
72 | |
73 | QStringList groups=args->getOptionList("group" ); |
74 | QString key=args->getOption("key" ); |
75 | QString file=args->getOption("file" ); |
76 | QString dflt=args->getOption("default" ); |
77 | QString type=args->getOption("type" ).toLower(); |
78 | |
79 | if (key.isNull()) { |
80 | KCmdLineArgs::usage(); |
81 | return 1; |
82 | } |
83 | |
84 | KComponentData inst(&aboutData); |
85 | KGlobal::config(); |
86 | |
87 | KConfig *konfig; |
88 | bool configMustDeleted = false; |
89 | if (file.isEmpty()) |
90 | konfig = KGlobal::config().data(); |
91 | else |
92 | { |
93 | konfig = new KConfig( file, KConfig::NoGlobals ); |
94 | configMustDeleted=true; |
95 | } |
96 | KConfigGroup cfgGroup = konfig->group("" ); |
97 | foreach (const QString &grp, groups) |
98 | cfgGroup = cfgGroup.group(grp); |
99 | if(type=="bool" ) { |
100 | dflt=dflt.toLower(); |
101 | bool def=(dflt=="true" || dflt=="on" || dflt=="yes" || dflt=="1" ); |
102 | bool retValue = !cfgGroup.readEntry(key, def); |
103 | if ( configMustDeleted ) |
104 | delete konfig; |
105 | return retValue; |
106 | } else if((type=="num" ) || (type=="int" )) { |
107 | int retValue = cfgGroup.readEntry(key, dflt.toInt()); |
108 | if ( configMustDeleted ) |
109 | delete konfig; |
110 | return retValue; |
111 | } else if (type=="path" ){ |
112 | fprintf(stdout, "%s\n" , cfgGroup.readPathEntry(key, dflt).toLocal8Bit().data()); |
113 | if ( configMustDeleted ) |
114 | delete konfig; |
115 | return 0; |
116 | } else { |
117 | /* Assume it's a string... */ |
118 | fprintf(stdout, "%s\n" , cfgGroup.readEntry(key, dflt).toLocal8Bit().data()); |
119 | if ( configMustDeleted ) |
120 | delete konfig; |
121 | return 0; |
122 | } |
123 | } |
124 | |
125 | |