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.
22 #include <sys/types.h>
26 #include <errno.h> /* extern int errno */
28 #include "lib/global.h"
29 #include "lib/vfs/mc-vfs/vfs.h" /* mc_stat */
31 #include "lib/mcconfig.h"
33 /*** global variables **************************************************/
35 mc_config_t
*mc_main_config
;
36 mc_config_t
*mc_panels_config
;
38 /*** file scope macro definitions **************************************/
40 /*** file scope type declarations **************************************/
42 /*** file scope variables **********************************************/
44 /*** file scope functions **********************************************/
45 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
48 mc_config_new_or_override_file (mc_config_t
* mc_config
, const gchar
* ini_path
, GError
** error
)
50 gchar
*data
, *written_data
;
51 gsize len
, total_written
;
56 data
= g_key_file_to_data (mc_config
->handle
, &len
, NULL
);
57 if (!exist_file (ini_path
))
59 ret
= g_file_set_contents (ini_path
, data
, len
, error
);
63 mc_util_make_backup_if_possible (ini_path
, "~");
65 fd
= mc_open (ini_path
, O_WRONLY
| O_TRUNC
| O_SYNC
, 0);
68 g_propagate_error (error
,
69 g_error_new (mc_main_error_quark (), 0, "%s",
70 unix_error_string (errno
)));
75 for (written_data
= data
, total_written
= len
;
76 (cur_written
= mc_write (fd
, (const void *) written_data
, total_written
)) > 0;
77 written_data
+= cur_written
, total_written
-= cur_written
);
81 if (cur_written
== -1)
83 mc_util_restore_from_backup_if_possible (ini_path
, "~");
84 g_propagate_error (error
,
85 g_error_new (mc_main_error_quark (), 0, "%s",
86 unix_error_string (errno
)));
90 mc_util_unlink_backup_if_possible (ini_path
, "~");
94 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
95 /*** public functions **************************************************/
96 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99 mc_config_init (const gchar
* ini_path
)
101 mc_config_t
*mc_config
;
104 mc_config
= g_try_malloc0 (sizeof (mc_config_t
));
106 if (mc_config
== NULL
)
109 mc_config
->handle
= g_key_file_new ();
110 if (mc_config
->handle
== NULL
)
115 if (ini_path
== NULL
)
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
);
128 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
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
);
141 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144 mc_config_has_param (mc_config_t
* mc_config
, const char *group
, const gchar
* param
)
146 if (!mc_config
|| !group
|| !param
)
149 return g_key_file_has_key (mc_config
->handle
, group
, param
, NULL
);
152 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155 mc_config_has_group (mc_config_t
* mc_config
, const char *group
)
157 if (!mc_config
|| !group
)
160 return g_key_file_has_group (mc_config
->handle
, group
);
163 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
166 mc_config_del_key (mc_config_t
* mc_config
, const char *group
, const gchar
* param
)
168 if (!mc_config
|| !group
|| !param
)
170 #if GLIB_CHECK_VERSION (2, 15, 0)
171 return g_key_file_remove_key (mc_config
->handle
, group
, param
, NULL
);
173 g_key_file_remove_key (mc_config
->handle
, group
, param
, NULL
);
178 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
181 mc_config_del_group (mc_config_t
* mc_config
, const char *group
)
183 if (!mc_config
|| !group
)
186 #if GLIB_CHECK_VERSION (2, 15, 0)
187 return g_key_file_remove_group (mc_config
->handle
, group
, NULL
);
189 g_key_file_remove_group (mc_config
->handle
, group
, NULL
);
194 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
197 mc_config_read_file (mc_config_t
* mc_config
, const gchar
* ini_path
)
199 mc_config_t
*tmp_config
;
200 gchar
**groups
, **curr_grp
;
201 gchar
**keys
, **curr_key
;
204 if (mc_config
== NULL
)
209 tmp_config
= mc_config_init (ini_path
);
210 if (tmp_config
== NULL
)
213 groups
= mc_config_get_groups (tmp_config
, NULL
);
217 mc_config_deinit (tmp_config
);
221 for (curr_grp
= groups
; *curr_grp
!= NULL
; curr_grp
++)
223 keys
= mc_config_get_keys (tmp_config
, *curr_grp
, NULL
);
224 for (curr_key
= keys
; *curr_key
!= NULL
; curr_key
++)
226 value
= g_key_file_get_value (tmp_config
->handle
, *curr_grp
, *curr_key
, NULL
);
230 g_key_file_set_value (mc_config
->handle
, *curr_grp
, *curr_key
, value
);
236 mc_config_deinit (tmp_config
);
240 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
243 mc_config_save_file (mc_config_t
* mc_config
, GError
** error
)
245 if (mc_config
== NULL
|| mc_config
->ini_path
== NULL
)
249 return mc_config_new_or_override_file (mc_config
, mc_config
->ini_path
, error
);
252 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
255 mc_config_save_to_file (mc_config_t
* mc_config
, const gchar
* ini_path
, GError
** error
)
257 if (mc_config
== NULL
)
261 return mc_config_new_or_override_file (mc_config
, ini_path
, error
);
265 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */