Ticket #1787: remove obsolete checks for <stdlib.h>
[free-mc.git] / src / hotlist.c
blobaf58c4f6e603bebdb651894a08ca784ab34d6323
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 "global.h"
45 #include "../src/tty/tty.h" /* COLS */
46 #include "../src/skin/skin.h"
47 #include "../src/tty/key.h" /* KEY_M_CTRL */
49 #include "dialog.h"
50 #include "widget.h"
51 #include "setup.h" /* For profile_bname */
52 #include "../src/mcconfig/mcconfig.h" /* Load/save directories hotlist */
53 #include "wtools.h" /* QuickDialog */
54 #include "panel.h" /* current_panel */
55 #include "main.h" /* update_panels() */
56 #include "layout.h" /* repaint_screen() */
57 #include "hotlist.h"
58 #include "command.h" /* cmdline */
59 #include "history.h"
60 #include "strutil.h"
61 #include "util.h"
62 #include "fileloc.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 USE_VFS
82 #include "../vfs/gc.h"
83 #define B_FREE_ALL_VFS (B_USER + 8)
84 #define B_REFRESH_VFS (B_USER + 9)
85 #endif
87 int hotlist_has_dot_dot = 1;
89 static WListbox *l_hotlist;
90 static WListbox *l_movelist;
92 static Dlg_head *hotlist_dlg;
93 static Dlg_head *movelist_dlg;
95 static WLabel *pname, *pname_group, *movelist_group;
97 enum HotListType {
98 HL_TYPE_GROUP,
99 HL_TYPE_ENTRY,
100 HL_TYPE_COMMENT,
101 HL_TYPE_DOTDOT
104 static struct {
106 * these parameters are intended to be user configurable
108 int expanded; /* expanded view of all groups at startup */
111 * these reflect run time state
114 int loaded; /* hotlist is loaded */
115 int readonly; /* hotlist readonly */
116 int file_error; /* parse error while reading file */
117 int running; /* we are running dlg (and have to
118 update listbox */
119 int moving; /* we are in moving hotlist currently */
120 int modified; /* hotlist was modified */
121 int type; /* LIST_HOTLIST || LIST_VFSLIST */
122 } hotlist_state;
124 static struct _hotlist_but {
125 int ret_cmd, flags, y, x;
126 const char *text;
127 int type;
128 widget_pos_flags_t pos_flags;
129 } hotlist_but[] = {
130 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"),
131 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
132 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"),
133 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
134 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"),
135 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
136 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"),
137 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
138 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &Entry"),
139 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
140 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &Group"),
141 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
142 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"),
143 LIST_HOTLIST | LIST_VFSLIST|LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
144 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"),
145 LIST_HOTLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
146 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),
147 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
148 #ifdef USE_VFS
149 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"),
150 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
151 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"),
152 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
153 #endif
154 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &To"),
155 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM }
158 /* Directory hotlist */
159 static struct hotlist{
160 enum HotListType type;
161 char *directory;
162 char *label;
163 struct hotlist *head;
164 struct hotlist *up;
165 struct hotlist *next;
166 } *hotlist = NULL;
168 static struct hotlist *current_group;
170 static void init_movelist (int, struct hotlist *);
171 static void add_new_group_cmd (void);
172 static void add_new_entry_cmd (void);
173 static void remove_from_hotlist (struct hotlist *entry);
174 static void load_hotlist (void);
175 static void add_dotdot_to_list (void);
177 #define new_hotlist() g_new0(struct hotlist, 1)
179 static void
180 hotlist_refresh (Dlg_head * dlg)
182 common_dialog_repaint (dlg);
183 tty_setcolor (COLOR_NORMAL);
184 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10),
185 dlg->cols - (UX * 2));
186 if (!hotlist_state.moving)
187 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2));
190 /* If current->data is 0, then we are dealing with a VFS pathname */
191 static void
192 update_path_name (void)
194 const char *text = "";
195 char *p;
196 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
197 Dlg_head *dlg = list->widget.parent;
199 if (list->current) {
200 if (list->current->data != 0) {
201 struct hotlist *hlp = (struct hotlist *) list->current->data;
203 if (hlp->type == HL_TYPE_ENTRY ||
204 hlp->type == HL_TYPE_DOTDOT)
205 text = hlp->directory;
206 else if (hlp->type == HL_TYPE_GROUP)
207 text = _("Subgroup - press ENTER to see list");
208 } else {
209 text = list->current->text;
212 if (!hotlist_state.moving)
213 label_set_text (pname,
214 str_trunc (text, dlg->cols - (UX * 2 + 4)));
216 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
217 if (!hotlist_state.moving)
218 label_set_text (pname_group,
219 str_trunc (p, dlg->cols - (UX * 2 + 4)));
220 else
221 label_set_text (movelist_group,
222 str_trunc (p, dlg->cols - (UX * 2 + 4)));
223 g_free (p);
225 dlg_redraw (dlg);
228 #define CHECK_BUFFER \
229 do { \
230 int i; \
232 if ((i = strlen (current->label) + 3) > buflen) { \
233 g_free (buf); \
234 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
236 buf[0] = '\0'; \
237 } while (0)
239 static void fill_listbox (void)
241 struct hotlist *current = current_group->head;
242 GString *buff = g_string_new ("");
244 while (current){
245 switch (current->type) {
246 case HL_TYPE_GROUP:
248 /* buff clean up */
249 g_string_truncate(buff, 0);
250 g_string_append(buff,"->");
251 g_string_append(buff,current->label);
252 if (hotlist_state.moving)
253 listbox_add_item (l_movelist, 0, 0, buff->str, current);
254 else
255 listbox_add_item (l_hotlist, 0, 0, buff->str, current);
257 break;
258 case HL_TYPE_DOTDOT:
259 case HL_TYPE_ENTRY:
260 if (hotlist_state.moving)
261 listbox_add_item (l_movelist, 0, 0, current->label, current);
262 else
263 listbox_add_item (l_hotlist, 0, 0, current->label, current);
264 break;
265 default:
266 break;
268 current = current->next;
270 g_string_free (buff, TRUE);
273 static void
274 unlink_entry (struct hotlist *entry)
276 struct hotlist *current = current_group->head;
278 if (current == entry)
279 current_group->head = entry->next;
280 else {
281 while (current && current->next != entry)
282 current = current->next;
283 if (current)
284 current->next = entry->next;
286 entry->next =
287 entry->up = 0;
290 #ifdef USE_VFS
291 static void add_name_to_list (const char *path)
293 listbox_add_item (l_hotlist, 0, 0, path, 0);
295 #endif /* !USE_VFS */
297 static int
298 hotlist_button_callback (int action)
300 switch (action) {
301 case B_MOVE:
303 struct hotlist *saved = current_group;
304 struct hotlist *item;
305 struct hotlist *moveto_item = 0;
306 struct hotlist *moveto_group = 0;
307 int ret;
309 if (!l_hotlist->current)
310 return MSG_NOT_HANDLED; /* empty group - nothing to do */
311 item = l_hotlist->current->data;
312 hotlist_state.moving = 1;
313 init_movelist (LIST_MOVELIST, item);
314 run_dlg (movelist_dlg);
315 ret = movelist_dlg->ret_value;
316 hotlist_state.moving = 0;
317 if (l_movelist->current)
318 moveto_item = l_movelist->current->data;
319 moveto_group = current_group;
320 destroy_dlg (movelist_dlg);
321 current_group = saved;
322 if (ret == B_CANCEL)
323 return MSG_NOT_HANDLED;
324 if (moveto_item == item)
325 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
326 it hardly changes anything ;) */
327 unlink_entry (item);
328 listbox_remove_current (l_hotlist, 1);
329 item->up = moveto_group;
330 if (!moveto_group->head)
331 moveto_group->head = item;
332 else if (!moveto_item) { /* we have group with just comments */
333 struct hotlist *p = moveto_group->head;
335 /* skip comments */
336 while (p->next)
337 p = p->next;
338 p->next = item;
339 } else if (ret == B_ENTER || ret == B_APPEND)
340 if (!moveto_item->next)
341 moveto_item->next = item;
342 else {
343 item->next = moveto_item->next;
344 moveto_item->next = item;
345 } else if (moveto_group->head == moveto_item) {
346 moveto_group->head = item;
347 item->next = moveto_item;
348 } else {
349 struct hotlist *p = moveto_group->head;
351 while (p->next != moveto_item)
352 p = p->next;
353 item->next = p->next;
354 p->next = item;
356 listbox_remove_list (l_hotlist);
357 fill_listbox ();
358 repaint_screen ();
359 hotlist_state.modified = 1;
360 return MSG_NOT_HANDLED;
361 break;
363 case B_REMOVE:
364 if (l_hotlist->current && l_hotlist->current->data)
365 remove_from_hotlist (l_hotlist->current->data);
366 return MSG_NOT_HANDLED;
367 break;
369 case B_NEW_GROUP:
370 add_new_group_cmd ();
371 return MSG_NOT_HANDLED;
372 break;
374 case B_ADD_CURRENT:
375 add2hotlist_cmd ();
376 return MSG_NOT_HANDLED;
377 break;
379 case B_NEW_ENTRY:
380 add_new_entry_cmd ();
381 return MSG_NOT_HANDLED;
382 break;
384 case B_ENTER:
386 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
387 if (list->current) {
388 if (list->current->data) {
389 struct hotlist *hlp =
390 (struct hotlist *) list->current->data;
391 if (hlp->type == HL_TYPE_ENTRY)
392 return MSG_HANDLED;
393 else if (hlp->type == HL_TYPE_DOTDOT) {
394 /* Fall through - go up */
397 else {
398 listbox_remove_list (list);
399 current_group = hlp;
400 fill_listbox ();
401 return MSG_NOT_HANDLED;
403 } else
404 return MSG_HANDLED;
407 /* Fall through if list empty - just go up */
409 case B_UP_GROUP:
411 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
412 listbox_remove_list (list);
413 current_group = current_group->up;
414 fill_listbox ();
415 return MSG_NOT_HANDLED;
416 break;
419 #ifdef USE_VFS
420 case B_FREE_ALL_VFS:
421 vfs_expire (1);
422 /* fall through */
424 case B_REFRESH_VFS:
425 listbox_remove_list (l_hotlist);
426 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
427 vfs_fill_names (add_name_to_list);
428 return MSG_NOT_HANDLED;
429 #endif /* USE_VFS */
431 default:
432 return MSG_HANDLED;
433 break;
438 static cb_ret_t
439 hotlist_callback (Dlg_head *h, Widget *sender,
440 dlg_msg_t msg, int parm, void *data)
442 switch (msg) {
443 case DLG_DRAW:
444 hotlist_refresh (h);
445 return MSG_HANDLED;
447 case DLG_UNHANDLED_KEY:
448 switch (parm) {
449 case KEY_M_CTRL | '\n':
450 goto l1;
451 case '\n':
452 case KEY_ENTER:
453 case KEY_RIGHT:
454 if (hotlist_button_callback (B_ENTER)) {
455 h->ret_value = B_ENTER;
456 dlg_stop (h);
458 return MSG_HANDLED;
459 break;
460 case KEY_LEFT:
461 if (hotlist_state.type != LIST_VFSLIST)
462 return !hotlist_button_callback (B_UP_GROUP);
463 else
464 return MSG_NOT_HANDLED;
465 break;
466 case KEY_DC:
467 if (!hotlist_state.moving) {
468 hotlist_button_callback (B_REMOVE);
469 return MSG_HANDLED;
471 break;
473 case ALT ('\n'):
474 case ALT ('\r'):
475 if (!hotlist_state.moving) {
476 if (l_hotlist->current) {
477 if (l_hotlist->current->data) {
478 struct hotlist *hlp =
479 (struct hotlist *) l_hotlist->current->data;
480 if (hlp->type == HL_TYPE_ENTRY) {
481 char *tmp =
482 g_strconcat ("cd ", hlp->directory, (char *) NULL);
483 stuff (cmdline, tmp, 0);
484 g_free (tmp);
485 dlg_stop (h);
486 h->ret_value = B_CANCEL;
487 return MSG_HANDLED;
492 return MSG_HANDLED; /* ignore key */
494 return MSG_NOT_HANDLED;
496 case DLG_POST_KEY:
497 if (hotlist_state.moving)
498 dlg_select_widget (l_movelist);
499 else
500 dlg_select_widget (l_hotlist);
501 /* always stay on hotlist */
502 /* fall through */
504 case DLG_INIT:
505 tty_setcolor (MENU_ENTRY_COLOR);
506 update_path_name ();
507 return MSG_HANDLED;
509 case DLG_RESIZE:
510 /* simply call dlg_set_size() with new size */
511 dlg_set_size (h, LINES - 2, COLS - 6);
512 return MSG_HANDLED;
514 default:
515 return default_dlg_callback (h, sender, msg, parm, data);
519 static int l_call (WListbox *list)
521 Dlg_head *dlg = list->widget.parent;
523 if (list->current){
524 if (list->current->data) {
525 struct hotlist *hlp = (struct hotlist*) list->current->data;
526 if (hlp->type == HL_TYPE_ENTRY) {
527 dlg->ret_value = B_ENTER;
528 dlg_stop (dlg);
529 return LISTBOX_DONE;
530 } else {
531 hotlist_button_callback (B_ENTER);
532 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
533 return LISTBOX_CONT;
535 } else {
536 dlg->ret_value = B_ENTER;
537 dlg_stop (dlg);
538 return LISTBOX_DONE;
542 hotlist_button_callback (B_UP_GROUP);
543 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
544 return LISTBOX_CONT;
548 * Expands all button names (once) and recalculates button positions.
549 * returns number of columns in the dialog box, which is 10 chars longer
550 * then buttonbar.
552 * If common width of the window (i.e. in xterm) is less than returned
553 * width - sorry :) (anyway this did not handled in previous version too)
555 static int
556 init_i18n_stuff(int list_type, int cols)
558 register int i;
559 static const char* cancel_but = N_("&Cancel");
561 #ifdef ENABLE_NLS
562 static int hotlist_i18n_flag = 0;
564 if (!hotlist_i18n_flag)
566 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
567 while (i--)
568 hotlist_but [i].text = _(hotlist_but [i].text);
570 cancel_but = _(cancel_but);
571 hotlist_i18n_flag = 1;
573 #endif /* ENABLE_NLS */
575 /* Dynamic resizing of buttonbars */
577 int len[2], count[2]; /* at most two lines of buttons */
578 int cur_x[2], row;
580 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
581 len[0] = len[1] = count[0] = count[1] = 0;
583 /* Count len of buttonbars, assuming 2 extra space between buttons */
584 while (i--)
586 if (! (hotlist_but[i].type & list_type))
587 continue;
589 row = hotlist_but [i].y;
590 ++count [row];
591 len [row] += str_term_width1 (hotlist_but [i].text) + 5;
592 if (hotlist_but [i].flags == DEFPUSH_BUTTON)
593 len [row] += 2;
595 len[0] -= 2;
596 len[1] -= 2;
598 cols = max(cols, max(len[0], len[1]));
600 /* arrange buttons */
602 cur_x[0] = cur_x[1] = 0;
603 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
604 while (i--)
606 if (! (hotlist_but[i].type & list_type))
607 continue;
609 row = hotlist_but [i].y;
611 if (hotlist_but [i].x != 0)
613 /* not first int the row */
614 if (!strcmp (hotlist_but [i].text, cancel_but))
615 hotlist_but [i].x =
616 cols - str_term_width1 (hotlist_but [i].text) - 13;
617 else
618 hotlist_but [i].x = cur_x [row];
621 cur_x [row] += str_term_width1 (hotlist_but [i].text) + 2
622 + (hotlist_but [i].flags == DEFPUSH_BUTTON ? 5 : 3);
626 return cols;
629 static void
630 init_hotlist (int list_type)
632 size_t i;
633 const char *title, *help_node;
634 int hotlist_cols;
636 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
638 do_refresh ();
640 hotlist_state.expanded =
641 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
643 if (list_type == LIST_VFSLIST) {
644 title = _("Active VFS directories");
645 help_node = "[vfshot]"; /* FIXME - no such node */
646 } else {
647 title = _("Directory hotlist");
648 help_node = "[Hotlist]";
651 hotlist_dlg =
652 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
653 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
655 for (i = 0; i < BUTTONS; i++) {
656 if (hotlist_but[i].type & list_type)
657 add_widget_autopos (hotlist_dlg,
658 button_new (BY + hotlist_but[i].y,
659 BX + hotlist_but[i].x,
660 hotlist_but[i].ret_cmd,
661 hotlist_but[i].flags,
662 hotlist_but[i].text,
663 hotlist_button_callback),
664 hotlist_but[i].pos_flags);
667 /* We add the labels.
668 * pname will hold entry's pathname;
669 * pname_group will hold name of current group
671 pname = label_new (UY - 11 + LINES, UX + 2, "");
672 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
673 if (!hotlist_state.moving) {
674 add_widget_autopos (hotlist_dlg,
675 label_new (UY - 12 + LINES, UX + 1,
676 _(" Directory path ")),
677 WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
679 /* This one holds the displayed pathname */
680 pname_group = label_new (UY, UX + 1, _(" Directory label "));
681 add_widget (hotlist_dlg, pname_group);
683 /* get new listbox */
684 l_hotlist =
685 listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8,
686 l_call);
688 /* Fill the hotlist with the active VFS or the hotlist */
689 #ifdef USE_VFS
690 if (list_type == LIST_VFSLIST) {
691 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
692 vfs_fill_names (add_name_to_list);
693 } else
694 #endif /* !USE_VFS */
695 fill_listbox ();
697 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
698 /* add listbox to the dialogs */
701 static void
702 init_movelist (int list_type, struct hotlist *item)
704 size_t i;
705 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
706 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
708 do_refresh ();
710 movelist_dlg =
711 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
712 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
713 g_free (hdr);
715 for (i = 0; i < BUTTONS; i++) {
716 if (hotlist_but[i].type & list_type)
717 add_widget (movelist_dlg,
718 button_new (BY - 4 + hotlist_but[i].y,
719 BX + hotlist_but[i].x,
720 hotlist_but[i].ret_cmd,
721 hotlist_but[i].flags,
722 hotlist_but[i].text,
723 hotlist_button_callback));
726 /* We add the labels. We are interested in the last one,
727 * that one will hold the path name label
729 movelist_group = label_new (UY, UX + 1, _(" Directory label "));
730 add_widget (movelist_dlg, movelist_group);
731 /* get new listbox */
732 l_movelist =
733 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
734 movelist_dlg->cols - 2 * UX - 2, l_call);
736 fill_listbox ();
738 add_widget (movelist_dlg, l_movelist);
739 /* add listbox to the dialogs */
743 * Destroy the list dialog.
744 * Don't confuse with done_hotlist() for the list in memory.
746 static void hotlist_done (void)
748 destroy_dlg (hotlist_dlg);
749 l_hotlist = NULL;
750 if (0)
751 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
752 repaint_screen ();
755 static inline char *
756 find_group_section (struct hotlist *grp)
758 return g_strconcat (grp->directory, ".Group", (char *) NULL);
762 /* 1.11.96 bor: added pos parameter to control placement of new item.
763 see widget.c, listbox_add_item()
764 now hotlist is in unsorted mode
766 enum {
767 HL_BEFORE_CURRENT = 1
768 ,HL_AFTER_CURRENT = 2
771 static struct hotlist *
772 add2hotlist (char *label, char *directory, enum HotListType type, int pos)
774 struct hotlist *new;
775 struct hotlist *current = NULL;
778 * Hotlist is neither loaded nor loading.
779 * Must be called by "Ctrl-x a" before using hotlist.
781 if (!current_group)
782 load_hotlist ();
784 if (l_hotlist && l_hotlist->current) {
785 current = l_hotlist->current->data;
787 /* Make sure `..' stays at the top of the list. */
788 if (current->type == HL_TYPE_DOTDOT)
789 pos = HL_AFTER_CURRENT;
792 new = new_hotlist ();
794 new->type = type;
795 new->label = label;
796 new->directory = directory;
797 new->up = current_group;
799 if (type == HL_TYPE_GROUP) {
800 current_group = new;
801 add_dotdot_to_list ();
802 current_group = new->up;
805 if (!current_group->head) { /* first element in group */
806 current_group->head = new;
807 } else if (pos == HL_AFTER_CURRENT) {
808 new->next = current->next;
809 current->next = new;
810 } else if (pos == HL_BEFORE_CURRENT &&
811 current == current_group->head) {
812 /* should be inserted before first item */
813 new->next = current;
814 current_group->head = new;
815 } else if (pos == HL_BEFORE_CURRENT) {
816 struct hotlist *p = current_group->head;
818 while (p->next != current)
819 p = p->next;
821 new->next = current;
822 p->next = new;
823 } else { /* append at the end */
824 struct hotlist *p = current_group->head;
826 while (p->next)
827 p = p->next;
829 p->next = new;
832 if (hotlist_state.running && type != HL_TYPE_COMMENT &&
833 type != HL_TYPE_DOTDOT) {
834 if (type == HL_TYPE_GROUP) {
835 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
837 listbox_add_item (l_hotlist, pos, 0, lbl, new);
838 g_free (lbl);
839 } else
840 listbox_add_item (l_hotlist, pos, 0, new->label, new);
841 listbox_select_entry (l_hotlist, l_hotlist->current);
843 return new;
847 #ifdef ENABLE_NLS
849 * Support routine for add_new_entry_input()/add_new_group_input()
850 * Change positions of buttons (first three widgets).
852 * This is just a quick hack. Accurate procedure must take care of
853 * internationalized label lengths and total buttonbar length...assume
854 * 64 is longer anyway.
856 static void
857 add_widgets_i18n (QuickWidget* qw, int len)
859 int i, l[3], space, cur_x;
861 for (i = 0; i < 3; i++) {
862 qw [i].u.button.text = _(qw [i].u.button.text);
863 l[i] = str_term_width1 (qw [i].u.button.text) + 3;
865 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
867 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space) {
868 qw [i].relative_x = cur_x;
869 qw [i].x_divisions = len;
872 #endif /* ENABLE_NLS */
874 static int
875 add_new_entry_input (const char *header, const char *text1, const char *text2,
876 const char *help, char **r1, char **r2)
878 #define RELATIVE_Y_BUTTONS 4
879 #define RELATIVE_Y_LABEL_PTH 3
880 #define RELATIVE_Y_INPUT_PTH 4
882 QuickWidget quick_widgets [] =
884 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
885 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
886 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
887 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
888 /* 4 */ QUICK_LABEL (RELATIVE_Y_LABEL_PTH, 80, 3, 0, text2),
889 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
890 /* 6 */ QUICK_LABEL (3, 80, 2, 0, text1),
891 QUICK_END
894 int len;
895 int i;
896 int lines1, lines2;
897 int cols1, cols2;
899 #ifdef ENABLE_NLS
900 static gboolean i18n_flag = FALSE;
901 #endif /* ENABLE_NLS */
903 msglen(text1, &lines1, &cols1);
904 msglen(text2, &lines2, &cols2);
905 len = max (str_term_width1 (header), cols1);
906 len = max (max (len, cols2) + 4, 64);
908 #ifdef ENABLE_NLS
909 if (!i18n_flag) {
910 add_widgets_i18n (quick_widgets, len);
911 i18n_flag = TRUE;
913 #endif /* ENABLE_NLS */
916 QuickDialog Quick_input =
918 len, lines1 + lines2 + 7, -1, -1, header,
919 help, quick_widgets, FALSE
922 for (i = 0; i < 7; i++)
923 quick_widgets [i].y_divisions = Quick_input.ylen;
925 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
926 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
927 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
928 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
929 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
931 i = quick_dialog (&Quick_input);
934 return (i != B_CANCEL) ? i : 0;
936 #undef RELATIVE_Y_BUTTONS
937 #undef RELATIVE_Y_LABEL_PTH
938 #undef RELATIVE_Y_INPUT_PTH
941 static void add_new_entry_cmd (void)
943 char *title, *url, *to_free;
944 int ret;
946 /* Take current directory as default value for input fields */
947 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
949 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"),
950 _("Directory path"), "[Hotlist]", &title, &url);
951 g_free (to_free);
953 if (!ret)
954 return;
955 if (!title || !*title || !url || !*url) {
956 g_free (title);
957 g_free (url);
958 return;
961 if (ret == B_ENTER || ret == B_APPEND)
962 add2hotlist (title, url, HL_TYPE_ENTRY, HL_AFTER_CURRENT);
963 else
964 add2hotlist (title, url, HL_TYPE_ENTRY, HL_BEFORE_CURRENT);
966 hotlist_state.modified = 1;
969 static int
970 add_new_group_input (const char *header, const char *label, char **result)
972 QuickWidget quick_widgets [] =
974 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
975 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
976 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
977 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input" , result),
978 /* 4 */ QUICK_LABEL (3, 80, 2, 0, label),
979 QUICK_END
982 int len;
983 int i;
984 int lines, cols;
985 int ret;
987 #ifdef ENABLE_NLS
988 static gboolean i18n_flag = FALSE;
989 #endif /* ENABLE_NLS */
991 msglen (label, &lines, &cols);
992 len = max (max (str_term_width1 (header), cols) + 4, 64);
994 #ifdef ENABLE_NLS
995 if (!i18n_flag) {
996 add_widgets_i18n (quick_widgets, len);
997 i18n_flag = TRUE;
999 #endif /* ENABLE_NLS */
1002 QuickDialog Quick_input =
1004 len, lines + 6, -1, -1, header,
1005 "[Hotlist]", quick_widgets, FALSE
1008 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
1009 quick_widgets variable above */
1011 for (i = 0; i < 5; i++)
1012 quick_widgets[i].y_divisions = Quick_input.ylen;
1014 for (i = 0; i < 4; i++)
1015 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1017 ret = quick_dialog (&Quick_input);
1020 return (ret != B_CANCEL) ? ret : 0;
1023 static void add_new_group_cmd (void)
1025 char *label;
1026 int ret;
1028 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
1029 if (!ret || !label || !*label)
1030 return;
1032 if (ret == B_ENTER || ret == B_APPEND)
1033 add2hotlist (label, 0, HL_TYPE_GROUP, HL_AFTER_CURRENT);
1034 else
1035 add2hotlist (label, 0, HL_TYPE_GROUP, HL_BEFORE_CURRENT);
1037 hotlist_state.modified = 1;
1040 void add2hotlist_cmd (void)
1042 char *lc_prompt, *label;
1043 const char *cp = _("Label for \"%s\":");
1044 int l = str_term_width1 (cp);
1045 char *label_string = g_strdup (current_panel->cwd);
1047 strip_password (label_string, 1);
1049 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS-2*UX-(l+8)));
1050 label = input_dialog (_(" Add to hotlist "), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1051 g_free (lc_prompt);
1053 if (!label || !*label) {
1054 g_free (label_string);
1055 g_free (label);
1056 return;
1058 add2hotlist (label, label_string, HL_TYPE_ENTRY, 0);
1059 hotlist_state.modified = 1;
1062 static void remove_group (struct hotlist *grp)
1064 struct hotlist *current = grp->head;
1066 while (current) {
1067 struct hotlist *next = current->next;
1069 if (current->type == HL_TYPE_GROUP)
1070 remove_group (current);
1072 g_free (current->label);
1073 g_free (current->directory);
1074 g_free (current);
1076 current = next;
1081 static void remove_from_hotlist (struct hotlist *entry)
1083 if (entry->type == HL_TYPE_DOTDOT)
1084 return;
1086 if (confirm_directory_hotlist_delete) {
1087 char *title;
1088 int result;
1090 title = g_strconcat (_(" Remove: "),
1091 str_trunc (entry->label, 30),
1092 " ",
1093 NULL);
1095 if (safe_delete)
1096 query_set_sel (1);
1097 result = query_dialog (title,
1098 _("\n Are you sure you want to remove this entry?"),
1099 D_ERROR, 2, _("&Yes"), _("&No"));
1101 g_free (title);
1103 if (result != 0)
1104 return;
1107 if (entry->type == HL_TYPE_GROUP) {
1108 if (entry->head) {
1109 char *header;
1110 int result;
1112 header = g_strconcat (_(" Remove: "),
1113 str_trunc (entry->label, 30),
1114 " ",
1115 NULL);
1116 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1117 D_ERROR, 2,
1118 _("&Yes"), _("&No"));
1119 g_free (header);
1121 if (result != 0)
1122 return;
1125 remove_group (entry);
1128 unlink_entry (entry);
1130 g_free (entry->label);
1131 g_free (entry->directory);
1132 g_free (entry);
1133 /* now remove list entry from screen */
1134 listbox_remove_current (l_hotlist, 1);
1135 hotlist_state.modified = 1;
1138 char *hotlist_cmd (int vfs_or_hotlist)
1140 char *target = NULL;
1142 hotlist_state.type = vfs_or_hotlist;
1143 load_hotlist ();
1145 init_hotlist (vfs_or_hotlist);
1147 /* display file info */
1148 tty_setcolor (SELECTED_COLOR);
1150 hotlist_state.running = 1;
1151 run_dlg (hotlist_dlg);
1152 hotlist_state.running = 0;
1153 save_hotlist ();
1155 switch (hotlist_dlg->ret_value) {
1156 case B_CANCEL:
1157 break;
1159 case B_ENTER:
1160 if (l_hotlist->current->data) {
1161 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
1162 target = g_strdup (hlp->directory);
1163 } else
1164 target = g_strdup (l_hotlist->current->text);
1165 break;
1168 hotlist_done ();
1169 return target;
1172 static void
1173 load_group (struct hotlist *grp)
1175 gchar **profile_keys, **keys;
1176 gsize len;
1177 char *group_section;
1178 struct hotlist *current = 0;
1180 group_section = find_group_section (grp);
1182 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1184 current_group = grp;
1186 while (*profile_keys){
1187 add2hotlist (
1188 mc_config_get_string(mc_main_config, group_section, *profile_keys, ""),
1189 g_strdup (*profile_keys),
1190 HL_TYPE_GROUP,
1192 profile_keys++;
1194 g_free (group_section);
1195 g_strfreev(keys);
1197 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory,&len);
1199 while (*profile_keys){
1200 add2hotlist (
1201 mc_config_get_string(mc_main_config,group_section,*profile_keys,""),
1202 g_strdup (*profile_keys),
1203 HL_TYPE_ENTRY,
1205 profile_keys++;
1207 g_strfreev(keys);
1209 for (current = grp->head; current; current = current->next)
1210 load_group (current);
1213 #define TKN_GROUP 0
1214 #define TKN_ENTRY 1
1215 #define TKN_STRING 2
1216 #define TKN_URL 3
1217 #define TKN_ENDGROUP 4
1218 #define TKN_COMMENT 5
1219 #define TKN_EOL 125
1220 #define TKN_EOF 126
1221 #define TKN_UNKNOWN 127
1223 static GString *tkn_buf = NULL;
1225 static char *hotlist_file_name;
1226 static FILE *hotlist_file;
1227 static time_t hotlist_file_mtime;
1229 static int hot_skip_blanks (void)
1231 int c;
1233 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1235 return c;
1239 static int hot_next_token (void)
1241 int c, ret=0;
1242 size_t l;
1245 if (tkn_buf == NULL) tkn_buf = g_string_new ("");
1246 g_string_set_size(tkn_buf,0);
1248 again:
1249 c = hot_skip_blanks ();
1250 switch (c) {
1251 case EOF:
1252 ret = TKN_EOF;
1253 break;
1254 case '\n':
1255 ret = TKN_EOL;
1256 break;
1257 case '#':
1258 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1259 g_string_append_c (tkn_buf, c);
1261 ret = TKN_COMMENT;
1262 break;
1263 case '"':
1264 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1265 if (c == '\\')
1266 if ((c = getc (hotlist_file)) == EOF){
1267 g_string_free (tkn_buf, TRUE);
1268 return TKN_EOF;
1270 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1272 if (c == EOF)
1273 ret = TKN_EOF;
1274 else
1275 ret = TKN_STRING;
1276 break;
1277 case '\\':
1278 if ((c = getc (hotlist_file)) == EOF){
1279 g_string_free (tkn_buf, TRUE);
1280 return TKN_EOF;
1282 if (c == '\n')
1283 goto again;
1285 /* fall through; it is taken as normal character */
1287 default:
1288 do {
1289 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1290 } while ((c = fgetc (hotlist_file)) != EOF &&
1291 (g_ascii_isalnum (c) || !isascii (c)));
1292 if (c != EOF)
1293 ungetc (c, hotlist_file);
1294 l = tkn_buf->len;
1295 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1296 ret = TKN_GROUP;
1297 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1298 ret = TKN_ENTRY;
1299 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1300 ret = TKN_ENDGROUP;
1301 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1302 ret = TKN_URL;
1303 else
1304 ret = TKN_UNKNOWN;
1305 break;
1307 return ret;
1310 #define SKIP_TO_EOL { \
1311 int _tkn; \
1312 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1315 #define CHECK_TOKEN(_TKN_) \
1316 if ((tkn = hot_next_token ()) != _TKN_) { \
1317 hotlist_state.readonly = 1; \
1318 hotlist_state.file_error = 1; \
1319 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1320 tkn = hot_next_token (); \
1321 break; \
1324 static void
1325 hot_load_group (struct hotlist * grp)
1327 int tkn;
1328 struct hotlist *new_grp;
1329 char *label, *url;
1331 current_group = grp;
1333 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1334 switch (tkn) {
1335 case TKN_GROUP:
1336 CHECK_TOKEN(TKN_STRING);
1337 new_grp = add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, 0);
1338 SKIP_TO_EOL;
1339 hot_load_group (new_grp);
1340 current_group = grp;
1341 break;
1342 case TKN_ENTRY:
1343 CHECK_TOKEN(TKN_STRING);
1344 label = g_strdup (tkn_buf->str);
1345 CHECK_TOKEN(TKN_URL);
1346 CHECK_TOKEN(TKN_STRING);
1347 url = g_strdup (tkn_buf->str);
1348 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1349 SKIP_TO_EOL;
1350 break;
1351 case TKN_COMMENT:
1352 label = g_strdup (tkn_buf->str);
1353 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1354 break;
1355 case TKN_EOF:
1356 hotlist_state.readonly = 1;
1357 hotlist_state.file_error = 1;
1358 return;
1359 break;
1360 case TKN_EOL:
1361 /* skip empty lines */
1362 break;
1363 default:
1364 hotlist_state.readonly = 1;
1365 hotlist_state.file_error = 1;
1366 SKIP_TO_EOL;
1367 break;
1369 SKIP_TO_EOL;
1372 static void
1373 hot_load_file (struct hotlist * grp)
1375 int tkn;
1376 struct hotlist *new_grp;
1377 char *label, *url;
1379 current_group = grp;
1381 while ((tkn = hot_next_token())!= TKN_EOF)
1382 switch (tkn) {
1383 case TKN_GROUP:
1384 CHECK_TOKEN(TKN_STRING);
1385 new_grp = add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, 0);
1386 SKIP_TO_EOL;
1387 hot_load_group (new_grp);
1388 current_group = grp;
1389 break;
1390 case TKN_ENTRY:
1391 CHECK_TOKEN(TKN_STRING);
1392 label = g_strdup (tkn_buf->str);
1393 CHECK_TOKEN(TKN_URL);
1394 CHECK_TOKEN(TKN_STRING);
1395 url = g_strdup (tkn_buf->str);
1396 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1397 SKIP_TO_EOL;
1398 break;
1399 case TKN_COMMENT:
1400 label = g_strdup (tkn_buf->str);
1401 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1402 break;
1403 case TKN_EOL:
1404 /* skip empty lines */
1405 break;
1406 default:
1407 hotlist_state.readonly = 1;
1408 hotlist_state.file_error = 1;
1409 SKIP_TO_EOL;
1410 break;
1414 static void
1415 clean_up_hotlist_groups (const char *section)
1417 char *grp_section;
1418 gchar **profile_keys, **keys;
1419 gsize len;
1421 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1422 if (mc_config_has_group(mc_main_config, section))
1423 mc_config_del_group (mc_main_config, section);
1425 if (mc_config_has_group (mc_main_config, grp_section)) {
1426 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section,&len);
1428 while (*profile_keys) {
1429 clean_up_hotlist_groups (*profile_keys);
1430 profile_keys++;
1432 g_strfreev(keys);
1433 mc_config_del_group (mc_main_config, grp_section);
1435 g_free (grp_section);
1440 static void
1441 load_hotlist (void)
1443 int remove_old_list = 0;
1444 struct stat stat_buf;
1446 if (hotlist_state.loaded) {
1447 stat (hotlist_file_name, &stat_buf);
1448 if (hotlist_file_mtime < stat_buf.st_mtime)
1449 done_hotlist ();
1450 else
1451 return;
1454 if (!hotlist_file_name)
1455 hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL);
1457 hotlist = new_hotlist ();
1458 hotlist->type = HL_TYPE_GROUP;
1459 hotlist->label = g_strdup (_(" Top level group "));
1460 hotlist->up = hotlist;
1462 * compatibility :-(
1464 hotlist->directory = g_strdup ("Hotlist");
1466 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1467 int result;
1469 load_group (hotlist);
1470 hotlist_state.loaded = 1;
1472 * just to be sure we got copy
1474 hotlist_state.modified = 1;
1475 result = save_hotlist ();
1476 hotlist_state.modified = 0;
1477 if (result) {
1478 remove_old_list = 1;
1479 } else {
1480 message (D_ERROR, _(" Hotlist Load "),
1481 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1482 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1484 } else {
1485 hot_load_file (hotlist);
1486 fclose (hotlist_file);
1487 hotlist_state.loaded = 1;
1490 if (remove_old_list) {
1491 GError *error = NULL;
1492 clean_up_hotlist_groups ("Hotlist");
1493 if (! mc_config_save_file (mc_main_config, &error))
1494 setup_save_config_show_error(mc_main_config->ini_path, &error);
1497 stat (hotlist_file_name, &stat_buf);
1498 hotlist_file_mtime = stat_buf.st_mtime;
1499 current_group = hotlist;
1503 static int list_level = 0;
1505 static void
1506 hot_save_group (struct hotlist *grp)
1508 struct hotlist *current = grp->head;
1509 int i;
1510 char *s;
1512 #define INDENT(n) \
1513 do { \
1514 for (i = 0; i < n; i++) \
1515 putc (' ', hotlist_file); \
1516 } while (0)
1518 for (;current; current = current->next)
1519 switch (current->type) {
1520 case HL_TYPE_GROUP:
1521 INDENT (list_level);
1522 fputs ("GROUP \"", hotlist_file);
1523 for (s = current->label; *s; s++) {
1524 if (*s == '"' || *s == '\\')
1525 putc ('\\', hotlist_file);
1526 putc (*s, hotlist_file);
1528 fputs ("\"\n", hotlist_file);
1529 list_level += 2;
1530 hot_save_group (current);
1531 list_level -= 2;
1532 INDENT (list_level);
1533 fputs ("ENDGROUP\n", hotlist_file);
1534 break;
1535 case HL_TYPE_ENTRY:
1536 INDENT(list_level);
1537 fputs ("ENTRY \"", hotlist_file);
1538 for (s = current->label; *s; s++) {
1539 if (*s == '"' || *s == '\\')
1540 putc ('\\', hotlist_file);
1541 putc (*s, hotlist_file);
1543 fputs ("\" URL \"", hotlist_file);
1544 for (s = current->directory; *s; s++) {
1545 if (*s == '"' || *s == '\\')
1546 putc ('\\', hotlist_file);
1547 putc (*s, hotlist_file);
1549 fputs ("\"\n", hotlist_file);
1550 break;
1551 case HL_TYPE_COMMENT:
1552 fprintf (hotlist_file, "#%s\n", current->label);
1553 break;
1554 case HL_TYPE_DOTDOT:
1555 /* do nothing */
1556 break;
1560 int save_hotlist (void)
1562 int saved = 0;
1563 struct stat stat_buf;
1565 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1566 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1568 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1569 hot_save_group (hotlist);
1570 fclose (hotlist_file);
1571 stat (hotlist_file_name, &stat_buf);
1572 hotlist_file_mtime = stat_buf.st_mtime;
1573 saved = 1;
1574 hotlist_state.modified = 0;
1575 } else
1576 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1579 return saved;
1583 * Unload list from memory.
1584 * Don't confuse with hotlist_done() for GUI.
1586 void done_hotlist (void)
1588 if (hotlist){
1589 remove_group (hotlist);
1590 g_free (hotlist->label);
1591 g_free (hotlist->directory);
1592 g_free (hotlist);
1593 hotlist = 0;
1596 hotlist_state.loaded = 0;
1598 g_free (hotlist_file_name);
1599 hotlist_file_name = 0;
1600 l_hotlist = 0;
1601 current_group = 0;
1603 if (tkn_buf){
1604 g_string_free (tkn_buf, TRUE);
1605 tkn_buf = NULL;
1609 static void
1610 add_dotdot_to_list (void)
1612 if (current_group != hotlist) {
1613 if (hotlist_has_dot_dot != 0)
1614 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, 0);