~cairo-dock-team/ubuntu/quantal/cairo-dock/3.0.2

« back to all changes in this revision

Viewing changes to src/gldit/cairo-dock-keyfile-utilities.c

  • Committer: Kees Cook
  • Date: 2011-08-11 19:31:26 UTC
  • mfrom: (19.1.1 cairo-dock)
  • Revision ID: kees@outflux.net-20110811193126-wh97aamdqx5gaf2f
Tags: 2.4.0~0beta2-0ubuntu1
releasing version 2.4.0~0beta2-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
}
74
74
 
75
75
 
76
 
void cairo_dock_flush_conf_file_full (GKeyFile *pKeyFile, const gchar *cConfFilePath, const gchar *cShareDataDirPath, gboolean bUseFileKeys, const gchar *cTemplateFileName)
77
 
{
78
 
        gchar *cTemplateConfFilePath = (*cTemplateFileName == '/' ? g_strdup (cTemplateFileName) : g_strdup_printf ("%s/%s", cShareDataDirPath, cTemplateFileName));
79
 
        cd_message ("%s (%s)", __func__, cTemplateConfFilePath);
80
 
        
81
 
        if (! g_file_test (cTemplateConfFilePath, G_FILE_TEST_EXISTS))
82
 
        {
83
 
                cd_warning ("Couldn't find any installed conf file in %s", cShareDataDirPath);
84
 
        }
85
 
        else
86
 
        {
87
 
                gchar *cCommand = g_strdup_printf ("/bin/cp \"%s\" \"%s\"", cTemplateConfFilePath, cConfFilePath);
88
 
                int r = system (cCommand);
89
 
                g_free (cCommand);
90
 
                
91
 
                cairo_dock_replace_values_in_conf_file (cConfFilePath, pKeyFile, bUseFileKeys, 0);
92
 
        }
93
 
        g_free (cTemplateConfFilePath);
94
 
}
95
 
 
96
 
 
97
 
 
98
 
void cairo_dock_replace_key_values (GKeyFile *pOriginalKeyFile, GKeyFile *pReplacementKeyFile, gboolean bUseOriginalKeys, gchar iIdentifier)
 
76
// pOriginalKeyFile is an up-to-date key-file
 
77
// pReplacementKeyFile is an old key-file containing values we want to use
 
78
// keys are filtered by the identifier on the original key-file.
 
79
// old keys not present in pOriginalKeyFile are added
 
80
// new keys in pOriginalKeyFile not present in pReplacementKeyFile and having valid comment are removed
 
81
static void cairo_dock_merge_key_files (GKeyFile *pOriginalKeyFile, GKeyFile *pReplacementKeyFile, gchar iIdentifier)
 
82
{
 
83
        // get the groups of the remplacement key-file.
 
84
        GError *erreur = NULL;
 
85
        gsize length = 0;
 
86
        gchar **pKeyList;
 
87
        gchar **pGroupList = g_key_file_get_groups (pReplacementKeyFile, &length);
 
88
        g_return_if_fail (pGroupList != NULL);
 
89
        gchar *cGroupName, *cKeyName, *cKeyValue, *cComment;
 
90
        int i, j;
 
91
        
 
92
        for (i = 0; pGroupList[i] != NULL; i ++)
 
93
        {
 
94
                cGroupName = pGroupList[i];
 
95
                
 
96
                // get the keys of the remplacement key-file.
 
97
                length = 0;
 
98
                pKeyList = g_key_file_get_keys (pReplacementKeyFile, cGroupName, NULL, NULL);
 
99
                g_return_if_fail (pKeyList != NULL);
 
100
                
 
101
                for (j = 0; pKeyList[j] != NULL; j ++)
 
102
                {
 
103
                        cKeyName = pKeyList[j];
 
104
                        
 
105
                        // check that the original identifier matches with the provided one.
 
106
                        if (iIdentifier != 0)
 
107
                        {
 
108
                                if (g_key_file_has_key (pOriginalKeyFile, cGroupName, cKeyName, NULL))  // if the key doesn't exist in the original key-file, don't check the identifier, and add it to the key-file; it probably means it's an old key that will be taken care of by the applet.
 
109
                                {
 
110
                                        cComment = g_key_file_get_comment (pOriginalKeyFile, cGroupName, cKeyName, NULL);
 
111
                                        if (cComment == NULL || cComment[0] == '\0' || cComment[1] != iIdentifier)
 
112
                                        {
 
113
                                                g_free (cComment);
 
114
                                                continue ;
 
115
                                        }
 
116
                                        g_free (cComment);
 
117
                                }
 
118
                        }
 
119
                        
 
120
                        // get the replacement value and set it to the key-file, creating it if it didn't exist (in this case, no need to add the comment, since the key will be removed again by the applet).
 
121
                        cKeyValue =  g_key_file_get_string (pReplacementKeyFile, cGroupName, cKeyName, &erreur);
 
122
                        if (erreur != NULL)
 
123
                        {
 
124
                                cd_warning (erreur->message);
 
125
                                g_error_free (erreur);
 
126
                                erreur = NULL;
 
127
                        }
 
128
                        else
 
129
                        {
 
130
                                if (cKeyValue && cKeyValue[strlen(cKeyValue) - 1] == '\n')
 
131
                                        cKeyValue[strlen(cKeyValue) - 1] = '\0';
 
132
                                g_key_file_set_string (pOriginalKeyFile, cGroupName, cKeyName, (cKeyValue != NULL ? cKeyValue : ""));
 
133
                        }
 
134
                        g_free (cKeyValue);
 
135
                }
 
136
                g_strfreev (pKeyList);
 
137
        }
 
138
        g_strfreev (pGroupList);
 
139
        
 
140
        // remove keys from the original key-file which are not in the remplacement key-file, except hidden and persistent keys.
 
141
        pGroupList = g_key_file_get_groups (pOriginalKeyFile, &length);
 
142
        g_return_if_fail (pGroupList != NULL);
 
143
        for (i = 0; pGroupList[i] != NULL; i ++)
 
144
        {
 
145
                cGroupName = pGroupList[i];
 
146
                
 
147
                // get the keys of the original key-file.
 
148
                length = 0;
 
149
                pKeyList = g_key_file_get_keys (pOriginalKeyFile, cGroupName, NULL, NULL);
 
150
                g_return_if_fail (pKeyList != NULL);
 
151
                
 
152
                for (j = 0; pKeyList[j] != NULL; j ++)
 
153
                {
 
154
                        cKeyName = pKeyList[j];
 
155
                        if (! g_key_file_has_key (pReplacementKeyFile, cGroupName, cKeyName, NULL))
 
156
                        {
 
157
                                cComment = g_key_file_get_comment (pOriginalKeyFile, cGroupName, cKeyName, NULL);
 
158
                                if (cComment != NULL && cComment[0] != '\0' && cComment[1] != '0')  // not hidden nor peristent
 
159
                                {
 
160
                                        g_key_file_remove_comment (pOriginalKeyFile, cGroupName, cKeyName, NULL);
 
161
                                        g_key_file_remove_key (pOriginalKeyFile, cGroupName, cKeyName, NULL);
 
162
                                }
 
163
                        }
 
164
                }
 
165
                g_strfreev (pKeyList);
 
166
        }
 
167
        g_strfreev (pGroupList);
 
168
}
 
169
 
 
170
void cairo_dock_merge_conf_files (const gchar *cConfFilePath, gchar *cReplacementConfFilePath, gchar iIdentifier)
 
171
{
 
172
        GKeyFile *pOriginalKeyFile = cairo_dock_open_key_file (cConfFilePath);
 
173
        g_return_if_fail (pOriginalKeyFile != NULL);
 
174
        GKeyFile *pReplacementKeyFile = cairo_dock_open_key_file (cReplacementConfFilePath);
 
175
        g_return_if_fail (pReplacementKeyFile != NULL);
 
176
        
 
177
        cairo_dock_merge_key_files (pOriginalKeyFile, pReplacementKeyFile, iIdentifier);
 
178
        cairo_dock_write_keys_to_file (pOriginalKeyFile, cConfFilePath);
 
179
        
 
180
        g_key_file_free (pOriginalKeyFile);
 
181
        g_key_file_free (pReplacementKeyFile);
 
182
}
 
183
 
 
184
 
 
185
// update launcher key-file: use ukf keys (= template) => remove old, add news
 
186
// update applet key-file: use vkf keys (= user) if exist in template or NULL/0 comment => keep user keys.
 
187
 
 
188
// pValuesKeyFile is a key-file with correct values, but old comments and possibly missing or old keys.
 
189
// pUptodateKeyFile is a template key-file with default values.
 
190
// bUpdateKeys is TRUE to use up-to-date keys.
 
191
static void _cairo_dock_replace_key_values (GKeyFile *pValuesKeyFile, GKeyFile *pUptodateKeyFile, gboolean bUpdateKeys)
 
192
{
 
193
        GKeyFile *pKeysKeyFile = (bUpdateKeys ? pUptodateKeyFile : pValuesKeyFile);
 
194
        
 
195
        // get the groups.
 
196
        GError *erreur = NULL;
 
197
        gsize length = 0;
 
198
        gchar **pKeyList;
 
199
        gchar **pGroupList = g_key_file_get_groups (pKeysKeyFile, &length);
 
200
        g_return_if_fail (pGroupList != NULL);
 
201
        gchar *cGroupName, *cKeyName, *cKeyValue, *cComment;
 
202
        int i, j;
 
203
        
 
204
        for (i = 0; pGroupList[i] != NULL; i ++)
 
205
        {
 
206
                cGroupName = pGroupList[i];
 
207
                
 
208
                // get the keys.
 
209
                length = 0;
 
210
                pKeyList = g_key_file_get_keys (pKeysKeyFile, cGroupName, NULL, NULL);
 
211
                g_return_if_fail (pKeyList != NULL);
 
212
                
 
213
                for (j = 0; pKeyList[j] != NULL; j ++)
 
214
                {
 
215
                        cKeyName = pKeyList[j];
 
216
                        cComment = NULL;
 
217
                        
 
218
                        // don't add old keys, except if they are hidden or persistent.
 
219
                        if (!g_key_file_has_key (pUptodateKeyFile, cGroupName, cKeyName, NULL))  // old key
 
220
                        {
 
221
                                cComment = g_key_file_get_comment (pValuesKeyFile, cGroupName, cKeyName, NULL);
 
222
                                if (cComment != NULL && cComment[0] != '\0' && cComment[1] != '0')  // not hidden nor persistent => skip it.
 
223
                                {
 
224
                                        g_free (cComment);
 
225
                                        continue;
 
226
                                }
 
227
                        }
 
228
                        
 
229
                        // get the replacement value and set it to the key-file, creating it if it didn't exist.
 
230
                        cKeyValue = g_key_file_get_string (pValuesKeyFile, cGroupName, cKeyName, &erreur);
 
231
                        if (erreur != NULL)  // key doesn't exist
 
232
                        {
 
233
                                cd_warning (erreur->message);
 
234
                                g_error_free (erreur);
 
235
                                erreur = NULL;
 
236
                        }
 
237
                        else
 
238
                        {
 
239
                                g_key_file_set_string (pUptodateKeyFile, cGroupName, cKeyName, (cKeyValue != NULL ? cKeyValue : ""));
 
240
                                if (cComment != NULL)  // if we got the comment, it means the key doesn't exist in the up-to-date key-file, so add it.
 
241
                                        g_key_file_set_comment (pUptodateKeyFile, cGroupName, cKeyName, cComment, NULL);
 
242
                        }
 
243
                        g_free (cKeyValue);
 
244
                        g_free (cComment);
 
245
                }
 
246
                
 
247
                g_strfreev (pKeyList);
 
248
        }
 
249
        g_strfreev (pGroupList);
 
250
        
 
251
}
 
252
 
 
253
void cairo_dock_upgrade_conf_file_full (const gchar *cConfFilePath, GKeyFile *pKeyFile, const gchar *cDefaultConfFilePath, gboolean bUpdateKeys)
 
254
{
 
255
        GKeyFile *pUptodateKeyFile = cairo_dock_open_key_file (cDefaultConfFilePath);
 
256
        g_return_if_fail (pUptodateKeyFile != NULL);
 
257
        
 
258
        _cairo_dock_replace_key_values (pKeyFile, pUptodateKeyFile, bUpdateKeys);
 
259
        
 
260
        cairo_dock_write_keys_to_file (pUptodateKeyFile, cConfFilePath);
 
261
        
 
262
        g_key_file_free (pUptodateKeyFile);
 
263
}
 
264
 
 
265
 
 
266
/**
 
267
// deprecated
 
268
static void cairo_dock_replace_key_values (GKeyFile *pOriginalKeyFile, GKeyFile *pReplacementKeyFile, gboolean bUseOriginalKeys, gchar iIdentifier)
99
269
{
100
270
        //g_print ("%s (%d, %d)\n", __func__, iIdentifier, bUseOriginalKeys);
101
271
        GError *erreur = NULL;
229
399
        }
230
400
}
231
401
 
232
 
 
233
 
void cairo_dock_replace_values_in_conf_file (const gchar *cConfFilePath, GKeyFile *pValidKeyFile, gboolean bUseFileKeys, gchar iIdentifier)
 
402
// deprecated
 
403
static void cairo_dock_replace_values_in_conf_file (const gchar *cConfFilePath, GKeyFile *pValidKeyFile, gboolean bUseFileKeys, gchar iIdentifier)
234
404
{
235
405
        GKeyFile *pConfKeyFile = cairo_dock_open_key_file (cConfFilePath);
236
406
        if (pConfKeyFile == NULL)
244
414
        g_key_file_free (pConfKeyFile);
245
415
}
246
416
 
 
417
// deprecated
 
418
void cairo_dock_flush_conf_file_full (GKeyFile *pKeyFile, const gchar *cConfFilePath, const gchar *cShareDataDirPath, gboolean bUseFileKeys, const gchar *cTemplateFileName)
 
419
{
 
420
        gchar *cTemplateConfFilePath = (*cTemplateFileName == '/' ? g_strdup (cTemplateFileName) : g_strdup_printf ("%s/%s", cShareDataDirPath, cTemplateFileName));
 
421
        cd_message ("%s (%s)", __func__, cTemplateConfFilePath);
 
422
        
 
423
        if (! g_file_test (cTemplateConfFilePath, G_FILE_TEST_EXISTS))
 
424
        {
 
425
                cd_warning ("Couldn't find any installed conf file in %s", cShareDataDirPath);
 
426
        }
 
427
        else
 
428
        {
 
429
                gchar *cCommand = g_strdup_printf ("/bin/cp \"%s\" \"%s\"", cTemplateConfFilePath, cConfFilePath);
 
430
                int r = system (cCommand);
 
431
                g_free (cCommand);
 
432
                
 
433
                cairo_dock_replace_values_in_conf_file (cConfFilePath, pKeyFile, bUseFileKeys, 0);
 
434
        }
 
435
        g_free (cTemplateConfFilePath);
 
436
}
 
437
 
 
438
// deprecated
247
439
void cairo_dock_replace_keys_by_identifier (const gchar *cConfFilePath, gchar *cReplacementConfFilePath, gchar iIdentifier)
248
440
{
249
441
        GKeyFile *pReplacementKeyFile = cairo_dock_open_key_file (cReplacementConfFilePath);
254
446
        cairo_dock_replace_values_in_conf_file (cConfFilePath, pReplacementKeyFile, TRUE, iIdentifier);
255
447
 
256
448
        g_key_file_free (pReplacementKeyFile);
257
 
}
 
449
}*/
258
450
 
259
451
 
260
452
 
287
479
{
288
480
        gchar *cPreviousVersion = NULL;
289
481
        cairo_dock_get_conf_file_version (pKeyFile, &cPreviousVersion);
290
 
        gboolean bNeedsUpdate;
291
 
        if (cPreviousVersion == NULL || strcmp (cPreviousVersion, cVersion) != 0)
292
 
                bNeedsUpdate = TRUE;
293
 
        else
294
 
                bNeedsUpdate = FALSE;
 
482
        gboolean bNeedsUpdate = (cPreviousVersion == NULL || strcmp (cPreviousVersion, cVersion) != 0);
295
483
        
296
484
        g_free (cPreviousVersion);
297
485
        return bNeedsUpdate;
364
552
 
365
553
gboolean cairo_dock_rename_group_in_conf_file (GKeyFile *pKeyFile, const gchar *cGroupName, const gchar *cNewGroupName)
366
554
{
367
 
        if (g_key_file_has_group (pKeyFile, cNewGroupName))
 
555
        if (! g_key_file_has_group (pKeyFile, cGroupName))
368
556
                return FALSE;
369
557
        
370
558
        gchar **pKeyList = g_key_file_get_keys (pKeyFile, cGroupName, NULL, NULL);