another typo: too late at light corrections...:-(
[midnight-commander.git] / src / hotlist.c
blob099f13c3456cda07556a8ccc26669484d13ef78e
1 /* Directory hotlist -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997 the Free Software Foundation.
4 Written by:
5 1994 Radek Doulik
6 1995 Janne Kukonlehto
7 1996 Andrej Borsenkow
8 1997 Norbert Warmuth
10 Janne did the original Hotlist code, Andrej made the groupable
11 hotlist; the move hotlist and revamped the file format and made
12 it stronger.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <config.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <string.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
39 #include "global.h"
40 #include "tty.h"
41 #include "win.h"
42 #include "color.h"
43 #include "dlg.h"
44 #include "widget.h"
45 #include "dialog.h" /* For do_refresh() */
46 #include "setup.h" /* For profile_bname */
47 #include "profile.h" /* Load/save directories hotlist */
49 #include "../vfs/vfs.h"
50 /* Needed for the extern declarations of integer parameters */
51 #include "wtools.h"
52 #include "dir.h"
53 #include "panel.h" /* Needed for the externs */
54 #include "file.h"
55 #include "main.h"
56 #include "hotlist.h"
57 #include "key.h"
58 #include "command.h"
60 #define UX 5
61 #define UY 2
63 #define BX UX
64 #define BY LINES-6
66 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
67 #define LABELS 3
68 #define B_ADD_CURRENT B_USER
69 #define B_REMOVE (B_USER + 1)
70 #define B_NEW_GROUP (B_USER + 2)
71 #define B_NEW_ENTRY (B_USER + 3)
72 #define B_UP_GROUP (B_USER + 4)
73 #define B_INSERT (B_USER + 5)
74 #define B_APPEND (B_USER + 6)
75 #define B_MOVE (B_USER + 7)
77 static WListbox *l_hotlist;
78 static WListbox *l_movelist;
80 static Dlg_head *hotlist_dlg;
81 static Dlg_head *movelist_dlg;
83 static WLabel *pname, *pname_group, *movelist_group;
85 enum HotListType {
86 HL_TYPE_GROUP,
87 HL_TYPE_ENTRY,
88 HL_TYPE_COMMENT
91 static struct {
93 * these parameters are intended to be user configurable
95 int expanded; /* expanded view of all groups at startup */
98 * these reflect run time state
101 int loaded; /* hotlist is loaded */
102 int readonly; /* hotlist readonly */
103 int file_error; /* parse error while reading file */
104 int running; /* we are running dlg (and have to
105 update listbox */
106 int moving; /* we are in moving hotlist currently */
107 int modified; /* hotlist was modified */
108 int type; /* LIST_HOTLIST || LIST_VFSLIST */
109 } hotlist_state;
111 static struct _hotlist_but {
112 int ret_cmd, flags, y, x;
113 char *text;
114 char *tkname;
115 int type;
116 } hotlist_but[] = {
117 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), "move", LIST_HOTLIST},
118 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"), "r", LIST_HOTLIST},
119 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"), "e", LIST_MOVELIST},
120 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"), "g", LIST_MOVELIST},
121 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &Entry"), "e", LIST_HOTLIST},
122 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &Group"), "g", LIST_HOTLIST},
123 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"), "cc", LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
124 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"), "up", LIST_HOTLIST|LIST_MOVELIST},
125 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),"ad", LIST_HOTLIST},
126 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &To"), "ct", LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
129 /* Directory hotlist */
130 static struct hotlist{
131 enum HotListType type;
132 char *directory;
133 char *label;
134 struct hotlist *head;
135 struct hotlist *up;
136 struct hotlist *next;
137 } *hotlist = NULL;
139 static struct hotlist *current_group;
141 static void init_movelist (int, struct hotlist *);
142 static void add_new_group_cmd (void);
143 static void add_new_entry_cmd (void);
144 static void remove_from_hotlist (struct hotlist *entry);
146 #define new_hotlist() g_new0(struct hotlist, 1)
148 static void
149 hotlist_refresh (Dlg_head * dlg)
151 common_dialog_repaint (dlg);
152 attrset (COLOR_NORMAL);
153 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10),
154 dlg->cols - (UX * 2));
155 if (!hotlist_state.moving)
156 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2));
159 /* If current->data is 0, then we are dealing with a VFS pathname */
160 static inline void update_path_name (void)
162 char *text, *p;
163 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
164 Dlg_head *dlg = hotlist_state.moving ? movelist_dlg : hotlist_dlg;
166 if (list->current){
167 if (list->current->data != 0) {
168 struct hotlist *hlp = (struct hotlist *)list->current->data;
170 if (hlp->type == HL_TYPE_ENTRY)
171 text = hlp->directory;
172 else
173 text = _("Subgroup - press ENTER to see list");
175 p = g_strconcat (" ", current_group->label, " ", NULL);
176 if (!hotlist_state.moving)
177 label_set_text (pname_group, name_trunc (p, dlg->cols - (UX*2+4)));
178 else
179 label_set_text (movelist_group, name_trunc (p, dlg->cols - (UX*2+4)));
180 g_free (p);
181 } else {
182 text = list->current->text;
184 } else {
185 text = "";
187 if (!hotlist_state.moving)
188 label_set_text (pname, name_trunc (text, dlg->cols - (UX*2+4)));
189 dlg_redraw (dlg);
192 #define CHECK_BUFFER \
193 do { \
194 int i; \
196 if ((i = strlen (current->label) + 3) > buflen) { \
197 g_free (buf); \
198 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
200 buf[0] = '\0'; \
201 } while (0)
203 static void fill_listbox (void)
205 struct hotlist *current = current_group->head;
206 static char *buf;
207 static int buflen;
209 if (!buf)
210 buf = g_malloc (buflen = 1024);
211 buf[0] = '\0';
213 while (current){
214 switch (current->type) {
215 case HL_TYPE_GROUP:
217 CHECK_BUFFER;
218 strcat (strcat (buf, "->"), current->label);
219 if (hotlist_state.moving)
220 listbox_add_item (l_movelist, 0, 0, buf, current);
221 else
222 listbox_add_item (l_hotlist, 0, 0, buf, current);
224 break;
225 case HL_TYPE_ENTRY:
226 if (hotlist_state.moving)
227 listbox_add_item (l_movelist, 0, 0, current->label, current);
228 else
229 listbox_add_item (l_hotlist, 0, 0, current->label, current);
230 break;
231 default:
232 break;
234 current = current->next;
238 #undef CHECK_BUFFER
240 static void
241 unlink_entry (struct hotlist *entry)
243 struct hotlist *current = current_group->head;
245 if (current == entry)
246 current_group->head = entry->next;
247 else
248 while (current && current->next != entry)
249 current = current->next;
250 if (current)
251 current->next = entry->next;
252 entry->next =
253 entry->up = 0;
256 static int hotlist_button_callback (int action, void *data)
258 switch (action) {
259 case B_MOVE:
261 struct hotlist *saved = current_group;
262 struct hotlist *item;
263 struct hotlist *moveto_item = 0;
264 struct hotlist *moveto_group = 0;
265 int ret;
267 if (!l_hotlist->current)
268 return 0; /* empty group - nothing to do */
269 item = l_hotlist->current->data;
270 hotlist_state.moving = 1;
271 init_movelist (LIST_MOVELIST, item);
272 run_dlg (movelist_dlg);
273 ret = movelist_dlg->ret_value;
274 hotlist_state.moving = 0;
275 if (l_movelist->current)
276 moveto_item = l_movelist->current->data;
277 moveto_group = current_group;
278 destroy_dlg (movelist_dlg);
279 current_group = saved;
280 if (ret == B_CANCEL)
281 return 0;
282 if (moveto_item == item)
283 return 0; /* If we insert/append a before/after a
284 it hardly changes anything ;) */
285 unlink_entry (item);
286 listbox_remove_current (l_hotlist, 1);
287 item->up = moveto_group;
288 if (!moveto_group->head)
289 moveto_group->head = item;
290 else if (!moveto_item) { /* we have group with just comments */
291 struct hotlist *p = moveto_group->head;
293 /* skip comments */
294 while (p->next)
295 p = p->next;
296 p->next = item;
297 } else if (ret == B_ENTER || ret == B_APPEND)
298 if (!moveto_item->next)
299 moveto_item->next = item;
300 else {
301 item->next = moveto_item->next;
302 moveto_item->next = item;
304 else if (moveto_group->head == moveto_item) {
305 moveto_group->head = item;
306 item->next = moveto_item;
307 } else {
308 struct hotlist *p = moveto_group->head;
310 while (p->next != moveto_item)
311 p = p->next;
312 item->next = p->next;
313 p->next = item;
315 listbox_remove_list (l_hotlist);
316 fill_listbox ();
317 repaint_screen ();
318 hotlist_state.modified = 1;
319 return 0;
320 break;
322 case B_REMOVE:
323 if (l_hotlist->current && l_hotlist->current->data)
324 remove_from_hotlist (l_hotlist->current->data);
325 return 0;
326 break;
328 case B_NEW_GROUP:
329 add_new_group_cmd ();
330 return 0;
331 break;
333 case B_ADD_CURRENT:
334 add2hotlist_cmd ();
335 return 0;
336 break;
338 case B_NEW_ENTRY:
339 add_new_entry_cmd ();
340 return 0;
341 break;
343 case B_ENTER:
345 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
346 if (list->current){
347 if (list->current->data) {
348 struct hotlist *hlp = (struct hotlist*) list->current->data;
349 if (hlp->type == HL_TYPE_ENTRY)
350 return 1;
351 else {
352 listbox_remove_list (list);
353 current_group = hlp;
354 fill_listbox ();
355 return 0;
357 } else
358 return 1;
361 /* Fall through if list empty - just go up */
363 case B_UP_GROUP:
365 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
366 listbox_remove_list (list);
367 current_group = current_group->up;
368 fill_listbox ();
369 return 0;
370 break;
373 default:
374 return 1;
375 break;
380 static int hotlist_callback (Dlg_head * h, int Par, int Msg)
382 switch (Msg) {
383 case DLG_DRAW:
384 hotlist_refresh (h);
385 break;
387 case DLG_UNHANDLED_KEY:
388 switch (Par) {
389 case '\n':
390 if (ctrl_pressed())
391 goto l1;
392 case KEY_ENTER:
393 case KEY_RIGHT:
394 if (hotlist_button_callback (B_ENTER, 0)) {
395 h->ret_value = B_ENTER;
396 dlg_stop (h);
398 return 1;
399 break;
400 case KEY_LEFT:
401 if (hotlist_state.type != LIST_VFSLIST )
402 return !hotlist_button_callback (B_UP_GROUP, 0);
403 else
404 return 0;
405 break;
406 case KEY_DC:
407 if (!hotlist_state.moving) {
408 hotlist_button_callback (B_REMOVE, 0);
409 return 1;
411 break;
413 case ALT('\n'):
414 case ALT('\r'):
415 if (!hotlist_state.moving)
417 if (l_hotlist->current){
418 if (l_hotlist->current->data) {
419 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
420 if (hlp->type == HL_TYPE_ENTRY) {
421 char *tmp = g_strconcat ( "cd ", hlp->directory, NULL);
422 stuff (cmdline, tmp, 0);
423 g_free (tmp);
424 dlg_stop (h);
425 h->ret_value = B_CANCEL;
426 return 1;
431 return 1; /* ignore key */
432 default:
433 return 0;
435 break;
437 case DLG_POST_KEY:
438 if (hotlist_state.moving)
439 dlg_select_widget (movelist_dlg, l_movelist);
440 else
441 dlg_select_widget (hotlist_dlg, l_hotlist);
442 /* always stay on hotlist */
443 /* fall through */
445 case DLG_INIT:
446 attrset (MENU_ENTRY_COLOR);
447 update_path_name ();
448 break;
450 return 0;
453 static int l_call (void *l)
455 WListbox *list = (WListbox *) l;
456 Dlg_head *dlg = hotlist_state.moving ? movelist_dlg : hotlist_dlg;
458 if (list->current){
459 if (list->current->data) {
460 struct hotlist *hlp = (struct hotlist*) list->current->data;
461 if (hlp->type == HL_TYPE_ENTRY) {
462 dlg->ret_value = B_ENTER;
463 dlg_stop (dlg);
464 return listbox_finish;
465 } else {
466 hotlist_button_callback (B_ENTER, (void *)0);
467 hotlist_callback (dlg, '\n', DLG_POST_KEY);
468 return listbox_nothing;
470 } else {
471 dlg->ret_value = B_ENTER;
472 dlg_stop (dlg);
473 return listbox_finish;
477 hotlist_button_callback (B_UP_GROUP, (void *)0);
478 hotlist_callback (dlg, 'u', DLG_POST_KEY);
479 return listbox_nothing;
482 #ifdef USE_VFS
483 static void add_name_to_list (char *path)
485 listbox_add_item (l_hotlist, 0, 0, path, 0);
487 #endif /* !USE_VFS */
490 * Expands all button names (once) and recalculates button positions.
491 * returns number of columns in the dialog box, which is 10 chars longer
492 * then buttonbar.
494 * If common width of the window (i.e. in xterm) is less than returned
495 * width - sorry :) (anyway this did not handled in previous version too)
497 static int
498 init_i18n_stuff(int list_type, int cols)
500 register int i;
501 static char* cancel_but = N_("&Cancel");
503 #ifdef ENABLE_NLS
504 static int hotlist_i18n_flag = 0;
506 if (!hotlist_i18n_flag)
508 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
509 while (i--)
510 hotlist_but [i].text = _(hotlist_but [i].text);
512 cancel_but = _(cancel_but);
513 hotlist_i18n_flag = 1;
515 #endif /* ENABLE_NLS */
517 /* Dynamic resizing of buttonbars */
519 int len[2], count[2]; /* at most two lines of buttons */
520 int cur_x[2], row;
522 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
523 len[0] = len[1] = count[0] = count[1] = 0;
525 /* Count len of buttonbars, assuming 2 extra space between buttons */
526 while (i--)
528 if (! (hotlist_but[i].type & list_type))
529 continue;
531 row = hotlist_but [i].y;
532 ++count [row];
533 len [row] += strlen (hotlist_but [i].text) + 5;
534 if (hotlist_but [i].flags == DEFPUSH_BUTTON)
535 len [row] += 2;
537 len[0] -= 2;
538 len[1] -= 2;
540 cols = max(cols, max(len[0], len[1]));
542 /* arrange buttons */
544 cur_x[0] = cur_x[1] = 0;
545 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
546 while (i--)
548 if (! (hotlist_but[i].type & list_type))
549 continue;
551 row = hotlist_but [i].y;
553 if (hotlist_but [i].x != 0)
555 /* not first int the row */
556 if (!strcmp (hotlist_but [i].text, cancel_but))
557 hotlist_but [i].x =
558 cols - strlen (hotlist_but [i].text) - 13;
559 else
560 hotlist_but [i].x = cur_x [row];
563 cur_x [row] += strlen (hotlist_but [i].text) + 2
564 + (hotlist_but [i].flags == DEFPUSH_BUTTON ? 5 : 3);
568 return cols;
571 static void
572 init_hotlist (int list_type)
574 int i;
575 char *title, *help_node;
576 int hotlist_cols;
578 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
580 do_refresh ();
582 hotlist_state.expanded =
583 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
584 0, profile_name);
586 if (list_type == LIST_VFSLIST) {
587 title = _("Active VFS directories");
588 help_node = "[vfshot]"; /* FIXME - no such node */
589 } else {
590 title = _("Directory hotlist");
591 help_node = "[Hotlist]";
594 hotlist_dlg =
595 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
596 hotlist_callback, help_node, title, DLG_CENTER);
598 for (i = 0; i < BUTTONS; i++) {
599 if (hotlist_but[i].type & list_type)
600 add_widget (hotlist_dlg,
601 button_new (BY + hotlist_but[i].y,
602 BX + hotlist_but[i].x,
603 hotlist_but[i].ret_cmd,
604 hotlist_but[i].flags,
605 hotlist_but[i].text,
606 hotlist_button_callback, 0,
607 hotlist_but[i].tkname));
610 /* We add the labels.
611 * pname will hold entry's pathname;
612 * pname_group will hold name of current group
614 pname = label_new (UY - 11 + LINES, UX + 2, "", "the-lab");
615 add_widget (hotlist_dlg, pname);
616 if (!hotlist_state.moving) {
617 add_widget (hotlist_dlg,
618 label_new (UY - 12 + LINES, UX + 1,
619 _(" Directory path "), NULL));
621 /* This one holds the displayed pathname */
622 pname_group = label_new (UY, UX + 1, _(" Directory label "), NULL);
623 add_widget (hotlist_dlg, pname_group);
625 /* get new listbox */
626 l_hotlist =
627 listbox_new (UY + 1, UX + 1, COLS - 2 * UX - 8, LINES - 14,
628 listbox_cback, l_call, "listbox");
630 /* Fill the hotlist with the active VFS or the hotlist */
631 #ifdef USE_VFS
632 if (list_type == LIST_VFSLIST) {
633 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
634 vfs_fill_names (add_name_to_list);
635 } else
636 #endif /* !USE_VFS */
637 fill_listbox ();
639 add_widget (hotlist_dlg, l_hotlist);
640 /* add listbox to the dialogs */
643 static void
644 init_movelist (int list_type, struct hotlist *item)
646 int i;
647 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
648 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
650 do_refresh ();
652 movelist_dlg =
653 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
654 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER);
655 g_free (hdr);
657 for (i = 0; i < BUTTONS; i++) {
658 if (hotlist_but[i].type & list_type)
659 add_widget (movelist_dlg,
660 button_new (BY - 4 + hotlist_but[i].y,
661 BX + hotlist_but[i].x,
662 hotlist_but[i].ret_cmd,
663 hotlist_but[i].flags,
664 hotlist_but[i].text,
665 hotlist_button_callback, 0,
666 hotlist_but[i].tkname));
669 /* We add the labels. We are interested in the last one,
670 * that one will hold the path name label
672 movelist_group = label_new (UY, UX + 1, _(" Directory label "), NULL);
673 add_widget (movelist_dlg, movelist_group);
674 /* get new listbox */
675 l_movelist =
676 listbox_new (UY + 1, UX + 1, movelist_dlg->cols - 2 * UX - 2,
677 movelist_dlg->lines - 8, listbox_cback, l_call,
678 "listbox");
680 fill_listbox ();
682 add_widget (movelist_dlg, l_movelist);
683 /* add listbox to the dialogs */
687 * Destroy the list dialog.
688 * Don't confuse with done_hotlist() for the list in memory.
690 static void hotlist_done (void)
692 destroy_dlg (hotlist_dlg);
693 l_hotlist = NULL;
694 if (0)
695 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
696 repaint_screen ();
699 static char *
700 find_group_section (struct hotlist *grp)
702 return g_strconcat (grp->directory, ".Group", NULL);
707 /* 1.11.96 bor: added pos parameter to control placement of new item.
708 see widget.c, listbox_add_item()
709 now hotlist is in unsorted mode
711 static struct hotlist *
712 add2hotlist (char *label, char *directory, enum HotListType type, int pos)
714 struct hotlist *new;
715 struct hotlist *current = NULL;
718 * Hotlist is neither loaded nor loading.
719 * Must be called by "Ctrl-x a" before using hotlist.
721 if (!current_group)
722 load_hotlist ();
724 if (l_hotlist && l_hotlist->current)
725 current = l_hotlist->current->data;
727 new = new_hotlist ();
729 new->type = type;
730 new->label = label;
731 new->directory = directory;
732 new->up = current_group;
734 if (!current_group->head) { /* first element in group */
735 current_group->head = new;
736 } else if (pos == 2) { /* should be appended after current*/
737 new->next = current->next;
738 current->next = new;
739 } else if (pos == 1 &&
740 current == current_group->head) {
741 /* should be inserted before first item */
742 new->next = current;
743 current_group->head = new;
744 } else if (pos == 1) { /* befor current */
745 struct hotlist *p = current_group->head;
747 while (p->next != current)
748 p = p->next;
750 new->next = current;
751 p->next = new;
752 } else { /* append at the end */
753 struct hotlist *p = current_group->head;
755 while (p->next)
756 p = p->next;
758 p->next = new;
761 if (hotlist_state.running && type != HL_TYPE_COMMENT) {
762 if (type == HL_TYPE_GROUP) {
763 char *lbl = g_strconcat ("->", new->label, NULL);
765 listbox_add_item (l_hotlist, pos, 0, lbl, new);
766 g_free (lbl);
767 } else
768 listbox_add_item (l_hotlist, pos, 0, new->label, new);
769 listbox_select_entry (l_hotlist, l_hotlist->current);
771 return new;
775 #ifdef ENABLE_NLS
777 * Support routine for add_new_entry_input()/add_new_group_input()
778 * Change positions of buttons (first three widgets).
780 * This is just a quick hack. Accurate procedure must take care of
781 * internationalized label lengths and total buttonbar length...assume
782 * 64 is longer anyway.
784 static void add_widgets_i18n(QuickWidget* qw, int len)
786 int i, l[3], space, cur_x;
788 for (i = 0; i < 3; i++)
790 qw [i].text = _(qw [i].text);
791 l[i] = strlen (qw [i].text) + 3;
793 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
795 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
797 qw [i].relative_x = cur_x;
798 qw [i].x_divisions = len;
801 #endif /* ENABLE_NLS */
803 static int add_new_entry_input (char *header, char *text1, char *text2, char *help, char **r1, char **r2)
805 #define RELATIVE_Y_BUTTONS 4
806 #define RELATIVE_Y_LABEL_PTH 3
807 #define RELATIVE_Y_INPUT_PTH 4
809 QuickDialog Quick_input;
810 static QuickWidget quick_widgets [] = {
811 { quick_button, 55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), 0, B_CANCEL,
812 0, 0, "button-cancel" },
813 { quick_button, 30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), 0, B_INSERT,
814 0, 0, "button-insert" },
815 { quick_button, 10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), 0, B_APPEND,
816 0, 0, "button-append" },
817 { quick_input, 4, 80, RELATIVE_Y_INPUT_PTH, 0, "",58, 0,
818 0, 0, "input-pth" },
819 { quick_label, RELATIVE_Y_LABEL_PTH, 80, 3, 0, 0, 0, 0,
820 0, 0, "label-pth" },
821 { quick_input, 4, 80, 3, 0, "", 58, 0,
822 0, 0, "input-lbl" },
823 { quick_label, 3, 80, 2, 0, 0, 0, 0,
824 0, 0, "label-lbl" },
825 { 0 } };
827 int len;
828 int i;
829 int lines1, lines2;
830 char *my_str1, *my_str2;
832 #ifdef ENABLE_NLS
833 static int i18n_flag = 0;
834 #endif /* ENABLE_NLS */
836 len = max (strlen (header), msglen (text1, &lines1));
837 len = max (len, msglen (text2, &lines2)) + 4;
838 len = max (len, 64);
840 #ifdef ENABLE_NLS
841 if (!i18n_flag)
843 add_widgets_i18n(quick_widgets, len);
844 i18n_flag = 1;
846 #endif /* ENABLE_NLS */
848 Quick_input.xlen = len;
849 Quick_input.xpos = -1;
850 Quick_input.title = header;
851 Quick_input.help = help;
852 Quick_input.i18n = 0;
853 quick_widgets [6].text = text1;
854 quick_widgets [4].text = text2;
855 quick_widgets [5].text = *r1;
856 quick_widgets [3].text = *r2;
858 for (i = 0; i < 7; i++)
859 quick_widgets [i].y_divisions = lines1+lines2+7;
860 Quick_input.ylen = lines1 + lines2 + 7;
862 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
863 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
864 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
865 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
866 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
868 quick_widgets [5].str_result = &my_str1;
869 quick_widgets [3].str_result = &my_str2;
871 Quick_input.widgets = quick_widgets;
872 if ((i = quick_dialog (&Quick_input)) != B_CANCEL){
873 *r1 = *(quick_widgets [5].str_result);
874 *r2 = *(quick_widgets [3].str_result);
875 return i;
876 } else
877 return 0;
880 static void add_new_entry_cmd (void)
882 char *title = 0, *url = 0;
883 int ret;
885 /* Take current directory as default value for input fields */
886 title = url = cpanel->cwd;
888 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
889 "[Hotlist]", &title, &url);
891 if (!ret || !title || !*title || !url || !*url)
892 return;
894 if (ret == B_ENTER || ret == B_APPEND)
895 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 2);
896 else
897 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 1);
899 hotlist_state.modified = 1;
902 static int add_new_group_input (char *header, char *label, char **result)
904 int ret;
905 QuickDialog Quick_input;
906 static QuickWidget quick_widgets [] = {
907 { quick_button, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
908 "button-cancel" },
909 { quick_button, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT, 0, 0,
910 "button-insert" },
911 { quick_button, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND, 0, 0,
912 "button-append" },
913 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
914 { quick_label, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
915 { 0 } };
916 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
917 quick_widgets variable above */
918 int len;
919 int i;
920 int lines;
921 char *my_str;
923 #ifdef ENABLE_NLS
924 static int i18n_flag = 0;
925 #endif /* ENABLE_NLS */
927 len = max (strlen (header), msglen (label, &lines)) + 4;
928 len = max (len, 64);
930 #ifdef ENABLE_NLS
931 if (!i18n_flag)
933 add_widgets_i18n(quick_widgets, len);
934 i18n_flag = 1;
936 #endif /* ENABLE_NLS */
938 Quick_input.xlen = len;
939 Quick_input.xpos = -1;
940 Quick_input.title = header;
941 Quick_input.help = "[Hotlist]";
942 Quick_input.i18n = 0;
943 quick_widgets [4].text = label;
945 for (i = 0; i < 5; i++)
946 quick_widgets [i].y_divisions = lines+6;
947 Quick_input.ylen = lines + 6;
949 for (i = 0; i < 4; i++)
950 quick_widgets [i].relative_y = relative_y[i] + 2 + lines;
952 quick_widgets [3].str_result = &my_str;
953 quick_widgets [3].text = "";
955 Quick_input.widgets = quick_widgets;
956 if ((ret = quick_dialog (&Quick_input)) != B_CANCEL){
957 *result = *(quick_widgets [3].str_result);
958 return ret;
959 } else
960 return 0;
963 static void add_new_group_cmd (void)
965 char *label;
966 int ret;
968 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
969 if (!ret || !label || !*label)
970 return;
972 if (ret == B_ENTER || ret == B_APPEND)
973 add2hotlist (label, 0, HL_TYPE_GROUP, 2);
974 else
975 add2hotlist (label, 0, HL_TYPE_GROUP, 1);
977 hotlist_state.modified = 1;
980 void add2hotlist_cmd (void)
982 char *prompt, *label;
983 char *cp = _("Label for \"%s\":");
984 int l = strlen (cp);
986 prompt = g_strdup_printf (cp, name_trunc (cpanel->cwd, COLS-2*UX-(l+8)));
987 label = input_dialog (_(" Add to hotlist "), prompt, cpanel->cwd);
988 g_free (prompt);
989 if (!label || !*label)
990 return;
992 add2hotlist (label,g_strdup (cpanel->cwd), HL_TYPE_ENTRY, 0);
993 hotlist_state.modified = 1;
996 static void remove_group (struct hotlist *grp)
998 struct hotlist *current = grp->head;
1000 while (current) {
1001 struct hotlist *next = current->next;
1003 if (current->type == HL_TYPE_GROUP)
1004 remove_group (current);
1006 if (current->label)
1007 g_free (current->label);
1008 if (current->directory)
1009 g_free (current->directory);
1010 g_free (current);
1012 current = next;
1017 static void remove_from_hotlist (struct hotlist *entry)
1019 if (entry->type == HL_TYPE_GROUP) {
1020 if (entry->head) {
1021 char *header;
1022 int result;
1024 header = g_strconcat (_(" Remove: "),
1025 name_trunc (entry->label, 30),
1026 " ",
1027 NULL);
1028 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1029 D_ERROR, 2,
1030 _("&No"), _("&Yes"));
1031 g_free (header);
1033 if (!result)
1034 return;
1037 remove_group (entry);
1040 unlink_entry (entry);
1042 if (entry->label)
1043 g_free (entry->label);
1044 if (entry->directory)
1045 g_free (entry->directory);
1046 g_free (entry);
1047 /* now remove list entry from screen */
1048 listbox_remove_current (l_hotlist, 1);
1049 hotlist_state.modified = 1;
1052 char *hotlist_cmd (int vfs_or_hotlist)
1054 char *target = NULL;
1056 hotlist_state.type = vfs_or_hotlist;
1057 load_hotlist ();
1059 init_hotlist (vfs_or_hotlist);
1061 /* display file info */
1062 attrset (SELECTED_COLOR);
1064 hotlist_state.running = 1;
1065 run_dlg (hotlist_dlg);
1066 hotlist_state.running = 0;
1067 save_hotlist ();
1069 switch (hotlist_dlg->ret_value) {
1070 case B_CANCEL:
1071 break;
1073 case B_ENTER:
1074 if (l_hotlist->current->data) {
1075 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
1076 target = g_strdup (hlp->directory);
1077 } else
1078 target = g_strdup (l_hotlist->current->text);
1079 break;
1082 hotlist_done ();
1083 return target;
1086 static void
1087 load_group (struct hotlist *grp)
1089 void *profile_keys;
1090 char *key, *value;
1091 char *group_section;
1092 struct hotlist *current = 0;
1094 group_section = find_group_section (grp);
1096 profile_keys = profile_init_iterator (group_section, profile_name);
1098 current_group = grp;
1100 while (profile_keys){
1101 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1102 add2hotlist (g_strdup (value), g_strdup (key), HL_TYPE_GROUP, 0);
1104 g_free (group_section);
1106 profile_keys = profile_init_iterator (grp->directory, profile_name);
1108 while (profile_keys){
1109 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1110 add2hotlist (g_strdup (value),g_strdup (key), HL_TYPE_ENTRY, 0);
1113 for (current = grp->head; current; current = current->next)
1114 load_group (current);
1117 #define TKN_GROUP 0
1118 #define TKN_ENTRY 1
1119 #define TKN_STRING 2
1120 #define TKN_URL 3
1121 #define TKN_ENDGROUP 4
1122 #define TKN_COMMENT 5
1123 #define TKN_EOL 125
1124 #define TKN_EOF 126
1125 #define TKN_UNKNOWN 127
1127 static char *tkn_buf;
1128 static int tkn_buf_length;
1129 static int tkn_length;
1131 static char *hotlist_file_name;
1132 static FILE *hotlist_file;
1133 static time_t hotlist_file_mtime;
1135 static int hot_skip_blanks (void)
1137 int c;
1139 while ((c = getc (hotlist_file)) != EOF && c != '\n' && isspace (c))
1141 return c;
1145 static int hot_next_token (void)
1147 int c;
1149 #define CHECK_BUF() \
1150 do { \
1151 if (tkn_length == tkn_buf_length) \
1152 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1153 : ( g_malloc (tkn_buf_length = 1024)); \
1154 } while (0)
1156 tkn_length = 0;
1158 again:
1159 c = hot_skip_blanks ();
1160 switch (c) {
1161 case EOF:
1162 return TKN_EOF;
1163 break;
1164 case '\n':
1165 return TKN_EOL;
1166 break;
1167 case '#':
1168 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1169 if (c == EOF)
1170 return TKN_EOF;
1171 if (c != '\n') {
1172 CHECK_BUF();
1173 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1176 CHECK_BUF();
1177 tkn_buf[tkn_length] = '\0';
1178 return TKN_COMMENT;
1179 break;
1180 case '"':
1181 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1182 if (c == '\\')
1183 if ((c = getc (hotlist_file)) == EOF)
1184 return TKN_EOF;
1185 CHECK_BUF();
1186 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1188 if (c == EOF)
1189 return TKN_EOF;
1190 CHECK_BUF();
1191 tkn_buf[tkn_length] = '\0';
1192 return TKN_STRING;
1193 break;
1194 case '\\':
1195 if ((c = getc (hotlist_file)) == EOF)
1196 return TKN_EOF;
1197 if (c == '\n')
1198 goto again;
1200 /* fall through; it is taken as normal character */
1202 default:
1203 do {
1204 CHECK_BUF();
1205 tkn_buf[tkn_length++] = toupper(c);
1206 } while ((c = fgetc (hotlist_file)) != EOF && isalnum (c));
1207 if (c != EOF)
1208 ungetc (c, hotlist_file);
1209 CHECK_BUF();
1210 tkn_buf[tkn_length] = '\0';
1211 if (strncmp (tkn_buf, "GROUP", tkn_length) == 0)
1212 return TKN_GROUP;
1213 else if (strncmp (tkn_buf, "ENTRY", tkn_length) == 0)
1214 return TKN_ENTRY;
1215 else if (strncmp (tkn_buf, "ENDGROUP", tkn_length) == 0)
1216 return TKN_ENDGROUP;
1217 else if (strncmp (tkn_buf, "URL", tkn_length) == 0)
1218 return TKN_URL;
1219 else
1220 return TKN_UNKNOWN;
1221 break;
1225 #define SKIP_TO_EOL { \
1226 int _tkn; \
1227 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1230 #define CHECK_TOKEN(_TKN_) \
1231 if ((tkn = hot_next_token ()) != _TKN_) { \
1232 hotlist_state.readonly = 1; \
1233 hotlist_state.file_error = 1; \
1234 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1235 tkn = hot_next_token (); \
1236 break; \
1239 static void
1240 hot_load_group (struct hotlist * grp)
1242 int tkn;
1243 struct hotlist *new_grp;
1244 char *label, *url;
1246 current_group = grp;
1248 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1249 switch (tkn) {
1250 case TKN_GROUP:
1251 CHECK_TOKEN(TKN_STRING);
1252 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1253 SKIP_TO_EOL;
1254 hot_load_group (new_grp);
1255 current_group = grp;
1256 break;
1257 case TKN_ENTRY:
1258 CHECK_TOKEN(TKN_STRING);
1259 label = g_strdup (tkn_buf);
1260 CHECK_TOKEN(TKN_URL);
1261 CHECK_TOKEN(TKN_STRING);
1262 url = g_strdup (tkn_buf);
1263 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1264 SKIP_TO_EOL;
1265 break;
1266 case TKN_COMMENT:
1267 label = g_strdup (tkn_buf);
1268 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1269 break;
1270 case TKN_EOF:
1271 hotlist_state.readonly = 1;
1272 hotlist_state.file_error = 1;
1273 return;
1274 break;
1275 case TKN_EOL:
1276 /* skip empty lines */
1277 break;
1278 default:
1279 hotlist_state.readonly = 1;
1280 hotlist_state.file_error = 1;
1281 SKIP_TO_EOL;
1282 break;
1284 SKIP_TO_EOL;
1287 static void
1288 hot_load_file (struct hotlist * grp)
1290 int tkn;
1291 struct hotlist *new_grp;
1292 char *label, *url;
1294 current_group = grp;
1296 while ((tkn = hot_next_token())!= TKN_EOF)
1297 switch (tkn) {
1298 case TKN_GROUP:
1299 CHECK_TOKEN(TKN_STRING);
1300 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1301 SKIP_TO_EOL;
1302 hot_load_group (new_grp);
1303 current_group = grp;
1304 break;
1305 case TKN_ENTRY:
1306 CHECK_TOKEN(TKN_STRING);
1307 label = g_strdup (tkn_buf);
1308 CHECK_TOKEN(TKN_URL);
1309 CHECK_TOKEN(TKN_STRING);
1310 url = g_strdup (tkn_buf);
1311 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1312 SKIP_TO_EOL;
1313 break;
1314 case TKN_COMMENT:
1315 label = g_strdup (tkn_buf);
1316 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1317 break;
1318 case TKN_EOL:
1319 /* skip empty lines */
1320 break;
1321 default:
1322 hotlist_state.readonly = 1;
1323 hotlist_state.file_error = 1;
1324 SKIP_TO_EOL;
1325 break;
1329 static void
1330 clean_up_hotlist_groups (char *section)
1332 char *grp_section;
1333 void *profile_keys;
1334 char *key, *value;
1336 grp_section = g_strconcat (section, ".Group", NULL);
1337 if (profile_has_section (section, profile_name))
1338 profile_clean_section (section, profile_name);
1339 if (profile_has_section (grp_section, profile_name)) {
1340 profile_keys = profile_init_iterator (grp_section, profile_name);
1342 while (profile_keys) {
1343 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1344 clean_up_hotlist_groups (key);
1346 profile_clean_section (grp_section, profile_name);
1348 g_free (grp_section);
1353 void load_hotlist (void)
1355 char *grp_section;
1356 int has_old_list = 0;
1357 int remove_old_list = 0;
1358 struct stat stat_buf;
1360 if (hotlist_state.loaded) {
1361 stat (hotlist_file_name, &stat_buf);
1362 if (hotlist_file_mtime < stat_buf.st_mtime)
1363 done_hotlist ();
1364 else
1365 return;
1368 if (!hotlist_file_name)
1369 hotlist_file_name = concat_dir_and_file (home_dir, HOTLIST_FILENAME);
1371 hotlist = new_hotlist ();
1372 hotlist->type = HL_TYPE_GROUP;
1373 hotlist->label = g_strdup (_(" Top level group "));
1374 hotlist->up = hotlist;
1376 * compatibility :-(
1378 hotlist->directory = g_strdup ("Hotlist");
1380 grp_section = g_strconcat ("Hotlist", ".Group", NULL);
1381 has_old_list = profile_has_section ("Hotlist", profile_name) ||
1382 profile_has_section (grp_section, profile_name);
1383 g_free (grp_section);
1385 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1386 int result;
1388 load_group (hotlist);
1389 hotlist_state.loaded = 1;
1391 * just to be sure we got copy
1393 hotlist_state.modified = 1;
1394 result = save_hotlist ();
1395 hotlist_state.modified = 0;
1396 if (result) {
1397 remove_old_list = 1;
1398 } else {
1399 char *msg;
1401 msg = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME,
1402 _(" file, your old hotlist entries were not deleted"), NULL);
1404 message (D_ERROR, _(" Hotlist Load "), msg);
1405 g_free (msg);
1407 } else {
1408 hot_load_file (hotlist);
1409 fclose (hotlist_file);
1410 hotlist_state.loaded = 1;
1411 if (has_old_list) {
1412 int result;
1413 char *msg;
1415 msg = g_strconcat (
1416 _("You have ~/"), HOTLIST_FILENAME, _(" file and [Hotlist] section in ~/"), PROFILE_NAME, "\n",
1417 _("Your ~/"), HOTLIST_FILENAME, _(" most probably was created\n"),
1418 _("by an earlier development version of MC\nand is more actual than ~/"),
1419 PROFILE_NAME, _(" entries\n\n"),
1420 _("You can choose between\n\n"
1421 " Remove - remove old hotlist entries from ~/"), PROFILE_NAME, "\n",
1422 _(" Keep - keep your old entries; you will be asked\n"
1423 " the same question next time\n"
1424 " Merge - add old entries to hotlist as group \"Entries from ~/"),
1425 PROFILE_NAME, "\"\n\n", NULL);
1427 result = query_dialog (_(" Hotlist Load "),
1428 msg, D_ERROR, 3, _("&Remove"), _("&Keep"), _("&Merge"));
1429 if (result == 0)
1430 remove_old_list = 1;
1431 else if (result == 2) {
1432 struct hotlist *grp = hotlist->head;
1433 struct hotlist *old;
1435 hotlist->head = 0;
1436 load_group (hotlist);
1438 old = new_hotlist ();
1439 old->type = HL_TYPE_GROUP;
1440 old->label = g_strconcat (_(" Entries from ~/"), PROFILE_NAME, NULL);
1441 old->up = hotlist;
1442 old->head = hotlist->head;
1443 old->next = grp;
1444 hotlist->head = old;
1445 hotlist_state.modified = 1;
1446 if (!save_hotlist ()){
1447 char *str;
1449 str = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME,
1450 _(" file, your old hotlist entries were not deleted"), NULL);
1452 message (D_ERROR, _(" Hotlist Load "), str);
1453 g_free (str);
1454 } else
1455 remove_old_list = 1;
1456 hotlist_state.modified = 0;
1461 if (remove_old_list) {
1462 clean_up_hotlist_groups ("Hotlist");
1463 sync_profiles ();
1466 stat (hotlist_file_name, &stat_buf);
1467 hotlist_file_mtime = stat_buf.st_mtime;
1468 current_group = hotlist;
1471 static void
1472 save_group (struct hotlist *grp)
1474 struct hotlist *current = grp->head;
1475 char *group_section;
1477 group_section = find_group_section (grp);
1479 profile_clean_section (group_section, profile_name);
1480 for (;current && current->type == HL_TYPE_GROUP; current = current->next){
1481 WritePrivateProfileString (group_section,
1482 current->directory,
1483 current->label,
1484 profile_name);
1486 g_free (group_section);
1488 for (current = grp->head;
1489 current && current->type == HL_TYPE_GROUP;
1490 current = current->next)
1491 save_group (current);
1493 profile_clean_section (grp->directory, profile_name);
1494 for (;current; current = current->next){
1495 WritePrivateProfileString (grp->directory,
1496 current->directory,
1497 current->label,
1498 profile_name);
1502 static int list_level = 0;
1504 static void
1505 hot_save_group (struct hotlist *grp)
1507 struct hotlist *current = grp->head;
1508 int i;
1509 char *s;
1511 #define INDENT(n) \
1512 do { \
1513 for (i = 0; i < n; i++) \
1514 putc (' ', hotlist_file); \
1515 } while (0)
1517 for (;current; current = current->next)
1518 switch (current->type) {
1519 case HL_TYPE_GROUP:
1520 INDENT (list_level);
1521 fputs ("GROUP \"", hotlist_file);
1522 for (s = current->label; *s; s++) {
1523 if (*s == '"')
1524 putc ('\\', hotlist_file);
1525 else if (*s == '\\')
1526 putc ('\\', hotlist_file);
1527 putc (*s, hotlist_file);
1529 fputs ("\"\n", hotlist_file);
1530 list_level += 2;
1531 hot_save_group (current);
1532 list_level -= 2;
1533 INDENT (list_level);
1534 fputs ("ENDGROUP\n", hotlist_file);
1535 break;
1536 case HL_TYPE_ENTRY:
1537 INDENT(list_level);
1538 fputs ("ENTRY \"", hotlist_file);
1539 for (s = current->label; *s; s++) {
1540 if (*s == '"')
1541 putc ('\\', hotlist_file);
1542 else if (*s == '\\')
1543 putc ('\\', hotlist_file);
1544 putc (*s, hotlist_file);
1546 fputs ("\" URL \"", hotlist_file);
1547 for (s = current->directory; *s; s++) {
1548 if (*s == '"')
1549 putc ('\\', hotlist_file);
1550 else if (*s == '\\')
1551 putc ('\\', hotlist_file);
1552 putc (*s, hotlist_file);
1554 fputs ("\"\n", hotlist_file);
1555 break;
1556 case HL_TYPE_COMMENT:
1557 fprintf (hotlist_file, "#%s\n", current->label);
1558 break;
1563 int save_hotlist (void)
1565 int saved = 0;
1566 struct stat stat_buf;
1568 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1569 char *fbak = g_strconcat (hotlist_file_name, ".bak", NULL);
1571 rename (hotlist_file_name, fbak);
1572 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1573 if (stat (fbak, &stat_buf) == 0)
1574 chmod (hotlist_file_name, stat_buf.st_mode);
1575 else
1576 chmod (hotlist_file_name, S_IRUSR | S_IWUSR);
1577 hot_save_group (hotlist);
1578 fclose (hotlist_file);
1579 stat (hotlist_file_name, &stat_buf);
1580 hotlist_file_mtime = stat_buf.st_mtime;
1581 saved = 1;
1582 hotlist_state.modified = 0;
1583 } else
1584 rename (fbak, hotlist_file_name);
1585 g_free (fbak);
1588 return saved;
1592 * Unload list from memory.
1593 * Don't confuse with hotlist_done() for GUI.
1595 void done_hotlist (void)
1597 if (hotlist){
1598 remove_group (hotlist);
1599 if (hotlist->label)
1600 g_free (hotlist->label);
1601 if (hotlist->directory)
1602 g_free (hotlist->directory);
1603 g_free (hotlist);
1604 hotlist = 0;
1607 hotlist_state.loaded = 0;
1609 if (hotlist_file_name){
1610 g_free (hotlist_file_name);
1611 hotlist_file_name = 0;
1613 l_hotlist = 0;
1614 current_group = 0;
1615 if (tkn_buf){
1616 g_free (tkn_buf);
1617 tkn_buf_length = 0;
1618 tkn_length = 0;
1619 tkn_buf = NULL;