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>
41
#if defined _WIN32 || defined _WIN64
42
#define KPATH_SEPARATOR ';'
44
#define KPATH_SEPARATOR ':'
47
static QString get_entry( QString* ll )
57
while( pos < l.length() && l[ pos ] != '\'' )
59
if( pos >= l.length())
64
*ll = l.mid( pos + 1 );
68
while( pos < l.length() && l[ pos ] != ' ' )
74
int main( int argc, char **argv )
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 ))
87
QFile f1( KStandardDirs::locateLocal( "config", "startupconfig" ));
88
if( !f1.open( QIODevice::WriteOnly ))
90
QFile f2( KStandardDirs::locateLocal( "config", "startupconfigfiles" ));
91
if( !f2.open( QIODevice::WriteOnly ))
93
QTextStream startupconfig( &f1 );
94
QTextStream startupconfigfiles( &f2 );
95
startupconfig << "#! /bin/sh\n";
102
if( keys.readLine( buf.data(), buf.length() ) < 0 )
104
line = QString::fromLocal8Bit(buf);
106
line = line.trimmed();
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())
117
if( group.startsWith( '[' ) && group.endsWith( ']' ) )
118
{ // whole config group
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();
128
QString key = it.key();
130
startupconfig << file.replace( ' ', '_' ).toLower()
131
<< "_" << group.replace( ' ', '_' ).toLower()
132
<< "_" << key.replace( ' ', '_' ).toLower()
133
<< "=" << KShell::quoteArg( value ) << "\n";
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";
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();
156
QString cfg = *it + "share/config/" + file;
157
if( KStandardDirs::exists( cfg ))
158
startupconfigfiles << cfg << "\n";
160
startupconfigfiles << "!" << cfg << "\n";
162
startupconfigfiles << "*\n";
165
// Get languages by priority from KLocale.
166
const QStringList langs = KGlobal::locale()->languageList();
167
startupconfig << "klocale_languages=" << langs.join( ":" ) << "\n";