Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / hotlist.c
blob9dc31b99b150ed2a09b4b82cfa16744fa861c4f1
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"
52 #include "dialog.h"
53 #include "widget.h"
54 #include "setup.h" /* For profile_bname */
55 #include "wtools.h" /* QuickDialog */
56 #include "panel.h" /* current_panel */
57 #include "main.h" /* update_panels() */
58 #include "layout.h" /* repaint_screen() */
59 #include "hotlist.h"
60 #include "command.h" /* cmdline */
61 #include "history.h"
63 #define UX 5
64 #define UY 2
66 #define BX UX
67 #define BY (LINES - 6)
69 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
70 #define LABELS 3
71 #define B_ADD_CURRENT B_USER
72 #define B_REMOVE (B_USER + 1)
73 #define B_NEW_GROUP (B_USER + 2)
74 #define B_NEW_ENTRY (B_USER + 3)
75 #define B_UP_GROUP (B_USER + 4)
76 #define B_INSERT (B_USER + 5)
77 #define B_APPEND (B_USER + 6)
78 #define B_MOVE (B_USER + 7)
80 #ifdef ENABLE_VFS
81 #include "lib/vfs/mc-vfs/gc.h"
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 { \
237 int i; \
239 if ((i = strlen (current->label) + 3) > buflen) { \
240 g_free (buf); \
241 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
243 buf[0] = '\0'; \
244 } while (0)
246 static void
247 fill_listbox (void)
249 struct hotlist *current = current_group->head;
250 GString *buff = g_string_new ("");
252 while (current)
254 switch (current->type)
256 case HL_TYPE_GROUP:
258 /* buff clean up */
259 g_string_truncate (buff, 0);
260 g_string_append (buff, "->");
261 g_string_append (buff, current->label);
262 if (hotlist_state.moving)
263 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
264 else
265 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
267 break;
268 case HL_TYPE_DOTDOT:
269 case HL_TYPE_ENTRY:
270 if (hotlist_state.moving)
271 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, current->label, current);
272 else
273 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, current->label, current);
274 break;
275 default:
276 break;
278 current = current->next;
280 g_string_free (buff, TRUE);
283 static void
284 unlink_entry (struct hotlist *entry)
286 struct hotlist *current = current_group->head;
288 if (current == entry)
289 current_group->head = entry->next;
290 else
292 while (current && current->next != entry)
293 current = current->next;
294 if (current)
295 current->next = entry->next;
297 entry->next = entry->up = 0;
300 #ifdef ENABLE_VFS
301 static void
302 add_name_to_list (const char *path)
304 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, path, 0);
306 #endif /* !ENABLE_VFS */
308 static int
309 hotlist_button_callback (int action)
311 switch (action)
313 case B_MOVE:
315 struct hotlist *saved = current_group;
316 struct hotlist *item = NULL;
317 struct hotlist *moveto_item = NULL;
318 struct hotlist *moveto_group = NULL;
319 int ret;
321 if (l_hotlist->count == 0)
322 return MSG_NOT_HANDLED; /* empty group - nothing to do */
324 listbox_get_current (l_hotlist, NULL, (void **) &item);
325 hotlist_state.moving = 1;
326 init_movelist (LIST_MOVELIST, item);
328 ret = run_dlg (movelist_dlg);
330 hotlist_state.moving = 0;
331 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
332 moveto_group = current_group;
333 destroy_dlg (movelist_dlg);
334 current_group = saved;
335 if (ret == B_CANCEL)
336 return MSG_NOT_HANDLED;
337 if (moveto_item == item)
338 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
339 it hardly changes anything ;) */
340 unlink_entry (item);
341 listbox_remove_current (l_hotlist);
342 item->up = moveto_group;
343 if (!moveto_group->head)
344 moveto_group->head = item;
345 else if (!moveto_item)
346 { /* we have group with just comments */
347 struct hotlist *p = moveto_group->head;
349 /* skip comments */
350 while (p->next)
351 p = p->next;
352 p->next = item;
354 else if (ret == B_ENTER || ret == B_APPEND)
355 if (!moveto_item->next)
356 moveto_item->next = item;
357 else
359 item->next = moveto_item->next;
360 moveto_item->next = item;
362 else if (moveto_group->head == moveto_item)
364 moveto_group->head = item;
365 item->next = moveto_item;
367 else
369 struct hotlist *p = moveto_group->head;
371 while (p->next != moveto_item)
372 p = p->next;
373 item->next = p->next;
374 p->next = item;
376 listbox_remove_list (l_hotlist);
377 fill_listbox ();
378 repaint_screen ();
379 hotlist_state.modified = 1;
380 return MSG_NOT_HANDLED;
382 case B_REMOVE:
384 struct hotlist *entry = NULL;
385 listbox_get_current (l_hotlist, NULL, (void **) &entry);
386 remove_from_hotlist (entry);
388 return MSG_NOT_HANDLED;
390 case B_NEW_GROUP:
391 add_new_group_cmd ();
392 return MSG_NOT_HANDLED;
394 case B_ADD_CURRENT:
395 add2hotlist_cmd ();
396 return MSG_NOT_HANDLED;
398 case B_NEW_ENTRY:
399 add_new_entry_cmd ();
400 return MSG_NOT_HANDLED;
402 case B_ENTER:
404 WListbox *list;
405 void *data;
406 struct hotlist *hlp;
408 list = hotlist_state.moving ? l_movelist : l_hotlist;
409 listbox_get_current (list, NULL, &data);
411 if (data == NULL)
412 return MSG_HANDLED;
414 hlp = (struct hotlist *) data;
416 if (hlp->type == HL_TYPE_ENTRY)
417 return MSG_HANDLED;
418 if (hlp->type != HL_TYPE_DOTDOT)
420 listbox_remove_list (list);
421 current_group = hlp;
422 fill_listbox ();
423 return MSG_NOT_HANDLED;
425 /* Fall through - go up */
427 /* Fall through if list empty - just go up */
429 case B_UP_GROUP:
431 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
432 listbox_remove_list (list);
433 current_group = current_group->up;
434 fill_listbox ();
435 return MSG_NOT_HANDLED;
438 #ifdef ENABLE_VFS
439 case B_FREE_ALL_VFS:
440 vfs_expire (1);
441 /* fall through */
443 case B_REFRESH_VFS:
444 listbox_remove_list (l_hotlist);
445 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
446 vfs_fill_names (add_name_to_list);
447 return MSG_NOT_HANDLED;
448 #endif /* ENABLE_VFS */
450 default:
451 return MSG_HANDLED;
455 static inline cb_ret_t
456 hotlist_handle_key (Dlg_head * h, int key)
458 switch (key)
460 case KEY_M_CTRL | '\n':
461 goto l1;
463 case '\n':
464 case KEY_ENTER:
465 case KEY_RIGHT:
466 if (hotlist_button_callback (B_ENTER))
468 h->ret_value = B_ENTER;
469 dlg_stop (h);
471 return MSG_HANDLED;
473 case KEY_LEFT:
474 if (hotlist_state.type != LIST_VFSLIST)
475 return !hotlist_button_callback (B_UP_GROUP);
476 else
477 return MSG_NOT_HANDLED;
479 case KEY_DC:
480 if (hotlist_state.moving)
481 return MSG_NOT_HANDLED;
482 else
484 hotlist_button_callback (B_REMOVE);
485 return MSG_HANDLED;
489 case ALT ('\n'):
490 case ALT ('\r'):
491 if (!hotlist_state.moving)
493 void *ldata = NULL;
495 listbox_get_current (l_hotlist, NULL, &ldata);
497 if (ldata != NULL)
499 struct hotlist *hlp = (struct hotlist *) ldata;
501 if (hlp->type == HL_TYPE_ENTRY)
503 char *tmp;
505 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
506 stuff (cmdline, tmp, 0);
507 g_free (tmp);
508 h->ret_value = B_CANCEL;
509 dlg_stop (h);
513 return MSG_HANDLED; /* ignore key */
515 default:
516 return MSG_NOT_HANDLED;
520 static cb_ret_t
521 hotlist_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
523 switch (msg)
525 case DLG_DRAW:
526 hotlist_refresh (h);
527 return MSG_HANDLED;
529 case DLG_UNHANDLED_KEY:
530 return hotlist_handle_key (h, parm);
532 case DLG_POST_KEY:
533 if (hotlist_state.moving)
534 dlg_select_widget (l_movelist);
535 else
536 dlg_select_widget (l_hotlist);
537 /* always stay on hotlist */
538 /* fall through */
540 case DLG_INIT:
541 tty_setcolor (MENU_ENTRY_COLOR);
542 update_path_name ();
543 return MSG_HANDLED;
545 case DLG_RESIZE:
546 /* simply call dlg_set_size() with new size */
547 dlg_set_size (h, LINES - 2, COLS - 6);
548 return MSG_HANDLED;
550 default:
551 return default_dlg_callback (h, sender, msg, parm, data);
555 static int
556 l_call (WListbox * list)
558 Dlg_head *dlg = list->widget.owner;
560 if (list->count != 0)
562 void *data = NULL;
564 listbox_get_current (list, NULL, &data);
566 if (data != NULL)
568 struct hotlist *hlp = (struct hotlist *) data;
569 if (hlp->type == HL_TYPE_ENTRY)
571 dlg->ret_value = B_ENTER;
572 dlg_stop (dlg);
573 return LISTBOX_DONE;
575 else
577 hotlist_button_callback (B_ENTER);
578 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
579 return LISTBOX_CONT;
582 else
584 dlg->ret_value = B_ENTER;
585 dlg_stop (dlg);
586 return LISTBOX_DONE;
590 hotlist_button_callback (B_UP_GROUP);
591 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
592 return LISTBOX_CONT;
596 * Expands all button names (once) and recalculates button positions.
597 * returns number of columns in the dialog box, which is 10 chars longer
598 * then buttonbar.
600 * If common width of the window (i.e. in xterm) is less than returned
601 * width - sorry :) (anyway this did not handled in previous version too)
603 static int
604 init_i18n_stuff (int list_type, int cols)
606 register int i;
607 static const char *cancel_but = N_("&Cancel");
609 #ifdef ENABLE_NLS
610 static int hotlist_i18n_flag = 0;
612 if (!hotlist_i18n_flag)
614 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
615 while (i--)
616 hotlist_but[i].text = _(hotlist_but[i].text);
618 cancel_but = _(cancel_but);
619 hotlist_i18n_flag = 1;
621 #endif /* ENABLE_NLS */
623 /* Dynamic resizing of buttonbars */
625 int len[2], count[2]; /* at most two lines of buttons */
626 int cur_x[2], row;
628 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
629 len[0] = len[1] = count[0] = count[1] = 0;
631 /* Count len of buttonbars, assuming 2 extra space between buttons */
632 while (i--)
634 if (!(hotlist_but[i].type & list_type))
635 continue;
637 row = hotlist_but[i].y;
638 ++count[row];
639 len[row] += str_term_width1 (hotlist_but[i].text) + 5;
640 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
641 len[row] += 2;
643 len[0] -= 2;
644 len[1] -= 2;
646 cols = max (cols, max (len[0], len[1]));
648 /* arrange buttons */
650 cur_x[0] = cur_x[1] = 0;
651 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
652 while (i--)
654 if (!(hotlist_but[i].type & list_type))
655 continue;
657 row = hotlist_but[i].y;
659 if (hotlist_but[i].x != 0)
661 /* not first int the row */
662 if (!strcmp (hotlist_but[i].text, cancel_but))
663 hotlist_but[i].x = cols - str_term_width1 (hotlist_but[i].text) - 13;
664 else
665 hotlist_but[i].x = cur_x[row];
668 cur_x[row] += str_term_width1 (hotlist_but[i].text) + 2
669 + (hotlist_but[i].flags == DEFPUSH_BUTTON ? 5 : 3);
673 return cols;
676 static void
677 init_hotlist (int list_type)
679 size_t i;
680 const char *title, *help_node;
681 int hotlist_cols;
683 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
685 do_refresh ();
687 hotlist_state.expanded =
688 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
690 if (list_type == LIST_VFSLIST)
692 title = _("Active VFS directories");
693 help_node = "[vfshot]"; /* FIXME - no such node */
695 else
697 title = _("Directory hotlist");
698 help_node = "[Hotlist]";
701 hotlist_dlg =
702 create_dlg (TRUE, 0, 0, LINES - 2, hotlist_cols, dialog_colors,
703 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
705 for (i = 0; i < BUTTONS; i++)
707 if (hotlist_but[i].type & list_type)
708 add_widget_autopos (hotlist_dlg,
709 button_new (BY + hotlist_but[i].y,
710 BX + hotlist_but[i].x,
711 hotlist_but[i].ret_cmd,
712 hotlist_but[i].flags,
713 hotlist_but[i].text,
714 hotlist_button_callback), hotlist_but[i].pos_flags);
717 /* We add the labels.
718 * pname will hold entry's pathname;
719 * pname_group will hold name of current group
721 pname = label_new (UY - 11 + LINES, UX + 2, "");
722 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
723 if (!hotlist_state.moving)
725 char label_text[BUF_TINY];
727 g_snprintf (label_text, sizeof (label_text), " %s ", _("Directory path"));
728 add_widget_autopos (hotlist_dlg,
729 label_new (UY - 12 + LINES, UX + 2,
730 label_text), WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
732 /* This one holds the displayed pathname */
733 pname_group = label_new (UY, UX + 2, _("Directory label"));
734 add_widget (hotlist_dlg, pname_group);
736 /* get new listbox */
737 l_hotlist = listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8, FALSE, l_call);
739 /* Fill the hotlist with the active VFS or the hotlist */
740 #ifdef ENABLE_VFS
741 if (list_type == LIST_VFSLIST)
743 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
744 vfs_fill_names (add_name_to_list);
746 else
747 #endif /* !ENABLE_VFS */
748 fill_listbox ();
750 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
751 /* add listbox to the dialogs */
754 static void
755 init_movelist (int list_type, struct hotlist *item)
757 size_t i;
758 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
759 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
761 do_refresh ();
763 movelist_dlg =
764 create_dlg (TRUE, 0, 0, LINES - 6, movelist_cols, dialog_colors,
765 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
766 g_free (hdr);
768 for (i = 0; i < BUTTONS; i++)
770 if (hotlist_but[i].type & list_type)
771 add_widget (movelist_dlg,
772 button_new (BY - 4 + hotlist_but[i].y,
773 BX + hotlist_but[i].x,
774 hotlist_but[i].ret_cmd,
775 hotlist_but[i].flags,
776 hotlist_but[i].text, hotlist_button_callback));
779 /* We add the labels. We are interested in the last one,
780 * that one will hold the path name label
782 movelist_group = label_new (UY, UX + 2, _("Directory label"));
783 add_widget (movelist_dlg, movelist_group);
784 /* get new listbox */
785 l_movelist =
786 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
787 movelist_dlg->cols - 2 * UX - 2, FALSE, l_call);
789 fill_listbox ();
791 add_widget (movelist_dlg, l_movelist);
792 /* add listbox to the dialogs */
796 * Destroy the list dialog.
797 * Don't confuse with done_hotlist() for the list in memory.
799 static void
800 hotlist_done (void)
802 destroy_dlg (hotlist_dlg);
803 l_hotlist = NULL;
804 if (0)
805 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
806 repaint_screen ();
809 static inline char *
810 find_group_section (struct hotlist *grp)
812 return g_strconcat (grp->directory, ".Group", (char *) NULL);
815 static struct hotlist *
816 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
818 struct hotlist *new;
819 struct hotlist *current = NULL;
822 * Hotlist is neither loaded nor loading.
823 * Must be called by "Ctrl-x a" before using hotlist.
825 if (!current_group)
826 load_hotlist ();
828 listbox_get_current (l_hotlist, NULL, (void **) &current);
830 /* Make sure `..' stays at the top of the list. */
831 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
832 pos = LISTBOX_APPEND_AFTER;
834 new = new_hotlist ();
836 new->type = type;
837 new->label = label;
838 new->directory = directory;
839 new->up = current_group;
841 if (type == HL_TYPE_GROUP)
843 current_group = new;
844 add_dotdot_to_list ();
845 current_group = new->up;
848 if (!current_group->head)
850 /* first element in group */
851 current_group->head = new;
853 else if (pos == LISTBOX_APPEND_AFTER)
855 new->next = current->next;
856 current->next = new;
858 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
860 /* should be inserted before first item */
861 new->next = current;
862 current_group->head = new;
864 else if (pos == LISTBOX_APPEND_BEFORE)
866 struct hotlist *p = current_group->head;
868 while (p->next != current)
869 p = p->next;
871 new->next = current;
872 p->next = new;
874 else
875 { /* append at the end */
876 struct hotlist *p = current_group->head;
878 while (p->next)
879 p = p->next;
881 p->next = new;
884 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
886 if (type == HL_TYPE_GROUP)
888 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
890 listbox_add_item (l_hotlist, pos, 0, lbl, new);
891 g_free (lbl);
893 else
894 listbox_add_item (l_hotlist, pos, 0, new->label, new);
895 listbox_select_entry (l_hotlist, l_hotlist->pos);
897 return new;
901 #ifdef ENABLE_NLS
903 * Support routine for add_new_entry_input()/add_new_group_input()
904 * Change positions of buttons (first three widgets).
906 * This is just a quick hack. Accurate procedure must take care of
907 * internationalized label lengths and total buttonbar length...assume
908 * 64 is longer anyway.
910 static void
911 add_widgets_i18n (QuickWidget * qw, int len)
913 int i, l[3], space, cur_x;
915 for (i = 0; i < 3; i++)
917 qw[i].u.button.text = _(qw[i].u.button.text);
918 l[i] = str_term_width1 (qw[i].u.button.text) + 3;
920 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
922 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
924 qw[i].relative_x = cur_x;
925 qw[i].x_divisions = len;
928 #endif /* ENABLE_NLS */
930 static int
931 add_new_entry_input (const char *header, const char *text1, const char *text2,
932 const char *help, char **r1, char **r2)
934 #define RELATIVE_Y_BUTTONS 4
935 #define RELATIVE_Y_LABEL_PTH 3
936 #define RELATIVE_Y_INPUT_PTH 4
938 QuickWidget quick_widgets[] = {
939 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
940 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
941 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
942 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
943 /* 4 */ QUICK_LABEL (4, 80, 3, 0, text2),
944 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
945 /* 6 */ QUICK_LABEL (4, 80, 2, 0, text1),
946 QUICK_END
949 int len;
950 int i;
951 int lines1, lines2;
952 int cols1, cols2;
954 #ifdef ENABLE_NLS
955 static gboolean i18n_flag = FALSE;
956 #endif /* ENABLE_NLS */
958 msglen (text1, &lines1, &cols1);
959 msglen (text2, &lines2, &cols2);
960 len = max (str_term_width1 (header), cols1);
961 len = max (max (len, cols2) + 4, 64);
963 #ifdef ENABLE_NLS
964 if (!i18n_flag)
966 add_widgets_i18n (quick_widgets, len);
967 i18n_flag = TRUE;
969 #endif /* ENABLE_NLS */
972 QuickDialog Quick_input = {
973 len, lines1 + lines2 + 7, -1, -1, header,
974 help, quick_widgets, FALSE
977 for (i = 0; i < 7; i++)
978 quick_widgets[i].y_divisions = Quick_input.ylen;
980 quick_widgets[0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
981 quick_widgets[1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
982 quick_widgets[2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
983 quick_widgets[3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
984 quick_widgets[4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
986 i = quick_dialog (&Quick_input);
989 return (i != B_CANCEL) ? i : 0;
991 #undef RELATIVE_Y_BUTTONS
992 #undef RELATIVE_Y_LABEL_PTH
993 #undef RELATIVE_Y_INPUT_PTH
996 static void
997 add_new_entry_cmd (void)
999 char *title, *url, *to_free;
1000 int ret;
1002 /* Take current directory as default value for input fields */
1003 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
1005 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1006 _("Directory path:"), "[Hotlist]", &title, &url);
1007 g_free (to_free);
1009 if (!ret)
1010 return;
1011 if (!title || !*title || !url || !*url)
1013 g_free (title);
1014 g_free (url);
1015 return;
1018 if (ret == B_ENTER || ret == B_APPEND)
1019 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1020 else
1021 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1023 hotlist_state.modified = 1;
1026 static int
1027 add_new_group_input (const char *header, const char *label, char **result)
1029 QuickWidget quick_widgets[] = {
1030 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
1031 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
1032 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
1033 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result),
1034 /* 4 */ QUICK_LABEL (4, 80, 2, 0, label),
1035 QUICK_END
1038 int len;
1039 int i;
1040 int lines, cols;
1041 int ret;
1043 #ifdef ENABLE_NLS
1044 static gboolean i18n_flag = FALSE;
1045 #endif /* ENABLE_NLS */
1047 msglen (label, &lines, &cols);
1048 len = max (max (str_term_width1 (header), cols) + 4, 64);
1050 #ifdef ENABLE_NLS
1051 if (!i18n_flag)
1053 add_widgets_i18n (quick_widgets, len);
1054 i18n_flag = TRUE;
1056 #endif /* ENABLE_NLS */
1059 QuickDialog Quick_input = {
1060 len, lines + 6, -1, -1, header,
1061 "[Hotlist]", quick_widgets, FALSE
1064 int relative_y[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1065 quick_widgets variable above */
1067 for (i = 0; i < 5; i++)
1068 quick_widgets[i].y_divisions = Quick_input.ylen;
1070 for (i = 0; i < 4; i++)
1071 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1073 ret = quick_dialog (&Quick_input);
1076 return (ret != B_CANCEL) ? ret : 0;
1079 static void
1080 add_new_group_cmd (void)
1082 char *label;
1083 int ret;
1085 ret = add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label);
1086 if (!ret || !label || !*label)
1087 return;
1089 if (ret == B_ENTER || ret == B_APPEND)
1090 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1091 else
1092 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1094 hotlist_state.modified = 1;
1097 void
1098 add2hotlist_cmd (void)
1100 char *lc_prompt, *label;
1101 const char *cp = _("Label for \"%s\":");
1102 int l = str_term_width1 (cp);
1103 char *label_string = g_strdup (current_panel->cwd);
1105 strip_password (label_string, 1);
1107 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS - 2 * UX - (l + 8)));
1108 label = input_dialog (_("Add to hotlist"), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1109 g_free (lc_prompt);
1111 if (!label || !*label)
1113 g_free (label_string);
1114 g_free (label);
1115 return;
1117 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1118 hotlist_state.modified = 1;
1121 static void
1122 remove_group (struct hotlist *grp)
1124 struct hotlist *current = grp->head;
1126 while (current)
1128 struct hotlist *next = current->next;
1130 if (current->type == HL_TYPE_GROUP)
1131 remove_group (current);
1133 g_free (current->label);
1134 g_free (current->directory);
1135 g_free (current);
1137 current = next;
1142 static void
1143 remove_from_hotlist (struct hotlist *entry)
1145 if (entry == NULL)
1146 return;
1148 if (entry->type == HL_TYPE_DOTDOT)
1149 return;
1151 if (confirm_directory_hotlist_delete)
1153 char *title;
1154 int result;
1156 title = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1158 if (safe_delete)
1159 query_set_sel (1);
1160 result = query_dialog (title,
1161 _("Are you sure you want to remove this entry?"),
1162 D_ERROR, 2, _("&Yes"), _("&No"));
1164 g_free (title);
1166 if (result != 0)
1167 return;
1170 if (entry->type == HL_TYPE_GROUP)
1172 if (entry->head)
1174 char *header;
1175 int result;
1177 header = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1178 result = query_dialog (header, _("Group not empty.\nRemove it?"),
1179 D_ERROR, 2, _("&Yes"), _("&No"));
1180 g_free (header);
1182 if (result != 0)
1183 return;
1186 remove_group (entry);
1189 unlink_entry (entry);
1191 g_free (entry->label);
1192 g_free (entry->directory);
1193 g_free (entry);
1194 /* now remove list entry from screen */
1195 listbox_remove_current (l_hotlist);
1196 hotlist_state.modified = 1;
1199 char *
1200 hotlist_cmd (int vfs_or_hotlist)
1202 char *target = NULL;
1204 hotlist_state.type = vfs_or_hotlist;
1205 load_hotlist ();
1207 init_hotlist (vfs_or_hotlist);
1209 /* display file info */
1210 tty_setcolor (SELECTED_COLOR);
1212 hotlist_state.running = 1;
1213 run_dlg (hotlist_dlg);
1214 hotlist_state.running = 0;
1215 save_hotlist ();
1217 switch (hotlist_dlg->ret_value)
1219 default:
1220 case B_CANCEL:
1221 break;
1223 case B_ENTER:
1225 char *text = NULL;
1226 struct hotlist *hlp = NULL;
1228 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1229 target = g_strdup (hlp != NULL ? hlp->directory : text);
1230 break;
1232 } /* switch */
1234 hotlist_done ();
1235 return target;
1238 static void
1239 load_group (struct hotlist *grp)
1241 gchar **profile_keys, **keys;
1242 gsize len;
1243 char *group_section;
1244 struct hotlist *current = 0;
1246 group_section = find_group_section (grp);
1248 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1250 current_group = grp;
1252 while (*profile_keys)
1254 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1255 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1256 profile_keys++;
1258 g_free (group_section);
1259 g_strfreev (keys);
1261 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1263 while (*profile_keys)
1265 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1266 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1267 profile_keys++;
1269 g_strfreev (keys);
1271 for (current = grp->head; current; current = current->next)
1272 load_group (current);
1275 #define TKN_GROUP 0
1276 #define TKN_ENTRY 1
1277 #define TKN_STRING 2
1278 #define TKN_URL 3
1279 #define TKN_ENDGROUP 4
1280 #define TKN_COMMENT 5
1281 #define TKN_EOL 125
1282 #define TKN_EOF 126
1283 #define TKN_UNKNOWN 127
1285 static GString *tkn_buf = NULL;
1287 static char *hotlist_file_name;
1288 static FILE *hotlist_file;
1289 static time_t hotlist_file_mtime;
1291 static int
1292 hot_skip_blanks (void)
1294 int c;
1296 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1298 return c;
1302 static int
1303 hot_next_token (void)
1305 int c, ret = 0;
1306 size_t l;
1309 if (tkn_buf == NULL)
1310 tkn_buf = g_string_new ("");
1311 g_string_set_size (tkn_buf, 0);
1313 again:
1314 c = hot_skip_blanks ();
1315 switch (c)
1317 case EOF:
1318 ret = TKN_EOF;
1319 break;
1320 case '\n':
1321 ret = TKN_EOL;
1322 break;
1323 case '#':
1324 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1326 g_string_append_c (tkn_buf, c);
1328 ret = TKN_COMMENT;
1329 break;
1330 case '"':
1331 while ((c = getc (hotlist_file)) != EOF && c != '"')
1333 if (c == '\\')
1335 c = getc (hotlist_file);
1336 if (c == EOF)
1338 g_string_free (tkn_buf, TRUE);
1339 return TKN_EOF;
1342 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1344 if (c == EOF)
1345 ret = TKN_EOF;
1346 else
1347 ret = TKN_STRING;
1348 break;
1349 case '\\':
1350 c = getc (hotlist_file);
1351 if (c == EOF)
1353 g_string_free (tkn_buf, TRUE);
1354 return TKN_EOF;
1356 if (c == '\n')
1357 goto again;
1359 /* fall through; it is taken as normal character */
1361 default:
1364 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1366 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1367 if (c != EOF)
1368 ungetc (c, hotlist_file);
1369 l = tkn_buf->len;
1370 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1371 ret = TKN_GROUP;
1372 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1373 ret = TKN_ENTRY;
1374 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1375 ret = TKN_ENDGROUP;
1376 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1377 ret = TKN_URL;
1378 else
1379 ret = TKN_UNKNOWN;
1380 break;
1382 return ret;
1385 #define SKIP_TO_EOL { \
1386 int _tkn; \
1387 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1390 #define CHECK_TOKEN(_TKN_) \
1391 tkn = hot_next_token (); \
1392 if (tkn != _TKN_) { \
1393 hotlist_state.readonly = 1; \
1394 hotlist_state.file_error = 1; \
1395 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1396 tkn = hot_next_token (); \
1397 break; \
1400 static void
1401 hot_load_group (struct hotlist *grp)
1403 int tkn;
1404 struct hotlist *new_grp;
1405 char *label, *url;
1407 current_group = grp;
1409 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1410 switch (tkn)
1412 case TKN_GROUP:
1413 CHECK_TOKEN (TKN_STRING);
1414 new_grp =
1415 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1416 SKIP_TO_EOL;
1417 hot_load_group (new_grp);
1418 current_group = grp;
1419 break;
1420 case TKN_ENTRY:
1421 CHECK_TOKEN (TKN_STRING);
1422 label = g_strdup (tkn_buf->str);
1423 CHECK_TOKEN (TKN_URL);
1424 CHECK_TOKEN (TKN_STRING);
1425 url = g_strdup (tkn_buf->str);
1426 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1427 SKIP_TO_EOL;
1428 break;
1429 case TKN_COMMENT:
1430 label = g_strdup (tkn_buf->str);
1431 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1432 break;
1433 case TKN_EOF:
1434 hotlist_state.readonly = 1;
1435 hotlist_state.file_error = 1;
1436 return;
1437 break;
1438 case TKN_EOL:
1439 /* skip empty lines */
1440 break;
1441 default:
1442 hotlist_state.readonly = 1;
1443 hotlist_state.file_error = 1;
1444 SKIP_TO_EOL;
1445 break;
1447 SKIP_TO_EOL;
1450 static void
1451 hot_load_file (struct hotlist *grp)
1453 int tkn;
1454 struct hotlist *new_grp;
1455 char *label, *url;
1457 current_group = grp;
1459 while ((tkn = hot_next_token ()) != TKN_EOF)
1460 switch (tkn)
1462 case TKN_GROUP:
1463 CHECK_TOKEN (TKN_STRING);
1464 new_grp =
1465 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1466 SKIP_TO_EOL;
1467 hot_load_group (new_grp);
1468 current_group = grp;
1469 break;
1470 case TKN_ENTRY:
1471 CHECK_TOKEN (TKN_STRING);
1472 label = g_strdup (tkn_buf->str);
1473 CHECK_TOKEN (TKN_URL);
1474 CHECK_TOKEN (TKN_STRING);
1475 url = g_strdup (tkn_buf->str);
1476 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1477 SKIP_TO_EOL;
1478 break;
1479 case TKN_COMMENT:
1480 label = g_strdup (tkn_buf->str);
1481 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1482 break;
1483 case TKN_EOL:
1484 /* skip empty lines */
1485 break;
1486 default:
1487 hotlist_state.readonly = 1;
1488 hotlist_state.file_error = 1;
1489 SKIP_TO_EOL;
1490 break;
1494 static void
1495 clean_up_hotlist_groups (const char *section)
1497 char *grp_section;
1498 gchar **profile_keys, **keys;
1499 gsize len;
1501 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1502 if (mc_config_has_group (mc_main_config, section))
1503 mc_config_del_group (mc_main_config, section);
1505 if (mc_config_has_group (mc_main_config, grp_section))
1507 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1509 while (*profile_keys)
1511 clean_up_hotlist_groups (*profile_keys);
1512 profile_keys++;
1514 g_strfreev (keys);
1515 mc_config_del_group (mc_main_config, grp_section);
1517 g_free (grp_section);
1522 static void
1523 load_hotlist (void)
1525 int remove_old_list = 0;
1526 struct stat stat_buf;
1528 if (hotlist_state.loaded)
1530 stat (hotlist_file_name, &stat_buf);
1531 if (hotlist_file_mtime < stat_buf.st_mtime)
1532 done_hotlist ();
1533 else
1534 return;
1537 if (!hotlist_file_name)
1538 hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL);
1540 hotlist = new_hotlist ();
1541 hotlist->type = HL_TYPE_GROUP;
1542 hotlist->label = g_strdup (_("Top level group"));
1543 hotlist->up = hotlist;
1545 * compatibility :-(
1547 hotlist->directory = g_strdup ("Hotlist");
1549 hotlist_file = fopen (hotlist_file_name, "r");
1550 if (hotlist_file == NULL)
1552 int result;
1554 load_group (hotlist);
1555 hotlist_state.loaded = 1;
1557 * just to be sure we got copy
1559 hotlist_state.modified = 1;
1560 result = save_hotlist ();
1561 hotlist_state.modified = 0;
1562 if (result)
1563 remove_old_list = 1;
1564 else
1565 message (D_ERROR, _("Hotlist Load"),
1566 _("MC was unable to write ~/%s file,\nyour old hotlist entries were not deleted"),
1567 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1569 else
1571 hot_load_file (hotlist);
1572 fclose (hotlist_file);
1573 hotlist_state.loaded = 1;
1576 if (remove_old_list)
1578 GError *error = NULL;
1579 clean_up_hotlist_groups ("Hotlist");
1580 if (!mc_config_save_file (mc_main_config, &error))
1581 setup_save_config_show_error (mc_main_config->ini_path, &error);
1584 stat (hotlist_file_name, &stat_buf);
1585 hotlist_file_mtime = stat_buf.st_mtime;
1586 current_group = hotlist;
1590 static int list_level = 0;
1592 static void
1593 hot_save_group (struct hotlist *grp)
1595 struct hotlist *current = grp->head;
1596 int i;
1597 char *s;
1599 #define INDENT(n) \
1600 do { \
1601 for (i = 0; i < n; i++) \
1602 putc (' ', hotlist_file); \
1603 } while (0)
1605 for (; current; current = current->next)
1606 switch (current->type)
1608 case HL_TYPE_GROUP:
1609 INDENT (list_level);
1610 fputs ("GROUP \"", hotlist_file);
1611 for (s = current->label; *s; s++)
1613 if (*s == '"' || *s == '\\')
1614 putc ('\\', hotlist_file);
1615 putc (*s, hotlist_file);
1617 fputs ("\"\n", hotlist_file);
1618 list_level += 2;
1619 hot_save_group (current);
1620 list_level -= 2;
1621 INDENT (list_level);
1622 fputs ("ENDGROUP\n", hotlist_file);
1623 break;
1624 case HL_TYPE_ENTRY:
1625 INDENT (list_level);
1626 fputs ("ENTRY \"", hotlist_file);
1627 for (s = current->label; *s; s++)
1629 if (*s == '"' || *s == '\\')
1630 putc ('\\', hotlist_file);
1631 putc (*s, hotlist_file);
1633 fputs ("\" URL \"", hotlist_file);
1634 for (s = current->directory; *s; s++)
1636 if (*s == '"' || *s == '\\')
1637 putc ('\\', hotlist_file);
1638 putc (*s, hotlist_file);
1640 fputs ("\"\n", hotlist_file);
1641 break;
1642 case HL_TYPE_COMMENT:
1643 fprintf (hotlist_file, "#%s\n", current->label);
1644 break;
1645 case HL_TYPE_DOTDOT:
1646 /* do nothing */
1647 break;
1652 save_hotlist (void)
1654 int saved = 0;
1655 struct stat stat_buf;
1657 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name)
1659 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1661 hotlist_file = fopen (hotlist_file_name, "w");
1662 if (hotlist_file != NULL)
1664 hot_save_group (hotlist);
1665 fclose (hotlist_file);
1666 stat (hotlist_file_name, &stat_buf);
1667 hotlist_file_mtime = stat_buf.st_mtime;
1668 saved = 1;
1669 hotlist_state.modified = 0;
1671 else
1672 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1675 return saved;
1679 * Unload list from memory.
1680 * Don't confuse with hotlist_done() for GUI.
1682 void
1683 done_hotlist (void)
1685 if (hotlist)
1687 remove_group (hotlist);
1688 g_free (hotlist->label);
1689 g_free (hotlist->directory);
1690 g_free (hotlist);
1691 hotlist = 0;
1694 hotlist_state.loaded = 0;
1696 g_free (hotlist_file_name);
1697 hotlist_file_name = 0;
1698 l_hotlist = 0;
1699 current_group = 0;
1701 if (tkn_buf)
1703 g_string_free (tkn_buf, TRUE);
1704 tkn_buf = NULL;
1708 static void
1709 add_dotdot_to_list (void)
1711 if (current_group != hotlist)
1713 if (hotlist_has_dot_dot != 0)
1714 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);