Added functions to convert GError messages (which are in UTF-8 charset)
[midnight-commander.git] / src / setup.c
blob7dc233f6d84a5d11866925cdda2df34a35cba3b2
1 /* Setup loading/saving.
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 #include <config.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
26 #include "global.h"
27 #include "tty.h"
28 #include "dir.h"
29 #include "panel.h"
30 #include "main.h"
31 #include "tree.h" /* xtree_mode */
32 #include "profile.h"
33 #include "setup.h"
34 #include "mouse.h" /* To make view.h happy */
35 #include "view.h" /* For the externs */
36 #include "key.h" /* For the externs */
37 #include "hotlist.h" /* load/save/done hotlist */
38 #include "panelize.h" /* load/save/done panelize */
39 #include "layout.h"
40 #include "menu.h" /* menubar_visible declaration */
41 #include "win.h" /* lookup_key */
42 #include "cmd.h"
43 #include "file.h" /* safe_delete */
45 #ifdef USE_VFS
46 #include "../vfs/gc.h"
47 #endif
49 #ifdef HAVE_CHARSET
50 #include "charsets.h"
51 #endif
53 #ifdef USE_NETCODE
54 # include "../vfs/ftpfs.h"
55 # include "../vfs/fish.h"
56 #endif
58 #ifdef USE_INTERNAL_EDIT
59 # include "../edit/edit.h"
60 #endif
62 #include "../src/strutil.h" /* str_isutf8 () */
65 extern char *find_ignore_dirs;
67 extern int num_history_items_recorded;
69 char *profile_name; /* .mc/ini */
70 char *global_profile_name; /* mc.lib */
72 char setup_color_string [4096];
73 char term_color_string [4096];
74 char color_terminal_string [512];
76 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
77 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
78 #define save_string WritePrivateProfileString
80 int startup_left_mode;
81 int startup_right_mode;
83 /* Ugly hack to allow panel_save_setup to work as a place holder for */
84 /* default panel values */
85 int saving_setup;
87 static const struct {
88 const char *key;
89 sortfn *sort_type;
90 } sort_names [] = {
91 { "name", (sortfn *) sort_name },
92 { "extension", (sortfn *) sort_ext },
93 { "time", (sortfn *) sort_time },
94 { "atime", (sortfn *) sort_atime },
95 { "ctime", (sortfn *) sort_ctime },
96 { "size", (sortfn *) sort_size },
97 { "inode", (sortfn *) sort_inode },
98 { "unsorted", (sortfn *) unsorted },
99 { 0, 0 }
102 static const struct {
103 const char *key;
104 int list_type;
105 } list_types [] = {
106 { "full", list_full },
107 { "brief", list_brief },
108 { "long", list_long },
109 { "user", list_user },
110 { 0, 0 }
113 static const struct {
114 const char *opt_name;
115 int opt_type;
116 } panel_types [] = {
117 { "listing", view_listing },
118 { "quickview", view_quick },
119 { "info", view_info },
120 { "tree", view_tree },
121 { 0, 0 }
124 static const struct {
125 const char *opt_name;
126 int *opt_addr;
127 } layout [] = {
128 { "equal_split", &equal_split },
129 { "first_panel_size", &first_panel_size },
130 { "message_visible", &message_visible },
131 { "keybar_visible", &keybar_visible },
132 { "xterm_title", &xterm_title },
133 { "output_lines", &output_lines },
134 { "command_prompt", &command_prompt },
135 { "menubar_visible", &menubar_visible },
136 { "show_mini_info", &show_mini_info },
137 { "permission_mode", &permission_mode },
138 { "filetype_mode", &filetype_mode },
139 { "free_space", &free_space },
140 { 0, 0 }
143 static const struct {
144 const char *opt_name;
145 int *opt_addr;
146 } int_options [] = {
147 { "show_backups", &show_backups },
148 { "show_dot_files", &show_dot_files },
149 { "verbose", &verbose },
150 { "mark_moves_down", &mark_moves_down },
151 { "pause_after_run", &pause_after_run },
152 { "shell_patterns", &easy_patterns },
153 { "auto_save_setup", &auto_save_setup },
154 { "auto_menu", &auto_menu },
155 { "use_internal_view", &use_internal_view },
156 { "use_internal_edit", &use_internal_edit },
157 { "clear_before_exec", &clear_before_exec },
158 { "mix_all_files", &mix_all_files },
159 { "fast_reload", &fast_reload },
160 { "fast_reload_msg_shown", &fast_reload_w },
161 { "confirm_delete", &confirm_delete },
162 { "confirm_overwrite", &confirm_overwrite },
163 { "confirm_execute", &confirm_execute },
164 { "confirm_exit", &confirm_exit },
165 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
166 { "safe_delete", &safe_delete },
167 { "mouse_repeat_rate", &mou_auto_repeat },
168 { "double_click_speed", &double_click_speed },
169 #ifndef HAVE_CHARSET
170 { "eight_bit_clean", &eight_bit_clean },
171 { "full_eight_bits", &full_eight_bits },
172 #endif /* !HAVE_CHARSET */
173 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
174 { "confirm_view_dir", &confirm_view_dir },
175 { "mouse_move_pages", &mouse_move_pages },
176 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
177 { "fast_refresh", &fast_refresh },
178 { "navigate_with_arrows", &navigate_with_arrows },
179 { "drop_menus", &drop_menus },
180 { "wrap_mode", &global_wrap_mode},
181 { "old_esc_mode", &old_esc_mode },
182 { "cd_symlinks", &cd_symlinks },
183 { "show_all_if_ambiguous", &show_all_if_ambiguous },
184 { "max_dirt_limit", &max_dirt_limit },
185 { "torben_fj_mode", &torben_fj_mode },
186 { "use_file_to_guess_type", &use_file_to_check_type },
187 { "alternate_plus_minus", &alternate_plus_minus },
188 { "only_leading_plus_minus", &only_leading_plus_minus },
189 { "show_output_starts_shell", &output_starts_shell },
190 { "panel_scroll_pages", &panel_scroll_pages },
191 { "xtree_mode", &xtree_mode },
192 { "num_history_items_recorded", &num_history_items_recorded },
193 { "file_op_compute_totals", &file_op_compute_totals },
194 #ifdef USE_VFS
195 { "vfs_timeout", &vfs_timeout },
196 #ifdef USE_NETCODE
197 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
198 { "use_netrc", &use_netrc },
199 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
200 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
201 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
202 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
203 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
204 { "fish_directory_timeout", &fish_directory_timeout },
205 #endif /* USE_NETCODE */
206 #endif /* USE_VFS */
207 #ifdef USE_INTERNAL_EDIT
208 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
209 { "editor_key_emulation", &edit_key_emulation },
210 { "editor_tab_spacing", &option_tab_spacing },
211 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
212 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
213 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
214 { "editor_fake_half_tabs", &option_fake_half_tabs },
215 { "editor_option_save_mode", &option_save_mode },
216 { "editor_option_save_position", &option_save_position },
217 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
218 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
219 { "editor_edit_confirm_save", &edit_confirm_save },
220 { "editor_syntax_highlighting", &option_syntax_highlighting },
221 { "editor_persistent_selections", &option_persistent_selections },
222 { "editor_visible_tabs", &visible_tabs },
223 { "editor_visible_spaces", &visible_tws },
224 #endif /* USE_INTERNAL_EDIT */
226 { "nice_rotating_dash", &nice_rotating_dash },
227 { "horizontal_split", &horizontal_split },
228 { "mcview_remember_file_position", &mcview_remember_file_position },
229 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
230 { 0, 0 }
233 static const struct {
234 const char *opt_name;
235 char **opt_addr;
236 const char *opt_defval;
237 } str_options [] = {
238 #ifdef USE_INTERNAL_EDIT
239 { "editor_backup_extension", &option_backup_ext, "~" },
240 #endif
241 { NULL, NULL, NULL }
244 void
245 panel_save_setup (struct WPanel *panel, const char *section)
247 char buffer [BUF_TINY];
248 int i;
250 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
251 save_string (section, "reverse", buffer, profile_name);
252 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
253 save_string (section, "case_sensitive", buffer, profile_name);
254 g_snprintf (buffer, sizeof (buffer), "%d", panel->exec_first);
255 save_string (section, "exec_first", buffer, profile_name);
256 for (i = 0; sort_names [i].key; i++)
257 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
258 save_string (section, "sort_order",
259 sort_names [i].key, profile_name);
260 break;
263 for (i = 0; list_types [i].key; i++)
264 if (list_types [i].list_type == panel->list_type){
265 save_string (section, "list_mode", list_types [i].key, profile_name);
266 break;
269 save_string (section, "user_format",
270 panel->user_format, profile_name);
272 for (i = 0; i < LIST_TYPES; i++){
273 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
274 save_string (section, buffer,
275 panel->user_status_format [i], profile_name);
278 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
279 save_string (section, "user_mini_status", buffer,
280 profile_name);
283 void
284 save_layout (void)
286 char *profile;
287 int i;
288 char buffer [BUF_TINY];
290 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
292 /* Save integer options */
293 for (i = 0; layout [i].opt_name; i++){
294 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
295 save_string ("Layout", layout [i].opt_name, buffer, profile);
298 g_free (profile);
301 void
302 save_configure (void)
304 char *profile;
305 int i;
307 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
309 /* Save integer options */
310 for (i = 0; int_options[i].opt_name; i++)
311 set_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
313 /* Save string options */
314 for (i = 0; str_options[i].opt_name != NULL; i++)
315 set_config_string (profile, str_options[i].opt_name,
316 *str_options[i].opt_addr);
318 g_free (profile);
321 static void
322 panel_save_type (const char *section, int type)
324 int i;
326 for (i = 0; panel_types [i].opt_name; i++)
327 if (panel_types [i].opt_type == type){
328 save_string (section, "display", panel_types [i].opt_name,
329 profile_name);
330 break;
334 static void
335 save_panel_types (void)
337 int type;
339 type = get_display_type (0);
340 panel_save_type ("New Left Panel", type);
341 if (type == view_listing)
342 panel_save_setup (left_panel, left_panel->panel_name);
343 type = get_display_type (1);
344 panel_save_type ("New Right Panel", type);
345 if (type == view_listing)
346 panel_save_setup (right_panel, right_panel->panel_name);
349 void
350 save_setup (void)
352 char *profile;
354 saving_setup = 1;
355 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
357 save_configure ();
359 save_layout ();
360 save_string ("Dirs", "other_dir",
361 get_other_type () == view_listing
362 ? other_panel->cwd : ".", profile);
363 if (current_panel != NULL)
364 WritePrivateProfileString ("Dirs", "current_is_left",
365 get_current_index () == 0 ? "1" : "0", profile);
366 save_hotlist ();
368 save_panelize ();
369 save_panel_types ();
370 /* directory_history_save (); */
372 #if defined(USE_VFS) && defined (USE_NETCODE)
373 WritePrivateProfileString ("Misc", "ftpfs_password",
374 ftpfs_anonymous_passwd, profile);
375 if (ftpfs_proxy_host)
376 WritePrivateProfileString ("Misc", "ftp_proxy_host",
377 ftpfs_proxy_host, profile);
378 #endif /* USE_VFS && USE_NETCODE */
380 #ifdef HAVE_CHARSET
381 save_string( "Misc", "display_codepage",
382 get_codepage_id( display_codepage ), profile_name );
383 save_string( "Misc", "source_codepage",
384 get_codepage_id( source_codepage ), profile_name );
385 #endif /* HAVE_CHARSET */
387 g_free (profile);
388 saving_setup = 0;
391 void
392 panel_load_setup (WPanel *panel, const char *section)
394 int i;
395 char buffer [BUF_TINY];
397 panel->reverse = load_int (section, "reverse", 0);
398 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
399 panel->exec_first = load_int (section, "exec_first", 0);
401 /* Load sort order */
402 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
403 panel->sort_type = (sortfn *) sort_name;
404 for (i = 0; sort_names [i].key; i++)
405 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
406 panel->sort_type = sort_names [i].sort_type;
407 break;
410 /* Load the listing mode */
411 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
412 panel->list_type = list_full;
413 for (i = 0; list_types [i].key; i++)
414 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
415 panel->list_type = list_types [i].list_type;
416 break;
419 /* User formats */
420 g_free (panel->user_format);
421 panel->user_format = g_strdup (get_profile_string (section, "user_format",
422 DEFAULT_USER_FORMAT,
423 profile_name));
424 for (i = 0; i < LIST_TYPES; i++){
425 g_free (panel->user_status_format [i]);
426 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
427 panel->user_status_format [i] =
428 g_strdup (get_profile_string (section, buffer,
429 DEFAULT_USER_FORMAT, profile_name));
432 panel->user_mini_status =
433 load_int (section, "user_mini_status", 0);
437 static void
438 load_layout (char *profile_name)
440 int i;
442 for (i = 0; layout [i].opt_name; i++)
443 *layout [i].opt_addr =
444 load_int ("Layout", layout [i].opt_name,
445 *layout [i].opt_addr);
448 static int
449 load_mode (const char *section)
451 char buffer [20];
452 int i;
454 int mode = view_listing;
456 /* Load the display mode */
457 load_string (section, "display", "listing", buffer, sizeof (buffer));
459 for (i = 0; panel_types [i].opt_name; i++)
460 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
461 mode = panel_types [i].opt_type;
462 break;
465 return mode;
468 #ifdef USE_NETCODE
469 static char *
470 do_load_string (const char *s, const char *ss, const char *def)
472 char *buffer = g_malloc (BUF_SMALL);
473 char *p;
475 load_string (s, ss, def, buffer, BUF_SMALL);
477 p = g_strdup (buffer);
478 g_free (buffer);
479 return p;
481 #endif /* !USE_NETCODE */
483 char *
484 setup_init (void)
486 char *profile;
487 char *inifile;
489 if (profile_name)
490 return profile_name;
492 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
493 if (!exist_file (profile)){
494 inifile = concat_dir_and_file (mc_home, "mc.ini");
495 if (exist_file (inifile)){
496 g_free (profile);
497 profile = inifile;
498 } else
499 g_free (inifile);
502 profile_name = profile;
504 return profile;
507 void
508 load_setup (void)
510 char *profile;
511 int i;
513 profile = setup_init ();
515 /* mc.lib is common for all users, but has priority lower than
516 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
517 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
519 /* Load integer boolean options */
520 for (i = 0; int_options[i].opt_name; i++)
521 *int_options[i].opt_addr =
522 get_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
524 /* Load string options */
525 for (i = 0; str_options[i].opt_name != NULL; i++)
526 *str_options[i].opt_addr = get_config_string (profile,
527 str_options[i].opt_name, str_options[i].opt_defval);
529 load_layout (profile);
531 load_panelize ();
533 startup_left_mode = load_mode ("New Left Panel");
534 startup_right_mode = load_mode ("New Right Panel");
536 /* At least one of the panels is a listing panel */
537 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
538 startup_left_mode = view_listing;
540 if (!other_dir){
541 char *buffer;
543 buffer = (char*) g_malloc (MC_MAXPATHLEN);
544 load_string ("Dirs", "other_dir", ".", buffer,
545 MC_MAXPATHLEN);
546 if (vfs_file_is_local (buffer))
547 other_dir = buffer;
548 else
549 g_free (buffer);
552 boot_current_is_left =
553 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
555 #ifdef USE_NETCODE
556 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
557 #endif
559 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
560 sizeof (setup_color_string));
561 if (setup_color_string [0])
562 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
564 /* The default color and the terminal dependent color */
565 load_string ("Colors", "base_color", "", setup_color_string,
566 sizeof (setup_color_string));
567 load_string ("Colors", getenv ("TERM"), "",
568 term_color_string, sizeof (term_color_string));
569 load_string ("Colors", "color_terminals", "",
570 color_terminal_string, sizeof (color_terminal_string));
572 /* Load the directory history */
573 /* directory_history_load (); */
574 /* Remove the temporal entries */
575 profile_clean_section ("Temporal:New Left Panel", profile_name);
576 profile_clean_section ("Temporal:New Right Panel", profile_name);
577 #if defined(USE_VFS) && defined (USE_NETCODE)
578 ftpfs_init_passwd ();
579 #endif /* USE_VFS && USE_NETCODE */
581 #ifdef HAVE_CHARSET
582 if ( load_codepages_list() > 0 ) {
583 char cpname[128];
584 load_string( "Misc", "display_codepage", "",
585 cpname, sizeof(cpname) );
586 if ( cpname[0] != '\0' )
588 display_codepage = get_codepage_index( cpname );
589 cp_display = get_codepage_id (display_codepage);
591 load_string( "Misc", "source_codepage", "",
592 cpname, sizeof(cpname) );
593 if ( cpname[0] != '\0' )
595 source_codepage = get_codepage_index( cpname );
596 cp_source = get_codepage_id (source_codepage);
599 init_translation_table( source_codepage, display_codepage );
600 if ( get_codepage_id( display_codepage ) )
601 utf8_display = str_isutf8 (get_codepage_id( display_codepage ));
602 #endif /* HAVE_CHARSET */
605 #if defined(USE_VFS) && defined (USE_NETCODE)
606 char *
607 load_anon_passwd ()
609 char buffer [255];
611 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
612 if (buffer [0])
613 return g_strdup (buffer);
614 else
615 return 0;
617 #endif /* USE_VFS && USE_NETCODE */
619 void done_setup (void)
621 g_free (profile_name);
622 g_free (global_profile_name);
623 done_hotlist ();
624 done_panelize ();
625 /* directory_history_free (); */
628 static void
629 load_keys_from_section (const char *terminal, const char *profile_name)
631 char *section_name;
632 void *profile_keys;
633 char *key, *value, *valcopy;
634 int key_code;
636 if (!terminal)
637 return;
639 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
640 profile_keys = profile_init_iterator (section_name, profile_name);
641 g_free (section_name);
642 while (profile_keys){
643 profile_keys = profile_iterator_next (profile_keys, &key, &value);
645 /* copy=other causes all keys from [terminal:other] to be loaded. */
646 if (g_strcasecmp (key, "copy") == 0) {
647 load_keys_from_section (value, profile_name);
648 continue;
651 key_code = lookup_key (key);
652 if (key_code){
653 valcopy = convert_controls (value);
654 define_sequence (key_code, valcopy, MCKEY_NOACTION);
655 g_free (valcopy);
660 void load_key_defs (void)
663 * Load keys from mc.lib before ~/.mc/ini, so that the user
664 * definitions override global settings.
666 load_keys_from_section ("general", global_profile_name);
667 load_keys_from_section (getenv ("TERM"), global_profile_name);
668 load_keys_from_section ("general", profile_name);
669 load_keys_from_section (getenv ("TERM"), profile_name);
671 /* We don't want a huge database loaded in core */
672 free_profile_name (global_profile_name);