Build hotlist dialogs in normal order.
[midnight-commander.git] / src / filemanager / hotlist.c
blobb4818283b8e2f6790b0555d026b9008ad80da9e7
1 /*
2 Directory hotlist -- for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Radek Doulik, 1994
10 Janne Kukonlehto, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2012
15 Janne did the original Hotlist code, Andrej made the groupable
16 hotlist; the move hotlist and revamped the file format and made
17 it stronger.
19 This file is part of the Midnight Commander.
21 The Midnight Commander is free software: you can redistribute it
22 and/or modify it under the terms of the GNU General Public License as
23 published by the Free Software Foundation, either version 3 of the License,
24 or (at your option) any later version.
26 The Midnight Commander is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 /** \file hotlist.c
36 * \brief Source: directory hotlist
39 #include <config.h>
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
48 #include "lib/global.h"
50 #include "lib/tty/tty.h" /* COLS */
51 #include "lib/tty/key.h" /* KEY_M_CTRL */
52 #include "lib/skin.h" /* colors */
53 #include "lib/mcconfig.h" /* Load/save directories hotlist */
54 #include "lib/fileloc.h"
55 #include "lib/strutil.h"
56 #include "lib/vfs/vfs.h"
57 #include "lib/util.h"
58 #include "lib/widget.h"
60 #include "src/setup.h" /* For profile_bname */
61 #include "src/history.h"
63 #include "midnight.h" /* current_panel */
64 #include "command.h" /* cmdline */
66 #include "hotlist.h"
68 /*** global variables ****************************************************************************/
70 /*** file scope macro definitions ****************************************************************/
72 #define UX 3
73 #define UY 2
75 #define BX UX
76 #define BY (LINES - 6)
78 #define LABELS 3
79 #define B_ADD_CURRENT B_USER
80 #define B_REMOVE (B_USER + 1)
81 #define B_NEW_GROUP (B_USER + 2)
82 #define B_NEW_ENTRY (B_USER + 3)
83 #define B_UP_GROUP (B_USER + 4)
84 #define B_INSERT (B_USER + 5)
85 #define B_APPEND (B_USER + 6)
86 #define B_MOVE (B_USER + 7)
88 #ifdef ENABLE_VFS
89 #define B_FREE_ALL_VFS (B_USER + 8)
90 #define B_REFRESH_VFS (B_USER + 9)
91 #endif
93 #define TKN_GROUP 0
94 #define TKN_ENTRY 1
95 #define TKN_STRING 2
96 #define TKN_URL 3
97 #define TKN_ENDGROUP 4
98 #define TKN_COMMENT 5
99 #define TKN_EOL 125
100 #define TKN_EOF 126
101 #define TKN_UNKNOWN 127
103 #define SKIP_TO_EOL \
105 int _tkn; \
106 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
109 #define CHECK_TOKEN(_TKN_) \
110 tkn = hot_next_token (); \
111 if (tkn != _TKN_) \
113 hotlist_state.readonly = TRUE; \
114 hotlist_state.file_error = TRUE; \
115 while (tkn != TKN_EOL && tkn != TKN_EOF) \
116 tkn = hot_next_token (); \
117 break; \
120 /*** file scope type declarations ****************************************************************/
122 enum HotListType
124 HL_TYPE_GROUP,
125 HL_TYPE_ENTRY,
126 HL_TYPE_COMMENT,
127 HL_TYPE_DOTDOT
130 static struct
133 * these parameters are intended to be user configurable
135 int expanded; /* expanded view of all groups at startup */
138 * these reflect run time state
141 gboolean loaded; /* hotlist is loaded */
142 gboolean readonly; /* hotlist readonly */
143 gboolean file_error; /* parse error while reading file */
144 gboolean running; /* we are running dlg (and have to
145 update listbox */
146 gboolean moving; /* we are in moving hotlist currently */
147 gboolean modified; /* hotlist was modified */
148 hotlist_t type; /* LIST_HOTLIST || LIST_VFSLIST */
149 } hotlist_state;
151 /* Directory hotlist */
152 struct hotlist
154 enum HotListType type;
155 char *directory;
156 char *label;
157 struct hotlist *head;
158 struct hotlist *up;
159 struct hotlist *next;
162 /*** file scope variables ************************************************************************/
164 static gboolean hotlist_has_dot_dot = TRUE;
166 static Dlg_head *hotlist_dlg, *movelist_dlg;
167 static WGroupbox *hotlist_group, *movelist_group;
168 static WListbox *l_hotlist, *l_movelist;
169 static WLabel *pname;
171 static struct
173 int ret_cmd, flags, y, x, len;
174 const char *text;
175 int type;
176 widget_pos_flags_t pos_flags;
177 } hotlist_but[] =
179 /* *INDENT-OFF* */
180 { B_ENTER, DEFPUSH_BUTTON, 0, 0, 0, N_("Change &to"),
181 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
182 #ifdef ENABLE_VFS
183 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, 0, N_("&Free VFSs now"),
184 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
185 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, 0, N_("&Refresh"),
186 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
187 #endif
188 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, 0, N_("&Add current"),
189 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
190 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, 0, N_("&Up"),
191 LIST_HOTLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
192 { B_CANCEL, NORMAL_BUTTON, 0, 53, 0, N_("&Cancel"),
193 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_RIGHT | WPOS_KEEP_BOTTOM },
194 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, 0, N_("New &group"),
195 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
196 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, 0, N_("New &entry"),
197 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
198 { B_INSERT, NORMAL_BUTTON, 1, 0, 0, N_("&Insert"),
199 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
200 { B_APPEND, NORMAL_BUTTON, 1, 15, 0, N_("A&ppend"),
201 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
202 { B_REMOVE, NORMAL_BUTTON, 1, 30, 0, N_("&Remove"),
203 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
204 { B_MOVE, NORMAL_BUTTON, 1, 42, 0, N_("&Move"),
205 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM }
206 /* *INDENT-ON* */
209 static const size_t hotlist_but_num = G_N_ELEMENTS (hotlist_but);
211 static struct hotlist *hotlist = NULL;
213 static struct hotlist *current_group;
215 static GString *tkn_buf = NULL;
217 static char *hotlist_file_name;
218 static FILE *hotlist_file;
219 static time_t hotlist_file_mtime;
221 static int list_level = 0;
223 /* --------------------------------------------------------------------------------------------- */
224 /*** file scope functions ************************************************************************/
225 /* --------------------------------------------------------------------------------------------- */
227 static void init_movelist (struct hotlist *item);
228 static void add_new_group_cmd (void);
229 static void add_new_entry_cmd (void);
230 static void remove_from_hotlist (struct hotlist *entry);
231 static void load_hotlist (void);
232 static void add_dotdot_to_list (void);
234 /* --------------------------------------------------------------------------------------------- */
235 /** If current->data is 0, then we are dealing with a VFS pathname */
237 static void
238 update_path_name (void)
240 const char *text = "";
241 char *p;
242 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
243 Widget *w = WIDGET (list);
245 if (list->count != 0)
247 char *ctext = NULL;
248 void *cdata = NULL;
250 listbox_get_current (list, &ctext, &cdata);
251 if (cdata == NULL)
252 text = ctext;
253 else
255 struct hotlist *hlp = (struct hotlist *) cdata;
257 if (hlp->type == HL_TYPE_ENTRY || hlp->type == HL_TYPE_DOTDOT)
258 text = hlp->directory;
259 else if (hlp->type == HL_TYPE_GROUP)
260 text = _("Subgroup - press ENTER to see list");
264 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
265 if (hotlist_state.moving)
266 groupbox_set_title (movelist_group, str_trunc (p, w->cols - 2));
267 else
269 groupbox_set_title (hotlist_group, str_trunc (p, w->cols - 2));
270 label_set_text (pname, str_trunc (text, w->cols));
272 g_free (p);
275 /* --------------------------------------------------------------------------------------------- */
277 static void
278 fill_listbox (WListbox * list)
280 struct hotlist *current;
281 GString *buff;
283 buff = g_string_new ("");
285 for (current = current_group->head; current != NULL; current = current->next)
286 switch (current->type)
288 case HL_TYPE_GROUP:
290 /* buff clean up */
291 g_string_truncate (buff, 0);
292 g_string_append (buff, "->");
293 g_string_append (buff, current->label);
294 listbox_add_item (list, LISTBOX_APPEND_AT_END, 0, buff->str, current);
296 break;
297 case HL_TYPE_DOTDOT:
298 case HL_TYPE_ENTRY:
299 listbox_add_item (list, LISTBOX_APPEND_AT_END, 0, current->label, current);
300 default:
301 break;
304 g_string_free (buff, TRUE);
307 /* --------------------------------------------------------------------------------------------- */
309 static void
310 unlink_entry (struct hotlist *entry)
312 struct hotlist *current = current_group->head;
314 if (current == entry)
315 current_group->head = entry->next;
316 else
318 while (current != NULL && current->next != entry)
319 current = current->next;
320 if (current != NULL)
321 current->next = entry->next;
323 entry->next = entry->up = NULL;
326 /* --------------------------------------------------------------------------------------------- */
328 #ifdef ENABLE_VFS
329 static void
330 add_name_to_list (const char *path)
332 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, path, 0);
334 #endif /* !ENABLE_VFS */
336 /* --------------------------------------------------------------------------------------------- */
338 static int
339 hotlist_button_callback (WButton * button, int action)
341 (void) button;
343 switch (action)
345 case B_MOVE:
347 struct hotlist *saved = current_group;
348 struct hotlist *item = NULL;
349 struct hotlist *moveto_item = NULL;
350 struct hotlist *moveto_group = NULL;
351 int ret;
353 if (l_hotlist->count == 0)
354 return 0; /* empty group - nothing to do */
356 listbox_get_current (l_hotlist, NULL, (void **) &item);
357 init_movelist (item);
358 hotlist_state.moving = TRUE;
359 ret = run_dlg (movelist_dlg);
360 hotlist_state.moving = FALSE;
361 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
362 moveto_group = current_group;
363 destroy_dlg (movelist_dlg);
364 current_group = saved;
365 if (ret == B_CANCEL)
366 return 0;
367 if (moveto_item == item)
368 return 0; /* If we insert/append a before/after a
369 it hardly changes anything ;) */
370 unlink_entry (item);
371 listbox_remove_current (l_hotlist);
372 item->up = moveto_group;
373 if (moveto_group->head == NULL)
374 moveto_group->head = item;
375 else if (moveto_item == NULL)
376 { /* we have group with just comments */
377 struct hotlist *p = moveto_group->head;
379 /* skip comments */
380 while (p->next != NULL)
381 p = p->next;
382 p->next = item;
384 else if (ret == B_ENTER || ret == B_APPEND)
386 if (moveto_item->next == NULL)
387 moveto_item->next = item;
388 else
390 item->next = moveto_item->next;
391 moveto_item->next = item;
394 else if (moveto_group->head == moveto_item)
396 moveto_group->head = item;
397 item->next = moveto_item;
399 else
401 struct hotlist *p = moveto_group->head;
403 while (p->next != moveto_item)
404 p = p->next;
405 item->next = p->next;
406 p->next = item;
408 listbox_remove_list (l_hotlist);
409 fill_listbox (l_hotlist);
410 repaint_screen ();
411 hotlist_state.modified = TRUE;
412 return 0;
414 case B_REMOVE:
416 struct hotlist *entry = NULL;
418 listbox_get_current (l_hotlist, NULL, (void **) &entry);
419 remove_from_hotlist (entry);
421 return 0;
423 case B_NEW_GROUP:
424 add_new_group_cmd ();
425 return 0;
427 case B_ADD_CURRENT:
428 add2hotlist_cmd ();
429 return 0;
431 case B_NEW_ENTRY:
432 add_new_entry_cmd ();
433 return 0;
435 case B_ENTER:
437 WListbox *list;
438 void *data;
439 struct hotlist *hlp;
441 list = hotlist_state.moving ? l_movelist : l_hotlist;
442 listbox_get_current (list, NULL, &data);
444 if (data == NULL)
445 return 1;
447 hlp = (struct hotlist *) data;
449 if (hlp->type == HL_TYPE_ENTRY)
450 return 1;
451 if (hlp->type != HL_TYPE_DOTDOT)
453 listbox_remove_list (list);
454 current_group = hlp;
455 fill_listbox (list);
456 return 0;
458 /* Fall through - go up */
460 /* Fall through if list empty - just go up */
462 case B_UP_GROUP:
464 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
466 listbox_remove_list (list);
467 current_group = current_group->up;
468 fill_listbox (list);
469 return 0;
472 #ifdef ENABLE_VFS
473 case B_FREE_ALL_VFS:
474 vfs_expire (TRUE);
475 /* fall through */
477 case B_REFRESH_VFS:
478 listbox_remove_list (l_hotlist);
479 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, mc_config_get_home_dir (), 0);
480 vfs_fill_names (add_name_to_list);
481 return 0;
482 #endif /* ENABLE_VFS */
484 default:
485 return 1;
489 /* --------------------------------------------------------------------------------------------- */
491 static inline cb_ret_t
492 hotlist_handle_key (Dlg_head * h, int key)
494 switch (key)
496 case KEY_M_CTRL | '\n':
497 goto l1;
499 case '\n':
500 case KEY_ENTER:
501 case KEY_RIGHT:
502 if (hotlist_button_callback (NULL, B_ENTER) != 0)
504 h->ret_value = B_ENTER;
505 dlg_stop (h);
507 return MSG_HANDLED;
509 case KEY_LEFT:
510 if (hotlist_state.type == LIST_VFSLIST)
511 return MSG_NOT_HANDLED;
512 return hotlist_button_callback (NULL, B_UP_GROUP) == 0 ? MSG_HANDLED : MSG_NOT_HANDLED;
514 case KEY_DC:
515 if (hotlist_state.moving)
516 return MSG_NOT_HANDLED;
517 hotlist_button_callback (NULL, B_REMOVE);
518 return MSG_HANDLED;
521 case ALT ('\n'):
522 case ALT ('\r'):
523 if (!hotlist_state.moving)
525 void *ldata = NULL;
527 listbox_get_current (l_hotlist, NULL, &ldata);
529 if (ldata != NULL)
531 struct hotlist *hlp = (struct hotlist *) ldata;
533 if (hlp->type == HL_TYPE_ENTRY)
535 char *tmp;
537 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
538 input_insert (cmdline, tmp, FALSE);
539 g_free (tmp);
540 h->ret_value = B_CANCEL;
541 dlg_stop (h);
545 return MSG_HANDLED; /* ignore key */
547 default:
548 return MSG_NOT_HANDLED;
552 /* --------------------------------------------------------------------------------------------- */
554 static cb_ret_t
555 hotlist_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
557 switch (msg)
559 case DLG_UNHANDLED_KEY:
560 return hotlist_handle_key (h, parm);
562 case DLG_POST_KEY:
563 dlg_select_widget (h == hotlist_dlg ? l_hotlist : l_movelist);
564 /* always stay on hotlist */
565 /* fall through */
567 case DLG_INIT:
568 update_path_name ();
569 return MSG_HANDLED;
571 case DLG_RESIZE:
572 /* simply call dlg_set_size() with new size */
573 dlg_set_size (h, LINES - (h == hotlist_dlg ? 2 : 6), COLS - 6);
574 return MSG_HANDLED;
576 default:
577 return default_dlg_callback (h, sender, msg, parm, data);
581 /* --------------------------------------------------------------------------------------------- */
583 static lcback_ret_t
584 hotlist_listbox_callback (WListbox * list)
586 Dlg_head *dlg = WIDGET (list)->owner;
588 if (list->count != 0)
590 void *data = NULL;
592 listbox_get_current (list, NULL, &data);
594 if (data != NULL)
596 struct hotlist *hlp = (struct hotlist *) data;
598 if (hlp->type == HL_TYPE_ENTRY)
600 dlg->ret_value = B_ENTER;
601 dlg_stop (dlg);
602 return LISTBOX_DONE;
604 else
606 hotlist_button_callback (NULL, B_ENTER);
607 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
608 return LISTBOX_CONT;
611 else
613 dlg->ret_value = B_ENTER;
614 dlg_stop (dlg);
615 return LISTBOX_DONE;
619 hotlist_button_callback (NULL, B_UP_GROUP);
620 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
621 return LISTBOX_CONT;
624 /* --------------------------------------------------------------------------------------------- */
626 * Expands all button names (once) and recalculates button positions.
627 * returns number of columns in the dialog box, which is 10 chars longer
628 * then buttonbar.
630 * If common width of the window (i.e. in xterm) is less than returned
631 * width - sorry :) (anyway this did not handled in previous version too)
634 static int
635 init_i18n_stuff (int list_type, int cols)
637 size_t i;
639 #ifdef ENABLE_NLS
640 static gboolean i18n_flag = FALSE;
642 if (!i18n_flag)
644 for (i = 0; i < hotlist_but_num; i++)
646 hotlist_but[i].text = _(hotlist_but[i].text);
647 hotlist_but[i].len = str_term_width1 (hotlist_but[i].text) + 3;
648 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
649 hotlist_but[i].len += 2;
652 i18n_flag = TRUE;
654 #endif /* ENABLE_NLS */
656 /* Dynamic resizing of buttonbars */
658 int len[2], count[2]; /* at most two lines of buttons */
659 int cur_x[2];
661 len[0] = len[1] = 0;
662 count[0] = count[1] = 0;
663 cur_x[0] = cur_x[1] = 0;
665 /* Count len of buttonbars, assuming 1 extra space between buttons */
666 for (i = 0; i < hotlist_but_num; i++)
667 if ((hotlist_but[i].type & list_type) != 0)
669 int row;
671 row = hotlist_but[i].y;
672 ++count[row];
673 len[row] += hotlist_but[i].len + 1;
676 (len[0])--;
677 (len[1])--;
679 cols = max (cols, max (len[0], len[1]));
681 /* arrange buttons */
682 for (i = 0; i < hotlist_but_num; i++)
683 if ((hotlist_but[i].type & list_type) != 0)
685 int row;
687 row = hotlist_but[i].y;
689 if (hotlist_but[i].x != 0)
691 /* not first int the row */
692 if (hotlist_but[i].ret_cmd == B_CANCEL)
693 hotlist_but[i].x = cols - hotlist_but[i].len - 6;
694 else
695 hotlist_but[i].x = cur_x[row];
698 cur_x[row] += hotlist_but[i].len + 1;
702 return cols;
705 /* --------------------------------------------------------------------------------------------- */
707 static void
708 init_hotlist (hotlist_t list_type)
710 size_t i;
711 const char *title, *help_node;
712 int lines, cols;
713 int y;
714 int dh = 0;
715 WGroupbox *path_box;
716 Widget *hotlist_widget;
718 do_refresh ();
720 lines = LINES - 2;
721 cols = init_i18n_stuff (list_type, COLS - 6);
723 hotlist_state.expanded =
724 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
726 #ifdef ENABLE_VFS
727 if (list_type == LIST_VFSLIST)
729 title = _("Active VFS directories");
730 help_node = "[vfshot]"; /* FIXME - no such node */
731 dh = 1;
733 else
734 #endif /* !ENABLE_VFS */
736 title = _("Directory hotlist");
737 help_node = "[Hotlist]";
740 hotlist_dlg =
741 create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, hotlist_callback, NULL, help_node,
742 title, DLG_CENTER);
744 y = UY;
745 hotlist_group = groupbox_new (y, UX, lines - 10 + dh, cols - 2 * UX, _("Top level group"));
746 hotlist_widget = WIDGET (hotlist_group);
747 add_widget_autopos (hotlist_dlg, hotlist_widget, WPOS_KEEP_ALL, NULL);
749 l_hotlist =
750 listbox_new (y + 1, UX + 1, hotlist_widget->lines - 2, hotlist_widget->cols - 2, FALSE,
751 hotlist_listbox_callback);
753 /* Fill the hotlist with the active VFS or the hotlist */
754 #ifdef ENABLE_VFS
755 if (list_type == LIST_VFSLIST)
757 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, mc_config_get_home_dir (), 0);
758 vfs_fill_names (add_name_to_list);
760 else
761 #endif /* !ENABLE_VFS */
762 fill_listbox (l_hotlist);
764 /* insert before groupbox to view scrollbar */
765 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL, NULL);
767 y += hotlist_widget->lines;
769 path_box = groupbox_new (y, UX, 3, hotlist_widget->cols, _("Directory path"));
770 add_widget_autopos (hotlist_dlg, path_box, WPOS_KEEP_BOTTOM | WPOS_KEEP_HORZ, NULL);
772 pname = label_new (y + 1, UX + 2, "");
773 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT, NULL);
774 y += WIDGET (path_box)->lines;
776 add_widget_autopos (hotlist_dlg, hline_new (y++, -1, -1), WPOS_KEEP_BOTTOM, NULL);
778 for (i = 0; i < hotlist_but_num; i++)
779 if ((hotlist_but[i].type & list_type) != 0)
780 add_widget_autopos (hotlist_dlg,
781 button_new (y + hotlist_but[i].y, UX + hotlist_but[i].x,
782 hotlist_but[i].ret_cmd, hotlist_but[i].flags,
783 hotlist_but[i].text, hotlist_button_callback),
784 hotlist_but[i].pos_flags, NULL);
786 dlg_select_widget (l_hotlist);
789 /* --------------------------------------------------------------------------------------------- */
791 static void
792 init_movelist ( struct hotlist *item)
794 size_t i;
795 char *hdr;
796 int lines, cols;
797 int y;
798 Widget *movelist_widget;
800 do_refresh ();
802 lines = LINES - 6;
803 cols = init_i18n_stuff (LIST_MOVELIST, COLS - 6);
805 hdr = g_strdup_printf (_("Moving %s"), item->label);
807 movelist_dlg =
808 create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, hotlist_callback, NULL, "[Hotlist]",
809 hdr, DLG_CENTER);
811 g_free (hdr);
813 y = UY;
814 movelist_group = groupbox_new (y, UX, lines - 7, cols - 2 * UX, _("Directory label"));
815 movelist_widget = WIDGET (movelist_group);
816 add_widget_autopos (movelist_dlg, movelist_widget, WPOS_KEEP_ALL, NULL);
818 l_movelist =
819 listbox_new (y + 1, UX + 1, movelist_widget->lines - 2, movelist_widget->cols - 2, FALSE,
820 hotlist_listbox_callback);
821 fill_listbox (l_movelist);
822 /* insert before groupbox to view scrollbar */
823 add_widget_autopos (movelist_dlg, l_movelist, WPOS_KEEP_ALL, NULL);
825 y += movelist_widget->lines;
827 add_widget_autopos (movelist_dlg, hline_new (y++, -1, -1), WPOS_KEEP_BOTTOM, NULL);
829 for (i = 0; i < hotlist_but_num; i++)
830 if ((hotlist_but[i].type & LIST_MOVELIST) != 0)
831 add_widget_autopos (movelist_dlg,
832 button_new (y + hotlist_but[i].y, UX + hotlist_but[i].x,
833 hotlist_but[i].ret_cmd, hotlist_but[i].flags,
834 hotlist_but[i].text, hotlist_button_callback),
835 hotlist_but[i].pos_flags, NULL);
837 dlg_select_widget (l_movelist);
840 /* --------------------------------------------------------------------------------------------- */
842 * Destroy the list dialog.
843 * Don't confuse with done_hotlist() for the list in memory.
846 static void
847 hotlist_done (void)
849 destroy_dlg (hotlist_dlg);
850 l_hotlist = NULL;
851 if (FALSE)
852 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
853 repaint_screen ();
856 /* --------------------------------------------------------------------------------------------- */
858 static inline char *
859 find_group_section (struct hotlist *grp)
861 return g_strconcat (grp->directory, ".Group", (char *) NULL);
864 /* --------------------------------------------------------------------------------------------- */
866 static struct hotlist *
867 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
869 struct hotlist *new;
870 struct hotlist *current = NULL;
873 * Hotlist is neither loaded nor loading.
874 * Must be called by "Ctrl-x a" before using hotlist.
876 if (current_group == NULL)
877 load_hotlist ();
879 listbox_get_current (l_hotlist, NULL, (void **) &current);
881 /* Make sure `..' stays at the top of the list. */
882 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
883 pos = LISTBOX_APPEND_AFTER;
885 new = g_new0 (struct hotlist, 1);
887 new->type = type;
888 new->label = label;
889 new->directory = directory;
890 new->up = current_group;
892 if (type == HL_TYPE_GROUP)
894 current_group = new;
895 add_dotdot_to_list ();
896 current_group = new->up;
899 if (current_group->head == NULL)
901 /* first element in group */
902 current_group->head = new;
904 else if (pos == LISTBOX_APPEND_AFTER)
906 new->next = current->next;
907 current->next = new;
909 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
911 /* should be inserted before first item */
912 new->next = current;
913 current_group->head = new;
915 else if (pos == LISTBOX_APPEND_BEFORE)
917 struct hotlist *p = current_group->head;
919 while (p->next != current)
920 p = p->next;
922 new->next = current;
923 p->next = new;
925 else
926 { /* append at the end */
927 struct hotlist *p = current_group->head;
929 while (p->next != NULL)
930 p = p->next;
932 p->next = new;
935 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
937 if (type == HL_TYPE_GROUP)
939 char *lbl;
941 lbl = g_strconcat ("->", new->label, (char *) NULL);
942 listbox_add_item (l_hotlist, pos, 0, lbl, new);
943 g_free (lbl);
945 else
946 listbox_add_item (l_hotlist, pos, 0, new->label, new);
947 listbox_select_entry (l_hotlist, l_hotlist->pos);
950 return new;
953 /* --------------------------------------------------------------------------------------------- */
955 static int
956 add_new_entry_input (const char *header, const char *text1, const char *text2,
957 const char *help, char **r1, char **r2)
959 quick_widget_t quick_widgets[] = {
960 /* *INDENT-OFF* */
961 QUICK_LABELED_INPUT (text1, input_label_above, *r1, 0, "input-lbl", r1, NULL),
962 QUICK_SEPARATOR (FALSE),
963 QUICK_LABELED_INPUT (text2, input_label_above, *r2, 0, "input-lbl", r2, NULL),
964 QUICK_START_BUTTONS (TRUE, TRUE),
965 QUICK_BUTTON (N_("&Append"), B_APPEND, NULL, NULL),
966 QUICK_BUTTON (N_("&Insert"), B_INSERT, NULL, NULL),
967 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
968 QUICK_END
969 /* *INDENT-ON* */
972 quick_dialog_t qdlg = {
973 -1, -1, 64,
974 header, help,
975 quick_widgets, NULL, NULL
978 int ret;
980 ret = quick_dialog (&qdlg);
982 return (ret != B_CANCEL) ? ret : 0;
985 /* --------------------------------------------------------------------------------------------- */
987 static void
988 add_new_entry_cmd (void)
990 char *title, *url, *to_free;
991 int ret;
993 /* Take current directory as default value for input fields */
994 to_free = title = url = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
996 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
997 _("Directory path:"), "[Hotlist]", &title, &url);
998 g_free (to_free);
1000 if (ret == 0)
1001 return;
1002 if (title == NULL || *title == '\0' || url == NULL || *url == '\0')
1004 g_free (title);
1005 g_free (url);
1006 return;
1009 if (ret == B_ENTER || ret == B_APPEND)
1010 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1011 else
1012 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1014 hotlist_state.modified = TRUE;
1017 /* --------------------------------------------------------------------------------------------- */
1019 static int
1020 add_new_group_input (const char *header, const char *label, char **result)
1022 quick_widget_t quick_widgets[] = {
1023 /* *INDENT-OFF* */
1024 QUICK_LABELED_INPUT (label, input_label_above, "", 0, "input", result, NULL),
1025 QUICK_START_BUTTONS (TRUE, TRUE),
1026 QUICK_BUTTON (N_("&Append"), B_APPEND, NULL, NULL),
1027 QUICK_BUTTON (N_("&Insert"), B_INSERT, NULL, NULL),
1028 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
1029 QUICK_END
1030 /* *INDENT-ON* */
1033 quick_dialog_t qdlg = {
1034 -1, -1, 64,
1035 header, "[Hotlist]",
1036 quick_widgets, NULL, NULL
1039 int ret;
1041 ret = quick_dialog (&qdlg);
1043 return (ret != B_CANCEL) ? ret : 0;
1046 /* --------------------------------------------------------------------------------------------- */
1048 static void
1049 add_new_group_cmd (void)
1051 char *label;
1052 int ret;
1054 ret = add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label);
1055 if (ret == 0 || label == NULL || *label == '\0')
1056 return;
1058 if (ret == B_ENTER || ret == B_APPEND)
1059 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1060 else
1061 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1063 hotlist_state.modified = TRUE;
1066 /* --------------------------------------------------------------------------------------------- */
1068 static void
1069 remove_group (struct hotlist *grp)
1071 struct hotlist *current = grp->head;
1073 while (current != NULL)
1075 struct hotlist *next = current->next;
1077 if (current->type == HL_TYPE_GROUP)
1078 remove_group (current);
1080 g_free (current->label);
1081 g_free (current->directory);
1082 g_free (current);
1084 current = next;
1088 /* --------------------------------------------------------------------------------------------- */
1090 static void
1091 remove_from_hotlist (struct hotlist *entry)
1093 if (entry == NULL)
1094 return;
1096 if (entry->type == HL_TYPE_DOTDOT)
1097 return;
1099 if (confirm_directory_hotlist_delete)
1101 char text[BUF_MEDIUM];
1102 int result;
1104 if (safe_delete)
1105 query_set_sel (1);
1107 g_snprintf (text, sizeof (text), _("Are you sure you want to remove entry \"%s\"?"),
1108 str_trunc (entry->label, 30));
1109 result = query_dialog (Q_ ("DialogTitle|Delete"), text, D_ERROR | D_CENTER, 2,
1110 _("&Yes"), _("&No"));
1111 if (result != 0)
1112 return;
1115 if (entry->type == HL_TYPE_GROUP)
1117 struct hotlist *head = entry->head;
1119 if (head != NULL && (head->type != HL_TYPE_DOTDOT || head->next != NULL))
1121 char text[BUF_MEDIUM];
1122 int result;
1124 g_snprintf (text, sizeof (text), _("Group \"%s\" is not empty.\nRemove it?"),
1125 str_trunc (entry->label, 30));
1126 result = query_dialog (Q_ ("DialogTitle|Delete"), text, D_ERROR | D_CENTER, 2,
1127 _("&Yes"), _("&No"));
1128 if (result != 0)
1129 return;
1132 remove_group (entry);
1135 unlink_entry (entry);
1137 g_free (entry->label);
1138 g_free (entry->directory);
1139 g_free (entry);
1140 /* now remove list entry from screen */
1141 listbox_remove_current (l_hotlist);
1142 hotlist_state.modified = TRUE;
1145 /* --------------------------------------------------------------------------------------------- */
1147 static void
1148 load_group (struct hotlist *grp)
1150 gchar **profile_keys, **keys;
1151 gsize len;
1152 char *group_section;
1153 struct hotlist *current = 0;
1155 group_section = find_group_section (grp);
1157 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1159 current_group = grp;
1161 while (*profile_keys != NULL)
1163 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1164 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1165 profile_keys++;
1167 g_free (group_section);
1168 g_strfreev (keys);
1170 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1172 while (*profile_keys != NULL)
1174 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1175 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1176 profile_keys++;
1178 g_strfreev (keys);
1180 for (current = grp->head; current; current = current->next)
1181 load_group (current);
1184 /* --------------------------------------------------------------------------------------------- */
1186 static int
1187 hot_skip_blanks (void)
1189 int c;
1191 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1193 return c;
1196 /* --------------------------------------------------------------------------------------------- */
1198 static int
1199 hot_next_token (void)
1201 int c, ret = 0;
1202 size_t l;
1204 if (tkn_buf == NULL)
1205 tkn_buf = g_string_new ("");
1206 g_string_set_size (tkn_buf, 0);
1208 again:
1209 c = hot_skip_blanks ();
1210 switch (c)
1212 case EOF:
1213 ret = TKN_EOF;
1214 break;
1215 case '\n':
1216 ret = TKN_EOL;
1217 break;
1218 case '#':
1219 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1220 g_string_append_c (tkn_buf, c);
1221 ret = TKN_COMMENT;
1222 break;
1223 case '"':
1224 while ((c = getc (hotlist_file)) != EOF && c != '"')
1226 if (c == '\\')
1228 c = getc (hotlist_file);
1229 if (c == EOF)
1231 g_string_free (tkn_buf, TRUE);
1232 return TKN_EOF;
1235 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1237 ret = (c == EOF) ? TKN_EOF : TKN_STRING;
1238 break;
1239 case '\\':
1240 c = getc (hotlist_file);
1241 if (c == EOF)
1243 g_string_free (tkn_buf, TRUE);
1244 return TKN_EOF;
1246 if (c == '\n')
1247 goto again;
1249 /* fall through; it is taken as normal character */
1251 default:
1254 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1256 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1257 if (c != EOF)
1258 ungetc (c, hotlist_file);
1259 l = tkn_buf->len;
1260 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1261 ret = TKN_GROUP;
1262 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1263 ret = TKN_ENTRY;
1264 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1265 ret = TKN_ENDGROUP;
1266 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1267 ret = TKN_URL;
1268 else
1269 ret = TKN_UNKNOWN;
1270 break;
1272 return ret;
1275 /* --------------------------------------------------------------------------------------------- */
1277 static void
1278 hot_load_group (struct hotlist *grp)
1280 int tkn;
1281 struct hotlist *new_grp;
1282 char *label, *url;
1284 current_group = grp;
1286 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1287 switch (tkn)
1289 case TKN_GROUP:
1290 CHECK_TOKEN (TKN_STRING);
1291 new_grp =
1292 add2hotlist (g_strndup (tkn_buf->str, tkn_buf->len), 0, HL_TYPE_GROUP,
1293 LISTBOX_APPEND_AT_END);
1294 SKIP_TO_EOL;
1295 hot_load_group (new_grp);
1296 current_group = grp;
1297 break;
1298 case TKN_ENTRY:
1300 CHECK_TOKEN (TKN_STRING);
1301 label = g_strndup (tkn_buf->str, tkn_buf->len);
1302 CHECK_TOKEN (TKN_URL);
1303 CHECK_TOKEN (TKN_STRING);
1304 url = tilde_expand (tkn_buf->str);
1305 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1306 SKIP_TO_EOL;
1308 break;
1309 case TKN_COMMENT:
1310 label = g_strndup (tkn_buf->str, tkn_buf->len);
1311 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1312 break;
1313 case TKN_EOF:
1314 hotlist_state.readonly = TRUE;
1315 hotlist_state.file_error = TRUE;
1316 return;
1317 case TKN_EOL:
1318 /* skip empty lines */
1319 break;
1320 default:
1321 hotlist_state.readonly = TRUE;
1322 hotlist_state.file_error = TRUE;
1323 SKIP_TO_EOL;
1324 break;
1326 SKIP_TO_EOL;
1329 /* --------------------------------------------------------------------------------------------- */
1331 static void
1332 hot_load_file (struct hotlist *grp)
1334 int tkn;
1335 struct hotlist *new_grp;
1336 char *label, *url;
1338 current_group = grp;
1340 while ((tkn = hot_next_token ()) != TKN_EOF)
1341 switch (tkn)
1343 case TKN_GROUP:
1344 CHECK_TOKEN (TKN_STRING);
1345 new_grp =
1346 add2hotlist (g_strndup (tkn_buf->str, tkn_buf->len), 0, HL_TYPE_GROUP,
1347 LISTBOX_APPEND_AT_END);
1348 SKIP_TO_EOL;
1349 hot_load_group (new_grp);
1350 current_group = grp;
1351 break;
1352 case TKN_ENTRY:
1354 CHECK_TOKEN (TKN_STRING);
1355 label = g_strndup (tkn_buf->str, tkn_buf->len);
1356 CHECK_TOKEN (TKN_URL);
1357 CHECK_TOKEN (TKN_STRING);
1358 url = tilde_expand (tkn_buf->str);
1359 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1360 SKIP_TO_EOL;
1362 break;
1363 case TKN_COMMENT:
1364 label = g_strndup (tkn_buf->str, tkn_buf->len);
1365 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1366 break;
1367 case TKN_EOL:
1368 /* skip empty lines */
1369 break;
1370 default:
1371 hotlist_state.readonly = TRUE;
1372 hotlist_state.file_error = TRUE;
1373 SKIP_TO_EOL;
1374 break;
1378 /* --------------------------------------------------------------------------------------------- */
1380 static void
1381 clean_up_hotlist_groups (const char *section)
1383 char *grp_section;
1384 gchar **profile_keys, **keys;
1385 gsize len;
1387 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1388 if (mc_config_has_group (mc_main_config, section))
1389 mc_config_del_group (mc_main_config, section);
1391 if (mc_config_has_group (mc_main_config, grp_section))
1393 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1395 while (*profile_keys != NULL)
1397 clean_up_hotlist_groups (*profile_keys);
1398 profile_keys++;
1400 g_strfreev (keys);
1401 mc_config_del_group (mc_main_config, grp_section);
1403 g_free (grp_section);
1406 /* --------------------------------------------------------------------------------------------- */
1408 static void
1409 load_hotlist (void)
1411 gboolean remove_old_list = FALSE;
1412 struct stat stat_buf;
1414 if (hotlist_state.loaded)
1416 stat (hotlist_file_name, &stat_buf);
1417 if (hotlist_file_mtime < stat_buf.st_mtime)
1418 done_hotlist ();
1419 else
1420 return;
1423 if (hotlist_file_name == NULL)
1424 hotlist_file_name = mc_config_get_full_path (MC_HOTLIST_FILE);
1426 hotlist = g_new0 (struct hotlist, 1);
1427 hotlist->type = HL_TYPE_GROUP;
1428 hotlist->label = g_strdup (_("Top level group"));
1429 hotlist->up = hotlist;
1431 * compatibility :-(
1433 hotlist->directory = g_strdup ("Hotlist");
1435 hotlist_file = fopen (hotlist_file_name, "r");
1436 if (hotlist_file == NULL)
1438 int result;
1440 load_group (hotlist);
1441 hotlist_state.loaded = TRUE;
1443 * just to be sure we got copy
1445 hotlist_state.modified = TRUE;
1446 result = save_hotlist ();
1447 hotlist_state.modified = FALSE;
1448 if (result != 0)
1449 remove_old_list = TRUE;
1450 else
1451 message (D_ERROR, _("Hotlist Load"),
1453 ("MC was unable to write %s file,\nyour old hotlist entries were not deleted"),
1454 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1456 else
1458 hot_load_file (hotlist);
1459 fclose (hotlist_file);
1460 hotlist_state.loaded = TRUE;
1463 if (remove_old_list)
1465 GError *error = NULL;
1467 clean_up_hotlist_groups ("Hotlist");
1468 if (!mc_config_save_file (mc_main_config, &error))
1469 setup_save_config_show_error (mc_main_config->ini_path, &error);
1472 stat (hotlist_file_name, &stat_buf);
1473 hotlist_file_mtime = stat_buf.st_mtime;
1474 current_group = hotlist;
1477 /* --------------------------------------------------------------------------------------------- */
1479 static void
1480 hot_save_group (struct hotlist *grp)
1482 struct hotlist *current;
1483 int i;
1484 char *s;
1486 #define INDENT(n) \
1487 do { \
1488 for (i = 0; i < n; i++) \
1489 putc (' ', hotlist_file); \
1490 } while (0)
1492 for (current = grp->head; current != NULL; current = current->next)
1493 switch (current->type)
1495 case HL_TYPE_GROUP:
1496 INDENT (list_level);
1497 fputs ("GROUP \"", hotlist_file);
1498 for (s = current->label; *s != '\0'; s++)
1500 if (*s == '"' || *s == '\\')
1501 putc ('\\', hotlist_file);
1502 putc (*s, hotlist_file);
1504 fputs ("\"\n", hotlist_file);
1505 list_level += 2;
1506 hot_save_group (current);
1507 list_level -= 2;
1508 INDENT (list_level);
1509 fputs ("ENDGROUP\n", hotlist_file);
1510 break;
1511 case HL_TYPE_ENTRY:
1512 INDENT (list_level);
1513 fputs ("ENTRY \"", hotlist_file);
1514 for (s = current->label; *s != '\0'; s++)
1516 if (*s == '"' || *s == '\\')
1517 putc ('\\', hotlist_file);
1518 putc (*s, hotlist_file);
1520 fputs ("\" URL \"", hotlist_file);
1521 for (s = current->directory; *s != '\0'; s++)
1523 if (*s == '"' || *s == '\\')
1524 putc ('\\', hotlist_file);
1525 putc (*s, hotlist_file);
1527 fputs ("\"\n", hotlist_file);
1528 break;
1529 case HL_TYPE_COMMENT:
1530 fprintf (hotlist_file, "#%s\n", current->label);
1531 break;
1532 case HL_TYPE_DOTDOT:
1533 /* do nothing */
1534 break;
1538 /* --------------------------------------------------------------------------------------------- */
1540 static void
1541 add_dotdot_to_list (void)
1543 if (current_group != hotlist && hotlist_has_dot_dot)
1544 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);
1547 /* --------------------------------------------------------------------------------------------- */
1548 /*** public functions ****************************************************************************/
1549 /* --------------------------------------------------------------------------------------------- */
1551 void
1552 add2hotlist_cmd (void)
1554 char *lc_prompt;
1555 const char *cp = N_("Label for \"%s\":");
1556 int l;
1557 char *label_string, *label;
1559 #ifdef ENABLE_NLS
1560 cp = _(cp);
1561 #endif
1563 l = str_term_width1 (cp);
1564 label_string = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
1565 lc_prompt = g_strdup_printf (cp, str_trunc (label_string, COLS - 2 * UX - (l + 8)));
1566 label = input_dialog (_("Add to hotlist"), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1567 g_free (lc_prompt);
1569 if (label == NULL || *label == '\0')
1571 g_free (label_string);
1572 g_free (label);
1574 else
1576 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1577 hotlist_state.modified = TRUE;
1581 /* --------------------------------------------------------------------------------------------- */
1583 char *
1584 hotlist_show (hotlist_t list_type)
1586 char *target = NULL;
1587 int res;
1589 hotlist_state.type = list_type;
1590 load_hotlist ();
1592 init_hotlist (list_type);
1594 /* display file info */
1595 tty_setcolor (SELECTED_COLOR);
1597 hotlist_state.running = TRUE;
1598 res = run_dlg (hotlist_dlg);
1599 hotlist_state.running = FALSE;
1600 save_hotlist ();
1602 if (res == B_ENTER)
1604 char *text = NULL;
1605 struct hotlist *hlp = NULL;
1607 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1608 target = g_strdup (hlp != NULL ? hlp->directory : text);
1611 hotlist_done ();
1612 return target;
1615 /* --------------------------------------------------------------------------------------------- */
1617 gboolean
1618 save_hotlist (void)
1620 gboolean saved = FALSE;
1621 struct stat stat_buf;
1623 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name != NULL)
1625 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1627 hotlist_file = fopen (hotlist_file_name, "w");
1628 if (hotlist_file == NULL)
1629 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1630 else
1632 hot_save_group (hotlist);
1633 fclose (hotlist_file);
1634 stat (hotlist_file_name, &stat_buf);
1635 hotlist_file_mtime = stat_buf.st_mtime;
1636 hotlist_state.modified = FALSE;
1637 saved = TRUE;
1641 return saved;
1644 /* --------------------------------------------------------------------------------------------- */
1646 * Unload list from memory.
1647 * Don't confuse with hotlist_done() for GUI.
1650 void
1651 done_hotlist (void)
1653 if (hotlist != NULL)
1655 remove_group (hotlist);
1656 g_free (hotlist->label);
1657 g_free (hotlist->directory);
1658 g_free (hotlist);
1659 hotlist = NULL;
1662 hotlist_state.loaded = FALSE;
1664 g_free (hotlist_file_name);
1665 hotlist_file_name = NULL;
1666 l_hotlist = NULL;
1667 current_group = NULL;
1669 if (tkn_buf != NULL)
1671 g_string_free (tkn_buf, TRUE);
1672 tkn_buf = NULL;
1676 /* --------------------------------------------------------------------------------------------- */