Ticket #2097: ChangeLog in its current form does not strictly make any sense
[midnight-commander.git] / src / hotlist.c
blobb80c376a834dafca65349e923cb48aa3ca4ee51e
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
98 HL_TYPE_GROUP,
99 HL_TYPE_ENTRY,
100 HL_TYPE_COMMENT,
101 HL_TYPE_DOTDOT
104 static struct
107 * these parameters are intended to be user configurable
109 int expanded; /* expanded view of all groups at startup */
112 * these reflect run time state
115 int loaded; /* hotlist is loaded */
116 int readonly; /* hotlist readonly */
117 int file_error; /* parse error while reading file */
118 int running; /* we are running dlg (and have to
119 update listbox */
120 int moving; /* we are in moving hotlist currently */
121 int modified; /* hotlist was modified */
122 int type; /* LIST_HOTLIST || LIST_VFSLIST */
123 } hotlist_state;
125 static struct _hotlist_but
127 int ret_cmd, flags, y, x;
128 const char *text;
129 int type;
130 widget_pos_flags_t pos_flags;
131 } hotlist_but[] =
133 /* *INDENT-OFF* */
134 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
135 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"),
136 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
137 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"),
138 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
139 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"),
140 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
141 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &Entry"),
142 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
143 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &Group"),
144 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
145 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"),
146 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
147 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"),
148 LIST_HOTLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
149 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),
150 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
151 #ifdef ENABLE_VFS
152 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"),
153 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
154 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"),
155 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
156 #endif
157 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &To"),
158 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM }
159 /* *INDENT-ON* */
162 /* Directory hotlist */
163 static struct hotlist
165 enum HotListType type;
166 char *directory;
167 char *label;
168 struct hotlist *head;
169 struct hotlist *up;
170 struct hotlist *next;
171 } *hotlist = NULL;
173 static struct hotlist *current_group;
175 static void init_movelist (int, struct hotlist *);
176 static void add_new_group_cmd (void);
177 static void add_new_entry_cmd (void);
178 static void remove_from_hotlist (struct hotlist *entry);
179 static void load_hotlist (void);
180 static void add_dotdot_to_list (void);
182 #define new_hotlist() g_new0(struct hotlist, 1)
184 static void
185 hotlist_refresh (Dlg_head * dlg)
187 common_dialog_repaint (dlg);
188 tty_setcolor (COLOR_NORMAL);
189 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10), dlg->cols - (UX * 2), TRUE);
190 if (!hotlist_state.moving)
191 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2), TRUE);
194 /* If current->data is 0, then we are dealing with a VFS pathname */
195 static void
196 update_path_name (void)
198 const char *text = "";
199 char *p;
200 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
201 Dlg_head *dlg = list->widget.parent;
203 if (list->count != 0)
205 char *ctext = NULL;
206 void *cdata = NULL;
208 listbox_get_current (list, &ctext, &cdata);
209 if (cdata == NULL)
210 text = ctext;
211 else
213 struct hotlist *hlp = (struct hotlist *) cdata;
215 if (hlp->type == HL_TYPE_ENTRY || hlp->type == HL_TYPE_DOTDOT)
216 text = hlp->directory;
217 else if (hlp->type == HL_TYPE_GROUP)
218 text = _("Subgroup - press ENTER to see list");
221 if (!hotlist_state.moving)
222 label_set_text (pname, str_trunc (text, dlg->cols - (UX * 2 + 4)));
224 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
225 if (!hotlist_state.moving)
226 label_set_text (pname_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
227 else
228 label_set_text (movelist_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
229 g_free (p);
231 dlg_redraw (dlg);
234 #define CHECK_BUFFER \
235 do { \
236 int i; \
238 if ((i = strlen (current->label) + 3) > buflen) { \
239 g_free (buf); \
240 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
242 buf[0] = '\0'; \
243 } while (0)
245 static void
246 fill_listbox (void)
248 struct hotlist *current = current_group->head;
249 GString *buff = g_string_new ("");
251 while (current)
253 switch (current->type)
255 case HL_TYPE_GROUP:
257 /* buff clean up */
258 g_string_truncate (buff, 0);
259 g_string_append (buff, "->");
260 g_string_append (buff, current->label);
261 if (hotlist_state.moving)
262 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
263 else
264 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
266 break;
267 case HL_TYPE_DOTDOT:
268 case HL_TYPE_ENTRY:
269 if (hotlist_state.moving)
270 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, current->label, current);
271 else
272 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, current->label, current);
273 break;
274 default:
275 break;
277 current = current->next;
279 g_string_free (buff, TRUE);
282 static void
283 unlink_entry (struct hotlist *entry)
285 struct hotlist *current = current_group->head;
287 if (current == entry)
288 current_group->head = entry->next;
289 else
291 while (current && current->next != entry)
292 current = current->next;
293 if (current)
294 current->next = entry->next;
296 entry->next = entry->up = 0;
299 #ifdef ENABLE_VFS
300 static void
301 add_name_to_list (const char *path)
303 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, path, 0);
305 #endif /* !ENABLE_VFS */
307 static int
308 hotlist_button_callback (int action)
310 switch (action)
312 case B_MOVE:
314 struct hotlist *saved = current_group;
315 struct hotlist *item = NULL;
316 struct hotlist *moveto_item = NULL;
317 struct hotlist *moveto_group = NULL;
318 int ret;
320 if (l_hotlist->count == 0)
321 return MSG_NOT_HANDLED; /* empty group - nothing to do */
323 listbox_get_current (l_hotlist, NULL, (void **) &item);
324 hotlist_state.moving = 1;
325 init_movelist (LIST_MOVELIST, item);
327 ret = run_dlg (movelist_dlg);
329 hotlist_state.moving = 0;
330 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
331 moveto_group = current_group;
332 destroy_dlg (movelist_dlg);
333 current_group = saved;
334 if (ret == B_CANCEL)
335 return MSG_NOT_HANDLED;
336 if (moveto_item == item)
337 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
338 it hardly changes anything ;) */
339 unlink_entry (item);
340 listbox_remove_current (l_hotlist);
341 item->up = moveto_group;
342 if (!moveto_group->head)
343 moveto_group->head = item;
344 else if (!moveto_item)
345 { /* we have group with just comments */
346 struct hotlist *p = moveto_group->head;
348 /* skip comments */
349 while (p->next)
350 p = p->next;
351 p->next = item;
353 else if (ret == B_ENTER || ret == B_APPEND)
354 if (!moveto_item->next)
355 moveto_item->next = item;
356 else
358 item->next = moveto_item->next;
359 moveto_item->next = item;
361 else if (moveto_group->head == moveto_item)
363 moveto_group->head = item;
364 item->next = moveto_item;
366 else
368 struct hotlist *p = moveto_group->head;
370 while (p->next != moveto_item)
371 p = p->next;
372 item->next = p->next;
373 p->next = item;
375 listbox_remove_list (l_hotlist);
376 fill_listbox ();
377 repaint_screen ();
378 hotlist_state.modified = 1;
379 return MSG_NOT_HANDLED;
381 case B_REMOVE:
383 struct hotlist *entry = NULL;
384 listbox_get_current (l_hotlist, NULL, (void **) &entry);
385 remove_from_hotlist (entry);
387 return MSG_NOT_HANDLED;
389 case B_NEW_GROUP:
390 add_new_group_cmd ();
391 return MSG_NOT_HANDLED;
393 case B_ADD_CURRENT:
394 add2hotlist_cmd ();
395 return MSG_NOT_HANDLED;
397 case B_NEW_ENTRY:
398 add_new_entry_cmd ();
399 return MSG_NOT_HANDLED;
401 case B_ENTER:
403 WListbox *list;
404 void *data;
405 struct hotlist *hlp;
407 list = hotlist_state.moving ? l_movelist : l_hotlist;
408 listbox_get_current (list, NULL, &data);
410 if (data == NULL)
411 return MSG_HANDLED;
413 hlp = (struct hotlist *) data;
415 if (hlp->type == HL_TYPE_ENTRY)
416 return MSG_HANDLED;
417 if (hlp->type != HL_TYPE_DOTDOT)
419 listbox_remove_list (list);
420 current_group = hlp;
421 fill_listbox ();
422 return MSG_NOT_HANDLED;
424 /* Fall through - go up */
426 /* Fall through if list empty - just go up */
428 case B_UP_GROUP:
430 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
431 listbox_remove_list (list);
432 current_group = current_group->up;
433 fill_listbox ();
434 return MSG_NOT_HANDLED;
437 #ifdef ENABLE_VFS
438 case B_FREE_ALL_VFS:
439 vfs_expire (1);
440 /* fall through */
442 case B_REFRESH_VFS:
443 listbox_remove_list (l_hotlist);
444 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
445 vfs_fill_names (add_name_to_list);
446 return MSG_NOT_HANDLED;
447 #endif /* ENABLE_VFS */
449 default:
450 return MSG_HANDLED;
454 static inline cb_ret_t
455 hotlist_handle_key (Dlg_head * h, int key)
457 switch (key)
459 case KEY_M_CTRL | '\n':
460 goto l1;
462 case '\n':
463 case KEY_ENTER:
464 case KEY_RIGHT:
465 if (hotlist_button_callback (B_ENTER))
467 h->ret_value = B_ENTER;
468 dlg_stop (h);
470 return MSG_HANDLED;
472 case KEY_LEFT:
473 if (hotlist_state.type != LIST_VFSLIST)
474 return !hotlist_button_callback (B_UP_GROUP);
475 else
476 return MSG_NOT_HANDLED;
478 case KEY_DC:
479 if (hotlist_state.moving)
480 return MSG_NOT_HANDLED;
481 else
483 hotlist_button_callback (B_REMOVE);
484 return MSG_HANDLED;
488 case ALT ('\n'):
489 case ALT ('\r'):
490 if (!hotlist_state.moving)
492 void *ldata = NULL;
494 listbox_get_current (l_hotlist, NULL, &ldata);
496 if (ldata != NULL)
498 struct hotlist *hlp = (struct hotlist *) ldata;
500 if (hlp->type == HL_TYPE_ENTRY)
502 char *tmp;
504 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
505 stuff (cmdline, tmp, 0);
506 g_free (tmp);
507 h->ret_value = B_CANCEL;
508 dlg_stop (h);
512 return MSG_HANDLED; /* ignore key */
514 default:
515 return MSG_NOT_HANDLED;
519 static cb_ret_t
520 hotlist_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
522 switch (msg)
524 case DLG_DRAW:
525 hotlist_refresh (h);
526 return MSG_HANDLED;
528 case DLG_UNHANDLED_KEY:
529 return hotlist_handle_key (h, parm);
531 case DLG_POST_KEY:
532 if (hotlist_state.moving)
533 dlg_select_widget (l_movelist);
534 else
535 dlg_select_widget (l_hotlist);
536 /* always stay on hotlist */
537 /* fall through */
539 case DLG_INIT:
540 tty_setcolor (MENU_ENTRY_COLOR);
541 update_path_name ();
542 return MSG_HANDLED;
544 case DLG_RESIZE:
545 /* simply call dlg_set_size() with new size */
546 dlg_set_size (h, LINES - 2, COLS - 6);
547 return MSG_HANDLED;
549 default:
550 return default_dlg_callback (h, sender, msg, parm, data);
554 static int
555 l_call (WListbox * list)
557 Dlg_head *dlg = list->widget.parent;
559 if (list->count != 0)
561 void *data = NULL;
563 listbox_get_current (list, NULL, &data);
565 if (data != NULL)
567 struct hotlist *hlp = (struct hotlist *) data;
568 if (hlp->type == HL_TYPE_ENTRY)
570 dlg->ret_value = B_ENTER;
571 dlg_stop (dlg);
572 return LISTBOX_DONE;
574 else
576 hotlist_button_callback (B_ENTER);
577 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
578 return LISTBOX_CONT;
581 else
583 dlg->ret_value = B_ENTER;
584 dlg_stop (dlg);
585 return LISTBOX_DONE;
589 hotlist_button_callback (B_UP_GROUP);
590 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
591 return LISTBOX_CONT;
595 * Expands all button names (once) and recalculates button positions.
596 * returns number of columns in the dialog box, which is 10 chars longer
597 * then buttonbar.
599 * If common width of the window (i.e. in xterm) is less than returned
600 * width - sorry :) (anyway this did not handled in previous version too)
602 static int
603 init_i18n_stuff (int list_type, int cols)
605 register int i;
606 static const char *cancel_but = N_("&Cancel");
608 #ifdef ENABLE_NLS
609 static int hotlist_i18n_flag = 0;
611 if (!hotlist_i18n_flag)
613 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
614 while (i--)
615 hotlist_but[i].text = _(hotlist_but[i].text);
617 cancel_but = _(cancel_but);
618 hotlist_i18n_flag = 1;
620 #endif /* ENABLE_NLS */
622 /* Dynamic resizing of buttonbars */
624 int len[2], count[2]; /* at most two lines of buttons */
625 int cur_x[2], row;
627 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
628 len[0] = len[1] = count[0] = count[1] = 0;
630 /* Count len of buttonbars, assuming 2 extra space between buttons */
631 while (i--)
633 if (!(hotlist_but[i].type & list_type))
634 continue;
636 row = hotlist_but[i].y;
637 ++count[row];
638 len[row] += str_term_width1 (hotlist_but[i].text) + 5;
639 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
640 len[row] += 2;
642 len[0] -= 2;
643 len[1] -= 2;
645 cols = max (cols, max (len[0], len[1]));
647 /* arrange buttons */
649 cur_x[0] = cur_x[1] = 0;
650 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
651 while (i--)
653 if (!(hotlist_but[i].type & list_type))
654 continue;
656 row = hotlist_but[i].y;
658 if (hotlist_but[i].x != 0)
660 /* not first int the row */
661 if (!strcmp (hotlist_but[i].text, cancel_but))
662 hotlist_but[i].x = cols - str_term_width1 (hotlist_but[i].text) - 13;
663 else
664 hotlist_but[i].x = cur_x[row];
667 cur_x[row] += str_term_width1 (hotlist_but[i].text) + 2
668 + (hotlist_but[i].flags == DEFPUSH_BUTTON ? 5 : 3);
672 return cols;
675 static void
676 init_hotlist (int list_type)
678 size_t i;
679 const char *title, *help_node;
680 int hotlist_cols;
682 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
684 do_refresh ();
686 hotlist_state.expanded =
687 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
689 if (list_type == LIST_VFSLIST)
691 title = _("Active VFS directories");
692 help_node = "[vfshot]"; /* FIXME - no such node */
694 else
696 title = _("Directory hotlist");
697 help_node = "[Hotlist]";
700 hotlist_dlg =
701 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
702 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
704 for (i = 0; i < BUTTONS; i++)
706 if (hotlist_but[i].type & list_type)
707 add_widget_autopos (hotlist_dlg,
708 button_new (BY + hotlist_but[i].y,
709 BX + hotlist_but[i].x,
710 hotlist_but[i].ret_cmd,
711 hotlist_but[i].flags,
712 hotlist_but[i].text,
713 hotlist_button_callback), hotlist_but[i].pos_flags);
716 /* We add the labels.
717 * pname will hold entry's pathname;
718 * pname_group will hold name of current group
720 pname = label_new (UY - 11 + LINES, UX + 2, "");
721 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
722 if (!hotlist_state.moving)
724 add_widget_autopos (hotlist_dlg,
725 label_new (UY - 12 + LINES, UX + 1,
726 _(" Directory path ")), WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
728 /* This one holds the displayed pathname */
729 pname_group = label_new (UY, UX + 1, _(" Directory label "));
730 add_widget (hotlist_dlg, pname_group);
732 /* get new listbox */
733 l_hotlist = listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8, FALSE, l_call);
735 /* Fill the hotlist with the active VFS or the hotlist */
736 #ifdef ENABLE_VFS
737 if (list_type == LIST_VFSLIST)
739 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
740 vfs_fill_names (add_name_to_list);
742 else
743 #endif /* !ENABLE_VFS */
744 fill_listbox ();
746 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
747 /* add listbox to the dialogs */
750 static void
751 init_movelist (int list_type, struct hotlist *item)
753 size_t i;
754 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
755 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
757 do_refresh ();
759 movelist_dlg =
760 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
761 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
762 g_free (hdr);
764 for (i = 0; i < BUTTONS; i++)
766 if (hotlist_but[i].type & list_type)
767 add_widget (movelist_dlg,
768 button_new (BY - 4 + hotlist_but[i].y,
769 BX + hotlist_but[i].x,
770 hotlist_but[i].ret_cmd,
771 hotlist_but[i].flags,
772 hotlist_but[i].text, hotlist_button_callback));
775 /* We add the labels. We are interested in the last one,
776 * that one will hold the path name label
778 movelist_group = label_new (UY, UX + 1, _(" Directory label "));
779 add_widget (movelist_dlg, movelist_group);
780 /* get new listbox */
781 l_movelist =
782 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
783 movelist_dlg->cols - 2 * UX - 2, FALSE, l_call);
785 fill_listbox ();
787 add_widget (movelist_dlg, l_movelist);
788 /* add listbox to the dialogs */
792 * Destroy the list dialog.
793 * Don't confuse with done_hotlist() for the list in memory.
795 static void
796 hotlist_done (void)
798 destroy_dlg (hotlist_dlg);
799 l_hotlist = NULL;
800 if (0)
801 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
802 repaint_screen ();
805 static inline char *
806 find_group_section (struct hotlist *grp)
808 return g_strconcat (grp->directory, ".Group", (char *) NULL);
811 static struct hotlist *
812 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
814 struct hotlist *new;
815 struct hotlist *current = NULL;
818 * Hotlist is neither loaded nor loading.
819 * Must be called by "Ctrl-x a" before using hotlist.
821 if (!current_group)
822 load_hotlist ();
824 listbox_get_current (l_hotlist, NULL, (void **) &current);
826 /* Make sure `..' stays at the top of the list. */
827 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
828 pos = LISTBOX_APPEND_AFTER;
830 new = new_hotlist ();
832 new->type = type;
833 new->label = label;
834 new->directory = directory;
835 new->up = current_group;
837 if (type == HL_TYPE_GROUP)
839 current_group = new;
840 add_dotdot_to_list ();
841 current_group = new->up;
844 if (!current_group->head)
846 /* first element in group */
847 current_group->head = new;
849 else if (pos == LISTBOX_APPEND_AFTER)
851 new->next = current->next;
852 current->next = new;
854 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
856 /* should be inserted before first item */
857 new->next = current;
858 current_group->head = new;
860 else if (pos == LISTBOX_APPEND_BEFORE)
862 struct hotlist *p = current_group->head;
864 while (p->next != current)
865 p = p->next;
867 new->next = current;
868 p->next = new;
870 else
871 { /* append at the end */
872 struct hotlist *p = current_group->head;
874 while (p->next)
875 p = p->next;
877 p->next = new;
880 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
882 if (type == HL_TYPE_GROUP)
884 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
886 listbox_add_item (l_hotlist, pos, 0, lbl, new);
887 g_free (lbl);
889 else
890 listbox_add_item (l_hotlist, pos, 0, new->label, new);
891 listbox_select_entry (l_hotlist, l_hotlist->pos);
893 return new;
897 #ifdef ENABLE_NLS
899 * Support routine for add_new_entry_input()/add_new_group_input()
900 * Change positions of buttons (first three widgets).
902 * This is just a quick hack. Accurate procedure must take care of
903 * internationalized label lengths and total buttonbar length...assume
904 * 64 is longer anyway.
906 static void
907 add_widgets_i18n (QuickWidget * qw, int len)
909 int i, l[3], space, cur_x;
911 for (i = 0; i < 3; i++)
913 qw[i].u.button.text = _(qw[i].u.button.text);
914 l[i] = str_term_width1 (qw[i].u.button.text) + 3;
916 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
918 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
920 qw[i].relative_x = cur_x;
921 qw[i].x_divisions = len;
924 #endif /* ENABLE_NLS */
926 static int
927 add_new_entry_input (const char *header, const char *text1, const char *text2,
928 const char *help, char **r1, char **r2)
930 #define RELATIVE_Y_BUTTONS 4
931 #define RELATIVE_Y_LABEL_PTH 3
932 #define RELATIVE_Y_INPUT_PTH 4
934 QuickWidget quick_widgets[] = {
935 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
936 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
937 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
938 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
939 /* 4 */ QUICK_LABEL (RELATIVE_Y_LABEL_PTH, 80, 3, 0, text2),
940 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
941 /* 6 */ QUICK_LABEL (3, 80, 2, 0, text1),
942 QUICK_END
945 int len;
946 int i;
947 int lines1, lines2;
948 int cols1, cols2;
950 #ifdef ENABLE_NLS
951 static gboolean i18n_flag = FALSE;
952 #endif /* ENABLE_NLS */
954 msglen (text1, &lines1, &cols1);
955 msglen (text2, &lines2, &cols2);
956 len = max (str_term_width1 (header), cols1);
957 len = max (max (len, cols2) + 4, 64);
959 #ifdef ENABLE_NLS
960 if (!i18n_flag)
962 add_widgets_i18n (quick_widgets, len);
963 i18n_flag = TRUE;
965 #endif /* ENABLE_NLS */
968 QuickDialog Quick_input = {
969 len, lines1 + lines2 + 7, -1, -1, header,
970 help, quick_widgets, FALSE
973 for (i = 0; i < 7; i++)
974 quick_widgets[i].y_divisions = Quick_input.ylen;
976 quick_widgets[0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
977 quick_widgets[1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
978 quick_widgets[2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
979 quick_widgets[3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
980 quick_widgets[4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
982 i = quick_dialog (&Quick_input);
985 return (i != B_CANCEL) ? i : 0;
987 #undef RELATIVE_Y_BUTTONS
988 #undef RELATIVE_Y_LABEL_PTH
989 #undef RELATIVE_Y_INPUT_PTH
992 static void
993 add_new_entry_cmd (void)
995 char *title, *url, *to_free;
996 int ret;
998 /* Take current directory as default value for input fields */
999 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
1001 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"),
1002 _("Directory path"), "[Hotlist]", &title, &url);
1003 g_free (to_free);
1005 if (!ret)
1006 return;
1007 if (!title || !*title || !url || !*url)
1009 g_free (title);
1010 g_free (url);
1011 return;
1014 if (ret == B_ENTER || ret == B_APPEND)
1015 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1016 else
1017 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1019 hotlist_state.modified = 1;
1022 static int
1023 add_new_group_input (const char *header, const char *label, char **result)
1025 QuickWidget quick_widgets[] = {
1026 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
1027 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
1028 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
1029 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result),
1030 /* 4 */ QUICK_LABEL (3, 80, 2, 0, label),
1031 QUICK_END
1034 int len;
1035 int i;
1036 int lines, cols;
1037 int ret;
1039 #ifdef ENABLE_NLS
1040 static gboolean i18n_flag = FALSE;
1041 #endif /* ENABLE_NLS */
1043 msglen (label, &lines, &cols);
1044 len = max (max (str_term_width1 (header), cols) + 4, 64);
1046 #ifdef ENABLE_NLS
1047 if (!i18n_flag)
1049 add_widgets_i18n (quick_widgets, len);
1050 i18n_flag = TRUE;
1052 #endif /* ENABLE_NLS */
1055 QuickDialog Quick_input = {
1056 len, lines + 6, -1, -1, header,
1057 "[Hotlist]", quick_widgets, FALSE
1060 int relative_y[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1061 quick_widgets variable above */
1063 for (i = 0; i < 5; i++)
1064 quick_widgets[i].y_divisions = Quick_input.ylen;
1066 for (i = 0; i < 4; i++)
1067 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1069 ret = quick_dialog (&Quick_input);
1072 return (ret != B_CANCEL) ? ret : 0;
1075 static void
1076 add_new_group_cmd (void)
1078 char *label;
1079 int ret;
1081 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
1082 if (!ret || !label || !*label)
1083 return;
1085 if (ret == B_ENTER || ret == B_APPEND)
1086 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1087 else
1088 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1090 hotlist_state.modified = 1;
1093 void
1094 add2hotlist_cmd (void)
1096 char *lc_prompt, *label;
1097 const char *cp = _("Label for \"%s\":");
1098 int l = str_term_width1 (cp);
1099 char *label_string = g_strdup (current_panel->cwd);
1101 strip_password (label_string, 1);
1103 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS - 2 * UX - (l + 8)));
1104 label = input_dialog (_(" Add to hotlist "), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1105 g_free (lc_prompt);
1107 if (!label || !*label)
1109 g_free (label_string);
1110 g_free (label);
1111 return;
1113 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1114 hotlist_state.modified = 1;
1117 static void
1118 remove_group (struct hotlist *grp)
1120 struct hotlist *current = grp->head;
1122 while (current)
1124 struct hotlist *next = current->next;
1126 if (current->type == HL_TYPE_GROUP)
1127 remove_group (current);
1129 g_free (current->label);
1130 g_free (current->directory);
1131 g_free (current);
1133 current = next;
1138 static void
1139 remove_from_hotlist (struct hotlist *entry)
1141 if (entry == NULL)
1142 return;
1144 if (entry->type == HL_TYPE_DOTDOT)
1145 return;
1147 if (confirm_directory_hotlist_delete)
1149 char *title;
1150 int result;
1152 title = g_strconcat (_(" Remove: "), str_trunc (entry->label, 30), " ", (char *) NULL);
1154 if (safe_delete)
1155 query_set_sel (1);
1156 result = query_dialog (title,
1157 _("\n Are you sure you want to remove this entry?"),
1158 D_ERROR, 2, _("&Yes"), _("&No"));
1160 g_free (title);
1162 if (result != 0)
1163 return;
1166 if (entry->type == HL_TYPE_GROUP)
1168 if (entry->head)
1170 char *header;
1171 int result;
1173 header = g_strconcat (_(" Remove: "), str_trunc (entry->label, 30), " ", (char *) NULL);
1174 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1175 D_ERROR, 2, _("&Yes"), _("&No"));
1176 g_free (header);
1178 if (result != 0)
1179 return;
1182 remove_group (entry);
1185 unlink_entry (entry);
1187 g_free (entry->label);
1188 g_free (entry->directory);
1189 g_free (entry);
1190 /* now remove list entry from screen */
1191 listbox_remove_current (l_hotlist);
1192 hotlist_state.modified = 1;
1195 char *
1196 hotlist_cmd (int vfs_or_hotlist)
1198 char *target = NULL;
1200 hotlist_state.type = vfs_or_hotlist;
1201 load_hotlist ();
1203 init_hotlist (vfs_or_hotlist);
1205 /* display file info */
1206 tty_setcolor (SELECTED_COLOR);
1208 hotlist_state.running = 1;
1209 run_dlg (hotlist_dlg);
1210 hotlist_state.running = 0;
1211 save_hotlist ();
1213 switch (hotlist_dlg->ret_value)
1215 default:
1216 case B_CANCEL:
1217 break;
1219 case B_ENTER:
1221 char *text = NULL;
1222 struct hotlist *hlp = NULL;
1224 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1225 target = g_strdup (hlp != NULL ? hlp->directory : text);
1226 break;
1228 } /* switch */
1230 hotlist_done ();
1231 return target;
1234 static void
1235 load_group (struct hotlist *grp)
1237 gchar **profile_keys, **keys;
1238 gsize len;
1239 char *group_section;
1240 struct hotlist *current = 0;
1242 group_section = find_group_section (grp);
1244 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1246 current_group = grp;
1248 while (*profile_keys)
1250 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1251 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1252 profile_keys++;
1254 g_free (group_section);
1255 g_strfreev (keys);
1257 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1259 while (*profile_keys)
1261 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1262 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1263 profile_keys++;
1265 g_strfreev (keys);
1267 for (current = grp->head; current; current = current->next)
1268 load_group (current);
1271 #define TKN_GROUP 0
1272 #define TKN_ENTRY 1
1273 #define TKN_STRING 2
1274 #define TKN_URL 3
1275 #define TKN_ENDGROUP 4
1276 #define TKN_COMMENT 5
1277 #define TKN_EOL 125
1278 #define TKN_EOF 126
1279 #define TKN_UNKNOWN 127
1281 static GString *tkn_buf = NULL;
1283 static char *hotlist_file_name;
1284 static FILE *hotlist_file;
1285 static time_t hotlist_file_mtime;
1287 static int
1288 hot_skip_blanks (void)
1290 int c;
1292 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1294 return c;
1298 static int
1299 hot_next_token (void)
1301 int c, ret = 0;
1302 size_t l;
1305 if (tkn_buf == NULL)
1306 tkn_buf = g_string_new ("");
1307 g_string_set_size (tkn_buf, 0);
1309 again:
1310 c = hot_skip_blanks ();
1311 switch (c)
1313 case EOF:
1314 ret = TKN_EOF;
1315 break;
1316 case '\n':
1317 ret = TKN_EOL;
1318 break;
1319 case '#':
1320 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1322 g_string_append_c (tkn_buf, c);
1324 ret = TKN_COMMENT;
1325 break;
1326 case '"':
1327 while ((c = getc (hotlist_file)) != EOF && c != '"')
1329 if (c == '\\')
1331 c = getc (hotlist_file);
1332 if (c == EOF)
1334 g_string_free (tkn_buf, TRUE);
1335 return TKN_EOF;
1338 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1340 if (c == EOF)
1341 ret = TKN_EOF;
1342 else
1343 ret = TKN_STRING;
1344 break;
1345 case '\\':
1346 c = getc (hotlist_file);
1347 if (c == EOF)
1349 g_string_free (tkn_buf, TRUE);
1350 return TKN_EOF;
1352 if (c == '\n')
1353 goto again;
1355 /* fall through; it is taken as normal character */
1357 default:
1360 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1362 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1363 if (c != EOF)
1364 ungetc (c, hotlist_file);
1365 l = tkn_buf->len;
1366 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1367 ret = TKN_GROUP;
1368 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1369 ret = TKN_ENTRY;
1370 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1371 ret = TKN_ENDGROUP;
1372 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1373 ret = TKN_URL;
1374 else
1375 ret = TKN_UNKNOWN;
1376 break;
1378 return ret;
1381 #define SKIP_TO_EOL { \
1382 int _tkn; \
1383 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1386 #define CHECK_TOKEN(_TKN_) \
1387 tkn = hot_next_token (); \
1388 if (tkn != _TKN_) { \
1389 hotlist_state.readonly = 1; \
1390 hotlist_state.file_error = 1; \
1391 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1392 tkn = hot_next_token (); \
1393 break; \
1396 static void
1397 hot_load_group (struct hotlist *grp)
1399 int tkn;
1400 struct hotlist *new_grp;
1401 char *label, *url;
1403 current_group = grp;
1405 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1406 switch (tkn)
1408 case TKN_GROUP:
1409 CHECK_TOKEN (TKN_STRING);
1410 new_grp =
1411 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1412 SKIP_TO_EOL;
1413 hot_load_group (new_grp);
1414 current_group = grp;
1415 break;
1416 case TKN_ENTRY:
1417 CHECK_TOKEN (TKN_STRING);
1418 label = g_strdup (tkn_buf->str);
1419 CHECK_TOKEN (TKN_URL);
1420 CHECK_TOKEN (TKN_STRING);
1421 url = g_strdup (tkn_buf->str);
1422 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1423 SKIP_TO_EOL;
1424 break;
1425 case TKN_COMMENT:
1426 label = g_strdup (tkn_buf->str);
1427 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1428 break;
1429 case TKN_EOF:
1430 hotlist_state.readonly = 1;
1431 hotlist_state.file_error = 1;
1432 return;
1433 break;
1434 case TKN_EOL:
1435 /* skip empty lines */
1436 break;
1437 default:
1438 hotlist_state.readonly = 1;
1439 hotlist_state.file_error = 1;
1440 SKIP_TO_EOL;
1441 break;
1443 SKIP_TO_EOL;
1446 static void
1447 hot_load_file (struct hotlist *grp)
1449 int tkn;
1450 struct hotlist *new_grp;
1451 char *label, *url;
1453 current_group = grp;
1455 while ((tkn = hot_next_token ()) != TKN_EOF)
1456 switch (tkn)
1458 case TKN_GROUP:
1459 CHECK_TOKEN (TKN_STRING);
1460 new_grp =
1461 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1462 SKIP_TO_EOL;
1463 hot_load_group (new_grp);
1464 current_group = grp;
1465 break;
1466 case TKN_ENTRY:
1467 CHECK_TOKEN (TKN_STRING);
1468 label = g_strdup (tkn_buf->str);
1469 CHECK_TOKEN (TKN_URL);
1470 CHECK_TOKEN (TKN_STRING);
1471 url = g_strdup (tkn_buf->str);
1472 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1473 SKIP_TO_EOL;
1474 break;
1475 case TKN_COMMENT:
1476 label = g_strdup (tkn_buf->str);
1477 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1478 break;
1479 case TKN_EOL:
1480 /* skip empty lines */
1481 break;
1482 default:
1483 hotlist_state.readonly = 1;
1484 hotlist_state.file_error = 1;
1485 SKIP_TO_EOL;
1486 break;
1490 static void
1491 clean_up_hotlist_groups (const char *section)
1493 char *grp_section;
1494 gchar **profile_keys, **keys;
1495 gsize len;
1497 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1498 if (mc_config_has_group (mc_main_config, section))
1499 mc_config_del_group (mc_main_config, section);
1501 if (mc_config_has_group (mc_main_config, grp_section))
1503 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1505 while (*profile_keys)
1507 clean_up_hotlist_groups (*profile_keys);
1508 profile_keys++;
1510 g_strfreev (keys);
1511 mc_config_del_group (mc_main_config, grp_section);
1513 g_free (grp_section);
1518 static void
1519 load_hotlist (void)
1521 int remove_old_list = 0;
1522 struct stat stat_buf;
1524 if (hotlist_state.loaded)
1526 stat (hotlist_file_name, &stat_buf);
1527 if (hotlist_file_mtime < stat_buf.st_mtime)
1528 done_hotlist ();
1529 else
1530 return;
1533 if (!hotlist_file_name)
1534 hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL);
1536 hotlist = new_hotlist ();
1537 hotlist->type = HL_TYPE_GROUP;
1538 hotlist->label = g_strdup (_(" Top level group "));
1539 hotlist->up = hotlist;
1541 * compatibility :-(
1543 hotlist->directory = g_strdup ("Hotlist");
1545 hotlist_file = fopen (hotlist_file_name, "r");
1546 if (hotlist_file == NULL)
1548 int result;
1550 load_group (hotlist);
1551 hotlist_state.loaded = 1;
1553 * just to be sure we got copy
1555 hotlist_state.modified = 1;
1556 result = save_hotlist ();
1557 hotlist_state.modified = 0;
1558 if (result)
1559 remove_old_list = 1;
1560 else
1561 message (D_ERROR, _(" Hotlist Load "),
1563 ("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1564 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1566 else
1568 hot_load_file (hotlist);
1569 fclose (hotlist_file);
1570 hotlist_state.loaded = 1;
1573 if (remove_old_list)
1575 GError *error = NULL;
1576 clean_up_hotlist_groups ("Hotlist");
1577 if (!mc_config_save_file (mc_main_config, &error))
1578 setup_save_config_show_error (mc_main_config->ini_path, &error);
1581 stat (hotlist_file_name, &stat_buf);
1582 hotlist_file_mtime = stat_buf.st_mtime;
1583 current_group = hotlist;
1587 static int list_level = 0;
1589 static void
1590 hot_save_group (struct hotlist *grp)
1592 struct hotlist *current = grp->head;
1593 int i;
1594 char *s;
1596 #define INDENT(n) \
1597 do { \
1598 for (i = 0; i < n; i++) \
1599 putc (' ', hotlist_file); \
1600 } while (0)
1602 for (; current; current = current->next)
1603 switch (current->type)
1605 case HL_TYPE_GROUP:
1606 INDENT (list_level);
1607 fputs ("GROUP \"", hotlist_file);
1608 for (s = current->label; *s; s++)
1610 if (*s == '"' || *s == '\\')
1611 putc ('\\', hotlist_file);
1612 putc (*s, hotlist_file);
1614 fputs ("\"\n", hotlist_file);
1615 list_level += 2;
1616 hot_save_group (current);
1617 list_level -= 2;
1618 INDENT (list_level);
1619 fputs ("ENDGROUP\n", hotlist_file);
1620 break;
1621 case HL_TYPE_ENTRY:
1622 INDENT (list_level);
1623 fputs ("ENTRY \"", hotlist_file);
1624 for (s = current->label; *s; s++)
1626 if (*s == '"' || *s == '\\')
1627 putc ('\\', hotlist_file);
1628 putc (*s, hotlist_file);
1630 fputs ("\" URL \"", hotlist_file);
1631 for (s = current->directory; *s; s++)
1633 if (*s == '"' || *s == '\\')
1634 putc ('\\', hotlist_file);
1635 putc (*s, hotlist_file);
1637 fputs ("\"\n", hotlist_file);
1638 break;
1639 case HL_TYPE_COMMENT:
1640 fprintf (hotlist_file, "#%s\n", current->label);
1641 break;
1642 case HL_TYPE_DOTDOT:
1643 /* do nothing */
1644 break;
1649 save_hotlist (void)
1651 int saved = 0;
1652 struct stat stat_buf;
1654 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name)
1656 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1658 hotlist_file = fopen (hotlist_file_name, "w");
1659 if (hotlist_file != NULL)
1661 hot_save_group (hotlist);
1662 fclose (hotlist_file);
1663 stat (hotlist_file_name, &stat_buf);
1664 hotlist_file_mtime = stat_buf.st_mtime;
1665 saved = 1;
1666 hotlist_state.modified = 0;
1668 else
1669 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1672 return saved;
1676 * Unload list from memory.
1677 * Don't confuse with hotlist_done() for GUI.
1679 void
1680 done_hotlist (void)
1682 if (hotlist)
1684 remove_group (hotlist);
1685 g_free (hotlist->label);
1686 g_free (hotlist->directory);
1687 g_free (hotlist);
1688 hotlist = 0;
1691 hotlist_state.loaded = 0;
1693 g_free (hotlist_file_name);
1694 hotlist_file_name = 0;
1695 l_hotlist = 0;
1696 current_group = 0;
1698 if (tkn_buf)
1700 g_string_free (tkn_buf, TRUE);
1701 tkn_buf = NULL;
1705 static void
1706 add_dotdot_to_list (void)
1708 if (current_group != hotlist)
1710 if (hotlist_has_dot_dot != 0)
1711 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);