Massive moved some dirs from $(srcdir)/src into $(srcdir)/lib
[midnight-commander.git] / lib / mcconfig / common.c
blob5bc4f401bdef32ec0a81921f39f0e32560f5f49e
1 /* Configure module for the Midnight Commander
2 Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <config.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h> /* extern int errno */
29 #include "global.h"
31 #include "../../lib/vfs/mc-vfs/vfs.h" /* mc_stat */
32 #include "../../src/util.h"
34 #include "mcconfig.h"
36 /*** global variables **************************************************/
38 mc_config_t *mc_main_config;
39 mc_config_t *mc_panels_config;
41 /*** file scope macro definitions **************************************/
43 /*** file scope type declarations **************************************/
45 /*** file scope variables **********************************************/
47 /*** file scope functions **********************************************/
48 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
50 static gboolean
51 mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
52 GError **error)
54 gchar *data, *written_data;
55 gsize len, total_written;
56 gboolean ret;
57 int fd;
58 ssize_t cur_written;
60 data = g_key_file_to_data (mc_config->handle, &len, NULL);
61 if (!exist_file (ini_path)) {
62 ret = g_file_set_contents (ini_path, data, len, error);
63 g_free (data);
64 return ret;
66 mc_util_make_backup_if_possible (ini_path, "~");
68 fd = mc_open (ini_path, O_WRONLY | O_TRUNC | O_SYNC, 0);
69 if (fd == -1) {
70 g_propagate_error (error, g_error_new (mc_main_error_quark() ,0, "%s", unix_error_string (errno)));
71 return FALSE;
74 for (written_data = data, total_written = len;
75 (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
76 written_data += cur_written, total_written -= cur_written);
77 mc_close (fd);
78 g_free (data);
80 if (cur_written == -1) {
81 mc_util_restore_from_backup_if_possible (ini_path, "~");
82 g_propagate_error (error, g_error_new (mc_main_error_quark() ,0, "%s", unix_error_string (errno)));
83 return FALSE;
86 mc_util_unlink_backup_if_possible (ini_path, "~");
87 return TRUE;
90 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
91 /*** public functions **************************************************/
92 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
94 mc_config_t *
95 mc_config_init (const gchar * ini_path)
97 mc_config_t *mc_config;
98 struct stat st;
100 mc_config = g_try_malloc0 (sizeof (mc_config_t));
102 if (mc_config == NULL)
103 return NULL;
105 mc_config->handle = g_key_file_new ();
106 if (mc_config->handle == NULL) {
107 g_free (mc_config);
108 return NULL;
110 if (!ini_path || !exist_file (ini_path)) {
111 return mc_config;
114 if (!mc_stat (ini_path, &st) && st.st_size) {
115 /* file present and not empty */
116 g_key_file_load_from_file (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL);
119 mc_config->ini_path = g_strdup (ini_path);
120 return mc_config;
123 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
125 void
126 mc_config_deinit (mc_config_t * mc_config)
128 if (!mc_config)
129 return;
131 g_free (mc_config->ini_path);
132 g_key_file_free (mc_config->handle);
133 g_free (mc_config);
137 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
139 gboolean
140 mc_config_has_param (mc_config_t * mc_config, const char *group, const gchar * param)
142 if (!mc_config || !group || !param)
143 return FALSE;
145 return g_key_file_has_key (mc_config->handle, group, param, NULL);
148 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
150 gboolean
151 mc_config_has_group (mc_config_t * mc_config, const char *group)
153 if (!mc_config || !group)
154 return FALSE;
156 return g_key_file_has_group (mc_config->handle, group);
159 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
162 gboolean
163 mc_config_del_param (mc_config_t * mc_config, const char *group, const gchar * param)
165 if (!mc_config || !group || !param)
166 return FALSE;
167 #if GLIB_CHECK_VERSION (2, 15, 0)
168 return g_key_file_remove_key (mc_config->handle, group, param, NULL);
169 #else
170 g_key_file_remove_key (mc_config->handle, group, param, NULL);
171 return TRUE;
172 #endif
175 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
177 gboolean
178 mc_config_del_group (mc_config_t * mc_config, const char *group)
180 if (!mc_config || !group)
181 return FALSE;
183 #if GLIB_CHECK_VERSION (2, 15, 0)
184 return g_key_file_remove_group (mc_config->handle, group, NULL);
185 #else
186 g_key_file_remove_group (mc_config->handle, group, NULL);
187 return TRUE;
188 #endif
191 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
193 gboolean
194 mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path)
196 mc_config_t *tmp_config;
197 gchar **groups, **curr_grp;
198 gchar **keys, **curr_key;
199 gchar *value;
201 if (mc_config == NULL) {
202 return FALSE;
205 tmp_config = mc_config_init (ini_path);
206 if (tmp_config == NULL)
207 return FALSE;
209 groups = mc_config_get_groups (tmp_config, NULL);
211 if (groups == NULL)
212 return FALSE;
214 for (curr_grp = groups; *curr_grp != NULL; curr_grp++) {
215 keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
216 for (curr_key = keys; *curr_key != NULL; curr_key++) {
217 value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
218 if (value == NULL)
219 continue;
221 g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
222 g_free (value);
224 g_strfreev (keys);
226 g_strfreev (groups);
227 mc_config_deinit (tmp_config);
228 return TRUE;
231 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
233 gboolean
234 mc_config_save_file (mc_config_t * mc_config, GError **error)
236 if (mc_config == NULL || mc_config->ini_path == NULL) {
237 return FALSE;
239 return mc_config_new_or_override_file (mc_config, mc_config->ini_path, error);
242 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
244 gboolean
245 mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError **error)
248 if (mc_config == NULL) {
249 return FALSE;
251 return mc_config_new_or_override_file (mc_config, ini_path, error);
255 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */