tdedostartupconfig.cpp
00001 /**************************************************************************** 00002 00003 Copyright (C) 2005 Lubos Lunak <l.lunak@kde.org> 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a 00006 copy of this software and associated documentation files (the "Software"), 00007 to deal in the Software without restriction, including without limitation 00008 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00009 and/or sell copies of the Software, and to permit persons to whom the 00010 Software is furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00018 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00020 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00021 DEALINGS IN THE SOFTWARE. 00022 00023 ****************************************************************************/ 00024 00025 #undef QT_NO_CAST_ASCII 00026 00027 // See description in tdestartupconfig.cpp . 00028 00029 #include <tqfile.h> 00030 #include <tqtextstream.h> 00031 #include <kinstance.h> 00032 #include <kstandarddirs.h> 00033 #include <tdeconfig.h> 00034 #include <kdebug.h> 00035 00036 TQString get_entry( TQString* ll ) 00037 { 00038 TQString& l = *ll; 00039 l = l.stripWhiteSpace(); 00040 if( l.isEmpty()) 00041 return TQString::null; 00042 TQString ret; 00043 if( l[ 0 ] == '\'' ) 00044 { 00045 unsigned int pos = 1; 00046 while( pos < l.length() && l[ pos ] != '\'' ) 00047 ret += l[ pos++ ]; 00048 if( pos >= l.length()) 00049 { 00050 *ll = TQString::null; 00051 return TQString::null; 00052 } 00053 *ll = l.mid( pos + 1 ); 00054 return ret; 00055 } 00056 unsigned int pos = 0; 00057 while( pos < l.length() && l[ pos ] != ' ' ) 00058 ret += l[ pos++ ]; 00059 *ll = l.mid( pos ); 00060 return ret; 00061 } 00062 00063 int main() 00064 { 00065 TDEInstance inst( "tdedostartupconfig" ); 00066 kdDebug() << "Running tdedostartupconfig." << endl; 00067 TQString keysname = locateLocal( "config", "startupconfigkeys" ); 00068 TQFile keys( keysname ); 00069 if( !keys.open( IO_ReadOnly )) 00070 return 3; 00071 TQFile f1( locateLocal( "config", "startupconfig" )); 00072 if( !f1.open( IO_WriteOnly )) 00073 return 4; 00074 TQFile f2( locateLocal( "config", "startupconfigfiles" )); 00075 if( !f2.open( IO_WriteOnly )) 00076 return 5; 00077 TQTextStream startupconfig( &f1 ); 00078 TQTextStream startupconfigfiles( &f2 ); 00079 startupconfig << "#! /bin/sh\n"; 00080 for(;;) 00081 { 00082 TQString line; 00083 if( keys.readLine( line, 1024 ) < 0 ) 00084 break; 00085 line = line.stripWhiteSpace(); 00086 if( line.isEmpty()) 00087 break; 00088 TQString tmp = line; 00089 TQString file, group, key, def; 00090 file = get_entry( &tmp ); 00091 group = get_entry( &tmp ); 00092 key = get_entry( &tmp ); 00093 def = get_entry( &tmp ); 00094 if( file.isEmpty() || group.isEmpty()) 00095 return 6; 00096 if( group.left( 1 ) == "[" && group.right( 1 ) == "]" ) 00097 { // whole config group 00098 TDEConfig cfg( file ); 00099 group = group.mid( 1, group.length() - 2 ); 00100 TQMap< TQString, TQString > entries = cfg.entryMap( group ); 00101 startupconfig << "# " << line << "\n"; 00102 for( TQMap< TQString, TQString >::ConstIterator it = entries.begin(); 00103 it != entries.end(); 00104 ++it ) 00105 { 00106 TQString key = it.key(); 00107 TQString value = *it; 00108 startupconfig << TQString(file.replace( ' ', '_' )).lower() 00109 << "_" << TQString(group.replace( ' ', '_' )).lower() 00110 << "_" << TQString(key.replace( ' ', '_' )).lower() 00111 << "=\"" << value.replace( "\"", "\\\"" ) << "\"\n"; 00112 } 00113 } 00114 else 00115 { // a single key 00116 if( key.isEmpty()) 00117 return 7; 00118 TDEConfig cfg( file ); 00119 cfg.setGroup( group ); 00120 TQString value = cfg.readEntry( key, def ); 00121 startupconfig << "# " << line << "\n"; 00122 startupconfig << TQString(file.replace( ' ', '_' )).lower() 00123 << "_" << TQString(group.replace( ' ', '_' )).lower() 00124 << "_" <<TQString( key.replace( ' ', '_' )).lower() 00125 << "=\"" << value.replace( "\"", "\\\"" ) << "\"\n"; 00126 } 00127 startupconfigfiles << line << endl; 00128 // use even currently non-existing paths in $TDEDIRS 00129 TQStringList dirs = TQStringList::split( KPATH_SEPARATOR, TDEGlobal::dirs()->kfsstnd_prefixes()); 00130 for( TQStringList::ConstIterator it = dirs.begin(); 00131 it != dirs.end(); 00132 ++it ) 00133 { 00134 TQString cfg = *it + "share/config/" + file; 00135 if( TDEStandardDirs::exists( cfg )) 00136 startupconfigfiles << cfg << "\n"; 00137 else 00138 startupconfigfiles << "!" << cfg << "\n"; 00139 } 00140 startupconfigfiles << "*\n"; 00141 } 00142 return 0; 00143 }