not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kstartupconfig / kstartupconfig.cpp
blobba452abe4ec8a1182b63043cea63fd418c795d30
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 ****************************************************************************/
27 This utility helps to have some configuration options available in startkde
28 without the need to launch anything linked to KDE libraries (which may need
29 some time to load).
31 The configuration options are written to $KDEHOME/share/config/startupconfigkeys,
32 one option per line, as <file> <group> <key> <default>. It is possible to
33 use ' for quoting multiword entries. Values of these options will be written
34 to $KDEHOME/share/config/startupconfig as a shell script that will set
35 the values to shell variables, named <file>_<group>_<key> (all spaces replaced
36 by underscores, everything lowercase). So e.g. line
37 "ksplashrc KSplash Theme Default" may result in "ksplashrc_ksplash_theme=Default".
39 In order to real a whole group it is possible to use <file> <[group]>, e.g.
40 "ksplashrc [KSplash]", which will set shell variables for all keys in the group.
41 It is not possible to specify default values, but since the configuration options
42 are processed in the order they are specified this can be solved by first
43 specifying a group and then all the entries that need default values.
45 When a kconf_update script is used to update such option, kstartupconfig is run
46 before kconf_update and therefore cannot see the change in time. To avoid this
47 problem, together with the kconf_update script also the matching global config
48 file should be updated (any change, kstartupconfig will see the timestamp change).
50 Note that the kdeglobals config file is not used as a depedendency for other config
51 files.
53 Since the checking is timestamp-based, config files that are frequently updated
54 should not be used.
56 Kstartupconfig works by storing every line from startupconfigkeys in file startupconfigfiles
57 followed by paths of all files that are relevant to the option. Non-existent files
58 have '!' prepended (for the case they'll be later created), the list of files is
59 terminated by line containing '*'. If the timestamps of all relevant files are older
60 than the timestamp of the startupconfigfile file, there's no need to update anything.
61 Otherwise kdostartupconfig is launched to create or update all the necessary files
62 (which already requires loading KDE libraries, but this case should be rare).
66 #include <kdefakes.h>
68 #include <config-workspace.h>
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <sys/wait.h>
73 #include <unistd.h>
74 #include <stdio.h>
75 #include <string.h>
76 #include <stdlib.h>
78 int main()
80 char kdehome[ 1024 ];
81 if( getenv( "KDEHOME" ))
82 strlcpy( kdehome, getenv( "KDEHOME" ), 1024 );
83 else if( getenv( "HOME" ))
85 strlcpy( kdehome, getenv( "HOME" ), 1024 );
86 strlcat( kdehome, "/" KDE_DEFAULT_HOME, 1024 );
88 else
89 return 1;
90 char filename[ 1024 ];
91 strlcpy( filename, kdehome, 1024 );
92 strlcat( filename, "/share/config/startupconfig", 1024 );
93 if( access( filename, R_OK ) != 0 )
95 int ret = system( "kdostartupconfig4" );
96 return WEXITSTATUS( ret );
98 strlcpy( filename, kdehome, 1024 );
99 strlcat( filename, "/share/config/startupconfigfiles", 1024 );
100 struct stat st;
101 if( stat( filename, &st ) != 0 )
103 int ret = system( "kdostartupconfig4" );
104 return WEXITSTATUS( ret );
106 time_t config_time = st.st_mtime;
107 FILE* config = fopen( filename, "r" );
108 if( config == NULL )
110 int ret = system( "kdostartupconfig4" );
111 return WEXITSTATUS( ret );
113 strlcpy( filename, kdehome, 1024 );
114 strlcat( filename, "/share/config/startupconfigkeys", 1024 );
115 FILE* keys = fopen( filename, "r" );
116 if( keys == NULL )
118 fclose( config );
119 return 2;
121 bool need_update = true;
122 for(;;)
124 char keyline[ 1024 ];
125 if( fgets( keyline, 1023, keys ) == NULL )
127 need_update = false;
128 break;
130 if( char* nl = strchr( keyline, '\n' ))
131 *nl = '\0';
132 char line[ 1024 ];
133 if( fgets( line, 1023, config ) == NULL )
134 break;
135 if( char* nl = strchr( line, '\n' ))
136 *nl = '\0';
137 if( strcmp( keyline, line ) != 0 )
138 break;
139 bool ok = false;
140 for(;;)
142 if( fgets( line, 1023, config ) == NULL )
143 break;
144 if( char* nl = strchr( line, '\n' ))
145 *nl = '\0';
146 if( *line == '\0' )
147 break;
148 if( *line == '*' )
150 ok = true;
151 break;
153 if( *line == '!' )
155 if( access( line + 1, R_OK ) == 0 )
156 break; // file now exists -> update
158 else
160 struct stat st;
161 if( stat( line, &st ) != 0 )
162 break;
163 if( st.st_mtime > config_time )
164 break;
167 if( !ok )
168 break;
170 fclose( keys );
171 fclose( config );
172 if( need_update )
174 int ret = system( "kdostartupconfig4" );
175 return WEXITSTATUS( ret );
177 return 0;