* POTFILES.in: add gnome/gcustom-layout.c file
[midnight-commander.git] / src / setup.c
blobfa8e65e69ec90647b5dfb8ee48fee8e4e37ba9c5
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 tree_panel_visible;
69 extern int we_can_afford_the_speed;
70 #endif
72 /* "$Id$" */
74 #ifdef USE_VFS
75 extern int vfs_timeout;
76 extern int tar_gzipped_memlimit;
77 #endif
79 extern char *find_ignore_dirs;
81 extern int num_history_items_recorded;
83 char *profile_name;
85 char setup_color_string [4096];
86 char term_color_string [4096];
87 char color_terminal_string [512];
89 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
90 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
91 #define save_string WritePrivateProfileString
93 int startup_left_mode;
94 int startup_right_mode;
96 /* Ugly hack to allow panel_save_setup to work as a place holder for */
97 /* default panel values */
98 int saving_setup;
100 static struct {
101 char *key;
102 sortfn *sort_type;
103 } sort_names [] = {
104 { "name", (sortfn *) sort_name },
105 { "extension", (sortfn *) sort_ext },
106 { "time", (sortfn *) sort_time },
107 { "atime", (sortfn *) sort_atime },
108 { "ctime", (sortfn *) sort_ctime },
109 { "size", (sortfn *) sort_size },
110 { "inode", (sortfn *) sort_inode },
111 { "unsorted", (sortfn *) unsorted },
112 { 0, 0 }
115 static struct {
116 char *key;
117 int list_type;
118 } list_types [] = {
119 { "full", list_full },
120 { "brief", list_brief },
121 { "long", list_long },
122 { "user", list_user },
123 { "icons", list_icons },
124 { 0, 0 }
127 static struct {
128 char *opt_name;
129 int opt_type;
130 } panel_types [] = {
131 { "listing", view_listing },
132 { "quickview", view_quick },
133 { "info", view_info },
134 { "tree", view_tree },
135 { 0, 0 }
138 static struct {
139 char *opt_name;
140 int *opt_addr;
141 } layout [] = {
142 #ifndef HAVE_GNOME
143 { "equal_split", &equal_split },
144 { "first_panel_size", &first_panel_size },
145 { "message_visible", &message_visible },
146 { "keybar_visible", &keybar_visible },
147 { "xterm_hintbar", &xterm_hintbar },
148 { "output_lines", &output_lines },
149 #endif
150 { "menubar_visible", &menubar_visible },
151 { "command_prompt", &command_prompt },
152 { "show_mini_info", &show_mini_info },
153 { "permission_mode", &permission_mode },
154 { "filetype_mode", &filetype_mode },
155 { 0, 0 }
159 #undef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
161 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
162 extern int preserve_uidgid;
163 #endif
165 static struct {
166 char *opt_name;
167 int *opt_addr;
168 } options [] = {
169 { "show_backups", &show_backups },
170 #ifdef HAVE_GNOME
171 { "gnome_show_dot_files", &show_dot_files },
172 #else
173 { "show_dot_files", &show_dot_files },
174 #endif
175 { "verbose", &verbose },
176 { "mark_moves_down", &mark_moves_down },
177 { "pause_after_run", &pause_after_run },
178 { "shell_patterns", &easy_patterns },
179 { "auto_save_setup", &auto_save_setup },
180 { "align_extensions", &align_extensions },
181 { "auto_menu", &auto_menu },
182 { "use_internal_view", &use_internal_view },
183 { "use_internal_edit", &use_internal_edit },
184 { "clear_before_exec", &clear_before_exec },
185 { "mix_all_files", &mix_all_files },
186 { "fast_reload", &fast_reload },
187 { "fast_reload_msg_shown", &fast_reload_w },
188 { "confirm_delete", &confirm_delete },
189 { "confirm_overwrite", &confirm_overwrite },
190 { "confirm_execute", &confirm_execute },
191 { "confirm_exit", &confirm_exit },
192 { "safe_delete", &know_not_what_am_i_doing },
193 { "mouse_repeat_rate", &mou_auto_repeat },
194 { "double_click_speed", &double_click_speed },
195 { "eight_bit_clean", &eight_bit_clean },
196 { "full_eight_bits", &full_eight_bits },
197 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
198 { "confirm_view_dir", &confirm_view_dir },
199 { "mouse_move_pages", &mouse_move_pages },
200 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
201 { "fast_refresh", &fast_refresh },
202 { "navigate_with_arrows", &navigate_with_arrows },
203 { "advanced_chown", &advanced_chfns },
204 { "drop_menus", &drop_menus },
205 { "wrap_mode", &global_wrap_mode},
206 { "old_esc_mode", &old_esc_mode },
207 { "cd_symlinks", &cd_symlinks },
208 { "show_all_if_ambiguous", &show_all_if_ambiguous },
209 { "have_fast_cpu", &have_fast_cpu },
210 { "iconify_on_exec", &iconify_on_exec },
211 { "torben_fj_mode", &torben_fj_mode },
212 { "use_file_to_guess_type", &use_file_to_check_type },
213 { "alternate_plus_minus", &alternate_plus_minus },
214 { "only_leading_plus_minus", &only_leading_plus_minus },
215 { "show_output_starts_shell", &output_starts_shell },
216 { "panel_scroll_pages", &panel_scroll_pages },
217 { "xtree_mode", &xtree_mode },
218 { "num_history_items_recorded", &num_history_items_recorded },
219 { "file_op_compute_totals", &file_op_compute_totals },
220 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
221 { "dive_into_subdirs", &dive_into_subdirs },
222 { "preserve_uidgid", &preserve_uidgid },
223 /* What about the other two options in the copy dialog
224 (follow links, stable symlinks) -Norbert */
225 { "tree_navigation_flag", &tree_navigation_flag },
226 #endif
227 #ifdef USE_VFS
228 { "vfs_timeout", &vfs_timeout },
229 #ifdef USE_NETCODE
230 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
231 { "use_netrc", &use_netrc },
232 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
233 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
234 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
235 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
236 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
237 #endif
238 #endif
239 #ifdef USE_INTERNAL_EDIT
240 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
241 { "editor_key_emulation", &edit_key_emulation },
242 { "editor_tab_spacing", &option_tab_spacing },
243 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
244 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
245 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
246 { "editor_fake_half_tabs", &option_fake_half_tabs },
247 { "editor_option_save_mode", &option_save_mode },
248 { "editor_option_backup_ext_int", &option_backup_ext_int },
249 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
250 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
251 { "editor_edit_confirm_save", &edit_confirm_save },
252 { "editor_syntax_highlighting", &option_syntax_highlighting },
253 #endif
255 #ifdef HAVE_GNOME
256 { "tree_panel_visible", &tree_panel_visible },
257 { "we_can_afford_the_speed", &we_can_afford_the_speed },
258 #else
259 { "nice_rotating_dash", &nice_rotating_dash },
260 { "horizontal_split", &horizontal_split },
261 #endif
262 { 0, 0 }
265 void
266 panel_save_setup (WPanel *panel, char *section)
268 char buffer [BUF_TINY];
269 int i;
271 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
272 save_string (section, "reverse", buffer, profile_name);
273 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
274 save_string (section, "case_sensitive", buffer, profile_name);
275 for (i = 0; sort_names [i].key; i++)
276 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
277 save_string (section, "sort_order",
278 sort_names [i].key, profile_name);
279 break;
282 for (i = 0; list_types [i].key; i++)
283 if (list_types [i].list_type == panel->list_type){
284 save_string (section, PORT_LIST_MODE_NAME, list_types [i].key, profile_name);
285 break;
288 save_string (section, "user_format",
289 panel->user_format, profile_name);
291 for (i = 0; i < LIST_TYPES; i++){
292 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
293 save_string (section, buffer,
294 panel->user_status_format [i], profile_name);
297 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
298 save_string (section, "user_mini_status", buffer,
299 profile_name);
302 void
303 save_layout (void)
305 char *profile;
306 int i;
307 char buffer [BUF_TINY];
309 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
311 /* Save integer options */
312 for (i = 0; layout [i].opt_name; i++){
313 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
314 save_string ("Layout", layout [i].opt_name, buffer, profile);
317 g_free (profile);
320 void
321 save_configure (void)
323 char *profile;
324 int i;
326 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
328 /* Save integer options */
329 for (i = 0; options [i].opt_name; i++)
330 set_int (profile, options [i].opt_name, *options [i].opt_addr);
332 g_free (profile);
335 static void
336 panel_save_type (char *section, int type)
338 int i;
340 for (i = 0; panel_types [i].opt_name; i++)
341 if (panel_types [i].opt_type == type){
342 save_string (section, "display", panel_types [i].opt_name,
343 profile_name);
344 break;
348 #ifndef PORT_HAS_SAVE_PANEL_TYPES
349 void
350 save_panel_types ()
352 int type;
354 type = get_display_type (0);
355 panel_save_type ("New Left Panel", type);
356 if (type == view_listing)
357 panel_save_setup (left_panel, left_panel->panel_name);
358 type = get_display_type (1);
359 panel_save_type ("New Right Panel", type);
360 if (type == view_listing)
361 panel_save_setup (right_panel, right_panel->panel_name);
363 #endif
365 void
366 save_setup (void)
368 char *profile;
369 #ifdef USE_VFS
370 #ifdef USE_NETCODE
371 extern char *ftpfs_anonymous_passwd;
372 extern char *ftpfs_proxy_host;
373 #endif
374 #endif
375 saving_setup = 1;
376 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
378 save_layout ();
379 save_configure ();
380 save_string ("Dirs", "other_dir",
381 get_other_type () == view_listing
382 ? opanel->cwd : ".", profile);
383 WritePrivateProfileString ("Dirs", "current_is_left",
384 get_current_index () == 0 ? "1" : "0", profile);
385 save_hotlist ();
386 save_panelize ();
387 save_panel_types ();
388 /* directory_history_save (); */
390 #ifdef USE_VFS
391 #ifdef USE_NETCODE
392 WritePrivateProfileString ("Misc", "ftpfs_password",
393 ftpfs_anonymous_passwd, profile);
394 if (ftpfs_proxy_host)
395 WritePrivateProfileString ("Misc", "ftp_proxy_host",
396 ftpfs_proxy_host, profile);
397 #endif
398 #endif
399 g_free (profile);
400 saving_setup = 0;
403 void
404 panel_load_setup (WPanel *panel, char *section)
406 int i;
407 char buffer [BUF_TINY];
409 panel->reverse = load_int (section, "reverse", 0);
410 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
412 /* Load sort order */
413 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
414 panel->sort_type = (sortfn *) sort_name;
415 for (i = 0; sort_names [i].key; i++)
416 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
417 panel->sort_type = sort_names [i].sort_type;
418 break;
421 /* Load the listing mode */
422 load_string (section, PORT_LIST_MODE_NAME, PORT_LIST_MODE_DEFAULT, buffer, sizeof (buffer));
423 panel->list_type = list_full;
424 for (i = 0; list_types [i].key; i++)
425 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
426 panel->list_type = list_types [i].list_type;
427 break;
429 #ifndef PORT_HAS_ICON_VIEW
430 if (panel->list_type == list_icons)
431 panel->list_type = list_full;
432 #endif
433 /* User formats */
434 if (panel->user_format){
435 g_free (panel->user_format);
436 panel->user_format = 0;
438 panel->user_format = g_strdup (get_profile_string (section, "user_format",
439 DEFAULT_USER_FORMAT,
440 profile_name));
441 for (i = 0; i < LIST_TYPES; i++){
442 if (panel->user_status_format [i])
443 g_free (panel->user_status_format [i]);
444 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
445 panel->user_status_format [i] =
446 g_strdup (get_profile_string (section, buffer,
447 DEFAULT_USER_FORMAT, profile_name));
450 panel->user_mini_status =
451 load_int (section, "user_mini_status", 0);
455 static void
456 load_layout (char *profile_name)
458 int i;
460 for (i = 0; layout [i].opt_name; i++)
461 *layout [i].opt_addr =
462 load_int ("Layout", layout [i].opt_name,
463 *layout [i].opt_addr);
466 static int
467 load_mode (char *section)
469 char buffer [20];
470 int i;
472 int mode = view_listing;
474 /* Load the display mode */
475 load_string (section, "display", "listing", buffer, sizeof (buffer));
477 for (i = 0; panel_types [i].opt_name; i++)
478 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
479 mode = panel_types [i].opt_type;
480 break;
483 return mode;
486 static char *
487 do_load_string (char *s, char *ss, char *def)
489 char *buffer = g_malloc (128);
490 char *p;
492 load_string (s, ss, def, buffer, 128);
494 p = g_strdup (buffer);
495 g_free (buffer);
496 return p;
499 char *
500 setup_init (void)
502 static char *buffer;
503 char *profile;
504 char *inifile;
506 if (profile_name)
507 return profile_name;
509 buffer = concat_dir_and_file (home_dir, PROFILE_NAME);
510 inifile = concat_dir_and_file (mc_home, "mc.ini");
511 if (exist_file (buffer)){
512 profile = buffer;
513 } else if (exist_file (inifile)){
514 profile = g_strdup (inifile);
515 g_free (buffer);
516 } else {
517 profile = buffer;
519 g_free (inifile);
521 profile_name = profile;
523 return profile;
526 void
527 load_setup (void)
529 char *profile;
531 int i;
532 #ifdef USE_NETCODE
533 extern char *ftpfs_proxy_host;
534 #endif
536 profile = setup_init ();
537 /* Load integer boolean options */
538 for (i = 0; options [i].opt_name; i++)
539 *options [i].opt_addr =
540 get_int (profile, options [i].opt_name, *options [i].opt_addr);
542 load_layout (profile);
544 load_panelize ();
546 startup_left_mode = load_mode ("New Left Panel");
547 startup_right_mode = load_mode ("New Right Panel");
549 /* At least one of the panels is a listing panel */
550 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
551 startup_left_mode = view_listing;
553 if (!other_dir){
554 char *buffer;
556 buffer = (char*) g_malloc (MC_MAXPATHLEN);
557 load_string ("Dirs", "other_dir", ".", buffer,
558 MC_MAXPATHLEN);
559 if (vfs_file_is_local (buffer))
560 other_dir = buffer;
561 else
562 g_free (buffer);
564 #ifdef USE_NETCODE
565 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
566 #endif
567 boot_current_is_left =
568 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
570 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
571 sizeof (setup_color_string));
572 if (setup_color_string [0])
573 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", NULL);
575 /* The default color and the terminal dependent color */
576 load_string ("Colors", "base_color", "", setup_color_string,
577 sizeof (setup_color_string));
578 load_string ("Colors", getenv ("TERM"), "",
579 term_color_string, sizeof (term_color_string));
580 load_string ("Colors", "color_terminals", "",
581 color_terminal_string, sizeof (color_terminal_string));
583 /* Load the directory history */
584 /* directory_history_load (); */
585 /* Remove the temporal entries */
586 profile_clean_section ("Temporal:New Left Panel", profile_name);
587 profile_clean_section ("Temporal:New Right Panel", profile_name);
588 #ifdef USE_VFS
589 #ifdef USE_NETCODE
590 ftpfs_init_passwd ();
591 #endif
592 #endif
595 #ifdef USE_VFS
596 #ifdef USE_NETCODE
597 char *
598 load_anon_passwd ()
600 char buffer [255];
602 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
603 if (buffer [0])
604 return g_strdup (buffer);
605 else
606 return 0;
608 #endif
609 #endif
611 void done_setup (void)
613 g_free (profile_name);
614 done_hotlist ();
615 done_panelize ();
616 /* directory_history_free (); */
619 static void
620 load_keys_from_section (char *terminal, char *profile_name)
622 char *section_name;
623 void *profile_keys;
624 char *key, *value, *valcopy;
625 int key_code;
627 if (!terminal)
628 return;
630 section_name = g_strconcat ("terminal:", terminal, NULL);
631 profile_keys = profile_init_iterator (section_name, profile_name);
632 if (!profile_keys){
633 g_free (section_name);
634 return;
637 while (profile_keys){
638 profile_keys = profile_iterator_next (profile_keys, &key, &value);
639 key_code = lookup_key (key);
640 valcopy = convert_controls (value);
641 if (key_code)
642 define_sequence (key_code, valcopy, MCKEY_NOACTION);
643 g_free (valcopy);
645 g_free (section_name);
646 return;
649 void load_key_defs (void)
651 char *libfile = concat_dir_and_file (mc_home, "mc.lib");
652 load_keys_from_section (getenv ("TERM"), profile_name);
653 load_keys_from_section ("general", profile_name);
655 load_keys_from_section (getenv ("TERM"), libfile);
656 load_keys_from_section ("general", libfile);
658 /* We don't want a huge database loaded in core */
659 free_profile_name (libfile);
660 g_free (libfile);