Added cs to the list of languages
[midnight-commander.git] / src / setup.c
blob7c592a3784d9121fb779ae645d2288f638e14903
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include <config.h>
19 #include <sys/types.h> /* Needed to include local .h files */
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <string.h>
23 #include "tty.h"
24 #include <stdio.h>
25 #include "global.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"
43 #include "x.h"
45 #ifdef HAVE_GNOME
46 # include "gdesktop.h"
47 #endif
49 #ifndef PORT_LIST_MODE_NAME
50 # define PORT_LIST_MODE_NAME "list_mode"
51 #endif
53 #ifndef PORT_LIST_MODE_DEFAULT
54 # define PORT_LIST_MODE_DEFAULT "full"
55 #endif
57 #include "../vfs/vfs.h"
58 #ifdef USE_NETCODE
59 # include "../vfs/ftpfs.h"
60 extern int use_netrc;
61 extern int ftpfs_retry_seconds;
62 extern int ftpfs_use_passive_connections;
63 extern int ftpfs_use_unix_list_options;
64 extern int ftpfs_first_cd_then_ls;
65 #endif
67 #ifdef HAVE_GNOME
68 extern int we_can_afford_the_speed;
69 #endif
71 /* "$Id$" */
73 #ifdef USE_VFS
74 extern int vfs_timeout;
75 extern int tar_gzipped_memlimit;
76 #endif
78 extern char *find_ignore_dirs;
80 extern int num_history_items_recorded;
82 char *profile_name;
84 char setup_color_string [4096];
85 char term_color_string [4096];
86 char color_terminal_string [512];
88 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
89 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
90 #define save_string WritePrivateProfileString
92 int startup_left_mode;
93 int startup_right_mode;
95 /* Ugly hack to allow panel_save_setup to work as a place holder for */
96 /* default panel values */
97 int saving_setup;
99 static struct {
100 char *key;
101 sortfn *sort_type;
102 } sort_names [] = {
103 { "name", (sortfn *) sort_name },
104 { "extension", (sortfn *) sort_ext },
105 { "time", (sortfn *) sort_time },
106 { "atime", (sortfn *) sort_atime },
107 { "ctime", (sortfn *) sort_ctime },
108 { "size", (sortfn *) sort_size },
109 { "inode", (sortfn *) sort_inode },
110 { "unsorted", (sortfn *) unsorted },
111 { 0, 0 }
114 static struct {
115 char *key;
116 int list_type;
117 } list_types [] = {
118 { "full", list_full },
119 { "brief", list_brief },
120 { "long", list_long },
121 { "user", list_user },
122 { "icons", list_icons },
123 { 0, 0 }
126 static struct {
127 char *opt_name;
128 int opt_type;
129 } panel_types [] = {
130 { "listing", view_listing },
131 { "quickview", view_quick },
132 { "info", view_info },
133 { "tree", view_tree },
134 { 0, 0 }
137 static struct {
138 char *opt_name;
139 int *opt_addr;
140 } layout [] = {
141 #ifndef HAVE_GNOME
142 { "equal_split", &equal_split },
143 { "first_panel_size", &first_panel_size },
144 { "message_visible", &message_visible },
145 { "keybar_visible", &keybar_visible },
146 { "xterm_hintbar", &xterm_hintbar },
147 { "output_lines", &output_lines },
148 #endif
149 { "menubar_visible", &menubar_visible },
150 { "command_prompt", &command_prompt },
151 { "show_mini_info", &show_mini_info },
152 { "permission_mode", &permission_mode },
153 { "filetype_mode", &filetype_mode },
154 { 0, 0 }
158 #undef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
160 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
161 extern int preserve_uidgid;
162 #endif
164 static struct {
165 char *opt_name;
166 int *opt_addr;
167 } options [] = {
168 { "show_backups", &show_backups },
169 { "show_dot_files", &show_dot_files },
170 { "verbose", &verbose },
171 { "mark_moves_down", &mark_moves_down },
172 { "pause_after_run", &pause_after_run },
173 { "shell_patterns", &easy_patterns },
174 { "auto_save_setup", &auto_save_setup },
175 { "align_extensions", &align_extensions },
176 { "auto_menu", &auto_menu },
177 { "use_internal_view", &use_internal_view },
178 { "use_internal_edit", &use_internal_edit },
179 { "clear_before_exec", &clear_before_exec },
180 { "mix_all_files", &mix_all_files },
181 { "fast_reload", &fast_reload },
182 { "fast_reload_msg_shown", &fast_reload_w },
183 { "confirm_delete", &confirm_delete },
184 { "confirm_overwrite", &confirm_overwrite },
185 { "confirm_execute", &confirm_execute },
186 { "confirm_exit", &confirm_exit },
187 { "safe_delete", &know_not_what_am_i_doing },
188 { "mouse_repeat_rate", &mou_auto_repeat },
189 { "double_click_speed", &double_click_speed },
190 { "eight_bit_clean", &eight_bit_clean },
191 { "full_eight_bits", &full_eight_bits },
192 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
193 { "confirm_view_dir", &confirm_view_dir },
194 { "mouse_move_pages", &mouse_move_pages },
195 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
196 { "fast_refresh", &fast_refresh },
197 { "navigate_with_arrows", &navigate_with_arrows },
198 { "advanced_chown", &advanced_chfns },
199 { "drop_menus", &drop_menus },
200 { "wrap_mode", &global_wrap_mode},
201 { "old_esc_mode", &old_esc_mode },
202 { "cd_symlinks", &cd_symlinks },
203 { "show_all_if_ambiguous", &show_all_if_ambiguous },
204 { "have_fast_cpu", &have_fast_cpu },
205 { "iconify_on_exec", &iconify_on_exec },
206 { "torben_fj_mode", &torben_fj_mode },
207 { "use_file_to_guess_type", &use_file_to_check_type },
208 { "alternate_plus_minus", &alternate_plus_minus },
209 { "only_leading_plus_minus", &only_leading_plus_minus },
210 { "show_output_starts_shell", &output_starts_shell },
211 { "panel_scroll_pages", &panel_scroll_pages },
212 { "xtree_mode", &xtree_mode },
213 { "num_history_items_recorded", &num_history_items_recorded },
214 { "file_op_compute_totals", &file_op_compute_totals },
215 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
216 { "dive_into_subdirs", &dive_into_subdirs },
217 { "preserve_uidgid", &preserve_uidgid },
218 /* What about the other two options in the copy dialog
219 (follow links, stable symlinks) -Norbert */
220 { "tree_navigation_flag", &tree_navigation_flag },
221 #endif
222 #ifdef USE_VFS
223 { "vfs_timeout", &vfs_timeout },
224 #ifdef USE_NETCODE
225 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
226 { "use_netrc", &use_netrc },
227 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
228 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
229 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
230 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
231 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
232 #endif
233 #endif
234 #ifdef USE_INTERNAL_EDIT
235 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
236 { "editor_key_emulation", &edit_key_emulation },
237 { "editor_tab_spacing", &option_tab_spacing },
238 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
239 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
240 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
241 { "editor_fake_half_tabs", &option_fake_half_tabs },
242 { "editor_option_save_mode", &option_save_mode },
243 { "editor_option_backup_ext_int", &option_backup_ext_int },
244 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
245 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
246 { "editor_edit_confirm_save", &edit_confirm_save },
247 { "editor_syntax_highlighting", &option_syntax_highlighting },
248 #endif
250 #ifdef HAVE_GNOME
251 { "tree_panel_visible", &tree_panel_visible },
252 { "we_can_afford_the_speed", &we_can_afford_the_speed },
253 #else
254 { "nice_rotating_dash", &nice_rotating_dash },
255 { "horizontal_split", &horizontal_split },
256 #endif
257 { 0, 0 }
260 void
261 panel_save_setup (WPanel *panel, char *section)
263 char buffer [BUF_TINY];
264 int i;
266 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
267 save_string (section, "reverse", buffer, profile_name);
268 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
269 save_string (section, "case_sensitive", buffer, profile_name);
270 for (i = 0; sort_names [i].key; i++)
271 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
272 save_string (section, "sort_order",
273 sort_names [i].key, profile_name);
274 break;
277 for (i = 0; list_types [i].key; i++)
278 if (list_types [i].list_type == panel->list_type){
279 save_string (section, PORT_LIST_MODE_NAME, list_types [i].key, profile_name);
280 break;
283 save_string (section, "user_format",
284 panel->user_format, profile_name);
286 for (i = 0; i < LIST_TYPES; i++){
287 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
288 save_string (section, buffer,
289 panel->user_status_format [i], profile_name);
292 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
293 save_string (section, "user_mini_status", buffer,
294 profile_name);
297 void
298 save_layout (void)
300 char *profile;
301 int i;
302 char buffer [BUF_TINY];
304 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
306 /* Save integer options */
307 for (i = 0; layout [i].opt_name; i++){
308 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
309 save_string ("Layout", layout [i].opt_name, buffer, profile);
312 g_free (profile);
315 void
316 save_configure (void)
318 char *profile;
319 int i;
321 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
323 /* Save integer options */
324 for (i = 0; options [i].opt_name; i++)
325 set_int (profile, options [i].opt_name, *options [i].opt_addr);
327 g_free (profile);
330 static void
331 panel_save_type (char *section, int type)
333 int i;
335 for (i = 0; panel_types [i].opt_name; i++)
336 if (panel_types [i].opt_type == type){
337 save_string (section, "display", panel_types [i].opt_name,
338 profile_name);
339 break;
343 #ifndef PORT_HAS_SAVE_PANEL_TYPES
344 void
345 save_panel_types ()
347 int type;
349 type = get_display_type (0);
350 panel_save_type ("New Left Panel", type);
351 if (type == view_listing)
352 panel_save_setup (left_panel, left_panel->panel_name);
353 type = get_display_type (1);
354 panel_save_type ("New Right Panel", type);
355 if (type == view_listing)
356 panel_save_setup (right_panel, right_panel->panel_name);
358 #endif
360 void
361 save_setup (void)
363 char *profile;
364 #ifdef USE_VFS
365 #ifdef USE_NETCODE
366 extern char *ftpfs_anonymous_passwd;
367 extern char *ftpfs_proxy_host;
368 #endif
369 #endif
370 saving_setup = 1;
371 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
373 save_layout ();
374 save_configure ();
375 save_string ("Dirs", "other_dir",
376 get_other_type () == view_listing
377 ? opanel->cwd : ".", profile);
378 WritePrivateProfileString ("Dirs", "current_is_left",
379 get_current_index () == 0 ? "1" : "0", profile);
380 save_hotlist ();
381 save_panelize ();
382 save_panel_types ();
383 /* directory_history_save (); */
385 #ifdef USE_VFS
386 #ifdef USE_NETCODE
387 WritePrivateProfileString ("Misc", "ftpfs_password",
388 ftpfs_anonymous_passwd, profile);
389 if (ftpfs_proxy_host)
390 WritePrivateProfileString ("Misc", "ftp_proxy_host",
391 ftpfs_proxy_host, profile);
392 #endif
393 #endif
394 g_free (profile);
395 saving_setup = 0;
398 void
399 panel_load_setup (WPanel *panel, char *section)
401 int i;
402 char buffer [BUF_TINY];
404 panel->reverse = load_int (section, "reverse", 0);
405 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
407 /* Load sort order */
408 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
409 panel->sort_type = (sortfn *) sort_name;
410 for (i = 0; sort_names [i].key; i++)
411 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
412 panel->sort_type = sort_names [i].sort_type;
413 break;
416 /* Load the listing mode */
417 load_string (section, PORT_LIST_MODE_NAME, PORT_LIST_MODE_DEFAULT, buffer, sizeof (buffer));
418 panel->list_type = list_full;
419 for (i = 0; list_types [i].key; i++)
420 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
421 panel->list_type = list_types [i].list_type;
422 break;
424 #ifndef PORT_HAS_ICON_VIEW
425 if (panel->list_type == list_icons)
426 panel->list_type = list_full;
427 #endif
428 /* User formats */
429 if (panel->user_format){
430 g_free (panel->user_format);
431 panel->user_format = 0;
433 panel->user_format = g_strdup (get_profile_string (section, "user_format",
434 DEFAULT_USER_FORMAT,
435 profile_name));
436 for (i = 0; i < LIST_TYPES; i++){
437 if (panel->user_status_format [i])
438 g_free (panel->user_status_format [i]);
439 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
440 panel->user_status_format [i] =
441 g_strdup (get_profile_string (section, buffer,
442 DEFAULT_USER_FORMAT, profile_name));
445 panel->user_mini_status =
446 load_int (section, "user_mini_status", 0);
450 static void
451 load_layout (char *profile_name)
453 int i;
455 for (i = 0; layout [i].opt_name; i++)
456 *layout [i].opt_addr =
457 load_int ("Layout", layout [i].opt_name,
458 *layout [i].opt_addr);
461 static int
462 load_mode (char *section)
464 char buffer [20];
465 int i;
467 int mode = view_listing;
469 /* Load the display mode */
470 load_string (section, "display", "listing", buffer, sizeof (buffer));
472 for (i = 0; panel_types [i].opt_name; i++)
473 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
474 mode = panel_types [i].opt_type;
475 break;
478 return mode;
481 static char *
482 do_load_string (char *s, char *ss, char *def)
484 char *buffer = g_malloc (128);
485 char *p;
487 load_string (s, ss, def, buffer, 128);
489 p = g_strdup (buffer);
490 g_free (buffer);
491 return p;
494 void
495 load_setup (void)
497 static char *buffer;
498 char *profile;
499 char *inifile;
500 int i;
501 #ifdef USE_NETCODE
502 extern char *ftpfs_proxy_host;
503 #endif
504 buffer = concat_dir_and_file (home_dir, PROFILE_NAME);
505 inifile = concat_dir_and_file (mc_home, "mc.ini");
506 if (exist_file (buffer)){
507 profile = buffer;
508 } else if (exist_file (inifile)){
509 profile = g_strdup (inifile);
510 g_free (buffer);
511 } else {
512 profile = buffer;
514 g_free (inifile);
516 profile_name = profile;
518 /* Load integer boolean options */
519 for (i = 0; options [i].opt_name; i++)
520 *options [i].opt_addr =
521 get_int (profile, options [i].opt_name, *options [i].opt_addr);
523 load_layout (profile);
525 load_panelize ();
527 startup_left_mode = load_mode ("New Left Panel");
528 startup_right_mode = load_mode ("New Right Panel");
530 /* At least one of the panels is a listing panel */
531 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
532 startup_left_mode = view_listing;
534 if (!other_dir){
535 buffer = (char*) g_malloc (MC_MAXPATHLEN);
536 load_string ("Dirs", "other_dir", ".", buffer,
537 MC_MAXPATHLEN);
538 if (vfs_file_is_local (buffer))
539 other_dir = buffer;
540 else
541 g_free (buffer);
543 #ifdef USE_NETCODE
544 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
545 #endif
546 boot_current_is_left =
547 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
549 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
550 sizeof (setup_color_string));
551 if (setup_color_string [0])
552 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", NULL);
554 /* The default color and the terminal dependent color */
555 load_string ("Colors", "base_color", "", setup_color_string,
556 sizeof (setup_color_string));
557 load_string ("Colors", getenv ("TERM"), "",
558 term_color_string, sizeof (term_color_string));
559 load_string ("Colors", "color_terminals", "",
560 color_terminal_string, sizeof (color_terminal_string));
562 /* Load the directory history */
563 /* directory_history_load (); */
564 /* Remove the temporal entries */
565 profile_clean_section ("Temporal:New Left Panel", profile_name);
566 profile_clean_section ("Temporal:New Right Panel", profile_name);
567 #ifdef USE_VFS
568 #ifdef USE_NETCODE
569 ftpfs_init_passwd ();
570 #endif
571 #endif
574 #ifdef USE_VFS
575 #ifdef USE_NETCODE
576 char *
577 load_anon_passwd ()
579 char buffer [255];
581 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
582 if (buffer [0])
583 return g_strdup (buffer);
584 else
585 return 0;
587 #endif
588 #endif
590 void done_setup (void)
592 g_free (profile_name);
593 done_hotlist ();
594 done_panelize ();
595 /* directory_history_free (); */
598 static void
599 load_keys_from_section (char *terminal, char *profile_name)
601 char *section_name;
602 void *profile_keys;
603 char *key, *value, *valcopy;
604 int key_code;
606 if (!terminal)
607 return;
609 section_name = g_strconcat ("terminal:", terminal, NULL);
610 profile_keys = profile_init_iterator (section_name, profile_name);
611 if (!profile_keys){
612 g_free (section_name);
613 return;
616 while (profile_keys){
617 profile_keys = profile_iterator_next (profile_keys, &key, &value);
618 key_code = lookup_key (key);
619 valcopy = convert_controls (value);
620 if (key_code)
621 define_sequence (key_code, valcopy, MCKEY_NOACTION);
622 g_free (valcopy);
624 g_free (section_name);
625 return;
628 void load_key_defs (void)
630 char *libfile = concat_dir_and_file (mc_home, "mc.lib");
631 load_keys_from_section (getenv ("TERM"), profile_name);
632 load_keys_from_section ("general", profile_name);
634 load_keys_from_section (getenv ("TERM"), libfile);
635 load_keys_from_section ("general", libfile);
637 /* We don't want a huge database loaded in core */
638 free_profile_name (libfile);
639 g_free (libfile);