Merge branch '2170_separate_colors'
[pantumic.git] / src / hotlist.c
blob18816114112c5a8e7b8c4b63e30131c8beafe779
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 { \
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 (WButton *button, int action)
311 (void) button;
313 switch (action)
315 case B_MOVE:
317 struct hotlist *saved = current_group;
318 struct hotlist *item = NULL;
319 struct hotlist *moveto_item = NULL;
320 struct hotlist *moveto_group = NULL;
321 int ret;
323 if (l_hotlist->count == 0)
324 return MSG_NOT_HANDLED; /* empty group - nothing to do */
326 listbox_get_current (l_hotlist, NULL, (void **) &item);
327 hotlist_state.moving = 1;
328 init_movelist (LIST_MOVELIST, item);
330 ret = run_dlg (movelist_dlg);
332 hotlist_state.moving = 0;
333 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
334 moveto_group = current_group;
335 destroy_dlg (movelist_dlg);
336 current_group = saved;
337 if (ret == B_CANCEL)
338 return MSG_NOT_HANDLED;
339 if (moveto_item == item)
340 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
341 it hardly changes anything ;) */
342 unlink_entry (item);
343 listbox_remove_current (l_hotlist);
344 item->up = moveto_group;
345 if (!moveto_group->head)
346 moveto_group->head = item;
347 else if (!moveto_item)
348 { /* we have group with just comments */
349 struct hotlist *p = moveto_group->head;
351 /* skip comments */
352 while (p->next)
353 p = p->next;
354 p->next = item;
356 else if (ret == B_ENTER || ret == B_APPEND)
357 if (!moveto_item->next)
358 moveto_item->next = item;
359 else
361 item->next = moveto_item->next;
362 moveto_item->next = item;
364 else if (moveto_group->head == moveto_item)
366 moveto_group->head = item;
367 item->next = moveto_item;
369 else
371 struct hotlist *p = moveto_group->head;
373 while (p->next != moveto_item)
374 p = p->next;
375 item->next = p->next;
376 p->next = item;
378 listbox_remove_list (l_hotlist);
379 fill_listbox ();
380 repaint_screen ();
381 hotlist_state.modified = 1;
382 return MSG_NOT_HANDLED;
384 case B_REMOVE:
386 struct hotlist *entry = NULL;
387 listbox_get_current (l_hotlist, NULL, (void **) &entry);
388 remove_from_hotlist (entry);
390 return MSG_NOT_HANDLED;
392 case B_NEW_GROUP:
393 add_new_group_cmd ();
394 return MSG_NOT_HANDLED;
396 case B_ADD_CURRENT:
397 add2hotlist_cmd ();
398 return MSG_NOT_HANDLED;
400 case B_NEW_ENTRY:
401 add_new_entry_cmd ();
402 return MSG_NOT_HANDLED;
404 case B_ENTER:
406 WListbox *list;
407 void *data;
408 struct hotlist *hlp;
410 list = hotlist_state.moving ? l_movelist : l_hotlist;
411 listbox_get_current (list, NULL, &data);
413 if (data == NULL)
414 return MSG_HANDLED;
416 hlp = (struct hotlist *) data;
418 if (hlp->type == HL_TYPE_ENTRY)
419 return MSG_HANDLED;
420 if (hlp->type != HL_TYPE_DOTDOT)
422 listbox_remove_list (list);
423 current_group = hlp;
424 fill_listbox ();
425 return MSG_NOT_HANDLED;
427 /* Fall through - go up */
429 /* Fall through if list empty - just go up */
431 case B_UP_GROUP:
433 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
434 listbox_remove_list (list);
435 current_group = current_group->up;
436 fill_listbox ();
437 return MSG_NOT_HANDLED;
440 #ifdef ENABLE_VFS
441 case B_FREE_ALL_VFS:
442 vfs_expire (1);
443 /* fall through */
445 case B_REFRESH_VFS:
446 listbox_remove_list (l_hotlist);
447 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
448 vfs_fill_names (add_name_to_list);
449 return MSG_NOT_HANDLED;
450 #endif /* ENABLE_VFS */
452 default:
453 return MSG_HANDLED;
457 static inline cb_ret_t
458 hotlist_handle_key (Dlg_head * h, int key)
460 switch (key)
462 case KEY_M_CTRL | '\n':
463 goto l1;
465 case '\n':
466 case KEY_ENTER:
467 case KEY_RIGHT:
468 if (hotlist_button_callback (NULL, B_ENTER))
470 h->ret_value = B_ENTER;
471 dlg_stop (h);
473 return MSG_HANDLED;
475 case KEY_LEFT:
476 if (hotlist_state.type != LIST_VFSLIST)
477 return !hotlist_button_callback (NULL, B_UP_GROUP);
478 else
479 return MSG_NOT_HANDLED;
481 case KEY_DC:
482 if (hotlist_state.moving)
483 return MSG_NOT_HANDLED;
484 else
486 hotlist_button_callback (NULL, B_REMOVE);
487 return MSG_HANDLED;
491 case ALT ('\n'):
492 case ALT ('\r'):
493 if (!hotlist_state.moving)
495 void *ldata = NULL;
497 listbox_get_current (l_hotlist, NULL, &ldata);
499 if (ldata != NULL)
501 struct hotlist *hlp = (struct hotlist *) ldata;
503 if (hlp->type == HL_TYPE_ENTRY)
505 char *tmp;
507 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
508 stuff (cmdline, tmp, 0);
509 g_free (tmp);
510 h->ret_value = B_CANCEL;
511 dlg_stop (h);
515 return MSG_HANDLED; /* ignore key */
517 default:
518 return MSG_NOT_HANDLED;
522 static cb_ret_t
523 hotlist_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
525 switch (msg)
527 case DLG_DRAW:
528 hotlist_refresh (h);
529 return MSG_HANDLED;
531 case DLG_UNHANDLED_KEY:
532 return hotlist_handle_key (h, parm);
534 case DLG_POST_KEY:
535 if (hotlist_state.moving)
536 dlg_select_widget (l_movelist);
537 else
538 dlg_select_widget (l_hotlist);
539 /* always stay on hotlist */
540 /* fall through */
542 case DLG_INIT:
543 tty_setcolor (MENU_ENTRY_COLOR);
544 update_path_name ();
545 return MSG_HANDLED;
547 case DLG_RESIZE:
548 /* simply call dlg_set_size() with new size */
549 dlg_set_size (h, LINES - 2, COLS - 6);
550 return MSG_HANDLED;
552 default:
553 return default_dlg_callback (h, sender, msg, parm, data);
557 static int
558 l_call (WListbox * list)
560 Dlg_head *dlg = list->widget.owner;
562 if (list->count != 0)
564 void *data = NULL;
566 listbox_get_current (list, NULL, &data);
568 if (data != NULL)
570 struct hotlist *hlp = (struct hotlist *) data;
571 if (hlp->type == HL_TYPE_ENTRY)
573 dlg->ret_value = B_ENTER;
574 dlg_stop (dlg);
575 return LISTBOX_DONE;
577 else
579 hotlist_button_callback (NULL, B_ENTER);
580 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
581 return LISTBOX_CONT;
584 else
586 dlg->ret_value = B_ENTER;
587 dlg_stop (dlg);
588 return LISTBOX_DONE;
592 hotlist_button_callback (NULL, B_UP_GROUP);
593 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
594 return LISTBOX_CONT;
598 * Expands all button names (once) and recalculates button positions.
599 * returns number of columns in the dialog box, which is 10 chars longer
600 * then buttonbar.
602 * If common width of the window (i.e. in xterm) is less than returned
603 * width - sorry :) (anyway this did not handled in previous version too)
605 static int
606 init_i18n_stuff (int list_type, int cols)
608 register int i;
609 static const char *cancel_but = N_("&Cancel");
611 #ifdef ENABLE_NLS
612 static int hotlist_i18n_flag = 0;
614 if (!hotlist_i18n_flag)
616 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
617 while (i--)
618 hotlist_but[i].text = _(hotlist_but[i].text);
620 cancel_but = _(cancel_but);
621 hotlist_i18n_flag = 1;
623 #endif /* ENABLE_NLS */
625 /* Dynamic resizing of buttonbars */
627 int len[2], count[2]; /* at most two lines of buttons */
628 int cur_x[2], row;
630 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
631 len[0] = len[1] = count[0] = count[1] = 0;
633 /* Count len of buttonbars, assuming 2 extra space between buttons */
634 while (i--)
636 if (!(hotlist_but[i].type & list_type))
637 continue;
639 row = hotlist_but[i].y;
640 ++count[row];
641 len[row] += str_term_width1 (hotlist_but[i].text) + 5;
642 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
643 len[row] += 2;
645 len[0] -= 2;
646 len[1] -= 2;
648 cols = max (cols, max (len[0], len[1]));
650 /* arrange buttons */
652 cur_x[0] = cur_x[1] = 0;
653 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
654 while (i--)
656 if (!(hotlist_but[i].type & list_type))
657 continue;
659 row = hotlist_but[i].y;
661 if (hotlist_but[i].x != 0)
663 /* not first int the row */
664 if (!strcmp (hotlist_but[i].text, cancel_but))
665 hotlist_but[i].x = cols - str_term_width1 (hotlist_but[i].text) - 13;
666 else
667 hotlist_but[i].x = cur_x[row];
670 cur_x[row] += str_term_width1 (hotlist_but[i].text) + 2
671 + (hotlist_but[i].flags == DEFPUSH_BUTTON ? 5 : 3);
675 return cols;
678 static void
679 init_hotlist (int list_type)
681 size_t i;
682 const char *title, *help_node;
683 int hotlist_cols;
685 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
687 do_refresh ();
689 hotlist_state.expanded =
690 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
692 if (list_type == LIST_VFSLIST)
694 title = _("Active VFS directories");
695 help_node = "[vfshot]"; /* FIXME - no such node */
697 else
699 title = _("Directory hotlist");
700 help_node = "[Hotlist]";
703 hotlist_dlg =
704 create_dlg (TRUE, 0, 0, LINES - 2, hotlist_cols, dialog_colors,
705 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
707 for (i = 0; i < BUTTONS; i++)
709 if (hotlist_but[i].type & list_type)
710 add_widget_autopos (hotlist_dlg,
711 button_new (BY + hotlist_but[i].y,
712 BX + hotlist_but[i].x,
713 hotlist_but[i].ret_cmd,
714 hotlist_but[i].flags,
715 hotlist_but[i].text,
716 hotlist_button_callback), hotlist_but[i].pos_flags);
719 /* We add the labels.
720 * pname will hold entry's pathname;
721 * pname_group will hold name of current group
723 pname = label_new (UY - 11 + LINES, UX + 2, "");
724 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
725 if (!hotlist_state.moving)
727 char label_text[BUF_TINY];
729 g_snprintf (label_text, sizeof (label_text), " %s ", _("Directory path"));
730 add_widget_autopos (hotlist_dlg,
731 label_new (UY - 12 + LINES, UX + 2,
732 label_text), WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
734 /* This one holds the displayed pathname */
735 pname_group = label_new (UY, UX + 2, _("Directory label"));
736 add_widget (hotlist_dlg, pname_group);
738 /* get new listbox */
739 l_hotlist = listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8, FALSE, l_call);
741 /* Fill the hotlist with the active VFS or the hotlist */
742 #ifdef ENABLE_VFS
743 if (list_type == LIST_VFSLIST)
745 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
746 vfs_fill_names (add_name_to_list);
748 else
749 #endif /* !ENABLE_VFS */
750 fill_listbox ();
752 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
753 /* add listbox to the dialogs */
756 static void
757 init_movelist (int list_type, struct hotlist *item)
759 size_t i;
760 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
761 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
763 do_refresh ();
765 movelist_dlg =
766 create_dlg (TRUE, 0, 0, LINES - 6, movelist_cols, dialog_colors,
767 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
768 g_free (hdr);
770 for (i = 0; i < BUTTONS; i++)
772 if (hotlist_but[i].type & list_type)
773 add_widget (movelist_dlg,
774 button_new (BY - 4 + hotlist_but[i].y,
775 BX + hotlist_but[i].x,
776 hotlist_but[i].ret_cmd,
777 hotlist_but[i].flags,
778 hotlist_but[i].text, hotlist_button_callback));
781 /* We add the labels. We are interested in the last one,
782 * that one will hold the path name label
784 movelist_group = label_new (UY, UX + 2, _("Directory label"));
785 add_widget (movelist_dlg, movelist_group);
786 /* get new listbox */
787 l_movelist =
788 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
789 movelist_dlg->cols - 2 * UX - 2, FALSE, l_call);
791 fill_listbox ();
793 add_widget (movelist_dlg, l_movelist);
794 /* add listbox to the dialogs */
798 * Destroy the list dialog.
799 * Don't confuse with done_hotlist() for the list in memory.
801 static void
802 hotlist_done (void)
804 destroy_dlg (hotlist_dlg);
805 l_hotlist = NULL;
806 if (0)
807 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
808 repaint_screen ();
811 static inline char *
812 find_group_section (struct hotlist *grp)
814 return g_strconcat (grp->directory, ".Group", (char *) NULL);
817 static struct hotlist *
818 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
820 struct hotlist *new;
821 struct hotlist *current = NULL;
824 * Hotlist is neither loaded nor loading.
825 * Must be called by "Ctrl-x a" before using hotlist.
827 if (!current_group)
828 load_hotlist ();
830 listbox_get_current (l_hotlist, NULL, (void **) &current);
832 /* Make sure `..' stays at the top of the list. */
833 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
834 pos = LISTBOX_APPEND_AFTER;
836 new = new_hotlist ();
838 new->type = type;
839 new->label = label;
840 new->directory = directory;
841 new->up = current_group;
843 if (type == HL_TYPE_GROUP)
845 current_group = new;
846 add_dotdot_to_list ();
847 current_group = new->up;
850 if (!current_group->head)
852 /* first element in group */
853 current_group->head = new;
855 else if (pos == LISTBOX_APPEND_AFTER)
857 new->next = current->next;
858 current->next = new;
860 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
862 /* should be inserted before first item */
863 new->next = current;
864 current_group->head = new;
866 else if (pos == LISTBOX_APPEND_BEFORE)
868 struct hotlist *p = current_group->head;
870 while (p->next != current)
871 p = p->next;
873 new->next = current;
874 p->next = new;
876 else
877 { /* append at the end */
878 struct hotlist *p = current_group->head;
880 while (p->next)
881 p = p->next;
883 p->next = new;
886 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
888 if (type == HL_TYPE_GROUP)
890 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
892 listbox_add_item (l_hotlist, pos, 0, lbl, new);
893 g_free (lbl);
895 else
896 listbox_add_item (l_hotlist, pos, 0, new->label, new);
897 listbox_select_entry (l_hotlist, l_hotlist->pos);
899 return new;
903 #ifdef ENABLE_NLS
905 * Support routine for add_new_entry_input()/add_new_group_input()
906 * Change positions of buttons (first three widgets).
908 * This is just a quick hack. Accurate procedure must take care of
909 * internationalized label lengths and total buttonbar length...assume
910 * 64 is longer anyway.
912 static void
913 add_widgets_i18n (QuickWidget * qw, int len)
915 int i, l[3], space, cur_x;
917 for (i = 0; i < 3; i++)
919 qw[i].u.button.text = _(qw[i].u.button.text);
920 l[i] = str_term_width1 (qw[i].u.button.text) + 3;
922 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
924 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
926 qw[i].relative_x = cur_x;
927 qw[i].x_divisions = len;
930 #endif /* ENABLE_NLS */
932 static int
933 add_new_entry_input (const char *header, const char *text1, const char *text2,
934 const char *help, char **r1, char **r2)
936 #define RELATIVE_Y_BUTTONS 4
937 #define RELATIVE_Y_LABEL_PTH 3
938 #define RELATIVE_Y_INPUT_PTH 4
940 QuickWidget quick_widgets[] = {
941 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
942 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
943 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
944 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
945 /* 4 */ QUICK_LABEL (4, 80, 3, 0, text2),
946 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
947 /* 6 */ QUICK_LABEL (4, 80, 2, 0, text1),
948 QUICK_END
951 int len;
952 int i;
953 int lines1, lines2;
954 int cols1, cols2;
956 #ifdef ENABLE_NLS
957 static gboolean i18n_flag = FALSE;
958 #endif /* ENABLE_NLS */
960 msglen (text1, &lines1, &cols1);
961 msglen (text2, &lines2, &cols2);
962 len = max (str_term_width1 (header), cols1);
963 len = max (max (len, cols2) + 4, 64);
965 #ifdef ENABLE_NLS
966 if (!i18n_flag)
968 add_widgets_i18n (quick_widgets, len);
969 i18n_flag = TRUE;
971 #endif /* ENABLE_NLS */
974 QuickDialog Quick_input = {
975 len, lines1 + lines2 + 7, -1, -1, header,
976 help, quick_widgets, NULL, FALSE
979 for (i = 0; i < 7; i++)
980 quick_widgets[i].y_divisions = Quick_input.ylen;
982 quick_widgets[0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
983 quick_widgets[1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
984 quick_widgets[2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
985 quick_widgets[3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
986 quick_widgets[4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
988 i = quick_dialog (&Quick_input);
991 return (i != B_CANCEL) ? i : 0;
993 #undef RELATIVE_Y_BUTTONS
994 #undef RELATIVE_Y_LABEL_PTH
995 #undef RELATIVE_Y_INPUT_PTH
998 static void
999 add_new_entry_cmd (void)
1001 char *title, *url, *to_free;
1002 int ret;
1004 /* Take current directory as default value for input fields */
1005 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
1007 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1008 _("Directory path:"), "[Hotlist]", &title, &url);
1009 g_free (to_free);
1011 if (!ret)
1012 return;
1013 if (!title || !*title || !url || !*url)
1015 g_free (title);
1016 g_free (url);
1017 return;
1020 if (ret == B_ENTER || ret == B_APPEND)
1021 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1022 else
1023 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1025 hotlist_state.modified = 1;
1028 static int
1029 add_new_group_input (const char *header, const char *label, char **result)
1031 QuickWidget quick_widgets[] = {
1032 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
1033 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
1034 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
1035 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result),
1036 /* 4 */ QUICK_LABEL (4, 80, 2, 0, label),
1037 QUICK_END
1040 int len;
1041 int i;
1042 int lines, cols;
1043 int ret;
1045 #ifdef ENABLE_NLS
1046 static gboolean i18n_flag = FALSE;
1047 #endif /* ENABLE_NLS */
1049 msglen (label, &lines, &cols);
1050 len = max (max (str_term_width1 (header), cols) + 4, 64);
1052 #ifdef ENABLE_NLS
1053 if (!i18n_flag)
1055 add_widgets_i18n (quick_widgets, len);
1056 i18n_flag = TRUE;
1058 #endif /* ENABLE_NLS */
1061 QuickDialog Quick_input = {
1062 len, lines + 6, -1, -1, header,
1063 "[Hotlist]", quick_widgets, NULL, FALSE
1066 int relative_y[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1067 quick_widgets variable above */
1069 for (i = 0; i < 5; i++)
1070 quick_widgets[i].y_divisions = Quick_input.ylen;
1072 for (i = 0; i < 4; i++)
1073 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1075 ret = quick_dialog (&Quick_input);
1078 return (ret != B_CANCEL) ? ret : 0;
1081 static void
1082 add_new_group_cmd (void)
1084 char *label;
1085 int ret;
1087 ret = add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label);
1088 if (!ret || !label || !*label)
1089 return;
1091 if (ret == B_ENTER || ret == B_APPEND)
1092 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1093 else
1094 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1096 hotlist_state.modified = 1;
1099 void
1100 add2hotlist_cmd (void)
1102 char *lc_prompt, *label;
1103 const char *cp = _("Label for \"%s\":");
1104 int l = str_term_width1 (cp);
1105 char *label_string = g_strdup (current_panel->cwd);
1107 strip_password (label_string, 1);
1109 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS - 2 * UX - (l + 8)));
1110 label = input_dialog (_("Add to hotlist"), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1111 g_free (lc_prompt);
1113 if (!label || !*label)
1115 g_free (label_string);
1116 g_free (label);
1117 return;
1119 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1120 hotlist_state.modified = 1;
1123 static void
1124 remove_group (struct hotlist *grp)
1126 struct hotlist *current = grp->head;
1128 while (current)
1130 struct hotlist *next = current->next;
1132 if (current->type == HL_TYPE_GROUP)
1133 remove_group (current);
1135 g_free (current->label);
1136 g_free (current->directory);
1137 g_free (current);
1139 current = next;
1144 static void
1145 remove_from_hotlist (struct hotlist *entry)
1147 if (entry == NULL)
1148 return;
1150 if (entry->type == HL_TYPE_DOTDOT)
1151 return;
1153 if (confirm_directory_hotlist_delete)
1155 char *title;
1156 int result;
1158 title = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1160 if (safe_delete)
1161 query_set_sel (1);
1162 result = query_dialog (title,
1163 _("Are you sure you want to remove this entry?"),
1164 D_ERROR, 2, _("&Yes"), _("&No"));
1166 g_free (title);
1168 if (result != 0)
1169 return;
1172 if (entry->type == HL_TYPE_GROUP)
1174 if (entry->head)
1176 char *header;
1177 int result;
1179 header = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1180 result = query_dialog (header, _("Group not empty.\nRemove it?"),
1181 D_ERROR, 2, _("&Yes"), _("&No"));
1182 g_free (header);
1184 if (result != 0)
1185 return;
1188 remove_group (entry);
1191 unlink_entry (entry);
1193 g_free (entry->label);
1194 g_free (entry->directory);
1195 g_free (entry);
1196 /* now remove list entry from screen */
1197 listbox_remove_current (l_hotlist);
1198 hotlist_state.modified = 1;
1201 char *
1202 hotlist_cmd (int vfs_or_hotlist)
1204 char *target = NULL;
1206 hotlist_state.type = vfs_or_hotlist;
1207 load_hotlist ();
1209 init_hotlist (vfs_or_hotlist);
1211 /* display file info */
1212 tty_setcolor (SELECTED_COLOR);
1214 hotlist_state.running = 1;
1215 run_dlg (hotlist_dlg);
1216 hotlist_state.running = 0;
1217 save_hotlist ();
1219 switch (hotlist_dlg->ret_value)
1221 default:
1222 case B_CANCEL:
1223 break;
1225 case B_ENTER:
1227 char *text = NULL;
1228 struct hotlist *hlp = NULL;
1230 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1231 target = g_strdup (hlp != NULL ? hlp->directory : text);
1232 break;
1234 } /* switch */
1236 hotlist_done ();
1237 return target;
1240 static void
1241 load_group (struct hotlist *grp)
1243 gchar **profile_keys, **keys;
1244 gsize len;
1245 char *group_section;
1246 struct hotlist *current = 0;
1248 group_section = find_group_section (grp);
1250 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1252 current_group = grp;
1254 while (*profile_keys)
1256 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1257 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1258 profile_keys++;
1260 g_free (group_section);
1261 g_strfreev (keys);
1263 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1265 while (*profile_keys)
1267 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1268 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1269 profile_keys++;
1271 g_strfreev (keys);
1273 for (current = grp->head; current; current = current->next)
1274 load_group (current);
1277 #define TKN_GROUP 0
1278 #define TKN_ENTRY 1
1279 #define TKN_STRING 2
1280 #define TKN_URL 3
1281 #define TKN_ENDGROUP 4
1282 #define TKN_COMMENT 5
1283 #define TKN_EOL 125
1284 #define TKN_EOF 126
1285 #define TKN_UNKNOWN 127
1287 static GString *tkn_buf = NULL;
1289 static char *hotlist_file_name;
1290 static FILE *hotlist_file;
1291 static time_t hotlist_file_mtime;
1293 static int
1294 hot_skip_blanks (void)
1296 int c;
1298 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1300 return c;
1304 static int
1305 hot_next_token (void)
1307 int c, ret = 0;
1308 size_t l;
1311 if (tkn_buf == NULL)
1312 tkn_buf = g_string_new ("");
1313 g_string_set_size (tkn_buf, 0);
1315 again:
1316 c = hot_skip_blanks ();
1317 switch (c)
1319 case EOF:
1320 ret = TKN_EOF;
1321 break;
1322 case '\n':
1323 ret = TKN_EOL;
1324 break;
1325 case '#':
1326 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1328 g_string_append_c (tkn_buf, c);
1330 ret = TKN_COMMENT;
1331 break;
1332 case '"':
1333 while ((c = getc (hotlist_file)) != EOF && c != '"')
1335 if (c == '\\')
1337 c = getc (hotlist_file);
1338 if (c == EOF)
1340 g_string_free (tkn_buf, TRUE);
1341 return TKN_EOF;
1344 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1346 if (c == EOF)
1347 ret = TKN_EOF;
1348 else
1349 ret = TKN_STRING;
1350 break;
1351 case '\\':
1352 c = getc (hotlist_file);
1353 if (c == EOF)
1355 g_string_free (tkn_buf, TRUE);
1356 return TKN_EOF;
1358 if (c == '\n')
1359 goto again;
1361 /* fall through; it is taken as normal character */
1363 default:
1366 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1368 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1369 if (c != EOF)
1370 ungetc (c, hotlist_file);
1371 l = tkn_buf->len;
1372 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1373 ret = TKN_GROUP;
1374 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1375 ret = TKN_ENTRY;
1376 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1377 ret = TKN_ENDGROUP;
1378 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1379 ret = TKN_URL;
1380 else
1381 ret = TKN_UNKNOWN;
1382 break;
1384 return ret;
1387 #define SKIP_TO_EOL { \
1388 int _tkn; \
1389 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1392 #define CHECK_TOKEN(_TKN_) \
1393 tkn = hot_next_token (); \
1394 if (tkn != _TKN_) { \
1395 hotlist_state.readonly = 1; \
1396 hotlist_state.file_error = 1; \
1397 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1398 tkn = hot_next_token (); \
1399 break; \
1402 static void
1403 hot_load_group (struct hotlist *grp)
1405 int tkn;
1406 struct hotlist *new_grp;
1407 char *label, *url;
1409 current_group = grp;
1411 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1412 switch (tkn)
1414 case TKN_GROUP:
1415 CHECK_TOKEN (TKN_STRING);
1416 new_grp =
1417 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1418 SKIP_TO_EOL;
1419 hot_load_group (new_grp);
1420 current_group = grp;
1421 break;
1422 case TKN_ENTRY:
1423 CHECK_TOKEN (TKN_STRING);
1424 label = g_strdup (tkn_buf->str);
1425 CHECK_TOKEN (TKN_URL);
1426 CHECK_TOKEN (TKN_STRING);
1427 url = g_strdup (tkn_buf->str);
1428 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1429 SKIP_TO_EOL;
1430 break;
1431 case TKN_COMMENT:
1432 label = g_strdup (tkn_buf->str);
1433 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1434 break;
1435 case TKN_EOF:
1436 hotlist_state.readonly = 1;
1437 hotlist_state.file_error = 1;
1438 return;
1439 break;
1440 case TKN_EOL:
1441 /* skip empty lines */
1442 break;
1443 default:
1444 hotlist_state.readonly = 1;
1445 hotlist_state.file_error = 1;
1446 SKIP_TO_EOL;
1447 break;
1449 SKIP_TO_EOL;
1452 static void
1453 hot_load_file (struct hotlist *grp)
1455 int tkn;
1456 struct hotlist *new_grp;
1457 char *label, *url;
1459 current_group = grp;
1461 while ((tkn = hot_next_token ()) != TKN_EOF)
1462 switch (tkn)
1464 case TKN_GROUP:
1465 CHECK_TOKEN (TKN_STRING);
1466 new_grp =
1467 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1468 SKIP_TO_EOL;
1469 hot_load_group (new_grp);
1470 current_group = grp;
1471 break;
1472 case TKN_ENTRY:
1473 CHECK_TOKEN (TKN_STRING);
1474 label = g_strdup (tkn_buf->str);
1475 CHECK_TOKEN (TKN_URL);
1476 CHECK_TOKEN (TKN_STRING);
1477 url = g_strdup (tkn_buf->str);
1478 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1479 SKIP_TO_EOL;
1480 break;
1481 case TKN_COMMENT:
1482 label = g_strdup (tkn_buf->str);
1483 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1484 break;
1485 case TKN_EOL:
1486 /* skip empty lines */
1487 break;
1488 default:
1489 hotlist_state.readonly = 1;
1490 hotlist_state.file_error = 1;
1491 SKIP_TO_EOL;
1492 break;
1496 static void
1497 clean_up_hotlist_groups (const char *section)
1499 char *grp_section;
1500 gchar **profile_keys, **keys;
1501 gsize len;
1503 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1504 if (mc_config_has_group (mc_main_config, section))
1505 mc_config_del_group (mc_main_config, section);
1507 if (mc_config_has_group (mc_main_config, grp_section))
1509 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1511 while (*profile_keys)
1513 clean_up_hotlist_groups (*profile_keys);
1514 profile_keys++;
1516 g_strfreev (keys);
1517 mc_config_del_group (mc_main_config, grp_section);
1519 g_free (grp_section);
1524 static void
1525 load_hotlist (void)
1527 int remove_old_list = 0;
1528 struct stat stat_buf;
1530 if (hotlist_state.loaded)
1532 stat (hotlist_file_name, &stat_buf);
1533 if (hotlist_file_mtime < stat_buf.st_mtime)
1534 done_hotlist ();
1535 else
1536 return;
1539 if (!hotlist_file_name)
1540 hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL);
1542 hotlist = new_hotlist ();
1543 hotlist->type = HL_TYPE_GROUP;
1544 hotlist->label = g_strdup (_("Top level group"));
1545 hotlist->up = hotlist;
1547 * compatibility :-(
1549 hotlist->directory = g_strdup ("Hotlist");
1551 hotlist_file = fopen (hotlist_file_name, "r");
1552 if (hotlist_file == NULL)
1554 int result;
1556 load_group (hotlist);
1557 hotlist_state.loaded = 1;
1559 * just to be sure we got copy
1561 hotlist_state.modified = 1;
1562 result = save_hotlist ();
1563 hotlist_state.modified = 0;
1564 if (result)
1565 remove_old_list = 1;
1566 else
1567 message (D_ERROR, _("Hotlist Load"),
1568 _("MC was unable to write ~/%s file,\nyour old hotlist entries were not deleted"),
1569 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1571 else
1573 hot_load_file (hotlist);
1574 fclose (hotlist_file);
1575 hotlist_state.loaded = 1;
1578 if (remove_old_list)
1580 GError *error = NULL;
1581 clean_up_hotlist_groups ("Hotlist");
1582 if (!mc_config_save_file (mc_main_config, &error))
1583 setup_save_config_show_error (mc_main_config->ini_path, &error);
1586 stat (hotlist_file_name, &stat_buf);
1587 hotlist_file_mtime = stat_buf.st_mtime;
1588 current_group = hotlist;
1592 static int list_level = 0;
1594 static void
1595 hot_save_group (struct hotlist *grp)
1597 struct hotlist *current = grp->head;
1598 int i;
1599 char *s;
1601 #define INDENT(n) \
1602 do { \
1603 for (i = 0; i < n; i++) \
1604 putc (' ', hotlist_file); \
1605 } while (0)
1607 for (; current; current = current->next)
1608 switch (current->type)
1610 case HL_TYPE_GROUP:
1611 INDENT (list_level);
1612 fputs ("GROUP \"", hotlist_file);
1613 for (s = current->label; *s; s++)
1615 if (*s == '"' || *s == '\\')
1616 putc ('\\', hotlist_file);
1617 putc (*s, hotlist_file);
1619 fputs ("\"\n", hotlist_file);
1620 list_level += 2;
1621 hot_save_group (current);
1622 list_level -= 2;
1623 INDENT (list_level);
1624 fputs ("ENDGROUP\n", hotlist_file);
1625 break;
1626 case HL_TYPE_ENTRY:
1627 INDENT (list_level);
1628 fputs ("ENTRY \"", hotlist_file);
1629 for (s = current->label; *s; s++)
1631 if (*s == '"' || *s == '\\')
1632 putc ('\\', hotlist_file);
1633 putc (*s, hotlist_file);
1635 fputs ("\" URL \"", hotlist_file);
1636 for (s = current->directory; *s; s++)
1638 if (*s == '"' || *s == '\\')
1639 putc ('\\', hotlist_file);
1640 putc (*s, hotlist_file);
1642 fputs ("\"\n", hotlist_file);
1643 break;
1644 case HL_TYPE_COMMENT:
1645 fprintf (hotlist_file, "#%s\n", current->label);
1646 break;
1647 case HL_TYPE_DOTDOT:
1648 /* do nothing */
1649 break;
1654 save_hotlist (void)
1656 int saved = 0;
1657 struct stat stat_buf;
1659 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name)
1661 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1663 hotlist_file = fopen (hotlist_file_name, "w");
1664 if (hotlist_file != NULL)
1666 hot_save_group (hotlist);
1667 fclose (hotlist_file);
1668 stat (hotlist_file_name, &stat_buf);
1669 hotlist_file_mtime = stat_buf.st_mtime;
1670 saved = 1;
1671 hotlist_state.modified = 0;
1673 else
1674 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1677 return saved;
1681 * Unload list from memory.
1682 * Don't confuse with hotlist_done() for GUI.
1684 void
1685 done_hotlist (void)
1687 if (hotlist)
1689 remove_group (hotlist);
1690 g_free (hotlist->label);
1691 g_free (hotlist->directory);
1692 g_free (hotlist);
1693 hotlist = 0;
1696 hotlist_state.loaded = 0;
1698 g_free (hotlist_file_name);
1699 hotlist_file_name = 0;
1700 l_hotlist = 0;
1701 current_group = 0;
1703 if (tkn_buf)
1705 g_string_free (tkn_buf, TRUE);
1706 tkn_buf = NULL;
1710 static void
1711 add_dotdot_to_list (void)
1713 if (current_group != hotlist)
1715 if (hotlist_has_dot_dot != 0)
1716 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);