Removed unused defines.
[midnight-commander.git] / src / setup.c
blob9e2b78a8125f4e898dab094699724d12421f2482
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 char *find_ignore_dirs;
78 extern int num_history_items_recorded;
80 char *profile_name; /* .mc/ini */
81 char *global_profile_name; /* mc.lib */
83 char *setup_color_string;
84 char *term_color_string;
85 char *color_terminal_string;
87 panel_view_mode_t startup_left_mode;
88 panel_view_mode_t startup_right_mode;
90 /* Ugly hack to allow panel_save_setup to work as a place holder for */
91 /* default panel values */
92 int saving_setup;
93 int setup_copymove_persistent_attr = 1;
95 static char *panels_profile_name = NULL; /* .mc/panels.ini */
97 /* *INDENT-OFF* */
98 static const struct {
99 const char *key;
100 int list_type;
101 } list_types [] = {
102 { "full", list_full },
103 { "brief", list_brief },
104 { "long", list_long },
105 { "user", list_user },
106 { 0, 0 }
109 static const struct {
110 const char *opt_name;
111 panel_view_mode_t opt_type;
112 } panel_types [] = {
113 { "listing", view_listing },
114 { "quickview", view_quick },
115 { "info", view_info },
116 { "tree", view_tree },
117 { NULL, view_listing }
120 static const struct {
121 const char *opt_name;
122 int *opt_addr;
123 } layout [] = {
124 { "equal_split", &equal_split },
125 { "first_panel_size", &first_panel_size },
126 { "message_visible", &message_visible },
127 { "keybar_visible", &keybar_visible },
128 { "xterm_title", &xterm_title },
129 { "output_lines", &output_lines },
130 { "command_prompt", &command_prompt },
131 { "menubar_visible", &menubar_visible },
132 { "show_mini_info", &show_mini_info },
133 { "permission_mode", &permission_mode },
134 { "filetype_mode", &filetype_mode },
135 { "free_space", &free_space },
136 { 0, 0 }
139 static const struct {
140 const char *opt_name;
141 int *opt_addr;
142 } int_options [] = {
143 { "show_backups", &show_backups },
144 { "kilobyte_si", &kilobyte_si },
145 { "show_dot_files", &show_dot_files },
146 { "verbose", &verbose },
147 { "mark_moves_down", &mark_moves_down },
148 { "pause_after_run", &pause_after_run },
149 { "shell_patterns", &easy_patterns },
150 { "auto_save_setup", &auto_save_setup },
151 { "auto_menu", &auto_menu },
152 { "use_internal_view", &use_internal_view },
153 { "use_internal_edit", &use_internal_edit },
154 { "clear_before_exec", &clear_before_exec },
155 { "mix_all_files", &mix_all_files },
156 { "fast_reload", &fast_reload },
157 { "fast_reload_msg_shown", &fast_reload_w },
158 { "confirm_delete", &confirm_delete },
159 { "confirm_overwrite", &confirm_overwrite },
160 { "confirm_execute", &confirm_execute },
161 { "confirm_exit", &confirm_exit },
162 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
163 { "safe_delete", &safe_delete },
164 { "mouse_repeat_rate", &mou_auto_repeat },
165 { "double_click_speed", &double_click_speed },
166 #ifndef HAVE_CHARSET
167 { "eight_bit_clean", &eight_bit_clean },
168 { "full_eight_bits", &full_eight_bits },
169 #endif /* !HAVE_CHARSET */
170 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
171 { "confirm_view_dir", &confirm_view_dir },
172 { "mouse_move_pages", &mouse_move_pages },
173 { "mouse_move_pages_viewer", &mcview_mouse_move_pages },
174 { "mouse_close_dialog", &mouse_close_dialog},
175 { "fast_refresh", &fast_refresh },
176 { "navigate_with_arrows", &navigate_with_arrows },
177 { "drop_menus", &drop_menus },
178 { "wrap_mode", &mcview_global_wrap_mode},
179 { "old_esc_mode", &old_esc_mode },
180 { "cd_symlinks", &cd_symlinks },
181 { "show_all_if_ambiguous", &show_all_if_ambiguous },
182 { "max_dirt_limit", &mcview_max_dirt_limit },
183 { "torben_fj_mode", &torben_fj_mode },
184 { "use_file_to_guess_type", &use_file_to_check_type },
185 { "alternate_plus_minus", &alternate_plus_minus },
186 { "only_leading_plus_minus", &only_leading_plus_minus },
187 { "show_output_starts_shell", &output_starts_shell },
188 { "panel_scroll_pages", &panel_scroll_pages },
189 { "xtree_mode", &xtree_mode },
190 { "num_history_items_recorded", &num_history_items_recorded },
191 { "file_op_compute_totals", &file_op_compute_totals },
192 #ifdef ENABLE_VFS
193 { "vfs_timeout", &vfs_timeout },
194 #ifdef USE_NETCODE
195 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
196 { "use_netrc", &use_netrc },
197 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
198 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
199 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
200 { "ftpfs_use_passive_connections_over_proxy", &ftpfs_use_passive_connections_over_proxy },
201 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
202 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
203 { "fish_directory_timeout", &fish_directory_timeout },
204 #endif /* USE_NETCODE */
205 #endif /* ENABLE_VFS */
206 #ifdef USE_INTERNAL_EDIT
207 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
208 { "editor_tab_spacing", &option_tab_spacing },
209 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
210 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
211 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
212 { "editor_fake_half_tabs", &option_fake_half_tabs },
213 { "editor_option_save_mode", &option_save_mode },
214 { "editor_option_save_position", &option_save_position },
215 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
216 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
217 { "editor_edit_confirm_save", &edit_confirm_save },
218 { "editor_syntax_highlighting", &option_syntax_highlighting },
219 { "editor_persistent_selections", &option_persistent_selections },
220 { "editor_cursor_beyond_eol", &option_cursor_beyond_eol },
221 { "editor_visible_tabs", &visible_tabs },
222 { "editor_visible_spaces", &visible_tws },
223 { "editor_line_state", &option_line_state },
224 { "editor_simple_statusbar", &simple_statusbar },
225 { "editor_check_new_line", &option_check_nl_at_eof },
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 static const struct
241 const char *opt_name;
242 char **opt_addr;
243 const char *opt_defval;
244 } str_options[] =
246 #ifdef USE_INTERNAL_EDIT
248 "editor_backup_extension", &option_backup_ext, "~"},
249 #endif
251 NULL, NULL, NULL}
254 void
255 panel_save_setup (struct WPanel *panel, const char *section)
257 char buffer[BUF_TINY];
258 size_t i;
260 mc_config_set_int (mc_panels_config, section, "reverse", panel->reverse);
261 mc_config_set_int (mc_panels_config, section, "case_sensitive", panel->case_sensitive);
262 mc_config_set_int (mc_panels_config, section, "exec_first", panel->exec_first);
265 mc_config_set_string (mc_panels_config, section, "sort_order", panel->current_sort_field->id);
267 for (i = 0; list_types[i].key; i++)
268 if (list_types[i].list_type == panel->list_type)
270 mc_config_set_string (mc_panels_config, section, "list_mode", list_types[i].key);
271 break;
274 mc_config_set_string (mc_panels_config, section, "user_format", panel->user_format);
276 for (i = 0; i < LIST_TYPES; i++)
278 g_snprintf (buffer, BUF_TINY, "user_status%lld", (long long) i);
279 mc_config_set_string (mc_panels_config, section, buffer, panel->user_status_format[i]);
282 mc_config_set_int (mc_panels_config, section, "user_mini_status", panel->user_mini_status);
285 void
286 save_layout (void)
288 char *profile;
289 int i;
291 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
293 /* Save integer options */
294 for (i = 0; layout[i].opt_name; i++)
296 mc_config_set_int (mc_main_config, "Layout", layout[i].opt_name, *layout[i].opt_addr);
298 mc_config_save_to_file (mc_main_config, profile, NULL);
300 g_free (profile);
303 void
304 save_configure (void)
306 char *profile;
307 GError *error = NULL;
308 int i;
310 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
312 /* Save integer options */
313 for (i = 0; int_options[i].opt_name; i++)
314 mc_config_set_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
315 *int_options[i].opt_addr);
317 /* Save string options */
318 for (i = 0; str_options[i].opt_name != NULL; i++)
319 mc_config_set_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
320 *str_options[i].opt_addr);
322 if (!mc_config_save_to_file (mc_main_config, profile, &error))
323 setup_save_config_show_error (profile, &error);
325 g_free (profile);
328 static void
329 panel_save_type (const char *section, panel_view_mode_t type)
331 int i;
333 for (i = 0; panel_types[i].opt_name != NULL; i++)
334 if (panel_types[i].opt_type == type)
336 mc_config_set_string (mc_panels_config, section, "display", panel_types[i].opt_name);
337 break;
341 void
342 save_panel_types (void)
344 panel_view_mode_t type;
346 if ((view_one_file != NULL) || (edit_one_file != NULL))
347 return;
349 if (!mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
350 "auto_save_setup_panels", auto_save_setup))
351 return;
353 type = get_display_type (0);
354 panel_save_type ("New Left Panel", type);
355 if (type == view_listing)
356 panel_save_setup (left_panel, left_panel->panel_name);
357 type = get_display_type (1);
358 panel_save_type ("New Right Panel", type);
359 if (type == view_listing)
360 panel_save_setup (right_panel, right_panel->panel_name);
362 mc_config_set_string (mc_panels_config, "Dirs", "other_dir", get_panel_dir_for (other_panel));
364 if (current_panel != NULL)
365 mc_config_set_string (mc_panels_config, "Dirs", "current_is_left",
366 get_current_index () == 0 ? "1" : "0");
368 if (mc_panels_config->ini_path == NULL)
369 mc_panels_config->ini_path = g_strdup (panels_profile_name);
371 mc_config_del_group (mc_panels_config, "Temporal:New Left Panel");
372 mc_config_del_group (mc_panels_config, "Temporal:New Right Panel");
374 mc_config_save_file (mc_panels_config, NULL);
377 gboolean
378 save_setup (void)
380 char *tmp_profile;
381 gboolean ret;
383 saving_setup = 1;
385 save_configure ();
387 save_layout ();
389 save_hotlist ();
391 save_panelize ();
392 save_panel_types ();
393 /* directory_history_save (); */
395 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
396 mc_config_set_string (mc_main_config, "Misc", "ftpfs_password", ftpfs_anonymous_passwd);
397 if (ftpfs_proxy_host)
398 mc_config_set_string (mc_main_config, "Misc", "ftp_proxy_host", ftpfs_proxy_host);
399 #endif /* ENABLE_VFS && USE_NETCODE */
401 #ifdef HAVE_CHARSET
402 mc_config_set_string (mc_main_config, "Misc", "display_codepage",
403 get_codepage_id (display_codepage));
404 mc_config_set_string (mc_main_config, "Misc", "source_codepage",
405 get_codepage_id (source_codepage));
406 #endif /* HAVE_CHARSET */
407 tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
408 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
410 g_free (tmp_profile);
411 saving_setup = 0;
412 return ret;
415 void
416 panel_load_setup (WPanel * panel, const char *section)
418 size_t i;
419 char *buffer, buffer2[BUF_TINY];
421 panel->reverse = mc_config_get_int (mc_panels_config, section, "reverse", 0);
422 panel->case_sensitive =
423 mc_config_get_int (mc_panels_config, section, "case_sensitive",
424 OS_SORT_CASE_SENSITIVE_DEFAULT);
425 panel->exec_first = mc_config_get_int (mc_panels_config, section, "exec_first", 0);
427 /* Load sort order */
428 buffer = mc_config_get_string (mc_panels_config, section, "sort_order", "name");
429 panel->current_sort_field = panel_get_field_by_id (buffer);
430 if (panel->current_sort_field == NULL)
431 panel->current_sort_field = panel_get_field_by_id ("name");
433 g_free (buffer);
435 /* Load the listing mode */
436 buffer = mc_config_get_string (mc_panels_config, section, "list_mode", "full");
437 panel->list_type = list_full;
438 for (i = 0; list_types[i].key; i++)
439 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 =
449 mc_config_get_string (mc_panels_config, section, "user_format", DEFAULT_USER_FORMAT);
451 for (i = 0; i < LIST_TYPES; i++)
453 g_free (panel->user_status_format[i]);
454 g_snprintf (buffer2, BUF_TINY, "user_status%lld", (long long) i);
455 panel->user_status_format[i] =
456 mc_config_get_string (mc_panels_config, section, buffer2, DEFAULT_USER_FORMAT);
459 panel->user_mini_status = 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)
486 mode = panel_types[i].opt_type;
487 break;
489 g_free (buffer);
490 return mode;
493 static const char *
494 setup__is_cfg_group_must_panel_config (const char *grp)
496 if (!strcasecmp ("Dirs", grp) ||
497 !strcasecmp ("Temporal:New Right Panel", grp) ||
498 !strcasecmp ("Temporal:New Left Panel", grp) ||
499 !strcasecmp ("New Left Panel", grp) || !strcasecmp ("New Right Panel", grp))
500 return grp;
501 return NULL;
504 static void
505 setup__move_panels_config_into_separate_file (const char *profile)
507 mc_config_t *tmp_cfg;
508 char **groups, **curr_grp;
509 const char *need_grp;
511 if (!exist_file (profile))
512 return;
514 tmp_cfg = mc_config_init (profile);
515 if (!tmp_cfg)
516 return;
518 curr_grp = groups = mc_config_get_groups (tmp_cfg, NULL);
519 if (!groups)
521 mc_config_deinit (tmp_cfg);
522 return;
524 while (*curr_grp)
526 if (setup__is_cfg_group_must_panel_config (*curr_grp) == NULL)
528 mc_config_del_group (tmp_cfg, *curr_grp);
530 curr_grp++;
533 mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
534 mc_config_deinit (tmp_cfg);
536 tmp_cfg = mc_config_init (profile);
537 if (!tmp_cfg)
539 g_strfreev (groups);
540 return;
543 curr_grp = groups;
545 while (*curr_grp)
547 need_grp = setup__is_cfg_group_must_panel_config (*curr_grp);
548 if (need_grp != NULL)
550 mc_config_del_group (tmp_cfg, need_grp);
552 curr_grp++;
554 g_strfreev (groups);
555 mc_config_save_file (tmp_cfg, NULL);
556 mc_config_deinit (tmp_cfg);
561 Get name of config file.
563 \param subdir
564 if not NULL, then config also search into specified subdir
566 \param config_file_name
567 If specified filename is relative, then will search in standart patches.
569 \return
570 Newly allocated path to config name or NULL if file not found
572 If config_file_name is a relative path, then search config in stantart pathes */
573 static char *
574 load_setup_get_full_config_name (const char *subdir, const char *config_file_name)
577 TODO: IMHO, in future this function must be placed into mc_config module.
578 Also, need to rename stupid mc_home and mc_home_alt to mc_sysconfdir and mc_datadir;
579 home_mc => mc_user_homedir
581 char *lc_basename, *ret;
583 if (config_file_name == NULL)
584 return NULL;
586 if (g_path_is_absolute (config_file_name))
587 return g_strdup (config_file_name);
590 lc_basename = g_path_get_basename (config_file_name);
591 if (lc_basename == NULL)
592 return NULL;
595 if (subdir)
596 ret = g_build_filename (home_dir, MC_USERCONF_DIR, subdir, lc_basename, NULL);
597 else
598 ret = g_build_filename (home_dir, MC_USERCONF_DIR, lc_basename, NULL);
600 if (exist_file (ret))
602 g_free (lc_basename);
603 return ret;
605 g_free (ret);
608 if (subdir)
609 ret = g_build_filename (mc_home, subdir, lc_basename, NULL);
610 else
611 ret = g_build_filename (mc_home, lc_basename, NULL);
613 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))
627 g_free (lc_basename);
628 return ret;
630 g_free (ret);
631 g_free (lc_basename);
632 return NULL;
637 Create new mc_config object from specified ini-file or
638 append data to existing mc_config object from ini-file
641 static void
642 load_setup_init_config_from_file (mc_config_t ** config, const char *fname)
645 TODO: IMHO, in future this function must be placed into mc_config module.
647 if (exist_file (fname))
649 if (*config)
650 mc_config_read_file (*config, fname);
651 else
652 *config = mc_config_init (fname);
658 static mc_config_t *
659 load_setup_get_keymap_profile_config (void)
662 TODO: IMHO, in future this function must be placed into mc_config module.
664 mc_config_t *keymap_config = NULL;
666 char *fname, *fname2;
668 /* 1) /usr/share/mc (mc_home_alt) */
669 fname = g_build_filename (mc_home_alt, GLOBAL_KEYMAP_FILE, NULL);
670 load_setup_init_config_from_file (&keymap_config, fname);
671 g_free (fname);
673 /* 2) /etc/mc (mc_home) */
674 fname = g_build_filename (mc_home, GLOBAL_KEYMAP_FILE, NULL);
675 load_setup_init_config_from_file (&keymap_config, fname);
676 g_free (fname);
678 /* 3) ~/.mc (home_dir?) */
679 fname = g_build_filename (home_dir, MC_USERCONF_DIR, GLOBAL_KEYMAP_FILE, NULL);
680 load_setup_init_config_from_file (&keymap_config, fname);
681 g_free (fname);
683 /* 4) main config; [Midnight Commander] -> keymap */
685 fname2 =
686 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "keymap", GLOBAL_KEYMAP_FILE);
687 fname = load_setup_get_full_config_name (NULL, fname2);
688 if (fname)
690 load_setup_init_config_from_file (&keymap_config, fname);
691 g_free (fname);
693 g_free (fname2);
695 /* 5) getenv("MC_KEYMAP") */
696 fname = load_setup_get_full_config_name (NULL, g_getenv ("MC_KEYMAP"));
697 if (fname)
699 load_setup_init_config_from_file (&keymap_config, fname);
700 g_free (fname);
703 /* 6) --keymap=<keymap> */
704 fname = load_setup_get_full_config_name (NULL, mc_args__keymap_file);
705 if (fname)
707 load_setup_init_config_from_file (&keymap_config, fname);
708 g_free (fname);
711 return keymap_config;
714 char *
715 setup_init (void)
717 char *profile;
718 char *inifile;
720 if (profile_name)
721 return profile_name;
723 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
724 if (!exist_file (profile))
726 inifile = concat_dir_and_file (mc_home, "mc.ini");
727 if (exist_file (inifile))
729 g_free (profile);
730 profile = inifile;
732 else
734 g_free (inifile);
735 inifile = concat_dir_and_file (mc_home_alt, "mc.ini");
736 if (exist_file (inifile))
738 g_free (profile);
739 profile = inifile;
741 else
742 g_free (inifile);
746 profile_name = profile;
748 return profile;
751 void
752 load_setup (void)
754 char *profile;
755 int i;
756 char *buffer;
758 profile = setup_init ();
760 /* mc.lib is common for all users, but has priority lower than
761 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
762 global_profile_name = concat_dir_and_file (mc_home, MC_GLOBAL_CONFIG_FILE);
763 if (!exist_file (global_profile_name))
765 g_free (global_profile_name);
766 global_profile_name = concat_dir_and_file (mc_home_alt, MC_GLOBAL_CONFIG_FILE);
769 panels_profile_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_PANELS_FILE, NULL);
771 mc_main_config = mc_config_init (profile);
773 if (!exist_file (panels_profile_name))
774 setup__move_panels_config_into_separate_file (profile);
776 mc_panels_config = mc_config_init (panels_profile_name);
778 /* Load integer boolean options */
779 for (i = 0; int_options[i].opt_name; i++)
780 *int_options[i].opt_addr =
781 mc_config_get_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
782 *int_options[i].opt_addr);
784 /* Load string options */
785 for (i = 0; str_options[i].opt_name != NULL; i++)
786 *str_options[i].opt_addr =
787 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
788 str_options[i].opt_defval);
790 load_layout ();
792 load_panelize ();
794 startup_left_mode = setup__load_panel_state ("New Left Panel");
795 startup_right_mode = setup__load_panel_state ("New Right Panel");
797 /* At least one of the panels is a listing panel */
798 if (startup_left_mode != view_listing && startup_right_mode != view_listing)
799 startup_left_mode = view_listing;
801 if (!other_dir)
803 buffer = mc_config_get_string (mc_panels_config, "Dirs", "other_dir", ".");
804 if (vfs_file_is_local (buffer))
805 other_dir = buffer;
806 else
807 g_free (buffer);
810 boot_current_is_left = mc_config_get_int (mc_panels_config, "Dirs", "current_is_left", 1);
812 #ifdef USE_NETCODE
813 ftpfs_proxy_host = mc_config_get_string (mc_main_config, "Misc", "ftp_proxy_host", "gate");
814 #endif
815 setup_color_string = mc_config_get_string (mc_main_config, "Misc", "find_ignore_dirs", "");
816 if (setup_color_string[0])
817 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
818 g_free (setup_color_string);
820 /* The default color and the terminal dependent color */
821 setup_color_string = mc_config_get_string (mc_main_config, "Colors", "base_color", "");
822 term_color_string = mc_config_get_string (mc_main_config, "Colors", getenv ("TERM"), "");
823 color_terminal_string = mc_config_get_string (mc_main_config, "Colors", "color_terminals", "");
825 /* Load the directory history */
826 /* directory_history_load (); */
827 /* Remove the temporal entries */
828 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
829 ftpfs_init_passwd ();
830 #endif /* ENABLE_VFS && USE_NETCODE */
832 #ifdef HAVE_CHARSET
833 if (load_codepages_list () > 0)
835 buffer = mc_config_get_string (mc_main_config, "Misc", "display_codepage", "");
836 if (buffer[0] != '\0')
838 display_codepage = get_codepage_index (buffer);
839 cp_display = get_codepage_id (display_codepage);
841 g_free (buffer);
842 buffer = mc_config_get_string (mc_main_config, "Misc", "source_codepage", "");
843 if (buffer[0] != '\0')
845 source_codepage = get_codepage_index (buffer);
846 cp_source = get_codepage_id (source_codepage);
848 g_free (buffer);
850 init_translation_table (source_codepage, display_codepage);
851 if (get_codepage_id (display_codepage))
852 utf8_display = str_isutf8 (get_codepage_id (display_codepage));
853 #endif /* HAVE_CHARSET */
856 #if defined(ENABLE_VFS) && defined (USE_NETCODE)
857 char *
858 load_anon_passwd ()
860 char *buffer;
862 buffer = mc_config_get_string (mc_main_config, "Misc", "ftpfs_password", "");
863 if (buffer[0])
864 return buffer;
866 g_free (buffer);
867 return NULL;
869 #endif /* ENABLE_VFS && USE_NETCODE */
871 void
872 done_setup (void)
874 int i;
876 g_free (profile_name);
877 g_free (global_profile_name);
878 g_free (color_terminal_string);
879 g_free (term_color_string);
880 g_free (setup_color_string);
881 g_free (panels_profile_name);
882 mc_config_deinit (mc_main_config);
883 mc_config_deinit (mc_panels_config);
885 for (i = 0; str_options[i].opt_name != NULL; i++)
886 g_free (*str_options[i].opt_addr);
888 done_hotlist ();
889 done_panelize ();
890 /* directory_history_free (); */
893 static void
894 load_keys_from_section (const char *terminal, mc_config_t * cfg)
896 char *section_name;
897 gchar **profile_keys, **keys;
898 gchar **values, **curr_values;
899 char *valcopy, *value;
900 long key_code;
901 gsize len, values_len;
903 if (!terminal)
904 return;
906 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
907 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
909 while (*profile_keys)
912 /* copy=other causes all keys from [terminal:other] to be loaded. */
913 if (g_strcasecmp (*profile_keys, "copy") == 0)
915 valcopy = mc_config_get_string (cfg, section_name, *profile_keys, "");
916 load_keys_from_section (valcopy, cfg);
917 g_free (valcopy);
918 profile_keys++;
919 continue;
921 curr_values = values =
922 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
924 key_code = lookup_key (*profile_keys, NULL);
925 if (key_code != 0)
927 if (curr_values)
929 while (*curr_values)
931 valcopy = convert_controls (*curr_values);
932 define_sequence (key_code, valcopy, MCKEY_NOACTION);
933 g_free (valcopy);
934 curr_values++;
937 else
939 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
940 valcopy = convert_controls (value);
941 define_sequence (key_code, valcopy, MCKEY_NOACTION);
942 g_free (valcopy);
943 g_free (value);
946 profile_keys++;
947 if (values)
948 g_strfreev (values);
950 g_strfreev (keys);
951 g_free (section_name);
954 void
955 load_key_defs (void)
958 * Load keys from mc.lib before ~/.mc/ini, so that the user
959 * definitions override global settings.
961 mc_config_t *mc_global_config;
963 mc_global_config = mc_config_init (global_profile_name);
964 if (mc_global_config != NULL)
966 load_keys_from_section ("general", mc_global_config);
967 load_keys_from_section (getenv ("TERM"), mc_global_config);
968 mc_config_deinit (mc_global_config);
970 load_keys_from_section ("general", mc_main_config);
971 load_keys_from_section (getenv ("TERM"), mc_main_config);
975 static void
976 load_keymap_from_section (const char *section_name, GArray * keymap, mc_config_t * cfg)
978 gchar **profile_keys, **keys;
979 gchar **values, **curr_values;
980 char *valcopy, *value;
981 int action;
982 gsize len, values_len;
984 if (!section_name)
985 return;
987 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
989 while (*profile_keys)
991 curr_values = values =
992 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
993 action = lookup_action (*profile_keys);
994 if (action > 0)
996 if (curr_values)
998 while (*curr_values)
1000 valcopy = convert_controls (*curr_values);
1001 keybind_cmd_bind (keymap, valcopy, action);
1002 g_free (valcopy);
1003 curr_values++;
1006 else
1008 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
1009 valcopy = convert_controls (value);
1010 /* define_sequence (key_code, valcopy, MCKEY_NOACTION); */
1011 g_free (valcopy);
1012 g_free (value);
1015 profile_keys++;
1016 if (values)
1017 g_strfreev (values);
1019 g_strfreev (keys);
1022 void
1023 load_keymap_defs (void)
1026 * Load keymap from GLOBAL_KEYMAP_FILE before ~/.mc/keymap, so that the user
1027 * definitions override global settings.
1029 mc_config_t *mc_global_keymap;
1031 mc_global_keymap = load_setup_get_keymap_profile_config ();
1033 if (mc_global_keymap != NULL)
1035 #ifdef USE_INTERNAL_EDIT
1036 editor_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1037 load_keymap_from_section ("editor", editor_keymap, mc_global_keymap);
1038 editor_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1039 load_keymap_from_section ("editor:xmap", editor_x_keymap, mc_global_keymap);
1040 #endif
1042 viewer_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1043 load_keymap_from_section ("viewer", viewer_keymap, mc_global_keymap);
1044 viewer_hex_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1045 load_keymap_from_section ("viewer:hex", viewer_hex_keymap, mc_global_keymap);
1047 main_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1048 load_keymap_from_section ("main", main_keymap, mc_global_keymap);
1049 main_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1050 load_keymap_from_section ("main:xmap", main_x_keymap, mc_global_keymap);
1052 panel_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1053 load_keymap_from_section ("panel", panel_keymap, mc_global_keymap);
1055 input_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1056 load_keymap_from_section ("input", input_keymap, mc_global_keymap);
1058 tree_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1059 load_keymap_from_section ("tree", tree_keymap, mc_global_keymap);
1061 help_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1062 load_keymap_from_section ("help", help_keymap, mc_global_keymap);
1064 mc_config_deinit (mc_global_keymap);
1068 void
1069 free_keymap_defs (void)
1071 #ifdef USE_INTERNAL_EDIT
1072 if (editor_keymap != NULL)
1073 g_array_free (editor_keymap, TRUE);
1074 if (editor_x_keymap != NULL)
1075 g_array_free (editor_x_keymap, TRUE);
1076 #endif
1077 if (viewer_keymap != NULL)
1078 g_array_free (viewer_keymap, TRUE);
1079 if (viewer_hex_keymap != NULL)
1080 g_array_free (viewer_hex_keymap, TRUE);
1081 if (main_keymap != NULL)
1082 g_array_free (main_keymap, TRUE);
1083 if (main_x_keymap != NULL)
1084 g_array_free (main_x_keymap, TRUE);
1085 if (panel_keymap != NULL)
1086 g_array_free (panel_keymap, TRUE);
1087 if (input_keymap != NULL)
1088 g_array_free (input_keymap, TRUE);
1089 if (tree_keymap != NULL)
1090 g_array_free (tree_keymap, TRUE);
1091 if (help_keymap != NULL)
1092 g_array_free (help_keymap, TRUE);
1095 void
1096 setup_save_config_show_error (const char *filename, GError ** error)
1098 if (error == NULL || *error == NULL)
1099 return;
1101 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"), filename, (*error)->message);
1103 g_error_free (*error);
1104 *error = NULL;