*** empty log message ***
[midnight-commander.git] / src / hotlist.c
blobec0aadef9aff4819133761a16aabc1b8ff478a6d
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 hotlist_refresh (Dlg_head *dlg)
150 dialog_repaint (dlg, COLOR_NORMAL, COLOR_HOT_NORMAL);
151 attrset (COLOR_NORMAL);
152 draw_box (dlg, 2, 5,
153 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 (input_w (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 init_hotlist (int list_type)
573 int i;
574 int hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
576 do_refresh ();
578 hotlist_state.expanded = GetPrivateProfileInt ("HotlistConfig",
579 "expanded_view_of_groups", 0, profile_name);
581 hotlist_dlg = create_dlg (0, 0, LINES-2, hotlist_cols, dialog_colors,
582 hotlist_callback,
583 list_type == LIST_VFSLIST ? "[vfshot]" : "[Hotlist]",
584 list_type == LIST_VFSLIST ? "vfshot" : "hotlist",
585 DLG_CENTER);
586 x_set_dialog_title (hotlist_dlg,
587 list_type == LIST_VFSLIST ? _("Active VFS directories") : _("Directory hotlist"));
589 #define XTRACT(i) BY+hotlist_but[i].y, BX+hotlist_but[i].x, hotlist_but[i].ret_cmd, hotlist_but[i].flags, hotlist_but[i].text, hotlist_button_callback, 0, hotlist_but[i].tkname
591 for (i = 0; i < BUTTONS; i++){
592 if (hotlist_but[i].type & list_type)
593 add_widget (hotlist_dlg, button_new (XTRACT (i)));
595 #undef XTRACT
597 /* We add the labels.
598 * pname will hold entry's pathname;
599 * pname_group will hold name of current group
601 pname = label_new (UY-11+LINES, UX+2, "", "the-lab");
602 add_widget (hotlist_dlg, pname);
603 if (!hotlist_state.moving) {
604 add_widget (hotlist_dlg, label_new (UY-12+LINES, UX+1, _(" Directory path "), NULL));
606 /* This one holds the displayed pathname */
607 pname_group = label_new (UY, UX+1, _(" Directory label "), NULL);
608 add_widget (hotlist_dlg, pname_group);
610 /* get new listbox */
611 l_hotlist = listbox_new (UY + 1, UX + 1, COLS-2*UX-8, LINES-14, listbox_cback, l_call, "listbox");
613 /* Fill the hotlist with the active VFS or the hotlist */
614 #ifdef USE_VFS
615 if (list_type == LIST_VFSLIST){
616 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
617 vfs_fill_names (add_name_to_list);
618 } else
619 #endif /* !USE_VFS */
620 fill_listbox ();
622 add_widget (hotlist_dlg, l_hotlist);
623 /* add listbox to the dialogs */
626 static void init_movelist (int list_type, struct hotlist *item)
628 int i;
629 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
630 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
632 do_refresh ();
634 movelist_dlg = create_dlg (0, 0, LINES-6, movelist_cols, dialog_colors,
635 hotlist_callback, "[Hotlist]",
636 "movelist",
637 DLG_CENTER);
638 x_set_dialog_title (movelist_dlg, hdr);
639 g_free (hdr);
641 #define XTRACT(i) BY-4+hotlist_but[i].y, BX+hotlist_but[i].x, hotlist_but[i].ret_cmd, hotlist_but[i].flags, hotlist_but[i].text, hotlist_button_callback, 0, hotlist_but[i].tkname
643 for (i = 0; i < BUTTONS; i++){
644 if (hotlist_but[i].type & list_type)
645 add_widget (movelist_dlg, button_new (XTRACT (i)));
648 #undef XTRACT
650 /* We add the labels. We are interested in the last one,
651 * that one will hold the path name label
653 movelist_group = label_new (UY, UX+1, _(" Directory label "), NULL);
654 add_widget (movelist_dlg, movelist_group);
655 /* get new listbox */
656 l_movelist = listbox_new (UY + 1, UX + 1,
657 movelist_dlg->cols - 2*UX - 2, movelist_dlg->lines - 8,
658 listbox_cback, l_call, "listbox");
660 fill_listbox ();
662 add_widget (movelist_dlg, l_movelist);
663 /* add listbox to the dialogs */
667 * Destroy the list dialog.
668 * Don't confuse with done_hotlist() for the list in memory.
670 static void hotlist_done (void)
672 destroy_dlg (hotlist_dlg);
673 l_hotlist = NULL;
674 if (0)
675 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
676 repaint_screen ();
679 static char *
680 find_group_section (struct hotlist *grp)
682 return g_strconcat (grp->directory, ".Group", NULL);
687 /* 1.11.96 bor: added pos parameter to control placement of new item.
688 see widget.c, listbox_add_item()
689 now hotlist is in unsorted mode
691 static struct hotlist *
692 add2hotlist (char *label, char *directory, enum HotListType type, int pos)
694 struct hotlist *new;
695 struct hotlist *current = NULL;
698 * Hotlist is neither loaded nor loading.
699 * Must be called by "Ctrl-x a" before using hotlist.
701 if (!current_group)
702 load_hotlist ();
704 if (l_hotlist && l_hotlist->current)
705 current = l_hotlist->current->data;
707 new = new_hotlist ();
709 new->type = type;
710 new->label = label;
711 new->directory = directory;
712 new->up = current_group;
714 if (!current_group->head) { /* first element in group */
715 current_group->head = new;
716 } else if (pos == 2) { /* should be appended after current*/
717 new->next = current->next;
718 current->next = new;
719 } else if (pos == 1 &&
720 current == current_group->head) {
721 /* should be inserted before first item */
722 new->next = current;
723 current_group->head = new;
724 } else if (pos == 1) { /* befor current */
725 struct hotlist *p = current_group->head;
727 while (p->next != current)
728 p = p->next;
730 new->next = current;
731 p->next = new;
732 } else { /* append at the end */
733 struct hotlist *p = current_group->head;
735 while (p->next)
736 p = p->next;
738 p->next = new;
741 if (hotlist_state.running && type != HL_TYPE_COMMENT) {
742 if (type == HL_TYPE_GROUP) {
743 char *lbl = g_strconcat ("->", new->label, NULL);
745 listbox_add_item (l_hotlist, pos, 0, lbl, new);
746 g_free (lbl);
747 } else
748 listbox_add_item (l_hotlist, pos, 0, new->label, new);
749 listbox_select_entry (l_hotlist, l_hotlist->current);
751 return new;
755 #ifdef ENABLE_NLS
757 * Support routine for add_new_entry_input()/add_new_group_input()
758 * Change positions of buttons (first three widgets).
760 * This is just a quick hack. Accurate procedure must take care of
761 * internationalized label lengths and total buttonbar length...assume
762 * 64 is longer anyway.
764 static void add_widgets_i18n(QuickWidget* qw, int len)
766 int i, l[3], space, cur_x;
768 for (i = 0; i < 3; i++)
770 qw [i].text = _(qw [i].text);
771 l[i] = strlen (qw [i].text) + 3;
773 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
775 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
777 qw [i].relative_x = cur_x;
778 qw [i].x_divisions = len;
781 #endif /* ENABLE_NLS */
783 static int add_new_entry_input (char *header, char *text1, char *text2, char *help, char **r1, char **r2)
785 #define RELATIVE_Y_BUTTONS 4
786 #define RELATIVE_Y_LABEL_PTH 3
787 #define RELATIVE_Y_INPUT_PTH 4
789 QuickDialog Quick_input;
790 static QuickWidget quick_widgets [] = {
791 { quick_button, 55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), 0, B_CANCEL,
792 0, 0, "button-cancel" },
793 { quick_button, 30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), 0, B_INSERT,
794 0, 0, "button-insert" },
795 { quick_button, 10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), 0, B_APPEND,
796 0, 0, "button-append" },
797 { quick_input, 4, 80, RELATIVE_Y_INPUT_PTH, 0, "",58, 0,
798 0, 0, "input-pth" },
799 { quick_label, RELATIVE_Y_LABEL_PTH, 80, 3, 0, 0, 0, 0,
800 0, 0, "label-pth" },
801 { quick_input, 4, 80, 3, 0, "", 58, 0,
802 0, 0, "input-lbl" },
803 { quick_label, 3, 80, 2, 0, 0, 0, 0,
804 0, 0, "label-lbl" },
805 { 0 } };
807 int len;
808 int i;
809 int lines1, lines2;
810 char *my_str1, *my_str2;
812 #ifdef ENABLE_NLS
813 static int i18n_flag = 0;
814 #endif /* ENABLE_NLS */
816 len = max (strlen (header), msglen (text1, &lines1));
817 len = max (len, msglen (text2, &lines2)) + 4;
818 len = max (len, 64);
820 #ifdef ENABLE_NLS
821 if (!i18n_flag)
823 add_widgets_i18n(quick_widgets, len);
824 i18n_flag = 1;
826 #endif /* ENABLE_NLS */
828 Quick_input.xlen = len;
829 Quick_input.xpos = -1;
830 Quick_input.title = header;
831 Quick_input.help = help;
832 Quick_input.class = "hotlist_new_entry";
833 Quick_input.i18n = 0;
834 quick_widgets [6].text = text1;
835 quick_widgets [4].text = text2;
836 quick_widgets [5].text = *r1;
837 quick_widgets [3].text = *r2;
839 for (i = 0; i < 7; i++)
840 quick_widgets [i].y_divisions = lines1+lines2+7;
841 Quick_input.ylen = lines1 + lines2 + 7;
843 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
844 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
845 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
846 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
847 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
849 quick_widgets [5].str_result = &my_str1;
850 quick_widgets [3].str_result = &my_str2;
852 Quick_input.widgets = quick_widgets;
853 if ((i = quick_dialog (&Quick_input)) != B_CANCEL){
854 *r1 = *(quick_widgets [5].str_result);
855 *r2 = *(quick_widgets [3].str_result);
856 return i;
857 } else
858 return 0;
861 static void add_new_entry_cmd (void)
863 char *title = 0, *url = 0;
864 int ret;
866 /* Take current directory as default value for input fields */
867 title = url = cpanel->cwd;
869 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
870 "[Hotlist]", &title, &url);
872 if (!ret || !title || !*title || !url || !*url)
873 return;
875 if (ret == B_ENTER || ret == B_APPEND)
876 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 2);
877 else
878 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 1);
880 hotlist_state.modified = 1;
883 static int add_new_group_input (char *header, char *label, char **result)
885 int ret;
886 QuickDialog Quick_input;
887 static QuickWidget quick_widgets [] = {
888 { quick_button, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
889 "button-cancel" },
890 { quick_button, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT, 0, 0,
891 "button-insert" },
892 { quick_button, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND, 0, 0,
893 "button-append" },
894 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
895 { quick_label, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
896 { 0 } };
897 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
898 quick_widgets variable above */
899 int len;
900 int i;
901 int lines;
902 char *my_str;
904 #ifdef ENABLE_NLS
905 static int i18n_flag = 0;
906 #endif /* ENABLE_NLS */
908 len = max (strlen (header), msglen (label, &lines)) + 4;
909 len = max (len, 64);
911 #ifdef ENABLE_NLS
912 if (!i18n_flag)
914 add_widgets_i18n(quick_widgets, len);
915 i18n_flag = 1;
917 #endif /* ENABLE_NLS */
919 Quick_input.xlen = len;
920 Quick_input.xpos = -1;
921 Quick_input.title = header;
922 Quick_input.help = "[Hotlist]";
923 Quick_input.class = "hotlist_new_group";
924 Quick_input.i18n = 0;
925 quick_widgets [4].text = label;
927 for (i = 0; i < 5; i++)
928 quick_widgets [i].y_divisions = lines+6;
929 Quick_input.ylen = lines + 6;
931 for (i = 0; i < 4; i++)
932 quick_widgets [i].relative_y = relative_y[i] + 2 + lines;
934 quick_widgets [3].str_result = &my_str;
935 quick_widgets [3].text = "";
937 Quick_input.widgets = quick_widgets;
938 if ((ret = quick_dialog (&Quick_input)) != B_CANCEL){
939 *result = *(quick_widgets [3].str_result);
940 return ret;
941 } else
942 return 0;
945 static void add_new_group_cmd (void)
947 char *label;
948 int ret;
950 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
951 if (!ret || !label || !*label)
952 return;
954 if (ret == B_ENTER || ret == B_APPEND)
955 add2hotlist (label, 0, HL_TYPE_GROUP, 2);
956 else
957 add2hotlist (label, 0, HL_TYPE_GROUP, 1);
959 hotlist_state.modified = 1;
962 void add2hotlist_cmd (void)
964 char *prompt, *label;
965 char *cp = _("Label for \"%s\":");
966 int l = strlen (cp);
968 prompt = g_strdup_printf (cp, name_trunc (cpanel->cwd, COLS-2*UX-(l+8)));
969 label = input_dialog (_(" Add to hotlist "), prompt, cpanel->cwd);
970 g_free (prompt);
971 if (!label || !*label)
972 return;
974 add2hotlist (label,g_strdup (cpanel->cwd), HL_TYPE_ENTRY, 0);
975 hotlist_state.modified = 1;
978 static void remove_group (struct hotlist *grp)
980 struct hotlist *current = grp->head;
982 while (current) {
983 struct hotlist *next = current->next;
985 if (current->type == HL_TYPE_GROUP)
986 remove_group (current);
988 if (current->label)
989 g_free (current->label);
990 if (current->directory)
991 g_free (current->directory);
992 g_free (current);
994 current = next;
999 static void remove_from_hotlist (struct hotlist *entry)
1001 if (entry->type == HL_TYPE_GROUP) {
1002 if (entry->head) {
1003 char *header;
1004 int result;
1006 header = g_strconcat (_(" Remove: "),
1007 name_trunc (entry->label, 30),
1008 " ",
1009 NULL);
1010 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1011 D_ERROR, 2,
1012 _("&No"), _("&Yes"));
1013 g_free (header);
1015 if (!result)
1016 return;
1019 remove_group (entry);
1022 unlink_entry (entry);
1024 if (entry->label)
1025 g_free (entry->label);
1026 if (entry->directory)
1027 g_free (entry->directory);
1028 g_free (entry);
1029 /* now remove list entry from screen */
1030 listbox_remove_current (l_hotlist, 1);
1031 hotlist_state.modified = 1;
1034 char *hotlist_cmd (int vfs_or_hotlist)
1036 char *target = NULL;
1038 hotlist_state.type = vfs_or_hotlist;
1039 load_hotlist ();
1041 init_hotlist (vfs_or_hotlist);
1043 /* display file info */
1044 attrset (SELECTED_COLOR);
1046 hotlist_state.running = 1;
1047 run_dlg (hotlist_dlg);
1048 hotlist_state.running = 0;
1049 save_hotlist ();
1051 switch (hotlist_dlg->ret_value) {
1052 case B_CANCEL:
1053 break;
1055 case B_ENTER:
1056 if (l_hotlist->current->data) {
1057 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
1058 target = g_strdup (hlp->directory);
1059 } else
1060 target = g_strdup (l_hotlist->current->text);
1061 break;
1064 hotlist_done ();
1065 return target;
1068 static void
1069 load_group (struct hotlist *grp)
1071 void *profile_keys;
1072 char *key, *value;
1073 char *group_section;
1074 struct hotlist *current = 0;
1076 group_section = find_group_section (grp);
1078 profile_keys = profile_init_iterator (group_section, profile_name);
1080 current_group = grp;
1082 while (profile_keys){
1083 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1084 add2hotlist (g_strdup (value), g_strdup (key), HL_TYPE_GROUP, 0);
1086 g_free (group_section);
1088 profile_keys = profile_init_iterator (grp->directory, profile_name);
1090 while (profile_keys){
1091 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1092 add2hotlist (g_strdup (value),g_strdup (key), HL_TYPE_ENTRY, 0);
1095 for (current = grp->head; current; current = current->next)
1096 load_group (current);
1099 #define TKN_GROUP 0
1100 #define TKN_ENTRY 1
1101 #define TKN_STRING 2
1102 #define TKN_URL 3
1103 #define TKN_ENDGROUP 4
1104 #define TKN_COMMENT 5
1105 #define TKN_EOL 125
1106 #define TKN_EOF 126
1107 #define TKN_UNKNOWN 127
1109 static char *tkn_buf;
1110 static int tkn_buf_length;
1111 static int tkn_length;
1113 static char *hotlist_file_name;
1114 static FILE *hotlist_file;
1115 static time_t hotlist_file_mtime;
1117 static int hot_skip_blanks (void)
1119 int c;
1121 while ((c = getc (hotlist_file)) != EOF && c != '\n' && isspace (c))
1123 return c;
1127 static int hot_next_token (void)
1129 int c;
1131 #define CHECK_BUF() \
1132 do { \
1133 if (tkn_length == tkn_buf_length) \
1134 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1135 : ( g_malloc (tkn_buf_length = 1024)); \
1136 } while (0)
1138 tkn_length = 0;
1140 again:
1141 c = hot_skip_blanks ();
1142 switch (c) {
1143 case EOF:
1144 return TKN_EOF;
1145 break;
1146 case '\n':
1147 return TKN_EOL;
1148 break;
1149 case '#':
1150 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1151 if (c == EOF)
1152 return TKN_EOF;
1153 if (c != '\n') {
1154 CHECK_BUF();
1155 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1158 CHECK_BUF();
1159 tkn_buf[tkn_length] = '\0';
1160 return TKN_COMMENT;
1161 break;
1162 case '"':
1163 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1164 if (c == '\\')
1165 if ((c = getc (hotlist_file)) == EOF)
1166 return TKN_EOF;
1167 CHECK_BUF();
1168 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1170 if (c == EOF)
1171 return TKN_EOF;
1172 CHECK_BUF();
1173 tkn_buf[tkn_length] = '\0';
1174 return TKN_STRING;
1175 break;
1176 case '\\':
1177 if ((c = getc (hotlist_file)) == EOF)
1178 return TKN_EOF;
1179 if (c == '\n')
1180 goto again;
1182 /* fall through; it is taken as normal character */
1184 default:
1185 do {
1186 CHECK_BUF();
1187 tkn_buf[tkn_length++] = toupper(c);
1188 } while ((c = fgetc (hotlist_file)) != EOF && isalnum (c));
1189 if (c != EOF)
1190 ungetc (c, hotlist_file);
1191 CHECK_BUF();
1192 tkn_buf[tkn_length] = '\0';
1193 if (strncmp (tkn_buf, "GROUP", tkn_length) == 0)
1194 return TKN_GROUP;
1195 else if (strncmp (tkn_buf, "ENTRY", tkn_length) == 0)
1196 return TKN_ENTRY;
1197 else if (strncmp (tkn_buf, "ENDGROUP", tkn_length) == 0)
1198 return TKN_ENDGROUP;
1199 else if (strncmp (tkn_buf, "URL", tkn_length) == 0)
1200 return TKN_URL;
1201 else
1202 return TKN_UNKNOWN;
1203 break;
1207 #define SKIP_TO_EOL { \
1208 int _tkn; \
1209 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1212 #define CHECK_TOKEN(_TKN_) \
1213 if ((tkn = hot_next_token ()) != _TKN_) { \
1214 hotlist_state.readonly = 1; \
1215 hotlist_state.file_error = 1; \
1216 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1217 tkn = hot_next_token (); \
1218 break; \
1221 static void
1222 hot_load_group (struct hotlist * grp)
1224 int tkn;
1225 struct hotlist *new_grp;
1226 char *label, *url;
1228 current_group = grp;
1230 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1231 switch (tkn) {
1232 case TKN_GROUP:
1233 CHECK_TOKEN(TKN_STRING);
1234 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1235 SKIP_TO_EOL;
1236 hot_load_group (new_grp);
1237 current_group = grp;
1238 break;
1239 case TKN_ENTRY:
1240 CHECK_TOKEN(TKN_STRING);
1241 label = g_strdup (tkn_buf);
1242 CHECK_TOKEN(TKN_URL);
1243 CHECK_TOKEN(TKN_STRING);
1244 url = g_strdup (tkn_buf);
1245 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1246 SKIP_TO_EOL;
1247 break;
1248 case TKN_COMMENT:
1249 label = g_strdup (tkn_buf);
1250 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1251 break;
1252 case TKN_EOF:
1253 hotlist_state.readonly = 1;
1254 hotlist_state.file_error = 1;
1255 return;
1256 break;
1257 case TKN_EOL:
1258 /* skip empty lines */
1259 break;
1260 default:
1261 hotlist_state.readonly = 1;
1262 hotlist_state.file_error = 1;
1263 SKIP_TO_EOL;
1264 break;
1266 SKIP_TO_EOL;
1269 static void
1270 hot_load_file (struct hotlist * grp)
1272 int tkn;
1273 struct hotlist *new_grp;
1274 char *label, *url;
1276 current_group = grp;
1278 while ((tkn = hot_next_token())!= TKN_EOF)
1279 switch (tkn) {
1280 case TKN_GROUP:
1281 CHECK_TOKEN(TKN_STRING);
1282 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1283 SKIP_TO_EOL;
1284 hot_load_group (new_grp);
1285 current_group = grp;
1286 break;
1287 case TKN_ENTRY:
1288 CHECK_TOKEN(TKN_STRING);
1289 label = g_strdup (tkn_buf);
1290 CHECK_TOKEN(TKN_URL);
1291 CHECK_TOKEN(TKN_STRING);
1292 url = g_strdup (tkn_buf);
1293 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1294 SKIP_TO_EOL;
1295 break;
1296 case TKN_COMMENT:
1297 label = g_strdup (tkn_buf);
1298 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1299 break;
1300 case TKN_EOL:
1301 /* skip empty lines */
1302 break;
1303 default:
1304 hotlist_state.readonly = 1;
1305 hotlist_state.file_error = 1;
1306 SKIP_TO_EOL;
1307 break;
1311 static void
1312 clean_up_hotlist_groups (char *section)
1314 char *grp_section;
1315 void *profile_keys;
1316 char *key, *value;
1318 grp_section = g_strconcat (section, ".Group", NULL);
1319 if (profile_has_section (section, profile_name))
1320 profile_clean_section (section, profile_name);
1321 if (profile_has_section (grp_section, profile_name)) {
1322 profile_keys = profile_init_iterator (grp_section, profile_name);
1324 while (profile_keys) {
1325 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1326 clean_up_hotlist_groups (key);
1328 profile_clean_section (grp_section, profile_name);
1330 g_free (grp_section);
1335 void load_hotlist (void)
1337 char *grp_section;
1338 int has_old_list = 0;
1339 int remove_old_list = 0;
1340 struct stat stat_buf;
1342 if (hotlist_state.loaded) {
1343 stat (hotlist_file_name, &stat_buf);
1344 if (hotlist_file_mtime < stat_buf.st_mtime)
1345 done_hotlist ();
1346 else
1347 return;
1350 if (!hotlist_file_name)
1351 hotlist_file_name = concat_dir_and_file (home_dir, HOTLIST_FILENAME);
1353 hotlist = new_hotlist ();
1354 hotlist->type = HL_TYPE_GROUP;
1355 hotlist->label = g_strdup (_(" Top level group "));
1356 hotlist->up = hotlist;
1358 * compatibility :-(
1360 hotlist->directory = g_strdup ("Hotlist");
1362 grp_section = g_strconcat ("Hotlist", ".Group", NULL);
1363 has_old_list = profile_has_section ("Hotlist", profile_name) ||
1364 profile_has_section (grp_section, profile_name);
1365 g_free (grp_section);
1367 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1368 int result;
1370 load_group (hotlist);
1371 hotlist_state.loaded = 1;
1373 * just to be sure we got copy
1375 hotlist_state.modified = 1;
1376 result = save_hotlist ();
1377 hotlist_state.modified = 0;
1378 if (result) {
1379 remove_old_list = 1;
1380 } else {
1381 char *msg;
1383 msg = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME,
1384 _(" file, your old hotlist entries were not deleted"), NULL);
1386 message (D_ERROR, _(" Hotlist Load "), msg);
1387 g_free (msg);
1389 } else {
1390 hot_load_file (hotlist);
1391 fclose (hotlist_file);
1392 hotlist_state.loaded = 1;
1393 if (has_old_list) {
1394 int result;
1395 char *msg;
1397 msg = g_strconcat (
1398 _("You have ~/"), HOTLIST_FILENAME, _(" file and [Hotlist] section in ~/"), PROFILE_NAME, "\n",
1399 _("Your ~/"), HOTLIST_FILENAME, _(" most probably was created\n"),
1400 _("by an earlier development version of MC\nand is more actual than ~/"),
1401 PROFILE_NAME, _(" entries\n\n"),
1402 _("You can choose between\n\n"
1403 " Remove - remove old hotlist entries from ~/"), PROFILE_NAME, "\n",
1404 _(" Keep - keep your old entries; you will be asked\n"
1405 " the same question next time\n"
1406 " Merge - add old entries to hotlist as group \"Entries from ~/"),
1407 PROFILE_NAME, "\"\n\n", NULL);
1409 result = query_dialog (_(" Hotlist Load "),
1410 msg, D_ERROR, 3, _("&Remove"), _("&Keep"), _("&Merge"));
1411 if (result == 0)
1412 remove_old_list = 1;
1413 else if (result == 2) {
1414 struct hotlist *grp = hotlist->head;
1415 struct hotlist *old;
1417 hotlist->head = 0;
1418 load_group (hotlist);
1420 old = new_hotlist ();
1421 old->type = HL_TYPE_GROUP;
1422 old->label = g_strconcat (_(" Entries from ~/"), PROFILE_NAME, NULL);
1423 old->up = hotlist;
1424 old->head = hotlist->head;
1425 old->next = grp;
1426 hotlist->head = old;
1427 hotlist_state.modified = 1;
1428 if (!save_hotlist ()){
1429 char *str;
1431 str = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME,
1432 _(" file your old hotlist entries were not deleted"), NULL);
1434 message (D_ERROR, _(" Hotlist Load "), str);
1435 g_free (str);
1436 } else
1437 remove_old_list = 1;
1438 hotlist_state.modified = 0;
1443 if (remove_old_list) {
1444 clean_up_hotlist_groups ("Hotlist");
1445 sync_profiles ();
1448 stat (hotlist_file_name, &stat_buf);
1449 hotlist_file_mtime = stat_buf.st_mtime;
1450 current_group = hotlist;
1453 static void
1454 save_group (struct hotlist *grp)
1456 struct hotlist *current = grp->head;
1457 char *group_section;
1459 group_section = find_group_section (grp);
1461 profile_clean_section (group_section, profile_name);
1462 for (;current && current->type == HL_TYPE_GROUP; current = current->next){
1463 WritePrivateProfileString (group_section,
1464 current->directory,
1465 current->label,
1466 profile_name);
1468 g_free (group_section);
1470 for (current = grp->head;
1471 current && current->type == HL_TYPE_GROUP;
1472 current = current->next)
1473 save_group (current);
1475 profile_clean_section (grp->directory, profile_name);
1476 for (;current; current = current->next){
1477 WritePrivateProfileString (grp->directory,
1478 current->directory,
1479 current->label,
1480 profile_name);
1484 static int list_level = 0;
1486 static void
1487 hot_save_group (struct hotlist *grp)
1489 struct hotlist *current = grp->head;
1490 int i;
1491 char *s;
1493 #define INDENT(n) \
1494 do { \
1495 for (i = 0; i < n; i++) \
1496 putc (' ', hotlist_file); \
1497 } while (0)
1499 for (;current; current = current->next)
1500 switch (current->type) {
1501 case HL_TYPE_GROUP:
1502 INDENT (list_level);
1503 fputs ("GROUP \"", hotlist_file);
1504 for (s = current->label; *s; s++) {
1505 if (*s == '"')
1506 putc ('\\', hotlist_file);
1507 else if (*s == '\\')
1508 putc ('\\', hotlist_file);
1509 putc (*s, hotlist_file);
1511 fputs ("\"\n", hotlist_file);
1512 list_level += 2;
1513 hot_save_group (current);
1514 list_level -= 2;
1515 INDENT (list_level);
1516 fputs ("ENDGROUP\n", hotlist_file);
1517 break;
1518 case HL_TYPE_ENTRY:
1519 INDENT(list_level);
1520 fputs ("ENTRY \"", hotlist_file);
1521 for (s = current->label; *s; s++) {
1522 if (*s == '"')
1523 putc ('\\', hotlist_file);
1524 else if (*s == '\\')
1525 putc ('\\', hotlist_file);
1526 putc (*s, hotlist_file);
1528 fputs ("\" URL \"", hotlist_file);
1529 for (s = current->directory; *s; s++) {
1530 if (*s == '"')
1531 putc ('\\', hotlist_file);
1532 else if (*s == '\\')
1533 putc ('\\', hotlist_file);
1534 putc (*s, hotlist_file);
1536 fputs ("\"\n", hotlist_file);
1537 break;
1538 case HL_TYPE_COMMENT:
1539 fprintf (hotlist_file, "#%s\n", current->label);
1540 break;
1545 int save_hotlist (void)
1547 int saved = 0;
1548 struct stat stat_buf;
1550 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1551 char *fbak = g_strconcat (hotlist_file_name, ".bak", NULL);
1553 rename (hotlist_file_name, fbak);
1554 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1555 if (stat (fbak, &stat_buf) == 0)
1556 chmod (hotlist_file_name, stat_buf.st_mode);
1557 else
1558 chmod (hotlist_file_name, S_IRUSR | S_IWUSR);
1559 hot_save_group (hotlist);
1560 fclose (hotlist_file);
1561 stat (hotlist_file_name, &stat_buf);
1562 hotlist_file_mtime = stat_buf.st_mtime;
1563 saved = 1;
1564 hotlist_state.modified = 0;
1565 } else
1566 rename (fbak, hotlist_file_name);
1567 g_free (fbak);
1570 return saved;
1574 * Unload list from memory.
1575 * Don't confuse with hotlist_done() for GUI.
1577 void done_hotlist (void)
1579 if (hotlist){
1580 remove_group (hotlist);
1581 if (hotlist->label)
1582 g_free (hotlist->label);
1583 if (hotlist->directory)
1584 g_free (hotlist->directory);
1585 g_free (hotlist);
1586 hotlist = 0;
1589 hotlist_state.loaded = 0;
1591 if (hotlist_file_name){
1592 g_free (hotlist_file_name);
1593 hotlist_file_name = 0;
1595 l_hotlist = 0;
1596 current_group = 0;
1597 if (tkn_buf){
1598 g_free (tkn_buf);
1599 tkn_buf_length = 0;
1600 tkn_length = 0;
1601 tkn_buf = NULL;