~ubuntu-branches/ubuntu/oneiric/cairo-dock/oneiric

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mto: (18.1.1 cairo-dock) (19.1.1 cairo-dock)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100809232612-pocdxliaxjdetm37
Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include <string.h>
 
21
#include <stdlib.h>
 
22
 
 
23
#include "cairo-dock-log.h"
 
24
#include "cairo-dock-keyfile-utilities.h"
 
25
 
 
26
 
 
27
GKeyFile *cairo_dock_open_key_file (const gchar *cConfFilePath)
 
28
{
 
29
        GKeyFile *pKeyFile = g_key_file_new ();
 
30
        GError *erreur = NULL;
 
31
        g_key_file_load_from_file (pKeyFile, cConfFilePath, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &erreur);
 
32
        if (erreur != NULL)
 
33
        {
 
34
                cd_debug ("while trying to load %s : %s", cConfFilePath, erreur->message);  // on ne met pas de warning car un fichier de conf peut ne pas exister la 1ere fois.
 
35
                g_error_free (erreur);
 
36
                g_key_file_free (pKeyFile);
 
37
                return NULL;
 
38
        }
 
39
        return pKeyFile;
 
40
}
 
41
 
 
42
void cairo_dock_write_keys_to_file (GKeyFile *pKeyFile, const gchar *cConfFilePath)
 
43
{
 
44
        cd_debug ("%s (%s)", __func__, cConfFilePath);
 
45
        GError *erreur = NULL;
 
46
 
 
47
        gchar *cDirectory = g_path_get_dirname (cConfFilePath);
 
48
        if (! g_file_test (cDirectory, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_EXECUTABLE))
 
49
        {
 
50
                g_mkdir_with_parents (cDirectory, 7*8*8+7*8+5);
 
51
        }
 
52
        g_free (cDirectory);
 
53
 
 
54
 
 
55
        gsize length=0;
 
56
        gchar *cNewConfFileContent = g_key_file_to_data (pKeyFile, &length, &erreur);
 
57
        if (erreur != NULL)
 
58
        {
 
59
                cd_warning ("Error while fetching data : %s", erreur->message);
 
60
                g_error_free (erreur);
 
61
                return ;
 
62
        }
 
63
        g_return_if_fail (cNewConfFileContent != NULL && *cNewConfFileContent != '\0');
 
64
 
 
65
        g_file_set_contents (cConfFilePath, cNewConfFileContent, length, &erreur);
 
66
        if (erreur != NULL)
 
67
        {
 
68
                cd_warning ("Error while writing data to %s : %s", cConfFilePath, erreur->message);
 
69
                g_error_free (erreur);
 
70
                return ;
 
71
        }
 
72
        g_free (cNewConfFileContent);
 
73
}
 
74
 
 
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)
 
99
{
 
100
        //g_print ("%s (%d, %d)\n", __func__, iIdentifier, bUseOriginalKeys);
 
101
        GError *erreur = NULL;
 
102
        gsize length = 0;
 
103
        gchar **pKeyList;
 
104
        gchar **pGroupList = g_key_file_get_groups ((bUseOriginalKeys ? pOriginalKeyFile : pReplacementKeyFile), &length);
 
105
        g_return_if_fail (pGroupList != NULL);
 
106
        gchar *cGroupName, *cKeyName, *cKeyValue, *cComment;
 
107
        int i, j;
 
108
 
 
109
        i = 0;
 
110
        while (pGroupList[i] != NULL)
 
111
        {
 
112
                cGroupName = pGroupList[i];
 
113
 
 
114
                length = 0;
 
115
                pKeyList = g_key_file_get_keys ((bUseOriginalKeys ? pOriginalKeyFile : pReplacementKeyFile), cGroupName, NULL, NULL);
 
116
                g_return_if_fail (pKeyList != NULL);
 
117
                
 
118
                j = 0;
 
119
                while (pKeyList[j] != NULL)
 
120
                {
 
121
                        cKeyName = pKeyList[j];
 
122
                        //g_print ("%s\n  %s", cKeyName, g_key_file_get_comment (pOriginalKeyFile, cGroupName, cKeyName, NULL));
 
123
 
 
124
                        if (iIdentifier != 0)
 
125
                        {
 
126
                                cComment = g_key_file_get_comment (bUseOriginalKeys ? pOriginalKeyFile : pReplacementKeyFile, cGroupName, cKeyName, NULL);
 
127
                                //g_print ("%s\n  %s", cKeyName, cComment);
 
128
                                if (cComment == NULL || strlen (cComment) < 2 || cComment[1] != iIdentifier)
 
129
                                {
 
130
                                        //g_print ("  on saute %s;%s (%s)\n", cGroupName, cKeyName, cComment);
 
131
                                        g_free (cComment);
 
132
                                        j ++;
 
133
                                        continue ;
 
134
                                }
 
135
                                g_free (cComment);
 
136
                        }
 
137
 
 
138
                        cKeyValue =  g_key_file_get_string (pReplacementKeyFile, cGroupName, cKeyName, &erreur);
 
139
                        if (erreur != NULL)
 
140
                        {
 
141
                                cd_warning (erreur->message);
 
142
                                g_error_free (erreur);
 
143
                                erreur = NULL;
 
144
                        }
 
145
                        else
 
146
                        {
 
147
                                //g_print (" -> %s\n", cKeyValue);
 
148
                                if (cKeyValue[strlen(cKeyValue) - 1] == '\n')
 
149
                                        cKeyValue[strlen(cKeyValue) - 1] = '\0';
 
150
                                g_key_file_set_string (pOriginalKeyFile, cGroupName, cKeyName, (cKeyValue != NULL ? cKeyValue : ""));
 
151
                        }
 
152
                        g_free (cKeyValue);
 
153
                        j ++;
 
154
                }
 
155
 
 
156
                g_strfreev (pKeyList);
 
157
                i ++;
 
158
        }
 
159
        g_strfreev (pGroupList);
 
160
        
 
161
        if (bUseOriginalKeys)
 
162
        {
 
163
                pGroupList = g_key_file_get_groups (pReplacementKeyFile, &length);
 
164
                i = 0;
 
165
                while (pGroupList[i] != NULL)
 
166
                {
 
167
                        cGroupName = pGroupList[i];
 
168
 
 
169
                        length = 0;
 
170
                        pKeyList = g_key_file_get_keys (pReplacementKeyFile, cGroupName, NULL, NULL);
 
171
 
 
172
                        j = 0;
 
173
                        while (pKeyList[j] != NULL)
 
174
                        {
 
175
                                cKeyName = pKeyList[j];
 
176
                                //g_print ("%s\n  %s", cKeyName, g_key_file_get_comment (pOriginalKeyFile, cGroupName, cKeyName, NULL));
 
177
 
 
178
                                cComment = g_key_file_get_comment (pReplacementKeyFile, cGroupName, cKeyName, NULL);
 
179
                                if (cComment == NULL || strlen (cComment) < 3 || (cComment[1] != '0' && cComment[2] != '0'))
 
180
                                {
 
181
                                        g_free (cComment);
 
182
                                        j ++;
 
183
                                        continue ;
 
184
                                }
 
185
                                if (iIdentifier != 0)
 
186
                                {
 
187
                                        if (cComment == NULL || strlen (cComment) < 2 || cComment[1] != iIdentifier)
 
188
                                        {
 
189
                                                //g_print ("  on saute %s;%s (%s)\n", cGroupName, cKeyName, cComment);
 
190
                                                g_free (cComment);
 
191
                                                j ++;
 
192
                                                continue ;
 
193
                                        }
 
194
                                }
 
195
 
 
196
                                cKeyValue =  g_key_file_get_string (pReplacementKeyFile, cGroupName, cKeyName, &erreur);
 
197
                                if (erreur != NULL)
 
198
                                {
 
199
                                        cd_warning (erreur->message);
 
200
                                        g_error_free (erreur);
 
201
                                        erreur = NULL;
 
202
                                }
 
203
                                else
 
204
                                {
 
205
                                        //g_print (" -> %s\n", cKeyValue);
 
206
                                        if (cKeyValue[strlen(cKeyValue) - 1] == '\n')
 
207
                                                cKeyValue[strlen(cKeyValue) - 1] = '\0';
 
208
                                        g_key_file_set_string (pOriginalKeyFile, cGroupName, cKeyName, (cKeyValue != NULL ? cKeyValue : ""));
 
209
                                        if (cComment != NULL)
 
210
                                        {
 
211
                                                g_key_file_set_comment (pOriginalKeyFile, cGroupName, cKeyName, cComment, &erreur);
 
212
                                                if (erreur != NULL)
 
213
                                                {
 
214
                                                        cd_warning (erreur->message);
 
215
                                                        g_error_free (erreur);
 
216
                                                        erreur = NULL;
 
217
                                                }
 
218
                                        }
 
219
                                }
 
220
                                g_free (cKeyValue);
 
221
                                g_free (cComment);
 
222
                                j ++;
 
223
                        }
 
224
 
 
225
                        g_strfreev (pKeyList);
 
226
                        i ++;
 
227
                }
 
228
                g_strfreev (pGroupList);
 
229
        }
 
230
}
 
231
 
 
232
 
 
233
void cairo_dock_replace_values_in_conf_file (const gchar *cConfFilePath, GKeyFile *pValidKeyFile, gboolean bUseFileKeys, gchar iIdentifier)
 
234
{
 
235
        GKeyFile *pConfKeyFile = cairo_dock_open_key_file (cConfFilePath);
 
236
        if (pConfKeyFile == NULL)
 
237
                return ;
 
238
 
 
239
        cd_debug ("%s (%s)\n", __func__,cConfFilePath );
 
240
        cairo_dock_replace_key_values (pConfKeyFile, pValidKeyFile, bUseFileKeys, iIdentifier);
 
241
 
 
242
        cairo_dock_write_keys_to_file (pConfKeyFile, cConfFilePath);
 
243
 
 
244
        g_key_file_free (pConfKeyFile);
 
245
}
 
246
 
 
247
void cairo_dock_replace_keys_by_identifier (const gchar *cConfFilePath, gchar *cReplacementConfFilePath, gchar iIdentifier)
 
248
{
 
249
        GKeyFile *pReplacementKeyFile = cairo_dock_open_key_file (cReplacementConfFilePath);
 
250
        if (pReplacementKeyFile == NULL)
 
251
                return ;
 
252
        
 
253
        cd_debug ("%s (%s <- %s, '%c')\n", __func__, cConfFilePath, cReplacementConfFilePath, iIdentifier);
 
254
        cairo_dock_replace_values_in_conf_file (cConfFilePath, pReplacementKeyFile, TRUE, iIdentifier);
 
255
 
 
256
        g_key_file_free (pReplacementKeyFile);
 
257
}
 
258
 
 
259
 
 
260
 
 
261
void cairo_dock_get_conf_file_version (GKeyFile *pKeyFile, gchar **cConfFileVersion)
 
262
{
 
263
        *cConfFileVersion = NULL;
 
264
        
 
265
        gchar *cFirstComment =  g_key_file_get_comment (pKeyFile, NULL, NULL, NULL);
 
266
        
 
267
        if (cFirstComment != NULL && *cFirstComment != '\0')
 
268
        {
 
269
                gchar *str = strchr (cFirstComment, '\n');
 
270
                if (str != NULL)
 
271
                        *str = '\0';
 
272
                
 
273
                str = strchr (cFirstComment, ';');  // le 1er est pour la langue (obsolete).
 
274
                if (str != NULL)
 
275
                {
 
276
                        *cConfFileVersion = g_strdup (str+1);
 
277
                }
 
278
                else
 
279
                {
 
280
                        *cConfFileVersion = g_strdup (cFirstComment + (*cFirstComment == '!'));  // le '!' est obsolete.
 
281
                }
 
282
        }
 
283
        g_free (cFirstComment);
 
284
}
 
285
 
 
286
gboolean cairo_dock_conf_file_needs_update (GKeyFile *pKeyFile, const gchar *cVersion)
 
287
{
 
288
        gchar *cPreviousVersion = NULL;
 
289
        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;
 
295
        
 
296
        g_free (cPreviousVersion);
 
297
        return bNeedsUpdate;
 
298
}
 
299
 
 
300
 
 
301
void cairo_dock_add_remove_element_to_key (const gchar *cConfFilePath, const gchar *cGroupName, const gchar *cKeyName, gchar *cElementName, gboolean bAdd)
 
302
{
 
303
        GKeyFile *pKeyFile = cairo_dock_open_key_file (cConfFilePath);
 
304
        if (pKeyFile == NULL)
 
305
                return ;
 
306
        
 
307
        gchar *cElementList = g_key_file_get_string (pKeyFile, cGroupName, cKeyName, NULL), *cNewElementList = NULL;
 
308
        if (cElementList != NULL && *cElementList == '\0')
 
309
        {
 
310
                g_free (cElementList);
 
311
                cElementList= NULL;
 
312
        }
 
313
        
 
314
        if (bAdd)
 
315
        {
 
316
                //g_print ("on rajoute %s\n", cElementName);
 
317
                if (cElementList != NULL)
 
318
                        cNewElementList = g_strdup_printf ("%s;%s", cElementList, cElementName);
 
319
                else
 
320
                        cNewElementList = g_strdup (cElementName);
 
321
        }
 
322
        else
 
323
        {
 
324
                //g_print ("on enleve %s\n", cElementName);
 
325
                gchar *str = g_strstr_len (cElementList, strlen (cElementList), cElementName);
 
326
                g_return_if_fail (str != NULL);
 
327
                if (str == cElementList)
 
328
                {
 
329
                        if (str[strlen (cElementName)] == '\0')
 
330
                                cNewElementList = g_strdup ("");
 
331
                        else
 
332
                                cNewElementList = g_strdup (str + strlen (cElementName) + 1);
 
333
                }
 
334
                else
 
335
                {
 
336
                        *(str-1) = '\0';
 
337
                        if (str[strlen (cElementName)] == '\0')
 
338
                                cNewElementList = g_strdup (cElementList);
 
339
                        else
 
340
                                cNewElementList = g_strdup_printf ("%s;%s", cElementList, str + strlen (cElementName) + 1);
 
341
                }
 
342
        }
 
343
        g_key_file_set_string (pKeyFile, cGroupName, cKeyName, cNewElementList);
 
344
        cairo_dock_write_keys_to_file (pKeyFile, cConfFilePath);
 
345
        g_free (cElementList);
 
346
        g_free (cNewElementList);
 
347
        g_key_file_free (pKeyFile);
 
348
}
 
349
 
 
350
 
 
351
void cairo_dock_add_widget_to_conf_file (GKeyFile *pKeyFile, const gchar *cGroupName, const gchar *ckeyName, const gchar *cInitialValue, CairoDockGUIWidgetType iWidgetType, const gchar *cAuthorizedValues, const gchar *cDescription, const gchar *cTooltip)
 
352
{
 
353
        g_key_file_set_string (pKeyFile, cGroupName, ckeyName, cInitialValue);
 
354
        gchar *Comment = g_strdup_printf ("%c0%s %s%s%s%s", iWidgetType, cAuthorizedValues ? cAuthorizedValues : "", cDescription, cTooltip ? "\n{" : "", cTooltip ? cTooltip : "", cTooltip ? "}" : "");
 
355
        g_key_file_set_comment (pKeyFile, cGroupName, ckeyName, Comment, NULL);
 
356
        g_free (Comment);
 
357
}
 
358
 
 
359
void cairo_dock_remove_group_key_from_conf_file (GKeyFile *pKeyFile, const gchar *cGroupName, const gchar *ckeyName)
 
360
{
 
361
        g_key_file_remove_comment (pKeyFile, cGroupName, ckeyName, NULL);
 
362
        g_key_file_remove_key (pKeyFile, cGroupName, ckeyName, NULL);
 
363
}
 
364
 
 
365
gboolean cairo_dock_rename_group_in_conf_file (GKeyFile *pKeyFile, const gchar *cGroupName, const gchar *cNewGroupName)
 
366
{
 
367
        if (g_key_file_has_group (pKeyFile, cNewGroupName))
 
368
                return FALSE;
 
369
        
 
370
        gchar **pKeyList = g_key_file_get_keys (pKeyFile, cGroupName, NULL, NULL);
 
371
        gchar *cValue;
 
372
        int i;
 
373
        for (i = 0; pKeyList[i] != NULL; i ++)
 
374
        {
 
375
                cValue = g_key_file_get_value (pKeyFile, cGroupName, pKeyList[i], NULL);
 
376
                g_key_file_set_value (pKeyFile, cNewGroupName, pKeyList[i], cValue);
 
377
                g_free (cValue);
 
378
        }
 
379
        g_strfreev (pKeyList);
 
380
        
 
381
        g_key_file_remove_group (pKeyFile, cGroupName, NULL);
 
382
        
 
383
        return TRUE;
 
384
}