Just a little correction at the it.po file.
[midnight-commander.git] / src / setup.c
blobca843dd9352e610d06f6cbc4cea517a284aa0247
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" /* xtree_mode */
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" /* safe_delete */
44 #ifdef HAVE_CHARSET
45 #include "charsets.h"
46 #endif
48 #ifdef USE_NETCODE
49 # include "../vfs/ftpfs.h"
50 #endif
52 #ifdef USE_INTERNAL_EDIT
53 # include "../edit/edit.h"
54 #endif
57 extern char *find_ignore_dirs;
59 extern int num_history_items_recorded;
61 char *profile_name; /* .mc/ini */
62 char *global_profile_name; /* mc.lib */
64 char setup_color_string [4096];
65 char term_color_string [4096];
66 char color_terminal_string [512];
68 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
69 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
70 #define save_string WritePrivateProfileString
72 int startup_left_mode;
73 int startup_right_mode;
75 /* Ugly hack to allow panel_save_setup to work as a place holder for */
76 /* default panel values */
77 int saving_setup;
79 static const struct {
80 char *key;
81 sortfn *sort_type;
82 } sort_names [] = {
83 { "name", (sortfn *) sort_name },
84 { "extension", (sortfn *) sort_ext },
85 { "time", (sortfn *) sort_time },
86 { "atime", (sortfn *) sort_atime },
87 { "ctime", (sortfn *) sort_ctime },
88 { "size", (sortfn *) sort_size },
89 { "inode", (sortfn *) sort_inode },
90 { "unsorted", (sortfn *) unsorted },
91 { 0, 0 }
94 static const struct {
95 char *key;
96 int list_type;
97 } list_types [] = {
98 { "full", list_full },
99 { "brief", list_brief },
100 { "long", list_long },
101 { "user", list_user },
102 { 0, 0 }
105 static const struct {
106 char *opt_name;
107 int opt_type;
108 } panel_types [] = {
109 { "listing", view_listing },
110 { "quickview", view_quick },
111 { "info", view_info },
112 { "tree", view_tree },
113 { 0, 0 }
116 static const struct {
117 char *opt_name;
118 int *opt_addr;
119 } layout [] = {
120 { "equal_split", &equal_split },
121 { "first_panel_size", &first_panel_size },
122 { "message_visible", &message_visible },
123 { "keybar_visible", &keybar_visible },
124 { "xterm_title", &xterm_title },
125 { "output_lines", &output_lines },
126 { "command_prompt", &command_prompt },
127 { "menubar_visible", &menubar_visible },
128 { "show_mini_info", &show_mini_info },
129 { "permission_mode", &permission_mode },
130 { "filetype_mode", &filetype_mode },
131 { 0, 0 }
134 static const struct {
135 char *opt_name;
136 int *opt_addr;
137 } options [] = {
138 { "show_backups", &show_backups },
139 { "show_dot_files", &show_dot_files },
140 { "verbose", &verbose },
141 { "mark_moves_down", &mark_moves_down },
142 { "pause_after_run", &pause_after_run },
143 { "shell_patterns", &easy_patterns },
144 { "auto_save_setup", &auto_save_setup },
145 { "auto_menu", &auto_menu },
146 { "use_internal_view", &use_internal_view },
147 { "use_internal_edit", &use_internal_edit },
148 { "clear_before_exec", &clear_before_exec },
149 { "mix_all_files", &mix_all_files },
150 { "fast_reload", &fast_reload },
151 { "fast_reload_msg_shown", &fast_reload_w },
152 { "confirm_delete", &confirm_delete },
153 { "confirm_overwrite", &confirm_overwrite },
154 { "confirm_execute", &confirm_execute },
155 { "confirm_exit", &confirm_exit },
156 { "safe_delete", &safe_delete },
157 { "mouse_repeat_rate", &mou_auto_repeat },
158 { "double_click_speed", &double_click_speed },
159 #ifndef HAVE_CHARSET
160 { "eight_bit_clean", &eight_bit_clean },
161 { "full_eight_bits", &full_eight_bits },
162 #endif /* !HAVE_CHARSET */
163 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
164 { "confirm_view_dir", &confirm_view_dir },
165 { "mouse_move_pages", &mouse_move_pages },
166 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
167 { "fast_refresh", &fast_refresh },
168 { "navigate_with_arrows", &navigate_with_arrows },
169 { "drop_menus", &drop_menus },
170 { "wrap_mode", &global_wrap_mode},
171 { "old_esc_mode", &old_esc_mode },
172 { "cd_symlinks", &cd_symlinks },
173 { "show_all_if_ambiguous", &show_all_if_ambiguous },
174 { "have_fast_cpu", &have_fast_cpu },
175 { "max_dirt_limit", &max_dirt_limit },
176 { "torben_fj_mode", &torben_fj_mode },
177 { "use_file_to_guess_type", &use_file_to_check_type },
178 { "alternate_plus_minus", &alternate_plus_minus },
179 { "only_leading_plus_minus", &only_leading_plus_minus },
180 { "show_output_starts_shell", &output_starts_shell },
181 { "panel_scroll_pages", &panel_scroll_pages },
182 { "xtree_mode", &xtree_mode },
183 { "num_history_items_recorded", &num_history_items_recorded },
184 { "file_op_compute_totals", &file_op_compute_totals },
185 #ifdef USE_VFS
186 { "vfs_timeout", &vfs_timeout },
187 #ifdef USE_NETCODE
188 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
189 { "use_netrc", &use_netrc },
190 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
191 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
192 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
193 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
194 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
195 #endif /* USE_NETCODE */
196 #endif /* USE_VFS */
197 #ifdef USE_INTERNAL_EDIT
198 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
199 { "editor_key_emulation", &edit_key_emulation },
200 { "editor_tab_spacing", &option_tab_spacing },
201 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
202 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
203 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
204 { "editor_fake_half_tabs", &option_fake_half_tabs },
205 { "editor_option_save_mode", &option_save_mode },
206 { "editor_option_save_position", &option_save_position },
207 { "editor_option_backup_ext_int", &option_backup_ext_int },
208 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
209 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
210 { "editor_edit_confirm_save", &edit_confirm_save },
211 { "editor_syntax_highlighting", &option_syntax_highlighting },
212 #endif /* USE_INTERNAL_EDIT */
214 { "nice_rotating_dash", &nice_rotating_dash },
215 { "horizontal_split", &horizontal_split },
216 { 0, 0 }
219 void
220 panel_save_setup (struct WPanel *panel, char *section)
222 char buffer [BUF_TINY];
223 int i;
225 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
226 save_string (section, "reverse", buffer, profile_name);
227 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
228 save_string (section, "case_sensitive", buffer, profile_name);
229 for (i = 0; sort_names [i].key; i++)
230 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
231 save_string (section, "sort_order",
232 sort_names [i].key, profile_name);
233 break;
236 for (i = 0; list_types [i].key; i++)
237 if (list_types [i].list_type == panel->list_type){
238 save_string (section, "list_mode", list_types [i].key, profile_name);
239 break;
242 save_string (section, "user_format",
243 panel->user_format, profile_name);
245 for (i = 0; i < LIST_TYPES; i++){
246 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
247 save_string (section, buffer,
248 panel->user_status_format [i], profile_name);
251 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
252 save_string (section, "user_mini_status", buffer,
253 profile_name);
256 void
257 save_layout (void)
259 char *profile;
260 int i;
261 char buffer [BUF_TINY];
263 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
265 /* Save integer options */
266 for (i = 0; layout [i].opt_name; i++){
267 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
268 save_string ("Layout", layout [i].opt_name, buffer, profile);
271 g_free (profile);
274 void
275 save_configure (void)
277 char *profile;
278 int i;
280 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
282 /* Save integer options */
283 for (i = 0; options [i].opt_name; i++)
284 set_int (profile, options [i].opt_name, *options [i].opt_addr);
286 g_free (profile);
289 static void
290 panel_save_type (char *section, int type)
292 int i;
294 for (i = 0; panel_types [i].opt_name; i++)
295 if (panel_types [i].opt_type == type){
296 save_string (section, "display", panel_types [i].opt_name,
297 profile_name);
298 break;
302 void
303 save_panel_types (void)
305 int type;
307 type = get_display_type (0);
308 panel_save_type ("New Left Panel", type);
309 if (type == view_listing)
310 panel_save_setup (left_panel, left_panel->panel_name);
311 type = get_display_type (1);
312 panel_save_type ("New Right Panel", type);
313 if (type == view_listing)
314 panel_save_setup (right_panel, right_panel->panel_name);
317 void
318 save_setup (void)
320 char *profile;
322 saving_setup = 1;
323 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
325 save_configure ();
327 save_layout ();
328 save_string ("Dirs", "other_dir",
329 get_other_type () == view_listing
330 ? opanel->cwd : ".", profile);
331 if (get_current_panel () != NULL)
332 WritePrivateProfileString ("Dirs", "current_is_left",
333 get_current_index () == 0 ? "1" : "0", profile);
334 save_hotlist ();
336 save_panelize ();
337 save_panel_types ();
338 /* directory_history_save (); */
340 #if defined(USE_VFS) && defined (USE_NETCODE)
341 WritePrivateProfileString ("Misc", "ftpfs_password",
342 ftpfs_anonymous_passwd, profile);
343 if (ftpfs_proxy_host)
344 WritePrivateProfileString ("Misc", "ftp_proxy_host",
345 ftpfs_proxy_host, profile);
346 #endif /* USE_VFS && USE_NETCODE */
348 #ifdef HAVE_CHARSET
349 save_string( "Misc", "display_codepage",
350 get_codepage_id( display_codepage ), profile_name );
351 #endif /* HAVE_CHARSET */
353 g_free (profile);
354 saving_setup = 0;
357 void
358 panel_load_setup (WPanel *panel, char *section)
360 int i;
361 char buffer [BUF_TINY];
363 panel->reverse = load_int (section, "reverse", 0);
364 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
366 /* Load sort order */
367 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
368 panel->sort_type = (sortfn *) sort_name;
369 for (i = 0; sort_names [i].key; i++)
370 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
371 panel->sort_type = sort_names [i].sort_type;
372 break;
375 /* Load the listing mode */
376 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
377 panel->list_type = list_full;
378 for (i = 0; list_types [i].key; i++)
379 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
380 panel->list_type = list_types [i].list_type;
381 break;
384 /* User formats */
385 if (panel->user_format){
386 g_free (panel->user_format);
387 panel->user_format = 0;
389 panel->user_format = g_strdup (get_profile_string (section, "user_format",
390 DEFAULT_USER_FORMAT,
391 profile_name));
392 for (i = 0; i < LIST_TYPES; i++){
393 if (panel->user_status_format [i])
394 g_free (panel->user_status_format [i]);
395 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
396 panel->user_status_format [i] =
397 g_strdup (get_profile_string (section, buffer,
398 DEFAULT_USER_FORMAT, profile_name));
401 panel->user_mini_status =
402 load_int (section, "user_mini_status", 0);
406 static void
407 load_layout (char *profile_name)
409 int i;
411 for (i = 0; layout [i].opt_name; i++)
412 *layout [i].opt_addr =
413 load_int ("Layout", layout [i].opt_name,
414 *layout [i].opt_addr);
417 static int
418 load_mode (char *section)
420 char buffer [20];
421 int i;
423 int mode = view_listing;
425 /* Load the display mode */
426 load_string (section, "display", "listing", buffer, sizeof (buffer));
428 for (i = 0; panel_types [i].opt_name; i++)
429 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
430 mode = panel_types [i].opt_type;
431 break;
434 return mode;
437 #ifdef USE_NETCODE
438 static char *
439 do_load_string (char *s, char *ss, char *def)
441 char *buffer = g_malloc (BUF_SMALL);
442 char *p;
444 load_string (s, ss, def, buffer, BUF_SMALL);
446 p = g_strdup (buffer);
447 g_free (buffer);
448 return p;
450 #endif /* !USE_NETCODE */
452 char *
453 setup_init (void)
455 char *profile;
456 char *inifile;
458 if (profile_name)
459 return profile_name;
461 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
462 if (!exist_file (profile)){
463 inifile = concat_dir_and_file (mc_home, "mc.ini");
464 if (exist_file (inifile)){
465 g_free (profile);
466 profile = inifile;
467 } else
468 g_free (inifile);
471 profile_name = profile;
473 return profile;
476 void
477 load_setup (void)
479 char *profile;
480 int i;
482 profile = setup_init ();
484 /* mc.lib is common for all users, but has priority lower than
485 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
486 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
488 /* Load integer boolean options */
489 for (i = 0; options [i].opt_name; i++)
490 *options [i].opt_addr =
491 get_int (profile, options [i].opt_name, *options [i].opt_addr);
493 load_layout (profile);
495 load_panelize ();
497 startup_left_mode = load_mode ("New Left Panel");
498 startup_right_mode = load_mode ("New Right Panel");
500 /* At least one of the panels is a listing panel */
501 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
502 startup_left_mode = view_listing;
504 if (!other_dir){
505 char *buffer;
507 buffer = (char*) g_malloc (MC_MAXPATHLEN);
508 load_string ("Dirs", "other_dir", ".", buffer,
509 MC_MAXPATHLEN);
510 if (vfs_file_is_local (buffer))
511 other_dir = buffer;
512 else
513 g_free (buffer);
516 boot_current_is_left =
517 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
519 #ifdef USE_NETCODE
520 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
521 #endif
523 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
524 sizeof (setup_color_string));
525 if (setup_color_string [0])
526 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", NULL);
528 /* The default color and the terminal dependent color */
529 load_string ("Colors", "base_color", "", setup_color_string,
530 sizeof (setup_color_string));
531 load_string ("Colors", getenv ("TERM"), "",
532 term_color_string, sizeof (term_color_string));
533 load_string ("Colors", "color_terminals", "",
534 color_terminal_string, sizeof (color_terminal_string));
536 /* Load the directory history */
537 /* directory_history_load (); */
538 /* Remove the temporal entries */
539 profile_clean_section ("Temporal:New Left Panel", profile_name);
540 profile_clean_section ("Temporal:New Right Panel", profile_name);
541 #if defined(USE_VFS) && defined (USE_NETCODE)
542 ftpfs_init_passwd ();
543 #endif /* USE_VFS && USE_NETCODE */
545 #ifdef HAVE_CHARSET
546 if ( load_codepages_list() > 0 ) {
547 char cpname[128];
548 load_string( "Misc", "display_codepage", "",
549 cpname, sizeof(cpname) );
550 if ( cpname[0] != '\0' )
551 display_codepage = get_codepage_index( cpname );
554 init_translation_table( source_codepage, display_codepage );
555 #endif /* HAVE_CHARSET */
558 #if defined(USE_VFS) && defined (USE_NETCODE)
559 char *
560 load_anon_passwd ()
562 char buffer [255];
564 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
565 if (buffer [0])
566 return g_strdup (buffer);
567 else
568 return 0;
570 #endif /* USE_VFS && USE_NETCODE */
572 void done_setup (void)
574 g_free (profile_name);
575 g_free (global_profile_name);
576 done_hotlist ();
577 done_panelize ();
578 /* directory_history_free (); */
581 static void
582 load_keys_from_section (char *terminal, char *profile_name)
584 char *section_name;
585 void *profile_keys;
586 char *key, *value, *valcopy;
587 int key_code;
589 if (!terminal)
590 return;
592 section_name = g_strconcat ("terminal:", terminal, NULL);
593 profile_keys = profile_init_iterator (section_name, profile_name);
594 g_free (section_name);
595 while (profile_keys){
596 profile_keys = profile_iterator_next (profile_keys, &key, &value);
597 key_code = lookup_key (key);
598 if (key_code){
599 valcopy = convert_controls (value);
600 define_sequence (key_code, valcopy, MCKEY_NOACTION);
601 g_free (valcopy);
606 void load_key_defs (void)
609 * Load keys from mc.lib before ~/.mc/ini, so that the user
610 * definitions override global settings.
612 load_keys_from_section ("general", global_profile_name);
613 load_keys_from_section (getenv ("TERM"), global_profile_name);
614 load_keys_from_section ("general", profile_name);
615 load_keys_from_section (getenv ("TERM"), profile_name);
617 /* We don't want a huge database loaded in core */
618 free_profile_name (global_profile_name);