Updated italian translation.
[midnight-commander.git] / src / setup.c
blob97d3b5025a238593baf56a8546f762d550c3d3d4
1 /* Setup loading/saving.
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 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. */
19 /** \file setup.c
20 * \brief Source: setup loading/saving
23 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #include "lib/global.h"
33 #include "lib/tty/tty.h"
34 #include "lib/tty/key.h"
35 #include "lib/tty/mouse.h" /* To make view.h happy */
36 #include "lib/vfs/mc-vfs/vfs.h"
37 #include "lib/mcconfig.h"
38 #include "lib/fileloc.h"
40 #ifdef ENABLE_VFS
41 #include "lib/vfs/mc-vfs/gc.h"
42 #endif
44 #ifdef USE_NETCODE
45 # include "lib/vfs/mc-vfs/ftpfs.h"
46 # include "lib/vfs/mc-vfs/fish.h"
47 #endif
48 #include "lib/strutil.h" /* str_isutf8 () */
50 #include "args.h"
51 #include "dir.h"
52 #include "panel.h"
53 #include "main.h"
54 #include "tree.h" /* xtree_mode */
55 #include "setup.h"
56 #include "src/viewer/mcviewer.h" /* For the externs */
57 #include "hotlist.h" /* load/save/done hotlist */
58 #include "panelize.h" /* load/save/done panelize */
59 #include "layout.h"
60 #include "menu.h" /* menubar_visible declaration */
61 #include "cmd.h"
62 #include "file.h" /* safe_delete */
63 #include "keybind.h" /* lookup_action */
64 #include "wtools.h"
66 #ifdef HAVE_CHARSET
67 #include "charsets.h"
68 #endif
70 #ifdef USE_INTERNAL_EDIT
71 # include "src/editor/edit.h"
72 #endif
76 extern int num_history_items_recorded;
78 char *profile_name; /* .mc/ini */
79 char *global_profile_name; /* mc.lib */
81 char *setup_color_string;
82 char *term_color_string;
83 char *color_terminal_string;
85 panel_view_mode_t startup_left_mode;
86 panel_view_mode_t startup_right_mode;
88 /* Ugly hack to allow panel_save_setup to work as a place holder for */
89 /* default panel values */
90 int saving_setup;
91 int setup_copymove_persistent_attr = 1;
93 static char *panels_profile_name = NULL; /* .mc/panels.ini */
95 static const struct {
96 const char *key;
97 int list_type;
98 } list_types [] = {
99 { "full", list_full },
100 { "brief", list_brief },
101 { "long", list_long },
102 { "user", list_user },
103 { 0, 0 }
106 static const struct {
107 const char *opt_name;
108 panel_view_mode_t opt_type;
109 } panel_types [] = {
110 { "listing", view_listing },
111 { "quickview", view_quick },
112 { "info", view_info },
113 { "tree", view_tree },
114 { NULL, view_listing }
117 static const struct {
118 const char *opt_name;
119 int *opt_addr;
120 } layout [] = {
121 { "equal_split", &equal_split },
122 { "first_panel_size", &first_panel_size },
123 { "message_visible", &message_visible },
124 { "keybar_visible", &keybar_visible },
125 { "xterm_title", &xterm_title },
126 { "output_lines", &output_lines },
127 { "command_prompt", &command_prompt },
128 { "menubar_visible", &menubar_visible },
129 { "show_mini_info", &show_mini_info },
130 { "permission_mode", &permission_mode },
131 { "filetype_mode", &filetype_mode },
132 { "free_space", &free_space },
133 { 0, 0 }
136 static const struct {
137 const char *opt_name;
138 int *opt_addr;
139 } int_options [] = {
140 { "show_backups", &show_backups },
141 { "kilobyte_si", &kilobyte_si },
142 { "show_dot_files", &show_dot_files },
143 { "verbose", &verbose },
144 { "mark_moves_down", &mark_moves_down },
145 { "pause_after_run", &pause_after_run },
146 { "shell_patterns", &easy_patterns },
147 { "auto_save_setup", &auto_save_setup },
148 { "auto_menu", &auto_menu },
149 { "use_internal_view", &use_internal_view },
150 { "use_internal_edit", &use_internal_edit },
151 { "clear_before_exec", &clear_before_exec },
152 { "mix_all_files", &mix_all_files },
153 { "fast_reload", &fast_reload },
154 { "fast_reload_msg_shown", &fast_reload_w },
155 { "confirm_delete", &confirm_delete },
156 { "confirm_overwrite", &confirm_overwrite },
157 { "confirm_execute", &confirm_execute },
158 { "confirm_history_cleanup", &confirm_history_cleanup },
159 { "confirm_exit", &confirm_exit },
160 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
161 { "safe_delete", &safe_delete },
162 { "mouse_repeat_rate", &mou_auto_repeat },
163 { "double_click_speed", &double_click_speed },
164 #ifndef HAVE_CHARSET
165 { "eight_bit_clean", &eight_bit_clean },
166 { "full_eight_bits", &full_eight_bits },
167 #endif /* !HAVE_CHARSET */
168 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
169 { "confirm_view_dir", &confirm_view_dir },
170 { "mouse_move_pages", &mouse_move_pages },
171 { "mouse_move_pages_viewer", &mcview_mouse_move_pages },
172 { "mouse_close_dialog", &mouse_close_dialog},
173 { "fast_refresh", &fast_refresh },
174 { "navigate_with_arrows", &navigate_with_arrows },
175 { "drop_menus", &drop_menus },
176 { "wrap_mode", &mcview_global_wrap_mode},
177 { "old_esc_mode", &old_esc_mode },
178 { "cd_symlinks", &cd_symlinks },
179 { "show_all_if_ambiguous", &show_all_if_ambiguous },
180 { "max_dirt_limit", &mcview_max_dirt_limit },
181 { "torben_fj_mode", &torben_fj_mode },
182 { "use_file_to_guess_type", &use_file_to_check_type },
183 { "alternate_plus_minus", &alternate_plus_minus },
184 { "only_leading_plus_minus", &only_leading_plus_minus },
185 { "show_output_starts_shell", &output_starts_shell },
186 { "panel_scroll_pages", &panel_scroll_pages },
187 { "xtree_mode", &xtree_mode },
188 { "num_history_items_recorded", &num_history_items_recorded },
189 { "file_op_compute_totals", &file_op_compute_totals },
190 #ifdef ENABLE_VFS
191 { "vfs_timeout", &vfs_timeout },
192 #ifdef USE_NETCODE
193 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
194 { "use_netrc", &use_netrc },
195 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
196 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
197 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
198 { "ftpfs_use_passive_connections_over_proxy", &ftpfs_use_passive_connections_over_proxy },
199 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
200 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
201 { "fish_directory_timeout", &fish_directory_timeout },
202 #endif /* USE_NETCODE */
203 #endif /* ENABLE_VFS */
204 #ifdef USE_INTERNAL_EDIT
205 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
206 { "editor_tab_spacing", &option_tab_spacing },
207 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
208 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
209 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
210 { "editor_fake_half_tabs", &option_fake_half_tabs },
211 { "editor_option_save_mode", &option_save_mode },
212 { "editor_option_save_position", &option_save_position },
213 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
214 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
215 { "editor_edit_confirm_save", &edit_confirm_save },
216 { "editor_syntax_highlighting", &option_syntax_highlighting },
217 { "editor_persistent_selections", &option_persistent_selections },
218 { "editor_cursor_beyond_eol", &option_cursor_beyond_eol },
219 { "editor_visible_tabs", &visible_tabs },
220 { "editor_visible_spaces", &visible_tws },
221 { "editor_line_state", &option_line_state },
222 { "editor_simple_statusbar", &simple_statusbar },
223 { "editor_check_new_line", &option_check_nl_at_eof },
224 { "editor_show_right_margin", &show_right_margin },
225 #endif /* USE_INTERNAL_EDIT */
227 { "nice_rotating_dash", &nice_rotating_dash },
228 { "horizontal_split", &horizontal_split },
229 { "mcview_remember_file_position", &mcview_remember_file_position },
230 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
231 { "reverse_files_only", &reverse_files_only },
232 { "copymove_persistent_attr", &setup_copymove_persistent_attr },
233 { "select_flags", &select_flags },
234 { 0, 0 }
237 extern char *user_recent_timeformat;
238 extern char *user_old_timeformat;
241 In order to use everywhere the same setup
242 for the locale we use defines
244 #define FMTYEAR _("%b %e %Y")
245 #define FMTTIME _("%b %e %H:%M")
247 static const struct {
248 const char *opt_name;
249 char **opt_addr;
250 const char *opt_defval;
251 } str_options [] = {
252 #ifdef USE_INTERNAL_EDIT
253 { "editor_backup_extension", &option_backup_ext, "~" },
254 #endif
255 { "mcview_eof", &mcview_show_eof, "" },
256 { NULL, NULL, NULL }
259 void
260 panel_save_setup (struct WPanel *panel, const char *section)
262 char *buffer;
263 size_t i;
265 mc_config_set_int(mc_panels_config, section, "reverse", panel->reverse);
266 mc_config_set_int(mc_panels_config, section, "case_sensitive", panel->case_sensitive);
267 mc_config_set_int(mc_panels_config, section, "exec_first", panel->exec_first);
270 mc_config_set_string(mc_panels_config, section, "sort_order", panel->current_sort_field->id);
272 for (i = 0; list_types [i].key; i++)
273 if (list_types [i].list_type == panel->list_type){
274 mc_config_set_string(mc_panels_config, section, "list_mode", list_types [i].key);
275 break;
278 mc_config_set_string(mc_panels_config, section, "user_format", panel->user_format);
280 for (i = 0; i < LIST_TYPES; i++){
281 buffer = g_strdup_printf("user_status%d", i);
282 mc_config_set_string(mc_panels_config, section, buffer, panel->user_status_format [i]);
283 g_free(buffer);
286 mc_config_set_int(mc_panels_config, section, "user_mini_status", panel->user_mini_status);
289 void
290 save_layout (void)
292 char *profile;
293 int i;
295 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
297 /* Save integer options */
298 for (i = 0; layout [i].opt_name; i++){
299 mc_config_set_int(mc_main_config, "Layout", layout [i].opt_name, *layout [i].opt_addr);
301 mc_config_save_to_file (mc_main_config, profile, NULL);
303 g_free (profile);
306 void
307 save_configure (void)
309 char *profile;
310 GError *error = NULL;
311 int i;
313 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
315 /* Save integer options */
316 for (i = 0; int_options[i].opt_name; i++)
317 mc_config_set_int(mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name, *int_options[i].opt_addr);
319 /* Save string options */
320 for (i = 0; str_options[i].opt_name != NULL; i++)
321 mc_config_set_string(mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name, *str_options[i].opt_addr);
323 if (! mc_config_save_to_file (mc_main_config, profile, &error))
324 setup_save_config_show_error(profile, &error);
326 g_free (profile);
329 static void
330 panel_save_type (const char *section, panel_view_mode_t type)
332 int i;
334 for (i = 0; panel_types [i].opt_name != NULL; i++)
335 if (panel_types [i].opt_type == type) {
336 mc_config_set_string (mc_panels_config, section,
337 "display", panel_types [i].opt_name);
338 break;
342 void
343 save_panel_types (void)
345 panel_view_mode_t type;
347 if (!mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
348 "auto_save_setup_panels", auto_save_setup))
349 return;
351 type = get_display_type (0);
352 panel_save_type ("New Left Panel", type);
353 if (type == view_listing)
354 panel_save_setup (left_panel, left_panel->panel_name);
355 type = get_display_type (1);
356 panel_save_type ("New Right Panel", type);
357 if (type == view_listing)
358 panel_save_setup (right_panel, right_panel->panel_name);
360 mc_config_set_string (mc_panels_config, "Dirs" , "other_dir",
361 get_panel_dir_for (other_panel));
363 if (current_panel != NULL)
364 mc_config_set_string (mc_panels_config, "Dirs" , "current_is_left",
365 get_current_index () == 0 ? "1" : "0");
367 if (mc_panels_config->ini_path == NULL)
368 mc_panels_config->ini_path = g_strdup (panels_profile_name);
370 mc_config_del_group (mc_panels_config, "Temporal:New Left Panel");
371 mc_config_del_group (mc_panels_config, "Temporal:New Right Panel");
373 mc_config_save_file (mc_panels_config, NULL);
376 gboolean
377 save_setup (void)
379 char *tmp_profile;
380 gboolean ret;
382 saving_setup = 1;
384 save_configure ();
386 save_layout ();
388 save_hotlist ();
390 save_panelize ();
391 save_panel_types ();
392 /* directory_history_save (); */
394 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
395 mc_config_set_string(mc_main_config, "Misc" , "ftpfs_password",
396 ftpfs_anonymous_passwd);
397 if (ftpfs_proxy_host)
398 mc_config_set_string(mc_main_config, "Misc" , "ftp_proxy_host",
399 ftpfs_proxy_host);
400 #endif /* ENABLE_VFS && USE_NETCODE */
402 #ifdef HAVE_CHARSET
403 mc_config_set_string (mc_main_config, "Misc" , "display_codepage",
404 get_codepage_id( display_codepage ));
405 mc_config_set_string (mc_main_config, "Misc" , "source_codepage",
406 get_codepage_id( default_source_codepage ));
407 mc_config_set_string (mc_main_config, "Misc" , "autodetect_codeset",
408 autodetect_codeset );
409 #endif /* HAVE_CHARSET */
410 tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
411 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
413 g_free (tmp_profile);
414 saving_setup = 0;
415 return ret;
418 void
419 panel_load_setup (WPanel *panel, const char *section)
421 size_t i;
422 char *buffer;
424 panel->reverse = mc_config_get_int(mc_panels_config, section, "reverse", 0);
425 panel->case_sensitive = mc_config_get_int(mc_panels_config, section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
426 panel->exec_first = mc_config_get_int(mc_panels_config, section, "exec_first", 0);
428 /* Load sort order */
429 buffer = mc_config_get_string(mc_panels_config, section, "sort_order", "name");
430 panel->current_sort_field = panel_get_field_by_id(buffer);
431 if (panel->current_sort_field == NULL )
432 panel->current_sort_field = panel_get_field_by_id("name");
434 g_free(buffer);
436 /* Load the listing mode */
437 buffer = mc_config_get_string(mc_panels_config, section, "list_mode", "full");
438 panel->list_type = list_full;
439 for (i = 0; list_types [i].key; i++)
440 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
441 panel->list_type = list_types [i].list_type;
442 break;
444 g_free(buffer);
446 /* User formats */
447 g_free (panel->user_format);
448 panel->user_format = mc_config_get_string(mc_panels_config, section, "user_format", DEFAULT_USER_FORMAT);
450 for (i = 0; i < LIST_TYPES; i++){
451 g_free (panel->user_status_format [i]);
452 buffer = g_strdup_printf("user_status%d",i);
453 panel->user_status_format [i] =
454 mc_config_get_string(mc_panels_config, section, buffer, DEFAULT_USER_FORMAT);
455 g_free(buffer);
458 panel->user_mini_status =
459 mc_config_get_int(mc_panels_config, section, "user_mini_status", 0);
463 static void
464 load_layout ()
466 int i;
468 for (i = 0; layout [i].opt_name; i++)
469 *layout [i].opt_addr =
470 mc_config_get_int(mc_main_config,"Layout", layout [i].opt_name, *layout [i].opt_addr);
473 static panel_view_mode_t
474 setup__load_panel_state (const char *section)
476 char *buffer;
477 size_t i;
478 panel_view_mode_t mode = view_listing;
480 /* Load the display mode */
481 buffer = mc_config_get_string (mc_panels_config, section, "display", "listing");
483 for (i = 0; panel_types [i].opt_name != NULL; i++)
484 if (g_strcasecmp (panel_types [i].opt_name, buffer) == 0) {
485 mode = panel_types [i].opt_type;
486 break;
488 g_free (buffer);
489 return mode;
492 static const char *
493 setup__is_cfg_group_must_panel_config(const char *grp)
495 if (
496 ! strcasecmp("Dirs",grp) ||
497 ! strcasecmp("Temporal:New Right Panel",grp) ||
498 ! strcasecmp("Temporal:New Left Panel",grp) ||
499 ! strcasecmp("New Left Panel",grp) ||
500 ! strcasecmp("New Right Panel",grp)
502 return grp;
503 return NULL;
506 static void
507 setup__move_panels_config_into_separate_file(const char*profile)
509 mc_config_t *tmp_cfg;
510 char **groups, **curr_grp;
511 const char *need_grp;
513 if (!exist_file(profile))
514 return;
516 tmp_cfg = mc_config_init(profile);
517 if (!tmp_cfg)
518 return;
520 curr_grp = groups = mc_config_get_groups (tmp_cfg, NULL);
521 if (!groups)
523 mc_config_deinit(tmp_cfg);
524 return;
526 while (*curr_grp)
528 if ( setup__is_cfg_group_must_panel_config(*curr_grp) == NULL)
530 mc_config_del_group (tmp_cfg, *curr_grp);
532 curr_grp++;
535 mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
536 mc_config_deinit(tmp_cfg);
538 tmp_cfg = mc_config_init(profile);
539 if (!tmp_cfg)
541 g_strfreev(groups);
542 return;
545 curr_grp = groups;
547 while (*curr_grp)
549 need_grp = setup__is_cfg_group_must_panel_config(*curr_grp);
550 if ( need_grp != NULL)
552 mc_config_del_group (tmp_cfg, need_grp);
554 curr_grp++;
556 g_strfreev(groups);
557 mc_config_save_file (tmp_cfg, NULL);
558 mc_config_deinit(tmp_cfg);
563 Get name of config file.
565 \param subdir
566 if not NULL, then config also search into specified subdir
568 \param config_file_name
569 If specified filename is relative, then will search in standart patches.
571 \return
572 Newly allocated path to config name or NULL if file not found
574 If config_file_name is a relative path, then search config in stantart pathes */
575 static char*
576 load_setup_get_full_config_name(const char *subdir, const char *config_file_name)
579 TODO: IMHO, in future this function must be placed into mc_config module.
580 Also, need to rename stupid mc_home and mc_home_alt to mc_sysconfdir and mc_datadir;
581 home_mc => mc_user_homedir
583 char *lc_basename, *ret;
585 if (config_file_name == NULL)
586 return NULL;
588 if (g_path_is_absolute (config_file_name))
589 return g_strdup(config_file_name);
592 lc_basename = g_path_get_basename(config_file_name);
593 if (lc_basename == NULL)
594 return NULL;
597 if (subdir)
598 ret = g_build_filename (home_dir, MC_USERCONF_DIR, subdir, lc_basename, NULL);
599 else
600 ret = g_build_filename (home_dir, MC_USERCONF_DIR, lc_basename, NULL);
602 if (exist_file(ret)) {
603 g_free(lc_basename);
604 return ret;
606 g_free(ret);
609 if (subdir)
610 ret = g_build_filename (mc_home, subdir, lc_basename, NULL);
611 else
612 ret = g_build_filename (mc_home, lc_basename, NULL);
614 if (exist_file(ret)) {
615 g_free(lc_basename);
616 return ret;
618 g_free(ret);
620 if (subdir)
621 ret = g_build_filename (mc_home_alt, subdir, lc_basename, NULL);
622 else
623 ret = g_build_filename (mc_home_alt, lc_basename, NULL);
625 if (exist_file(ret)) {
626 g_free(lc_basename);
627 return ret;
629 g_free(ret);
630 g_free(lc_basename);
631 return NULL;
636 Create new mc_config object from specified ini-file or
637 append data to existing mc_config object from ini-file
640 static void
641 load_setup_init_config_from_file(mc_config_t **config, const char *fname)
644 TODO: IMHO, in future this function must be placed into mc_config module.
646 if (exist_file(fname)) {
647 if (*config)
648 mc_config_read_file( *config, fname);
649 else
650 *config = mc_config_init(fname);
656 static mc_config_t *
657 load_setup_get_keymap_profile_config(void)
660 TODO: IMHO, in future this function must be placed into mc_config module.
662 mc_config_t *keymap_config = NULL ;
664 char *fname, *fname2;
666 /* 1) /usr/share/mc (mc_home_alt) */
667 fname = g_build_filename (mc_home_alt, GLOBAL_KEYMAP_FILE, NULL);
668 load_setup_init_config_from_file( &keymap_config, fname);
669 g_free(fname);
671 /* 2) /etc/mc (mc_home) */
672 fname = g_build_filename (mc_home, GLOBAL_KEYMAP_FILE, NULL);
673 load_setup_init_config_from_file( &keymap_config, fname);
674 g_free(fname);
676 /* 3) ~/.mc (home_dir?) */
677 fname = g_build_filename (home_dir, MC_USERCONF_DIR, GLOBAL_KEYMAP_FILE, NULL);
678 load_setup_init_config_from_file( &keymap_config, fname);
679 g_free(fname);
681 /* 4) main config; [Midnight Commander] -> keymap*/
683 fname2 = mc_config_get_string(mc_main_config, CONFIG_APP_SECTION, "keymap" , GLOBAL_KEYMAP_FILE);
684 fname = load_setup_get_full_config_name(NULL, fname2);
685 if (fname)
687 load_setup_init_config_from_file( &keymap_config, fname);
688 g_free(fname);
690 g_free(fname2);
692 /* 5) getenv("MC_KEYMAP") */
693 fname = load_setup_get_full_config_name(NULL, g_getenv ("MC_KEYMAP"));
694 if (fname)
696 load_setup_init_config_from_file( &keymap_config, fname);
697 g_free(fname);
700 /* 6) --keymap=<keymap> */
701 fname = load_setup_get_full_config_name(NULL, mc_args__keymap_file);
702 if (fname)
704 load_setup_init_config_from_file( &keymap_config, fname);
705 g_free(fname);
708 return keymap_config;
711 char *
712 setup_init (void)
714 char *profile;
715 char *inifile;
717 if (profile_name)
718 return profile_name;
720 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
721 if (!exist_file (profile)){
722 inifile = concat_dir_and_file (mc_home, "mc.ini");
723 if (exist_file (inifile)){
724 g_free (profile);
725 profile = inifile;
726 } else {
727 g_free (inifile);
728 inifile = concat_dir_and_file (mc_home_alt, "mc.ini");
729 if (exist_file (inifile)) {
730 g_free (profile);
731 profile = inifile;
732 } else
733 g_free (inifile);
737 profile_name = profile;
739 return profile;
742 void
743 load_setup (void)
745 char *profile;
746 int i;
747 char *buffer;
749 profile = setup_init ();
751 /* mc.lib is common for all users, but has priority lower than
752 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
753 global_profile_name = concat_dir_and_file (mc_home, MC_GLOBAL_CONFIG_FILE);
754 if (!exist_file (global_profile_name)) {
755 g_free (global_profile_name);
756 global_profile_name = concat_dir_and_file (mc_home_alt, MC_GLOBAL_CONFIG_FILE);
759 panels_profile_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_PANELS_FILE, NULL);
761 mc_main_config = mc_config_init (profile);
763 if (!exist_file(panels_profile_name))
764 setup__move_panels_config_into_separate_file (profile);
766 mc_panels_config = mc_config_init (panels_profile_name);
768 /* Load integer boolean options */
769 for (i = 0; int_options[i].opt_name; i++)
770 *int_options[i].opt_addr =
771 mc_config_get_int(mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name, *int_options[i].opt_addr);
773 /* Load string options */
774 for (i = 0; str_options[i].opt_name != NULL; i++)
775 *str_options[i].opt_addr =
776 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name, str_options[i].opt_defval);
778 load_layout ();
780 load_panelize ();
782 startup_left_mode = setup__load_panel_state ("New Left Panel");
783 startup_right_mode = setup__load_panel_state ("New Right Panel");
785 /* At least one of the panels is a listing panel */
786 if (startup_left_mode != view_listing && startup_right_mode != view_listing)
787 startup_left_mode = view_listing;
789 if (!other_dir){
790 buffer = mc_config_get_string (mc_panels_config, "Dirs", "other_dir", ".");
791 if (vfs_file_is_local (buffer))
792 other_dir = buffer;
793 else
794 g_free (buffer);
797 boot_current_is_left =
798 mc_config_get_int (mc_panels_config, "Dirs", "current_is_left", 1);
800 /* Load time formats */
801 user_recent_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_recent", FMTTIME);
802 user_old_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_old", FMTYEAR);
804 #ifdef USE_NETCODE
805 ftpfs_proxy_host = mc_config_get_string (mc_main_config, "Misc", "ftp_proxy_host", "gate");
806 #endif
808 /* The default color and the terminal dependent color */
809 setup_color_string = mc_config_get_string (mc_main_config, "Colors", "base_color", "");
810 term_color_string = mc_config_get_string (mc_main_config, "Colors", getenv ("TERM"), "");
811 color_terminal_string = mc_config_get_string (mc_main_config, "Colors", "color_terminals", "");
813 /* Load the directory history */
814 /* directory_history_load (); */
815 /* Remove the temporal entries */
816 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
817 ftpfs_init_passwd ();
818 #endif /* ENABLE_VFS && USE_NETCODE */
820 #ifdef HAVE_CHARSET
821 if (load_codepages_list () > 0) {
822 buffer = mc_config_get_string (mc_main_config, "Misc", "display_codepage", "");
823 if (buffer[0] != '\0') {
824 display_codepage = get_codepage_index (buffer);
825 cp_display = get_codepage_id (display_codepage);
827 g_free(buffer);
828 buffer = mc_config_get_string(mc_main_config, "Misc", "source_codepage", "");
829 if (buffer[0] != '\0') {
830 default_source_codepage = get_codepage_index (buffer);
831 source_codepage = default_source_codepage; /* May be source_codepage don't needed this */
832 cp_source = get_codepage_id (source_codepage);
834 g_free(buffer);
837 autodetect_codeset = mc_config_get_string (mc_main_config, "Misc", "autodetect_codeset", "");
838 if ((autodetect_codeset[0] != '\0') && (strcmp(autodetect_codeset, "off")))
839 is_autodetect_codeset_enabled=TRUE;
841 init_translation_table (source_codepage, display_codepage);
842 if (get_codepage_id (display_codepage))
843 utf8_display = str_isutf8 (get_codepage_id (display_codepage));
844 #endif /* HAVE_CHARSET */
847 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
848 char *
849 load_anon_passwd ()
851 char *buffer;
853 buffer = mc_config_get_string(mc_main_config, "Misc", "ftpfs_password", "");
854 if (buffer [0])
855 return buffer;
857 g_free(buffer);
858 return NULL;
860 #endif /* ENABLE_VFS && USE_NETCODE */
862 void
863 done_setup (void)
865 int i;
867 g_free (profile_name);
868 g_free (global_profile_name);
869 g_free (color_terminal_string);
870 g_free (term_color_string);
871 g_free (setup_color_string);
872 g_free (panels_profile_name);
873 mc_config_deinit (mc_main_config);
874 mc_config_deinit (mc_panels_config);
876 g_free(user_recent_timeformat);
877 g_free(user_old_timeformat);
879 for (i = 0; str_options[i].opt_name != NULL; i++)
880 g_free (*str_options[i].opt_addr);
882 done_hotlist ();
883 done_panelize ();
884 /* directory_history_free (); */
887 static void
888 load_keys_from_section (const char *terminal, mc_config_t *cfg)
890 char *section_name;
891 gchar **profile_keys, **keys;
892 gchar **values, **curr_values;
893 char *valcopy, *value;
894 long key_code;
895 gsize len, values_len;
897 if (!terminal)
898 return;
900 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
901 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
903 while (*profile_keys){
905 /* copy=other causes all keys from [terminal:other] to be loaded. */
906 if (g_strcasecmp (*profile_keys, "copy") == 0) {
907 valcopy = mc_config_get_string (cfg, section_name, *profile_keys, "");
908 load_keys_from_section (valcopy, cfg);
909 g_free(valcopy);
910 profile_keys++;
911 continue;
913 curr_values = values = mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
915 key_code = lookup_key (*profile_keys, NULL);
916 if (key_code != 0) {
917 if (curr_values){
918 while (*curr_values){
919 valcopy = convert_controls (*curr_values);
920 define_sequence (key_code, valcopy, MCKEY_NOACTION);
921 g_free (valcopy);
922 curr_values++;
924 } else {
925 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
926 valcopy = convert_controls (value);
927 define_sequence (key_code, valcopy, MCKEY_NOACTION);
928 g_free (valcopy);
929 g_free (value);
932 profile_keys++;
933 if (values)
934 g_strfreev (values);
936 g_strfreev (keys);
937 g_free (section_name);
940 void load_key_defs (void)
943 * Load keys from mc.lib before ~/.mc/ini, so that the user
944 * definitions override global settings.
946 mc_config_t *mc_global_config;
948 mc_global_config = mc_config_init(global_profile_name);
949 if (mc_global_config != NULL)
951 load_keys_from_section ("general", mc_global_config);
952 load_keys_from_section (getenv ("TERM"), mc_global_config);
953 mc_config_deinit(mc_global_config);
955 load_keys_from_section ("general", mc_main_config);
956 load_keys_from_section (getenv ("TERM"), mc_main_config);
960 static void
961 load_keymap_from_section (const char *section_name, GArray *keymap, mc_config_t *cfg)
963 gchar **profile_keys, **keys;
964 gchar **values, **curr_values;
965 char *valcopy, *value;
966 int action;
967 gsize len, values_len;
969 if (!section_name)
970 return;
972 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
974 while (*profile_keys) {
975 curr_values = values = mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
976 action = lookup_action (*profile_keys);
977 if (action>0) {
978 if (curr_values) {
979 while (*curr_values) {
980 valcopy = convert_controls (*curr_values);
981 keybind_cmd_bind (keymap, valcopy, action);
982 g_free (valcopy);
983 curr_values++;
985 } else {
986 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
987 valcopy = convert_controls (value);
988 /* define_sequence (key_code, valcopy, MCKEY_NOACTION); */
989 g_free (valcopy);
990 g_free (value);
993 profile_keys++;
994 if (values)
995 g_strfreev(values);
997 g_strfreev(keys);
1000 void
1001 load_keymap_defs (void)
1004 * Load keymap from GLOBAL_KEYMAP_FILE before ~/.mc/keymap, so that the user
1005 * definitions override global settings.
1007 mc_config_t *mc_global_keymap;
1009 mc_global_keymap = load_setup_get_keymap_profile_config();
1011 if (mc_global_keymap != NULL)
1013 #ifdef USE_INTERNAL_EDIT
1014 editor_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1015 load_keymap_from_section ("editor", editor_keymap, mc_global_keymap);
1016 editor_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1017 load_keymap_from_section ("editor:xmap", editor_x_keymap, mc_global_keymap);
1018 #endif
1020 viewer_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1021 load_keymap_from_section ("viewer", viewer_keymap, mc_global_keymap);
1022 viewer_hex_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1023 load_keymap_from_section ("viewer:hex", viewer_hex_keymap, mc_global_keymap);
1025 main_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1026 load_keymap_from_section ("main", main_keymap, mc_global_keymap);
1027 main_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1028 load_keymap_from_section ("main:xmap", main_x_keymap, mc_global_keymap);
1030 panel_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1031 load_keymap_from_section ("panel", panel_keymap, mc_global_keymap);
1033 input_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1034 load_keymap_from_section ("input", input_keymap, mc_global_keymap);
1036 tree_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1037 load_keymap_from_section ("tree", tree_keymap, mc_global_keymap);
1039 help_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1040 load_keymap_from_section ("help", help_keymap, mc_global_keymap);
1042 mc_config_deinit (mc_global_keymap);
1046 void
1047 free_keymap_defs (void)
1049 #ifdef USE_INTERNAL_EDIT
1050 if (editor_keymap != NULL)
1051 g_array_free (editor_keymap, TRUE);
1052 if (editor_x_keymap != NULL)
1053 g_array_free (editor_x_keymap, TRUE);
1054 #endif
1055 if (viewer_keymap != NULL)
1056 g_array_free (viewer_keymap, TRUE);
1057 if (viewer_hex_keymap != NULL)
1058 g_array_free (viewer_hex_keymap, TRUE);
1059 if (main_keymap != NULL)
1060 g_array_free (main_keymap, TRUE);
1061 if (main_x_keymap != NULL)
1062 g_array_free (main_x_keymap, TRUE);
1063 if (panel_keymap != NULL)
1064 g_array_free (panel_keymap, TRUE);
1065 if (input_keymap != NULL)
1066 g_array_free (input_keymap, TRUE);
1067 if (tree_keymap != NULL)
1068 g_array_free (tree_keymap, TRUE);
1069 if (help_keymap != NULL)
1070 g_array_free (help_keymap, TRUE);
1073 void
1074 setup_save_config_show_error(const char *filename, GError **error)
1076 if (error == NULL || *error == NULL)
1077 return;
1079 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"),
1080 filename, (*error)->message);
1082 g_error_free(*error);
1083 *error = NULL;