Init/deinit clean up
[pantumic.git] / src / hotlist.c
blob45c9503afe2d6b209bc166274ac9e1e5cb52a8e6
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"
51 #include "lib/vfs/mc-vfs/vfs.h"
52 #include "lib/util.h"
53 #include "lib/widget.h"
55 #include "setup.h" /* For profile_bname */
56 #include "midnight.h" /* current_panel */
57 #include "layout.h" /* repaint_screen() */
58 #include "command.h" /* cmdline */
59 #include "history.h"
61 #include "hotlist.h"
63 /*** global variables ****************************************************************************/
65 int hotlist_has_dot_dot = 1;
67 /*** file scope macro definitions ****************************************************************/
69 #define UX 5
70 #define UY 2
72 #define BX UX
73 #define BY (LINES - 6)
75 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
76 #define LABELS 3
77 #define B_ADD_CURRENT B_USER
78 #define B_REMOVE (B_USER + 1)
79 #define B_NEW_GROUP (B_USER + 2)
80 #define B_NEW_ENTRY (B_USER + 3)
81 #define B_UP_GROUP (B_USER + 4)
82 #define B_INSERT (B_USER + 5)
83 #define B_APPEND (B_USER + 6)
84 #define B_MOVE (B_USER + 7)
86 #ifdef ENABLE_VFS
87 #define B_FREE_ALL_VFS (B_USER + 8)
88 #define B_REFRESH_VFS (B_USER + 9)
89 #endif
91 #define new_hotlist() g_new0(struct hotlist, 1)
93 #define CHECK_BUFFER \
94 do \
95 { \
96 size_t i; \
97 i = strlen (current->label); \
98 if (i + 3 > buflen) { \
99 g_free (buf); \
100 buflen = 1024 * (i/1024 + 1); \
101 buf = g_malloc (buflen); \
103 buf[0] = '\0'; \
104 } while (0)
106 #define TKN_GROUP 0
107 #define TKN_ENTRY 1
108 #define TKN_STRING 2
109 #define TKN_URL 3
110 #define TKN_ENDGROUP 4
111 #define TKN_COMMENT 5
112 #define TKN_EOL 125
113 #define TKN_EOF 126
114 #define TKN_UNKNOWN 127
116 #define SKIP_TO_EOL \
118 int _tkn; \
119 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
122 #define CHECK_TOKEN(_TKN_) \
123 tkn = hot_next_token (); \
124 if (tkn != _TKN_) \
126 hotlist_state.readonly = 1; \
127 hotlist_state.file_error = 1; \
128 while (tkn != TKN_EOL && tkn != TKN_EOF) \
129 tkn = hot_next_token (); \
130 break; \
133 /*** file scope type declarations ****************************************************************/
135 enum HotListType
137 HL_TYPE_GROUP,
138 HL_TYPE_ENTRY,
139 HL_TYPE_COMMENT,
140 HL_TYPE_DOTDOT
143 static struct
146 * these parameters are intended to be user configurable
148 int expanded; /* expanded view of all groups at startup */
151 * these reflect run time state
154 int loaded; /* hotlist is loaded */
155 int readonly; /* hotlist readonly */
156 int file_error; /* parse error while reading file */
157 int running; /* we are running dlg (and have to
158 update listbox */
159 int moving; /* we are in moving hotlist currently */
160 int modified; /* hotlist was modified */
161 int type; /* LIST_HOTLIST || LIST_VFSLIST */
162 } hotlist_state;
164 /* Directory hotlist */
165 struct hotlist
167 enum HotListType type;
168 char *directory;
169 char *label;
170 struct hotlist *head;
171 struct hotlist *up;
172 struct hotlist *next;
175 /*** file scope variables ************************************************************************/
177 static WListbox *l_hotlist;
178 static WListbox *l_movelist;
180 static Dlg_head *hotlist_dlg;
181 static Dlg_head *movelist_dlg;
183 static WLabel *pname, *pname_group, *movelist_group;
185 static struct _hotlist_but
187 int ret_cmd, flags, y, x;
188 const char *text;
189 int type;
190 widget_pos_flags_t pos_flags;
191 } hotlist_but[] =
193 /* *INDENT-OFF* */
194 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
195 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"),
196 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
197 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"),
198 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
199 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"),
200 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
201 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &entry"),
202 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
203 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &group"),
204 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
205 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"),
206 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
207 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"),
208 LIST_HOTLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
209 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),
210 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
211 #ifdef ENABLE_VFS
212 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"),
213 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
214 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"),
215 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
216 #endif
217 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &to"),
218 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM }
219 /* *INDENT-ON* */
222 static struct hotlist *hotlist = NULL;
224 static struct hotlist *current_group;
226 static GString *tkn_buf = NULL;
228 static char *hotlist_file_name;
229 static FILE *hotlist_file;
230 static time_t hotlist_file_mtime;
232 static int list_level = 0;
234 /*** file scope functions ************************************************************************/
235 /* --------------------------------------------------------------------------------------------- */
237 static void init_movelist (int, struct hotlist *);
238 static void add_new_group_cmd (void);
239 static void add_new_entry_cmd (void);
240 static void remove_from_hotlist (struct hotlist *entry);
241 static void load_hotlist (void);
242 static void add_dotdot_to_list (void);
244 /* --------------------------------------------------------------------------------------------- */
246 static void
247 hotlist_refresh (Dlg_head * dlg)
249 /* TODO: use groupboxes here */
250 common_dialog_repaint (dlg);
251 tty_setcolor (COLOR_NORMAL);
252 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10), dlg->cols - (UX * 2), TRUE);
253 if (!hotlist_state.moving)
254 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2), TRUE);
257 /* --------------------------------------------------------------------------------------------- */
258 /** If current->data is 0, then we are dealing with a VFS pathname */
260 static void
261 update_path_name (void)
263 const char *text = "";
264 char *p;
265 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
266 Dlg_head *dlg = list->widget.owner;
268 if (list->count != 0)
270 char *ctext = NULL;
271 void *cdata = NULL;
273 listbox_get_current (list, &ctext, &cdata);
274 if (cdata == NULL)
275 text = ctext;
276 else
278 struct hotlist *hlp = (struct hotlist *) cdata;
280 if (hlp->type == HL_TYPE_ENTRY || hlp->type == HL_TYPE_DOTDOT)
281 text = hlp->directory;
282 else if (hlp->type == HL_TYPE_GROUP)
283 text = _("Subgroup - press ENTER to see list");
286 if (!hotlist_state.moving)
287 label_set_text (pname, str_trunc (text, dlg->cols - (UX * 2 + 4)));
289 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
290 if (!hotlist_state.moving)
291 label_set_text (pname_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
292 else
293 label_set_text (movelist_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
294 g_free (p);
296 dlg_redraw (dlg);
299 /* --------------------------------------------------------------------------------------------- */
301 static void
302 fill_listbox (void)
304 struct hotlist *current = current_group->head;
305 GString *buff = g_string_new ("");
307 while (current)
309 switch (current->type)
311 case HL_TYPE_GROUP:
313 /* buff clean up */
314 g_string_truncate (buff, 0);
315 g_string_append (buff, "->");
316 g_string_append (buff, current->label);
317 if (hotlist_state.moving)
318 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
319 else
320 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
322 break;
323 case HL_TYPE_DOTDOT:
324 case HL_TYPE_ENTRY:
325 if (hotlist_state.moving)
326 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, current->label, current);
327 else
328 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, current->label, current);
329 break;
330 default:
331 break;
333 current = current->next;
335 g_string_free (buff, TRUE);
338 /* --------------------------------------------------------------------------------------------- */
340 static void
341 unlink_entry (struct hotlist *entry)
343 struct hotlist *current = current_group->head;
345 if (current == entry)
346 current_group->head = entry->next;
347 else
349 while (current && current->next != entry)
350 current = current->next;
351 if (current)
352 current->next = entry->next;
354 entry->next = entry->up = 0;
357 /* --------------------------------------------------------------------------------------------- */
359 #ifdef ENABLE_VFS
360 static void
361 add_name_to_list (const char *path)
363 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, path, 0);
365 #endif /* !ENABLE_VFS */
367 /* --------------------------------------------------------------------------------------------- */
369 static int
370 hotlist_button_callback (WButton * button, int action)
372 (void) button;
374 switch (action)
376 case B_MOVE:
378 struct hotlist *saved = current_group;
379 struct hotlist *item = NULL;
380 struct hotlist *moveto_item = NULL;
381 struct hotlist *moveto_group = NULL;
382 int ret;
384 if (l_hotlist->count == 0)
385 return MSG_NOT_HANDLED; /* empty group - nothing to do */
387 listbox_get_current (l_hotlist, NULL, (void **) &item);
388 hotlist_state.moving = 1;
389 init_movelist (LIST_MOVELIST, item);
391 ret = run_dlg (movelist_dlg);
393 hotlist_state.moving = 0;
394 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
395 moveto_group = current_group;
396 destroy_dlg (movelist_dlg);
397 current_group = saved;
398 if (ret == B_CANCEL)
399 return MSG_NOT_HANDLED;
400 if (moveto_item == item)
401 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
402 it hardly changes anything ;) */
403 unlink_entry (item);
404 listbox_remove_current (l_hotlist);
405 item->up = moveto_group;
406 if (!moveto_group->head)
407 moveto_group->head = item;
408 else if (!moveto_item)
409 { /* we have group with just comments */
410 struct hotlist *p = moveto_group->head;
412 /* skip comments */
413 while (p->next)
414 p = p->next;
415 p->next = item;
417 else if (ret == B_ENTER || ret == B_APPEND)
418 if (!moveto_item->next)
419 moveto_item->next = item;
420 else
422 item->next = moveto_item->next;
423 moveto_item->next = item;
425 else if (moveto_group->head == moveto_item)
427 moveto_group->head = item;
428 item->next = moveto_item;
430 else
432 struct hotlist *p = moveto_group->head;
434 while (p->next != moveto_item)
435 p = p->next;
436 item->next = p->next;
437 p->next = item;
439 listbox_remove_list (l_hotlist);
440 fill_listbox ();
441 repaint_screen ();
442 hotlist_state.modified = 1;
443 return MSG_NOT_HANDLED;
445 case B_REMOVE:
447 struct hotlist *entry = NULL;
448 listbox_get_current (l_hotlist, NULL, (void **) &entry);
449 remove_from_hotlist (entry);
451 return MSG_NOT_HANDLED;
453 case B_NEW_GROUP:
454 add_new_group_cmd ();
455 return MSG_NOT_HANDLED;
457 case B_ADD_CURRENT:
458 add2hotlist_cmd ();
459 return MSG_NOT_HANDLED;
461 case B_NEW_ENTRY:
462 add_new_entry_cmd ();
463 return MSG_NOT_HANDLED;
465 case B_ENTER:
467 WListbox *list;
468 void *data;
469 struct hotlist *hlp;
471 list = hotlist_state.moving ? l_movelist : l_hotlist;
472 listbox_get_current (list, NULL, &data);
474 if (data == NULL)
475 return MSG_HANDLED;
477 hlp = (struct hotlist *) data;
479 if (hlp->type == HL_TYPE_ENTRY)
480 return MSG_HANDLED;
481 if (hlp->type != HL_TYPE_DOTDOT)
483 listbox_remove_list (list);
484 current_group = hlp;
485 fill_listbox ();
486 return MSG_NOT_HANDLED;
488 /* Fall through - go up */
490 /* Fall through if list empty - just go up */
492 case B_UP_GROUP:
494 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
495 listbox_remove_list (list);
496 current_group = current_group->up;
497 fill_listbox ();
498 return MSG_NOT_HANDLED;
501 #ifdef ENABLE_VFS
502 case B_FREE_ALL_VFS:
503 vfs_expire (1);
504 /* fall through */
506 case B_REFRESH_VFS:
507 listbox_remove_list (l_hotlist);
508 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
509 vfs_fill_names (add_name_to_list);
510 return MSG_NOT_HANDLED;
511 #endif /* ENABLE_VFS */
513 default:
514 return MSG_HANDLED;
518 /* --------------------------------------------------------------------------------------------- */
520 static inline cb_ret_t
521 hotlist_handle_key (Dlg_head * h, int key)
523 switch (key)
525 case KEY_M_CTRL | '\n':
526 goto l1;
528 case '\n':
529 case KEY_ENTER:
530 case KEY_RIGHT:
531 if (hotlist_button_callback (NULL, B_ENTER))
533 h->ret_value = B_ENTER;
534 dlg_stop (h);
536 return MSG_HANDLED;
538 case KEY_LEFT:
539 if (hotlist_state.type != LIST_VFSLIST)
540 return !hotlist_button_callback (NULL, B_UP_GROUP);
541 else
542 return MSG_NOT_HANDLED;
544 case KEY_DC:
545 if (hotlist_state.moving)
546 return MSG_NOT_HANDLED;
547 else
549 hotlist_button_callback (NULL, B_REMOVE);
550 return MSG_HANDLED;
554 case ALT ('\n'):
555 case ALT ('\r'):
556 if (!hotlist_state.moving)
558 void *ldata = NULL;
560 listbox_get_current (l_hotlist, NULL, &ldata);
562 if (ldata != NULL)
564 struct hotlist *hlp = (struct hotlist *) ldata;
566 if (hlp->type == HL_TYPE_ENTRY)
568 char *tmp;
570 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
571 input_insert (cmdline, tmp, FALSE);
572 g_free (tmp);
573 h->ret_value = B_CANCEL;
574 dlg_stop (h);
578 return MSG_HANDLED; /* ignore key */
580 default:
581 return MSG_NOT_HANDLED;
585 /* --------------------------------------------------------------------------------------------- */
587 static cb_ret_t
588 hotlist_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
590 switch (msg)
592 case DLG_DRAW:
593 hotlist_refresh (h);
594 return MSG_HANDLED;
596 case DLG_UNHANDLED_KEY:
597 return hotlist_handle_key (h, parm);
599 case DLG_POST_KEY:
600 if (hotlist_state.moving)
601 dlg_select_widget (l_movelist);
602 else
603 dlg_select_widget (l_hotlist);
604 /* always stay on hotlist */
605 /* fall through */
607 case DLG_INIT:
608 tty_setcolor (MENU_ENTRY_COLOR);
609 update_path_name ();
610 return MSG_HANDLED;
612 case DLG_RESIZE:
613 /* simply call dlg_set_size() with new size */
614 dlg_set_size (h, LINES - 2, COLS - 6);
615 return MSG_HANDLED;
617 default:
618 return default_dlg_callback (h, sender, msg, parm, data);
622 /* --------------------------------------------------------------------------------------------- */
624 static lcback_ret_t
625 l_call (WListbox * list)
627 Dlg_head *dlg = list->widget.owner;
629 if (list->count != 0)
631 void *data = NULL;
633 listbox_get_current (list, NULL, &data);
635 if (data != NULL)
637 struct hotlist *hlp = (struct hotlist *) data;
638 if (hlp->type == HL_TYPE_ENTRY)
640 dlg->ret_value = B_ENTER;
641 dlg_stop (dlg);
642 return LISTBOX_DONE;
644 else
646 hotlist_button_callback (NULL, B_ENTER);
647 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
648 return LISTBOX_CONT;
651 else
653 dlg->ret_value = B_ENTER;
654 dlg_stop (dlg);
655 return LISTBOX_DONE;
659 hotlist_button_callback (NULL, B_UP_GROUP);
660 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
661 return LISTBOX_CONT;
664 /* --------------------------------------------------------------------------------------------- */
666 * Expands all button names (once) and recalculates button positions.
667 * returns number of columns in the dialog box, which is 10 chars longer
668 * then buttonbar.
670 * If common width of the window (i.e. in xterm) is less than returned
671 * width - sorry :) (anyway this did not handled in previous version too)
674 static int
675 init_i18n_stuff (int list_type, int cols)
677 register int i;
678 static const char *cancel_but = N_("&Cancel");
680 #ifdef ENABLE_NLS
681 static int hotlist_i18n_flag = 0;
683 if (!hotlist_i18n_flag)
685 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
686 while (i--)
687 hotlist_but[i].text = _(hotlist_but[i].text);
689 cancel_but = _(cancel_but);
690 hotlist_i18n_flag = 1;
692 #endif /* ENABLE_NLS */
694 /* Dynamic resizing of buttonbars */
696 int len[2], count[2]; /* at most two lines of buttons */
697 int cur_x[2], row;
699 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
700 len[0] = len[1] = count[0] = count[1] = 0;
702 /* Count len of buttonbars, assuming 2 extra space between buttons */
703 while (i--)
705 if (!(hotlist_but[i].type & list_type))
706 continue;
708 row = hotlist_but[i].y;
709 ++count[row];
710 len[row] += str_term_width1 (hotlist_but[i].text) + 5;
711 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
712 len[row] += 2;
714 len[0] -= 2;
715 len[1] -= 2;
717 cols = max (cols, max (len[0], len[1]));
719 /* arrange buttons */
721 cur_x[0] = cur_x[1] = 0;
722 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
723 while (i--)
725 if (!(hotlist_but[i].type & list_type))
726 continue;
728 row = hotlist_but[i].y;
730 if (hotlist_but[i].x != 0)
732 /* not first int the row */
733 if (!strcmp (hotlist_but[i].text, cancel_but))
734 hotlist_but[i].x = cols - str_term_width1 (hotlist_but[i].text) - 13;
735 else
736 hotlist_but[i].x = cur_x[row];
739 cur_x[row] += str_term_width1 (hotlist_but[i].text) + 2
740 + (hotlist_but[i].flags == DEFPUSH_BUTTON ? 5 : 3);
744 return cols;
747 /* --------------------------------------------------------------------------------------------- */
749 static void
750 init_hotlist (int list_type)
752 size_t i;
753 const char *title, *help_node;
754 int hotlist_cols;
756 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
758 do_refresh ();
760 hotlist_state.expanded =
761 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
763 if (list_type == LIST_VFSLIST)
765 title = _("Active VFS directories");
766 help_node = "[vfshot]"; /* FIXME - no such node */
768 else
770 title = _("Directory hotlist");
771 help_node = "[Hotlist]";
774 hotlist_dlg =
775 create_dlg (TRUE, 0, 0, LINES - 2, hotlist_cols, dialog_colors,
776 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
778 for (i = 0; i < BUTTONS; i++)
780 if (hotlist_but[i].type & list_type)
781 add_widget_autopos (hotlist_dlg,
782 button_new (BY + hotlist_but[i].y,
783 BX + hotlist_but[i].x,
784 hotlist_but[i].ret_cmd,
785 hotlist_but[i].flags,
786 hotlist_but[i].text,
787 hotlist_button_callback), hotlist_but[i].pos_flags);
790 /* We add the labels.
791 * pname will hold entry's pathname;
792 * pname_group will hold name of current group
794 pname = label_new (UY - 11 + LINES, UX + 2, "");
795 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
796 if (!hotlist_state.moving)
798 char label_text[BUF_TINY];
800 g_snprintf (label_text, sizeof (label_text), " %s ", _("Directory path"));
801 add_widget_autopos (hotlist_dlg,
802 label_new (UY - 12 + LINES, UX + 2,
803 label_text), WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
805 /* This one holds the displayed pathname */
806 pname_group = label_new (UY, UX + 2, _("Directory label"));
807 add_widget (hotlist_dlg, pname_group);
809 /* get new listbox */
810 l_hotlist = listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8, FALSE, l_call);
812 /* Fill the hotlist with the active VFS or the hotlist */
813 #ifdef ENABLE_VFS
814 if (list_type == LIST_VFSLIST)
816 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, home_dir, 0);
817 vfs_fill_names (add_name_to_list);
819 else
820 #endif /* !ENABLE_VFS */
821 fill_listbox ();
823 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
824 /* add listbox to the dialogs */
827 /* --------------------------------------------------------------------------------------------- */
829 static void
830 init_movelist (int list_type, struct hotlist *item)
832 size_t i;
833 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
834 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
836 do_refresh ();
838 movelist_dlg =
839 create_dlg (TRUE, 0, 0, LINES - 6, movelist_cols, dialog_colors,
840 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
841 g_free (hdr);
843 for (i = 0; i < BUTTONS; i++)
845 if (hotlist_but[i].type & list_type)
846 add_widget (movelist_dlg,
847 button_new (BY - 4 + hotlist_but[i].y,
848 BX + hotlist_but[i].x,
849 hotlist_but[i].ret_cmd,
850 hotlist_but[i].flags,
851 hotlist_but[i].text, hotlist_button_callback));
854 /* We add the labels. We are interested in the last one,
855 * that one will hold the path name label
857 movelist_group = label_new (UY, UX + 2, _("Directory label"));
858 add_widget (movelist_dlg, movelist_group);
859 /* get new listbox */
860 l_movelist =
861 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
862 movelist_dlg->cols - 2 * UX - 2, FALSE, l_call);
864 fill_listbox ();
866 add_widget (movelist_dlg, l_movelist);
867 /* add listbox to the dialogs */
870 /* --------------------------------------------------------------------------------------------- */
872 * Destroy the list dialog.
873 * Don't confuse with done_hotlist() for the list in memory.
876 static void
877 hotlist_done (void)
879 destroy_dlg (hotlist_dlg);
880 l_hotlist = NULL;
881 if (0)
882 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
883 repaint_screen ();
886 /* --------------------------------------------------------------------------------------------- */
888 static inline char *
889 find_group_section (struct hotlist *grp)
891 return g_strconcat (grp->directory, ".Group", (char *) NULL);
894 /* --------------------------------------------------------------------------------------------- */
896 static struct hotlist *
897 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
899 struct hotlist *new;
900 struct hotlist *current = NULL;
903 * Hotlist is neither loaded nor loading.
904 * Must be called by "Ctrl-x a" before using hotlist.
906 if (!current_group)
907 load_hotlist ();
909 listbox_get_current (l_hotlist, NULL, (void **) &current);
911 /* Make sure `..' stays at the top of the list. */
912 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
913 pos = LISTBOX_APPEND_AFTER;
915 new = new_hotlist ();
917 new->type = type;
918 new->label = label;
919 new->directory = directory;
920 new->up = current_group;
922 if (type == HL_TYPE_GROUP)
924 current_group = new;
925 add_dotdot_to_list ();
926 current_group = new->up;
929 if (!current_group->head)
931 /* first element in group */
932 current_group->head = new;
934 else if (pos == LISTBOX_APPEND_AFTER)
936 new->next = current->next;
937 current->next = new;
939 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
941 /* should be inserted before first item */
942 new->next = current;
943 current_group->head = new;
945 else if (pos == LISTBOX_APPEND_BEFORE)
947 struct hotlist *p = current_group->head;
949 while (p->next != current)
950 p = p->next;
952 new->next = current;
953 p->next = new;
955 else
956 { /* append at the end */
957 struct hotlist *p = current_group->head;
959 while (p->next)
960 p = p->next;
962 p->next = new;
965 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
967 if (type == HL_TYPE_GROUP)
969 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
971 listbox_add_item (l_hotlist, pos, 0, lbl, new);
972 g_free (lbl);
974 else
975 listbox_add_item (l_hotlist, pos, 0, new->label, new);
976 listbox_select_entry (l_hotlist, l_hotlist->pos);
978 return new;
982 /* --------------------------------------------------------------------------------------------- */
984 * Support routine for add_new_entry_input()/add_new_group_input()
985 * Change positions of buttons (first three widgets).
987 * This is just a quick hack. Accurate procedure must take care of
988 * internationalized label lengths and total buttonbar length...assume
989 * 64 is longer anyway.
992 #ifdef ENABLE_NLS
993 static void
994 add_widgets_i18n (QuickWidget * qw, int len)
996 int i, l[3], space, cur_x;
998 for (i = 0; i < 3; i++)
1000 qw[i].u.button.text = _(qw[i].u.button.text);
1001 l[i] = str_term_width1 (qw[i].u.button.text) + 3;
1003 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
1005 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
1007 qw[i].relative_x = cur_x;
1008 qw[i].x_divisions = len;
1011 #endif /* ENABLE_NLS */
1013 /* --------------------------------------------------------------------------------------------- */
1015 static int
1016 add_new_entry_input (const char *header, const char *text1, const char *text2,
1017 const char *help, char **r1, char **r2)
1019 #define RELATIVE_Y_BUTTONS 4
1020 #define RELATIVE_Y_LABEL_PTH 3
1021 #define RELATIVE_Y_INPUT_PTH 4
1023 QuickWidget quick_widgets[] = {
1024 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
1025 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
1026 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
1027 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
1028 /* 4 */ QUICK_LABEL (4, 80, 3, 0, text2),
1029 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
1030 /* 6 */ QUICK_LABEL (4, 80, 2, 0, text1),
1031 QUICK_END
1034 int len;
1035 int i;
1036 int lines1, lines2;
1037 int cols1, cols2;
1039 #ifdef ENABLE_NLS
1040 static gboolean i18n_flag = FALSE;
1041 #endif /* ENABLE_NLS */
1043 len = str_term_width1 (header);
1044 str_msg_term_size (text1, &lines1, &cols1);
1045 str_msg_term_size (text2, &lines2, &cols2);
1046 len = max (len, cols1);
1047 len = max (max (len, cols2) + 4, 64);
1049 #ifdef ENABLE_NLS
1050 if (!i18n_flag)
1052 add_widgets_i18n (quick_widgets, len);
1053 i18n_flag = TRUE;
1055 #endif /* ENABLE_NLS */
1058 QuickDialog Quick_input = {
1059 len, lines1 + lines2 + 7, -1, -1, header,
1060 help, quick_widgets, NULL, FALSE
1063 for (i = 0; i < 7; i++)
1064 quick_widgets[i].y_divisions = Quick_input.ylen;
1066 quick_widgets[0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
1067 quick_widgets[1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
1068 quick_widgets[2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
1069 quick_widgets[3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
1070 quick_widgets[4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
1072 i = quick_dialog (&Quick_input);
1075 return (i != B_CANCEL) ? i : 0;
1077 #undef RELATIVE_Y_BUTTONS
1078 #undef RELATIVE_Y_LABEL_PTH
1079 #undef RELATIVE_Y_INPUT_PTH
1082 /* --------------------------------------------------------------------------------------------- */
1084 static void
1085 add_new_entry_cmd (void)
1087 char *title, *url, *to_free;
1088 int ret;
1090 /* Take current directory as default value for input fields */
1091 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
1093 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1094 _("Directory path:"), "[Hotlist]", &title, &url);
1095 g_free (to_free);
1097 if (!ret)
1098 return;
1099 if (!title || !*title || !url || !*url)
1101 g_free (title);
1102 g_free (url);
1103 return;
1106 if (ret == B_ENTER || ret == B_APPEND)
1107 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1108 else
1109 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1111 hotlist_state.modified = 1;
1114 /* --------------------------------------------------------------------------------------------- */
1116 static int
1117 add_new_group_input (const char *header, const char *label, char **result)
1119 QuickWidget quick_widgets[] = {
1120 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
1121 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
1122 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
1123 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result),
1124 /* 4 */ QUICK_LABEL (4, 80, 2, 0, label),
1125 QUICK_END
1128 int len;
1129 int i;
1130 int lines, cols;
1131 int ret;
1133 #ifdef ENABLE_NLS
1134 static gboolean i18n_flag = FALSE;
1135 #endif /* ENABLE_NLS */
1137 len = str_term_width1 (header);
1138 str_msg_term_size (label, &lines, &cols);
1139 len = max (max (len, cols) + 4, 64);
1141 #ifdef ENABLE_NLS
1142 if (!i18n_flag)
1144 add_widgets_i18n (quick_widgets, len);
1145 i18n_flag = TRUE;
1147 #endif /* ENABLE_NLS */
1150 QuickDialog Quick_input = {
1151 len, lines + 6, -1, -1, header,
1152 "[Hotlist]", quick_widgets, NULL, FALSE
1155 int relative_y[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1156 quick_widgets variable above */
1158 for (i = 0; i < 5; i++)
1159 quick_widgets[i].y_divisions = Quick_input.ylen;
1161 for (i = 0; i < 4; i++)
1162 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1164 ret = quick_dialog (&Quick_input);
1167 return (ret != B_CANCEL) ? ret : 0;
1170 /* --------------------------------------------------------------------------------------------- */
1172 static void
1173 add_new_group_cmd (void)
1175 char *label;
1176 int ret;
1178 ret = add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label);
1179 if (!ret || !label || !*label)
1180 return;
1182 if (ret == B_ENTER || ret == B_APPEND)
1183 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1184 else
1185 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1187 hotlist_state.modified = 1;
1190 /* --------------------------------------------------------------------------------------------- */
1192 static void
1193 remove_group (struct hotlist *grp)
1195 struct hotlist *current = grp->head;
1197 while (current)
1199 struct hotlist *next = current->next;
1201 if (current->type == HL_TYPE_GROUP)
1202 remove_group (current);
1204 g_free (current->label);
1205 g_free (current->directory);
1206 g_free (current);
1208 current = next;
1213 /* --------------------------------------------------------------------------------------------- */
1215 static void
1216 remove_from_hotlist (struct hotlist *entry)
1218 if (entry == NULL)
1219 return;
1221 if (entry->type == HL_TYPE_DOTDOT)
1222 return;
1224 if (confirm_directory_hotlist_delete)
1226 char *title;
1227 int result;
1229 title = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1231 if (safe_delete)
1232 query_set_sel (1);
1233 result = query_dialog (title,
1234 _("Are you sure you want to remove this entry?"),
1235 D_ERROR, 2, _("&Yes"), _("&No"));
1237 g_free (title);
1239 if (result != 0)
1240 return;
1243 if (entry->type == HL_TYPE_GROUP)
1245 if (entry->head)
1247 char *header;
1248 int result;
1250 header = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1251 result = query_dialog (header, _("Group not empty.\nRemove it?"),
1252 D_ERROR, 2, _("&Yes"), _("&No"));
1253 g_free (header);
1255 if (result != 0)
1256 return;
1259 remove_group (entry);
1262 unlink_entry (entry);
1264 g_free (entry->label);
1265 g_free (entry->directory);
1266 g_free (entry);
1267 /* now remove list entry from screen */
1268 listbox_remove_current (l_hotlist);
1269 hotlist_state.modified = 1;
1272 /* --------------------------------------------------------------------------------------------- */
1274 static void
1275 load_group (struct hotlist *grp)
1277 gchar **profile_keys, **keys;
1278 gsize len;
1279 char *group_section;
1280 struct hotlist *current = 0;
1282 group_section = find_group_section (grp);
1284 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1286 current_group = grp;
1288 while (*profile_keys)
1290 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1291 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1292 profile_keys++;
1294 g_free (group_section);
1295 g_strfreev (keys);
1297 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1299 while (*profile_keys)
1301 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1302 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1303 profile_keys++;
1305 g_strfreev (keys);
1307 for (current = grp->head; current; current = current->next)
1308 load_group (current);
1311 /* --------------------------------------------------------------------------------------------- */
1313 static int
1314 hot_skip_blanks (void)
1316 int c;
1318 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1320 return c;
1324 /* --------------------------------------------------------------------------------------------- */
1326 static int
1327 hot_next_token (void)
1329 int c, ret = 0;
1330 size_t l;
1333 if (tkn_buf == NULL)
1334 tkn_buf = g_string_new ("");
1335 g_string_set_size (tkn_buf, 0);
1337 again:
1338 c = hot_skip_blanks ();
1339 switch (c)
1341 case EOF:
1342 ret = TKN_EOF;
1343 break;
1344 case '\n':
1345 ret = TKN_EOL;
1346 break;
1347 case '#':
1348 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1350 g_string_append_c (tkn_buf, c);
1352 ret = TKN_COMMENT;
1353 break;
1354 case '"':
1355 while ((c = getc (hotlist_file)) != EOF && c != '"')
1357 if (c == '\\')
1359 c = getc (hotlist_file);
1360 if (c == EOF)
1362 g_string_free (tkn_buf, TRUE);
1363 return TKN_EOF;
1366 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1368 if (c == EOF)
1369 ret = TKN_EOF;
1370 else
1371 ret = TKN_STRING;
1372 break;
1373 case '\\':
1374 c = getc (hotlist_file);
1375 if (c == EOF)
1377 g_string_free (tkn_buf, TRUE);
1378 return TKN_EOF;
1380 if (c == '\n')
1381 goto again;
1383 /* fall through; it is taken as normal character */
1385 default:
1388 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1390 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1391 if (c != EOF)
1392 ungetc (c, hotlist_file);
1393 l = tkn_buf->len;
1394 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1395 ret = TKN_GROUP;
1396 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1397 ret = TKN_ENTRY;
1398 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1399 ret = TKN_ENDGROUP;
1400 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1401 ret = TKN_URL;
1402 else
1403 ret = TKN_UNKNOWN;
1404 break;
1406 return ret;
1409 /* --------------------------------------------------------------------------------------------- */
1411 static void
1412 hot_load_group (struct hotlist *grp)
1414 int tkn;
1415 struct hotlist *new_grp;
1416 char *label, *url;
1418 current_group = grp;
1420 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1421 switch (tkn)
1423 case TKN_GROUP:
1424 CHECK_TOKEN (TKN_STRING);
1425 new_grp =
1426 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1427 SKIP_TO_EOL;
1428 hot_load_group (new_grp);
1429 current_group = grp;
1430 break;
1431 case TKN_ENTRY:
1432 CHECK_TOKEN (TKN_STRING);
1433 label = g_strdup (tkn_buf->str);
1434 CHECK_TOKEN (TKN_URL);
1435 CHECK_TOKEN (TKN_STRING);
1436 url = g_strdup (tkn_buf->str);
1437 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1438 SKIP_TO_EOL;
1439 break;
1440 case TKN_COMMENT:
1441 label = g_strdup (tkn_buf->str);
1442 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1443 break;
1444 case TKN_EOF:
1445 hotlist_state.readonly = 1;
1446 hotlist_state.file_error = 1;
1447 return;
1448 break;
1449 case TKN_EOL:
1450 /* skip empty lines */
1451 break;
1452 default:
1453 hotlist_state.readonly = 1;
1454 hotlist_state.file_error = 1;
1455 SKIP_TO_EOL;
1456 break;
1458 SKIP_TO_EOL;
1461 /* --------------------------------------------------------------------------------------------- */
1463 static void
1464 hot_load_file (struct hotlist *grp)
1466 int tkn;
1467 struct hotlist *new_grp;
1468 char *label, *url;
1470 current_group = grp;
1472 while ((tkn = hot_next_token ()) != TKN_EOF)
1473 switch (tkn)
1475 case TKN_GROUP:
1476 CHECK_TOKEN (TKN_STRING);
1477 new_grp =
1478 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1479 SKIP_TO_EOL;
1480 hot_load_group (new_grp);
1481 current_group = grp;
1482 break;
1483 case TKN_ENTRY:
1484 CHECK_TOKEN (TKN_STRING);
1485 label = g_strdup (tkn_buf->str);
1486 CHECK_TOKEN (TKN_URL);
1487 CHECK_TOKEN (TKN_STRING);
1488 url = g_strdup (tkn_buf->str);
1489 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1490 SKIP_TO_EOL;
1491 break;
1492 case TKN_COMMENT:
1493 label = g_strdup (tkn_buf->str);
1494 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1495 break;
1496 case TKN_EOL:
1497 /* skip empty lines */
1498 break;
1499 default:
1500 hotlist_state.readonly = 1;
1501 hotlist_state.file_error = 1;
1502 SKIP_TO_EOL;
1503 break;
1507 /* --------------------------------------------------------------------------------------------- */
1509 static void
1510 clean_up_hotlist_groups (const char *section)
1512 char *grp_section;
1513 gchar **profile_keys, **keys;
1514 gsize len;
1516 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1517 if (mc_config_has_group (mc_main_config, section))
1518 mc_config_del_group (mc_main_config, section);
1520 if (mc_config_has_group (mc_main_config, grp_section))
1522 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1524 while (*profile_keys)
1526 clean_up_hotlist_groups (*profile_keys);
1527 profile_keys++;
1529 g_strfreev (keys);
1530 mc_config_del_group (mc_main_config, grp_section);
1532 g_free (grp_section);
1535 /* --------------------------------------------------------------------------------------------- */
1537 static void
1538 load_hotlist (void)
1540 int remove_old_list = 0;
1541 struct stat stat_buf;
1543 if (hotlist_state.loaded)
1545 stat (hotlist_file_name, &stat_buf);
1546 if (hotlist_file_mtime < stat_buf.st_mtime)
1547 done_hotlist ();
1548 else
1549 return;
1552 if (!hotlist_file_name)
1553 hotlist_file_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_HOTLIST_FILE, NULL);
1555 hotlist = new_hotlist ();
1556 hotlist->type = HL_TYPE_GROUP;
1557 hotlist->label = g_strdup (_("Top level group"));
1558 hotlist->up = hotlist;
1560 * compatibility :-(
1562 hotlist->directory = g_strdup ("Hotlist");
1564 hotlist_file = fopen (hotlist_file_name, "r");
1565 if (hotlist_file == NULL)
1567 int result;
1569 load_group (hotlist);
1570 hotlist_state.loaded = 1;
1572 * just to be sure we got copy
1574 hotlist_state.modified = 1;
1575 result = save_hotlist ();
1576 hotlist_state.modified = 0;
1577 if (result)
1578 remove_old_list = 1;
1579 else
1580 message (D_ERROR, _("Hotlist Load"),
1582 ("MC was unable to write ~/%s file,\nyour old hotlist entries were not deleted"),
1583 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1585 else
1587 hot_load_file (hotlist);
1588 fclose (hotlist_file);
1589 hotlist_state.loaded = 1;
1592 if (remove_old_list)
1594 GError *error = NULL;
1595 clean_up_hotlist_groups ("Hotlist");
1596 if (!mc_config_save_file (mc_main_config, &error))
1597 setup_save_config_show_error (mc_main_config->ini_path, &error);
1600 stat (hotlist_file_name, &stat_buf);
1601 hotlist_file_mtime = stat_buf.st_mtime;
1602 current_group = hotlist;
1605 /* --------------------------------------------------------------------------------------------- */
1607 static void
1608 hot_save_group (struct hotlist *grp)
1610 struct hotlist *current = grp->head;
1611 int i;
1612 char *s;
1614 #define INDENT(n) \
1615 do { \
1616 for (i = 0; i < n; i++) \
1617 putc (' ', hotlist_file); \
1618 } while (0)
1620 for (; current; current = current->next)
1621 switch (current->type)
1623 case HL_TYPE_GROUP:
1624 INDENT (list_level);
1625 fputs ("GROUP \"", hotlist_file);
1626 for (s = current->label; *s; s++)
1628 if (*s == '"' || *s == '\\')
1629 putc ('\\', hotlist_file);
1630 putc (*s, hotlist_file);
1632 fputs ("\"\n", hotlist_file);
1633 list_level += 2;
1634 hot_save_group (current);
1635 list_level -= 2;
1636 INDENT (list_level);
1637 fputs ("ENDGROUP\n", hotlist_file);
1638 break;
1639 case HL_TYPE_ENTRY:
1640 INDENT (list_level);
1641 fputs ("ENTRY \"", hotlist_file);
1642 for (s = current->label; *s; s++)
1644 if (*s == '"' || *s == '\\')
1645 putc ('\\', hotlist_file);
1646 putc (*s, hotlist_file);
1648 fputs ("\" URL \"", hotlist_file);
1649 for (s = current->directory; *s; s++)
1651 if (*s == '"' || *s == '\\')
1652 putc ('\\', hotlist_file);
1653 putc (*s, hotlist_file);
1655 fputs ("\"\n", hotlist_file);
1656 break;
1657 case HL_TYPE_COMMENT:
1658 fprintf (hotlist_file, "#%s\n", current->label);
1659 break;
1660 case HL_TYPE_DOTDOT:
1661 /* do nothing */
1662 break;
1666 /* --------------------------------------------------------------------------------------------- */
1668 static void
1669 add_dotdot_to_list (void)
1671 if (current_group != hotlist)
1673 if (hotlist_has_dot_dot != 0)
1674 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);
1678 /* --------------------------------------------------------------------------------------------- */
1679 /*** public functions ****************************************************************************/
1680 /* --------------------------------------------------------------------------------------------- */
1682 void
1683 add2hotlist_cmd (void)
1685 char *lc_prompt, *label;
1686 const char *cp = _("Label for \"%s\":");
1687 int l = str_term_width1 (cp);
1688 char *label_string = g_strdup (current_panel->cwd);
1690 strip_password (label_string, 1);
1692 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS - 2 * UX - (l + 8)));
1693 label = input_dialog (_("Add to hotlist"), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1694 g_free (lc_prompt);
1696 if (!label || !*label)
1698 g_free (label_string);
1699 g_free (label);
1700 return;
1702 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1703 hotlist_state.modified = 1;
1706 /* --------------------------------------------------------------------------------------------- */
1708 char *
1709 hotlist_cmd (int vfs_or_hotlist)
1711 char *target = NULL;
1713 hotlist_state.type = vfs_or_hotlist;
1714 load_hotlist ();
1716 init_hotlist (vfs_or_hotlist);
1718 /* display file info */
1719 tty_setcolor (SELECTED_COLOR);
1721 hotlist_state.running = 1;
1722 run_dlg (hotlist_dlg);
1723 hotlist_state.running = 0;
1724 save_hotlist ();
1726 switch (hotlist_dlg->ret_value)
1728 default:
1729 case B_CANCEL:
1730 break;
1732 case B_ENTER:
1734 char *text = NULL;
1735 struct hotlist *hlp = NULL;
1737 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1738 target = g_strdup (hlp != NULL ? hlp->directory : text);
1739 break;
1741 } /* switch */
1743 hotlist_done ();
1744 return target;
1747 /* --------------------------------------------------------------------------------------------- */
1750 save_hotlist (void)
1752 int saved = 0;
1753 struct stat stat_buf;
1755 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name)
1757 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1759 hotlist_file = fopen (hotlist_file_name, "w");
1760 if (hotlist_file != NULL)
1762 hot_save_group (hotlist);
1763 fclose (hotlist_file);
1764 stat (hotlist_file_name, &stat_buf);
1765 hotlist_file_mtime = stat_buf.st_mtime;
1766 saved = 1;
1767 hotlist_state.modified = 0;
1769 else
1770 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1773 return saved;
1776 /* --------------------------------------------------------------------------------------------- */
1778 * Unload list from memory.
1779 * Don't confuse with hotlist_done() for GUI.
1782 void
1783 done_hotlist (void)
1785 if (hotlist)
1787 remove_group (hotlist);
1788 g_free (hotlist->label);
1789 g_free (hotlist->directory);
1790 g_free (hotlist);
1791 hotlist = 0;
1794 hotlist_state.loaded = 0;
1796 g_free (hotlist_file_name);
1797 hotlist_file_name = 0;
1798 l_hotlist = 0;
1799 current_group = 0;
1801 if (tkn_buf)
1803 g_string_free (tkn_buf, TRUE);
1804 tkn_buf = NULL;
1808 /* --------------------------------------------------------------------------------------------- */