Updated italian translation.
[midnight-commander.git] / src / hotlist.c
blob81ea8e22fa01f977454f62b90e2662f1fc24c6f4
1 /* Directory hotlist -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 Written by:
6 1994 Radek Doulik
7 1995 Janne Kukonlehto
8 1996 Andrej Borsenkow
9 1997 Norbert Warmuth
11 Janne did the original Hotlist code, Andrej made the groupable
12 hotlist; the move hotlist and revamped the file format and made
13 it stronger.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 /** \file hotlist.c
31 * \brief Source: directory hotlist
34 #include <config.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
43 #include "lib/global.h"
45 #include "lib/tty/tty.h" /* COLS */
46 #include "lib/tty/key.h" /* KEY_M_CTRL */
47 #include "lib/skin.h" /* colors */
48 #include "lib/mcconfig.h" /* Load/save directories hotlist */
49 #include "lib/fileloc.h"
50 #include "lib/strutil.h"
52 #include "dialog.h"
53 #include "widget.h"
54 #include "setup.h" /* For profile_bname */
55 #include "wtools.h" /* QuickDialog */
56 #include "panel.h" /* current_panel */
57 #include "main.h" /* update_panels() */
58 #include "layout.h" /* repaint_screen() */
59 #include "hotlist.h"
60 #include "command.h" /* cmdline */
61 #include "history.h"
63 #define UX 5
64 #define UY 2
66 #define BX UX
67 #define BY (LINES - 6)
69 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
70 #define LABELS 3
71 #define B_ADD_CURRENT B_USER
72 #define B_REMOVE (B_USER + 1)
73 #define B_NEW_GROUP (B_USER + 2)
74 #define B_NEW_ENTRY (B_USER + 3)
75 #define B_UP_GROUP (B_USER + 4)
76 #define B_INSERT (B_USER + 5)
77 #define B_APPEND (B_USER + 6)
78 #define B_MOVE (B_USER + 7)
80 #ifdef ENABLE_VFS
81 #include "lib/vfs/mc-vfs/gc.h"
82 #define B_FREE_ALL_VFS (B_USER + 8)
83 #define B_REFRESH_VFS (B_USER + 9)
84 #endif
86 int hotlist_has_dot_dot = 1;
88 static WListbox *l_hotlist;
89 static WListbox *l_movelist;
91 static Dlg_head *hotlist_dlg;
92 static Dlg_head *movelist_dlg;
94 static WLabel *pname, *pname_group, *movelist_group;
96 enum HotListType {
97 HL_TYPE_GROUP,
98 HL_TYPE_ENTRY,
99 HL_TYPE_COMMENT,
100 HL_TYPE_DOTDOT
103 static struct {
105 * these parameters are intended to be user configurable
107 int expanded; /* expanded view of all groups at startup */
110 * these reflect run time state
113 int loaded; /* hotlist is loaded */
114 int readonly; /* hotlist readonly */
115 int file_error; /* parse error while reading file */
116 int running; /* we are running dlg (and have to
117 update listbox */
118 int moving; /* we are in moving hotlist currently */
119 int modified; /* hotlist was modified */
120 int type; /* LIST_HOTLIST || LIST_VFSLIST */
121 } hotlist_state;
123 static struct _hotlist_but {
124 int ret_cmd, flags, y, x;
125 const char *text;
126 int type;
127 widget_pos_flags_t pos_flags;
128 } hotlist_but[] = {
129 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"),
130 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
131 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"),
132 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
133 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"),
134 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
135 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"),
136 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
137 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &Entry"),
138 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
139 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &Group"),
140 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
141 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"),
142 LIST_HOTLIST | LIST_VFSLIST|LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
143 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"),
144 LIST_HOTLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
145 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),
146 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
147 #ifdef ENABLE_VFS
148 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"),
149 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
150 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"),
151 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
152 #endif
153 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &To"),
154 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM }
157 /* Directory hotlist */
158 static struct hotlist{
159 enum HotListType type;
160 char *directory;
161 char *label;
162 struct hotlist *head;
163 struct hotlist *up;
164 struct hotlist *next;
165 } *hotlist = NULL;
167 static struct hotlist *current_group;
169 static void init_movelist (int, struct hotlist *);
170 static void add_new_group_cmd (void);
171 static void add_new_entry_cmd (void);
172 static void remove_from_hotlist (struct hotlist *entry);
173 static void load_hotlist (void);
174 static void add_dotdot_to_list (void);
176 #define new_hotlist() g_new0(struct hotlist, 1)
178 static void
179 hotlist_refresh (Dlg_head * dlg)
181 common_dialog_repaint (dlg);
182 tty_setcolor (COLOR_NORMAL);
183 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10),
184 dlg->cols - (UX * 2));
185 if (!hotlist_state.moving)
186 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2));
189 /* If current->data is 0, then we are dealing with a VFS pathname */
190 static void
191 update_path_name (void)
193 const char *text = "";
194 char *p;
195 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
196 Dlg_head *dlg = list->widget.parent;
198 if (list->count != 0) {
199 char *ctext = NULL;
200 void *cdata = NULL;
202 listbox_get_current (list, &ctext, &cdata);
203 if (cdata == NULL)
204 text = ctext;
205 else {
206 struct hotlist *hlp = (struct hotlist *) cdata;
208 if (hlp->type == HL_TYPE_ENTRY ||
209 hlp->type == HL_TYPE_DOTDOT)
210 text = hlp->directory;
211 else if (hlp->type == HL_TYPE_GROUP)
212 text = _("Subgroup - press ENTER to see list");
215 if (!hotlist_state.moving)
216 label_set_text (pname,
217 str_trunc (text, dlg->cols - (UX * 2 + 4)));
219 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
220 if (!hotlist_state.moving)
221 label_set_text (pname_group,
222 str_trunc (p, dlg->cols - (UX * 2 + 4)));
223 else
224 label_set_text (movelist_group,
225 str_trunc (p, dlg->cols - (UX * 2 + 4)));
226 g_free (p);
228 dlg_redraw (dlg);
231 #define CHECK_BUFFER \
232 do { \
233 int i; \
235 if ((i = strlen (current->label) + 3) > buflen) { \
236 g_free (buf); \
237 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
239 buf[0] = '\0'; \
240 } while (0)
242 static void fill_listbox (void)
244 struct hotlist *current = current_group->head;
245 GString *buff = g_string_new ("");
247 while (current){
248 switch (current->type) {
249 case HL_TYPE_GROUP:
251 /* buff clean up */
252 g_string_truncate(buff, 0);
253 g_string_append(buff,"->");
254 g_string_append(buff,current->label);
255 if (hotlist_state.moving)
256 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
257 else
258 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
260 break;
261 case HL_TYPE_DOTDOT:
262 case HL_TYPE_ENTRY:
263 if (hotlist_state.moving)
264 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, current->label, current);
265 else
266 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, current->label, current);
267 break;
268 default:
269 break;
271 current = current->next;
273 g_string_free (buff, TRUE);
276 static void
277 unlink_entry (struct hotlist *entry)
279 struct hotlist *current = current_group->head;
281 if (current == entry)
282 current_group->head = entry->next;
283 else {
284 while (current && current->next != entry)
285 current = current->next;
286 if (current)
287 current->next = entry->next;
289 entry->next =
290 entry->up = 0;
293 #ifdef ENABLE_VFS
294 static void add_name_to_list (const char *path)
296 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, path, 0);
298 #endif /* !ENABLE_VFS */
300 static int
301 hotlist_button_callback (int action)
303 switch (action) {
304 case B_MOVE:
306 struct hotlist *saved = current_group;
307 struct hotlist *item = NULL;
308 struct hotlist *moveto_item = NULL;
309 struct hotlist *moveto_group = NULL;
310 int ret;
312 if (l_hotlist->count == 0)
313 return MSG_NOT_HANDLED; /* empty group - nothing to do */
315 listbox_get_current (l_hotlist, NULL, (void **) &item);
316 hotlist_state.moving = 1;
317 init_movelist (LIST_MOVELIST, item);
319 ret = run_dlg (movelist_dlg);
321 hotlist_state.moving = 0;
322 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
323 moveto_group = current_group;
324 destroy_dlg (movelist_dlg);
325 current_group = saved;
326 if (ret == B_CANCEL)
327 return MSG_NOT_HANDLED;
328 if (moveto_item == item)
329 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
330 it hardly changes anything ;) */
331 unlink_entry (item);
332 listbox_remove_current (l_hotlist);
333 item->up = moveto_group;
334 if (!moveto_group->head)
335 moveto_group->head = item;
336 else if (!moveto_item) { /* we have group with just comments */
337 struct hotlist *p = moveto_group->head;
339 /* skip comments */
340 while (p->next)
341 p = p->next;
342 p->next = item;
343 } else if (ret == B_ENTER || ret == B_APPEND)
344 if (!moveto_item->next)
345 moveto_item->next = item;
346 else {
347 item->next = moveto_item->next;
348 moveto_item->next = item;
349 } else if (moveto_group->head == moveto_item) {
350 moveto_group->head = item;
351 item->next = moveto_item;
352 } else {
353 struct hotlist *p = moveto_group->head;
355 while (p->next != moveto_item)
356 p = p->next;
357 item->next = p->next;
358 p->next = item;
360 listbox_remove_list (l_hotlist);
361 fill_listbox ();
362 repaint_screen ();
363 hotlist_state.modified = 1;
364 return MSG_NOT_HANDLED;
366 case B_REMOVE:
368 struct hotlist *entry = NULL;
369 listbox_get_current (l_hotlist, NULL, (void **) &entry);
370 remove_from_hotlist (entry);
372 return MSG_NOT_HANDLED;
374 case B_NEW_GROUP:
375 add_new_group_cmd ();
376 return MSG_NOT_HANDLED;
378 case B_ADD_CURRENT:
379 add2hotlist_cmd ();
380 return MSG_NOT_HANDLED;
382 case B_NEW_ENTRY:
383 add_new_entry_cmd ();
384 return MSG_NOT_HANDLED;
386 case B_ENTER:
388 WListbox *list;
389 void *data;
390 struct hotlist *hlp;
392 list = hotlist_state.moving ? l_movelist : l_hotlist;
393 listbox_get_current (list, NULL, &data);
395 if (data == NULL)
396 return MSG_HANDLED;
398 hlp = (struct hotlist *) data;
400 if (hlp->type == HL_TYPE_ENTRY)
401 return MSG_HANDLED;
402 if (hlp->type != HL_TYPE_DOTDOT) {
403 listbox_remove_list (list);
404 current_group = hlp;
405 fill_listbox ();
406 return MSG_NOT_HANDLED;
408 /* Fall through - go up */
410 /* Fall through if list empty - just go up */
412 case B_UP_GROUP:
414 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
415 listbox_remove_list (list);
416 current_group = current_group->up;
417 fill_listbox ();
418 return MSG_NOT_HANDLED;
421 #ifdef ENABLE_VFS
422 case B_FREE_ALL_VFS:
423 vfs_expire (1);
424 /* fall through */
426 case B_REFRESH_VFS:
427 listbox_remove_list (l_hotlist);
428 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
429 vfs_fill_names (add_name_to_list);
430 return MSG_NOT_HANDLED;
431 #endif /* ENABLE_VFS */
433 default:
434 return MSG_HANDLED;
438 static inline cb_ret_t
439 hotlist_handle_key (Dlg_head *h, int key)
441 switch (key) {
442 case KEY_M_CTRL | '\n':
443 goto l1;
445 case '\n':
446 case KEY_ENTER:
447 case KEY_RIGHT:
448 if (hotlist_button_callback (B_ENTER)) {
449 h->ret_value = B_ENTER;
450 dlg_stop (h);
452 return MSG_HANDLED;
454 case KEY_LEFT:
455 if (hotlist_state.type != LIST_VFSLIST)
456 return !hotlist_button_callback (B_UP_GROUP);
457 else
458 return MSG_NOT_HANDLED;
460 case KEY_DC:
461 if (hotlist_state.moving)
462 return MSG_NOT_HANDLED;
463 else {
464 hotlist_button_callback (B_REMOVE);
465 return MSG_HANDLED;
469 case ALT ('\n'):
470 case ALT ('\r'):
471 if (!hotlist_state.moving) {
472 void *ldata = NULL;
474 listbox_get_current (l_hotlist, NULL, &ldata);
476 if (ldata != NULL) {
477 struct hotlist *hlp = (struct hotlist *) ldata;
479 if (hlp->type == HL_TYPE_ENTRY) {
480 char *tmp;
482 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
483 stuff (cmdline, tmp, 0);
484 g_free (tmp);
485 h->ret_value = B_CANCEL;
486 dlg_stop (h);
490 return MSG_HANDLED; /* ignore key */
492 default:
493 return MSG_NOT_HANDLED;
497 static cb_ret_t
498 hotlist_callback (Dlg_head *h, Widget *sender,
499 dlg_msg_t msg, int parm, void *data)
501 switch (msg) {
502 case DLG_DRAW:
503 hotlist_refresh (h);
504 return MSG_HANDLED;
506 case DLG_UNHANDLED_KEY:
507 return hotlist_handle_key (h, parm);
509 case DLG_POST_KEY:
510 if (hotlist_state.moving)
511 dlg_select_widget (l_movelist);
512 else
513 dlg_select_widget (l_hotlist);
514 /* always stay on hotlist */
515 /* fall through */
517 case DLG_INIT:
518 tty_setcolor (MENU_ENTRY_COLOR);
519 update_path_name ();
520 return MSG_HANDLED;
522 case DLG_RESIZE:
523 /* simply call dlg_set_size() with new size */
524 dlg_set_size (h, LINES - 2, COLS - 6);
525 return MSG_HANDLED;
527 default:
528 return default_dlg_callback (h, sender, msg, parm, data);
532 static int
533 l_call (WListbox *list)
535 Dlg_head *dlg = list->widget.parent;
537 if (list->count != 0) {
538 void *data = NULL;
540 listbox_get_current (list, NULL, &data);
542 if (data != NULL) {
543 struct hotlist *hlp = (struct hotlist*) data;
544 if (hlp->type == HL_TYPE_ENTRY) {
545 dlg->ret_value = B_ENTER;
546 dlg_stop (dlg);
547 return LISTBOX_DONE;
548 } else {
549 hotlist_button_callback (B_ENTER);
550 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
551 return LISTBOX_CONT;
553 } else {
554 dlg->ret_value = B_ENTER;
555 dlg_stop (dlg);
556 return LISTBOX_DONE;
560 hotlist_button_callback (B_UP_GROUP);
561 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
562 return LISTBOX_CONT;
566 * Expands all button names (once) and recalculates button positions.
567 * returns number of columns in the dialog box, which is 10 chars longer
568 * then buttonbar.
570 * If common width of the window (i.e. in xterm) is less than returned
571 * width - sorry :) (anyway this did not handled in previous version too)
573 static int
574 init_i18n_stuff(int list_type, int cols)
576 register int i;
577 static const char* cancel_but = N_("&Cancel");
579 #ifdef ENABLE_NLS
580 static int hotlist_i18n_flag = 0;
582 if (!hotlist_i18n_flag)
584 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
585 while (i--)
586 hotlist_but [i].text = _(hotlist_but [i].text);
588 cancel_but = _(cancel_but);
589 hotlist_i18n_flag = 1;
591 #endif /* ENABLE_NLS */
593 /* Dynamic resizing of buttonbars */
595 int len[2], count[2]; /* at most two lines of buttons */
596 int cur_x[2], row;
598 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
599 len[0] = len[1] = count[0] = count[1] = 0;
601 /* Count len of buttonbars, assuming 2 extra space between buttons */
602 while (i--)
604 if (! (hotlist_but[i].type & list_type))
605 continue;
607 row = hotlist_but [i].y;
608 ++count [row];
609 len [row] += str_term_width1 (hotlist_but [i].text) + 5;
610 if (hotlist_but [i].flags == DEFPUSH_BUTTON)
611 len [row] += 2;
613 len[0] -= 2;
614 len[1] -= 2;
616 cols = max(cols, max(len[0], len[1]));
618 /* arrange buttons */
620 cur_x[0] = cur_x[1] = 0;
621 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
622 while (i--)
624 if (! (hotlist_but[i].type & list_type))
625 continue;
627 row = hotlist_but [i].y;
629 if (hotlist_but [i].x != 0)
631 /* not first int the row */
632 if (!strcmp (hotlist_but [i].text, cancel_but))
633 hotlist_but [i].x =
634 cols - str_term_width1 (hotlist_but [i].text) - 13;
635 else
636 hotlist_but [i].x = cur_x [row];
639 cur_x [row] += str_term_width1 (hotlist_but [i].text) + 2
640 + (hotlist_but [i].flags == DEFPUSH_BUTTON ? 5 : 3);
644 return cols;
647 static void
648 init_hotlist (int list_type)
650 size_t i;
651 const char *title, *help_node;
652 int hotlist_cols;
654 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
656 do_refresh ();
658 hotlist_state.expanded =
659 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
661 if (list_type == LIST_VFSLIST) {
662 title = _("Active VFS directories");
663 help_node = "[vfshot]"; /* FIXME - no such node */
664 } else {
665 title = _("Directory hotlist");
666 help_node = "[Hotlist]";
669 hotlist_dlg =
670 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
671 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
673 for (i = 0; i < BUTTONS; i++) {
674 if (hotlist_but[i].type & list_type)
675 add_widget_autopos (hotlist_dlg,
676 button_new (BY + hotlist_but[i].y,
677 BX + hotlist_but[i].x,
678 hotlist_but[i].ret_cmd,
679 hotlist_but[i].flags,
680 hotlist_but[i].text,
681 hotlist_button_callback),
682 hotlist_but[i].pos_flags);
685 /* We add the labels.
686 * pname will hold entry's pathname;
687 * pname_group will hold name of current group
689 pname = label_new (UY - 11 + LINES, UX + 2, "");
690 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
691 if (!hotlist_state.moving) {
692 add_widget_autopos (hotlist_dlg,
693 label_new (UY - 12 + LINES, UX + 1,
694 _(" Directory path ")),
695 WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
697 /* This one holds the displayed pathname */
698 pname_group = label_new (UY, UX + 1, _(" Directory label "));
699 add_widget (hotlist_dlg, pname_group);
701 /* get new listbox */
702 l_hotlist =
703 listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8,
704 FALSE, l_call);
706 /* Fill the hotlist with the active VFS or the hotlist */
707 #ifdef ENABLE_VFS
708 if (list_type == LIST_VFSLIST) {
709 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
710 vfs_fill_names (add_name_to_list);
711 } else
712 #endif /* !ENABLE_VFS */
713 fill_listbox ();
715 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
716 /* add listbox to the dialogs */
719 static void
720 init_movelist (int list_type, struct hotlist *item)
722 size_t i;
723 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
724 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
726 do_refresh ();
728 movelist_dlg =
729 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
730 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
731 g_free (hdr);
733 for (i = 0; i < BUTTONS; i++) {
734 if (hotlist_but[i].type & list_type)
735 add_widget (movelist_dlg,
736 button_new (BY - 4 + hotlist_but[i].y,
737 BX + hotlist_but[i].x,
738 hotlist_but[i].ret_cmd,
739 hotlist_but[i].flags,
740 hotlist_but[i].text,
741 hotlist_button_callback));
744 /* We add the labels. We are interested in the last one,
745 * that one will hold the path name label
747 movelist_group = label_new (UY, UX + 1, _(" Directory label "));
748 add_widget (movelist_dlg, movelist_group);
749 /* get new listbox */
750 l_movelist =
751 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
752 movelist_dlg->cols - 2 * UX - 2, FALSE, l_call);
754 fill_listbox ();
756 add_widget (movelist_dlg, l_movelist);
757 /* add listbox to the dialogs */
761 * Destroy the list dialog.
762 * Don't confuse with done_hotlist() for the list in memory.
764 static void hotlist_done (void)
766 destroy_dlg (hotlist_dlg);
767 l_hotlist = NULL;
768 if (0)
769 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
770 repaint_screen ();
773 static inline char *
774 find_group_section (struct hotlist *grp)
776 return g_strconcat (grp->directory, ".Group", (char *) NULL);
779 static struct hotlist *
780 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
782 struct hotlist *new;
783 struct hotlist *current = NULL;
786 * Hotlist is neither loaded nor loading.
787 * Must be called by "Ctrl-x a" before using hotlist.
789 if (!current_group)
790 load_hotlist ();
792 listbox_get_current (l_hotlist, NULL, (void **) &current);
794 /* Make sure `..' stays at the top of the list. */
795 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
796 pos = LISTBOX_APPEND_AFTER;
798 new = new_hotlist ();
800 new->type = type;
801 new->label = label;
802 new->directory = directory;
803 new->up = current_group;
805 if (type == HL_TYPE_GROUP) {
806 current_group = new;
807 add_dotdot_to_list ();
808 current_group = new->up;
811 if (!current_group->head) {
812 /* first element in group */
813 current_group->head = new;
814 } else if (pos == LISTBOX_APPEND_AFTER) {
815 new->next = current->next;
816 current->next = new;
817 } else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head) {
818 /* should be inserted before first item */
819 new->next = current;
820 current_group->head = new;
821 } else if (pos == LISTBOX_APPEND_BEFORE) {
822 struct hotlist *p = current_group->head;
824 while (p->next != current)
825 p = p->next;
827 new->next = current;
828 p->next = new;
829 } else { /* append at the end */
830 struct hotlist *p = current_group->head;
832 while (p->next)
833 p = p->next;
835 p->next = new;
838 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT) {
839 if (type == HL_TYPE_GROUP) {
840 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
842 listbox_add_item (l_hotlist, pos, 0, lbl, new);
843 g_free (lbl);
844 } else
845 listbox_add_item (l_hotlist, pos, 0, new->label, new);
846 listbox_select_entry (l_hotlist, l_hotlist->pos);
848 return new;
852 #ifdef ENABLE_NLS
854 * Support routine for add_new_entry_input()/add_new_group_input()
855 * Change positions of buttons (first three widgets).
857 * This is just a quick hack. Accurate procedure must take care of
858 * internationalized label lengths and total buttonbar length...assume
859 * 64 is longer anyway.
861 static void
862 add_widgets_i18n (QuickWidget* qw, int len)
864 int i, l[3], space, cur_x;
866 for (i = 0; i < 3; i++) {
867 qw [i].u.button.text = _(qw [i].u.button.text);
868 l[i] = str_term_width1 (qw [i].u.button.text) + 3;
870 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
872 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space) {
873 qw [i].relative_x = cur_x;
874 qw [i].x_divisions = len;
877 #endif /* ENABLE_NLS */
879 static int
880 add_new_entry_input (const char *header, const char *text1, const char *text2,
881 const char *help, char **r1, char **r2)
883 #define RELATIVE_Y_BUTTONS 4
884 #define RELATIVE_Y_LABEL_PTH 3
885 #define RELATIVE_Y_INPUT_PTH 4
887 QuickWidget quick_widgets [] =
889 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
890 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
891 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
892 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
893 /* 4 */ QUICK_LABEL (RELATIVE_Y_LABEL_PTH, 80, 3, 0, text2),
894 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
895 /* 6 */ QUICK_LABEL (3, 80, 2, 0, text1),
896 QUICK_END
899 int len;
900 int i;
901 int lines1, lines2;
902 int cols1, cols2;
904 #ifdef ENABLE_NLS
905 static gboolean i18n_flag = FALSE;
906 #endif /* ENABLE_NLS */
908 msglen(text1, &lines1, &cols1);
909 msglen(text2, &lines2, &cols2);
910 len = max (str_term_width1 (header), cols1);
911 len = max (max (len, cols2) + 4, 64);
913 #ifdef ENABLE_NLS
914 if (!i18n_flag) {
915 add_widgets_i18n (quick_widgets, len);
916 i18n_flag = TRUE;
918 #endif /* ENABLE_NLS */
921 QuickDialog Quick_input =
923 len, lines1 + lines2 + 7, -1, -1, header,
924 help, quick_widgets, FALSE
927 for (i = 0; i < 7; i++)
928 quick_widgets [i].y_divisions = Quick_input.ylen;
930 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
931 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
932 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
933 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
934 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
936 i = quick_dialog (&Quick_input);
939 return (i != B_CANCEL) ? i : 0;
941 #undef RELATIVE_Y_BUTTONS
942 #undef RELATIVE_Y_LABEL_PTH
943 #undef RELATIVE_Y_INPUT_PTH
946 static void add_new_entry_cmd (void)
948 char *title, *url, *to_free;
949 int ret;
951 /* Take current directory as default value for input fields */
952 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
954 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"),
955 _("Directory path"), "[Hotlist]", &title, &url);
956 g_free (to_free);
958 if (!ret)
959 return;
960 if (!title || !*title || !url || !*url) {
961 g_free (title);
962 g_free (url);
963 return;
966 if (ret == B_ENTER || ret == B_APPEND)
967 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
968 else
969 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
971 hotlist_state.modified = 1;
974 static int
975 add_new_group_input (const char *header, const char *label, char **result)
977 QuickWidget quick_widgets [] =
979 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
980 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
981 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
982 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input" , result),
983 /* 4 */ QUICK_LABEL (3, 80, 2, 0, label),
984 QUICK_END
987 int len;
988 int i;
989 int lines, cols;
990 int ret;
992 #ifdef ENABLE_NLS
993 static gboolean i18n_flag = FALSE;
994 #endif /* ENABLE_NLS */
996 msglen (label, &lines, &cols);
997 len = max (max (str_term_width1 (header), cols) + 4, 64);
999 #ifdef ENABLE_NLS
1000 if (!i18n_flag) {
1001 add_widgets_i18n (quick_widgets, len);
1002 i18n_flag = TRUE;
1004 #endif /* ENABLE_NLS */
1007 QuickDialog Quick_input =
1009 len, lines + 6, -1, -1, header,
1010 "[Hotlist]", quick_widgets, FALSE
1013 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
1014 quick_widgets variable above */
1016 for (i = 0; i < 5; i++)
1017 quick_widgets[i].y_divisions = Quick_input.ylen;
1019 for (i = 0; i < 4; i++)
1020 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1022 ret = quick_dialog (&Quick_input);
1025 return (ret != B_CANCEL) ? ret : 0;
1028 static void add_new_group_cmd (void)
1030 char *label;
1031 int ret;
1033 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
1034 if (!ret || !label || !*label)
1035 return;
1037 if (ret == B_ENTER || ret == B_APPEND)
1038 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1039 else
1040 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1042 hotlist_state.modified = 1;
1045 void add2hotlist_cmd (void)
1047 char *lc_prompt, *label;
1048 const char *cp = _("Label for \"%s\":");
1049 int l = str_term_width1 (cp);
1050 char *label_string = g_strdup (current_panel->cwd);
1052 strip_password (label_string, 1);
1054 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS-2*UX-(l+8)));
1055 label = input_dialog (_(" Add to hotlist "), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1056 g_free (lc_prompt);
1058 if (!label || !*label) {
1059 g_free (label_string);
1060 g_free (label);
1061 return;
1063 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1064 hotlist_state.modified = 1;
1067 static void remove_group (struct hotlist *grp)
1069 struct hotlist *current = grp->head;
1071 while (current) {
1072 struct hotlist *next = current->next;
1074 if (current->type == HL_TYPE_GROUP)
1075 remove_group (current);
1077 g_free (current->label);
1078 g_free (current->directory);
1079 g_free (current);
1081 current = next;
1086 static void remove_from_hotlist (struct hotlist *entry)
1088 if (entry == NULL)
1089 return;
1091 if (entry->type == HL_TYPE_DOTDOT)
1092 return;
1094 if (confirm_directory_hotlist_delete) {
1095 char *title;
1096 int result;
1098 title = g_strconcat (_(" Remove: "),
1099 str_trunc (entry->label, 30),
1100 " ", (char *) NULL);
1102 if (safe_delete)
1103 query_set_sel (1);
1104 result = query_dialog (title,
1105 _("\n Are you sure you want to remove this entry?"),
1106 D_ERROR, 2, _("&Yes"), _("&No"));
1108 g_free (title);
1110 if (result != 0)
1111 return;
1114 if (entry->type == HL_TYPE_GROUP) {
1115 if (entry->head) {
1116 char *header;
1117 int result;
1119 header = g_strconcat (_(" Remove: "),
1120 str_trunc (entry->label, 30),
1121 " ", (char *) NULL);
1122 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1123 D_ERROR, 2,
1124 _("&Yes"), _("&No"));
1125 g_free (header);
1127 if (result != 0)
1128 return;
1131 remove_group (entry);
1134 unlink_entry (entry);
1136 g_free (entry->label);
1137 g_free (entry->directory);
1138 g_free (entry);
1139 /* now remove list entry from screen */
1140 listbox_remove_current (l_hotlist);
1141 hotlist_state.modified = 1;
1144 char *
1145 hotlist_cmd (int vfs_or_hotlist)
1147 char *target = NULL;
1149 hotlist_state.type = vfs_or_hotlist;
1150 load_hotlist ();
1152 init_hotlist (vfs_or_hotlist);
1154 /* display file info */
1155 tty_setcolor (SELECTED_COLOR);
1157 hotlist_state.running = 1;
1158 run_dlg (hotlist_dlg);
1159 hotlist_state.running = 0;
1160 save_hotlist ();
1162 switch (hotlist_dlg->ret_value) {
1163 default:
1164 case B_CANCEL:
1165 break;
1167 case B_ENTER:
1169 char *text = NULL;
1170 struct hotlist *hlp = NULL;
1172 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1173 target = g_strdup (hlp != NULL ? hlp->directory : text);
1174 break;
1176 } /* switch */
1178 hotlist_done ();
1179 return target;
1182 static void
1183 load_group (struct hotlist *grp)
1185 gchar **profile_keys, **keys;
1186 gsize len;
1187 char *group_section;
1188 struct hotlist *current = 0;
1190 group_section = find_group_section (grp);
1192 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1194 current_group = grp;
1196 while (*profile_keys){
1197 add2hotlist (
1198 mc_config_get_string(mc_main_config, group_section, *profile_keys, ""),
1199 g_strdup (*profile_keys),
1200 HL_TYPE_GROUP,
1201 LISTBOX_APPEND_AT_END);
1202 profile_keys++;
1204 g_free (group_section);
1205 g_strfreev(keys);
1207 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory,&len);
1209 while (*profile_keys){
1210 add2hotlist (
1211 mc_config_get_string(mc_main_config,group_section,*profile_keys,""),
1212 g_strdup (*profile_keys),
1213 HL_TYPE_ENTRY,
1214 LISTBOX_APPEND_AT_END);
1215 profile_keys++;
1217 g_strfreev(keys);
1219 for (current = grp->head; current; current = current->next)
1220 load_group (current);
1223 #define TKN_GROUP 0
1224 #define TKN_ENTRY 1
1225 #define TKN_STRING 2
1226 #define TKN_URL 3
1227 #define TKN_ENDGROUP 4
1228 #define TKN_COMMENT 5
1229 #define TKN_EOL 125
1230 #define TKN_EOF 126
1231 #define TKN_UNKNOWN 127
1233 static GString *tkn_buf = NULL;
1235 static char *hotlist_file_name;
1236 static FILE *hotlist_file;
1237 static time_t hotlist_file_mtime;
1239 static int hot_skip_blanks (void)
1241 int c;
1243 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1245 return c;
1249 static int hot_next_token (void)
1251 int c, ret=0;
1252 size_t l;
1255 if (tkn_buf == NULL) tkn_buf = g_string_new ("");
1256 g_string_set_size(tkn_buf,0);
1258 again:
1259 c = hot_skip_blanks ();
1260 switch (c) {
1261 case EOF:
1262 ret = TKN_EOF;
1263 break;
1264 case '\n':
1265 ret = TKN_EOL;
1266 break;
1267 case '#':
1268 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1269 g_string_append_c (tkn_buf, c);
1271 ret = TKN_COMMENT;
1272 break;
1273 case '"':
1274 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1275 if (c == '\\')
1276 if ((c = getc (hotlist_file)) == EOF){
1277 g_string_free (tkn_buf, TRUE);
1278 return TKN_EOF;
1280 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1282 if (c == EOF)
1283 ret = TKN_EOF;
1284 else
1285 ret = TKN_STRING;
1286 break;
1287 case '\\':
1288 if ((c = getc (hotlist_file)) == EOF){
1289 g_string_free (tkn_buf, TRUE);
1290 return TKN_EOF;
1292 if (c == '\n')
1293 goto again;
1295 /* fall through; it is taken as normal character */
1297 default:
1298 do {
1299 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1300 } while ((c = fgetc (hotlist_file)) != EOF &&
1301 (g_ascii_isalnum (c) || !isascii (c)));
1302 if (c != EOF)
1303 ungetc (c, hotlist_file);
1304 l = tkn_buf->len;
1305 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1306 ret = TKN_GROUP;
1307 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1308 ret = TKN_ENTRY;
1309 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1310 ret = TKN_ENDGROUP;
1311 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1312 ret = TKN_URL;
1313 else
1314 ret = TKN_UNKNOWN;
1315 break;
1317 return ret;
1320 #define SKIP_TO_EOL { \
1321 int _tkn; \
1322 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1325 #define CHECK_TOKEN(_TKN_) \
1326 if ((tkn = hot_next_token ()) != _TKN_) { \
1327 hotlist_state.readonly = 1; \
1328 hotlist_state.file_error = 1; \
1329 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1330 tkn = hot_next_token (); \
1331 break; \
1334 static void
1335 hot_load_group (struct hotlist * grp)
1337 int tkn;
1338 struct hotlist *new_grp;
1339 char *label, *url;
1341 current_group = grp;
1343 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1344 switch (tkn) {
1345 case TKN_GROUP:
1346 CHECK_TOKEN(TKN_STRING);
1347 new_grp = add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1348 SKIP_TO_EOL;
1349 hot_load_group (new_grp);
1350 current_group = grp;
1351 break;
1352 case TKN_ENTRY:
1353 CHECK_TOKEN(TKN_STRING);
1354 label = g_strdup (tkn_buf->str);
1355 CHECK_TOKEN(TKN_URL);
1356 CHECK_TOKEN(TKN_STRING);
1357 url = g_strdup (tkn_buf->str);
1358 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1359 SKIP_TO_EOL;
1360 break;
1361 case TKN_COMMENT:
1362 label = g_strdup (tkn_buf->str);
1363 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1364 break;
1365 case TKN_EOF:
1366 hotlist_state.readonly = 1;
1367 hotlist_state.file_error = 1;
1368 return;
1369 break;
1370 case TKN_EOL:
1371 /* skip empty lines */
1372 break;
1373 default:
1374 hotlist_state.readonly = 1;
1375 hotlist_state.file_error = 1;
1376 SKIP_TO_EOL;
1377 break;
1379 SKIP_TO_EOL;
1382 static void
1383 hot_load_file (struct hotlist * grp)
1385 int tkn;
1386 struct hotlist *new_grp;
1387 char *label, *url;
1389 current_group = grp;
1391 while ((tkn = hot_next_token())!= TKN_EOF)
1392 switch (tkn) {
1393 case TKN_GROUP:
1394 CHECK_TOKEN(TKN_STRING);
1395 new_grp = add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1396 SKIP_TO_EOL;
1397 hot_load_group (new_grp);
1398 current_group = grp;
1399 break;
1400 case TKN_ENTRY:
1401 CHECK_TOKEN(TKN_STRING);
1402 label = g_strdup (tkn_buf->str);
1403 CHECK_TOKEN(TKN_URL);
1404 CHECK_TOKEN(TKN_STRING);
1405 url = g_strdup (tkn_buf->str);
1406 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1407 SKIP_TO_EOL;
1408 break;
1409 case TKN_COMMENT:
1410 label = g_strdup (tkn_buf->str);
1411 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1412 break;
1413 case TKN_EOL:
1414 /* skip empty lines */
1415 break;
1416 default:
1417 hotlist_state.readonly = 1;
1418 hotlist_state.file_error = 1;
1419 SKIP_TO_EOL;
1420 break;
1424 static void
1425 clean_up_hotlist_groups (const char *section)
1427 char *grp_section;
1428 gchar **profile_keys, **keys;
1429 gsize len;
1431 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1432 if (mc_config_has_group(mc_main_config, section))
1433 mc_config_del_group (mc_main_config, section);
1435 if (mc_config_has_group (mc_main_config, grp_section)) {
1436 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section,&len);
1438 while (*profile_keys) {
1439 clean_up_hotlist_groups (*profile_keys);
1440 profile_keys++;
1442 g_strfreev(keys);
1443 mc_config_del_group (mc_main_config, grp_section);
1445 g_free (grp_section);
1450 static void
1451 load_hotlist (void)
1453 int remove_old_list = 0;
1454 struct stat stat_buf;
1456 if (hotlist_state.loaded) {
1457 stat (hotlist_file_name, &stat_buf);
1458 if (hotlist_file_mtime < stat_buf.st_mtime)
1459 done_hotlist ();
1460 else
1461 return;
1464 if (!hotlist_file_name)
1465 hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL);
1467 hotlist = new_hotlist ();
1468 hotlist->type = HL_TYPE_GROUP;
1469 hotlist->label = g_strdup (_(" Top level group "));
1470 hotlist->up = hotlist;
1472 * compatibility :-(
1474 hotlist->directory = g_strdup ("Hotlist");
1476 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1477 int result;
1479 load_group (hotlist);
1480 hotlist_state.loaded = 1;
1482 * just to be sure we got copy
1484 hotlist_state.modified = 1;
1485 result = save_hotlist ();
1486 hotlist_state.modified = 0;
1487 if (result) {
1488 remove_old_list = 1;
1489 } else {
1490 message (D_ERROR, _(" Hotlist Load "),
1491 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1492 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1494 } else {
1495 hot_load_file (hotlist);
1496 fclose (hotlist_file);
1497 hotlist_state.loaded = 1;
1500 if (remove_old_list) {
1501 GError *error = NULL;
1502 clean_up_hotlist_groups ("Hotlist");
1503 if (! mc_config_save_file (mc_main_config, &error))
1504 setup_save_config_show_error(mc_main_config->ini_path, &error);
1507 stat (hotlist_file_name, &stat_buf);
1508 hotlist_file_mtime = stat_buf.st_mtime;
1509 current_group = hotlist;
1513 static int list_level = 0;
1515 static void
1516 hot_save_group (struct hotlist *grp)
1518 struct hotlist *current = grp->head;
1519 int i;
1520 char *s;
1522 #define INDENT(n) \
1523 do { \
1524 for (i = 0; i < n; i++) \
1525 putc (' ', hotlist_file); \
1526 } while (0)
1528 for (;current; current = current->next)
1529 switch (current->type) {
1530 case HL_TYPE_GROUP:
1531 INDENT (list_level);
1532 fputs ("GROUP \"", hotlist_file);
1533 for (s = current->label; *s; s++) {
1534 if (*s == '"' || *s == '\\')
1535 putc ('\\', hotlist_file);
1536 putc (*s, hotlist_file);
1538 fputs ("\"\n", hotlist_file);
1539 list_level += 2;
1540 hot_save_group (current);
1541 list_level -= 2;
1542 INDENT (list_level);
1543 fputs ("ENDGROUP\n", hotlist_file);
1544 break;
1545 case HL_TYPE_ENTRY:
1546 INDENT(list_level);
1547 fputs ("ENTRY \"", hotlist_file);
1548 for (s = current->label; *s; s++) {
1549 if (*s == '"' || *s == '\\')
1550 putc ('\\', hotlist_file);
1551 putc (*s, hotlist_file);
1553 fputs ("\" URL \"", hotlist_file);
1554 for (s = current->directory; *s; s++) {
1555 if (*s == '"' || *s == '\\')
1556 putc ('\\', hotlist_file);
1557 putc (*s, hotlist_file);
1559 fputs ("\"\n", hotlist_file);
1560 break;
1561 case HL_TYPE_COMMENT:
1562 fprintf (hotlist_file, "#%s\n", current->label);
1563 break;
1564 case HL_TYPE_DOTDOT:
1565 /* do nothing */
1566 break;
1570 int save_hotlist (void)
1572 int saved = 0;
1573 struct stat stat_buf;
1575 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1576 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1578 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1579 hot_save_group (hotlist);
1580 fclose (hotlist_file);
1581 stat (hotlist_file_name, &stat_buf);
1582 hotlist_file_mtime = stat_buf.st_mtime;
1583 saved = 1;
1584 hotlist_state.modified = 0;
1585 } else
1586 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1589 return saved;
1593 * Unload list from memory.
1594 * Don't confuse with hotlist_done() for GUI.
1596 void done_hotlist (void)
1598 if (hotlist){
1599 remove_group (hotlist);
1600 g_free (hotlist->label);
1601 g_free (hotlist->directory);
1602 g_free (hotlist);
1603 hotlist = 0;
1606 hotlist_state.loaded = 0;
1608 g_free (hotlist_file_name);
1609 hotlist_file_name = 0;
1610 l_hotlist = 0;
1611 current_group = 0;
1613 if (tkn_buf){
1614 g_string_free (tkn_buf, TRUE);
1615 tkn_buf = NULL;
1619 static void
1620 add_dotdot_to_list (void)
1622 if (current_group != hotlist) {
1623 if (hotlist_has_dot_dot != 0)
1624 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);