1 | /* Write KConfig() entries - for use in shell scripts. |
2 | |
3 | Copyright (c) 2001 Red Hat, Inc. |
4 | Copyright (c) 2001 Luís Pedro Coelho <luis_pedro@netcabo.pt> |
5 | |
6 | Programmed by Luís Pedro Coelho <luis_pedro@netcabo.pt> |
7 | based on kreadconfig by Bernhard Rosenkraenzer <bero@redhat.com> |
8 | |
9 | This program is free software; you can redistribute it and/or |
10 | modify it under the terms of the GNU General Public License as |
11 | published by the Free Software Foundation; either version 2 of |
12 | the License, or (at your option) any later version. |
13 | |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | */ |
22 | |
23 | #include <kconfig.h> |
24 | #include <kconfiggroup.h> |
25 | #include <kglobal.h> |
26 | #include <kapplication.h> |
27 | #include <kcmdlineargs.h> |
28 | #include <klocale.h> |
29 | #include <kaboutdata.h> |
30 | #include <stdio.h> |
31 | //Added by qt3to4: |
32 | |
33 | int main(int argc, char **argv) |
34 | { |
35 | KAboutData aboutData("kwriteconfig" , 0, ki18n("KWriteConfig" ), |
36 | "1.0.0" , |
37 | ki18n("Write KConfig entries - for use in shell scripts" ), |
38 | KAboutData::License_GPL, |
39 | ki18n("(c) 2001 Red Hat, Inc. & Luís Pedro Coelho" )); |
40 | aboutData.addAuthor(ki18n("Luís Pedro Coelho" ), KLocalizedString(), "luis_pedro@netcabo.pt" ); |
41 | aboutData.addAuthor(ki18n("Bernhard Rosenkraenzer" ), ki18n("Wrote kreadconfig on which this is based" ), "bero@redhat.com" ); |
42 | KCmdLineArgs::init(argc, argv, &aboutData); |
43 | |
44 | QCoreApplication app(argc, argv); |
45 | |
46 | KCmdLineOptions options; |
47 | options.add("file <file>" , ki18n("Use <file> instead of global config" )); |
48 | options.add("group <group>" , ki18n("Group to look in. Use repeatedly for nested groups." ), "KDE" ); |
49 | options.add("key <key>" , ki18n("Key to look for" )); |
50 | options.add("type <type>" , ki18n("Type of variable. Use \"bool\" for a boolean, otherwise it is treated as a string" )); |
51 | options.add("+value" , ki18n( "The value to write. Mandatory, on a shell use '' for empty" )); |
52 | KCmdLineArgs::addCmdLineOptions(options); |
53 | KCmdLineArgs *args=KCmdLineArgs::parsedArgs(); |
54 | |
55 | QStringList groups=args->getOptionList("group" ); |
56 | QString key=args->getOption("key" ); |
57 | QString file=args->getOption("file" ); |
58 | QString type=args->getOption("type" ).toLower(); |
59 | |
60 | |
61 | if (key.isNull() || !args->count()) { |
62 | KCmdLineArgs::usage(); |
63 | return 1; |
64 | } |
65 | QByteArray value = args->arg( 0 ).toLocal8Bit(); |
66 | |
67 | KComponentData inst(&aboutData); |
68 | |
69 | KConfig *konfig; |
70 | if (file.isEmpty()) |
71 | konfig = new KConfig(QString::fromLatin1( "kdeglobals" ), KConfig::NoGlobals ); |
72 | else |
73 | konfig = new KConfig( file, KConfig::NoGlobals ); |
74 | |
75 | KConfigGroup cfgGroup = konfig->group("" ); |
76 | foreach (const QString &grp, groups) |
77 | cfgGroup = cfgGroup.group(grp); |
78 | if ( konfig->accessMode() != KConfig::ReadWrite || cfgGroup.isEntryImmutable( key ) ) return 2; |
79 | |
80 | if(type=="bool" ) { |
81 | // For symmetry with kreadconfig we accept a wider range of values as true than Qt |
82 | bool boolvalue=(value=="true" || value=="on" || value=="yes" || value=="1" ); |
83 | cfgGroup.writeEntry( key, boolvalue ); |
84 | } else if (type=="path" ) { |
85 | cfgGroup.writePathEntry( key, QString::fromLocal8Bit( value ) ); |
86 | } else { |
87 | cfgGroup.writeEntry( key, QString::fromLocal8Bit( value ) ); |
88 | } |
89 | konfig->sync(); |
90 | delete konfig; |
91 | return 0; |
92 | } |
93 | |
94 | |