vfs_timeout and use_netrc variables were moved to vfs.h file.
[midnight-commander.git] / src / setup.c
blob556af5747e712755b13a8a53f81654d8138d9e2b
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"
39 #include "lib/strutil.h" /* str_isutf8 () */
41 #ifdef USE_NETCODE
42 # include "lib/vfs/mc-vfs/ftpfs.h"
43 # include "lib/vfs/mc-vfs/fish.h"
44 #endif
47 #include "args.h"
48 #include "dir.h"
49 #include "panel.h"
50 #include "main.h"
51 #include "tree.h" /* xtree_mode */
52 #include "setup.h"
53 #include "src/viewer/mcviewer.h" /* For the externs */
54 #include "hotlist.h" /* load/save/done hotlist */
55 #include "panelize.h" /* load/save/done panelize */
56 #include "layout.h"
57 #include "menu.h" /* menubar_visible declaration */
58 #include "cmd.h"
59 #include "file.h" /* safe_delete */
60 #include "keybind.h" /* lookup_action */
61 #include "wtools.h"
63 #ifdef HAVE_CHARSET
64 #include "charsets.h"
65 #endif
67 #ifdef USE_INTERNAL_EDIT
68 # include "src/editor/edit.h"
69 #endif
73 extern int num_history_items_recorded;
75 char *profile_name; /* .mc/ini */
76 char *global_profile_name; /* mc.lib */
78 char *setup_color_string;
79 char *term_color_string;
80 char *color_terminal_string;
82 panel_view_mode_t startup_left_mode;
83 panel_view_mode_t startup_right_mode;
85 /* Ugly hack to allow panel_save_setup to work as a place holder for */
86 /* default panel values */
87 int saving_setup;
88 int setup_copymove_persistent_attr = 1;
90 static char *panels_profile_name = NULL; /* .mc/panels.ini */
92 /* *INDENT-OFF* */
93 static const struct {
94 const char *key;
95 int list_type;
96 } list_types [] = {
97 { "full", list_full },
98 { "brief", list_brief },
99 { "long", list_long },
100 { "user", list_user },
101 { 0, 0 }
104 static const struct {
105 const char *opt_name;
106 panel_view_mode_t opt_type;
107 } panel_types [] = {
108 { "listing", view_listing },
109 { "quickview", view_quick },
110 { "info", view_info },
111 { "tree", view_tree },
112 { NULL, view_listing }
115 static const struct {
116 const char *opt_name;
117 int *opt_addr;
118 } layout [] = {
119 { "equal_split", &equal_split },
120 { "first_panel_size", &first_panel_size },
121 { "message_visible", &message_visible },
122 { "keybar_visible", &keybar_visible },
123 { "xterm_title", &xterm_title },
124 { "output_lines", &output_lines },
125 { "command_prompt", &command_prompt },
126 { "menubar_visible", &menubar_visible },
127 { "show_mini_info", &show_mini_info },
128 { "permission_mode", &permission_mode },
129 { "filetype_mode", &filetype_mode },
130 { "free_space", &free_space },
131 { 0, 0 }
134 static const struct {
135 const char *opt_name;
136 int *opt_addr;
137 } int_options [] = {
138 { "show_backups", &show_backups },
139 { "kilobyte_si", &kilobyte_si },
140 { "show_dot_files", &show_dot_files },
141 { "verbose", &verbose },
142 { "mark_moves_down", &mark_moves_down },
143 { "pause_after_run", &pause_after_run },
144 { "shell_patterns", &easy_patterns },
145 { "auto_save_setup", &auto_save_setup },
146 { "auto_menu", &auto_menu },
147 { "use_internal_view", &use_internal_view },
148 { "use_internal_edit", &use_internal_edit },
149 { "clear_before_exec", &clear_before_exec },
150 { "mix_all_files", &mix_all_files },
151 { "fast_reload", &fast_reload },
152 { "fast_reload_msg_shown", &fast_reload_w },
153 { "confirm_delete", &confirm_delete },
154 { "confirm_overwrite", &confirm_overwrite },
155 { "confirm_execute", &confirm_execute },
156 { "confirm_history_cleanup", &confirm_history_cleanup },
157 { "confirm_exit", &confirm_exit },
158 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
159 { "safe_delete", &safe_delete },
160 { "mouse_repeat_rate", &mou_auto_repeat },
161 { "double_click_speed", &double_click_speed },
162 #ifndef HAVE_CHARSET
163 { "eight_bit_clean", &eight_bit_clean },
164 { "full_eight_bits", &full_eight_bits },
165 #endif /* !HAVE_CHARSET */
166 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
167 { "confirm_view_dir", &confirm_view_dir },
168 { "mouse_move_pages", &mouse_move_pages },
169 { "mouse_move_pages_viewer", &mcview_mouse_move_pages },
170 { "mouse_close_dialog", &mouse_close_dialog},
171 { "fast_refresh", &fast_refresh },
172 { "navigate_with_arrows", &navigate_with_arrows },
173 { "drop_menus", &drop_menus },
174 { "wrap_mode", &mcview_global_wrap_mode},
175 { "old_esc_mode", &old_esc_mode },
176 { "cd_symlinks", &cd_symlinks },
177 { "show_all_if_ambiguous", &show_all_if_ambiguous },
178 { "max_dirt_limit", &mcview_max_dirt_limit },
179 { "torben_fj_mode", &torben_fj_mode },
180 { "use_file_to_guess_type", &use_file_to_check_type },
181 { "alternate_plus_minus", &alternate_plus_minus },
182 { "only_leading_plus_minus", &only_leading_plus_minus },
183 { "show_output_starts_shell", &output_starts_shell },
184 { "panel_scroll_pages", &panel_scroll_pages },
185 { "xtree_mode", &xtree_mode },
186 { "num_history_items_recorded", &num_history_items_recorded },
187 { "file_op_compute_totals", &file_op_compute_totals },
188 #ifdef ENABLE_VFS
189 { "vfs_timeout", &vfs_timeout },
190 #ifdef USE_NETCODE
191 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
192 { "use_netrc", &use_netrc },
193 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
194 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
195 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
196 { "ftpfs_use_passive_connections_over_proxy", &ftpfs_use_passive_connections_over_proxy },
197 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
198 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
199 { "fish_directory_timeout", &fish_directory_timeout },
200 #endif /* USE_NETCODE */
201 #endif /* ENABLE_VFS */
202 #ifdef USE_INTERNAL_EDIT
203 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
204 { "editor_tab_spacing", &option_tab_spacing },
205 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
206 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
207 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
208 { "editor_fake_half_tabs", &option_fake_half_tabs },
209 { "editor_option_save_mode", &option_save_mode },
210 { "editor_option_save_position", &option_save_position },
211 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
212 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
213 { "editor_edit_confirm_save", &edit_confirm_save },
214 { "editor_syntax_highlighting", &option_syntax_highlighting },
215 { "editor_persistent_selections", &option_persistent_selections },
216 { "editor_cursor_beyond_eol", &option_cursor_beyond_eol },
217 { "editor_visible_tabs", &visible_tabs },
218 { "editor_visible_spaces", &visible_tws },
219 { "editor_line_state", &option_line_state },
220 { "editor_simple_statusbar", &simple_statusbar },
221 { "editor_check_new_line", &option_check_nl_at_eof },
222 { "editor_show_right_margin", &show_right_margin },
223 #endif /* USE_INTERNAL_EDIT */
225 { "nice_rotating_dash", &nice_rotating_dash },
226 { "horizontal_split", &horizontal_split },
227 { "mcview_remember_file_position", &mcview_remember_file_position },
228 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
229 { "reverse_files_only", &reverse_files_only },
230 { "copymove_persistent_attr", &setup_copymove_persistent_attr },
231 { "select_flags", &select_flags },
232 { "quick_search_case_sensitive", &quick_search_case_sensitive },
233 { 0, 0 }
235 /* *INDENT-ON* */
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
249 const char *opt_name;
250 char **opt_addr;
251 const char *opt_defval;
252 } str_options[] =
254 #ifdef USE_INTERNAL_EDIT
256 "editor_backup_extension", &option_backup_ext, "~"},
257 #endif
259 "mcview_eof", &mcview_show_eof, ""},
261 NULL, NULL, NULL}
264 void
265 panel_save_setup (struct WPanel *panel, const char *section)
267 char buffer[BUF_TINY];
268 size_t i;
270 mc_config_set_int (mc_panels_config, section, "reverse", panel->reverse);
271 mc_config_set_int (mc_panels_config, section, "case_sensitive", panel->case_sensitive);
272 mc_config_set_int (mc_panels_config, section, "exec_first", panel->exec_first);
275 mc_config_set_string (mc_panels_config, section, "sort_order", panel->current_sort_field->id);
277 for (i = 0; list_types[i].key; i++)
278 if (list_types[i].list_type == panel->list_type)
280 mc_config_set_string (mc_panels_config, section, "list_mode", list_types[i].key);
281 break;
284 mc_config_set_string (mc_panels_config, section, "user_format", panel->user_format);
286 for (i = 0; i < LIST_TYPES; i++)
288 g_snprintf (buffer, BUF_TINY, "user_status%lld", (long long) i);
289 mc_config_set_string (mc_panels_config, section, buffer, panel->user_status_format[i]);
292 mc_config_set_int (mc_panels_config, section, "user_mini_status", panel->user_mini_status);
295 void
296 save_layout (void)
298 char *profile;
299 int i;
301 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
303 /* Save integer options */
304 for (i = 0; layout[i].opt_name; i++)
306 mc_config_set_int (mc_main_config, "Layout", layout[i].opt_name, *layout[i].opt_addr);
308 mc_config_save_to_file (mc_main_config, profile, NULL);
310 g_free (profile);
313 void
314 save_configure (void)
316 char *profile;
317 GError *error = NULL;
318 int i;
320 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
322 /* Save integer options */
323 for (i = 0; int_options[i].opt_name; i++)
324 mc_config_set_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
325 *int_options[i].opt_addr);
327 /* Save string options */
328 for (i = 0; str_options[i].opt_name != NULL; i++)
329 mc_config_set_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
330 *str_options[i].opt_addr);
332 if (!mc_config_save_to_file (mc_main_config, profile, &error))
333 setup_save_config_show_error (profile, &error);
335 g_free (profile);
338 static void
339 panel_save_type (const char *section, panel_view_mode_t type)
341 int i;
343 for (i = 0; panel_types[i].opt_name != NULL; i++)
344 if (panel_types[i].opt_type == type)
346 mc_config_set_string (mc_panels_config, section, "display", panel_types[i].opt_name);
347 break;
351 void
352 save_panel_types (void)
354 panel_view_mode_t type;
356 if (mc_run_mode != MC_RUN_FULL)
357 return;
359 if (!mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
360 "auto_save_setup_panels", auto_save_setup))
361 return;
363 type = get_display_type (0);
364 panel_save_type ("New Left Panel", type);
365 if (type == view_listing)
366 panel_save_setup (left_panel, left_panel->panel_name);
367 type = get_display_type (1);
368 panel_save_type ("New Right Panel", type);
369 if (type == view_listing)
370 panel_save_setup (right_panel, right_panel->panel_name);
372 mc_config_set_string (mc_panels_config, "Dirs", "other_dir", get_panel_dir_for (other_panel));
374 if (current_panel != NULL)
375 mc_config_set_string (mc_panels_config, "Dirs", "current_is_left",
376 get_current_index () == 0 ? "1" : "0");
378 if (mc_panels_config->ini_path == NULL)
379 mc_panels_config->ini_path = g_strdup (panels_profile_name);
381 mc_config_del_group (mc_panels_config, "Temporal:New Left Panel");
382 mc_config_del_group (mc_panels_config, "Temporal:New Right Panel");
384 mc_config_save_file (mc_panels_config, NULL);
387 gboolean
388 save_setup (void)
390 char *tmp_profile;
391 gboolean ret;
393 saving_setup = 1;
395 save_configure ();
397 save_layout ();
399 save_hotlist ();
401 save_panelize ();
402 save_panel_types ();
403 /* directory_history_save (); */
405 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
406 mc_config_set_string (mc_main_config, "Misc", "ftpfs_password", ftpfs_anonymous_passwd);
407 if (ftpfs_proxy_host)
408 mc_config_set_string (mc_main_config, "Misc", "ftp_proxy_host", ftpfs_proxy_host);
409 #endif /* ENABLE_VFS && USE_NETCODE */
411 #ifdef HAVE_CHARSET
412 mc_config_set_string (mc_main_config, "Misc", "display_codepage",
413 get_codepage_id (display_codepage));
414 mc_config_set_string (mc_main_config, "Misc", "source_codepage",
415 get_codepage_id (default_source_codepage));
416 mc_config_set_string (mc_main_config, "Misc", "autodetect_codeset", autodetect_codeset);
417 #endif /* HAVE_CHARSET */
418 tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
419 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
421 g_free (tmp_profile);
422 saving_setup = 0;
423 return ret;
426 void
427 panel_load_setup (WPanel * panel, const char *section)
429 size_t i;
430 char *buffer, buffer2[BUF_TINY];
432 panel->reverse = mc_config_get_int (mc_panels_config, section, "reverse", 0);
433 panel->case_sensitive =
434 mc_config_get_int (mc_panels_config, section, "case_sensitive",
435 OS_SORT_CASE_SENSITIVE_DEFAULT);
436 panel->exec_first = mc_config_get_int (mc_panels_config, section, "exec_first", 0);
438 /* Load sort order */
439 buffer = mc_config_get_string (mc_panels_config, section, "sort_order", "name");
440 panel->current_sort_field = panel_get_field_by_id (buffer);
441 if (panel->current_sort_field == NULL)
442 panel->current_sort_field = panel_get_field_by_id ("name");
444 g_free (buffer);
446 /* Load the listing mode */
447 buffer = mc_config_get_string (mc_panels_config, section, "list_mode", "full");
448 panel->list_type = list_full;
449 for (i = 0; list_types[i].key; i++)
450 if (g_strcasecmp (list_types[i].key, buffer) == 0)
452 panel->list_type = list_types[i].list_type;
453 break;
455 g_free (buffer);
457 /* User formats */
458 g_free (panel->user_format);
459 panel->user_format =
460 mc_config_get_string (mc_panels_config, section, "user_format", DEFAULT_USER_FORMAT);
462 for (i = 0; i < LIST_TYPES; i++)
464 g_free (panel->user_status_format[i]);
465 g_snprintf (buffer2, BUF_TINY, "user_status%lld", (long long) i);
466 panel->user_status_format[i] =
467 mc_config_get_string (mc_panels_config, section, buffer2, DEFAULT_USER_FORMAT);
470 panel->user_mini_status = mc_config_get_int (mc_panels_config, section, "user_mini_status", 0);
474 static void
475 load_layout ()
477 int i;
479 for (i = 0; layout[i].opt_name; i++)
480 *layout[i].opt_addr =
481 mc_config_get_int (mc_main_config, "Layout", layout[i].opt_name, *layout[i].opt_addr);
484 static panel_view_mode_t
485 setup__load_panel_state (const char *section)
487 char *buffer;
488 size_t i;
489 panel_view_mode_t mode = view_listing;
491 /* Load the display mode */
492 buffer = mc_config_get_string (mc_panels_config, section, "display", "listing");
494 for (i = 0; panel_types[i].opt_name != NULL; i++)
495 if (g_strcasecmp (panel_types[i].opt_name, buffer) == 0)
497 mode = panel_types[i].opt_type;
498 break;
500 g_free (buffer);
501 return mode;
504 static const char *
505 setup__is_cfg_group_must_panel_config (const char *grp)
507 if (!strcasecmp ("Dirs", grp) ||
508 !strcasecmp ("Temporal:New Right Panel", grp) ||
509 !strcasecmp ("Temporal:New Left Panel", grp) ||
510 !strcasecmp ("New Left Panel", grp) || !strcasecmp ("New Right Panel", grp))
511 return grp;
512 return NULL;
515 static void
516 setup__move_panels_config_into_separate_file (const char *profile)
518 mc_config_t *tmp_cfg;
519 char **groups, **curr_grp;
520 const char *need_grp;
522 if (!exist_file (profile))
523 return;
525 tmp_cfg = mc_config_init (profile);
526 if (!tmp_cfg)
527 return;
529 curr_grp = groups = mc_config_get_groups (tmp_cfg, NULL);
530 if (!groups)
532 mc_config_deinit (tmp_cfg);
533 return;
535 while (*curr_grp)
537 if (setup__is_cfg_group_must_panel_config (*curr_grp) == NULL)
539 mc_config_del_group (tmp_cfg, *curr_grp);
541 curr_grp++;
544 mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
545 mc_config_deinit (tmp_cfg);
547 tmp_cfg = mc_config_init (profile);
548 if (!tmp_cfg)
550 g_strfreev (groups);
551 return;
554 curr_grp = groups;
556 while (*curr_grp)
558 need_grp = setup__is_cfg_group_must_panel_config (*curr_grp);
559 if (need_grp != NULL)
561 mc_config_del_group (tmp_cfg, need_grp);
563 curr_grp++;
565 g_strfreev (groups);
566 mc_config_save_file (tmp_cfg, NULL);
567 mc_config_deinit (tmp_cfg);
572 Get name of config file.
574 \param subdir
575 if not NULL, then config also search into specified subdir
577 \param config_file_name
578 If specified filename is relative, then will search in standart patches.
580 \return
581 Newly allocated path to config name or NULL if file not found
583 If config_file_name is a relative path, then search config in stantart pathes */
584 static char *
585 load_setup_get_full_config_name (const char *subdir, const char *config_file_name)
588 TODO: IMHO, in future this function must be placed into mc_config module.
589 Also, need to rename stupid mc_home and mc_home_alt to mc_sysconfdir and mc_datadir;
590 home_mc => mc_user_homedir
592 char *lc_basename, *ret;
594 if (config_file_name == NULL)
595 return NULL;
597 if (g_path_is_absolute (config_file_name))
598 return g_strdup (config_file_name);
601 lc_basename = g_path_get_basename (config_file_name);
602 if (lc_basename == NULL)
603 return NULL;
606 if (subdir)
607 ret = g_build_filename (home_dir, MC_USERCONF_DIR, subdir, lc_basename, NULL);
608 else
609 ret = g_build_filename (home_dir, MC_USERCONF_DIR, lc_basename, NULL);
611 if (exist_file (ret))
613 g_free (lc_basename);
614 return ret;
616 g_free (ret);
619 if (subdir)
620 ret = g_build_filename (mc_home, subdir, lc_basename, NULL);
621 else
622 ret = g_build_filename (mc_home, lc_basename, NULL);
624 if (exist_file (ret))
626 g_free (lc_basename);
627 return ret;
629 g_free (ret);
631 if (subdir)
632 ret = g_build_filename (mc_home_alt, subdir, lc_basename, NULL);
633 else
634 ret = g_build_filename (mc_home_alt, lc_basename, NULL);
636 if (exist_file (ret))
638 g_free (lc_basename);
639 return ret;
641 g_free (ret);
642 g_free (lc_basename);
643 return NULL;
648 Create new mc_config object from specified ini-file or
649 append data to existing mc_config object from ini-file
652 static void
653 load_setup_init_config_from_file (mc_config_t ** config, const char *fname)
656 TODO: IMHO, in future this function must be placed into mc_config module.
658 if (exist_file (fname))
660 if (*config)
661 mc_config_read_file (*config, fname);
662 else
663 *config = mc_config_init (fname);
669 static mc_config_t *
670 load_setup_get_keymap_profile_config (void)
673 TODO: IMHO, in future this function must be placed into mc_config module.
675 mc_config_t *keymap_config = NULL;
677 char *fname, *fname2;
679 /* 1) /usr/share/mc (mc_home_alt) */
680 fname = g_build_filename (mc_home_alt, GLOBAL_KEYMAP_FILE, NULL);
681 load_setup_init_config_from_file (&keymap_config, fname);
682 g_free (fname);
684 /* 2) /etc/mc (mc_home) */
685 fname = g_build_filename (mc_home, GLOBAL_KEYMAP_FILE, NULL);
686 load_setup_init_config_from_file (&keymap_config, fname);
687 g_free (fname);
689 /* 3) ~/.mc (home_dir?) */
690 fname = g_build_filename (home_dir, MC_USERCONF_DIR, GLOBAL_KEYMAP_FILE, NULL);
691 load_setup_init_config_from_file (&keymap_config, fname);
692 g_free (fname);
694 /* 4) main config; [Midnight Commander] -> keymap */
696 fname2 =
697 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "keymap", GLOBAL_KEYMAP_FILE);
698 fname = load_setup_get_full_config_name (NULL, fname2);
699 if (fname)
701 load_setup_init_config_from_file (&keymap_config, fname);
702 g_free (fname);
704 g_free (fname2);
706 /* 5) getenv("MC_KEYMAP") */
707 fname = load_setup_get_full_config_name (NULL, g_getenv ("MC_KEYMAP"));
708 if (fname)
710 load_setup_init_config_from_file (&keymap_config, fname);
711 g_free (fname);
714 /* 6) --keymap=<keymap> */
715 fname = load_setup_get_full_config_name (NULL, mc_args__keymap_file);
716 if (fname)
718 load_setup_init_config_from_file (&keymap_config, fname);
719 g_free (fname);
722 return keymap_config;
725 char *
726 setup_init (void)
728 char *profile;
729 char *inifile;
731 if (profile_name)
732 return profile_name;
734 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
735 if (!exist_file (profile))
737 inifile = concat_dir_and_file (mc_home, "mc.ini");
738 if (exist_file (inifile))
740 g_free (profile);
741 profile = inifile;
743 else
745 g_free (inifile);
746 inifile = concat_dir_and_file (mc_home_alt, "mc.ini");
747 if (exist_file (inifile))
749 g_free (profile);
750 profile = inifile;
752 else
753 g_free (inifile);
757 profile_name = profile;
759 return profile;
762 void
763 load_setup (void)
765 char *profile;
766 int i;
767 char *buffer;
769 profile = setup_init ();
771 /* mc.lib is common for all users, but has priority lower than
772 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
773 global_profile_name = concat_dir_and_file (mc_home, MC_GLOBAL_CONFIG_FILE);
774 if (!exist_file (global_profile_name))
776 g_free (global_profile_name);
777 global_profile_name = concat_dir_and_file (mc_home_alt, MC_GLOBAL_CONFIG_FILE);
780 panels_profile_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_PANELS_FILE, NULL);
782 mc_main_config = mc_config_init (profile);
784 if (!exist_file (panels_profile_name))
785 setup__move_panels_config_into_separate_file (profile);
787 mc_panels_config = mc_config_init (panels_profile_name);
789 /* Load integer boolean options */
790 for (i = 0; int_options[i].opt_name; i++)
791 *int_options[i].opt_addr =
792 mc_config_get_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
793 *int_options[i].opt_addr);
795 /* Load string options */
796 for (i = 0; str_options[i].opt_name != NULL; i++)
797 *str_options[i].opt_addr =
798 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
799 str_options[i].opt_defval);
801 load_layout ();
803 load_panelize ();
805 startup_left_mode = setup__load_panel_state ("New Left Panel");
806 startup_right_mode = setup__load_panel_state ("New Right Panel");
808 /* At least one of the panels is a listing panel */
809 if (startup_left_mode != view_listing && startup_right_mode != view_listing)
810 startup_left_mode = view_listing;
812 if (mc_run_param1 == NULL)
814 buffer = mc_config_get_string (mc_panels_config, "Dirs", "other_dir", ".");
815 if (vfs_file_is_local (buffer))
816 mc_run_param1 = buffer;
817 else
818 g_free (buffer);
821 boot_current_is_left = mc_config_get_int (mc_panels_config, "Dirs", "current_is_left", 1);
823 /* Load time formats */
824 user_recent_timeformat =
825 mc_config_get_string (mc_main_config, "Misc", "timeformat_recent", FMTTIME);
826 user_old_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_old", FMTYEAR);
828 #ifdef USE_NETCODE
829 ftpfs_proxy_host = mc_config_get_string (mc_main_config, "Misc", "ftp_proxy_host", "gate");
830 #endif
832 /* The default color and the terminal dependent color */
833 setup_color_string = mc_config_get_string (mc_main_config, "Colors", "base_color", "");
834 term_color_string = mc_config_get_string (mc_main_config, "Colors", getenv ("TERM"), "");
835 color_terminal_string = mc_config_get_string (mc_main_config, "Colors", "color_terminals", "");
837 /* Load the directory history */
838 /* directory_history_load (); */
839 /* Remove the temporal entries */
840 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
841 ftpfs_init_passwd ();
842 #endif /* ENABLE_VFS && USE_NETCODE */
844 #ifdef HAVE_CHARSET
845 if (load_codepages_list () > 0)
847 buffer = mc_config_get_string (mc_main_config, "Misc", "display_codepage", "");
848 if (buffer[0] != '\0')
850 display_codepage = get_codepage_index (buffer);
851 cp_display = get_codepage_id (display_codepage);
853 g_free (buffer);
854 buffer = mc_config_get_string (mc_main_config, "Misc", "source_codepage", "");
855 if (buffer[0] != '\0')
857 default_source_codepage = get_codepage_index (buffer);
858 source_codepage = default_source_codepage; /* May be source_codepage don't needed this */
859 cp_source = get_codepage_id (source_codepage);
861 g_free (buffer);
864 autodetect_codeset = mc_config_get_string (mc_main_config, "Misc", "autodetect_codeset", "");
865 if ((autodetect_codeset[0] != '\0') && (strcmp (autodetect_codeset, "off")))
866 is_autodetect_codeset_enabled = TRUE;
868 g_free (init_translation_table (source_codepage, display_codepage));
869 if (get_codepage_id (display_codepage))
870 utf8_display = str_isutf8 (get_codepage_id (display_codepage));
871 #endif /* HAVE_CHARSET */
874 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
875 char *
876 load_anon_passwd ()
878 char *buffer;
880 buffer = mc_config_get_string (mc_main_config, "Misc", "ftpfs_password", "");
881 if (buffer[0])
882 return buffer;
884 g_free (buffer);
885 return NULL;
887 #endif /* ENABLE_VFS && USE_NETCODE */
889 void
890 done_setup (void)
892 int i;
894 g_free (profile_name);
895 g_free (global_profile_name);
896 g_free (color_terminal_string);
897 g_free (term_color_string);
898 g_free (setup_color_string);
899 g_free (panels_profile_name);
900 mc_config_deinit (mc_main_config);
901 mc_config_deinit (mc_panels_config);
903 g_free (user_recent_timeformat);
904 g_free (user_old_timeformat);
906 for (i = 0; str_options[i].opt_name != NULL; i++)
907 g_free (*str_options[i].opt_addr);
909 done_hotlist ();
910 done_panelize ();
911 /* directory_history_free (); */
914 static void
915 load_keys_from_section (const char *terminal, mc_config_t * cfg)
917 char *section_name;
918 gchar **profile_keys, **keys;
919 gchar **values, **curr_values;
920 char *valcopy, *value;
921 long key_code;
922 gsize len, values_len;
924 if (!terminal)
925 return;
927 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
928 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
930 while (*profile_keys)
933 /* copy=other causes all keys from [terminal:other] to be loaded. */
934 if (g_strcasecmp (*profile_keys, "copy") == 0)
936 valcopy = mc_config_get_string (cfg, section_name, *profile_keys, "");
937 load_keys_from_section (valcopy, cfg);
938 g_free (valcopy);
939 profile_keys++;
940 continue;
942 curr_values = values =
943 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
945 key_code = lookup_key (*profile_keys, NULL);
946 if (key_code != 0)
948 if (curr_values)
950 while (*curr_values)
952 valcopy = convert_controls (*curr_values);
953 define_sequence (key_code, valcopy, MCKEY_NOACTION);
954 g_free (valcopy);
955 curr_values++;
958 else
960 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
961 valcopy = convert_controls (value);
962 define_sequence (key_code, valcopy, MCKEY_NOACTION);
963 g_free (valcopy);
964 g_free (value);
967 profile_keys++;
968 if (values)
969 g_strfreev (values);
971 g_strfreev (keys);
972 g_free (section_name);
975 void
976 load_key_defs (void)
979 * Load keys from mc.lib before ~/.mc/ini, so that the user
980 * definitions override global settings.
982 mc_config_t *mc_global_config;
984 mc_global_config = mc_config_init (global_profile_name);
985 if (mc_global_config != NULL)
987 load_keys_from_section ("general", mc_global_config);
988 load_keys_from_section (getenv ("TERM"), mc_global_config);
989 mc_config_deinit (mc_global_config);
991 load_keys_from_section ("general", mc_main_config);
992 load_keys_from_section (getenv ("TERM"), mc_main_config);
996 static void
997 load_keymap_from_section (const char *section_name, GArray * keymap, mc_config_t * cfg)
999 gchar **profile_keys, **keys;
1000 gchar **values, **curr_values;
1001 char *valcopy, *value;
1002 int action;
1003 gsize len, values_len;
1005 if (!section_name)
1006 return;
1008 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
1010 while (*profile_keys)
1012 curr_values = values =
1013 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
1014 action = lookup_action (*profile_keys);
1015 if (action > 0)
1017 if (curr_values)
1019 while (*curr_values)
1021 valcopy = convert_controls (*curr_values);
1022 keybind_cmd_bind (keymap, valcopy, action);
1023 g_free (valcopy);
1024 curr_values++;
1027 else
1029 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
1030 valcopy = convert_controls (value);
1031 /* define_sequence (key_code, valcopy, MCKEY_NOACTION); */
1032 g_free (valcopy);
1033 g_free (value);
1036 profile_keys++;
1037 if (values)
1038 g_strfreev (values);
1040 g_strfreev (keys);
1043 void
1044 load_keymap_defs (void)
1047 * Load keymap from GLOBAL_KEYMAP_FILE before ~/.mc/keymap, so that the user
1048 * definitions override global settings.
1050 mc_config_t *mc_global_keymap;
1052 mc_global_keymap = load_setup_get_keymap_profile_config ();
1054 if (mc_global_keymap != NULL)
1056 #ifdef USE_INTERNAL_EDIT
1057 editor_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1058 load_keymap_from_section ("editor", editor_keymap, mc_global_keymap);
1059 editor_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1060 load_keymap_from_section ("editor:xmap", editor_x_keymap, mc_global_keymap);
1061 #endif
1063 viewer_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1064 load_keymap_from_section ("viewer", viewer_keymap, mc_global_keymap);
1065 viewer_hex_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1066 load_keymap_from_section ("viewer:hex", viewer_hex_keymap, mc_global_keymap);
1068 main_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1069 load_keymap_from_section ("main", main_keymap, mc_global_keymap);
1070 main_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1071 load_keymap_from_section ("main:xmap", main_x_keymap, mc_global_keymap);
1073 panel_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1074 load_keymap_from_section ("panel", panel_keymap, mc_global_keymap);
1076 input_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1077 load_keymap_from_section ("input", input_keymap, mc_global_keymap);
1079 tree_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1080 load_keymap_from_section ("tree", tree_keymap, mc_global_keymap);
1082 help_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1083 load_keymap_from_section ("help", help_keymap, mc_global_keymap);
1085 #ifdef USE_DIFF_VIEW
1086 diff_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1087 load_keymap_from_section ("diffviewer", diff_keymap, mc_global_keymap);
1088 #endif
1089 mc_config_deinit (mc_global_keymap);
1093 void
1094 free_keymap_defs (void)
1096 #ifdef USE_INTERNAL_EDIT
1097 if (editor_keymap != NULL)
1098 g_array_free (editor_keymap, TRUE);
1099 if (editor_x_keymap != NULL)
1100 g_array_free (editor_x_keymap, TRUE);
1101 #endif
1102 if (viewer_keymap != NULL)
1103 g_array_free (viewer_keymap, TRUE);
1104 if (viewer_hex_keymap != NULL)
1105 g_array_free (viewer_hex_keymap, TRUE);
1106 if (main_keymap != NULL)
1107 g_array_free (main_keymap, TRUE);
1108 if (main_x_keymap != NULL)
1109 g_array_free (main_x_keymap, TRUE);
1110 if (panel_keymap != NULL)
1111 g_array_free (panel_keymap, TRUE);
1112 if (input_keymap != NULL)
1113 g_array_free (input_keymap, TRUE);
1114 if (tree_keymap != NULL)
1115 g_array_free (tree_keymap, TRUE);
1116 if (help_keymap != NULL)
1117 g_array_free (help_keymap, TRUE);
1118 #ifdef USE_DIFF_VIEW
1119 if (diff_keymap != NULL)
1120 g_array_free (diff_keymap, TRUE);
1121 #endif
1124 void
1125 setup_save_config_show_error (const char *filename, GError ** error)
1127 if (error == NULL || *error == NULL)
1128 return;
1130 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"), filename, (*error)->message);
1132 g_error_free (*error);
1133 *error = NULL;