Add $ and ` for escaping and reorder it according to the ascii values
[midnight-commander.git] / src / hotlist.c
blob90678fe001f91a6029606ac927a5b3b62505c9b6
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 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 #include <config.h>
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "global.h"
41 #include "tty.h" /* COLS */
42 #include "color.h" /* dialog_colors */
43 #include "dialog.h"
44 #include "widget.h"
45 #include "setup.h" /* For profile_bname */
46 #include "profile.h" /* Load/save directories hotlist */
47 #include "wtools.h" /* QuickDialog */
48 #include "panel.h" /* current_panel */
49 #include "main.h" /* repaint_screen */
50 #include "hotlist.h"
51 #include "key.h" /* KEY_M_CTRL */
52 #include "command.h" /* cmdline */
53 #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
54 #include "history.h"
56 #define UX 5
57 #define UY 2
59 #define BX UX
60 #define BY (LINES - 6)
62 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
63 #define LABELS 3
64 #define B_ADD_CURRENT B_USER
65 #define B_REMOVE (B_USER + 1)
66 #define B_NEW_GROUP (B_USER + 2)
67 #define B_NEW_ENTRY (B_USER + 3)
68 #define B_UP_GROUP (B_USER + 4)
69 #define B_INSERT (B_USER + 5)
70 #define B_APPEND (B_USER + 6)
71 #define B_MOVE (B_USER + 7)
73 #ifdef USE_VFS
74 #include "../vfs/gc.h"
75 #define B_FREE_ALL_VFS (B_USER + 8)
76 #define B_REFRESH_VFS (B_USER + 9)
77 #endif
79 int hotlist_has_dot_dot = 1;
81 static WListbox *l_hotlist;
82 static WListbox *l_movelist;
84 static Dlg_head *hotlist_dlg;
85 static Dlg_head *movelist_dlg;
87 static WLabel *pname, *pname_group, *movelist_group;
89 enum HotListType {
90 HL_TYPE_GROUP,
91 HL_TYPE_ENTRY,
92 HL_TYPE_COMMENT,
93 HL_TYPE_DOTDOT
96 static struct {
98 * these parameters are intended to be user configurable
100 int expanded; /* expanded view of all groups at startup */
103 * these reflect run time state
106 int loaded; /* hotlist is loaded */
107 int readonly; /* hotlist readonly */
108 int file_error; /* parse error while reading file */
109 int running; /* we are running dlg (and have to
110 update listbox */
111 int moving; /* we are in moving hotlist currently */
112 int modified; /* hotlist was modified */
113 int type; /* LIST_HOTLIST || LIST_VFSLIST */
114 } hotlist_state;
116 static struct _hotlist_but {
117 int ret_cmd, flags, y, x;
118 const char *text;
119 int type;
120 } hotlist_but[] = {
121 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), LIST_HOTLIST},
122 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"), LIST_HOTLIST},
123 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"), LIST_MOVELIST},
124 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"), LIST_MOVELIST},
125 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &Entry"), LIST_HOTLIST},
126 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &Group"), LIST_HOTLIST},
127 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"), LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
128 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"), LIST_HOTLIST|LIST_MOVELIST},
129 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"), LIST_HOTLIST},
130 #ifdef USE_VFS
131 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"), LIST_VFSLIST},
132 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"), LIST_VFSLIST},
133 #endif
134 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &To"), LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
137 /* Directory hotlist */
138 static struct hotlist{
139 enum HotListType type;
140 char *directory;
141 char *label;
142 struct hotlist *head;
143 struct hotlist *up;
144 struct hotlist *next;
145 } *hotlist = NULL;
147 static struct hotlist *current_group;
149 static void init_movelist (int, struct hotlist *);
150 static void add_new_group_cmd (void);
151 static void add_new_entry_cmd (void);
152 static void remove_from_hotlist (struct hotlist *entry);
153 static void load_hotlist (void);
154 static void add_dotdot_to_list (void);
156 #define new_hotlist() g_new0(struct hotlist, 1)
158 static void
159 hotlist_refresh (Dlg_head * dlg)
161 common_dialog_repaint (dlg);
162 attrset (COLOR_NORMAL);
163 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10),
164 dlg->cols - (UX * 2));
165 if (!hotlist_state.moving)
166 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2));
169 /* If current->data is 0, then we are dealing with a VFS pathname */
170 static inline void
171 update_path_name (void)
173 const char *text = "";
174 char *p;
175 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
176 Dlg_head *dlg = list->widget.parent;
178 if (list->current) {
179 if (list->current->data != 0) {
180 struct hotlist *hlp = (struct hotlist *) list->current->data;
182 if (hlp->type == HL_TYPE_ENTRY ||
183 hlp->type == HL_TYPE_DOTDOT)
184 text = hlp->directory;
185 else if (hlp->type == HL_TYPE_GROUP)
186 text = _("Subgroup - press ENTER to see list");
187 } else {
188 text = list->current->text;
191 if (!hotlist_state.moving)
192 label_set_text (pname,
193 name_trunc (text, dlg->cols - (UX * 2 + 4)));
195 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
196 if (!hotlist_state.moving)
197 label_set_text (pname_group,
198 name_trunc (p, dlg->cols - (UX * 2 + 4)));
199 else
200 label_set_text (movelist_group,
201 name_trunc (p, dlg->cols - (UX * 2 + 4)));
202 g_free (p);
204 dlg_redraw (dlg);
207 #define CHECK_BUFFER \
208 do { \
209 int i; \
211 if ((i = strlen (current->label) + 3) > buflen) { \
212 g_free (buf); \
213 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
215 buf[0] = '\0'; \
216 } while (0)
218 static void fill_listbox (void)
220 struct hotlist *current = current_group->head;
221 static char *buf;
222 static int buflen;
224 if (!buf)
225 buf = g_malloc (buflen = 1024);
226 buf[0] = '\0';
228 while (current){
229 switch (current->type) {
230 case HL_TYPE_GROUP:
232 CHECK_BUFFER;
233 strcat (strcat (buf, "->"), current->label);
234 if (hotlist_state.moving)
235 listbox_add_item (l_movelist, 0, 0, buf, current);
236 else
237 listbox_add_item (l_hotlist, 0, 0, buf, current);
239 break;
240 case HL_TYPE_DOTDOT:
241 case HL_TYPE_ENTRY:
242 if (hotlist_state.moving)
243 listbox_add_item (l_movelist, 0, 0, current->label, current);
244 else
245 listbox_add_item (l_hotlist, 0, 0, current->label, current);
246 break;
247 default:
248 break;
250 current = current->next;
254 #undef CHECK_BUFFER
256 static void
257 unlink_entry (struct hotlist *entry)
259 struct hotlist *current = current_group->head;
261 if (current == entry)
262 current_group->head = entry->next;
263 else {
264 while (current && current->next != entry)
265 current = current->next;
266 if (current)
267 current->next = entry->next;
269 entry->next =
270 entry->up = 0;
273 #ifdef USE_VFS
274 static void add_name_to_list (const char *path)
276 listbox_add_item (l_hotlist, 0, 0, path, 0);
278 #endif /* !USE_VFS */
280 static int
281 hotlist_button_callback (int action)
283 switch (action) {
284 case B_MOVE:
286 struct hotlist *saved = current_group;
287 struct hotlist *item;
288 struct hotlist *moveto_item = 0;
289 struct hotlist *moveto_group = 0;
290 int ret;
292 if (!l_hotlist->current)
293 return MSG_NOT_HANDLED; /* empty group - nothing to do */
294 item = l_hotlist->current->data;
295 hotlist_state.moving = 1;
296 init_movelist (LIST_MOVELIST, item);
297 run_dlg (movelist_dlg);
298 ret = movelist_dlg->ret_value;
299 hotlist_state.moving = 0;
300 if (l_movelist->current)
301 moveto_item = l_movelist->current->data;
302 moveto_group = current_group;
303 destroy_dlg (movelist_dlg);
304 current_group = saved;
305 if (ret == B_CANCEL)
306 return MSG_NOT_HANDLED;
307 if (moveto_item == item)
308 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
309 it hardly changes anything ;) */
310 unlink_entry (item);
311 listbox_remove_current (l_hotlist, 1);
312 item->up = moveto_group;
313 if (!moveto_group->head)
314 moveto_group->head = item;
315 else if (!moveto_item) { /* we have group with just comments */
316 struct hotlist *p = moveto_group->head;
318 /* skip comments */
319 while (p->next)
320 p = p->next;
321 p->next = item;
322 } else if (ret == B_ENTER || ret == B_APPEND)
323 if (!moveto_item->next)
324 moveto_item->next = item;
325 else {
326 item->next = moveto_item->next;
327 moveto_item->next = item;
328 } else if (moveto_group->head == moveto_item) {
329 moveto_group->head = item;
330 item->next = moveto_item;
331 } else {
332 struct hotlist *p = moveto_group->head;
334 while (p->next != moveto_item)
335 p = p->next;
336 item->next = p->next;
337 p->next = item;
339 listbox_remove_list (l_hotlist);
340 fill_listbox ();
341 repaint_screen ();
342 hotlist_state.modified = 1;
343 return MSG_NOT_HANDLED;
344 break;
346 case B_REMOVE:
347 if (l_hotlist->current && l_hotlist->current->data)
348 remove_from_hotlist (l_hotlist->current->data);
349 return MSG_NOT_HANDLED;
350 break;
352 case B_NEW_GROUP:
353 add_new_group_cmd ();
354 return MSG_NOT_HANDLED;
355 break;
357 case B_ADD_CURRENT:
358 add2hotlist_cmd ();
359 return MSG_NOT_HANDLED;
360 break;
362 case B_NEW_ENTRY:
363 add_new_entry_cmd ();
364 return MSG_NOT_HANDLED;
365 break;
367 case B_ENTER:
369 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
370 if (list->current) {
371 if (list->current->data) {
372 struct hotlist *hlp =
373 (struct hotlist *) list->current->data;
374 if (hlp->type == HL_TYPE_ENTRY)
375 return MSG_HANDLED;
376 else if (hlp->type == HL_TYPE_DOTDOT) {
377 /* Fall through - go up */
380 else {
381 listbox_remove_list (list);
382 current_group = hlp;
383 fill_listbox ();
384 return MSG_NOT_HANDLED;
386 } else
387 return MSG_HANDLED;
390 /* Fall through if list empty - just go up */
392 case B_UP_GROUP:
394 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
395 listbox_remove_list (list);
396 current_group = current_group->up;
397 fill_listbox ();
398 return MSG_NOT_HANDLED;
399 break;
402 #ifdef USE_VFS
403 case B_FREE_ALL_VFS:
404 vfs_expire (1);
405 /* fall through */
407 case B_REFRESH_VFS:
408 listbox_remove_list (l_hotlist);
409 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
410 vfs_fill_names (add_name_to_list);
411 return MSG_NOT_HANDLED;
412 #endif /* USE_VFS */
414 default:
415 return MSG_HANDLED;
416 break;
421 static cb_ret_t
422 hotlist_callback (Dlg_head *h, dlg_msg_t msg, int parm)
424 switch (msg) {
425 case DLG_DRAW:
426 hotlist_refresh (h);
427 return MSG_HANDLED;
429 case DLG_UNHANDLED_KEY:
430 switch (parm) {
431 case KEY_M_CTRL | '\n':
432 goto l1;
433 case '\n':
434 case KEY_ENTER:
435 case KEY_RIGHT:
436 if (hotlist_button_callback (B_ENTER)) {
437 h->ret_value = B_ENTER;
438 dlg_stop (h);
440 return MSG_HANDLED;
441 break;
442 case KEY_LEFT:
443 if (hotlist_state.type != LIST_VFSLIST)
444 return !hotlist_button_callback (B_UP_GROUP);
445 else
446 return MSG_NOT_HANDLED;
447 break;
448 case KEY_DC:
449 if (!hotlist_state.moving) {
450 hotlist_button_callback (B_REMOVE);
451 return MSG_HANDLED;
453 break;
455 case ALT ('\n'):
456 case ALT ('\r'):
457 if (!hotlist_state.moving) {
458 if (l_hotlist->current) {
459 if (l_hotlist->current->data) {
460 struct hotlist *hlp =
461 (struct hotlist *) l_hotlist->current->data;
462 if (hlp->type == HL_TYPE_ENTRY) {
463 char *tmp =
464 g_strconcat ("cd ", hlp->directory, (char *) NULL);
465 stuff (cmdline, tmp, 0);
466 g_free (tmp);
467 dlg_stop (h);
468 h->ret_value = B_CANCEL;
469 return MSG_HANDLED;
474 return MSG_HANDLED; /* ignore key */
476 return MSG_NOT_HANDLED;
478 case DLG_POST_KEY:
479 if (hotlist_state.moving)
480 dlg_select_widget (l_movelist);
481 else
482 dlg_select_widget (l_hotlist);
483 /* always stay on hotlist */
484 /* fall through */
486 case DLG_INIT:
487 attrset (MENU_ENTRY_COLOR);
488 update_path_name ();
489 return MSG_HANDLED;
491 default:
492 return default_dlg_callback (h, msg, parm);
496 static int l_call (WListbox *list)
498 Dlg_head *dlg = list->widget.parent;
500 if (list->current){
501 if (list->current->data) {
502 struct hotlist *hlp = (struct hotlist*) list->current->data;
503 if (hlp->type == HL_TYPE_ENTRY) {
504 dlg->ret_value = B_ENTER;
505 dlg_stop (dlg);
506 return LISTBOX_DONE;
507 } else {
508 hotlist_button_callback (B_ENTER);
509 hotlist_callback (dlg, DLG_POST_KEY, '\n');
510 return LISTBOX_CONT;
512 } else {
513 dlg->ret_value = B_ENTER;
514 dlg_stop (dlg);
515 return LISTBOX_DONE;
519 hotlist_button_callback (B_UP_GROUP);
520 hotlist_callback (dlg, DLG_POST_KEY, 'u');
521 return LISTBOX_CONT;
525 * Expands all button names (once) and recalculates button positions.
526 * returns number of columns in the dialog box, which is 10 chars longer
527 * then buttonbar.
529 * If common width of the window (i.e. in xterm) is less than returned
530 * width - sorry :) (anyway this did not handled in previous version too)
532 static int
533 init_i18n_stuff(int list_type, int cols)
535 register int i;
536 static const char* cancel_but = N_("&Cancel");
538 #ifdef ENABLE_NLS
539 static int hotlist_i18n_flag = 0;
541 if (!hotlist_i18n_flag)
543 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
544 while (i--)
545 hotlist_but [i].text = _(hotlist_but [i].text);
547 cancel_but = _(cancel_but);
548 hotlist_i18n_flag = 1;
550 #endif /* ENABLE_NLS */
552 /* Dynamic resizing of buttonbars */
554 int len[2], count[2]; /* at most two lines of buttons */
555 int cur_x[2], row;
557 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
558 len[0] = len[1] = count[0] = count[1] = 0;
560 /* Count len of buttonbars, assuming 2 extra space between buttons */
561 while (i--)
563 if (! (hotlist_but[i].type & list_type))
564 continue;
566 row = hotlist_but [i].y;
567 ++count [row];
568 len [row] += strlen (hotlist_but [i].text) + 5;
569 if (hotlist_but [i].flags == DEFPUSH_BUTTON)
570 len [row] += 2;
572 len[0] -= 2;
573 len[1] -= 2;
575 cols = max(cols, max(len[0], len[1]));
577 /* arrange buttons */
579 cur_x[0] = cur_x[1] = 0;
580 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
581 while (i--)
583 if (! (hotlist_but[i].type & list_type))
584 continue;
586 row = hotlist_but [i].y;
588 if (hotlist_but [i].x != 0)
590 /* not first int the row */
591 if (!strcmp (hotlist_but [i].text, cancel_but))
592 hotlist_but [i].x =
593 cols - strlen (hotlist_but [i].text) - 13;
594 else
595 hotlist_but [i].x = cur_x [row];
598 cur_x [row] += strlen (hotlist_but [i].text) + 2
599 + (hotlist_but [i].flags == DEFPUSH_BUTTON ? 5 : 3);
603 return cols;
606 static void
607 init_hotlist (int list_type)
609 size_t i;
610 const char *title, *help_node;
611 int hotlist_cols;
613 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
615 do_refresh ();
617 hotlist_state.expanded =
618 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
619 0, profile_name);
621 if (list_type == LIST_VFSLIST) {
622 title = _("Active VFS directories");
623 help_node = "[vfshot]"; /* FIXME - no such node */
624 } else {
625 title = _("Directory hotlist");
626 help_node = "[Hotlist]";
629 hotlist_dlg =
630 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
631 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
633 for (i = 0; i < BUTTONS; i++) {
634 if (hotlist_but[i].type & list_type)
635 add_widget (hotlist_dlg,
636 button_new (BY + hotlist_but[i].y,
637 BX + hotlist_but[i].x,
638 hotlist_but[i].ret_cmd,
639 hotlist_but[i].flags,
640 hotlist_but[i].text,
641 hotlist_button_callback));
644 /* We add the labels.
645 * pname will hold entry's pathname;
646 * pname_group will hold name of current group
648 pname = label_new (UY - 11 + LINES, UX + 2, "");
649 add_widget (hotlist_dlg, pname);
650 if (!hotlist_state.moving) {
651 add_widget (hotlist_dlg,
652 label_new (UY - 12 + LINES, UX + 1,
653 _(" Directory path ")));
655 /* This one holds the displayed pathname */
656 pname_group = label_new (UY, UX + 1, _(" Directory label "));
657 add_widget (hotlist_dlg, pname_group);
659 /* get new listbox */
660 l_hotlist =
661 listbox_new (UY + 1, UX + 1, COLS - 2 * UX - 8, LINES - 14,
662 l_call);
664 /* Fill the hotlist with the active VFS or the hotlist */
665 #ifdef USE_VFS
666 if (list_type == LIST_VFSLIST) {
667 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
668 vfs_fill_names (add_name_to_list);
669 } else
670 #endif /* !USE_VFS */
671 fill_listbox ();
673 add_widget (hotlist_dlg, l_hotlist);
674 /* add listbox to the dialogs */
677 static void
678 init_movelist (int list_type, struct hotlist *item)
680 size_t i;
681 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
682 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
684 do_refresh ();
686 movelist_dlg =
687 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
688 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
689 g_free (hdr);
691 for (i = 0; i < BUTTONS; i++) {
692 if (hotlist_but[i].type & list_type)
693 add_widget (movelist_dlg,
694 button_new (BY - 4 + hotlist_but[i].y,
695 BX + hotlist_but[i].x,
696 hotlist_but[i].ret_cmd,
697 hotlist_but[i].flags,
698 hotlist_but[i].text,
699 hotlist_button_callback));
702 /* We add the labels. We are interested in the last one,
703 * that one will hold the path name label
705 movelist_group = label_new (UY, UX + 1, _(" Directory label "));
706 add_widget (movelist_dlg, movelist_group);
707 /* get new listbox */
708 l_movelist =
709 listbox_new (UY + 1, UX + 1, movelist_dlg->cols - 2 * UX - 2,
710 movelist_dlg->lines - 8, l_call);
712 fill_listbox ();
714 add_widget (movelist_dlg, l_movelist);
715 /* add listbox to the dialogs */
719 * Destroy the list dialog.
720 * Don't confuse with done_hotlist() for the list in memory.
722 static void hotlist_done (void)
724 destroy_dlg (hotlist_dlg);
725 l_hotlist = NULL;
726 if (0)
727 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
728 repaint_screen ();
731 static inline char *
732 find_group_section (struct hotlist *grp)
734 return g_strconcat (grp->directory, ".Group", (char *) NULL);
738 /* 1.11.96 bor: added pos parameter to control placement of new item.
739 see widget.c, listbox_add_item()
740 now hotlist is in unsorted mode
742 enum {
743 HL_BEFORE_CURRENT = 1
744 ,HL_AFTER_CURRENT = 2
747 static struct hotlist *
748 add2hotlist (char *label, char *directory, enum HotListType type, int pos)
750 struct hotlist *new;
751 struct hotlist *current = NULL;
754 * Hotlist is neither loaded nor loading.
755 * Must be called by "Ctrl-x a" before using hotlist.
757 if (!current_group)
758 load_hotlist ();
760 if (l_hotlist && l_hotlist->current) {
761 current = l_hotlist->current->data;
763 /* Make sure `..' stays at the top of the list. */
764 if (current->type == HL_TYPE_DOTDOT)
765 pos = HL_AFTER_CURRENT;
768 new = new_hotlist ();
770 new->type = type;
771 new->label = label;
772 new->directory = directory;
773 new->up = current_group;
775 if (type == HL_TYPE_GROUP) {
776 current_group = new;
777 add_dotdot_to_list ();
778 current_group = new->up;
781 if (!current_group->head) { /* first element in group */
782 current_group->head = new;
783 } else if (pos == HL_AFTER_CURRENT) {
784 new->next = current->next;
785 current->next = new;
786 } else if (pos == HL_BEFORE_CURRENT &&
787 current == current_group->head) {
788 /* should be inserted before first item */
789 new->next = current;
790 current_group->head = new;
791 } else if (pos == HL_BEFORE_CURRENT) {
792 struct hotlist *p = current_group->head;
794 while (p->next != current)
795 p = p->next;
797 new->next = current;
798 p->next = new;
799 } else { /* append at the end */
800 struct hotlist *p = current_group->head;
802 while (p->next)
803 p = p->next;
805 p->next = new;
808 if (hotlist_state.running && type != HL_TYPE_COMMENT &&
809 type != HL_TYPE_DOTDOT) {
810 if (type == HL_TYPE_GROUP) {
811 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
813 listbox_add_item (l_hotlist, pos, 0, lbl, new);
814 g_free (lbl);
815 } else
816 listbox_add_item (l_hotlist, pos, 0, new->label, new);
817 listbox_select_entry (l_hotlist, l_hotlist->current);
819 return new;
823 #ifdef ENABLE_NLS
825 * Support routine for add_new_entry_input()/add_new_group_input()
826 * Change positions of buttons (first three widgets).
828 * This is just a quick hack. Accurate procedure must take care of
829 * internationalized label lengths and total buttonbar length...assume
830 * 64 is longer anyway.
832 static void add_widgets_i18n(QuickWidget* qw, int len)
834 int i, l[3], space, cur_x;
836 for (i = 0; i < 3; i++)
838 qw [i].text = _(qw [i].text);
839 l[i] = strlen (qw [i].text) + 3;
841 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
843 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
845 qw [i].relative_x = cur_x;
846 qw [i].x_divisions = len;
849 #endif /* ENABLE_NLS */
851 static int
852 add_new_entry_input (const char *header, const char *text1, const char *text2,
853 const char *help, char **r1, char **r2)
855 #define RELATIVE_Y_BUTTONS 4
856 #define RELATIVE_Y_LABEL_PTH 3
857 #define RELATIVE_Y_INPUT_PTH 4
859 QuickDialog Quick_input;
860 static QuickWidget quick_widgets [] = {
861 { quick_button, 55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), 0, B_CANCEL,
862 0, 0, NULL },
863 { quick_button, 30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), 0, B_INSERT,
864 0, 0, NULL },
865 { quick_button, 10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), 0, B_APPEND,
866 0, 0, NULL },
867 { quick_input, 4, 80, RELATIVE_Y_INPUT_PTH, 0, "",58, 0,
868 0, 0, "input-pth" },
869 { quick_label, RELATIVE_Y_LABEL_PTH, 80, 3, 0, 0, 0, 0,
870 0, 0, NULL },
871 { quick_input, 4, 80, 3, 0, "", 58, 0,
872 0, 0, "input-lbl" },
873 { quick_label, 3, 80, 2, 0, 0, 0, 0,
874 0, 0, NULL },
875 NULL_QuickWidget };
877 int len;
878 int i;
879 int lines1, lines2;
880 int cols1, cols2;
882 #ifdef ENABLE_NLS
883 static int i18n_flag = 0;
884 #endif /* ENABLE_NLS */
886 msglen(text1, &lines1, &cols1);
887 msglen(text2, &lines2, &cols2);
888 len = max ((int) strlen (header), cols1);
889 len = max (len, cols2) + 4;
890 len = max (len, 64);
892 #ifdef ENABLE_NLS
893 if (!i18n_flag)
895 add_widgets_i18n(quick_widgets, len);
896 i18n_flag = 1;
898 #endif /* ENABLE_NLS */
900 Quick_input.xlen = len;
901 Quick_input.xpos = -1;
902 Quick_input.title = header;
903 Quick_input.help = help;
904 Quick_input.i18n = 0;
905 quick_widgets [6].text = text1;
906 quick_widgets [4].text = text2;
907 quick_widgets [5].text = *r1;
908 quick_widgets [3].text = *r2;
910 for (i = 0; i < 7; i++)
911 quick_widgets [i].y_divisions = lines1+lines2+7;
912 Quick_input.ylen = lines1 + lines2 + 7;
914 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
915 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
916 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
917 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
918 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
920 quick_widgets [5].str_result = r1;
921 quick_widgets [3].str_result = r2;
923 Quick_input.widgets = quick_widgets;
924 if ((i = quick_dialog (&Quick_input)) != B_CANCEL){
925 return i;
926 } else
927 return 0;
930 static void add_new_entry_cmd (void)
932 char *title, *url, *to_free;
933 int ret;
935 /* Take current directory as default value for input fields */
936 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
938 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"),
939 _("Directory path"), "[Hotlist]", &title, &url);
940 g_free (to_free);
942 if (!ret)
943 return;
944 if (!title || !*title || !url || !*url) {
945 g_free (title);
946 g_free (url);
947 return;
950 if (ret == B_ENTER || ret == B_APPEND)
951 add2hotlist (title, url, HL_TYPE_ENTRY, HL_AFTER_CURRENT);
952 else
953 add2hotlist (title, url, HL_TYPE_ENTRY, HL_BEFORE_CURRENT);
955 hotlist_state.modified = 1;
958 static int
959 add_new_group_input (const char *header, const char *label, char **result)
961 int ret;
962 QuickDialog Quick_input;
963 static QuickWidget quick_widgets [] = {
964 { quick_button, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
965 NULL },
966 { quick_button, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT, 0, 0,
967 NULL },
968 { quick_button, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND, 0, 0,
969 NULL },
970 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
971 { quick_label, 3, 80, 2, 0, 0, 0, 0, 0, 0, NULL },
972 NULL_QuickWidget };
973 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
974 quick_widgets variable above */
975 int len;
976 int i;
977 int lines, cols;
979 #ifdef ENABLE_NLS
980 static int i18n_flag = 0;
981 #endif /* ENABLE_NLS */
983 msglen (label, &lines, &cols);
984 len = max ((int) strlen (header), cols) + 4;
985 len = max (len, 64);
987 #ifdef ENABLE_NLS
988 if (!i18n_flag)
990 add_widgets_i18n(quick_widgets, len);
991 i18n_flag = 1;
993 #endif /* ENABLE_NLS */
995 Quick_input.xlen = len;
996 Quick_input.xpos = -1;
997 Quick_input.title = header;
998 Quick_input.help = "[Hotlist]";
999 Quick_input.i18n = 0;
1000 quick_widgets [4].text = label;
1002 for (i = 0; i < 5; i++)
1003 quick_widgets [i].y_divisions = lines+6;
1004 Quick_input.ylen = lines + 6;
1006 for (i = 0; i < 4; i++)
1007 quick_widgets [i].relative_y = relative_y[i] + 2 + lines;
1009 quick_widgets [3].str_result = result;
1010 quick_widgets [3].text = "";
1012 Quick_input.widgets = quick_widgets;
1013 if ((ret = quick_dialog (&Quick_input)) != B_CANCEL){
1014 return ret;
1015 } else
1016 return 0;
1019 static void add_new_group_cmd (void)
1021 char *label;
1022 int ret;
1024 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
1025 if (!ret || !label || !*label)
1026 return;
1028 if (ret == B_ENTER || ret == B_APPEND)
1029 add2hotlist (label, 0, HL_TYPE_GROUP, HL_AFTER_CURRENT);
1030 else
1031 add2hotlist (label, 0, HL_TYPE_GROUP, HL_BEFORE_CURRENT);
1033 hotlist_state.modified = 1;
1036 void add2hotlist_cmd (void)
1038 char *prompt, *label;
1039 const char *cp = _("Label for \"%s\":");
1040 int l = strlen (cp);
1041 char *label_string = g_strdup (current_panel->cwd);
1043 strip_password (label_string, 1);
1045 prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS-2*UX-(l+8)));
1046 label = input_dialog (_(" Add to hotlist "), prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1047 g_free (prompt);
1049 if (!label || !*label) {
1050 g_free (label_string);
1051 g_free (label);
1052 return;
1054 add2hotlist (label, label_string, HL_TYPE_ENTRY, 0);
1055 hotlist_state.modified = 1;
1058 static void remove_group (struct hotlist *grp)
1060 struct hotlist *current = grp->head;
1062 while (current) {
1063 struct hotlist *next = current->next;
1065 if (current->type == HL_TYPE_GROUP)
1066 remove_group (current);
1068 g_free (current->label);
1069 g_free (current->directory);
1070 g_free (current);
1072 current = next;
1077 static void remove_from_hotlist (struct hotlist *entry)
1079 if (entry->type == HL_TYPE_DOTDOT)
1080 return;
1082 if (confirm_directory_hotlist_delete) {
1083 char *title;
1084 int result;
1086 title = g_strconcat (_(" Remove: "),
1087 name_trunc (entry->label, 30),
1088 " ",
1089 NULL);
1091 if (safe_delete)
1092 query_set_sel (1);
1093 result = query_dialog (title,
1094 _("\n Are you sure you want to remove this entry?"),
1095 D_ERROR, 2, _("&Yes"), _("&No"));
1097 g_free (title);
1099 if (result != 0)
1100 return;
1103 if (entry->type == HL_TYPE_GROUP) {
1104 if (entry->head) {
1105 char *header;
1106 int result;
1108 header = g_strconcat (_(" Remove: "),
1109 name_trunc (entry->label, 30),
1110 " ",
1111 NULL);
1112 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1113 D_ERROR, 2,
1114 _("&Yes"), _("&No"));
1115 g_free (header);
1117 if (result != 0)
1118 return;
1121 remove_group (entry);
1124 unlink_entry (entry);
1126 g_free (entry->label);
1127 g_free (entry->directory);
1128 g_free (entry);
1129 /* now remove list entry from screen */
1130 listbox_remove_current (l_hotlist, 1);
1131 hotlist_state.modified = 1;
1134 char *hotlist_cmd (int vfs_or_hotlist)
1136 char *target = NULL;
1138 hotlist_state.type = vfs_or_hotlist;
1139 load_hotlist ();
1141 init_hotlist (vfs_or_hotlist);
1143 /* display file info */
1144 attrset (SELECTED_COLOR);
1146 hotlist_state.running = 1;
1147 run_dlg (hotlist_dlg);
1148 hotlist_state.running = 0;
1149 save_hotlist ();
1151 switch (hotlist_dlg->ret_value) {
1152 case B_CANCEL:
1153 break;
1155 case B_ENTER:
1156 if (l_hotlist->current->data) {
1157 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
1158 target = g_strdup (hlp->directory);
1159 } else
1160 target = g_strdup (l_hotlist->current->text);
1161 break;
1164 hotlist_done ();
1165 return target;
1168 static void
1169 load_group (struct hotlist *grp)
1171 void *profile_keys;
1172 char *key, *value;
1173 char *group_section;
1174 struct hotlist *current = 0;
1176 group_section = find_group_section (grp);
1178 profile_keys = profile_init_iterator (group_section, profile_name);
1180 current_group = grp;
1182 while (profile_keys){
1183 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1184 add2hotlist (g_strdup (value), g_strdup (key), HL_TYPE_GROUP, 0);
1186 g_free (group_section);
1188 profile_keys = profile_init_iterator (grp->directory, profile_name);
1190 while (profile_keys){
1191 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1192 add2hotlist (g_strdup (value),g_strdup (key), HL_TYPE_ENTRY, 0);
1195 for (current = grp->head; current; current = current->next)
1196 load_group (current);
1199 #define TKN_GROUP 0
1200 #define TKN_ENTRY 1
1201 #define TKN_STRING 2
1202 #define TKN_URL 3
1203 #define TKN_ENDGROUP 4
1204 #define TKN_COMMENT 5
1205 #define TKN_EOL 125
1206 #define TKN_EOF 126
1207 #define TKN_UNKNOWN 127
1209 static char *tkn_buf;
1210 static int tkn_buf_length;
1211 static int tkn_length;
1213 static char *hotlist_file_name;
1214 static FILE *hotlist_file;
1215 static time_t hotlist_file_mtime;
1217 static int hot_skip_blanks (void)
1219 int c;
1221 while ((c = getc (hotlist_file)) != EOF && c != '\n' && isspace (c))
1223 return c;
1227 static int hot_next_token (void)
1229 int c;
1231 #define CHECK_BUF() \
1232 do { \
1233 if (tkn_length == tkn_buf_length) \
1234 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1235 : ( g_malloc (tkn_buf_length = 1024)); \
1236 } while (0)
1238 tkn_length = 0;
1240 again:
1241 c = hot_skip_blanks ();
1242 switch (c) {
1243 case EOF:
1244 return TKN_EOF;
1245 break;
1246 case '\n':
1247 return TKN_EOL;
1248 break;
1249 case '#':
1250 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1251 if (c == EOF)
1252 return TKN_EOF;
1253 if (c != '\n') {
1254 CHECK_BUF();
1255 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1258 CHECK_BUF();
1259 tkn_buf[tkn_length] = '\0';
1260 return TKN_COMMENT;
1261 break;
1262 case '"':
1263 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1264 if (c == '\\')
1265 if ((c = getc (hotlist_file)) == EOF)
1266 return TKN_EOF;
1267 CHECK_BUF();
1268 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1270 if (c == EOF)
1271 return TKN_EOF;
1272 CHECK_BUF();
1273 tkn_buf[tkn_length] = '\0';
1274 return TKN_STRING;
1275 break;
1276 case '\\':
1277 if ((c = getc (hotlist_file)) == EOF)
1278 return TKN_EOF;
1279 if (c == '\n')
1280 goto again;
1282 /* fall through; it is taken as normal character */
1284 default:
1285 do {
1286 CHECK_BUF();
1287 tkn_buf[tkn_length++] = toupper(c);
1288 } while ((c = fgetc (hotlist_file)) != EOF && isalnum (c));
1289 if (c != EOF)
1290 ungetc (c, hotlist_file);
1291 CHECK_BUF();
1292 tkn_buf[tkn_length] = '\0';
1293 if (strncmp (tkn_buf, "GROUP", tkn_length) == 0)
1294 return TKN_GROUP;
1295 else if (strncmp (tkn_buf, "ENTRY", tkn_length) == 0)
1296 return TKN_ENTRY;
1297 else if (strncmp (tkn_buf, "ENDGROUP", tkn_length) == 0)
1298 return TKN_ENDGROUP;
1299 else if (strncmp (tkn_buf, "URL", tkn_length) == 0)
1300 return TKN_URL;
1301 else
1302 return TKN_UNKNOWN;
1303 break;
1307 #define SKIP_TO_EOL { \
1308 int _tkn; \
1309 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1312 #define CHECK_TOKEN(_TKN_) \
1313 if ((tkn = hot_next_token ()) != _TKN_) { \
1314 hotlist_state.readonly = 1; \
1315 hotlist_state.file_error = 1; \
1316 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1317 tkn = hot_next_token (); \
1318 break; \
1321 static void
1322 hot_load_group (struct hotlist * grp)
1324 int tkn;
1325 struct hotlist *new_grp;
1326 char *label, *url;
1328 current_group = grp;
1330 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1331 switch (tkn) {
1332 case TKN_GROUP:
1333 CHECK_TOKEN(TKN_STRING);
1334 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1335 SKIP_TO_EOL;
1336 hot_load_group (new_grp);
1337 current_group = grp;
1338 break;
1339 case TKN_ENTRY:
1340 CHECK_TOKEN(TKN_STRING);
1341 label = g_strdup (tkn_buf);
1342 CHECK_TOKEN(TKN_URL);
1343 CHECK_TOKEN(TKN_STRING);
1344 url = g_strdup (tkn_buf);
1345 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1346 SKIP_TO_EOL;
1347 break;
1348 case TKN_COMMENT:
1349 label = g_strdup (tkn_buf);
1350 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1351 break;
1352 case TKN_EOF:
1353 hotlist_state.readonly = 1;
1354 hotlist_state.file_error = 1;
1355 return;
1356 break;
1357 case TKN_EOL:
1358 /* skip empty lines */
1359 break;
1360 default:
1361 hotlist_state.readonly = 1;
1362 hotlist_state.file_error = 1;
1363 SKIP_TO_EOL;
1364 break;
1366 SKIP_TO_EOL;
1369 static void
1370 hot_load_file (struct hotlist * grp)
1372 int tkn;
1373 struct hotlist *new_grp;
1374 char *label, *url;
1376 current_group = grp;
1378 while ((tkn = hot_next_token())!= TKN_EOF)
1379 switch (tkn) {
1380 case TKN_GROUP:
1381 CHECK_TOKEN(TKN_STRING);
1382 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1383 SKIP_TO_EOL;
1384 hot_load_group (new_grp);
1385 current_group = grp;
1386 break;
1387 case TKN_ENTRY:
1388 CHECK_TOKEN(TKN_STRING);
1389 label = g_strdup (tkn_buf);
1390 CHECK_TOKEN(TKN_URL);
1391 CHECK_TOKEN(TKN_STRING);
1392 url = g_strdup (tkn_buf);
1393 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1394 SKIP_TO_EOL;
1395 break;
1396 case TKN_COMMENT:
1397 label = g_strdup (tkn_buf);
1398 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1399 break;
1400 case TKN_EOL:
1401 /* skip empty lines */
1402 break;
1403 default:
1404 hotlist_state.readonly = 1;
1405 hotlist_state.file_error = 1;
1406 SKIP_TO_EOL;
1407 break;
1411 static void
1412 clean_up_hotlist_groups (const char *section)
1414 char *grp_section;
1415 void *profile_keys;
1416 char *key, *value;
1418 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1419 if (profile_has_section (section, profile_name))
1420 profile_clean_section (section, profile_name);
1421 if (profile_has_section (grp_section, profile_name)) {
1422 profile_keys = profile_init_iterator (grp_section, profile_name);
1424 while (profile_keys) {
1425 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1426 clean_up_hotlist_groups (key);
1428 profile_clean_section (grp_section, profile_name);
1430 g_free (grp_section);
1435 static void
1436 load_hotlist (void)
1438 int remove_old_list = 0;
1439 struct stat stat_buf;
1441 if (hotlist_state.loaded) {
1442 stat (hotlist_file_name, &stat_buf);
1443 if (hotlist_file_mtime < stat_buf.st_mtime)
1444 done_hotlist ();
1445 else
1446 return;
1449 if (!hotlist_file_name)
1450 hotlist_file_name = concat_dir_and_file (home_dir, HOTLIST_FILENAME);
1452 hotlist = new_hotlist ();
1453 hotlist->type = HL_TYPE_GROUP;
1454 hotlist->label = g_strdup (_(" Top level group "));
1455 hotlist->up = hotlist;
1457 * compatibility :-(
1459 hotlist->directory = g_strdup ("Hotlist");
1461 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1462 int result;
1464 load_group (hotlist);
1465 hotlist_state.loaded = 1;
1467 * just to be sure we got copy
1469 hotlist_state.modified = 1;
1470 result = save_hotlist ();
1471 hotlist_state.modified = 0;
1472 if (result) {
1473 remove_old_list = 1;
1474 } else {
1475 message (D_ERROR, _(" Hotlist Load "),
1476 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1477 HOTLIST_FILENAME);
1479 } else {
1480 hot_load_file (hotlist);
1481 fclose (hotlist_file);
1482 hotlist_state.loaded = 1;
1485 if (remove_old_list) {
1486 clean_up_hotlist_groups ("Hotlist");
1487 sync_profiles ();
1490 stat (hotlist_file_name, &stat_buf);
1491 hotlist_file_mtime = stat_buf.st_mtime;
1492 current_group = hotlist;
1496 static int list_level = 0;
1498 static void
1499 hot_save_group (struct hotlist *grp)
1501 struct hotlist *current = grp->head;
1502 int i;
1503 char *s;
1505 #define INDENT(n) \
1506 do { \
1507 for (i = 0; i < n; i++) \
1508 putc (' ', hotlist_file); \
1509 } while (0)
1511 for (;current; current = current->next)
1512 switch (current->type) {
1513 case HL_TYPE_GROUP:
1514 INDENT (list_level);
1515 fputs ("GROUP \"", hotlist_file);
1516 for (s = current->label; *s; s++) {
1517 if (*s == '"' || *s == '\\')
1518 putc ('\\', hotlist_file);
1519 putc (*s, hotlist_file);
1521 fputs ("\"\n", hotlist_file);
1522 list_level += 2;
1523 hot_save_group (current);
1524 list_level -= 2;
1525 INDENT (list_level);
1526 fputs ("ENDGROUP\n", hotlist_file);
1527 break;
1528 case HL_TYPE_ENTRY:
1529 INDENT(list_level);
1530 fputs ("ENTRY \"", hotlist_file);
1531 for (s = current->label; *s; s++) {
1532 if (*s == '"' || *s == '\\')
1533 putc ('\\', hotlist_file);
1534 putc (*s, hotlist_file);
1536 fputs ("\" URL \"", hotlist_file);
1537 for (s = current->directory; *s; s++) {
1538 if (*s == '"' || *s == '\\')
1539 putc ('\\', hotlist_file);
1540 putc (*s, hotlist_file);
1542 fputs ("\"\n", hotlist_file);
1543 break;
1544 case HL_TYPE_COMMENT:
1545 fprintf (hotlist_file, "#%s\n", current->label);
1546 break;
1547 case HL_TYPE_DOTDOT:
1548 /* do nothing */
1549 break;
1553 int save_hotlist (void)
1555 int saved = 0;
1556 struct stat stat_buf;
1558 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1559 char *fbak = g_strconcat (hotlist_file_name, ".bak", (char *) NULL);
1561 rename (hotlist_file_name, fbak);
1562 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1563 if (stat (fbak, &stat_buf) == 0)
1564 chmod (hotlist_file_name, stat_buf.st_mode);
1565 else
1566 chmod (hotlist_file_name, S_IRUSR | S_IWUSR);
1567 hot_save_group (hotlist);
1568 fclose (hotlist_file);
1569 stat (hotlist_file_name, &stat_buf);
1570 hotlist_file_mtime = stat_buf.st_mtime;
1571 saved = 1;
1572 hotlist_state.modified = 0;
1573 } else
1574 rename (fbak, hotlist_file_name);
1575 g_free (fbak);
1578 return saved;
1582 * Unload list from memory.
1583 * Don't confuse with hotlist_done() for GUI.
1585 void done_hotlist (void)
1587 if (hotlist){
1588 remove_group (hotlist);
1589 g_free (hotlist->label);
1590 g_free (hotlist->directory);
1591 g_free (hotlist);
1592 hotlist = 0;
1595 hotlist_state.loaded = 0;
1597 g_free (hotlist_file_name);
1598 hotlist_file_name = 0;
1599 l_hotlist = 0;
1600 current_group = 0;
1601 if (tkn_buf){
1602 g_free (tkn_buf);
1603 tkn_buf_length = 0;
1604 tkn_length = 0;
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);