not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kstartupconfig / kdostartupconfig.cpp
blobfb704ec3afb96c7626c4c7adfff6bdcbe968f116
1 /****************************************************************************
3 Copyright (C) 2005 Lubos Lunak <l.lunak@kde.org>
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:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
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.
23 ****************************************************************************/
25 #undef QT_NO_CAST_ASCII
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>
40 #if defined _WIN32 || defined _WIN64
41 #define KPATH_SEPARATOR ';'
42 #else
43 #define KPATH_SEPARATOR ':'
44 #endif
46 static QString get_entry( QString* ll )
48 QString& l = *ll;
49 l = l.trimmed();
50 if( l.isEmpty())
51 return QString();
52 QString ret;
53 if( l[ 0 ] == '\'' )
55 int pos = 1;
56 while( pos < l.length() && l[ pos ] != '\'' )
57 ret += l[ pos++ ];
58 if( pos >= l.length())
60 *ll = QString();
61 return QString();
63 *ll = l.mid( pos + 1 );
64 return ret;
66 int pos = 0;
67 while( pos < l.length() && l[ pos ] != ' ' )
68 ret += l[ pos++ ];
69 *ll = l.mid( pos );
70 return ret;
73 int main( int argc, char **argv )
75 #define I18N_NOEXTRACT( x ) ki18n( x )
76 // Set catalog to "kdelibs4" for KLocale to initialize languages properly.
77 KAboutData about( "kdostartupconfig4", "kdelibs4",
78 I18N_NOEXTRACT( "kdostartupconfig4" ), "1.0" );
79 KComponentData inst( &about );
80 kDebug() << "Running kdostartupconfig.";
81 KCmdLineArgs::init( argc, argv, &about ); // for KLocale not to complain about encoding
82 QString keysname = KStandardDirs::locateLocal( "config", "startupconfigkeys" );
83 QFile keys( keysname );
84 if( !keys.open( QIODevice::ReadOnly ))
85 return 3;
86 QFile f1( KStandardDirs::locateLocal( "config", "startupconfig" ));
87 if( !f1.open( QIODevice::WriteOnly ))
88 return 4;
89 QFile f2( KStandardDirs::locateLocal( "config", "startupconfigfiles" ));
90 if( !f2.open( QIODevice::WriteOnly ))
91 return 5;
92 QTextStream startupconfig( &f1 );
93 QTextStream startupconfigfiles( &f2 );
94 startupconfig << "#! /bin/sh\n";
95 for(;;)
97 QString line;
99 QByteArray buf;
100 buf.resize(1024);
101 if( keys.readLine( buf.data(), buf.length() ) < 0 )
102 break;
103 line = QString::fromLocal8Bit(buf);
105 line = line.trimmed();
106 if( line.isEmpty())
107 break;
108 QString tmp = line;
109 QString file, group, key, def;
110 file = get_entry( &tmp );
111 group = get_entry( &tmp );
112 key = get_entry( &tmp );
113 def = get_entry( &tmp );
114 if( file.isEmpty() || group.isEmpty())
115 return 6;
116 if( group.startsWith( '[' ) && group.endsWith( ']' ) )
117 { // whole config group
118 KConfig cfg( file );
119 group = group.mid( 1, group.length() - 2 );
120 KConfigGroup cg(&cfg, group);
121 QMap< QString, QString > entries = cg.entryMap( );
122 startupconfig << "# " << line << "\n";
123 for( QMap< QString, QString >::ConstIterator it = entries.constBegin();
124 it != entries.constEnd();
125 ++it )
127 QString key = it.key();
128 QString value = *it;
129 startupconfig << file.replace( ' ', '_' ).toLower()
130 << "_" << group.replace( ' ', '_' ).toLower()
131 << "_" << key.replace( ' ', '_' ).toLower()
132 << "=\"" << value.replace( "\"", "\\\"" ) << "\"\n";
135 else
136 { // a single key
137 if( key.isEmpty())
138 return 7;
139 KConfig cfg( file );
140 KConfigGroup cg(&cfg, group );
141 QString value = cg.readEntry( key, def );
142 startupconfig << "# " << line << "\n";
143 startupconfig << file.replace( ' ', '_' ).toLower()
144 << "_" << group.replace( ' ', '_' ).toLower()
145 << "_" << key.replace( ' ', '_' ).toLower()
146 << "=\"" << value.replace( "\"", "\\\"" ) << "\"\n";
148 startupconfigfiles << line << endl;
149 // use even currently non-existing paths in $KDEDIRS
150 const QStringList dirs = KGlobal::dirs()->kfsstnd_prefixes().split( KPATH_SEPARATOR, QString::SkipEmptyParts);
151 for( QStringList::ConstIterator it = dirs.constBegin();
152 it != dirs.constEnd();
153 ++it )
155 QString cfg = *it + "share/config/" + file;
156 if( KStandardDirs::exists( cfg ))
157 startupconfigfiles << cfg << "\n";
158 else
159 startupconfigfiles << "!" << cfg << "\n";
161 startupconfigfiles << "*\n";
164 // Get languages by priority from KLocale.
165 QStringList langs = KGlobal::locale()->languageList();
166 startupconfig << "klocale_languages=" << langs.join( ":" ) << "\n";
167 return 0;