Remove GLib-compatible code related to GLib < 2.26.
[midnight-commander.git] / lib / mcconfig / common.c
blobff6afdba65ac75d33512b538000706ed554d3caa
1 /*
2 Configure module for the Midnight Commander
4 Copyright (C) 1994-2015
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <config.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h> /* extern int errno */
30 #include "lib/global.h"
31 #include "lib/vfs/vfs.h" /* mc_stat */
32 #include "lib/util.h"
33 #include "lib/mcconfig.h"
35 /*** global variables **************************************************/
37 mc_config_t *mc_main_config;
38 mc_config_t *mc_panels_config;
40 /*** file scope macro definitions **************************************/
42 /*** file scope type declarations **************************************/
44 /*** file scope variables **********************************************/
46 /*** file scope functions **********************************************/
47 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
49 static gboolean
50 mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
52 gchar *data, *written_data;
53 gsize len, total_written;
54 gboolean ret;
55 int fd;
56 ssize_t cur_written;
57 vfs_path_t *ini_vpath;
59 mc_return_val_if_error (mcerror, FALSE);
61 data = g_key_file_to_data (mc_config->handle, &len, NULL);
62 if (!exist_file (ini_path))
64 ret = g_file_set_contents (ini_path, data, len, mcerror);
65 g_free (data);
66 return ret;
69 mc_util_make_backup_if_possible (ini_path, "~");
71 ini_vpath = vfs_path_from_str (ini_path);
72 fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
73 vfs_path_free (ini_vpath);
75 if (fd == -1)
77 mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
78 g_free (data);
79 return FALSE;
82 for (written_data = data, total_written = len;
83 (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
84 written_data += cur_written, total_written -= cur_written)
86 mc_close (fd);
87 g_free (data);
89 if (cur_written == -1)
91 mc_util_restore_from_backup_if_possible (ini_path, "~");
92 mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
93 return FALSE;
96 mc_util_unlink_backup_if_possible (ini_path, "~");
97 return TRUE;
100 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
101 /*** public functions **************************************************/
102 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
104 mc_config_t *
105 mc_config_init (const gchar * ini_path, gboolean read_only)
107 mc_config_t *mc_config;
108 struct stat st;
110 mc_config = g_try_malloc0 (sizeof (mc_config_t));
112 if (mc_config == NULL)
113 return NULL;
115 mc_config->handle = g_key_file_new ();
116 if (mc_config->handle == NULL)
118 g_free (mc_config);
119 return NULL;
121 if (ini_path == NULL)
122 return mc_config;
124 if (exist_file (ini_path))
126 vfs_path_t *vpath;
128 vpath = vfs_path_from_str (ini_path);
129 if (mc_stat (vpath, &st) == 0 && st.st_size != 0)
131 GKeyFileFlags flags = G_KEY_FILE_NONE;
133 if (!read_only)
134 flags |= G_KEY_FILE_KEEP_COMMENTS;
136 /* file exists and not empty */
137 g_key_file_load_from_file (mc_config->handle, ini_path, flags, NULL);
139 vfs_path_free (vpath);
142 mc_config->ini_path = g_strdup (ini_path);
143 return mc_config;
146 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148 void
149 mc_config_deinit (mc_config_t * mc_config)
151 if (mc_config != NULL)
153 g_free (mc_config->ini_path);
154 g_key_file_free (mc_config->handle);
155 g_free (mc_config);
159 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161 gboolean
162 mc_config_has_param (const mc_config_t * mc_config, const char *group, const gchar * param)
164 if (!mc_config || !group || !param)
165 return FALSE;
167 return g_key_file_has_key (mc_config->handle, group, param, NULL);
170 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
172 gboolean
173 mc_config_has_group (mc_config_t * mc_config, const char *group)
175 if (!mc_config || !group)
176 return FALSE;
178 return g_key_file_has_group (mc_config->handle, group);
181 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
183 gboolean
184 mc_config_del_key (mc_config_t * mc_config, const char *group, const gchar * param)
186 if (!mc_config || !group || !param)
187 return FALSE;
189 return g_key_file_remove_key (mc_config->handle, group, param, NULL);
192 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
194 gboolean
195 mc_config_del_group (mc_config_t * mc_config, const char *group)
197 if (!mc_config || !group)
198 return FALSE;
200 return g_key_file_remove_group (mc_config->handle, group, NULL);
203 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
205 gboolean
206 mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean read_only,
207 gboolean remove_empty)
209 mc_config_t *tmp_config;
210 gchar **groups, **curr_grp;
211 gchar *value;
212 gboolean ok;
214 if (mc_config == NULL)
215 return FALSE;
217 tmp_config = mc_config_init (ini_path, read_only);
218 if (tmp_config == NULL)
219 return FALSE;
221 groups = mc_config_get_groups (tmp_config, NULL);
222 ok = (*groups != NULL);
224 for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
226 gchar **keys, **curr_key;
228 keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
230 for (curr_key = keys; *curr_key != NULL; curr_key++)
232 value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
233 if (value != NULL)
235 if (*value == '\0' && remove_empty)
236 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
237 else
238 g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
239 g_free (value);
241 else if (remove_empty)
242 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
244 g_strfreev (keys);
247 g_strfreev (groups);
248 mc_config_deinit (tmp_config);
250 return ok;
253 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
255 gboolean
256 mc_config_save_file (mc_config_t * mc_config, GError ** mcerror)
258 mc_return_val_if_error (mcerror, FALSE);
260 if (mc_config == NULL || mc_config->ini_path == NULL)
261 return FALSE;
263 return mc_config_new_or_override_file (mc_config, mc_config->ini_path, mcerror);
266 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
268 gboolean
269 mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
271 mc_return_val_if_error (mcerror, FALSE);
273 if (mc_config == NULL)
274 return FALSE;
276 return mc_config_new_or_override_file (mc_config, ini_path, mcerror);
280 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */