src/*/*.[ch]: fix indentation.
[midnight-commander.git] / src / filemanager / hotlist.c
blob6139edd9d57de0cb0026b2e3d733fda71f5663b2
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 WDialog *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 (WDialog * 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 (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
557 WDialog *h = DIALOG (w);
559 switch (msg)
561 case MSG_UNHANDLED_KEY:
562 return hotlist_handle_key (h, parm);
564 case MSG_POST_KEY:
565 dlg_select_widget (h == hotlist_dlg ? l_hotlist : l_movelist);
566 /* always stay on hotlist */
567 /* fall through */
569 case MSG_INIT:
570 update_path_name ();
571 return MSG_HANDLED;
573 case MSG_RESIZE:
574 /* simply call dlg_set_size() with new size */
575 dlg_set_size (h, LINES - (h == hotlist_dlg ? 2 : 6), COLS - 6);
576 return MSG_HANDLED;
578 default:
579 return dlg_default_callback (w, sender, msg, parm, data);
583 /* --------------------------------------------------------------------------------------------- */
585 static lcback_ret_t
586 hotlist_listbox_callback (WListbox * list)
588 WDialog *dlg = WIDGET (list)->owner;
590 if (list->count != 0)
592 void *data = NULL;
594 listbox_get_current (list, NULL, &data);
596 if (data != NULL)
598 struct hotlist *hlp = (struct hotlist *) data;
600 if (hlp->type == HL_TYPE_ENTRY)
602 dlg->ret_value = B_ENTER;
603 dlg_stop (dlg);
604 return LISTBOX_DONE;
606 else
608 hotlist_button_callback (NULL, B_ENTER);
609 send_message (dlg, NULL, MSG_POST_KEY, '\n', NULL);
610 return LISTBOX_CONT;
613 else
615 dlg->ret_value = B_ENTER;
616 dlg_stop (dlg);
617 return LISTBOX_DONE;
621 hotlist_button_callback (NULL, B_UP_GROUP);
622 send_message (dlg, NULL, MSG_POST_KEY, 'u', NULL);
623 return LISTBOX_CONT;
626 /* --------------------------------------------------------------------------------------------- */
628 * Expands all button names (once) and recalculates button positions.
629 * returns number of columns in the dialog box, which is 10 chars longer
630 * then buttonbar.
632 * If common width of the window (i.e. in xterm) is less than returned
633 * width - sorry :) (anyway this did not handled in previous version too)
636 static int
637 init_i18n_stuff (int list_type, int cols)
639 size_t i;
641 #ifdef ENABLE_NLS
642 static gboolean i18n_flag = FALSE;
644 if (!i18n_flag)
646 for (i = 0; i < hotlist_but_num; i++)
648 hotlist_but[i].text = _(hotlist_but[i].text);
649 hotlist_but[i].len = str_term_width1 (hotlist_but[i].text) + 3;
650 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
651 hotlist_but[i].len += 2;
654 i18n_flag = TRUE;
656 #endif /* ENABLE_NLS */
658 /* Dynamic resizing of buttonbars */
660 int len[2], count[2]; /* at most two lines of buttons */
661 int cur_x[2];
663 len[0] = len[1] = 0;
664 count[0] = count[1] = 0;
665 cur_x[0] = cur_x[1] = 0;
667 /* Count len of buttonbars, assuming 1 extra space between buttons */
668 for (i = 0; i < hotlist_but_num; i++)
669 if ((hotlist_but[i].type & list_type) != 0)
671 int row;
673 row = hotlist_but[i].y;
674 ++count[row];
675 len[row] += hotlist_but[i].len + 1;
678 (len[0])--;
679 (len[1])--;
681 cols = max (cols, max (len[0], len[1]));
683 /* arrange buttons */
684 for (i = 0; i < hotlist_but_num; i++)
685 if ((hotlist_but[i].type & list_type) != 0)
687 int row;
689 row = hotlist_but[i].y;
691 if (hotlist_but[i].x != 0)
693 /* not first int the row */
694 if (hotlist_but[i].ret_cmd == B_CANCEL)
695 hotlist_but[i].x = cols - hotlist_but[i].len - 6;
696 else
697 hotlist_but[i].x = cur_x[row];
700 cur_x[row] += hotlist_but[i].len + 1;
704 return cols;
707 /* --------------------------------------------------------------------------------------------- */
709 static void
710 init_hotlist (hotlist_t list_type)
712 size_t i;
713 const char *title, *help_node;
714 int lines, cols;
715 int y;
716 int dh = 0;
717 WGroupbox *path_box;
718 Widget *hotlist_widget;
720 do_refresh ();
722 lines = LINES - 2;
723 cols = init_i18n_stuff (list_type, COLS - 6);
725 hotlist_state.expanded =
726 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
728 #ifdef ENABLE_VFS
729 if (list_type == LIST_VFSLIST)
731 title = _("Active VFS directories");
732 help_node = "[vfshot]"; /* FIXME - no such node */
733 dh = 1;
735 else
736 #endif /* !ENABLE_VFS */
738 title = _("Directory hotlist");
739 help_node = "[Hotlist]";
742 hotlist_dlg =
743 create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, hotlist_callback, NULL, help_node,
744 title, DLG_CENTER);
746 y = UY;
747 hotlist_group = groupbox_new (y, UX, lines - 10 + dh, cols - 2 * UX, _("Top level group"));
748 hotlist_widget = WIDGET (hotlist_group);
749 add_widget_autopos (hotlist_dlg, hotlist_widget, WPOS_KEEP_ALL, NULL);
751 l_hotlist =
752 listbox_new (y + 1, UX + 1, hotlist_widget->lines - 2, hotlist_widget->cols - 2, FALSE,
753 hotlist_listbox_callback);
755 /* Fill the hotlist with the active VFS or the hotlist */
756 #ifdef ENABLE_VFS
757 if (list_type == LIST_VFSLIST)
759 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, mc_config_get_home_dir (), 0);
760 vfs_fill_names (add_name_to_list);
762 else
763 #endif /* !ENABLE_VFS */
764 fill_listbox (l_hotlist);
766 /* insert before groupbox to view scrollbar */
767 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL, NULL);
769 y += hotlist_widget->lines;
771 path_box = groupbox_new (y, UX, 3, hotlist_widget->cols, _("Directory path"));
772 add_widget_autopos (hotlist_dlg, path_box, WPOS_KEEP_BOTTOM | WPOS_KEEP_HORZ, NULL);
774 pname = label_new (y + 1, UX + 2, "");
775 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT, NULL);
776 y += WIDGET (path_box)->lines;
778 add_widget_autopos (hotlist_dlg, hline_new (y++, -1, -1), WPOS_KEEP_BOTTOM, NULL);
780 for (i = 0; i < hotlist_but_num; i++)
781 if ((hotlist_but[i].type & list_type) != 0)
782 add_widget_autopos (hotlist_dlg,
783 button_new (y + hotlist_but[i].y, UX + hotlist_but[i].x,
784 hotlist_but[i].ret_cmd, hotlist_but[i].flags,
785 hotlist_but[i].text, hotlist_button_callback),
786 hotlist_but[i].pos_flags, NULL);
788 dlg_select_widget (l_hotlist);
791 /* --------------------------------------------------------------------------------------------- */
793 static void
794 init_movelist (struct hotlist *item)
796 size_t i;
797 char *hdr;
798 int lines, cols;
799 int y;
800 Widget *movelist_widget;
802 do_refresh ();
804 lines = LINES - 6;
805 cols = init_i18n_stuff (LIST_MOVELIST, COLS - 6);
807 hdr = g_strdup_printf (_("Moving %s"), item->label);
809 movelist_dlg =
810 create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, hotlist_callback, NULL, "[Hotlist]",
811 hdr, DLG_CENTER);
813 g_free (hdr);
815 y = UY;
816 movelist_group = groupbox_new (y, UX, lines - 7, cols - 2 * UX, _("Directory label"));
817 movelist_widget = WIDGET (movelist_group);
818 add_widget_autopos (movelist_dlg, movelist_widget, WPOS_KEEP_ALL, NULL);
820 l_movelist =
821 listbox_new (y + 1, UX + 1, movelist_widget->lines - 2, movelist_widget->cols - 2, FALSE,
822 hotlist_listbox_callback);
823 fill_listbox (l_movelist);
824 /* insert before groupbox to view scrollbar */
825 add_widget_autopos (movelist_dlg, l_movelist, WPOS_KEEP_ALL, NULL);
827 y += movelist_widget->lines;
829 add_widget_autopos (movelist_dlg, hline_new (y++, -1, -1), WPOS_KEEP_BOTTOM, NULL);
831 for (i = 0; i < hotlist_but_num; i++)
832 if ((hotlist_but[i].type & LIST_MOVELIST) != 0)
833 add_widget_autopos (movelist_dlg,
834 button_new (y + hotlist_but[i].y, UX + hotlist_but[i].x,
835 hotlist_but[i].ret_cmd, hotlist_but[i].flags,
836 hotlist_but[i].text, hotlist_button_callback),
837 hotlist_but[i].pos_flags, NULL);
839 dlg_select_widget (l_movelist);
842 /* --------------------------------------------------------------------------------------------- */
844 * Destroy the list dialog.
845 * Don't confuse with done_hotlist() for the list in memory.
848 static void
849 hotlist_done (void)
851 destroy_dlg (hotlist_dlg);
852 l_hotlist = NULL;
853 if (FALSE)
854 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
855 repaint_screen ();
858 /* --------------------------------------------------------------------------------------------- */
860 static inline char *
861 find_group_section (struct hotlist *grp)
863 return g_strconcat (grp->directory, ".Group", (char *) NULL);
866 /* --------------------------------------------------------------------------------------------- */
868 static struct hotlist *
869 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
871 struct hotlist *new;
872 struct hotlist *current = NULL;
875 * Hotlist is neither loaded nor loading.
876 * Must be called by "Ctrl-x a" before using hotlist.
878 if (current_group == NULL)
879 load_hotlist ();
881 listbox_get_current (l_hotlist, NULL, (void **) &current);
883 /* Make sure `..' stays at the top of the list. */
884 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
885 pos = LISTBOX_APPEND_AFTER;
887 new = g_new0 (struct hotlist, 1);
889 new->type = type;
890 new->label = label;
891 new->directory = directory;
892 new->up = current_group;
894 if (type == HL_TYPE_GROUP)
896 current_group = new;
897 add_dotdot_to_list ();
898 current_group = new->up;
901 if (current_group->head == NULL)
903 /* first element in group */
904 current_group->head = new;
906 else if (pos == LISTBOX_APPEND_AFTER)
908 new->next = current->next;
909 current->next = new;
911 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
913 /* should be inserted before first item */
914 new->next = current;
915 current_group->head = new;
917 else if (pos == LISTBOX_APPEND_BEFORE)
919 struct hotlist *p = current_group->head;
921 while (p->next != current)
922 p = p->next;
924 new->next = current;
925 p->next = new;
927 else
928 { /* append at the end */
929 struct hotlist *p = current_group->head;
931 while (p->next != NULL)
932 p = p->next;
934 p->next = new;
937 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
939 if (type == HL_TYPE_GROUP)
941 char *lbl;
943 lbl = g_strconcat ("->", new->label, (char *) NULL);
944 listbox_add_item (l_hotlist, pos, 0, lbl, new);
945 g_free (lbl);
947 else
948 listbox_add_item (l_hotlist, pos, 0, new->label, new);
949 listbox_select_entry (l_hotlist, l_hotlist->pos);
952 return new;
955 /* --------------------------------------------------------------------------------------------- */
957 static int
958 add_new_entry_input (const char *header, const char *text1, const char *text2,
959 const char *help, char **r1, char **r2)
961 quick_widget_t quick_widgets[] = {
962 /* *INDENT-OFF* */
963 QUICK_LABELED_INPUT (text1, input_label_above, *r1, 0, "input-lbl", r1, NULL),
964 QUICK_SEPARATOR (FALSE),
965 QUICK_LABELED_INPUT (text2, input_label_above, *r2, 0, "input-lbl", r2, NULL),
966 QUICK_START_BUTTONS (TRUE, TRUE),
967 QUICK_BUTTON (N_("&Append"), B_APPEND, NULL, NULL),
968 QUICK_BUTTON (N_("&Insert"), B_INSERT, NULL, NULL),
969 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
970 QUICK_END
971 /* *INDENT-ON* */
974 quick_dialog_t qdlg = {
975 -1, -1, 64,
976 header, help,
977 quick_widgets, NULL, NULL
980 int ret;
982 ret = quick_dialog (&qdlg);
984 return (ret != B_CANCEL) ? ret : 0;
987 /* --------------------------------------------------------------------------------------------- */
989 static void
990 add_new_entry_cmd (void)
992 char *title, *url, *to_free;
993 int ret;
995 /* Take current directory as default value for input fields */
996 to_free = title = url = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
998 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
999 _("Directory path:"), "[Hotlist]", &title, &url);
1000 g_free (to_free);
1002 if (ret == 0)
1003 return;
1004 if (title == NULL || *title == '\0' || url == NULL || *url == '\0')
1006 g_free (title);
1007 g_free (url);
1008 return;
1011 if (ret == B_ENTER || ret == B_APPEND)
1012 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1013 else
1014 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1016 hotlist_state.modified = TRUE;
1019 /* --------------------------------------------------------------------------------------------- */
1021 static int
1022 add_new_group_input (const char *header, const char *label, char **result)
1024 quick_widget_t quick_widgets[] = {
1025 /* *INDENT-OFF* */
1026 QUICK_LABELED_INPUT (label, input_label_above, "", 0, "input", result, NULL),
1027 QUICK_START_BUTTONS (TRUE, TRUE),
1028 QUICK_BUTTON (N_("&Append"), B_APPEND, NULL, NULL),
1029 QUICK_BUTTON (N_("&Insert"), B_INSERT, NULL, NULL),
1030 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
1031 QUICK_END
1032 /* *INDENT-ON* */
1035 quick_dialog_t qdlg = {
1036 -1, -1, 64,
1037 header, "[Hotlist]",
1038 quick_widgets, NULL, NULL
1041 int ret;
1043 ret = quick_dialog (&qdlg);
1045 return (ret != B_CANCEL) ? ret : 0;
1048 /* --------------------------------------------------------------------------------------------- */
1050 static void
1051 add_new_group_cmd (void)
1053 char *label;
1054 int ret;
1056 ret = add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label);
1057 if (ret == 0 || label == NULL || *label == '\0')
1058 return;
1060 if (ret == B_ENTER || ret == B_APPEND)
1061 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1062 else
1063 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1065 hotlist_state.modified = TRUE;
1068 /* --------------------------------------------------------------------------------------------- */
1070 static void
1071 remove_group (struct hotlist *grp)
1073 struct hotlist *current = grp->head;
1075 while (current != NULL)
1077 struct hotlist *next = current->next;
1079 if (current->type == HL_TYPE_GROUP)
1080 remove_group (current);
1082 g_free (current->label);
1083 g_free (current->directory);
1084 g_free (current);
1086 current = next;
1090 /* --------------------------------------------------------------------------------------------- */
1092 static void
1093 remove_from_hotlist (struct hotlist *entry)
1095 if (entry == NULL)
1096 return;
1098 if (entry->type == HL_TYPE_DOTDOT)
1099 return;
1101 if (confirm_directory_hotlist_delete)
1103 char text[BUF_MEDIUM];
1104 int result;
1106 if (safe_delete)
1107 query_set_sel (1);
1109 g_snprintf (text, sizeof (text), _("Are you sure you want to remove entry \"%s\"?"),
1110 str_trunc (entry->label, 30));
1111 result = query_dialog (Q_ ("DialogTitle|Delete"), text, D_ERROR | D_CENTER, 2,
1112 _("&Yes"), _("&No"));
1113 if (result != 0)
1114 return;
1117 if (entry->type == HL_TYPE_GROUP)
1119 struct hotlist *head = entry->head;
1121 if (head != NULL && (head->type != HL_TYPE_DOTDOT || head->next != NULL))
1123 char text[BUF_MEDIUM];
1124 int result;
1126 g_snprintf (text, sizeof (text), _("Group \"%s\" is not empty.\nRemove it?"),
1127 str_trunc (entry->label, 30));
1128 result = query_dialog (Q_ ("DialogTitle|Delete"), text, D_ERROR | D_CENTER, 2,
1129 _("&Yes"), _("&No"));
1130 if (result != 0)
1131 return;
1134 remove_group (entry);
1137 unlink_entry (entry);
1139 g_free (entry->label);
1140 g_free (entry->directory);
1141 g_free (entry);
1142 /* now remove list entry from screen */
1143 listbox_remove_current (l_hotlist);
1144 hotlist_state.modified = TRUE;
1147 /* --------------------------------------------------------------------------------------------- */
1149 static void
1150 load_group (struct hotlist *grp)
1152 gchar **profile_keys, **keys;
1153 gsize len;
1154 char *group_section;
1155 struct hotlist *current = 0;
1157 group_section = find_group_section (grp);
1159 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1161 current_group = grp;
1163 while (*profile_keys != NULL)
1165 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1166 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1167 profile_keys++;
1169 g_free (group_section);
1170 g_strfreev (keys);
1172 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1174 while (*profile_keys != NULL)
1176 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1177 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1178 profile_keys++;
1180 g_strfreev (keys);
1182 for (current = grp->head; current; current = current->next)
1183 load_group (current);
1186 /* --------------------------------------------------------------------------------------------- */
1188 static int
1189 hot_skip_blanks (void)
1191 int c;
1193 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1195 return c;
1198 /* --------------------------------------------------------------------------------------------- */
1200 static int
1201 hot_next_token (void)
1203 int c, ret = 0;
1204 size_t l;
1206 if (tkn_buf == NULL)
1207 tkn_buf = g_string_new ("");
1208 g_string_set_size (tkn_buf, 0);
1210 again:
1211 c = hot_skip_blanks ();
1212 switch (c)
1214 case EOF:
1215 ret = TKN_EOF;
1216 break;
1217 case '\n':
1218 ret = TKN_EOL;
1219 break;
1220 case '#':
1221 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1222 g_string_append_c (tkn_buf, c);
1223 ret = TKN_COMMENT;
1224 break;
1225 case '"':
1226 while ((c = getc (hotlist_file)) != EOF && c != '"')
1228 if (c == '\\')
1230 c = getc (hotlist_file);
1231 if (c == EOF)
1233 g_string_free (tkn_buf, TRUE);
1234 return TKN_EOF;
1237 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1239 ret = (c == EOF) ? TKN_EOF : TKN_STRING;
1240 break;
1241 case '\\':
1242 c = getc (hotlist_file);
1243 if (c == EOF)
1245 g_string_free (tkn_buf, TRUE);
1246 return TKN_EOF;
1248 if (c == '\n')
1249 goto again;
1251 /* fall through; it is taken as normal character */
1253 default:
1256 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1258 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1259 if (c != EOF)
1260 ungetc (c, hotlist_file);
1261 l = tkn_buf->len;
1262 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1263 ret = TKN_GROUP;
1264 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1265 ret = TKN_ENTRY;
1266 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1267 ret = TKN_ENDGROUP;
1268 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1269 ret = TKN_URL;
1270 else
1271 ret = TKN_UNKNOWN;
1272 break;
1274 return ret;
1277 /* --------------------------------------------------------------------------------------------- */
1279 static void
1280 hot_load_group (struct hotlist *grp)
1282 int tkn;
1283 struct hotlist *new_grp;
1284 char *label, *url;
1286 current_group = grp;
1288 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1289 switch (tkn)
1291 case TKN_GROUP:
1292 CHECK_TOKEN (TKN_STRING);
1293 new_grp =
1294 add2hotlist (g_strndup (tkn_buf->str, tkn_buf->len), 0, HL_TYPE_GROUP,
1295 LISTBOX_APPEND_AT_END);
1296 SKIP_TO_EOL;
1297 hot_load_group (new_grp);
1298 current_group = grp;
1299 break;
1300 case TKN_ENTRY:
1302 CHECK_TOKEN (TKN_STRING);
1303 label = g_strndup (tkn_buf->str, tkn_buf->len);
1304 CHECK_TOKEN (TKN_URL);
1305 CHECK_TOKEN (TKN_STRING);
1306 url = tilde_expand (tkn_buf->str);
1307 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1308 SKIP_TO_EOL;
1310 break;
1311 case TKN_COMMENT:
1312 label = g_strndup (tkn_buf->str, tkn_buf->len);
1313 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1314 break;
1315 case TKN_EOF:
1316 hotlist_state.readonly = TRUE;
1317 hotlist_state.file_error = TRUE;
1318 return;
1319 case TKN_EOL:
1320 /* skip empty lines */
1321 break;
1322 default:
1323 hotlist_state.readonly = TRUE;
1324 hotlist_state.file_error = TRUE;
1325 SKIP_TO_EOL;
1326 break;
1328 SKIP_TO_EOL;
1331 /* --------------------------------------------------------------------------------------------- */
1333 static void
1334 hot_load_file (struct hotlist *grp)
1336 int tkn;
1337 struct hotlist *new_grp;
1338 char *label, *url;
1340 current_group = grp;
1342 while ((tkn = hot_next_token ()) != TKN_EOF)
1343 switch (tkn)
1345 case TKN_GROUP:
1346 CHECK_TOKEN (TKN_STRING);
1347 new_grp =
1348 add2hotlist (g_strndup (tkn_buf->str, tkn_buf->len), 0, HL_TYPE_GROUP,
1349 LISTBOX_APPEND_AT_END);
1350 SKIP_TO_EOL;
1351 hot_load_group (new_grp);
1352 current_group = grp;
1353 break;
1354 case TKN_ENTRY:
1356 CHECK_TOKEN (TKN_STRING);
1357 label = g_strndup (tkn_buf->str, tkn_buf->len);
1358 CHECK_TOKEN (TKN_URL);
1359 CHECK_TOKEN (TKN_STRING);
1360 url = tilde_expand (tkn_buf->str);
1361 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1362 SKIP_TO_EOL;
1364 break;
1365 case TKN_COMMENT:
1366 label = g_strndup (tkn_buf->str, tkn_buf->len);
1367 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1368 break;
1369 case TKN_EOL:
1370 /* skip empty lines */
1371 break;
1372 default:
1373 hotlist_state.readonly = TRUE;
1374 hotlist_state.file_error = TRUE;
1375 SKIP_TO_EOL;
1376 break;
1380 /* --------------------------------------------------------------------------------------------- */
1382 static void
1383 clean_up_hotlist_groups (const char *section)
1385 char *grp_section;
1386 gchar **profile_keys, **keys;
1387 gsize len;
1389 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1390 if (mc_config_has_group (mc_main_config, section))
1391 mc_config_del_group (mc_main_config, section);
1393 if (mc_config_has_group (mc_main_config, grp_section))
1395 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1397 while (*profile_keys != NULL)
1399 clean_up_hotlist_groups (*profile_keys);
1400 profile_keys++;
1402 g_strfreev (keys);
1403 mc_config_del_group (mc_main_config, grp_section);
1405 g_free (grp_section);
1408 /* --------------------------------------------------------------------------------------------- */
1410 static void
1411 load_hotlist (void)
1413 gboolean remove_old_list = FALSE;
1414 struct stat stat_buf;
1416 if (hotlist_state.loaded)
1418 stat (hotlist_file_name, &stat_buf);
1419 if (hotlist_file_mtime < stat_buf.st_mtime)
1420 done_hotlist ();
1421 else
1422 return;
1425 if (hotlist_file_name == NULL)
1426 hotlist_file_name = mc_config_get_full_path (MC_HOTLIST_FILE);
1428 hotlist = g_new0 (struct hotlist, 1);
1429 hotlist->type = HL_TYPE_GROUP;
1430 hotlist->label = g_strdup (_("Top level group"));
1431 hotlist->up = hotlist;
1433 * compatibility :-(
1435 hotlist->directory = g_strdup ("Hotlist");
1437 hotlist_file = fopen (hotlist_file_name, "r");
1438 if (hotlist_file == NULL)
1440 int result;
1442 load_group (hotlist);
1443 hotlist_state.loaded = TRUE;
1445 * just to be sure we got copy
1447 hotlist_state.modified = TRUE;
1448 result = save_hotlist ();
1449 hotlist_state.modified = FALSE;
1450 if (result != 0)
1451 remove_old_list = TRUE;
1452 else
1453 message (D_ERROR, _("Hotlist Load"),
1455 ("MC was unable to write %s file,\nyour old hotlist entries were not deleted"),
1456 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1458 else
1460 hot_load_file (hotlist);
1461 fclose (hotlist_file);
1462 hotlist_state.loaded = TRUE;
1465 if (remove_old_list)
1467 GError *error = NULL;
1469 clean_up_hotlist_groups ("Hotlist");
1470 if (!mc_config_save_file (mc_main_config, &error))
1471 setup_save_config_show_error (mc_main_config->ini_path, &error);
1474 stat (hotlist_file_name, &stat_buf);
1475 hotlist_file_mtime = stat_buf.st_mtime;
1476 current_group = hotlist;
1479 /* --------------------------------------------------------------------------------------------- */
1481 static void
1482 hot_save_group (struct hotlist *grp)
1484 struct hotlist *current;
1485 int i;
1486 char *s;
1488 #define INDENT(n) \
1489 do { \
1490 for (i = 0; i < n; i++) \
1491 putc (' ', hotlist_file); \
1492 } while (0)
1494 for (current = grp->head; current != NULL; current = current->next)
1495 switch (current->type)
1497 case HL_TYPE_GROUP:
1498 INDENT (list_level);
1499 fputs ("GROUP \"", hotlist_file);
1500 for (s = current->label; *s != '\0'; s++)
1502 if (*s == '"' || *s == '\\')
1503 putc ('\\', hotlist_file);
1504 putc (*s, hotlist_file);
1506 fputs ("\"\n", hotlist_file);
1507 list_level += 2;
1508 hot_save_group (current);
1509 list_level -= 2;
1510 INDENT (list_level);
1511 fputs ("ENDGROUP\n", hotlist_file);
1512 break;
1513 case HL_TYPE_ENTRY:
1514 INDENT (list_level);
1515 fputs ("ENTRY \"", hotlist_file);
1516 for (s = current->label; *s != '\0'; s++)
1518 if (*s == '"' || *s == '\\')
1519 putc ('\\', hotlist_file);
1520 putc (*s, hotlist_file);
1522 fputs ("\" URL \"", hotlist_file);
1523 for (s = current->directory; *s != '\0'; s++)
1525 if (*s == '"' || *s == '\\')
1526 putc ('\\', hotlist_file);
1527 putc (*s, hotlist_file);
1529 fputs ("\"\n", hotlist_file);
1530 break;
1531 case HL_TYPE_COMMENT:
1532 fprintf (hotlist_file, "#%s\n", current->label);
1533 break;
1534 case HL_TYPE_DOTDOT:
1535 /* do nothing */
1536 break;
1540 /* --------------------------------------------------------------------------------------------- */
1542 static void
1543 add_dotdot_to_list (void)
1545 if (current_group != hotlist && hotlist_has_dot_dot)
1546 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);
1549 /* --------------------------------------------------------------------------------------------- */
1550 /*** public functions ****************************************************************************/
1551 /* --------------------------------------------------------------------------------------------- */
1553 void
1554 add2hotlist_cmd (void)
1556 char *lc_prompt;
1557 const char *cp = N_("Label for \"%s\":");
1558 int l;
1559 char *label_string, *label;
1561 #ifdef ENABLE_NLS
1562 cp = _(cp);
1563 #endif
1565 l = str_term_width1 (cp);
1566 label_string = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
1567 lc_prompt = g_strdup_printf (cp, str_trunc (label_string, COLS - 2 * UX - (l + 8)));
1568 label = input_dialog (_("Add to hotlist"), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1569 g_free (lc_prompt);
1571 if (label == NULL || *label == '\0')
1573 g_free (label_string);
1574 g_free (label);
1576 else
1578 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1579 hotlist_state.modified = TRUE;
1583 /* --------------------------------------------------------------------------------------------- */
1585 char *
1586 hotlist_show (hotlist_t list_type)
1588 char *target = NULL;
1589 int res;
1591 hotlist_state.type = list_type;
1592 load_hotlist ();
1594 init_hotlist (list_type);
1596 /* display file info */
1597 tty_setcolor (SELECTED_COLOR);
1599 hotlist_state.running = TRUE;
1600 res = run_dlg (hotlist_dlg);
1601 hotlist_state.running = FALSE;
1602 save_hotlist ();
1604 if (res == B_ENTER)
1606 char *text = NULL;
1607 struct hotlist *hlp = NULL;
1609 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1610 target = g_strdup (hlp != NULL ? hlp->directory : text);
1613 hotlist_done ();
1614 return target;
1617 /* --------------------------------------------------------------------------------------------- */
1619 gboolean
1620 save_hotlist (void)
1622 gboolean saved = FALSE;
1623 struct stat stat_buf;
1625 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name != NULL)
1627 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1629 hotlist_file = fopen (hotlist_file_name, "w");
1630 if (hotlist_file == NULL)
1631 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1632 else
1634 hot_save_group (hotlist);
1635 fclose (hotlist_file);
1636 stat (hotlist_file_name, &stat_buf);
1637 hotlist_file_mtime = stat_buf.st_mtime;
1638 hotlist_state.modified = FALSE;
1639 saved = TRUE;
1643 return saved;
1646 /* --------------------------------------------------------------------------------------------- */
1648 * Unload list from memory.
1649 * Don't confuse with hotlist_done() for GUI.
1652 void
1653 done_hotlist (void)
1655 if (hotlist != NULL)
1657 remove_group (hotlist);
1658 g_free (hotlist->label);
1659 g_free (hotlist->directory);
1660 g_free (hotlist);
1661 hotlist = NULL;
1664 hotlist_state.loaded = FALSE;
1666 g_free (hotlist_file_name);
1667 hotlist_file_name = NULL;
1668 l_hotlist = NULL;
1669 current_group = NULL;
1671 if (tkn_buf != NULL)
1673 g_string_free (tkn_buf, TRUE);
1674 tkn_buf = NULL;
1678 /* --------------------------------------------------------------------------------------------- */