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>
33 #include <kconfiggroup.h>
35 #include <kaboutdata.h>
36 #include <kcmdlineargs.h>
40 #if defined _WIN32 || defined _WIN64
41 #define KPATH_SEPARATOR ';'
43 #define KPATH_SEPARATOR ':'
46 static QString
get_entry( QString
* ll
)
56 while( pos
< l
.length() && l
[ pos
] != '\'' )
58 if( pos
>= l
.length())
63 *ll
= l
.mid( pos
+ 1 );
67 while( pos
< l
.length() && l
[ pos
] != ' ' )
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
))
86 QFile
f1( KStandardDirs::locateLocal( "config", "startupconfig" ));
87 if( !f1
.open( QIODevice::WriteOnly
))
89 QFile
f2( KStandardDirs::locateLocal( "config", "startupconfigfiles" ));
90 if( !f2
.open( QIODevice::WriteOnly
))
92 QTextStream
startupconfig( &f1
);
93 QTextStream
startupconfigfiles( &f2
);
94 startupconfig
<< "#! /bin/sh\n";
101 if( keys
.readLine( buf
.data(), buf
.length() ) < 0 )
103 line
= QString::fromLocal8Bit(buf
);
105 line
= line
.trimmed();
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())
116 if( group
.startsWith( '[' ) && group
.endsWith( ']' ) )
117 { // whole config group
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();
127 QString key
= it
.key();
129 startupconfig
<< file
.replace( ' ', '_' ).toLower()
130 << "_" << group
.replace( ' ', '_' ).toLower()
131 << "_" << key
.replace( ' ', '_' ).toLower()
132 << "=\"" << value
.replace( "\"", "\\\"" ) << "\"\n";
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();
155 QString cfg
= *it
+ "share/config/" + file
;
156 if( KStandardDirs::exists( cfg
))
157 startupconfigfiles
<< cfg
<< "\n";
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";