Identation of some source files.
[midnight-commander.git] / src / setup.c
blob78dfb1b657abf805dffa99861691c926cb810007
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 /* *INDENT-OFF* */
96 static const struct {
97 const char *key;
98 int list_type;
99 } list_types [] = {
100 { "full", list_full },
101 { "brief", list_brief },
102 { "long", list_long },
103 { "user", list_user },
104 { 0, 0 }
107 static const struct {
108 const char *opt_name;
109 panel_view_mode_t opt_type;
110 } panel_types [] = {
111 { "listing", view_listing },
112 { "quickview", view_quick },
113 { "info", view_info },
114 { "tree", view_tree },
115 { NULL, view_listing }
118 static const struct {
119 const char *opt_name;
120 int *opt_addr;
121 } layout [] = {
122 { "equal_split", &equal_split },
123 { "first_panel_size", &first_panel_size },
124 { "message_visible", &message_visible },
125 { "keybar_visible", &keybar_visible },
126 { "xterm_title", &xterm_title },
127 { "output_lines", &output_lines },
128 { "command_prompt", &command_prompt },
129 { "menubar_visible", &menubar_visible },
130 { "show_mini_info", &show_mini_info },
131 { "permission_mode", &permission_mode },
132 { "filetype_mode", &filetype_mode },
133 { "free_space", &free_space },
134 { 0, 0 }
137 static const struct {
138 const char *opt_name;
139 int *opt_addr;
140 } int_options [] = {
141 { "show_backups", &show_backups },
142 { "kilobyte_si", &kilobyte_si },
143 { "show_dot_files", &show_dot_files },
144 { "verbose", &verbose },
145 { "mark_moves_down", &mark_moves_down },
146 { "pause_after_run", &pause_after_run },
147 { "shell_patterns", &easy_patterns },
148 { "auto_save_setup", &auto_save_setup },
149 { "auto_menu", &auto_menu },
150 { "use_internal_view", &use_internal_view },
151 { "use_internal_edit", &use_internal_edit },
152 { "clear_before_exec", &clear_before_exec },
153 { "mix_all_files", &mix_all_files },
154 { "fast_reload", &fast_reload },
155 { "fast_reload_msg_shown", &fast_reload_w },
156 { "confirm_delete", &confirm_delete },
157 { "confirm_overwrite", &confirm_overwrite },
158 { "confirm_execute", &confirm_execute },
159 { "confirm_history_cleanup", &confirm_history_cleanup },
160 { "confirm_exit", &confirm_exit },
161 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
162 { "safe_delete", &safe_delete },
163 { "mouse_repeat_rate", &mou_auto_repeat },
164 { "double_click_speed", &double_click_speed },
165 #ifndef HAVE_CHARSET
166 { "eight_bit_clean", &eight_bit_clean },
167 { "full_eight_bits", &full_eight_bits },
168 #endif /* !HAVE_CHARSET */
169 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
170 { "confirm_view_dir", &confirm_view_dir },
171 { "mouse_move_pages", &mouse_move_pages },
172 { "mouse_move_pages_viewer", &mcview_mouse_move_pages },
173 { "mouse_close_dialog", &mouse_close_dialog},
174 { "fast_refresh", &fast_refresh },
175 { "navigate_with_arrows", &navigate_with_arrows },
176 { "drop_menus", &drop_menus },
177 { "wrap_mode", &mcview_global_wrap_mode},
178 { "old_esc_mode", &old_esc_mode },
179 { "cd_symlinks", &cd_symlinks },
180 { "show_all_if_ambiguous", &show_all_if_ambiguous },
181 { "max_dirt_limit", &mcview_max_dirt_limit },
182 { "torben_fj_mode", &torben_fj_mode },
183 { "use_file_to_guess_type", &use_file_to_check_type },
184 { "alternate_plus_minus", &alternate_plus_minus },
185 { "only_leading_plus_minus", &only_leading_plus_minus },
186 { "show_output_starts_shell", &output_starts_shell },
187 { "panel_scroll_pages", &panel_scroll_pages },
188 { "xtree_mode", &xtree_mode },
189 { "num_history_items_recorded", &num_history_items_recorded },
190 { "file_op_compute_totals", &file_op_compute_totals },
191 #ifdef ENABLE_VFS
192 { "vfs_timeout", &vfs_timeout },
193 #ifdef USE_NETCODE
194 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
195 { "use_netrc", &use_netrc },
196 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
197 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
198 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
199 { "ftpfs_use_passive_connections_over_proxy", &ftpfs_use_passive_connections_over_proxy },
200 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
201 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
202 { "fish_directory_timeout", &fish_directory_timeout },
203 #endif /* USE_NETCODE */
204 #endif /* ENABLE_VFS */
205 #ifdef USE_INTERNAL_EDIT
206 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
207 { "editor_tab_spacing", &option_tab_spacing },
208 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
209 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
210 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
211 { "editor_fake_half_tabs", &option_fake_half_tabs },
212 { "editor_option_save_mode", &option_save_mode },
213 { "editor_option_save_position", &option_save_position },
214 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
215 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
216 { "editor_edit_confirm_save", &edit_confirm_save },
217 { "editor_syntax_highlighting", &option_syntax_highlighting },
218 { "editor_persistent_selections", &option_persistent_selections },
219 { "editor_cursor_beyond_eol", &option_cursor_beyond_eol },
220 { "editor_visible_tabs", &visible_tabs },
221 { "editor_visible_spaces", &visible_tws },
222 { "editor_line_state", &option_line_state },
223 { "editor_simple_statusbar", &simple_statusbar },
224 { "editor_check_new_line", &option_check_nl_at_eof },
225 { "editor_show_right_margin", &show_right_margin },
226 #endif /* USE_INTERNAL_EDIT */
228 { "nice_rotating_dash", &nice_rotating_dash },
229 { "horizontal_split", &horizontal_split },
230 { "mcview_remember_file_position", &mcview_remember_file_position },
231 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
232 { "reverse_files_only", &reverse_files_only },
233 { "copymove_persistent_attr", &setup_copymove_persistent_attr },
234 { "select_flags", &select_flags },
235 { 0, 0 }
237 /* *INDENT-ON* */
239 extern char *user_recent_timeformat;
240 extern char *user_old_timeformat;
243 In order to use everywhere the same setup
244 for the locale we use defines
246 #define FMTYEAR _("%b %e %Y")
247 #define FMTTIME _("%b %e %H:%M")
249 static const struct
251 const char *opt_name;
252 char **opt_addr;
253 const char *opt_defval;
254 } str_options[] =
256 #ifdef USE_INTERNAL_EDIT
258 "editor_backup_extension", &option_backup_ext, "~"},
259 #endif
261 "mcview_eof", &mcview_show_eof, ""},
263 NULL, NULL, NULL}
266 void
267 panel_save_setup (struct WPanel *panel, const char *section)
269 char buffer[BUF_TINY];
270 size_t i;
272 mc_config_set_int (mc_panels_config, section, "reverse", panel->reverse);
273 mc_config_set_int (mc_panels_config, section, "case_sensitive", panel->case_sensitive);
274 mc_config_set_int (mc_panels_config, section, "exec_first", panel->exec_first);
277 mc_config_set_string (mc_panels_config, section, "sort_order", panel->current_sort_field->id);
279 for (i = 0; list_types[i].key; i++)
280 if (list_types[i].list_type == panel->list_type)
282 mc_config_set_string (mc_panels_config, section, "list_mode", list_types[i].key);
283 break;
286 mc_config_set_string (mc_panels_config, section, "user_format", panel->user_format);
288 for (i = 0; i < LIST_TYPES; i++)
290 g_snprintf (buffer, BUF_TINY, "user_status%lld", (long long) i);
291 mc_config_set_string (mc_panels_config, section, buffer, panel->user_status_format[i]);
294 mc_config_set_int (mc_panels_config, section, "user_mini_status", panel->user_mini_status);
297 void
298 save_layout (void)
300 char *profile;
301 int i;
303 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
305 /* Save integer options */
306 for (i = 0; layout[i].opt_name; i++)
308 mc_config_set_int (mc_main_config, "Layout", layout[i].opt_name, *layout[i].opt_addr);
310 mc_config_save_to_file (mc_main_config, profile, NULL);
312 g_free (profile);
315 void
316 save_configure (void)
318 char *profile;
319 GError *error = NULL;
320 int i;
322 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
324 /* Save integer options */
325 for (i = 0; int_options[i].opt_name; i++)
326 mc_config_set_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
327 *int_options[i].opt_addr);
329 /* Save string options */
330 for (i = 0; str_options[i].opt_name != NULL; i++)
331 mc_config_set_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
332 *str_options[i].opt_addr);
334 if (!mc_config_save_to_file (mc_main_config, profile, &error))
335 setup_save_config_show_error (profile, &error);
337 g_free (profile);
340 static void
341 panel_save_type (const char *section, panel_view_mode_t type)
343 int i;
345 for (i = 0; panel_types[i].opt_name != NULL; i++)
346 if (panel_types[i].opt_type == type)
348 mc_config_set_string (mc_panels_config, section, "display", panel_types[i].opt_name);
349 break;
353 void
354 save_panel_types (void)
356 panel_view_mode_t type;
358 if ((view_one_file != NULL) || (edit_one_file != NULL))
359 return;
361 if (!mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
362 "auto_save_setup_panels", auto_save_setup))
363 return;
365 type = get_display_type (0);
366 panel_save_type ("New Left Panel", type);
367 if (type == view_listing)
368 panel_save_setup (left_panel, left_panel->panel_name);
369 type = get_display_type (1);
370 panel_save_type ("New Right Panel", type);
371 if (type == view_listing)
372 panel_save_setup (right_panel, right_panel->panel_name);
374 mc_config_set_string (mc_panels_config, "Dirs", "other_dir", get_panel_dir_for (other_panel));
376 if (current_panel != NULL)
377 mc_config_set_string (mc_panels_config, "Dirs", "current_is_left",
378 get_current_index () == 0 ? "1" : "0");
380 if (mc_panels_config->ini_path == NULL)
381 mc_panels_config->ini_path = g_strdup (panels_profile_name);
383 mc_config_del_group (mc_panels_config, "Temporal:New Left Panel");
384 mc_config_del_group (mc_panels_config, "Temporal:New Right Panel");
386 mc_config_save_file (mc_panels_config, NULL);
389 gboolean
390 save_setup (void)
392 char *tmp_profile;
393 gboolean ret;
395 saving_setup = 1;
397 save_configure ();
399 save_layout ();
401 save_hotlist ();
403 save_panelize ();
404 save_panel_types ();
405 /* directory_history_save (); */
407 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
408 mc_config_set_string (mc_main_config, "Misc", "ftpfs_password", ftpfs_anonymous_passwd);
409 if (ftpfs_proxy_host)
410 mc_config_set_string (mc_main_config, "Misc", "ftp_proxy_host", ftpfs_proxy_host);
411 #endif /* ENABLE_VFS && USE_NETCODE */
413 #ifdef HAVE_CHARSET
414 mc_config_set_string (mc_main_config, "Misc", "display_codepage",
415 get_codepage_id (display_codepage));
416 mc_config_set_string (mc_main_config, "Misc", "source_codepage",
417 get_codepage_id (default_source_codepage));
418 mc_config_set_string (mc_main_config, "Misc", "autodetect_codeset", autodetect_codeset);
419 #endif /* HAVE_CHARSET */
420 tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
421 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
423 g_free (tmp_profile);
424 saving_setup = 0;
425 return ret;
428 void
429 panel_load_setup (WPanel * panel, const char *section)
431 size_t i;
432 char *buffer, buffer2[BUF_TINY];
434 panel->reverse = mc_config_get_int (mc_panels_config, section, "reverse", 0);
435 panel->case_sensitive =
436 mc_config_get_int (mc_panels_config, section, "case_sensitive",
437 OS_SORT_CASE_SENSITIVE_DEFAULT);
438 panel->exec_first = mc_config_get_int (mc_panels_config, section, "exec_first", 0);
440 /* Load sort order */
441 buffer = mc_config_get_string (mc_panels_config, section, "sort_order", "name");
442 panel->current_sort_field = panel_get_field_by_id (buffer);
443 if (panel->current_sort_field == NULL)
444 panel->current_sort_field = panel_get_field_by_id ("name");
446 g_free (buffer);
448 /* Load the listing mode */
449 buffer = mc_config_get_string (mc_panels_config, section, "list_mode", "full");
450 panel->list_type = list_full;
451 for (i = 0; list_types[i].key; i++)
452 if (g_strcasecmp (list_types[i].key, buffer) == 0)
454 panel->list_type = list_types[i].list_type;
455 break;
457 g_free (buffer);
459 /* User formats */
460 g_free (panel->user_format);
461 panel->user_format =
462 mc_config_get_string (mc_panels_config, section, "user_format", DEFAULT_USER_FORMAT);
464 for (i = 0; i < LIST_TYPES; i++)
466 g_free (panel->user_status_format[i]);
467 g_snprintf (buffer2, BUF_TINY, "user_status%lld", (long long) i);
468 panel->user_status_format[i] =
469 mc_config_get_string (mc_panels_config, section, buffer2, DEFAULT_USER_FORMAT);
472 panel->user_mini_status = mc_config_get_int (mc_panels_config, section, "user_mini_status", 0);
476 static void
477 load_layout ()
479 int i;
481 for (i = 0; layout[i].opt_name; i++)
482 *layout[i].opt_addr =
483 mc_config_get_int (mc_main_config, "Layout", layout[i].opt_name, *layout[i].opt_addr);
486 static panel_view_mode_t
487 setup__load_panel_state (const char *section)
489 char *buffer;
490 size_t i;
491 panel_view_mode_t mode = view_listing;
493 /* Load the display mode */
494 buffer = mc_config_get_string (mc_panels_config, section, "display", "listing");
496 for (i = 0; panel_types[i].opt_name != NULL; i++)
497 if (g_strcasecmp (panel_types[i].opt_name, buffer) == 0)
499 mode = panel_types[i].opt_type;
500 break;
502 g_free (buffer);
503 return mode;
506 static const char *
507 setup__is_cfg_group_must_panel_config (const char *grp)
509 if (!strcasecmp ("Dirs", grp) ||
510 !strcasecmp ("Temporal:New Right Panel", grp) ||
511 !strcasecmp ("Temporal:New Left Panel", grp) ||
512 !strcasecmp ("New Left Panel", grp) || !strcasecmp ("New Right Panel", grp))
513 return grp;
514 return NULL;
517 static void
518 setup__move_panels_config_into_separate_file (const char *profile)
520 mc_config_t *tmp_cfg;
521 char **groups, **curr_grp;
522 const char *need_grp;
524 if (!exist_file (profile))
525 return;
527 tmp_cfg = mc_config_init (profile);
528 if (!tmp_cfg)
529 return;
531 curr_grp = groups = mc_config_get_groups (tmp_cfg, NULL);
532 if (!groups)
534 mc_config_deinit (tmp_cfg);
535 return;
537 while (*curr_grp)
539 if (setup__is_cfg_group_must_panel_config (*curr_grp) == NULL)
541 mc_config_del_group (tmp_cfg, *curr_grp);
543 curr_grp++;
546 mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
547 mc_config_deinit (tmp_cfg);
549 tmp_cfg = mc_config_init (profile);
550 if (!tmp_cfg)
552 g_strfreev (groups);
553 return;
556 curr_grp = groups;
558 while (*curr_grp)
560 need_grp = setup__is_cfg_group_must_panel_config (*curr_grp);
561 if (need_grp != NULL)
563 mc_config_del_group (tmp_cfg, need_grp);
565 curr_grp++;
567 g_strfreev (groups);
568 mc_config_save_file (tmp_cfg, NULL);
569 mc_config_deinit (tmp_cfg);
574 Get name of config file.
576 \param subdir
577 if not NULL, then config also search into specified subdir
579 \param config_file_name
580 If specified filename is relative, then will search in standart patches.
582 \return
583 Newly allocated path to config name or NULL if file not found
585 If config_file_name is a relative path, then search config in stantart pathes */
586 static char *
587 load_setup_get_full_config_name (const char *subdir, const char *config_file_name)
590 TODO: IMHO, in future this function must be placed into mc_config module.
591 Also, need to rename stupid mc_home and mc_home_alt to mc_sysconfdir and mc_datadir;
592 home_mc => mc_user_homedir
594 char *lc_basename, *ret;
596 if (config_file_name == NULL)
597 return NULL;
599 if (g_path_is_absolute (config_file_name))
600 return g_strdup (config_file_name);
603 lc_basename = g_path_get_basename (config_file_name);
604 if (lc_basename == NULL)
605 return NULL;
608 if (subdir)
609 ret = g_build_filename (home_dir, MC_USERCONF_DIR, subdir, lc_basename, NULL);
610 else
611 ret = g_build_filename (home_dir, MC_USERCONF_DIR, lc_basename, NULL);
613 if (exist_file (ret))
615 g_free (lc_basename);
616 return ret;
618 g_free (ret);
621 if (subdir)
622 ret = g_build_filename (mc_home, subdir, lc_basename, NULL);
623 else
624 ret = g_build_filename (mc_home, lc_basename, NULL);
626 if (exist_file (ret))
628 g_free (lc_basename);
629 return ret;
631 g_free (ret);
633 if (subdir)
634 ret = g_build_filename (mc_home_alt, subdir, lc_basename, NULL);
635 else
636 ret = g_build_filename (mc_home_alt, lc_basename, NULL);
638 if (exist_file (ret))
640 g_free (lc_basename);
641 return ret;
643 g_free (ret);
644 g_free (lc_basename);
645 return NULL;
650 Create new mc_config object from specified ini-file or
651 append data to existing mc_config object from ini-file
654 static void
655 load_setup_init_config_from_file (mc_config_t ** config, const char *fname)
658 TODO: IMHO, in future this function must be placed into mc_config module.
660 if (exist_file (fname))
662 if (*config)
663 mc_config_read_file (*config, fname);
664 else
665 *config = mc_config_init (fname);
671 static mc_config_t *
672 load_setup_get_keymap_profile_config (void)
675 TODO: IMHO, in future this function must be placed into mc_config module.
677 mc_config_t *keymap_config = NULL;
679 char *fname, *fname2;
681 /* 1) /usr/share/mc (mc_home_alt) */
682 fname = g_build_filename (mc_home_alt, GLOBAL_KEYMAP_FILE, NULL);
683 load_setup_init_config_from_file (&keymap_config, fname);
684 g_free (fname);
686 /* 2) /etc/mc (mc_home) */
687 fname = g_build_filename (mc_home, GLOBAL_KEYMAP_FILE, NULL);
688 load_setup_init_config_from_file (&keymap_config, fname);
689 g_free (fname);
691 /* 3) ~/.mc (home_dir?) */
692 fname = g_build_filename (home_dir, MC_USERCONF_DIR, GLOBAL_KEYMAP_FILE, NULL);
693 load_setup_init_config_from_file (&keymap_config, fname);
694 g_free (fname);
696 /* 4) main config; [Midnight Commander] -> keymap */
698 fname2 =
699 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "keymap", GLOBAL_KEYMAP_FILE);
700 fname = load_setup_get_full_config_name (NULL, fname2);
701 if (fname)
703 load_setup_init_config_from_file (&keymap_config, fname);
704 g_free (fname);
706 g_free (fname2);
708 /* 5) getenv("MC_KEYMAP") */
709 fname = load_setup_get_full_config_name (NULL, g_getenv ("MC_KEYMAP"));
710 if (fname)
712 load_setup_init_config_from_file (&keymap_config, fname);
713 g_free (fname);
716 /* 6) --keymap=<keymap> */
717 fname = load_setup_get_full_config_name (NULL, mc_args__keymap_file);
718 if (fname)
720 load_setup_init_config_from_file (&keymap_config, fname);
721 g_free (fname);
724 return keymap_config;
727 char *
728 setup_init (void)
730 char *profile;
731 char *inifile;
733 if (profile_name)
734 return profile_name;
736 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
737 if (!exist_file (profile))
739 inifile = concat_dir_and_file (mc_home, "mc.ini");
740 if (exist_file (inifile))
742 g_free (profile);
743 profile = inifile;
745 else
747 g_free (inifile);
748 inifile = concat_dir_and_file (mc_home_alt, "mc.ini");
749 if (exist_file (inifile))
751 g_free (profile);
752 profile = inifile;
754 else
755 g_free (inifile);
759 profile_name = profile;
761 return profile;
764 void
765 load_setup (void)
767 char *profile;
768 int i;
769 char *buffer;
771 profile = setup_init ();
773 /* mc.lib is common for all users, but has priority lower than
774 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
775 global_profile_name = concat_dir_and_file (mc_home, MC_GLOBAL_CONFIG_FILE);
776 if (!exist_file (global_profile_name))
778 g_free (global_profile_name);
779 global_profile_name = concat_dir_and_file (mc_home_alt, MC_GLOBAL_CONFIG_FILE);
782 panels_profile_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_PANELS_FILE, NULL);
784 mc_main_config = mc_config_init (profile);
786 if (!exist_file (panels_profile_name))
787 setup__move_panels_config_into_separate_file (profile);
789 mc_panels_config = mc_config_init (panels_profile_name);
791 /* Load integer boolean options */
792 for (i = 0; int_options[i].opt_name; i++)
793 *int_options[i].opt_addr =
794 mc_config_get_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
795 *int_options[i].opt_addr);
797 /* Load string options */
798 for (i = 0; str_options[i].opt_name != NULL; i++)
799 *str_options[i].opt_addr =
800 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
801 str_options[i].opt_defval);
803 load_layout ();
805 load_panelize ();
807 startup_left_mode = setup__load_panel_state ("New Left Panel");
808 startup_right_mode = setup__load_panel_state ("New Right Panel");
810 /* At least one of the panels is a listing panel */
811 if (startup_left_mode != view_listing && startup_right_mode != view_listing)
812 startup_left_mode = view_listing;
814 if (!other_dir)
816 buffer = mc_config_get_string (mc_panels_config, "Dirs", "other_dir", ".");
817 if (vfs_file_is_local (buffer))
818 other_dir = buffer;
819 else
820 g_free (buffer);
823 boot_current_is_left = mc_config_get_int (mc_panels_config, "Dirs", "current_is_left", 1);
825 /* Load time formats */
826 user_recent_timeformat =
827 mc_config_get_string (mc_main_config, "Misc", "timeformat_recent", FMTTIME);
828 user_old_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_old", FMTYEAR);
830 #ifdef USE_NETCODE
831 ftpfs_proxy_host = mc_config_get_string (mc_main_config, "Misc", "ftp_proxy_host", "gate");
832 #endif
834 /* The default color and the terminal dependent color */
835 setup_color_string = mc_config_get_string (mc_main_config, "Colors", "base_color", "");
836 term_color_string = mc_config_get_string (mc_main_config, "Colors", getenv ("TERM"), "");
837 color_terminal_string = mc_config_get_string (mc_main_config, "Colors", "color_terminals", "");
839 /* Load the directory history */
840 /* directory_history_load (); */
841 /* Remove the temporal entries */
842 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
843 ftpfs_init_passwd ();
844 #endif /* ENABLE_VFS && USE_NETCODE */
846 #ifdef HAVE_CHARSET
847 if (load_codepages_list () > 0)
849 buffer = mc_config_get_string (mc_main_config, "Misc", "display_codepage", "");
850 if (buffer[0] != '\0')
852 display_codepage = get_codepage_index (buffer);
853 cp_display = get_codepage_id (display_codepage);
855 g_free (buffer);
856 buffer = mc_config_get_string (mc_main_config, "Misc", "source_codepage", "");
857 if (buffer[0] != '\0')
859 default_source_codepage = get_codepage_index (buffer);
860 source_codepage = default_source_codepage; /* May be source_codepage don't needed this */
861 cp_source = get_codepage_id (source_codepage);
863 g_free (buffer);
866 autodetect_codeset = mc_config_get_string (mc_main_config, "Misc", "autodetect_codeset", "");
867 if ((autodetect_codeset[0] != '\0') && (strcmp (autodetect_codeset, "off")))
868 is_autodetect_codeset_enabled = TRUE;
870 init_translation_table (source_codepage, display_codepage);
871 if (get_codepage_id (display_codepage))
872 utf8_display = str_isutf8 (get_codepage_id (display_codepage));
873 #endif /* HAVE_CHARSET */
876 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
877 char *
878 load_anon_passwd ()
880 char *buffer;
882 buffer = mc_config_get_string (mc_main_config, "Misc", "ftpfs_password", "");
883 if (buffer[0])
884 return buffer;
886 g_free (buffer);
887 return NULL;
889 #endif /* ENABLE_VFS && USE_NETCODE */
891 void
892 done_setup (void)
894 int i;
896 g_free (profile_name);
897 g_free (global_profile_name);
898 g_free (color_terminal_string);
899 g_free (term_color_string);
900 g_free (setup_color_string);
901 g_free (panels_profile_name);
902 mc_config_deinit (mc_main_config);
903 mc_config_deinit (mc_panels_config);
905 g_free (user_recent_timeformat);
906 g_free (user_old_timeformat);
908 for (i = 0; str_options[i].opt_name != NULL; i++)
909 g_free (*str_options[i].opt_addr);
911 done_hotlist ();
912 done_panelize ();
913 /* directory_history_free (); */
916 static void
917 load_keys_from_section (const char *terminal, mc_config_t * cfg)
919 char *section_name;
920 gchar **profile_keys, **keys;
921 gchar **values, **curr_values;
922 char *valcopy, *value;
923 long key_code;
924 gsize len, values_len;
926 if (!terminal)
927 return;
929 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
930 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
932 while (*profile_keys)
935 /* copy=other causes all keys from [terminal:other] to be loaded. */
936 if (g_strcasecmp (*profile_keys, "copy") == 0)
938 valcopy = mc_config_get_string (cfg, section_name, *profile_keys, "");
939 load_keys_from_section (valcopy, cfg);
940 g_free (valcopy);
941 profile_keys++;
942 continue;
944 curr_values = values =
945 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
947 key_code = lookup_key (*profile_keys, NULL);
948 if (key_code != 0)
950 if (curr_values)
952 while (*curr_values)
954 valcopy = convert_controls (*curr_values);
955 define_sequence (key_code, valcopy, MCKEY_NOACTION);
956 g_free (valcopy);
957 curr_values++;
960 else
962 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
963 valcopy = convert_controls (value);
964 define_sequence (key_code, valcopy, MCKEY_NOACTION);
965 g_free (valcopy);
966 g_free (value);
969 profile_keys++;
970 if (values)
971 g_strfreev (values);
973 g_strfreev (keys);
974 g_free (section_name);
977 void
978 load_key_defs (void)
981 * Load keys from mc.lib before ~/.mc/ini, so that the user
982 * definitions override global settings.
984 mc_config_t *mc_global_config;
986 mc_global_config = mc_config_init (global_profile_name);
987 if (mc_global_config != NULL)
989 load_keys_from_section ("general", mc_global_config);
990 load_keys_from_section (getenv ("TERM"), mc_global_config);
991 mc_config_deinit (mc_global_config);
993 load_keys_from_section ("general", mc_main_config);
994 load_keys_from_section (getenv ("TERM"), mc_main_config);
998 static void
999 load_keymap_from_section (const char *section_name, GArray * keymap, mc_config_t * cfg)
1001 gchar **profile_keys, **keys;
1002 gchar **values, **curr_values;
1003 char *valcopy, *value;
1004 int action;
1005 gsize len, values_len;
1007 if (!section_name)
1008 return;
1010 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
1012 while (*profile_keys)
1014 curr_values = values =
1015 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
1016 action = lookup_action (*profile_keys);
1017 if (action > 0)
1019 if (curr_values)
1021 while (*curr_values)
1023 valcopy = convert_controls (*curr_values);
1024 keybind_cmd_bind (keymap, valcopy, action);
1025 g_free (valcopy);
1026 curr_values++;
1029 else
1031 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
1032 valcopy = convert_controls (value);
1033 /* define_sequence (key_code, valcopy, MCKEY_NOACTION); */
1034 g_free (valcopy);
1035 g_free (value);
1038 profile_keys++;
1039 if (values)
1040 g_strfreev (values);
1042 g_strfreev (keys);
1045 void
1046 load_keymap_defs (void)
1049 * Load keymap from GLOBAL_KEYMAP_FILE before ~/.mc/keymap, so that the user
1050 * definitions override global settings.
1052 mc_config_t *mc_global_keymap;
1054 mc_global_keymap = load_setup_get_keymap_profile_config ();
1056 if (mc_global_keymap != NULL)
1058 #ifdef USE_INTERNAL_EDIT
1059 editor_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1060 load_keymap_from_section ("editor", editor_keymap, mc_global_keymap);
1061 editor_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1062 load_keymap_from_section ("editor:xmap", editor_x_keymap, mc_global_keymap);
1063 #endif
1065 viewer_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1066 load_keymap_from_section ("viewer", viewer_keymap, mc_global_keymap);
1067 viewer_hex_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1068 load_keymap_from_section ("viewer:hex", viewer_hex_keymap, mc_global_keymap);
1070 main_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1071 load_keymap_from_section ("main", main_keymap, mc_global_keymap);
1072 main_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1073 load_keymap_from_section ("main:xmap", main_x_keymap, mc_global_keymap);
1075 panel_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1076 load_keymap_from_section ("panel", panel_keymap, mc_global_keymap);
1078 input_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1079 load_keymap_from_section ("input", input_keymap, mc_global_keymap);
1081 tree_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1082 load_keymap_from_section ("tree", tree_keymap, mc_global_keymap);
1084 help_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1085 load_keymap_from_section ("help", help_keymap, mc_global_keymap);
1087 mc_config_deinit (mc_global_keymap);
1091 void
1092 free_keymap_defs (void)
1094 #ifdef USE_INTERNAL_EDIT
1095 if (editor_keymap != NULL)
1096 g_array_free (editor_keymap, TRUE);
1097 if (editor_x_keymap != NULL)
1098 g_array_free (editor_x_keymap, TRUE);
1099 #endif
1100 if (viewer_keymap != NULL)
1101 g_array_free (viewer_keymap, TRUE);
1102 if (viewer_hex_keymap != NULL)
1103 g_array_free (viewer_hex_keymap, TRUE);
1104 if (main_keymap != NULL)
1105 g_array_free (main_keymap, TRUE);
1106 if (main_x_keymap != NULL)
1107 g_array_free (main_x_keymap, TRUE);
1108 if (panel_keymap != NULL)
1109 g_array_free (panel_keymap, TRUE);
1110 if (input_keymap != NULL)
1111 g_array_free (input_keymap, TRUE);
1112 if (tree_keymap != NULL)
1113 g_array_free (tree_keymap, TRUE);
1114 if (help_keymap != NULL)
1115 g_array_free (help_keymap, TRUE);
1118 void
1119 setup_save_config_show_error (const char *filename, GError ** error)
1121 if (error == NULL || *error == NULL)
1122 return;
1124 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"), filename, (*error)->message);
1126 g_error_free (*error);
1127 *error = NULL;