mc_fhl_init_from_standard_files(): fixed logic and file read order for config files
[midnight-commander.git] / src / hotlist.c
blob438c2cc2f1ed2ec8f2f8a2164a0c5749a0ff66f4
1 /* Directory hotlist -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 Written by:
6 1994 Radek Doulik
7 1995 Janne Kukonlehto
8 1996 Andrej Borsenkow
9 1997 Norbert Warmuth
11 Janne did the original Hotlist code, Andrej made the groupable
12 hotlist; the move hotlist and revamped the file format and made
13 it stronger.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 /** \file hotlist.c
31 * \brief Source: directory hotlist
34 #include <config.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
43 #include "lib/global.h"
45 #include "lib/tty/tty.h" /* COLS */
46 #include "lib/tty/key.h" /* KEY_M_CTRL */
47 #include "lib/skin.h" /* colors */
48 #include "lib/mcconfig.h" /* Load/save directories hotlist */
49 #include "lib/fileloc.h"
50 #include "lib/strutil.h"
51 #include "lib/vfs/mc-vfs/vfs.h"
53 #include "dialog.h"
54 #include "widget.h"
55 #include "setup.h" /* For profile_bname */
56 #include "wtools.h" /* QuickDialog */
57 #include "panel.h" /* current_panel */
58 #include "main.h" /* update_panels() */
59 #include "layout.h" /* repaint_screen() */
60 #include "hotlist.h"
61 #include "command.h" /* cmdline */
62 #include "history.h"
64 #define UX 5
65 #define UY 2
67 #define BX UX
68 #define BY (LINES - 6)
70 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
71 #define LABELS 3
72 #define B_ADD_CURRENT B_USER
73 #define B_REMOVE (B_USER + 1)
74 #define B_NEW_GROUP (B_USER + 2)
75 #define B_NEW_ENTRY (B_USER + 3)
76 #define B_UP_GROUP (B_USER + 4)
77 #define B_INSERT (B_USER + 5)
78 #define B_APPEND (B_USER + 6)
79 #define B_MOVE (B_USER + 7)
81 #ifdef ENABLE_VFS
82 #define B_FREE_ALL_VFS (B_USER + 8)
83 #define B_REFRESH_VFS (B_USER + 9)
84 #endif
86 int hotlist_has_dot_dot = 1;
88 static WListbox *l_hotlist;
89 static WListbox *l_movelist;
91 static Dlg_head *hotlist_dlg;
92 static Dlg_head *movelist_dlg;
94 static WLabel *pname, *pname_group, *movelist_group;
96 enum HotListType
98 HL_TYPE_GROUP,
99 HL_TYPE_ENTRY,
100 HL_TYPE_COMMENT,
101 HL_TYPE_DOTDOT
104 static struct
107 * these parameters are intended to be user configurable
109 int expanded; /* expanded view of all groups at startup */
112 * these reflect run time state
115 int loaded; /* hotlist is loaded */
116 int readonly; /* hotlist readonly */
117 int file_error; /* parse error while reading file */
118 int running; /* we are running dlg (and have to
119 update listbox */
120 int moving; /* we are in moving hotlist currently */
121 int modified; /* hotlist was modified */
122 int type; /* LIST_HOTLIST || LIST_VFSLIST */
123 } hotlist_state;
125 static struct _hotlist_but
127 int ret_cmd, flags, y, x;
128 const char *text;
129 int type;
130 widget_pos_flags_t pos_flags;
131 } hotlist_but[] =
133 /* *INDENT-OFF* */
134 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
135 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"),
136 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
137 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"),
138 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
139 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"),
140 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
141 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &entry"),
142 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
143 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &group"),
144 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
145 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"),
146 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
147 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"),
148 LIST_HOTLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
149 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),
150 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
151 #ifdef ENABLE_VFS
152 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"),
153 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
154 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"),
155 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
156 #endif
157 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &to"),
158 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM }
159 /* *INDENT-ON* */
162 /* Directory hotlist */
163 static struct hotlist
165 enum HotListType type;
166 char *directory;
167 char *label;
168 struct hotlist *head;
169 struct hotlist *up;
170 struct hotlist *next;
171 } *hotlist = NULL;
173 static struct hotlist *current_group;
175 static void init_movelist (int, struct hotlist *);
176 static void add_new_group_cmd (void);
177 static void add_new_entry_cmd (void);
178 static void remove_from_hotlist (struct hotlist *entry);
179 static void load_hotlist (void);
180 static void add_dotdot_to_list (void);
182 #define new_hotlist() g_new0(struct hotlist, 1)
184 static void
185 hotlist_refresh (Dlg_head * dlg)
187 /* TODO: use groupboxes here */
188 common_dialog_repaint (dlg);
189 tty_setcolor (COLOR_NORMAL);
190 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10), dlg->cols - (UX * 2), TRUE);
191 if (!hotlist_state.moving)
192 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2), TRUE);
195 /* If current->data is 0, then we are dealing with a VFS pathname */
196 static void
197 update_path_name (void)
199 const char *text = "";
200 char *p;
201 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
202 Dlg_head *dlg = list->widget.owner;
204 if (list->count != 0)
206 char *ctext = NULL;
207 void *cdata = NULL;
209 listbox_get_current (list, &ctext, &cdata);
210 if (cdata == NULL)
211 text = ctext;
212 else
214 struct hotlist *hlp = (struct hotlist *) cdata;
216 if (hlp->type == HL_TYPE_ENTRY || hlp->type == HL_TYPE_DOTDOT)
217 text = hlp->directory;
218 else if (hlp->type == HL_TYPE_GROUP)
219 text = _("Subgroup - press ENTER to see list");
222 if (!hotlist_state.moving)
223 label_set_text (pname, str_trunc (text, dlg->cols - (UX * 2 + 4)));
225 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
226 if (!hotlist_state.moving)
227 label_set_text (pname_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
228 else
229 label_set_text (movelist_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
230 g_free (p);
232 dlg_redraw (dlg);
235 #define CHECK_BUFFER \
236 do \
238 size_t i; \
239 i = strlen (current->label); \
240 if (i + 3 > buflen) { \
241 g_free (buf); \
242 buflen = 1024 * (i/1024 + 1); \
243 buf = g_malloc (buflen); \
245 buf[0] = '\0'; \
246 } while (0)
248 static void
249 fill_listbox (void)
251 struct hotlist *current = current_group->head;
252 GString *buff = g_string_new ("");
254 while (current)
256 switch (current->type)
258 case HL_TYPE_GROUP:
260 /* buff clean up */
261 g_string_truncate (buff, 0);
262 g_string_append (buff, "->");
263 g_string_append (buff, current->label);
264 if (hotlist_state.moving)
265 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
266 else
267 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
269 break;
270 case HL_TYPE_DOTDOT:
271 case HL_TYPE_ENTRY:
272 if (hotlist_state.moving)
273 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, current->label, current);
274 else
275 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, current->label, current);
276 break;
277 default:
278 break;
280 current = current->next;
282 g_string_free (buff, TRUE);
285 static void
286 unlink_entry (struct hotlist *entry)
288 struct hotlist *current = current_group->head;
290 if (current == entry)
291 current_group->head = entry->next;
292 else
294 while (current && current->next != entry)
295 current = current->next;
296 if (current)
297 current->next = entry->next;
299 entry->next = entry->up = 0;
302 #ifdef ENABLE_VFS
303 static void
304 add_name_to_list (const char *path)
306 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, path, 0);
308 #endif /* !ENABLE_VFS */
310 static int
311 hotlist_button_callback (WButton *button, int action)
313 (void) button;
315 switch (action)
317 case B_MOVE:
319 struct hotlist *saved = current_group;
320 struct hotlist *item = NULL;
321 struct hotlist *moveto_item = NULL;
322 struct hotlist *moveto_group = NULL;
323 int ret;
325 if (l_hotlist->count == 0)
326 return MSG_NOT_HANDLED; /* empty group - nothing to do */
328 listbox_get_current (l_hotlist, NULL, (void **) &item);
329 hotlist_state.moving = 1;
330 init_movelist (LIST_MOVELIST, item);
332 ret = run_dlg (movelist_dlg);
334 hotlist_state.moving = 0;
335 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
336 moveto_group = current_group;
337 destroy_dlg (movelist_dlg);
338 current_group = saved;
339 if (ret == B_CANCEL)
340 return MSG_NOT_HANDLED;
341 if (moveto_item == item)
342 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
343 it hardly changes anything ;) */
344 unlink_entry (item);
345 listbox_remove_current (l_hotlist);
346 item->up = moveto_group;
347 if (!moveto_group->head)
348 moveto_group->head = item;
349 else if (!moveto_item)
350 { /* we have group with just comments */
351 struct hotlist *p = moveto_group->head;
353 /* skip comments */
354 while (p->next)
355 p = p->next;
356 p->next = item;
358 else if (ret == B_ENTER || ret == B_APPEND)
359 if (!moveto_item->next)
360 moveto_item->next = item;
361 else
363 item->next = moveto_item->next;
364 moveto_item->next = item;
366 else if (moveto_group->head == moveto_item)
368 moveto_group->head = item;
369 item->next = moveto_item;
371 else
373 struct hotlist *p = moveto_group->head;
375 while (p->next != moveto_item)
376 p = p->next;
377 item->next = p->next;
378 p->next = item;
380 listbox_remove_list (l_hotlist);
381 fill_listbox ();
382 repaint_screen ();
383 hotlist_state.modified = 1;
384 return MSG_NOT_HANDLED;
386 case B_REMOVE:
388 struct hotlist *entry = NULL;
389 listbox_get_current (l_hotlist, NULL, (void **) &entry);
390 remove_from_hotlist (entry);
392 return MSG_NOT_HANDLED;
394 case B_NEW_GROUP:
395 add_new_group_cmd ();
396 return MSG_NOT_HANDLED;
398 case B_ADD_CURRENT:
399 add2hotlist_cmd ();
400 return MSG_NOT_HANDLED;
402 case B_NEW_ENTRY:
403 add_new_entry_cmd ();
404 return MSG_NOT_HANDLED;
406 case B_ENTER:
408 WListbox *list;
409 void *data;
410 struct hotlist *hlp;
412 list = hotlist_state.moving ? l_movelist : l_hotlist;
413 listbox_get_current (list, NULL, &data);
415 if (data == NULL)
416 return MSG_HANDLED;
418 hlp = (struct hotlist *) data;
420 if (hlp->type == HL_TYPE_ENTRY)
421 return MSG_HANDLED;
422 if (hlp->type != HL_TYPE_DOTDOT)
424 listbox_remove_list (list);
425 current_group = hlp;
426 fill_listbox ();
427 return MSG_NOT_HANDLED;
429 /* Fall through - go up */
431 /* Fall through if list empty - just go up */
433 case B_UP_GROUP:
435 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
436 listbox_remove_list (list);
437 current_group = current_group->up;
438 fill_listbox ();
439 return MSG_NOT_HANDLED;
442 #ifdef ENABLE_VFS
443 case B_FREE_ALL_VFS:
444 vfs_expire (1);
445 /* fall through */
447 case B_REFRESH_VFS:
448 listbox_remove_list (l_hotlist);
449 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
450 vfs_fill_names (add_name_to_list);
451 return MSG_NOT_HANDLED;
452 #endif /* ENABLE_VFS */
454 default:
455 return MSG_HANDLED;
459 static inline cb_ret_t
460 hotlist_handle_key (Dlg_head * h, int key)
462 switch (key)
464 case KEY_M_CTRL | '\n':
465 goto l1;
467 case '\n':
468 case KEY_ENTER:
469 case KEY_RIGHT:
470 if (hotlist_button_callback (NULL, B_ENTER))
472 h->ret_value = B_ENTER;
473 dlg_stop (h);
475 return MSG_HANDLED;
477 case KEY_LEFT:
478 if (hotlist_state.type != LIST_VFSLIST)
479 return !hotlist_button_callback (NULL, B_UP_GROUP);
480 else
481 return MSG_NOT_HANDLED;
483 case KEY_DC:
484 if (hotlist_state.moving)
485 return MSG_NOT_HANDLED;
486 else
488 hotlist_button_callback (NULL, B_REMOVE);
489 return MSG_HANDLED;
493 case ALT ('\n'):
494 case ALT ('\r'):
495 if (!hotlist_state.moving)
497 void *ldata = NULL;
499 listbox_get_current (l_hotlist, NULL, &ldata);
501 if (ldata != NULL)
503 struct hotlist *hlp = (struct hotlist *) ldata;
505 if (hlp->type == HL_TYPE_ENTRY)
507 char *tmp;
509 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
510 stuff (cmdline, tmp, 0);
511 g_free (tmp);
512 h->ret_value = B_CANCEL;
513 dlg_stop (h);
517 return MSG_HANDLED; /* ignore key */
519 default:
520 return MSG_NOT_HANDLED;
524 static cb_ret_t
525 hotlist_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
527 switch (msg)
529 case DLG_DRAW:
530 hotlist_refresh (h);
531 return MSG_HANDLED;
533 case DLG_UNHANDLED_KEY:
534 return hotlist_handle_key (h, parm);
536 case DLG_POST_KEY:
537 if (hotlist_state.moving)
538 dlg_select_widget (l_movelist);
539 else
540 dlg_select_widget (l_hotlist);
541 /* always stay on hotlist */
542 /* fall through */
544 case DLG_INIT:
545 tty_setcolor (MENU_ENTRY_COLOR);
546 update_path_name ();
547 return MSG_HANDLED;
549 case DLG_RESIZE:
550 /* simply call dlg_set_size() with new size */
551 dlg_set_size (h, LINES - 2, COLS - 6);
552 return MSG_HANDLED;
554 default:
555 return default_dlg_callback (h, sender, msg, parm, data);
559 static int
560 l_call (WListbox * list)
562 Dlg_head *dlg = list->widget.owner;
564 if (list->count != 0)
566 void *data = NULL;
568 listbox_get_current (list, NULL, &data);
570 if (data != NULL)
572 struct hotlist *hlp = (struct hotlist *) data;
573 if (hlp->type == HL_TYPE_ENTRY)
575 dlg->ret_value = B_ENTER;
576 dlg_stop (dlg);
577 return LISTBOX_DONE;
579 else
581 hotlist_button_callback (NULL, B_ENTER);
582 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
583 return LISTBOX_CONT;
586 else
588 dlg->ret_value = B_ENTER;
589 dlg_stop (dlg);
590 return LISTBOX_DONE;
594 hotlist_button_callback (NULL, B_UP_GROUP);
595 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
596 return LISTBOX_CONT;
600 * Expands all button names (once) and recalculates button positions.
601 * returns number of columns in the dialog box, which is 10 chars longer
602 * then buttonbar.
604 * If common width of the window (i.e. in xterm) is less than returned
605 * width - sorry :) (anyway this did not handled in previous version too)
607 static int
608 init_i18n_stuff (int list_type, int cols)
610 register int i;
611 static const char *cancel_but = N_("&Cancel");
613 #ifdef ENABLE_NLS
614 static int hotlist_i18n_flag = 0;
616 if (!hotlist_i18n_flag)
618 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
619 while (i--)
620 hotlist_but[i].text = _(hotlist_but[i].text);
622 cancel_but = _(cancel_but);
623 hotlist_i18n_flag = 1;
625 #endif /* ENABLE_NLS */
627 /* Dynamic resizing of buttonbars */
629 int len[2], count[2]; /* at most two lines of buttons */
630 int cur_x[2], row;
632 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
633 len[0] = len[1] = count[0] = count[1] = 0;
635 /* Count len of buttonbars, assuming 2 extra space between buttons */
636 while (i--)
638 if (!(hotlist_but[i].type & list_type))
639 continue;
641 row = hotlist_but[i].y;
642 ++count[row];
643 len[row] += str_term_width1 (hotlist_but[i].text) + 5;
644 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
645 len[row] += 2;
647 len[0] -= 2;
648 len[1] -= 2;
650 cols = max (cols, max (len[0], len[1]));
652 /* arrange buttons */
654 cur_x[0] = cur_x[1] = 0;
655 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
656 while (i--)
658 if (!(hotlist_but[i].type & list_type))
659 continue;
661 row = hotlist_but[i].y;
663 if (hotlist_but[i].x != 0)
665 /* not first int the row */
666 if (!strcmp (hotlist_but[i].text, cancel_but))
667 hotlist_but[i].x = cols - str_term_width1 (hotlist_but[i].text) - 13;
668 else
669 hotlist_but[i].x = cur_x[row];
672 cur_x[row] += str_term_width1 (hotlist_but[i].text) + 2
673 + (hotlist_but[i].flags == DEFPUSH_BUTTON ? 5 : 3);
677 return cols;
680 static void
681 init_hotlist (int list_type)
683 size_t i;
684 const char *title, *help_node;
685 int hotlist_cols;
687 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
689 do_refresh ();
691 hotlist_state.expanded =
692 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
694 if (list_type == LIST_VFSLIST)
696 title = _("Active VFS directories");
697 help_node = "[vfshot]"; /* FIXME - no such node */
699 else
701 title = _("Directory hotlist");
702 help_node = "[Hotlist]";
705 hotlist_dlg =
706 create_dlg (TRUE, 0, 0, LINES - 2, hotlist_cols, dialog_colors,
707 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
709 for (i = 0; i < BUTTONS; i++)
711 if (hotlist_but[i].type & list_type)
712 add_widget_autopos (hotlist_dlg,
713 button_new (BY + hotlist_but[i].y,
714 BX + hotlist_but[i].x,
715 hotlist_but[i].ret_cmd,
716 hotlist_but[i].flags,
717 hotlist_but[i].text,
718 hotlist_button_callback), hotlist_but[i].pos_flags);
721 /* We add the labels.
722 * pname will hold entry's pathname;
723 * pname_group will hold name of current group
725 pname = label_new (UY - 11 + LINES, UX + 2, "");
726 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
727 if (!hotlist_state.moving)
729 char label_text[BUF_TINY];
731 g_snprintf (label_text, sizeof (label_text), " %s ", _("Directory path"));
732 add_widget_autopos (hotlist_dlg,
733 label_new (UY - 12 + LINES, UX + 2,
734 label_text), WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
736 /* This one holds the displayed pathname */
737 pname_group = label_new (UY, UX + 2, _("Directory label"));
738 add_widget (hotlist_dlg, pname_group);
740 /* get new listbox */
741 l_hotlist = listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8, FALSE, l_call);
743 /* Fill the hotlist with the active VFS or the hotlist */
744 #ifdef ENABLE_VFS
745 if (list_type == LIST_VFSLIST)
747 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
748 vfs_fill_names (add_name_to_list);
750 else
751 #endif /* !ENABLE_VFS */
752 fill_listbox ();
754 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
755 /* add listbox to the dialogs */
758 static void
759 init_movelist (int list_type, struct hotlist *item)
761 size_t i;
762 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
763 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
765 do_refresh ();
767 movelist_dlg =
768 create_dlg (TRUE, 0, 0, LINES - 6, movelist_cols, dialog_colors,
769 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
770 g_free (hdr);
772 for (i = 0; i < BUTTONS; i++)
774 if (hotlist_but[i].type & list_type)
775 add_widget (movelist_dlg,
776 button_new (BY - 4 + hotlist_but[i].y,
777 BX + hotlist_but[i].x,
778 hotlist_but[i].ret_cmd,
779 hotlist_but[i].flags,
780 hotlist_but[i].text, hotlist_button_callback));
783 /* We add the labels. We are interested in the last one,
784 * that one will hold the path name label
786 movelist_group = label_new (UY, UX + 2, _("Directory label"));
787 add_widget (movelist_dlg, movelist_group);
788 /* get new listbox */
789 l_movelist =
790 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
791 movelist_dlg->cols - 2 * UX - 2, FALSE, l_call);
793 fill_listbox ();
795 add_widget (movelist_dlg, l_movelist);
796 /* add listbox to the dialogs */
800 * Destroy the list dialog.
801 * Don't confuse with done_hotlist() for the list in memory.
803 static void
804 hotlist_done (void)
806 destroy_dlg (hotlist_dlg);
807 l_hotlist = NULL;
808 if (0)
809 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
810 repaint_screen ();
813 static inline char *
814 find_group_section (struct hotlist *grp)
816 return g_strconcat (grp->directory, ".Group", (char *) NULL);
819 static struct hotlist *
820 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
822 struct hotlist *new;
823 struct hotlist *current = NULL;
826 * Hotlist is neither loaded nor loading.
827 * Must be called by "Ctrl-x a" before using hotlist.
829 if (!current_group)
830 load_hotlist ();
832 listbox_get_current (l_hotlist, NULL, (void **) &current);
834 /* Make sure `..' stays at the top of the list. */
835 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
836 pos = LISTBOX_APPEND_AFTER;
838 new = new_hotlist ();
840 new->type = type;
841 new->label = label;
842 new->directory = directory;
843 new->up = current_group;
845 if (type == HL_TYPE_GROUP)
847 current_group = new;
848 add_dotdot_to_list ();
849 current_group = new->up;
852 if (!current_group->head)
854 /* first element in group */
855 current_group->head = new;
857 else if (pos == LISTBOX_APPEND_AFTER)
859 new->next = current->next;
860 current->next = new;
862 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
864 /* should be inserted before first item */
865 new->next = current;
866 current_group->head = new;
868 else if (pos == LISTBOX_APPEND_BEFORE)
870 struct hotlist *p = current_group->head;
872 while (p->next != current)
873 p = p->next;
875 new->next = current;
876 p->next = new;
878 else
879 { /* append at the end */
880 struct hotlist *p = current_group->head;
882 while (p->next)
883 p = p->next;
885 p->next = new;
888 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
890 if (type == HL_TYPE_GROUP)
892 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
894 listbox_add_item (l_hotlist, pos, 0, lbl, new);
895 g_free (lbl);
897 else
898 listbox_add_item (l_hotlist, pos, 0, new->label, new);
899 listbox_select_entry (l_hotlist, l_hotlist->pos);
901 return new;
905 #ifdef ENABLE_NLS
907 * Support routine for add_new_entry_input()/add_new_group_input()
908 * Change positions of buttons (first three widgets).
910 * This is just a quick hack. Accurate procedure must take care of
911 * internationalized label lengths and total buttonbar length...assume
912 * 64 is longer anyway.
914 static void
915 add_widgets_i18n (QuickWidget * qw, int len)
917 int i, l[3], space, cur_x;
919 for (i = 0; i < 3; i++)
921 qw[i].u.button.text = _(qw[i].u.button.text);
922 l[i] = str_term_width1 (qw[i].u.button.text) + 3;
924 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
926 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
928 qw[i].relative_x = cur_x;
929 qw[i].x_divisions = len;
932 #endif /* ENABLE_NLS */
934 static int
935 add_new_entry_input (const char *header, const char *text1, const char *text2,
936 const char *help, char **r1, char **r2)
938 #define RELATIVE_Y_BUTTONS 4
939 #define RELATIVE_Y_LABEL_PTH 3
940 #define RELATIVE_Y_INPUT_PTH 4
942 QuickWidget quick_widgets[] = {
943 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
944 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
945 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
946 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
947 /* 4 */ QUICK_LABEL (4, 80, 3, 0, text2),
948 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
949 /* 6 */ QUICK_LABEL (4, 80, 2, 0, text1),
950 QUICK_END
953 int len;
954 int i;
955 int lines1, lines2;
956 int cols1, cols2;
958 #ifdef ENABLE_NLS
959 static gboolean i18n_flag = FALSE;
960 #endif /* ENABLE_NLS */
962 msglen (text1, &lines1, &cols1);
963 msglen (text2, &lines2, &cols2);
964 len = max (str_term_width1 (header), cols1);
965 len = max (max (len, cols2) + 4, 64);
967 #ifdef ENABLE_NLS
968 if (!i18n_flag)
970 add_widgets_i18n (quick_widgets, len);
971 i18n_flag = TRUE;
973 #endif /* ENABLE_NLS */
976 QuickDialog Quick_input = {
977 len, lines1 + lines2 + 7, -1, -1, header,
978 help, quick_widgets, NULL, FALSE
981 for (i = 0; i < 7; i++)
982 quick_widgets[i].y_divisions = Quick_input.ylen;
984 quick_widgets[0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
985 quick_widgets[1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
986 quick_widgets[2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
987 quick_widgets[3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
988 quick_widgets[4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
990 i = quick_dialog (&Quick_input);
993 return (i != B_CANCEL) ? i : 0;
995 #undef RELATIVE_Y_BUTTONS
996 #undef RELATIVE_Y_LABEL_PTH
997 #undef RELATIVE_Y_INPUT_PTH
1000 static void
1001 add_new_entry_cmd (void)
1003 char *title, *url, *to_free;
1004 int ret;
1006 /* Take current directory as default value for input fields */
1007 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
1009 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1010 _("Directory path:"), "[Hotlist]", &title, &url);
1011 g_free (to_free);
1013 if (!ret)
1014 return;
1015 if (!title || !*title || !url || !*url)
1017 g_free (title);
1018 g_free (url);
1019 return;
1022 if (ret == B_ENTER || ret == B_APPEND)
1023 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1024 else
1025 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1027 hotlist_state.modified = 1;
1030 static int
1031 add_new_group_input (const char *header, const char *label, char **result)
1033 QuickWidget quick_widgets[] = {
1034 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
1035 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
1036 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
1037 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result),
1038 /* 4 */ QUICK_LABEL (4, 80, 2, 0, label),
1039 QUICK_END
1042 int len;
1043 int i;
1044 int lines, cols;
1045 int ret;
1047 #ifdef ENABLE_NLS
1048 static gboolean i18n_flag = FALSE;
1049 #endif /* ENABLE_NLS */
1051 msglen (label, &lines, &cols);
1052 len = max (max (str_term_width1 (header), cols) + 4, 64);
1054 #ifdef ENABLE_NLS
1055 if (!i18n_flag)
1057 add_widgets_i18n (quick_widgets, len);
1058 i18n_flag = TRUE;
1060 #endif /* ENABLE_NLS */
1063 QuickDialog Quick_input = {
1064 len, lines + 6, -1, -1, header,
1065 "[Hotlist]", quick_widgets, NULL, FALSE
1068 int relative_y[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1069 quick_widgets variable above */
1071 for (i = 0; i < 5; i++)
1072 quick_widgets[i].y_divisions = Quick_input.ylen;
1074 for (i = 0; i < 4; i++)
1075 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1077 ret = quick_dialog (&Quick_input);
1080 return (ret != B_CANCEL) ? ret : 0;
1083 static void
1084 add_new_group_cmd (void)
1086 char *label;
1087 int ret;
1089 ret = add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label);
1090 if (!ret || !label || !*label)
1091 return;
1093 if (ret == B_ENTER || ret == B_APPEND)
1094 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1095 else
1096 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1098 hotlist_state.modified = 1;
1101 void
1102 add2hotlist_cmd (void)
1104 char *lc_prompt, *label;
1105 const char *cp = _("Label for \"%s\":");
1106 int l = str_term_width1 (cp);
1107 char *label_string = g_strdup (current_panel->cwd);
1109 strip_password (label_string, 1);
1111 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS - 2 * UX - (l + 8)));
1112 label = input_dialog (_("Add to hotlist"), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1113 g_free (lc_prompt);
1115 if (!label || !*label)
1117 g_free (label_string);
1118 g_free (label);
1119 return;
1121 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1122 hotlist_state.modified = 1;
1125 static void
1126 remove_group (struct hotlist *grp)
1128 struct hotlist *current = grp->head;
1130 while (current)
1132 struct hotlist *next = current->next;
1134 if (current->type == HL_TYPE_GROUP)
1135 remove_group (current);
1137 g_free (current->label);
1138 g_free (current->directory);
1139 g_free (current);
1141 current = next;
1146 static void
1147 remove_from_hotlist (struct hotlist *entry)
1149 if (entry == NULL)
1150 return;
1152 if (entry->type == HL_TYPE_DOTDOT)
1153 return;
1155 if (confirm_directory_hotlist_delete)
1157 char *title;
1158 int result;
1160 title = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1162 if (safe_delete)
1163 query_set_sel (1);
1164 result = query_dialog (title,
1165 _("Are you sure you want to remove this entry?"),
1166 D_ERROR, 2, _("&Yes"), _("&No"));
1168 g_free (title);
1170 if (result != 0)
1171 return;
1174 if (entry->type == HL_TYPE_GROUP)
1176 if (entry->head)
1178 char *header;
1179 int result;
1181 header = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1182 result = query_dialog (header, _("Group not empty.\nRemove it?"),
1183 D_ERROR, 2, _("&Yes"), _("&No"));
1184 g_free (header);
1186 if (result != 0)
1187 return;
1190 remove_group (entry);
1193 unlink_entry (entry);
1195 g_free (entry->label);
1196 g_free (entry->directory);
1197 g_free (entry);
1198 /* now remove list entry from screen */
1199 listbox_remove_current (l_hotlist);
1200 hotlist_state.modified = 1;
1203 char *
1204 hotlist_cmd (int vfs_or_hotlist)
1206 char *target = NULL;
1208 hotlist_state.type = vfs_or_hotlist;
1209 load_hotlist ();
1211 init_hotlist (vfs_or_hotlist);
1213 /* display file info */
1214 tty_setcolor (SELECTED_COLOR);
1216 hotlist_state.running = 1;
1217 run_dlg (hotlist_dlg);
1218 hotlist_state.running = 0;
1219 save_hotlist ();
1221 switch (hotlist_dlg->ret_value)
1223 default:
1224 case B_CANCEL:
1225 break;
1227 case B_ENTER:
1229 char *text = NULL;
1230 struct hotlist *hlp = NULL;
1232 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1233 target = g_strdup (hlp != NULL ? hlp->directory : text);
1234 break;
1236 } /* switch */
1238 hotlist_done ();
1239 return target;
1242 static void
1243 load_group (struct hotlist *grp)
1245 gchar **profile_keys, **keys;
1246 gsize len;
1247 char *group_section;
1248 struct hotlist *current = 0;
1250 group_section = find_group_section (grp);
1252 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1254 current_group = grp;
1256 while (*profile_keys)
1258 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1259 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1260 profile_keys++;
1262 g_free (group_section);
1263 g_strfreev (keys);
1265 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1267 while (*profile_keys)
1269 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1270 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1271 profile_keys++;
1273 g_strfreev (keys);
1275 for (current = grp->head; current; current = current->next)
1276 load_group (current);
1279 #define TKN_GROUP 0
1280 #define TKN_ENTRY 1
1281 #define TKN_STRING 2
1282 #define TKN_URL 3
1283 #define TKN_ENDGROUP 4
1284 #define TKN_COMMENT 5
1285 #define TKN_EOL 125
1286 #define TKN_EOF 126
1287 #define TKN_UNKNOWN 127
1289 static GString *tkn_buf = NULL;
1291 static char *hotlist_file_name;
1292 static FILE *hotlist_file;
1293 static time_t hotlist_file_mtime;
1295 static int
1296 hot_skip_blanks (void)
1298 int c;
1300 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1302 return c;
1306 static int
1307 hot_next_token (void)
1309 int c, ret = 0;
1310 size_t l;
1313 if (tkn_buf == NULL)
1314 tkn_buf = g_string_new ("");
1315 g_string_set_size (tkn_buf, 0);
1317 again:
1318 c = hot_skip_blanks ();
1319 switch (c)
1321 case EOF:
1322 ret = TKN_EOF;
1323 break;
1324 case '\n':
1325 ret = TKN_EOL;
1326 break;
1327 case '#':
1328 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1330 g_string_append_c (tkn_buf, c);
1332 ret = TKN_COMMENT;
1333 break;
1334 case '"':
1335 while ((c = getc (hotlist_file)) != EOF && c != '"')
1337 if (c == '\\')
1339 c = getc (hotlist_file);
1340 if (c == EOF)
1342 g_string_free (tkn_buf, TRUE);
1343 return TKN_EOF;
1346 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1348 if (c == EOF)
1349 ret = TKN_EOF;
1350 else
1351 ret = TKN_STRING;
1352 break;
1353 case '\\':
1354 c = getc (hotlist_file);
1355 if (c == EOF)
1357 g_string_free (tkn_buf, TRUE);
1358 return TKN_EOF;
1360 if (c == '\n')
1361 goto again;
1363 /* fall through; it is taken as normal character */
1365 default:
1368 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1370 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1371 if (c != EOF)
1372 ungetc (c, hotlist_file);
1373 l = tkn_buf->len;
1374 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1375 ret = TKN_GROUP;
1376 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1377 ret = TKN_ENTRY;
1378 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1379 ret = TKN_ENDGROUP;
1380 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1381 ret = TKN_URL;
1382 else
1383 ret = TKN_UNKNOWN;
1384 break;
1386 return ret;
1389 #define SKIP_TO_EOL \
1391 int _tkn; \
1392 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1395 #define CHECK_TOKEN(_TKN_) \
1396 tkn = hot_next_token (); \
1397 if (tkn != _TKN_) \
1399 hotlist_state.readonly = 1; \
1400 hotlist_state.file_error = 1; \
1401 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1402 tkn = hot_next_token (); \
1403 break; \
1406 static void
1407 hot_load_group (struct hotlist *grp)
1409 int tkn;
1410 struct hotlist *new_grp;
1411 char *label, *url;
1413 current_group = grp;
1415 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1416 switch (tkn)
1418 case TKN_GROUP:
1419 CHECK_TOKEN (TKN_STRING);
1420 new_grp =
1421 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1422 SKIP_TO_EOL;
1423 hot_load_group (new_grp);
1424 current_group = grp;
1425 break;
1426 case TKN_ENTRY:
1427 CHECK_TOKEN (TKN_STRING);
1428 label = g_strdup (tkn_buf->str);
1429 CHECK_TOKEN (TKN_URL);
1430 CHECK_TOKEN (TKN_STRING);
1431 url = g_strdup (tkn_buf->str);
1432 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1433 SKIP_TO_EOL;
1434 break;
1435 case TKN_COMMENT:
1436 label = g_strdup (tkn_buf->str);
1437 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1438 break;
1439 case TKN_EOF:
1440 hotlist_state.readonly = 1;
1441 hotlist_state.file_error = 1;
1442 return;
1443 break;
1444 case TKN_EOL:
1445 /* skip empty lines */
1446 break;
1447 default:
1448 hotlist_state.readonly = 1;
1449 hotlist_state.file_error = 1;
1450 SKIP_TO_EOL;
1451 break;
1453 SKIP_TO_EOL;
1456 static void
1457 hot_load_file (struct hotlist *grp)
1459 int tkn;
1460 struct hotlist *new_grp;
1461 char *label, *url;
1463 current_group = grp;
1465 while ((tkn = hot_next_token ()) != TKN_EOF)
1466 switch (tkn)
1468 case TKN_GROUP:
1469 CHECK_TOKEN (TKN_STRING);
1470 new_grp =
1471 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1472 SKIP_TO_EOL;
1473 hot_load_group (new_grp);
1474 current_group = grp;
1475 break;
1476 case TKN_ENTRY:
1477 CHECK_TOKEN (TKN_STRING);
1478 label = g_strdup (tkn_buf->str);
1479 CHECK_TOKEN (TKN_URL);
1480 CHECK_TOKEN (TKN_STRING);
1481 url = g_strdup (tkn_buf->str);
1482 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1483 SKIP_TO_EOL;
1484 break;
1485 case TKN_COMMENT:
1486 label = g_strdup (tkn_buf->str);
1487 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1488 break;
1489 case TKN_EOL:
1490 /* skip empty lines */
1491 break;
1492 default:
1493 hotlist_state.readonly = 1;
1494 hotlist_state.file_error = 1;
1495 SKIP_TO_EOL;
1496 break;
1500 static void
1501 clean_up_hotlist_groups (const char *section)
1503 char *grp_section;
1504 gchar **profile_keys, **keys;
1505 gsize len;
1507 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1508 if (mc_config_has_group (mc_main_config, section))
1509 mc_config_del_group (mc_main_config, section);
1511 if (mc_config_has_group (mc_main_config, grp_section))
1513 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1515 while (*profile_keys)
1517 clean_up_hotlist_groups (*profile_keys);
1518 profile_keys++;
1520 g_strfreev (keys);
1521 mc_config_del_group (mc_main_config, grp_section);
1523 g_free (grp_section);
1528 static void
1529 load_hotlist (void)
1531 int remove_old_list = 0;
1532 struct stat stat_buf;
1534 if (hotlist_state.loaded)
1536 stat (hotlist_file_name, &stat_buf);
1537 if (hotlist_file_mtime < stat_buf.st_mtime)
1538 done_hotlist ();
1539 else
1540 return;
1543 if (!hotlist_file_name)
1544 hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL);
1546 hotlist = new_hotlist ();
1547 hotlist->type = HL_TYPE_GROUP;
1548 hotlist->label = g_strdup (_("Top level group"));
1549 hotlist->up = hotlist;
1551 * compatibility :-(
1553 hotlist->directory = g_strdup ("Hotlist");
1555 hotlist_file = fopen (hotlist_file_name, "r");
1556 if (hotlist_file == NULL)
1558 int result;
1560 load_group (hotlist);
1561 hotlist_state.loaded = 1;
1563 * just to be sure we got copy
1565 hotlist_state.modified = 1;
1566 result = save_hotlist ();
1567 hotlist_state.modified = 0;
1568 if (result)
1569 remove_old_list = 1;
1570 else
1571 message (D_ERROR, _("Hotlist Load"),
1572 _("MC was unable to write ~/%s file,\nyour old hotlist entries were not deleted"),
1573 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1575 else
1577 hot_load_file (hotlist);
1578 fclose (hotlist_file);
1579 hotlist_state.loaded = 1;
1582 if (remove_old_list)
1584 GError *error = NULL;
1585 clean_up_hotlist_groups ("Hotlist");
1586 if (!mc_config_save_file (mc_main_config, &error))
1587 setup_save_config_show_error (mc_main_config->ini_path, &error);
1590 stat (hotlist_file_name, &stat_buf);
1591 hotlist_file_mtime = stat_buf.st_mtime;
1592 current_group = hotlist;
1596 static int list_level = 0;
1598 static void
1599 hot_save_group (struct hotlist *grp)
1601 struct hotlist *current = grp->head;
1602 int i;
1603 char *s;
1605 #define INDENT(n) \
1606 do { \
1607 for (i = 0; i < n; i++) \
1608 putc (' ', hotlist_file); \
1609 } while (0)
1611 for (; current; current = current->next)
1612 switch (current->type)
1614 case HL_TYPE_GROUP:
1615 INDENT (list_level);
1616 fputs ("GROUP \"", hotlist_file);
1617 for (s = current->label; *s; s++)
1619 if (*s == '"' || *s == '\\')
1620 putc ('\\', hotlist_file);
1621 putc (*s, hotlist_file);
1623 fputs ("\"\n", hotlist_file);
1624 list_level += 2;
1625 hot_save_group (current);
1626 list_level -= 2;
1627 INDENT (list_level);
1628 fputs ("ENDGROUP\n", hotlist_file);
1629 break;
1630 case HL_TYPE_ENTRY:
1631 INDENT (list_level);
1632 fputs ("ENTRY \"", hotlist_file);
1633 for (s = current->label; *s; s++)
1635 if (*s == '"' || *s == '\\')
1636 putc ('\\', hotlist_file);
1637 putc (*s, hotlist_file);
1639 fputs ("\" URL \"", hotlist_file);
1640 for (s = current->directory; *s; s++)
1642 if (*s == '"' || *s == '\\')
1643 putc ('\\', hotlist_file);
1644 putc (*s, hotlist_file);
1646 fputs ("\"\n", hotlist_file);
1647 break;
1648 case HL_TYPE_COMMENT:
1649 fprintf (hotlist_file, "#%s\n", current->label);
1650 break;
1651 case HL_TYPE_DOTDOT:
1652 /* do nothing */
1653 break;
1658 save_hotlist (void)
1660 int saved = 0;
1661 struct stat stat_buf;
1663 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name)
1665 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1667 hotlist_file = fopen (hotlist_file_name, "w");
1668 if (hotlist_file != NULL)
1670 hot_save_group (hotlist);
1671 fclose (hotlist_file);
1672 stat (hotlist_file_name, &stat_buf);
1673 hotlist_file_mtime = stat_buf.st_mtime;
1674 saved = 1;
1675 hotlist_state.modified = 0;
1677 else
1678 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1681 return saved;
1685 * Unload list from memory.
1686 * Don't confuse with hotlist_done() for GUI.
1688 void
1689 done_hotlist (void)
1691 if (hotlist)
1693 remove_group (hotlist);
1694 g_free (hotlist->label);
1695 g_free (hotlist->directory);
1696 g_free (hotlist);
1697 hotlist = 0;
1700 hotlist_state.loaded = 0;
1702 g_free (hotlist_file_name);
1703 hotlist_file_name = 0;
1704 l_hotlist = 0;
1705 current_group = 0;
1707 if (tkn_buf)
1709 g_string_free (tkn_buf, TRUE);
1710 tkn_buf = NULL;
1714 static void
1715 add_dotdot_to_list (void)
1717 if (current_group != hotlist)
1719 if (hotlist_has_dot_dot != 0)
1720 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);