- Start rosapps rearrange and cleanup process.
[reactos.git] / rosapps / applications / mc / src / setup.c
1 /* Setup loading/saving.
2 Copyright (C) 1994 Miguel de Icaza
3
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.
8
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.
13
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. */
17
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 <stdlib.h>
25 #include <stdio.h>
26 #include "mad.h"
27 #include "dir.h"
28 #include "file.h"
29 #include "global.h"
30 #include "util.h" /* Functions and externs */
31 #include "panel.h"
32 #include "main.h"
33 #include "tree.h"
34 #include "profile.h"
35 #define WANT_DEFAULTS
36 #include "setup.h"
37 #include "mouse.h" /* To make view.h happy */
38 #include "view.h" /* For the externs */
39 #include "key.h" /* For the externs */
40 #include "hotlist.h" /* load/save/done hotlist */
41 #include "panelize.h" /* load/save/done panelize */
42 #include "layout.h"
43 #include "menu.h" /* menubar_visible declaration */
44 #include "win.h" /* lookup_key */
45 #include "cmd.h"
46 #include "x.h"
47
48 #ifdef HAVE_GNOME
49 # include "gdesktop.h"
50 #endif
51
52 #ifndef PORT_LIST_MODE_NAME
53 # define PORT_LIST_MODE_NAME "list_mode"
54 #endif
55
56 #ifndef PORT_LIST_MODE_DEFAULT
57 # define PORT_LIST_MODE_DEFAULT "full"
58 #endif
59
60 #include "../vfs/vfs.h"
61 #ifdef USE_NETCODE
62 # include "../vfs/ftpfs.h"
63 extern int use_netrc;
64 extern int ftpfs_retry_seconds;
65 extern int ftpfs_use_passive_connections;
66 extern int ftpfs_use_unix_list_options;
67 #endif
68
69 /* "$Id$" */
70
71 #ifdef USE_VFS
72 extern int vfs_timeout;
73 extern int tar_gzipped_memlimit;
74 #endif
75
76 extern char *find_ignore_dirs;
77
78 extern int num_history_items_recorded;
79
80 char *profile_name;
81
82 char setup_color_string [4096];
83 char term_color_string [4096];
84 char color_terminal_string [512];
85
86 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
87 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
88 #define save_string WritePrivateProfileString
89
90 int startup_left_mode;
91 int startup_right_mode;
92
93 /* Ugly hack to allow panel_save_setup to work as a place holder for */
94 /* default panel values */
95 int saving_setup;
96
97 static struct {
98 char *key;
99 sortfn *sort_type;
100 } sort_names [] = {
101 { "name", (sortfn *) sort_name },
102 { "extension", (sortfn *) sort_ext },
103 { "time", (sortfn *) sort_time },
104 { "atime", (sortfn *) sort_atime },
105 { "ctime", (sortfn *) sort_ctime },
106 { "size", (sortfn *) sort_size },
107 { "inode", (sortfn *) sort_inode },
108 { "unsorted", (sortfn *) unsorted },
109 { 0, 0 }
110 };
111
112 static struct {
113 char *key;
114 int list_type;
115 } list_types [] = {
116 { "full", list_full },
117 { "brief", list_brief },
118 { "long", list_long },
119 { "user", list_user },
120 { "icons", list_icons },
121 { 0, 0 }
122 };
123
124 static struct {
125 char *opt_name;
126 int opt_type;
127 } panel_types [] = {
128 { "listing", view_listing },
129 { "quickview", view_quick },
130 { "info", view_info },
131 { "tree", view_tree },
132 { 0, 0 }
133 };
134
135 static struct {
136 char *opt_name;
137 int *opt_addr;
138 } layout [] = {
139 #ifndef HAVE_GNOME
140 { "equal_split", &equal_split },
141 { "first_panel_size", &first_panel_size },
142 { "message_visible", &message_visible },
143 { "keybar_visible", &keybar_visible },
144 { "xterm_hintbar", &xterm_hintbar },
145 { "output_lines", &output_lines },
146 #endif
147 { "menubar_visible", &menubar_visible },
148 { "command_prompt", &command_prompt },
149 { "show_mini_info", &show_mini_info },
150 { "permission_mode", &permission_mode },
151 { "filetype_mode", &filetype_mode },
152 { 0, 0 }
153 };
154
155
156 #undef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
157
158 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
159 extern int preserve_uidgid;
160 #endif
161
162 static struct {
163 char *opt_name;
164 int *opt_addr;
165 } options [] = {
166 { "show_backups", &show_backups },
167 { "show_dot_files", &show_dot_files },
168 { "verbose", &verbose },
169 { "mark_moves_down", &mark_moves_down },
170 { "pause_after_run", &pause_after_run },
171 { "shell_patterns", &easy_patterns },
172 { "auto_save_setup", &auto_save_setup },
173 { "align_extensions", &align_extensions },
174 { "auto_menu", &auto_menu },
175 { "use_internal_view", &use_internal_view },
176 { "use_internal_edit", &use_internal_edit },
177 { "clear_before_exec", &clear_before_exec },
178 { "mix_all_files", &mix_all_files },
179 { "fast_reload", &fast_reload },
180 { "fast_reload_msg_shown", &fast_reload_w },
181 { "confirm_delete", &confirm_delete },
182 { "confirm_overwrite", &confirm_overwrite },
183 { "confirm_execute", &confirm_execute },
184 { "confirm_exit", &confirm_exit },
185 { "safe_delete", &know_not_what_am_i_doing },
186 { "mouse_repeat_rate", &mou_auto_repeat },
187 { "double_click_speed", &double_click_speed },
188 { "eight_bit_clean", &eight_bit_clean },
189 { "full_eight_bits", &full_eight_bits },
190 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
191 { "confirm_view_dir", &confirm_view_dir },
192 { "mouse_move_pages", &mouse_move_pages },
193 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
194 { "fast_refresh", &fast_refresh },
195 { "navigate_with_arrows", &navigate_with_arrows },
196 { "advanced_chown", &advanced_chfns },
197 { "drop_menus", &drop_menus },
198 { "wrap_mode", &global_wrap_mode},
199 { "old_esc_mode", &old_esc_mode },
200 { "cd_symlinks", &cd_symlinks },
201 { "show_all_if_ambiguous", &show_all_if_ambiguous },
202 { "have_fast_cpu", &have_fast_cpu },
203 { "iconify_on_exec", &iconify_on_exec },
204 { "torben_fj_mode", &torben_fj_mode },
205 { "use_file_to_guess_type", &use_file_to_check_type },
206 { "alternate_plus_minus", &alternate_plus_minus },
207 { "only_leading_plus_minus", &only_leading_plus_minus },
208 { "show_output_starts_shell", &output_starts_shell },
209 { "panel_scroll_pages", &panel_scroll_pages },
210 { "xtree_mode", &xtree_mode },
211 { "num_history_items_recorded", &num_history_items_recorded },
212 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
213 { "dive_into_subdirs", &dive_into_subdirs },
214 { "preserve_uidgid", &preserve_uidgid },
215 /* What about the other two options in the copy dialog
216 (follow links, stable symlinks) -Norbert */
217 { "tree_navigation_flag", &tree_navigation_flag },
218 #endif
219 #ifdef USE_VFS
220 { "tar_gzipped_memlimit", &tar_gzipped_memlimit },
221 { "vfs_timeout", &vfs_timeout },
222 { "vfs_use_targz_memlimit", &vfs_use_limit },
223 #ifdef USE_NETCODE
224 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
225 { "use_netrc", &use_netrc },
226 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
227 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
228 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
229 { "ftpfs_use_unix_list_options", &ftpfs_use_passive_connections },
230 #endif
231 #endif
232 #ifdef USE_INTERNAL_EDIT
233 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
234 { "editor_key_emulation", &edit_key_emulation },
235 { "editor_tab_spacing", &option_tab_spacing },
236 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
237 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
238 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
239 { "editor_fake_half_tabs", &option_fake_half_tabs },
240 { "editor_option_save_mode", &option_save_mode },
241 { "editor_option_backup_ext_int", &option_backup_ext_int },
242 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
243 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
244 { "editor_edit_confirm_save", &edit_confirm_save },
245 { "editor_syntax_highlighting", &option_syntax_highlighting },
246 #endif
247
248 #ifdef HAVE_GNOME
249 { "want_transparent_text", &want_transparent_text },
250 { "want_transparent_icons", &want_transparent_icons },
251 { "icons_snap_to_grid", &icons_snap_to_grid },
252 #else
253 { "nice_rotating_dash", &nice_rotating_dash },
254 { "horizontal_split", &horizontal_split },
255 #endif
256 { 0, 0 }
257 };
258
259 void panel_save_setup (WPanel *panel, char *section)
260 {
261 char buffer [40];
262 int i;
263
264 sprintf (buffer, "%d", panel->reverse);
265 save_string (section, "reverse", buffer, profile_name);
266 sprintf (buffer, "%d", panel->case_sensitive);
267 save_string (section, "case_sensitive", buffer, profile_name);
268 for (i = 0; sort_names [i].key; i++)
269 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
270 save_string (section, "sort_order",
271 sort_names [i].key, profile_name);
272 break;
273 }
274
275 for (i = 0; list_types [i].key; i++)
276 if (list_types [i].list_type == panel->list_type){
277 save_string (section, PORT_LIST_MODE_NAME, list_types [i].key, profile_name);
278 break;
279 }
280
281 save_string (section, "user_format",
282 panel->user_format, profile_name);
283
284 for (i = 0; i < LIST_TYPES; i++){
285 sprintf (buffer, "user_status%d", i);
286 save_string (section, buffer,
287 panel->user_status_format [i], profile_name);
288 }
289
290 sprintf (buffer, "%d", panel->user_mini_status);
291 save_string (section, "user_mini_status", buffer,
292 profile_name);
293 }
294
295 void save_layout (void)
296 {
297 char *profile;
298 int i;
299 char buffer [6];
300
301 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
302
303 /* Save integer options */
304 for (i = 0; layout [i].opt_name; i++){
305 sprintf (buffer, "%d", *layout [i].opt_addr);
306 save_string ("Layout", layout [i].opt_name, buffer, profile);
307 }
308
309 free (profile);
310 }
311
312 void save_configure (void)
313 {
314 char *profile;
315 int i;
316
317 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
318
319 /* Save integer options */
320 for (i = 0; options [i].opt_name; i++)
321 set_int (profile, options [i].opt_name, *options [i].opt_addr);
322
323 free (profile);
324 }
325
326 static void panel_save_type (char *section, int type)
327 {
328 int i;
329
330 for (i = 0; panel_types [i].opt_name; i++)
331 if (panel_types [i].opt_type == type){
332 save_string (section, "display", panel_types [i].opt_name,
333 profile_name);
334 break;
335 }
336 }
337
338 #ifndef PORT_HAS_SAVE_PANEL_TYPES
339 void save_panel_types ()
340 {
341 int type;
342
343 type = get_display_type (0);
344 panel_save_type ("New Left Panel", type);
345 if (type == view_listing)
346 panel_save_setup (left_panel, left_panel->panel_name);
347 type = get_display_type (1);
348 panel_save_type ("New Right Panel", type);
349 if (type == view_listing)
350 panel_save_setup (right_panel, right_panel->panel_name);
351 }
352 #endif
353
354 void save_setup (void)
355 {
356 char *profile;
357 #ifdef USE_VFS
358 #ifdef USE_NETCODE
359 extern char *ftpfs_anonymous_passwd;
360 extern char *ftpfs_proxy_host;
361 #endif
362 #endif
363 saving_setup = 1;
364 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
365
366 save_layout ();
367 save_configure ();
368 save_string ("Dirs", "other_dir",
369 get_other_type () == view_listing
370 ? opanel->cwd : ".", profile);
371 WritePrivateProfileString ("Dirs", "current_is_left",
372 get_current_index () == 0 ? "1" : "0", profile);
373 save_hotlist ();
374 save_panelize ();
375 save_panel_types ();
376 /* directory_history_save (); */
377
378 #ifdef USE_VFS
379 #ifdef USE_NETCODE
380 WritePrivateProfileString ("Misc", "ftpfs_password",
381 ftpfs_anonymous_passwd, profile);
382 if (ftpfs_proxy_host)
383 WritePrivateProfileString ("Misc", "ftp_proxy_host",
384 ftpfs_proxy_host, profile);
385 #endif
386 #endif
387 free (profile);
388 saving_setup = 0;
389 }
390
391 void panel_load_setup (WPanel *panel, char *section)
392 {
393 int i;
394 char buffer [40];
395
396 panel->reverse = load_int (section, "reverse", 0);
397 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
398
399 /* Load sort order */
400 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
401 panel->sort_type = (sortfn *) sort_name;
402 for (i = 0; sort_names [i].key; i++)
403 if (strcasecmp (sort_names [i].key, buffer) == 0){
404 panel->sort_type = sort_names [i].sort_type;
405 break;
406 }
407
408 /* Load the listing mode */
409 load_string (section, PORT_LIST_MODE_NAME, PORT_LIST_MODE_DEFAULT, buffer, sizeof (buffer));
410 panel->list_type = list_full;
411 for (i = 0; list_types [i].key; i++)
412 if (strcasecmp (list_types [i].key, buffer) == 0){
413 panel->list_type = list_types [i].list_type;
414 break;
415 }
416 #ifndef PORT_HAS_ICON_VIEW
417 if (panel->list_type == list_icons)
418 panel->list_type = list_full;
419 #endif
420 /* User formats */
421 if (panel->user_format){
422 free (panel->user_format);
423 panel->user_format = 0;
424 }
425 panel->user_format = strdup (get_profile_string (section, "user_format",
426 DEFAULT_USER_FORMAT,
427 profile_name));
428 for (i = 0; i < LIST_TYPES; i++){
429 if (panel->user_status_format [i])
430 free (panel->user_status_format [i]);
431 sprintf (buffer, "user_status%d", i);
432 panel->user_status_format [i] =
433 strdup (get_profile_string (section, buffer,
434 DEFAULT_USER_FORMAT, profile_name));
435 }
436
437 panel->user_mini_status =
438 load_int (section, "user_mini_status", 0);
439
440 }
441
442 static void load_layout (char *profile_name)
443 {
444 int i;
445
446 for (i = 0; layout [i].opt_name; i++)
447 *layout [i].opt_addr =
448 load_int ("Layout", layout [i].opt_name,
449 *layout [i].opt_addr);
450 }
451
452 static int load_mode (char *section)
453 {
454 char buffer [20];
455 int i;
456
457 int mode = view_listing;
458
459 /* Load the display mode */
460 load_string (section, "display", "listing", buffer, sizeof (buffer));
461
462 for (i = 0; panel_types [i].opt_name; i++)
463 if (strcasecmp (panel_types [i].opt_name, buffer) == 0){
464 mode = panel_types [i].opt_type;
465 break;
466 }
467
468 return mode;
469 }
470
471 char *do_load_string (char *s, char *ss, char *def)
472 {
473 char *buffer = xmalloc (128, "dls");
474 char *p;
475
476 load_string (s, ss, def, buffer, 128);
477
478 p = strdup (buffer);
479 free (buffer);
480 return p;
481 }
482
483 void load_setup (void)
484 {
485 static char *buffer;
486 char *profile;
487 char *inifile;
488 int i;
489 #ifdef USE_NETCODE
490 extern char *ftpfs_proxy_host;
491 #endif
492 buffer = concat_dir_and_file (home_dir, PROFILE_NAME);
493 inifile = concat_dir_and_file (mc_home, "mc.ini");
494 if (exist_file (buffer)){
495 profile = buffer;
496 } else if (exist_file (inifile)){
497 profile = strdup (inifile);
498 free (buffer);
499 } else {
500 profile = buffer;
501 }
502 free (inifile);
503
504 profile_name = profile;
505
506 /* Load integer boolean options */
507 for (i = 0; options [i].opt_name; i++)
508 *options [i].opt_addr =
509 get_int (profile, options [i].opt_name, *options [i].opt_addr);
510
511 load_layout (profile);
512
513 load_panelize ();
514
515 startup_left_mode = load_mode ("New Left Panel");
516 startup_right_mode = load_mode ("New Right Panel");
517
518 /* At least one of the panels is a listing panel */
519 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
520 startup_left_mode = view_listing;
521
522 if (!other_dir){
523 buffer = (char*) malloc (MC_MAXPATHLEN);
524 load_string ("Dirs", "other_dir", ".", buffer,
525 MC_MAXPATHLEN);
526 if (vfs_file_is_local (buffer))
527 other_dir = buffer;
528 else
529 free (buffer);
530 }
531 #ifdef USE_NETCODE
532 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
533 #endif
534 boot_current_is_left =
535 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
536
537 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
538 sizeof (setup_color_string));
539 if (setup_color_string [0])
540 find_ignore_dirs = copy_strings (":", setup_color_string, ":", 0);
541
542 /* The default color and the terminal dependent color */
543 load_string ("Colors", "base_color", "", setup_color_string,
544 sizeof (setup_color_string));
545 load_string ("Colors", getenv ("TERM"), "",
546 term_color_string, sizeof (term_color_string));
547 load_string ("Colors", "color_terminals", "",
548 color_terminal_string, sizeof (color_terminal_string));
549
550 /* Load the directory history */
551 /* directory_history_load (); */
552 /* Remove the temporal entries */
553 profile_clean_section ("Temporal:New Left Panel", profile_name);
554 profile_clean_section ("Temporal:New Right Panel", profile_name);
555 #ifdef USE_VFS
556 #ifdef USE_NETCODE
557 ftpfs_init_passwd ();
558 #endif
559 #endif
560 }
561
562 #ifdef USE_VFS
563 #ifdef USE_NETCODE
564 char *load_anon_passwd ()
565 {
566 char buffer [255];
567
568 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
569 if (buffer [0])
570 return strdup (buffer);
571 else
572 return 0;
573 }
574 #endif
575 #endif
576
577 void done_setup (void)
578 {
579 free (profile_name);
580 done_hotlist ();
581 done_panelize ();
582 /* directory_history_free (); */
583 }
584
585 void load_keys_from_section (char *terminal, char *profile_name)
586 {
587 char *section_name;
588 void *profile_keys;
589 char *key, *value, *valcopy;
590 int key_code;
591
592 if (!terminal)
593 return;
594
595 section_name = copy_strings ("terminal:", terminal, 0);
596 profile_keys = profile_init_iterator (section_name, profile_name);
597 if (!profile_keys){
598 free (section_name);
599 return;
600 }
601
602 while (profile_keys){
603 profile_keys = profile_iterator_next (profile_keys, &key, &value);
604 key_code = lookup_key (key);
605 valcopy = convert_controls (value);
606 if (key_code)
607 define_sequence (key_code, valcopy, MCKEY_NOACTION);
608 free (valcopy);
609 }
610 free (section_name);
611 return;
612 }
613
614 void load_key_defs (void)
615 {
616 char *libfile = concat_dir_and_file (mc_home, "mc.lib");
617 load_keys_from_section (getenv ("TERM"), profile_name);
618 load_keys_from_section ("general", profile_name);
619
620 load_keys_from_section (getenv ("TERM"), libfile);
621 load_keys_from_section ("general", libfile);
622
623 /* We don't want a huge database loaded in core */
624 free_profile_name (libfile);
625 free (libfile);
626 }