Updated RU translation.
[midnight-commander.git] / lib / mcconfig / common.c
blob4e8329be442ce65e576b8180514fbe2b6c41f1cd
1 /*
2 Configure module for the Midnight Commander
4 Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <config.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h> /* extern int errno */
32 #include "lib/global.h"
33 #include "lib/vfs/vfs.h" /* mc_stat */
34 #include "lib/util.h"
35 #include "lib/mcconfig.h"
37 /*** global variables **************************************************/
39 mc_config_t *mc_main_config;
40 mc_config_t *mc_panels_config;
42 /*** file scope macro definitions **************************************/
44 /*** file scope type declarations **************************************/
46 /*** file scope variables **********************************************/
48 /*** file scope functions **********************************************/
49 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
51 static gboolean
52 mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, 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))
63 ret = g_file_set_contents (ini_path, data, len, error);
64 g_free (data);
65 return ret;
67 mc_util_make_backup_if_possible (ini_path, "~");
69 fd = mc_open (ini_path, O_WRONLY | O_TRUNC, 0);
70 if (fd == -1)
72 g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
73 g_free (data);
74 return FALSE;
77 for (written_data = data, total_written = len;
78 (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
79 written_data += cur_written, total_written -= cur_written);
80 mc_close (fd);
81 g_free (data);
83 if (cur_written == -1)
85 mc_util_restore_from_backup_if_possible (ini_path, "~");
86 g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
87 return FALSE;
90 mc_util_unlink_backup_if_possible (ini_path, "~");
91 return TRUE;
94 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
95 /*** public functions **************************************************/
96 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98 mc_config_t *
99 mc_config_init (const gchar * ini_path)
101 mc_config_t *mc_config;
102 struct stat st;
104 mc_config = g_try_malloc0 (sizeof (mc_config_t));
106 if (mc_config == NULL)
107 return NULL;
109 mc_config->handle = g_key_file_new ();
110 if (mc_config->handle == NULL)
112 g_free (mc_config);
113 return NULL;
115 if (ini_path == NULL)
116 return mc_config;
118 if (exist_file (ini_path) && mc_stat (ini_path, &st) == 0 && st.st_size != 0)
120 /* file exists and not empty */
121 g_key_file_load_from_file (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL);
124 mc_config->ini_path = g_strdup (ini_path);
125 return mc_config;
128 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
130 void
131 mc_config_deinit (mc_config_t * mc_config)
133 if (mc_config != NULL)
135 g_free (mc_config->ini_path);
136 g_key_file_free (mc_config->handle);
137 g_free (mc_config);
141 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
143 gboolean
144 mc_config_has_param (const mc_config_t * mc_config, const char *group, const gchar * param)
146 if (!mc_config || !group || !param)
147 return FALSE;
149 return g_key_file_has_key (mc_config->handle, group, param, NULL);
152 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
154 gboolean
155 mc_config_has_group (mc_config_t * mc_config, const char *group)
157 if (!mc_config || !group)
158 return FALSE;
160 return g_key_file_has_group (mc_config->handle, group);
163 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
165 gboolean
166 mc_config_del_key (mc_config_t * mc_config, const char *group, const gchar * param)
168 if (!mc_config || !group || !param)
169 return FALSE;
170 #if GLIB_CHECK_VERSION (2, 15, 0)
171 return g_key_file_remove_key (mc_config->handle, group, param, NULL);
172 #else
173 g_key_file_remove_key (mc_config->handle, group, param, NULL);
174 return TRUE;
175 #endif
178 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
180 gboolean
181 mc_config_del_group (mc_config_t * mc_config, const char *group)
183 if (!mc_config || !group)
184 return FALSE;
186 #if GLIB_CHECK_VERSION (2, 15, 0)
187 return g_key_file_remove_group (mc_config->handle, group, NULL);
188 #else
189 g_key_file_remove_group (mc_config->handle, group, NULL);
190 return TRUE;
191 #endif
194 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
196 gboolean
197 mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean remove_empty)
199 mc_config_t *tmp_config;
200 gchar **groups, **curr_grp;
201 gchar **keys, **curr_key;
202 gchar *value;
204 if (mc_config == NULL)
205 return FALSE;
207 tmp_config = mc_config_init (ini_path);
208 if (tmp_config == NULL)
209 return FALSE;
211 groups = mc_config_get_groups (tmp_config, NULL);
213 if (groups == NULL)
215 mc_config_deinit (tmp_config);
216 return FALSE;
219 for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
221 keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
222 for (curr_key = keys; *curr_key != NULL; curr_key++)
224 value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
225 if (value != NULL)
227 if (*value == '\0' && remove_empty)
228 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
229 else
230 g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
231 g_free (value);
233 else if (remove_empty)
234 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
236 g_strfreev (keys);
238 g_strfreev (groups);
239 mc_config_deinit (tmp_config);
240 return TRUE;
243 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
245 gboolean
246 mc_config_save_file (mc_config_t * mc_config, GError ** error)
248 if (mc_config == NULL || mc_config->ini_path == NULL)
250 return FALSE;
252 return mc_config_new_or_override_file (mc_config, mc_config->ini_path, error);
255 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
257 gboolean
258 mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** error)
260 if (mc_config == NULL)
262 return FALSE;
264 return mc_config_new_or_override_file (mc_config, ini_path, error);
268 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */