1 | /**************************************************************************** |
2 | |
3 | Copyright (C) 2005 Lubos Lunak <l.lunak@kde.org> |
4 | |
5 | Permission is hereby granted, free of charge, to any person obtaining a |
6 | copy of this software and associated documentation files (the "Software"), |
7 | to deal in the Software without restriction, including without limitation |
8 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9 | and/or sell copies of the Software, and to permit persons to whom the |
10 | Software is furnished to do so, subject to the following conditions: |
11 | |
12 | The above copyright notice and this permission notice shall be included in |
13 | all copies or substantial portions of the Software. |
14 | |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21 | DEALINGS IN THE SOFTWARE. |
22 | |
23 | ****************************************************************************/ |
24 | |
25 | #undef QT_NO_CAST_ASCII |
26 | |
27 | // See description in kstartupconfig.cpp . |
28 | #include <QtCore/QFile> |
29 | #include <QtCore/QTextStream> |
30 | #include <kcomponentdata.h> |
31 | #include <kstandarddirs.h> |
32 | #include <kconfig.h> |
33 | #include <kconfiggroup.h> |
34 | #include <kdebug.h> |
35 | #include <kaboutdata.h> |
36 | #include <kcmdlineargs.h> |
37 | #include <klocale.h> |
38 | #include <kshell.h> |
39 | |
40 | |
41 | #if defined _WIN32 || defined _WIN64 |
42 | #define KPATH_SEPARATOR ';' |
43 | #else |
44 | #define KPATH_SEPARATOR ':' |
45 | #endif |
46 | |
47 | static QString get_entry( QString* ll ) |
48 | { |
49 | QString& l = *ll; |
50 | l = l.trimmed(); |
51 | if( l.isEmpty()) |
52 | return QString(); |
53 | QString ret; |
54 | if( l[ 0 ] == '\'' ) |
55 | { |
56 | int pos = 1; |
57 | while( pos < l.length() && l[ pos ] != '\'' ) |
58 | ret += l[ pos++ ]; |
59 | if( pos >= l.length()) |
60 | { |
61 | *ll = QString(); |
62 | return QString(); |
63 | } |
64 | *ll = l.mid( pos + 1 ); |
65 | return ret; |
66 | } |
67 | int pos = 0; |
68 | while( pos < l.length() && l[ pos ] != ' ' ) |
69 | ret += l[ pos++ ]; |
70 | *ll = l.mid( pos ); |
71 | return ret; |
72 | } |
73 | |
74 | int main( int argc, char **argv ) |
75 | { |
76 | #define I18N_NOEXTRACT( x ) ki18n( x ) |
77 | // Set catalog to "kdelibs4" for KLocale to initialize languages properly. |
78 | KAboutData about( "kdostartupconfig4" , "kdelibs4" , |
79 | I18N_NOEXTRACT( "kdostartupconfig4" ), "1.0" ); |
80 | KComponentData inst( &about ); |
81 | kDebug() << "Running kdostartupconfig." ; |
82 | KCmdLineArgs::init( argc, argv, &about ); // for KLocale not to complain about encoding |
83 | QString keysname = KStandardDirs::locateLocal( "config" , "startupconfigkeys" ); |
84 | QFile keys( keysname ); |
85 | if( !keys.open( QIODevice::ReadOnly )) |
86 | return 3; |
87 | QFile f1( KStandardDirs::locateLocal( "config" , "startupconfig" )); |
88 | if( !f1.open( QIODevice::WriteOnly )) |
89 | return 4; |
90 | QFile f2( KStandardDirs::locateLocal( "config" , "startupconfigfiles" )); |
91 | if( !f2.open( QIODevice::WriteOnly )) |
92 | return 5; |
93 | QTextStream startupconfig( &f1 ); |
94 | QTextStream startupconfigfiles( &f2 ); |
95 | startupconfig << "#! /bin/sh\n" ; |
96 | for(;;) |
97 | { |
98 | QString line; |
99 | { |
100 | QByteArray buf; |
101 | buf.resize(1024); |
102 | if( keys.readLine( buf.data(), buf.length() ) < 0 ) |
103 | break; |
104 | line = QString::fromLocal8Bit(buf); |
105 | } |
106 | line = line.trimmed(); |
107 | if( line.isEmpty()) |
108 | break; |
109 | QString tmp = line; |
110 | QString file, group, key, def; |
111 | file = get_entry( &tmp ); |
112 | group = get_entry( &tmp ); |
113 | key = get_entry( &tmp ); |
114 | def = get_entry( &tmp ); |
115 | if( file.isEmpty() || group.isEmpty()) |
116 | return 6; |
117 | if( group.startsWith( '[' ) && group.endsWith( ']' ) ) |
118 | { // whole config group |
119 | KConfig cfg( file ); |
120 | group = group.mid( 1, group.length() - 2 ); |
121 | KConfigGroup cg(&cfg, group); |
122 | QMap< QString, QString > entries = cg.entryMap( ); |
123 | startupconfig << "# " << line << "\n" ; |
124 | for( QMap< QString, QString >::ConstIterator it = entries.constBegin(); |
125 | it != entries.constEnd(); |
126 | ++it ) |
127 | { |
128 | QString key = it.key(); |
129 | QString value = *it; |
130 | startupconfig << file.replace( ' ', '_' ).toLower() |
131 | << "_" << group.replace( ' ', '_' ).toLower() |
132 | << "_" << key.replace( ' ', '_' ).toLower() |
133 | << "=" << KShell::quoteArg( value ) << "\n" ; |
134 | } |
135 | } |
136 | else |
137 | { // a single key |
138 | if( key.isEmpty()) |
139 | return 7; |
140 | KConfig cfg( file ); |
141 | KConfigGroup cg(&cfg, group ); |
142 | QString value = cg.readEntry( key, def ); |
143 | startupconfig << "# " << line << "\n" ; |
144 | startupconfig << file.replace( ' ', '_' ).toLower() |
145 | << "_" << group.replace( ' ', '_' ).toLower() |
146 | << "_" << key.replace( ' ', '_' ).toLower() |
147 | << "=" << KShell::quoteArg( value ) << "\n" ; |
148 | } |
149 | startupconfigfiles << line << endl; |
150 | // use even currently non-existing paths in $KDEDIRS |
151 | const QStringList dirs = KGlobal::dirs()->kfsstnd_prefixes().split( KPATH_SEPARATOR, QString::SkipEmptyParts); |
152 | for( QStringList::ConstIterator it = dirs.constBegin(); |
153 | it != dirs.constEnd(); |
154 | ++it ) |
155 | { |
156 | QString cfg = *it + "share/config/" + file; |
157 | if( KStandardDirs::exists( cfg )) |
158 | startupconfigfiles << cfg << "\n" ; |
159 | else |
160 | startupconfigfiles << "!" << cfg << "\n" ; |
161 | } |
162 | startupconfigfiles << "*\n" ; |
163 | } |
164 | |
165 | // Get languages by priority from KLocale. |
166 | const QStringList langs = KGlobal::locale()->languageList(); |
167 | startupconfig << "klocale_languages=" << langs.join( ":" ) << "\n" ; |
168 | return 0; |
169 | } |
170 | |