76
void cairo_dock_flush_conf_file_full (GKeyFile *pKeyFile, const gchar *cConfFilePath, const gchar *cShareDataDirPath, gboolean bUseFileKeys, const gchar *cTemplateFileName)
78
gchar *cTemplateConfFilePath = (*cTemplateFileName == '/' ? g_strdup (cTemplateFileName) : g_strdup_printf ("%s/%s", cShareDataDirPath, cTemplateFileName));
79
cd_message ("%s (%s)", __func__, cTemplateConfFilePath);
81
if (! g_file_test (cTemplateConfFilePath, G_FILE_TEST_EXISTS))
83
cd_warning ("Couldn't find any installed conf file in %s", cShareDataDirPath);
87
gchar *cCommand = g_strdup_printf ("/bin/cp \"%s\" \"%s\"", cTemplateConfFilePath, cConfFilePath);
88
int r = system (cCommand);
91
cairo_dock_replace_values_in_conf_file (cConfFilePath, pKeyFile, bUseFileKeys, 0);
93
g_free (cTemplateConfFilePath);
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)
83
// get the groups of the remplacement key-file.
84
GError *erreur = NULL;
87
gchar **pGroupList = g_key_file_get_groups (pReplacementKeyFile, &length);
88
g_return_if_fail (pGroupList != NULL);
89
gchar *cGroupName, *cKeyName, *cKeyValue, *cComment;
92
for (i = 0; pGroupList[i] != NULL; i ++)
94
cGroupName = pGroupList[i];
96
// get the keys of the remplacement key-file.
98
pKeyList = g_key_file_get_keys (pReplacementKeyFile, cGroupName, NULL, NULL);
99
g_return_if_fail (pKeyList != NULL);
101
for (j = 0; pKeyList[j] != NULL; j ++)
103
cKeyName = pKeyList[j];
105
// check that the original identifier matches with the provided one.
106
if (iIdentifier != 0)
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.
110
cComment = g_key_file_get_comment (pOriginalKeyFile, cGroupName, cKeyName, NULL);
111
if (cComment == NULL || cComment[0] == '\0' || cComment[1] != iIdentifier)
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);
124
cd_warning (erreur->message);
125
g_error_free (erreur);
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 : ""));
136
g_strfreev (pKeyList);
138
g_strfreev (pGroupList);
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 ++)
145
cGroupName = pGroupList[i];
147
// get the keys of the original key-file.
149
pKeyList = g_key_file_get_keys (pOriginalKeyFile, cGroupName, NULL, NULL);
150
g_return_if_fail (pKeyList != NULL);
152
for (j = 0; pKeyList[j] != NULL; j ++)
154
cKeyName = pKeyList[j];
155
if (! g_key_file_has_key (pReplacementKeyFile, cGroupName, cKeyName, NULL))
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
160
g_key_file_remove_comment (pOriginalKeyFile, cGroupName, cKeyName, NULL);
161
g_key_file_remove_key (pOriginalKeyFile, cGroupName, cKeyName, NULL);
165
g_strfreev (pKeyList);
167
g_strfreev (pGroupList);
170
void cairo_dock_merge_conf_files (const gchar *cConfFilePath, gchar *cReplacementConfFilePath, gchar iIdentifier)
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);
177
cairo_dock_merge_key_files (pOriginalKeyFile, pReplacementKeyFile, iIdentifier);
178
cairo_dock_write_keys_to_file (pOriginalKeyFile, cConfFilePath);
180
g_key_file_free (pOriginalKeyFile);
181
g_key_file_free (pReplacementKeyFile);
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.
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)
193
GKeyFile *pKeysKeyFile = (bUpdateKeys ? pUptodateKeyFile : pValuesKeyFile);
196
GError *erreur = NULL;
199
gchar **pGroupList = g_key_file_get_groups (pKeysKeyFile, &length);
200
g_return_if_fail (pGroupList != NULL);
201
gchar *cGroupName, *cKeyName, *cKeyValue, *cComment;
204
for (i = 0; pGroupList[i] != NULL; i ++)
206
cGroupName = pGroupList[i];
210
pKeyList = g_key_file_get_keys (pKeysKeyFile, cGroupName, NULL, NULL);
211
g_return_if_fail (pKeyList != NULL);
213
for (j = 0; pKeyList[j] != NULL; j ++)
215
cKeyName = pKeyList[j];
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
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.
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
233
cd_warning (erreur->message);
234
g_error_free (erreur);
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);
247
g_strfreev (pKeyList);
249
g_strfreev (pGroupList);
253
void cairo_dock_upgrade_conf_file_full (const gchar *cConfFilePath, GKeyFile *pKeyFile, const gchar *cDefaultConfFilePath, gboolean bUpdateKeys)
255
GKeyFile *pUptodateKeyFile = cairo_dock_open_key_file (cDefaultConfFilePath);
256
g_return_if_fail (pUptodateKeyFile != NULL);
258
_cairo_dock_replace_key_values (pKeyFile, pUptodateKeyFile, bUpdateKeys);
260
cairo_dock_write_keys_to_file (pUptodateKeyFile, cConfFilePath);
262
g_key_file_free (pUptodateKeyFile);
268
static void cairo_dock_replace_key_values (GKeyFile *pOriginalKeyFile, GKeyFile *pReplacementKeyFile, gboolean bUseOriginalKeys, gchar iIdentifier)
100
270
//g_print ("%s (%d, %d)\n", __func__, iIdentifier, bUseOriginalKeys);
101
271
GError *erreur = NULL;