Last fuzzy trimmed...
[midnight-commander.git] / src / setup.c
blob5305873714171ff80b4b3081159f591a8d4cba21
1 /* Setup loading/saving.
2 Copyright (C) 1994 Miguel de Icaza
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <config.h>
19 #include <sys/types.h> /* Needed to include local .h files */
20 #include <sys/stat.h>
21 #include <string.h>
22 #include <stdio.h>
24 #include "global.h"
25 #include "tty.h"
26 #include "dir.h"
27 #include "panel.h"
28 #include "main.h"
29 #include "tree.h"
30 #include "profile.h"
31 #define WANT_WIDGETS
32 #include "setup.h"
33 #include "mouse.h" /* To make view.h happy */
34 #include "view.h" /* For the externs */
35 #include "key.h" /* For the externs */
36 #include "hotlist.h" /* load/save/done hotlist */
37 #include "panelize.h" /* load/save/done panelize */
38 #include "layout.h"
39 #include "menu.h" /* menubar_visible declaration */
40 #include "win.h" /* lookup_key */
41 #include "cmd.h"
42 #include "file.h"
44 #ifdef HAVE_CHARSET
45 #include "charsets.h"
46 #endif
48 #include "../vfs/vfs.h"
49 #ifdef USE_NETCODE
50 # include "../vfs/ftpfs.h"
51 #endif
53 #ifdef USE_INTERNAL_EDIT
54 # include "../edit/edit.h"
55 #endif
58 /* "$Id$" */
60 extern char *find_ignore_dirs;
62 extern int num_history_items_recorded;
64 char *profile_name; /* .mc/ini */
65 char *global_profile_name; /* mc.lib */
67 char setup_color_string [4096];
68 char term_color_string [4096];
69 char color_terminal_string [512];
71 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
72 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
73 #define save_string WritePrivateProfileString
75 int startup_left_mode;
76 int startup_right_mode;
78 /* Ugly hack to allow panel_save_setup to work as a place holder for */
79 /* default panel values */
80 int saving_setup;
82 static const struct {
83 char *key;
84 sortfn *sort_type;
85 } sort_names [] = {
86 { "name", (sortfn *) sort_name },
87 { "extension", (sortfn *) sort_ext },
88 { "time", (sortfn *) sort_time },
89 { "atime", (sortfn *) sort_atime },
90 { "ctime", (sortfn *) sort_ctime },
91 { "size", (sortfn *) sort_size },
92 { "inode", (sortfn *) sort_inode },
93 { "unsorted", (sortfn *) unsorted },
94 { 0, 0 }
97 static const struct {
98 char *key;
99 int list_type;
100 } list_types [] = {
101 { "full", list_full },
102 { "brief", list_brief },
103 { "long", list_long },
104 { "user", list_user },
105 { "icons", list_icons },
106 { 0, 0 }
109 static const struct {
110 char *opt_name;
111 int opt_type;
112 } panel_types [] = {
113 { "listing", view_listing },
114 { "quickview", view_quick },
115 { "info", view_info },
116 { "tree", view_tree },
117 { 0, 0 }
120 static const struct {
121 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_hintbar", &xterm_hintbar },
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 { 0, 0 }
139 #undef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
141 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
142 extern int preserve_uidgid;
143 #endif
145 static const struct {
146 char *opt_name;
147 int *opt_addr;
148 } options [] = {
149 { "show_backups", &show_backups },
150 { "show_dot_files", &show_dot_files },
151 { "verbose", &verbose },
152 { "mark_moves_down", &mark_moves_down },
153 { "pause_after_run", &pause_after_run },
154 { "shell_patterns", &easy_patterns },
155 { "auto_save_setup", &auto_save_setup },
156 { "auto_menu", &auto_menu },
157 { "use_internal_view", &use_internal_view },
158 { "use_internal_edit", &use_internal_edit },
159 { "clear_before_exec", &clear_before_exec },
160 { "mix_all_files", &mix_all_files },
161 { "fast_reload", &fast_reload },
162 { "fast_reload_msg_shown", &fast_reload_w },
163 { "confirm_delete", &confirm_delete },
164 { "confirm_overwrite", &confirm_overwrite },
165 { "confirm_execute", &confirm_execute },
166 { "confirm_exit", &confirm_exit },
167 { "safe_delete", &know_not_what_am_i_doing },
168 { "mouse_repeat_rate", &mou_auto_repeat },
169 { "double_click_speed", &double_click_speed },
170 #ifndef HAVE_CHARSET
171 { "eight_bit_clean", &eight_bit_clean },
172 { "full_eight_bits", &full_eight_bits },
173 #endif /* !HAVE_CHARSET */
174 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
175 { "confirm_view_dir", &confirm_view_dir },
176 { "mouse_move_pages", &mouse_move_pages },
177 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
178 { "fast_refresh", &fast_refresh },
179 { "navigate_with_arrows", &navigate_with_arrows },
180 { "advanced_chown", &advanced_chfns },
181 { "drop_menus", &drop_menus },
182 { "wrap_mode", &global_wrap_mode},
183 { "old_esc_mode", &old_esc_mode },
184 { "cd_symlinks", &cd_symlinks },
185 { "show_all_if_ambiguous", &show_all_if_ambiguous },
186 { "have_fast_cpu", &have_fast_cpu },
187 { "torben_fj_mode", &torben_fj_mode },
188 { "use_file_to_guess_type", &use_file_to_check_type },
189 { "alternate_plus_minus", &alternate_plus_minus },
190 { "only_leading_plus_minus", &only_leading_plus_minus },
191 { "show_output_starts_shell", &output_starts_shell },
192 { "panel_scroll_pages", &panel_scroll_pages },
193 { "xtree_mode", &xtree_mode },
194 { "num_history_items_recorded", &num_history_items_recorded },
195 { "file_op_compute_totals", &file_op_compute_totals },
196 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
197 { "dive_into_subdirs", &dive_into_subdirs },
198 { "preserve_uidgid", &preserve_uidgid },
199 /* What about the other two options in the copy dialog
200 (follow links, stable symlinks) -Norbert */
201 { "tree_navigation_flag", &tree_navigation_flag },
202 #endif /* SAVE_CHANGES_OUTSIDE_OPTIONS_MENU */
203 #ifdef USE_VFS
204 { "vfs_timeout", &vfs_timeout },
205 #ifdef USE_NETCODE
206 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
207 { "use_netrc", &use_netrc },
208 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
209 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
210 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
211 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
212 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
213 #endif /* USE_NETCODE */
214 #endif /* USE_VFS */
215 #ifdef USE_INTERNAL_EDIT
216 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
217 { "editor_key_emulation", &edit_key_emulation },
218 { "editor_tab_spacing", &option_tab_spacing },
219 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
220 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
221 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
222 { "editor_fake_half_tabs", &option_fake_half_tabs },
223 { "editor_option_save_mode", &option_save_mode },
224 { "editor_option_backup_ext_int", &option_backup_ext_int },
225 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
226 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
227 { "editor_edit_confirm_save", &edit_confirm_save },
228 { "editor_syntax_highlighting", &option_syntax_highlighting },
229 #endif /* USE_INTERNAL_EDIT */
231 { "nice_rotating_dash", &nice_rotating_dash },
232 { "horizontal_split", &horizontal_split },
233 { 0, 0 }
236 void
237 panel_save_setup (WPanel *panel, char *section)
239 char buffer [BUF_TINY];
240 int i;
242 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
243 save_string (section, "reverse", buffer, profile_name);
244 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
245 save_string (section, "case_sensitive", buffer, profile_name);
246 for (i = 0; sort_names [i].key; i++)
247 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
248 save_string (section, "sort_order",
249 sort_names [i].key, profile_name);
250 break;
253 for (i = 0; list_types [i].key; i++)
254 if (list_types [i].list_type == panel->list_type){
255 save_string (section, "list_mode", list_types [i].key, profile_name);
256 break;
259 save_string (section, "user_format",
260 panel->user_format, profile_name);
262 for (i = 0; i < LIST_TYPES; i++){
263 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
264 save_string (section, buffer,
265 panel->user_status_format [i], profile_name);
268 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
269 save_string (section, "user_mini_status", buffer,
270 profile_name);
273 void
274 save_layout (void)
276 char *profile;
277 int i;
278 char buffer [BUF_TINY];
280 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
282 /* Save integer options */
283 for (i = 0; layout [i].opt_name; i++){
284 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
285 save_string ("Layout", layout [i].opt_name, buffer, profile);
288 g_free (profile);
291 void
292 save_configure (void)
294 char *profile;
295 int i;
297 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
299 /* Save integer options */
300 for (i = 0; options [i].opt_name; i++)
301 set_int (profile, options [i].opt_name, *options [i].opt_addr);
303 g_free (profile);
306 static void
307 panel_save_type (char *section, int type)
309 int i;
311 for (i = 0; panel_types [i].opt_name; i++)
312 if (panel_types [i].opt_type == type){
313 save_string (section, "display", panel_types [i].opt_name,
314 profile_name);
315 break;
319 void
320 save_panel_types ()
322 int type;
324 type = get_display_type (0);
325 panel_save_type ("New Left Panel", type);
326 if (type == view_listing)
327 panel_save_setup (left_panel, left_panel->panel_name);
328 type = get_display_type (1);
329 panel_save_type ("New Right Panel", type);
330 if (type == view_listing)
331 panel_save_setup (right_panel, right_panel->panel_name);
334 void
335 save_setup (void)
337 char *profile;
339 saving_setup = 1;
340 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
342 save_configure ();
344 save_layout ();
345 save_string ("Dirs", "other_dir",
346 get_other_type () == view_listing
347 ? opanel->cwd : ".", profile);
348 if (get_current_panel () != NULL)
349 WritePrivateProfileString ("Dirs", "current_is_left",
350 get_current_index () == 0 ? "1" : "0", profile);
351 save_hotlist ();
353 save_panelize ();
354 save_panel_types ();
355 /* directory_history_save (); */
357 #if defined(USE_VFS) && defined (USE_NETCODE)
358 WritePrivateProfileString ("Misc", "ftpfs_password",
359 ftpfs_anonymous_passwd, profile);
360 if (ftpfs_proxy_host)
361 WritePrivateProfileString ("Misc", "ftp_proxy_host",
362 ftpfs_proxy_host, profile);
363 #endif /* USE_VFS && USE_NETCODE */
365 #ifdef HAVE_CHARSET
366 save_string( "Misc", "display_codepage",
367 get_codepage_id( display_codepage ), profile_name );
368 #endif /* HAVE_CHARSET */
370 g_free (profile);
371 saving_setup = 0;
374 void
375 panel_load_setup (WPanel *panel, char *section)
377 int i;
378 char buffer [BUF_TINY];
380 panel->reverse = load_int (section, "reverse", 0);
381 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
383 /* Load sort order */
384 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
385 panel->sort_type = (sortfn *) sort_name;
386 for (i = 0; sort_names [i].key; i++)
387 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
388 panel->sort_type = sort_names [i].sort_type;
389 break;
392 /* Load the listing mode */
393 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
394 panel->list_type = list_full;
395 for (i = 0; list_types [i].key; i++)
396 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
397 panel->list_type = list_types [i].list_type;
398 break;
401 if (panel->list_type == list_icons)
402 panel->list_type = list_full;
404 /* User formats */
405 if (panel->user_format){
406 g_free (panel->user_format);
407 panel->user_format = 0;
409 panel->user_format = g_strdup (get_profile_string (section, "user_format",
410 DEFAULT_USER_FORMAT,
411 profile_name));
412 for (i = 0; i < LIST_TYPES; i++){
413 if (panel->user_status_format [i])
414 g_free (panel->user_status_format [i]);
415 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
416 panel->user_status_format [i] =
417 g_strdup (get_profile_string (section, buffer,
418 DEFAULT_USER_FORMAT, profile_name));
421 panel->user_mini_status =
422 load_int (section, "user_mini_status", 0);
426 static void
427 load_layout (char *profile_name)
429 int i;
431 for (i = 0; layout [i].opt_name; i++)
432 *layout [i].opt_addr =
433 load_int ("Layout", layout [i].opt_name,
434 *layout [i].opt_addr);
437 static int
438 load_mode (char *section)
440 char buffer [20];
441 int i;
443 int mode = view_listing;
445 /* Load the display mode */
446 load_string (section, "display", "listing", buffer, sizeof (buffer));
448 for (i = 0; panel_types [i].opt_name; i++)
449 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
450 mode = panel_types [i].opt_type;
451 break;
454 return mode;
457 #ifdef USE_NETCODE
458 static char *
459 do_load_string (char *s, char *ss, char *def)
461 char *buffer = g_malloc (BUF_SMALL);
462 char *p;
464 load_string (s, ss, def, buffer, BUF_SMALL);
466 p = g_strdup (buffer);
467 g_free (buffer);
468 return p;
470 #endif /* !USE_NETCODE */
472 char *
473 setup_init (void)
475 char *profile;
476 char *inifile;
478 if (profile_name)
479 return profile_name;
481 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
482 if (!exist_file (profile)){
483 inifile = concat_dir_and_file (mc_home, "mc.ini");
484 if (exist_file (inifile)){
485 g_free (profile);
486 profile = inifile;
487 } else
488 g_free (inifile);
491 profile_name = profile;
493 return profile;
496 void
497 load_setup (void)
499 char *profile;
500 int i;
502 profile = setup_init ();
504 /* mc.lib is common for all users, but has priority lower than
505 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
506 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
508 /* Load integer boolean options */
509 for (i = 0; options [i].opt_name; i++)
510 *options [i].opt_addr =
511 get_int (profile, options [i].opt_name, *options [i].opt_addr);
513 load_layout (profile);
515 load_panelize ();
517 startup_left_mode = load_mode ("New Left Panel");
518 startup_right_mode = load_mode ("New Right Panel");
520 /* At least one of the panels is a listing panel */
521 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
522 startup_left_mode = view_listing;
524 if (!other_dir){
525 char *buffer;
527 buffer = (char*) g_malloc (MC_MAXPATHLEN);
528 load_string ("Dirs", "other_dir", ".", buffer,
529 MC_MAXPATHLEN);
530 if (vfs_file_is_local (buffer))
531 other_dir = buffer;
532 else
533 g_free (buffer);
536 boot_current_is_left =
537 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
539 #ifdef USE_NETCODE
540 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
541 #endif
543 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
544 sizeof (setup_color_string));
545 if (setup_color_string [0])
546 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", NULL);
548 /* The default color and the terminal dependent color */
549 load_string ("Colors", "base_color", "", setup_color_string,
550 sizeof (setup_color_string));
551 load_string ("Colors", getenv ("TERM"), "",
552 term_color_string, sizeof (term_color_string));
553 load_string ("Colors", "color_terminals", "",
554 color_terminal_string, sizeof (color_terminal_string));
556 /* Load the directory history */
557 /* directory_history_load (); */
558 /* Remove the temporal entries */
559 profile_clean_section ("Temporal:New Left Panel", profile_name);
560 profile_clean_section ("Temporal:New Right Panel", profile_name);
561 #if defined(USE_VFS) && defined (USE_NETCODE)
562 ftpfs_init_passwd ();
563 #endif /* USE_VFS && USE_NETCODE */
565 #ifdef HAVE_CHARSET
566 if ( load_codepages_list() > 0 ) {
567 char cpname[128];
568 load_string( "Misc", "display_codepage", "",
569 cpname, sizeof(cpname) );
570 if ( cpname[0] != '\0' )
571 display_codepage = get_codepage_index( cpname );
574 init_translation_table( source_codepage, display_codepage );
575 #endif /* HAVE_CHARSET */
578 #if defined(USE_VFS) && defined (USE_NETCODE)
579 char *
580 load_anon_passwd ()
582 char buffer [255];
584 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
585 if (buffer [0])
586 return g_strdup (buffer);
587 else
588 return 0;
590 #endif /* USE_VFS && USE_NETCODE */
592 void done_setup (void)
594 g_free (profile_name);
595 g_free (global_profile_name);
596 done_hotlist ();
597 done_panelize ();
598 /* directory_history_free (); */
601 static void
602 load_keys_from_section (char *terminal, char *profile_name)
604 char *section_name;
605 void *profile_keys;
606 char *key, *value, *valcopy;
607 int key_code;
609 if (!terminal)
610 return;
612 section_name = g_strconcat ("terminal:", terminal, NULL);
613 profile_keys = profile_init_iterator (section_name, profile_name);
614 if (!profile_keys){
615 g_free (section_name);
616 return;
619 while (profile_keys){
620 profile_keys = profile_iterator_next (profile_keys, &key, &value);
621 key_code = lookup_key (key);
622 if (key_code){
623 valcopy = convert_controls (value);
624 define_sequence (key_code, valcopy, MCKEY_NOACTION);
625 g_free (valcopy);
628 g_free (section_name);
629 return;
632 void load_key_defs (void)
635 * Load keys from mc.lib before ~/.mc/ini, so that the user
636 * definitions override global settings.
638 load_keys_from_section ("general", global_profile_name);
639 load_keys_from_section (getenv ("TERM"), global_profile_name);
640 load_keys_from_section ("general", profile_name);
641 load_keys_from_section (getenv ("TERM"), profile_name);
643 /* We don't want a huge database loaded in core */
644 free_profile_name (global_profile_name);