Removed type SHELL_ESCAPED_STR in favour of plain char*
[midnight-commander.git] / src / hotlist.c
blobee8f3bde0b91ba7077d20a3e219d967954601823
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>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
39 #include <mhl/memory.h>
40 #include <mhl/string.h>
42 #include "global.h"
43 #include "tty.h" /* COLS */
44 #include "color.h" /* dialog_colors */
45 #include "dialog.h"
46 #include "widget.h"
47 #include "setup.h" /* For profile_bname */
48 #include "profile.h" /* Load/save directories hotlist */
49 #include "wtools.h" /* QuickDialog */
50 #include "panel.h" /* current_panel */
51 #include "main.h" /* repaint_screen */
52 #include "hotlist.h"
53 #include "key.h" /* KEY_M_CTRL */
54 #include "command.h" /* cmdline */
55 #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
56 #include "history.h"
58 #define UX 5
59 #define UY 2
61 #define BX UX
62 #define BY (LINES - 6)
64 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
65 #define LABELS 3
66 #define B_ADD_CURRENT B_USER
67 #define B_REMOVE (B_USER + 1)
68 #define B_NEW_GROUP (B_USER + 2)
69 #define B_NEW_ENTRY (B_USER + 3)
70 #define B_UP_GROUP (B_USER + 4)
71 #define B_INSERT (B_USER + 5)
72 #define B_APPEND (B_USER + 6)
73 #define B_MOVE (B_USER + 7)
75 #ifdef USE_VFS
76 #include "../vfs/gc.h"
77 #define B_FREE_ALL_VFS (B_USER + 8)
78 #define B_REFRESH_VFS (B_USER + 9)
79 #endif
81 int hotlist_has_dot_dot = 1;
83 static WListbox *l_hotlist;
84 static WListbox *l_movelist;
86 static Dlg_head *hotlist_dlg;
87 static Dlg_head *movelist_dlg;
89 static WLabel *pname, *pname_group, *movelist_group;
91 enum HotListType {
92 HL_TYPE_GROUP,
93 HL_TYPE_ENTRY,
94 HL_TYPE_COMMENT,
95 HL_TYPE_DOTDOT
98 static struct {
100 * these parameters are intended to be user configurable
102 int expanded; /* expanded view of all groups at startup */
105 * these reflect run time state
108 int loaded; /* hotlist is loaded */
109 int readonly; /* hotlist readonly */
110 int file_error; /* parse error while reading file */
111 int running; /* we are running dlg (and have to
112 update listbox */
113 int moving; /* we are in moving hotlist currently */
114 int modified; /* hotlist was modified */
115 int type; /* LIST_HOTLIST || LIST_VFSLIST */
116 } hotlist_state;
118 static struct _hotlist_but {
119 int ret_cmd, flags, y, x;
120 const char *text;
121 int type;
122 } hotlist_but[] = {
123 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), LIST_HOTLIST},
124 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"), LIST_HOTLIST},
125 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"), LIST_MOVELIST},
126 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"), LIST_MOVELIST},
127 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &Entry"), LIST_HOTLIST},
128 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &Group"), LIST_HOTLIST},
129 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"), LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
130 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"), LIST_HOTLIST|LIST_MOVELIST},
131 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"), LIST_HOTLIST},
132 #ifdef USE_VFS
133 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"), LIST_VFSLIST},
134 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"), LIST_VFSLIST},
135 #endif
136 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &To"), LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
139 /* Directory hotlist */
140 static struct hotlist{
141 enum HotListType type;
142 char *directory;
143 char *label;
144 struct hotlist *head;
145 struct hotlist *up;
146 struct hotlist *next;
147 } *hotlist = NULL;
149 static struct hotlist *current_group;
151 static void init_movelist (int, struct hotlist *);
152 static void add_new_group_cmd (void);
153 static void add_new_entry_cmd (void);
154 static void remove_from_hotlist (struct hotlist *entry);
155 static void load_hotlist (void);
156 static void add_dotdot_to_list (void);
158 #define new_hotlist() g_new0(struct hotlist, 1)
160 static void
161 hotlist_refresh (Dlg_head * dlg)
163 common_dialog_repaint (dlg);
164 attrset (COLOR_NORMAL);
165 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10),
166 dlg->cols - (UX * 2));
167 if (!hotlist_state.moving)
168 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2));
171 /* If current->data is 0, then we are dealing with a VFS pathname */
172 static inline void
173 update_path_name (void)
175 const char *text = "";
176 char *p;
177 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
178 Dlg_head *dlg = list->widget.parent;
180 if (list->current) {
181 if (list->current->data != 0) {
182 struct hotlist *hlp = (struct hotlist *) list->current->data;
184 if (hlp->type == HL_TYPE_ENTRY ||
185 hlp->type == HL_TYPE_DOTDOT)
186 text = hlp->directory;
187 else if (hlp->type == HL_TYPE_GROUP)
188 text = _("Subgroup - press ENTER to see list");
189 } else {
190 text = list->current->text;
193 if (!hotlist_state.moving)
194 label_set_text (pname,
195 name_trunc (text, dlg->cols - (UX * 2 + 4)));
197 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
198 if (!hotlist_state.moving)
199 label_set_text (pname_group,
200 name_trunc (p, dlg->cols - (UX * 2 + 4)));
201 else
202 label_set_text (movelist_group,
203 name_trunc (p, dlg->cols - (UX * 2 + 4)));
204 g_free (p);
206 dlg_redraw (dlg);
209 #define CHECK_BUFFER \
210 do { \
211 int i; \
213 if ((i = strlen (current->label) + 3) > buflen) { \
214 g_free (buf); \
215 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
217 buf[0] = '\0'; \
218 } while (0)
220 static void fill_listbox (void)
222 struct hotlist *current = current_group->head;
223 static char *buf;
224 static int buflen;
226 if (!buf)
227 buf = g_malloc (buflen = 1024);
228 buf[0] = '\0';
230 while (current){
231 switch (current->type) {
232 case HL_TYPE_GROUP:
234 CHECK_BUFFER;
235 strcat (strcat (buf, "->"), current->label);
236 if (hotlist_state.moving)
237 listbox_add_item (l_movelist, 0, 0, buf, current);
238 else
239 listbox_add_item (l_hotlist, 0, 0, buf, current);
241 break;
242 case HL_TYPE_DOTDOT:
243 case HL_TYPE_ENTRY:
244 if (hotlist_state.moving)
245 listbox_add_item (l_movelist, 0, 0, current->label, current);
246 else
247 listbox_add_item (l_hotlist, 0, 0, current->label, current);
248 break;
249 default:
250 break;
252 current = current->next;
256 #undef CHECK_BUFFER
258 static void
259 unlink_entry (struct hotlist *entry)
261 struct hotlist *current = current_group->head;
263 if (current == entry)
264 current_group->head = entry->next;
265 else {
266 while (current && current->next != entry)
267 current = current->next;
268 if (current)
269 current->next = entry->next;
271 entry->next =
272 entry->up = 0;
275 #ifdef USE_VFS
276 static void add_name_to_list (const char *path)
278 listbox_add_item (l_hotlist, 0, 0, path, 0);
280 #endif /* !USE_VFS */
282 static int
283 hotlist_button_callback (int action)
285 switch (action) {
286 case B_MOVE:
288 struct hotlist *saved = current_group;
289 struct hotlist *item;
290 struct hotlist *moveto_item = 0;
291 struct hotlist *moveto_group = 0;
292 int ret;
294 if (!l_hotlist->current)
295 return MSG_NOT_HANDLED; /* empty group - nothing to do */
296 item = l_hotlist->current->data;
297 hotlist_state.moving = 1;
298 init_movelist (LIST_MOVELIST, item);
299 run_dlg (movelist_dlg);
300 ret = movelist_dlg->ret_value;
301 hotlist_state.moving = 0;
302 if (l_movelist->current)
303 moveto_item = l_movelist->current->data;
304 moveto_group = current_group;
305 destroy_dlg (movelist_dlg);
306 current_group = saved;
307 if (ret == B_CANCEL)
308 return MSG_NOT_HANDLED;
309 if (moveto_item == item)
310 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
311 it hardly changes anything ;) */
312 unlink_entry (item);
313 listbox_remove_current (l_hotlist, 1);
314 item->up = moveto_group;
315 if (!moveto_group->head)
316 moveto_group->head = item;
317 else if (!moveto_item) { /* we have group with just comments */
318 struct hotlist *p = moveto_group->head;
320 /* skip comments */
321 while (p->next)
322 p = p->next;
323 p->next = item;
324 } else if (ret == B_ENTER || ret == B_APPEND)
325 if (!moveto_item->next)
326 moveto_item->next = item;
327 else {
328 item->next = moveto_item->next;
329 moveto_item->next = item;
330 } else if (moveto_group->head == moveto_item) {
331 moveto_group->head = item;
332 item->next = moveto_item;
333 } else {
334 struct hotlist *p = moveto_group->head;
336 while (p->next != moveto_item)
337 p = p->next;
338 item->next = p->next;
339 p->next = item;
341 listbox_remove_list (l_hotlist);
342 fill_listbox ();
343 repaint_screen ();
344 hotlist_state.modified = 1;
345 return MSG_NOT_HANDLED;
346 break;
348 case B_REMOVE:
349 if (l_hotlist->current && l_hotlist->current->data)
350 remove_from_hotlist (l_hotlist->current->data);
351 return MSG_NOT_HANDLED;
352 break;
354 case B_NEW_GROUP:
355 add_new_group_cmd ();
356 return MSG_NOT_HANDLED;
357 break;
359 case B_ADD_CURRENT:
360 add2hotlist_cmd ();
361 return MSG_NOT_HANDLED;
362 break;
364 case B_NEW_ENTRY:
365 add_new_entry_cmd ();
366 return MSG_NOT_HANDLED;
367 break;
369 case B_ENTER:
371 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
372 if (list->current) {
373 if (list->current->data) {
374 struct hotlist *hlp =
375 (struct hotlist *) list->current->data;
376 if (hlp->type == HL_TYPE_ENTRY)
377 return MSG_HANDLED;
378 else if (hlp->type == HL_TYPE_DOTDOT) {
379 /* Fall through - go up */
382 else {
383 listbox_remove_list (list);
384 current_group = hlp;
385 fill_listbox ();
386 return MSG_NOT_HANDLED;
388 } else
389 return MSG_HANDLED;
392 /* Fall through if list empty - just go up */
394 case B_UP_GROUP:
396 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
397 listbox_remove_list (list);
398 current_group = current_group->up;
399 fill_listbox ();
400 return MSG_NOT_HANDLED;
401 break;
404 #ifdef USE_VFS
405 case B_FREE_ALL_VFS:
406 vfs_expire (1);
407 /* fall through */
409 case B_REFRESH_VFS:
410 listbox_remove_list (l_hotlist);
411 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
412 vfs_fill_names (add_name_to_list);
413 return MSG_NOT_HANDLED;
414 #endif /* USE_VFS */
416 default:
417 return MSG_HANDLED;
418 break;
423 static cb_ret_t
424 hotlist_callback (Dlg_head *h, dlg_msg_t msg, int parm)
426 switch (msg) {
427 case DLG_DRAW:
428 hotlist_refresh (h);
429 return MSG_HANDLED;
431 case DLG_UNHANDLED_KEY:
432 switch (parm) {
433 case KEY_M_CTRL | '\n':
434 goto l1;
435 case '\n':
436 case KEY_ENTER:
437 case KEY_RIGHT:
438 if (hotlist_button_callback (B_ENTER)) {
439 h->ret_value = B_ENTER;
440 dlg_stop (h);
442 return MSG_HANDLED;
443 break;
444 case KEY_LEFT:
445 if (hotlist_state.type != LIST_VFSLIST)
446 return !hotlist_button_callback (B_UP_GROUP);
447 else
448 return MSG_NOT_HANDLED;
449 break;
450 case KEY_DC:
451 if (!hotlist_state.moving) {
452 hotlist_button_callback (B_REMOVE);
453 return MSG_HANDLED;
455 break;
457 case ALT ('\n'):
458 case ALT ('\r'):
459 if (!hotlist_state.moving) {
460 if (l_hotlist->current) {
461 if (l_hotlist->current->data) {
462 struct hotlist *hlp =
463 (struct hotlist *) l_hotlist->current->data;
464 if (hlp->type == HL_TYPE_ENTRY) {
465 char *tmp =
466 g_strconcat ("cd ", hlp->directory, (char *) NULL);
467 stuff (cmdline, tmp, 0);
468 g_free (tmp);
469 dlg_stop (h);
470 h->ret_value = B_CANCEL;
471 return MSG_HANDLED;
476 return MSG_HANDLED; /* ignore key */
478 return MSG_NOT_HANDLED;
480 case DLG_POST_KEY:
481 if (hotlist_state.moving)
482 dlg_select_widget (l_movelist);
483 else
484 dlg_select_widget (l_hotlist);
485 /* always stay on hotlist */
486 /* fall through */
488 case DLG_INIT:
489 attrset (MENU_ENTRY_COLOR);
490 update_path_name ();
491 return MSG_HANDLED;
493 default:
494 return default_dlg_callback (h, msg, parm);
498 static int l_call (WListbox *list)
500 Dlg_head *dlg = list->widget.parent;
502 if (list->current){
503 if (list->current->data) {
504 struct hotlist *hlp = (struct hotlist*) list->current->data;
505 if (hlp->type == HL_TYPE_ENTRY) {
506 dlg->ret_value = B_ENTER;
507 dlg_stop (dlg);
508 return LISTBOX_DONE;
509 } else {
510 hotlist_button_callback (B_ENTER);
511 hotlist_callback (dlg, DLG_POST_KEY, '\n');
512 return LISTBOX_CONT;
514 } else {
515 dlg->ret_value = B_ENTER;
516 dlg_stop (dlg);
517 return LISTBOX_DONE;
521 hotlist_button_callback (B_UP_GROUP);
522 hotlist_callback (dlg, DLG_POST_KEY, 'u');
523 return LISTBOX_CONT;
527 * Expands all button names (once) and recalculates button positions.
528 * returns number of columns in the dialog box, which is 10 chars longer
529 * then buttonbar.
531 * If common width of the window (i.e. in xterm) is less than returned
532 * width - sorry :) (anyway this did not handled in previous version too)
534 static int
535 init_i18n_stuff(int list_type, int cols)
537 register int i;
538 static const char* cancel_but = N_("&Cancel");
540 #ifdef ENABLE_NLS
541 static int hotlist_i18n_flag = 0;
543 if (!hotlist_i18n_flag)
545 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
546 while (i--)
547 hotlist_but [i].text = _(hotlist_but [i].text);
549 cancel_but = _(cancel_but);
550 hotlist_i18n_flag = 1;
552 #endif /* ENABLE_NLS */
554 /* Dynamic resizing of buttonbars */
556 int len[2], count[2]; /* at most two lines of buttons */
557 int cur_x[2], row;
559 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
560 len[0] = len[1] = count[0] = count[1] = 0;
562 /* Count len of buttonbars, assuming 2 extra space between buttons */
563 while (i--)
565 if (! (hotlist_but[i].type & list_type))
566 continue;
568 row = hotlist_but [i].y;
569 ++count [row];
570 len [row] += strlen (hotlist_but [i].text) + 5;
571 if (hotlist_but [i].flags == DEFPUSH_BUTTON)
572 len [row] += 2;
574 len[0] -= 2;
575 len[1] -= 2;
577 cols = max(cols, max(len[0], len[1]));
579 /* arrange buttons */
581 cur_x[0] = cur_x[1] = 0;
582 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
583 while (i--)
585 if (! (hotlist_but[i].type & list_type))
586 continue;
588 row = hotlist_but [i].y;
590 if (hotlist_but [i].x != 0)
592 /* not first int the row */
593 if (!strcmp (hotlist_but [i].text, cancel_but))
594 hotlist_but [i].x =
595 cols - strlen (hotlist_but [i].text) - 13;
596 else
597 hotlist_but [i].x = cur_x [row];
600 cur_x [row] += strlen (hotlist_but [i].text) + 2
601 + (hotlist_but [i].flags == DEFPUSH_BUTTON ? 5 : 3);
605 return cols;
608 static void
609 init_hotlist (int list_type)
611 size_t i;
612 const char *title, *help_node;
613 int hotlist_cols;
615 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
617 do_refresh ();
619 hotlist_state.expanded =
620 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
621 0, profile_name);
623 if (list_type == LIST_VFSLIST) {
624 title = _("Active VFS directories");
625 help_node = "[vfshot]"; /* FIXME - no such node */
626 } else {
627 title = _("Directory hotlist");
628 help_node = "[Hotlist]";
631 hotlist_dlg =
632 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
633 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
635 for (i = 0; i < BUTTONS; i++) {
636 if (hotlist_but[i].type & list_type)
637 add_widget (hotlist_dlg,
638 button_new (BY + hotlist_but[i].y,
639 BX + hotlist_but[i].x,
640 hotlist_but[i].ret_cmd,
641 hotlist_but[i].flags,
642 hotlist_but[i].text,
643 hotlist_button_callback));
646 /* We add the labels.
647 * pname will hold entry's pathname;
648 * pname_group will hold name of current group
650 pname = label_new (UY - 11 + LINES, UX + 2, "");
651 add_widget (hotlist_dlg, pname);
652 if (!hotlist_state.moving) {
653 add_widget (hotlist_dlg,
654 label_new (UY - 12 + LINES, UX + 1,
655 _(" Directory path ")));
657 /* This one holds the displayed pathname */
658 pname_group = label_new (UY, UX + 1, _(" Directory label "));
659 add_widget (hotlist_dlg, pname_group);
661 /* get new listbox */
662 l_hotlist =
663 listbox_new (UY + 1, UX + 1, COLS - 2 * UX - 8, LINES - 14,
664 l_call);
666 /* Fill the hotlist with the active VFS or the hotlist */
667 #ifdef USE_VFS
668 if (list_type == LIST_VFSLIST) {
669 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
670 vfs_fill_names (add_name_to_list);
671 } else
672 #endif /* !USE_VFS */
673 fill_listbox ();
675 add_widget (hotlist_dlg, l_hotlist);
676 /* add listbox to the dialogs */
679 static void
680 init_movelist (int list_type, struct hotlist *item)
682 size_t i;
683 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
684 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
686 do_refresh ();
688 movelist_dlg =
689 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
690 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
691 g_free (hdr);
693 for (i = 0; i < BUTTONS; i++) {
694 if (hotlist_but[i].type & list_type)
695 add_widget (movelist_dlg,
696 button_new (BY - 4 + hotlist_but[i].y,
697 BX + hotlist_but[i].x,
698 hotlist_but[i].ret_cmd,
699 hotlist_but[i].flags,
700 hotlist_but[i].text,
701 hotlist_button_callback));
704 /* We add the labels. We are interested in the last one,
705 * that one will hold the path name label
707 movelist_group = label_new (UY, UX + 1, _(" Directory label "));
708 add_widget (movelist_dlg, movelist_group);
709 /* get new listbox */
710 l_movelist =
711 listbox_new (UY + 1, UX + 1, movelist_dlg->cols - 2 * UX - 2,
712 movelist_dlg->lines - 8, l_call);
714 fill_listbox ();
716 add_widget (movelist_dlg, l_movelist);
717 /* add listbox to the dialogs */
721 * Destroy the list dialog.
722 * Don't confuse with done_hotlist() for the list in memory.
724 static void hotlist_done (void)
726 destroy_dlg (hotlist_dlg);
727 l_hotlist = NULL;
728 if (0)
729 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
730 repaint_screen ();
733 static inline char *
734 find_group_section (struct hotlist *grp)
736 return g_strconcat (grp->directory, ".Group", (char *) NULL);
740 /* 1.11.96 bor: added pos parameter to control placement of new item.
741 see widget.c, listbox_add_item()
742 now hotlist is in unsorted mode
744 enum {
745 HL_BEFORE_CURRENT = 1
746 ,HL_AFTER_CURRENT = 2
749 static struct hotlist *
750 add2hotlist (char *label, char *directory, enum HotListType type, int pos)
752 struct hotlist *new;
753 struct hotlist *current = NULL;
756 * Hotlist is neither loaded nor loading.
757 * Must be called by "Ctrl-x a" before using hotlist.
759 if (!current_group)
760 load_hotlist ();
762 if (l_hotlist && l_hotlist->current) {
763 current = l_hotlist->current->data;
765 /* Make sure `..' stays at the top of the list. */
766 if (current->type == HL_TYPE_DOTDOT)
767 pos = HL_AFTER_CURRENT;
770 new = new_hotlist ();
772 new->type = type;
773 new->label = label;
774 new->directory = directory;
775 new->up = current_group;
777 if (type == HL_TYPE_GROUP) {
778 current_group = new;
779 add_dotdot_to_list ();
780 current_group = new->up;
783 if (!current_group->head) { /* first element in group */
784 current_group->head = new;
785 } else if (pos == HL_AFTER_CURRENT) {
786 new->next = current->next;
787 current->next = new;
788 } else if (pos == HL_BEFORE_CURRENT &&
789 current == current_group->head) {
790 /* should be inserted before first item */
791 new->next = current;
792 current_group->head = new;
793 } else if (pos == HL_BEFORE_CURRENT) {
794 struct hotlist *p = current_group->head;
796 while (p->next != current)
797 p = p->next;
799 new->next = current;
800 p->next = new;
801 } else { /* append at the end */
802 struct hotlist *p = current_group->head;
804 while (p->next)
805 p = p->next;
807 p->next = new;
810 if (hotlist_state.running && type != HL_TYPE_COMMENT &&
811 type != HL_TYPE_DOTDOT) {
812 if (type == HL_TYPE_GROUP) {
813 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
815 listbox_add_item (l_hotlist, pos, 0, lbl, new);
816 g_free (lbl);
817 } else
818 listbox_add_item (l_hotlist, pos, 0, new->label, new);
819 listbox_select_entry (l_hotlist, l_hotlist->current);
821 return new;
825 #ifdef ENABLE_NLS
827 * Support routine for add_new_entry_input()/add_new_group_input()
828 * Change positions of buttons (first three widgets).
830 * This is just a quick hack. Accurate procedure must take care of
831 * internationalized label lengths and total buttonbar length...assume
832 * 64 is longer anyway.
834 static void add_widgets_i18n(QuickWidget* qw, int len)
836 int i, l[3], space, cur_x;
838 for (i = 0; i < 3; i++)
840 qw [i].text = _(qw [i].text);
841 l[i] = strlen (qw [i].text) + 3;
843 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
845 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
847 qw [i].relative_x = cur_x;
848 qw [i].x_divisions = len;
851 #endif /* ENABLE_NLS */
853 static int
854 add_new_entry_input (const char *header, const char *text1, const char *text2,
855 const char *help, char **r1, char **r2)
857 #define RELATIVE_Y_BUTTONS 4
858 #define RELATIVE_Y_LABEL_PTH 3
859 #define RELATIVE_Y_INPUT_PTH 4
861 QuickDialog Quick_input;
862 static QuickWidget quick_widgets [] = {
863 { quick_button, 55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), 0, B_CANCEL,
864 0, 0, NULL },
865 { quick_button, 30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), 0, B_INSERT,
866 0, 0, NULL },
867 { quick_button, 10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), 0, B_APPEND,
868 0, 0, NULL },
869 { quick_input, 4, 80, RELATIVE_Y_INPUT_PTH, 0, "",58, 0,
870 0, 0, "input-pth" },
871 { quick_label, RELATIVE_Y_LABEL_PTH, 80, 3, 0, 0, 0, 0,
872 0, 0, NULL },
873 { quick_input, 4, 80, 3, 0, "", 58, 0,
874 0, 0, "input-lbl" },
875 { quick_label, 3, 80, 2, 0, 0, 0, 0,
876 0, 0, NULL },
877 NULL_QuickWidget };
879 int len;
880 int i;
881 int lines1, lines2;
882 int cols1, cols2;
884 #ifdef ENABLE_NLS
885 static int i18n_flag = 0;
886 #endif /* ENABLE_NLS */
888 msglen(text1, &lines1, &cols1);
889 msglen(text2, &lines2, &cols2);
890 len = max ((int) strlen (header), cols1);
891 len = max (len, cols2) + 4;
892 len = max (len, 64);
894 #ifdef ENABLE_NLS
895 if (!i18n_flag)
897 add_widgets_i18n(quick_widgets, len);
898 i18n_flag = 1;
900 #endif /* ENABLE_NLS */
902 Quick_input.xlen = len;
903 Quick_input.xpos = -1;
904 Quick_input.title = header;
905 Quick_input.help = help;
906 Quick_input.i18n = 0;
907 quick_widgets [6].text = text1;
908 quick_widgets [4].text = text2;
909 quick_widgets [5].text = *r1;
910 quick_widgets [3].text = *r2;
912 for (i = 0; i < 7; i++)
913 quick_widgets [i].y_divisions = lines1+lines2+7;
914 Quick_input.ylen = lines1 + lines2 + 7;
916 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
917 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
918 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
919 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
920 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
922 quick_widgets [5].str_result = r1;
923 quick_widgets [3].str_result = r2;
925 Quick_input.widgets = quick_widgets;
926 if ((i = quick_dialog (&Quick_input)) != B_CANCEL){
927 return i;
928 } else
929 return 0;
932 static void add_new_entry_cmd (void)
934 char *title, *url, *to_free;
935 int ret;
937 /* Take current directory as default value for input fields */
938 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
940 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"),
941 _("Directory path"), "[Hotlist]", &title, &url);
942 g_free (to_free);
944 if (!ret)
945 return;
946 if (!title || !*title || !url || !*url) {
947 g_free (title);
948 g_free (url);
949 return;
952 if (ret == B_ENTER || ret == B_APPEND)
953 add2hotlist (title, url, HL_TYPE_ENTRY, HL_AFTER_CURRENT);
954 else
955 add2hotlist (title, url, HL_TYPE_ENTRY, HL_BEFORE_CURRENT);
957 hotlist_state.modified = 1;
960 static int
961 add_new_group_input (const char *header, const char *label, char **result)
963 int ret;
964 QuickDialog Quick_input;
965 static QuickWidget quick_widgets [] = {
966 { quick_button, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
967 NULL },
968 { quick_button, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT, 0, 0,
969 NULL },
970 { quick_button, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND, 0, 0,
971 NULL },
972 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
973 { quick_label, 3, 80, 2, 0, 0, 0, 0, 0, 0, NULL },
974 NULL_QuickWidget };
975 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
976 quick_widgets variable above */
977 int len;
978 int i;
979 int lines, cols;
981 #ifdef ENABLE_NLS
982 static int i18n_flag = 0;
983 #endif /* ENABLE_NLS */
985 msglen (label, &lines, &cols);
986 len = max ((int) strlen (header), cols) + 4;
987 len = max (len, 64);
989 #ifdef ENABLE_NLS
990 if (!i18n_flag)
992 add_widgets_i18n(quick_widgets, len);
993 i18n_flag = 1;
995 #endif /* ENABLE_NLS */
997 Quick_input.xlen = len;
998 Quick_input.xpos = -1;
999 Quick_input.title = header;
1000 Quick_input.help = "[Hotlist]";
1001 Quick_input.i18n = 0;
1002 quick_widgets [4].text = label;
1004 for (i = 0; i < 5; i++)
1005 quick_widgets [i].y_divisions = lines+6;
1006 Quick_input.ylen = lines + 6;
1008 for (i = 0; i < 4; i++)
1009 quick_widgets [i].relative_y = relative_y[i] + 2 + lines;
1011 quick_widgets [3].str_result = result;
1012 quick_widgets [3].text = "";
1014 Quick_input.widgets = quick_widgets;
1015 if ((ret = quick_dialog (&Quick_input)) != B_CANCEL){
1016 return ret;
1017 } else
1018 return 0;
1021 static void add_new_group_cmd (void)
1023 char *label;
1024 int ret;
1026 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
1027 if (!ret || !label || !*label)
1028 return;
1030 if (ret == B_ENTER || ret == B_APPEND)
1031 add2hotlist (label, 0, HL_TYPE_GROUP, HL_AFTER_CURRENT);
1032 else
1033 add2hotlist (label, 0, HL_TYPE_GROUP, HL_BEFORE_CURRENT);
1035 hotlist_state.modified = 1;
1038 void add2hotlist_cmd (void)
1040 char *prompt, *label;
1041 const char *cp = _("Label for \"%s\":");
1042 int l = strlen (cp);
1043 char *label_string = g_strdup (current_panel->cwd);
1045 strip_password (label_string, 1);
1047 prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS-2*UX-(l+8)));
1048 label = input_dialog (_(" Add to hotlist "), prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1049 g_free (prompt);
1051 if (!label || !*label) {
1052 g_free (label_string);
1053 g_free (label);
1054 return;
1056 add2hotlist (label, label_string, HL_TYPE_ENTRY, 0);
1057 hotlist_state.modified = 1;
1060 static void remove_group (struct hotlist *grp)
1062 struct hotlist *current = grp->head;
1064 while (current) {
1065 struct hotlist *next = current->next;
1067 if (current->type == HL_TYPE_GROUP)
1068 remove_group (current);
1070 g_free (current->label);
1071 g_free (current->directory);
1072 g_free (current);
1074 current = next;
1079 static void remove_from_hotlist (struct hotlist *entry)
1081 if (entry->type == HL_TYPE_DOTDOT)
1082 return;
1084 if (confirm_directory_hotlist_delete) {
1085 char *title;
1086 int result;
1088 title = g_strconcat (_(" Remove: "),
1089 name_trunc (entry->label, 30),
1090 " ",
1091 NULL);
1093 if (safe_delete)
1094 query_set_sel (1);
1095 result = query_dialog (title,
1096 _("\n Are you sure you want to remove this entry?"),
1097 D_ERROR, 2, _("&Yes"), _("&No"));
1099 g_free (title);
1101 if (result != 0)
1102 return;
1105 if (entry->type == HL_TYPE_GROUP) {
1106 if (entry->head) {
1107 char *header;
1108 int result;
1110 header = g_strconcat (_(" Remove: "),
1111 name_trunc (entry->label, 30),
1112 " ",
1113 NULL);
1114 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1115 D_ERROR, 2,
1116 _("&Yes"), _("&No"));
1117 g_free (header);
1119 if (result != 0)
1120 return;
1123 remove_group (entry);
1126 unlink_entry (entry);
1128 g_free (entry->label);
1129 g_free (entry->directory);
1130 g_free (entry);
1131 /* now remove list entry from screen */
1132 listbox_remove_current (l_hotlist, 1);
1133 hotlist_state.modified = 1;
1136 char *hotlist_cmd (int vfs_or_hotlist)
1138 char *target = NULL;
1140 hotlist_state.type = vfs_or_hotlist;
1141 load_hotlist ();
1143 init_hotlist (vfs_or_hotlist);
1145 /* display file info */
1146 attrset (SELECTED_COLOR);
1148 hotlist_state.running = 1;
1149 run_dlg (hotlist_dlg);
1150 hotlist_state.running = 0;
1151 save_hotlist ();
1153 switch (hotlist_dlg->ret_value) {
1154 case B_CANCEL:
1155 break;
1157 case B_ENTER:
1158 if (l_hotlist->current->data) {
1159 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
1160 target = g_strdup (hlp->directory);
1161 } else
1162 target = g_strdup (l_hotlist->current->text);
1163 break;
1166 hotlist_done ();
1167 return target;
1170 static void
1171 load_group (struct hotlist *grp)
1173 void *profile_keys;
1174 char *key, *value;
1175 char *group_section;
1176 struct hotlist *current = 0;
1178 group_section = find_group_section (grp);
1180 profile_keys = profile_init_iterator (group_section, profile_name);
1182 current_group = grp;
1184 while (profile_keys){
1185 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1186 add2hotlist (g_strdup (value), g_strdup (key), HL_TYPE_GROUP, 0);
1188 g_free (group_section);
1190 profile_keys = profile_init_iterator (grp->directory, profile_name);
1192 while (profile_keys){
1193 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1194 add2hotlist (g_strdup (value),g_strdup (key), HL_TYPE_ENTRY, 0);
1197 for (current = grp->head; current; current = current->next)
1198 load_group (current);
1201 #define TKN_GROUP 0
1202 #define TKN_ENTRY 1
1203 #define TKN_STRING 2
1204 #define TKN_URL 3
1205 #define TKN_ENDGROUP 4
1206 #define TKN_COMMENT 5
1207 #define TKN_EOL 125
1208 #define TKN_EOF 126
1209 #define TKN_UNKNOWN 127
1211 static char *tkn_buf;
1212 static int tkn_buf_length;
1213 static int tkn_length;
1215 static char *hotlist_file_name;
1216 static FILE *hotlist_file;
1217 static time_t hotlist_file_mtime;
1219 static int hot_skip_blanks (void)
1221 int c;
1223 while ((c = getc (hotlist_file)) != EOF && c != '\n' && isspace (c))
1225 return c;
1229 static int hot_next_token (void)
1231 int c;
1233 #define CHECK_BUF() \
1234 do { \
1235 if (tkn_length == tkn_buf_length) \
1236 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1237 : ( g_malloc (tkn_buf_length = 1024)); \
1238 } while (0)
1240 tkn_length = 0;
1242 again:
1243 c = hot_skip_blanks ();
1244 switch (c) {
1245 case EOF:
1246 return TKN_EOF;
1247 break;
1248 case '\n':
1249 return TKN_EOL;
1250 break;
1251 case '#':
1252 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1253 if (c == EOF)
1254 return TKN_EOF;
1255 if (c != '\n') {
1256 CHECK_BUF();
1257 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1260 CHECK_BUF();
1261 tkn_buf[tkn_length] = '\0';
1262 return TKN_COMMENT;
1263 break;
1264 case '"':
1265 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1266 if (c == '\\')
1267 if ((c = getc (hotlist_file)) == EOF)
1268 return TKN_EOF;
1269 CHECK_BUF();
1270 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1272 if (c == EOF)
1273 return TKN_EOF;
1274 CHECK_BUF();
1275 tkn_buf[tkn_length] = '\0';
1276 return TKN_STRING;
1277 break;
1278 case '\\':
1279 if ((c = getc (hotlist_file)) == EOF)
1280 return TKN_EOF;
1281 if (c == '\n')
1282 goto again;
1284 /* fall through; it is taken as normal character */
1286 default:
1287 do {
1288 CHECK_BUF();
1289 tkn_buf[tkn_length++] = toupper(c);
1290 } while ((c = fgetc (hotlist_file)) != EOF && isalnum (c));
1291 if (c != EOF)
1292 ungetc (c, hotlist_file);
1293 CHECK_BUF();
1294 tkn_buf[tkn_length] = '\0';
1295 if (strncmp (tkn_buf, "GROUP", tkn_length) == 0)
1296 return TKN_GROUP;
1297 else if (strncmp (tkn_buf, "ENTRY", tkn_length) == 0)
1298 return TKN_ENTRY;
1299 else if (strncmp (tkn_buf, "ENDGROUP", tkn_length) == 0)
1300 return TKN_ENDGROUP;
1301 else if (strncmp (tkn_buf, "URL", tkn_length) == 0)
1302 return TKN_URL;
1303 else
1304 return TKN_UNKNOWN;
1305 break;
1309 #define SKIP_TO_EOL { \
1310 int _tkn; \
1311 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1314 #define CHECK_TOKEN(_TKN_) \
1315 if ((tkn = hot_next_token ()) != _TKN_) { \
1316 hotlist_state.readonly = 1; \
1317 hotlist_state.file_error = 1; \
1318 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1319 tkn = hot_next_token (); \
1320 break; \
1323 static void
1324 hot_load_group (struct hotlist * grp)
1326 int tkn;
1327 struct hotlist *new_grp;
1328 char *label, *url;
1330 current_group = grp;
1332 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1333 switch (tkn) {
1334 case TKN_GROUP:
1335 CHECK_TOKEN(TKN_STRING);
1336 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1337 SKIP_TO_EOL;
1338 hot_load_group (new_grp);
1339 current_group = grp;
1340 break;
1341 case TKN_ENTRY:
1342 CHECK_TOKEN(TKN_STRING);
1343 label = g_strdup (tkn_buf);
1344 CHECK_TOKEN(TKN_URL);
1345 CHECK_TOKEN(TKN_STRING);
1346 url = g_strdup (tkn_buf);
1347 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1348 SKIP_TO_EOL;
1349 break;
1350 case TKN_COMMENT:
1351 label = g_strdup (tkn_buf);
1352 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1353 break;
1354 case TKN_EOF:
1355 hotlist_state.readonly = 1;
1356 hotlist_state.file_error = 1;
1357 return;
1358 break;
1359 case TKN_EOL:
1360 /* skip empty lines */
1361 break;
1362 default:
1363 hotlist_state.readonly = 1;
1364 hotlist_state.file_error = 1;
1365 SKIP_TO_EOL;
1366 break;
1368 SKIP_TO_EOL;
1371 static void
1372 hot_load_file (struct hotlist * grp)
1374 int tkn;
1375 struct hotlist *new_grp;
1376 char *label, *url;
1378 current_group = grp;
1380 while ((tkn = hot_next_token())!= TKN_EOF)
1381 switch (tkn) {
1382 case TKN_GROUP:
1383 CHECK_TOKEN(TKN_STRING);
1384 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1385 SKIP_TO_EOL;
1386 hot_load_group (new_grp);
1387 current_group = grp;
1388 break;
1389 case TKN_ENTRY:
1390 CHECK_TOKEN(TKN_STRING);
1391 label = g_strdup (tkn_buf);
1392 CHECK_TOKEN(TKN_URL);
1393 CHECK_TOKEN(TKN_STRING);
1394 url = g_strdup (tkn_buf);
1395 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1396 SKIP_TO_EOL;
1397 break;
1398 case TKN_COMMENT:
1399 label = g_strdup (tkn_buf);
1400 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1401 break;
1402 case TKN_EOL:
1403 /* skip empty lines */
1404 break;
1405 default:
1406 hotlist_state.readonly = 1;
1407 hotlist_state.file_error = 1;
1408 SKIP_TO_EOL;
1409 break;
1413 static void
1414 clean_up_hotlist_groups (const char *section)
1416 char *grp_section;
1417 void *profile_keys;
1418 char *key, *value;
1420 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1421 if (profile_has_section (section, profile_name))
1422 profile_clean_section (section, profile_name);
1423 if (profile_has_section (grp_section, profile_name)) {
1424 profile_keys = profile_init_iterator (grp_section, profile_name);
1426 while (profile_keys) {
1427 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1428 clean_up_hotlist_groups (key);
1430 profile_clean_section (grp_section, profile_name);
1432 g_free (grp_section);
1437 static void
1438 load_hotlist (void)
1440 int remove_old_list = 0;
1441 struct stat stat_buf;
1443 if (hotlist_state.loaded) {
1444 stat (hotlist_file_name, &stat_buf);
1445 if (hotlist_file_mtime < stat_buf.st_mtime)
1446 done_hotlist ();
1447 else
1448 return;
1451 if (!hotlist_file_name)
1452 hotlist_file_name = mhl_str_dir_plus_file (home_dir, HOTLIST_FILENAME);
1454 hotlist = new_hotlist ();
1455 hotlist->type = HL_TYPE_GROUP;
1456 hotlist->label = g_strdup (_(" Top level group "));
1457 hotlist->up = hotlist;
1459 * compatibility :-(
1461 hotlist->directory = g_strdup ("Hotlist");
1463 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1464 int result;
1466 load_group (hotlist);
1467 hotlist_state.loaded = 1;
1469 * just to be sure we got copy
1471 hotlist_state.modified = 1;
1472 result = save_hotlist ();
1473 hotlist_state.modified = 0;
1474 if (result) {
1475 remove_old_list = 1;
1476 } else {
1477 message (D_ERROR, _(" Hotlist Load "),
1478 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1479 HOTLIST_FILENAME);
1481 } else {
1482 hot_load_file (hotlist);
1483 fclose (hotlist_file);
1484 hotlist_state.loaded = 1;
1487 if (remove_old_list) {
1488 clean_up_hotlist_groups ("Hotlist");
1489 sync_profiles ();
1492 stat (hotlist_file_name, &stat_buf);
1493 hotlist_file_mtime = stat_buf.st_mtime;
1494 current_group = hotlist;
1498 static int list_level = 0;
1500 static void
1501 hot_save_group (struct hotlist *grp)
1503 struct hotlist *current = grp->head;
1504 int i;
1505 char *s;
1507 #define INDENT(n) \
1508 do { \
1509 for (i = 0; i < n; i++) \
1510 putc (' ', hotlist_file); \
1511 } while (0)
1513 for (;current; current = current->next)
1514 switch (current->type) {
1515 case HL_TYPE_GROUP:
1516 INDENT (list_level);
1517 fputs ("GROUP \"", hotlist_file);
1518 for (s = current->label; *s; s++) {
1519 if (*s == '"' || *s == '\\')
1520 putc ('\\', hotlist_file);
1521 putc (*s, hotlist_file);
1523 fputs ("\"\n", hotlist_file);
1524 list_level += 2;
1525 hot_save_group (current);
1526 list_level -= 2;
1527 INDENT (list_level);
1528 fputs ("ENDGROUP\n", hotlist_file);
1529 break;
1530 case HL_TYPE_ENTRY:
1531 INDENT(list_level);
1532 fputs ("ENTRY \"", hotlist_file);
1533 for (s = current->label; *s; s++) {
1534 if (*s == '"' || *s == '\\')
1535 putc ('\\', hotlist_file);
1536 putc (*s, hotlist_file);
1538 fputs ("\" URL \"", hotlist_file);
1539 for (s = current->directory; *s; s++) {
1540 if (*s == '"' || *s == '\\')
1541 putc ('\\', hotlist_file);
1542 putc (*s, hotlist_file);
1544 fputs ("\"\n", hotlist_file);
1545 break;
1546 case HL_TYPE_COMMENT:
1547 fprintf (hotlist_file, "#%s\n", current->label);
1548 break;
1549 case HL_TYPE_DOTDOT:
1550 /* do nothing */
1551 break;
1555 int save_hotlist (void)
1557 int saved = 0;
1558 struct stat stat_buf;
1560 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1561 char *fbak = g_strconcat (hotlist_file_name, ".bak", (char *) NULL);
1563 rename (hotlist_file_name, fbak);
1564 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1565 if (stat (fbak, &stat_buf) == 0)
1566 chmod (hotlist_file_name, stat_buf.st_mode);
1567 else
1568 chmod (hotlist_file_name, S_IRUSR | S_IWUSR);
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 rename (fbak, hotlist_file_name);
1577 g_free (fbak);
1580 return saved;
1584 * Unload list from memory.
1585 * Don't confuse with hotlist_done() for GUI.
1587 void done_hotlist (void)
1589 if (hotlist){
1590 remove_group (hotlist);
1591 g_free (hotlist->label);
1592 g_free (hotlist->directory);
1593 g_free (hotlist);
1594 hotlist = 0;
1597 hotlist_state.loaded = 0;
1599 g_free (hotlist_file_name);
1600 hotlist_file_name = 0;
1601 l_hotlist = 0;
1602 current_group = 0;
1603 if (tkn_buf){
1604 g_free (tkn_buf);
1605 tkn_buf_length = 0;
1606 tkn_length = 0;
1607 tkn_buf = NULL;
1611 static void
1612 add_dotdot_to_list (void)
1614 if (current_group != hotlist) {
1615 if (hotlist_has_dot_dot != 0)
1616 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, 0);