Added two macro examples:
[zuwinko.git] / src / filemanager / hotlist.c
blobaba978aee85b4a31ae2dbd951a9f7830951aef3a
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 "src/setup.h" /* For profile_bname */
56 #include "src/history.h"
58 #include "midnight.h" /* current_panel */
59 #include "layout.h" /* repaint_screen() */
60 #include "command.h" /* cmdline */
62 #include "hotlist.h"
64 /*** global variables ****************************************************************************/
66 int hotlist_has_dot_dot = 1;
68 /*** file scope macro definitions ****************************************************************/
70 #define UX 5
71 #define UY 2
73 #define BX UX
74 #define BY (LINES - 6)
76 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
77 #define LABELS 3
78 #define B_ADD_CURRENT B_USER
79 #define B_REMOVE (B_USER + 1)
80 #define B_NEW_GROUP (B_USER + 2)
81 #define B_NEW_ENTRY (B_USER + 3)
82 #define B_UP_GROUP (B_USER + 4)
83 #define B_INSERT (B_USER + 5)
84 #define B_APPEND (B_USER + 6)
85 #define B_MOVE (B_USER + 7)
87 #ifdef ENABLE_VFS
88 #define B_FREE_ALL_VFS (B_USER + 8)
89 #define B_REFRESH_VFS (B_USER + 9)
90 #endif
92 #define new_hotlist() g_new0(struct hotlist, 1)
94 #define CHECK_BUFFER \
95 do \
96 { \
97 size_t i; \
98 i = strlen (current->label); \
99 if (i + 3 > buflen) { \
100 g_free (buf); \
101 buflen = 1024 * (i/1024 + 1); \
102 buf = g_malloc (buflen); \
104 buf[0] = '\0'; \
105 } while (0)
107 #define TKN_GROUP 0
108 #define TKN_ENTRY 1
109 #define TKN_STRING 2
110 #define TKN_URL 3
111 #define TKN_ENDGROUP 4
112 #define TKN_COMMENT 5
113 #define TKN_EOL 125
114 #define TKN_EOF 126
115 #define TKN_UNKNOWN 127
117 #define SKIP_TO_EOL \
119 int _tkn; \
120 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
123 #define CHECK_TOKEN(_TKN_) \
124 tkn = hot_next_token (); \
125 if (tkn != _TKN_) \
127 hotlist_state.readonly = 1; \
128 hotlist_state.file_error = 1; \
129 while (tkn != TKN_EOL && tkn != TKN_EOF) \
130 tkn = hot_next_token (); \
131 break; \
134 /*** file scope type declarations ****************************************************************/
136 enum HotListType
138 HL_TYPE_GROUP,
139 HL_TYPE_ENTRY,
140 HL_TYPE_COMMENT,
141 HL_TYPE_DOTDOT
144 static struct
147 * these parameters are intended to be user configurable
149 int expanded; /* expanded view of all groups at startup */
152 * these reflect run time state
155 int loaded; /* hotlist is loaded */
156 int readonly; /* hotlist readonly */
157 int file_error; /* parse error while reading file */
158 int running; /* we are running dlg (and have to
159 update listbox */
160 int moving; /* we are in moving hotlist currently */
161 int modified; /* hotlist was modified */
162 int type; /* LIST_HOTLIST || LIST_VFSLIST */
163 } hotlist_state;
165 /* Directory hotlist */
166 struct hotlist
168 enum HotListType type;
169 char *directory;
170 char *label;
171 struct hotlist *head;
172 struct hotlist *up;
173 struct hotlist *next;
176 /*** file scope variables ************************************************************************/
178 static WListbox *l_hotlist;
179 static WListbox *l_movelist;
181 static Dlg_head *hotlist_dlg;
182 static Dlg_head *movelist_dlg;
184 static WLabel *pname, *pname_group, *movelist_group;
186 static struct _hotlist_but
188 int ret_cmd, flags, y, x;
189 const char *text;
190 int type;
191 widget_pos_flags_t pos_flags;
192 } hotlist_but[] =
194 /* *INDENT-OFF* */
195 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
196 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"),
197 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
198 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"),
199 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
200 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"),
201 LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
202 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &entry"),
203 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
204 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &group"),
205 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
206 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"),
207 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
208 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"),
209 LIST_HOTLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
210 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),
211 LIST_HOTLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
212 #ifdef ENABLE_VFS
213 { B_REFRESH_VFS, NORMAL_BUTTON, 0, 43, N_("&Refresh"),
214 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
215 { B_FREE_ALL_VFS, NORMAL_BUTTON, 0, 20, N_("Fr&ee VFSs now"),
216 LIST_VFSLIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM },
217 #endif
218 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &to"),
219 LIST_HOTLIST | LIST_VFSLIST | LIST_MOVELIST, WPOS_KEEP_LEFT | WPOS_KEEP_BOTTOM }
220 /* *INDENT-ON* */
223 static struct hotlist *hotlist = NULL;
225 static struct hotlist *current_group;
227 static GString *tkn_buf = NULL;
229 static char *hotlist_file_name;
230 static FILE *hotlist_file;
231 static time_t hotlist_file_mtime;
233 static int list_level = 0;
235 /*** file scope functions ************************************************************************/
236 /* --------------------------------------------------------------------------------------------- */
238 static void init_movelist (int, struct hotlist *);
239 static void add_new_group_cmd (void);
240 static void add_new_entry_cmd (void);
241 static void remove_from_hotlist (struct hotlist *entry);
242 static void load_hotlist (void);
243 static void add_dotdot_to_list (void);
245 /* --------------------------------------------------------------------------------------------- */
247 static void
248 hotlist_refresh (Dlg_head * dlg)
250 /* TODO: use groupboxes here */
251 common_dialog_repaint (dlg);
252 tty_setcolor (COLOR_NORMAL);
253 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10), dlg->cols - (UX * 2), TRUE);
254 if (!hotlist_state.moving)
255 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2), TRUE);
258 /* --------------------------------------------------------------------------------------------- */
259 /** If current->data is 0, then we are dealing with a VFS pathname */
261 static void
262 update_path_name (void)
264 const char *text = "";
265 char *p;
266 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
267 Dlg_head *dlg = list->widget.owner;
269 if (list->count != 0)
271 char *ctext = NULL;
272 void *cdata = NULL;
274 listbox_get_current (list, &ctext, &cdata);
275 if (cdata == NULL)
276 text = ctext;
277 else
279 struct hotlist *hlp = (struct hotlist *) cdata;
281 if (hlp->type == HL_TYPE_ENTRY || hlp->type == HL_TYPE_DOTDOT)
282 text = hlp->directory;
283 else if (hlp->type == HL_TYPE_GROUP)
284 text = _("Subgroup - press ENTER to see list");
287 if (!hotlist_state.moving)
288 label_set_text (pname, str_trunc (text, dlg->cols - (UX * 2 + 4)));
290 p = g_strconcat (" ", current_group->label, " ", (char *) NULL);
291 if (!hotlist_state.moving)
292 label_set_text (pname_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
293 else
294 label_set_text (movelist_group, str_trunc (p, dlg->cols - (UX * 2 + 4)));
295 g_free (p);
297 dlg_redraw (dlg);
300 /* --------------------------------------------------------------------------------------------- */
302 static void
303 fill_listbox (void)
305 struct hotlist *current = current_group->head;
306 GString *buff = g_string_new ("");
308 while (current)
310 switch (current->type)
312 case HL_TYPE_GROUP:
314 /* buff clean up */
315 g_string_truncate (buff, 0);
316 g_string_append (buff, "->");
317 g_string_append (buff, current->label);
318 if (hotlist_state.moving)
319 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
320 else
321 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, buff->str, current);
323 break;
324 case HL_TYPE_DOTDOT:
325 case HL_TYPE_ENTRY:
326 if (hotlist_state.moving)
327 listbox_add_item (l_movelist, LISTBOX_APPEND_AT_END, 0, current->label, current);
328 else
329 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, current->label, current);
330 break;
331 default:
332 break;
334 current = current->next;
336 g_string_free (buff, TRUE);
339 /* --------------------------------------------------------------------------------------------- */
341 static void
342 unlink_entry (struct hotlist *entry)
344 struct hotlist *current = current_group->head;
346 if (current == entry)
347 current_group->head = entry->next;
348 else
350 while (current && current->next != entry)
351 current = current->next;
352 if (current)
353 current->next = entry->next;
355 entry->next = entry->up = 0;
358 /* --------------------------------------------------------------------------------------------- */
360 #ifdef ENABLE_VFS
361 static void
362 add_name_to_list (const char *path)
364 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, path, 0);
366 #endif /* !ENABLE_VFS */
368 /* --------------------------------------------------------------------------------------------- */
370 static int
371 hotlist_button_callback (WButton * button, int action)
373 (void) button;
375 switch (action)
377 case B_MOVE:
379 struct hotlist *saved = current_group;
380 struct hotlist *item = NULL;
381 struct hotlist *moveto_item = NULL;
382 struct hotlist *moveto_group = NULL;
383 int ret;
385 if (l_hotlist->count == 0)
386 return MSG_NOT_HANDLED; /* empty group - nothing to do */
388 listbox_get_current (l_hotlist, NULL, (void **) &item);
389 hotlist_state.moving = 1;
390 init_movelist (LIST_MOVELIST, item);
392 ret = run_dlg (movelist_dlg);
394 hotlist_state.moving = 0;
395 listbox_get_current (l_movelist, NULL, (void **) &moveto_item);
396 moveto_group = current_group;
397 destroy_dlg (movelist_dlg);
398 current_group = saved;
399 if (ret == B_CANCEL)
400 return MSG_NOT_HANDLED;
401 if (moveto_item == item)
402 return MSG_NOT_HANDLED; /* If we insert/append a before/after a
403 it hardly changes anything ;) */
404 unlink_entry (item);
405 listbox_remove_current (l_hotlist);
406 item->up = moveto_group;
407 if (!moveto_group->head)
408 moveto_group->head = item;
409 else if (!moveto_item)
410 { /* we have group with just comments */
411 struct hotlist *p = moveto_group->head;
413 /* skip comments */
414 while (p->next)
415 p = p->next;
416 p->next = item;
418 else if (ret == B_ENTER || ret == B_APPEND)
419 if (!moveto_item->next)
420 moveto_item->next = item;
421 else
423 item->next = moveto_item->next;
424 moveto_item->next = item;
426 else if (moveto_group->head == moveto_item)
428 moveto_group->head = item;
429 item->next = moveto_item;
431 else
433 struct hotlist *p = moveto_group->head;
435 while (p->next != moveto_item)
436 p = p->next;
437 item->next = p->next;
438 p->next = item;
440 listbox_remove_list (l_hotlist);
441 fill_listbox ();
442 repaint_screen ();
443 hotlist_state.modified = 1;
444 return MSG_NOT_HANDLED;
446 case B_REMOVE:
448 struct hotlist *entry = NULL;
449 listbox_get_current (l_hotlist, NULL, (void **) &entry);
450 remove_from_hotlist (entry);
452 return MSG_NOT_HANDLED;
454 case B_NEW_GROUP:
455 add_new_group_cmd ();
456 return MSG_NOT_HANDLED;
458 case B_ADD_CURRENT:
459 add2hotlist_cmd ();
460 return MSG_NOT_HANDLED;
462 case B_NEW_ENTRY:
463 add_new_entry_cmd ();
464 return MSG_NOT_HANDLED;
466 case B_ENTER:
468 WListbox *list;
469 void *data;
470 struct hotlist *hlp;
472 list = hotlist_state.moving ? l_movelist : l_hotlist;
473 listbox_get_current (list, NULL, &data);
475 if (data == NULL)
476 return MSG_HANDLED;
478 hlp = (struct hotlist *) data;
480 if (hlp->type == HL_TYPE_ENTRY)
481 return MSG_HANDLED;
482 if (hlp->type != HL_TYPE_DOTDOT)
484 listbox_remove_list (list);
485 current_group = hlp;
486 fill_listbox ();
487 return MSG_NOT_HANDLED;
489 /* Fall through - go up */
491 /* Fall through if list empty - just go up */
493 case B_UP_GROUP:
495 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
496 listbox_remove_list (list);
497 current_group = current_group->up;
498 fill_listbox ();
499 return MSG_NOT_HANDLED;
502 #ifdef ENABLE_VFS
503 case B_FREE_ALL_VFS:
504 vfs_expire (1);
505 /* fall through */
507 case B_REFRESH_VFS:
508 listbox_remove_list (l_hotlist);
509 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, mc_config_get_home_dir (), 0);
510 vfs_fill_names (add_name_to_list);
511 return MSG_NOT_HANDLED;
512 #endif /* ENABLE_VFS */
514 default:
515 return MSG_HANDLED;
519 /* --------------------------------------------------------------------------------------------- */
521 static inline cb_ret_t
522 hotlist_handle_key (Dlg_head * h, int key)
524 switch (key)
526 case KEY_M_CTRL | '\n':
527 goto l1;
529 case '\n':
530 case KEY_ENTER:
531 case KEY_RIGHT:
532 if (hotlist_button_callback (NULL, B_ENTER))
534 h->ret_value = B_ENTER;
535 dlg_stop (h);
537 return MSG_HANDLED;
539 case KEY_LEFT:
540 if (hotlist_state.type != LIST_VFSLIST)
541 return !hotlist_button_callback (NULL, B_UP_GROUP);
542 else
543 return MSG_NOT_HANDLED;
545 case KEY_DC:
546 if (hotlist_state.moving)
547 return MSG_NOT_HANDLED;
548 else
550 hotlist_button_callback (NULL, B_REMOVE);
551 return MSG_HANDLED;
555 case ALT ('\n'):
556 case ALT ('\r'):
557 if (!hotlist_state.moving)
559 void *ldata = NULL;
561 listbox_get_current (l_hotlist, NULL, &ldata);
563 if (ldata != NULL)
565 struct hotlist *hlp = (struct hotlist *) ldata;
567 if (hlp->type == HL_TYPE_ENTRY)
569 char *tmp;
571 tmp = g_strconcat ("cd ", hlp->directory, (char *) NULL);
572 input_insert (cmdline, tmp, FALSE);
573 g_free (tmp);
574 h->ret_value = B_CANCEL;
575 dlg_stop (h);
579 return MSG_HANDLED; /* ignore key */
581 default:
582 return MSG_NOT_HANDLED;
586 /* --------------------------------------------------------------------------------------------- */
588 static cb_ret_t
589 hotlist_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
591 switch (msg)
593 case DLG_DRAW:
594 hotlist_refresh (h);
595 return MSG_HANDLED;
597 case DLG_UNHANDLED_KEY:
598 return hotlist_handle_key (h, parm);
600 case DLG_POST_KEY:
601 if (hotlist_state.moving)
602 dlg_select_widget (l_movelist);
603 else
604 dlg_select_widget (l_hotlist);
605 /* always stay on hotlist */
606 /* fall through */
608 case DLG_INIT:
609 tty_setcolor (MENU_ENTRY_COLOR);
610 update_path_name ();
611 return MSG_HANDLED;
613 case DLG_RESIZE:
614 /* simply call dlg_set_size() with new size */
615 dlg_set_size (h, LINES - 2, COLS - 6);
616 return MSG_HANDLED;
618 default:
619 return default_dlg_callback (h, sender, msg, parm, data);
623 /* --------------------------------------------------------------------------------------------- */
625 static lcback_ret_t
626 l_call (WListbox * list)
628 Dlg_head *dlg = list->widget.owner;
630 if (list->count != 0)
632 void *data = NULL;
634 listbox_get_current (list, NULL, &data);
636 if (data != NULL)
638 struct hotlist *hlp = (struct hotlist *) data;
639 if (hlp->type == HL_TYPE_ENTRY)
641 dlg->ret_value = B_ENTER;
642 dlg_stop (dlg);
643 return LISTBOX_DONE;
645 else
647 hotlist_button_callback (NULL, B_ENTER);
648 hotlist_callback (dlg, NULL, DLG_POST_KEY, '\n', NULL);
649 return LISTBOX_CONT;
652 else
654 dlg->ret_value = B_ENTER;
655 dlg_stop (dlg);
656 return LISTBOX_DONE;
660 hotlist_button_callback (NULL, B_UP_GROUP);
661 hotlist_callback (dlg, NULL, DLG_POST_KEY, 'u', NULL);
662 return LISTBOX_CONT;
665 /* --------------------------------------------------------------------------------------------- */
667 * Expands all button names (once) and recalculates button positions.
668 * returns number of columns in the dialog box, which is 10 chars longer
669 * then buttonbar.
671 * If common width of the window (i.e. in xterm) is less than returned
672 * width - sorry :) (anyway this did not handled in previous version too)
675 static int
676 init_i18n_stuff (int list_type, int cols)
678 register int i;
679 static const char *cancel_but = N_("&Cancel");
681 #ifdef ENABLE_NLS
682 static int hotlist_i18n_flag = 0;
684 if (!hotlist_i18n_flag)
686 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
687 while (i--)
688 hotlist_but[i].text = _(hotlist_but[i].text);
690 cancel_but = _(cancel_but);
691 hotlist_i18n_flag = 1;
693 #endif /* ENABLE_NLS */
695 /* Dynamic resizing of buttonbars */
697 int len[2], count[2]; /* at most two lines of buttons */
698 int cur_x[2], row;
700 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
701 len[0] = len[1] = count[0] = count[1] = 0;
703 /* Count len of buttonbars, assuming 2 extra space between buttons */
704 while (i--)
706 if (!(hotlist_but[i].type & list_type))
707 continue;
709 row = hotlist_but[i].y;
710 ++count[row];
711 len[row] += str_term_width1 (hotlist_but[i].text) + 5;
712 if (hotlist_but[i].flags == DEFPUSH_BUTTON)
713 len[row] += 2;
715 len[0] -= 2;
716 len[1] -= 2;
718 cols = max (cols, max (len[0], len[1]));
720 /* arrange buttons */
722 cur_x[0] = cur_x[1] = 0;
723 i = sizeof (hotlist_but) / sizeof (hotlist_but[0]);
724 while (i--)
726 if (!(hotlist_but[i].type & list_type))
727 continue;
729 row = hotlist_but[i].y;
731 if (hotlist_but[i].x != 0)
733 /* not first int the row */
734 if (!strcmp (hotlist_but[i].text, cancel_but))
735 hotlist_but[i].x = cols - str_term_width1 (hotlist_but[i].text) - 13;
736 else
737 hotlist_but[i].x = cur_x[row];
740 cur_x[row] += str_term_width1 (hotlist_but[i].text) + 2
741 + (hotlist_but[i].flags == DEFPUSH_BUTTON ? 5 : 3);
745 return cols;
748 /* --------------------------------------------------------------------------------------------- */
750 static void
751 init_hotlist (int list_type)
753 size_t i;
754 const char *title, *help_node;
755 int hotlist_cols;
757 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
759 do_refresh ();
761 hotlist_state.expanded =
762 mc_config_get_int (mc_main_config, "HotlistConfig", "expanded_view_of_groups", 0);
764 if (list_type == LIST_VFSLIST)
766 title = _("Active VFS directories");
767 help_node = "[vfshot]"; /* FIXME - no such node */
769 else
771 title = _("Directory hotlist");
772 help_node = "[Hotlist]";
775 hotlist_dlg =
776 create_dlg (TRUE, 0, 0, LINES - 2, hotlist_cols, dialog_colors,
777 hotlist_callback, help_node, title, DLG_CENTER | DLG_REVERSE);
779 for (i = 0; i < BUTTONS; i++)
781 if (hotlist_but[i].type & list_type)
782 add_widget_autopos (hotlist_dlg,
783 button_new (BY + hotlist_but[i].y,
784 BX + hotlist_but[i].x,
785 hotlist_but[i].ret_cmd,
786 hotlist_but[i].flags,
787 hotlist_but[i].text,
788 hotlist_button_callback), hotlist_but[i].pos_flags);
791 /* We add the labels.
792 * pname will hold entry's pathname;
793 * pname_group will hold name of current group
795 pname = label_new (UY - 11 + LINES, UX + 2, "");
796 add_widget_autopos (hotlist_dlg, pname, WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
797 if (!hotlist_state.moving)
799 char label_text[BUF_TINY];
801 g_snprintf (label_text, sizeof (label_text), " %s ", _("Directory path"));
802 add_widget_autopos (hotlist_dlg,
803 label_new (UY - 12 + LINES, UX + 2,
804 label_text), WPOS_KEEP_BOTTOM | WPOS_KEEP_LEFT);
806 /* This one holds the displayed pathname */
807 pname_group = label_new (UY, UX + 2, _("Directory label"));
808 add_widget (hotlist_dlg, pname_group);
810 /* get new listbox */
811 l_hotlist = listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8, FALSE, l_call);
813 /* Fill the hotlist with the active VFS or the hotlist */
814 #ifdef ENABLE_VFS
815 if (list_type == LIST_VFSLIST)
817 listbox_add_item (l_hotlist, LISTBOX_APPEND_AT_END, 0, mc_config_get_home_dir (), 0);
818 vfs_fill_names (add_name_to_list);
820 else
821 #endif /* !ENABLE_VFS */
822 fill_listbox ();
824 add_widget_autopos (hotlist_dlg, l_hotlist, WPOS_KEEP_ALL);
825 /* add listbox to the dialogs */
828 /* --------------------------------------------------------------------------------------------- */
830 static void
831 init_movelist (int list_type, struct hotlist *item)
833 size_t i;
834 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
835 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
837 do_refresh ();
839 movelist_dlg =
840 create_dlg (TRUE, 0, 0, LINES - 6, movelist_cols, dialog_colors,
841 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER | DLG_REVERSE);
842 g_free (hdr);
844 for (i = 0; i < BUTTONS; i++)
846 if (hotlist_but[i].type & list_type)
847 add_widget (movelist_dlg,
848 button_new (BY - 4 + hotlist_but[i].y,
849 BX + hotlist_but[i].x,
850 hotlist_but[i].ret_cmd,
851 hotlist_but[i].flags,
852 hotlist_but[i].text, hotlist_button_callback));
855 /* We add the labels. We are interested in the last one,
856 * that one will hold the path name label
858 movelist_group = label_new (UY, UX + 2, _("Directory label"));
859 add_widget (movelist_dlg, movelist_group);
860 /* get new listbox */
861 l_movelist =
862 listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8,
863 movelist_dlg->cols - 2 * UX - 2, FALSE, l_call);
865 fill_listbox ();
867 add_widget (movelist_dlg, l_movelist);
868 /* add listbox to the dialogs */
871 /* --------------------------------------------------------------------------------------------- */
873 * Destroy the list dialog.
874 * Don't confuse with done_hotlist() for the list in memory.
877 static void
878 hotlist_done (void)
880 destroy_dlg (hotlist_dlg);
881 l_hotlist = NULL;
882 if (0)
883 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
884 repaint_screen ();
887 /* --------------------------------------------------------------------------------------------- */
889 static inline char *
890 find_group_section (struct hotlist *grp)
892 return g_strconcat (grp->directory, ".Group", (char *) NULL);
895 /* --------------------------------------------------------------------------------------------- */
897 static struct hotlist *
898 add2hotlist (char *label, char *directory, enum HotListType type, listbox_append_t pos)
900 struct hotlist *new;
901 struct hotlist *current = NULL;
904 * Hotlist is neither loaded nor loading.
905 * Must be called by "Ctrl-x a" before using hotlist.
907 if (!current_group)
908 load_hotlist ();
910 listbox_get_current (l_hotlist, NULL, (void **) &current);
912 /* Make sure `..' stays at the top of the list. */
913 if ((current != NULL) && (current->type == HL_TYPE_DOTDOT))
914 pos = LISTBOX_APPEND_AFTER;
916 new = new_hotlist ();
918 new->type = type;
919 new->label = label;
920 new->directory = directory;
921 new->up = current_group;
923 if (type == HL_TYPE_GROUP)
925 current_group = new;
926 add_dotdot_to_list ();
927 current_group = new->up;
930 if (!current_group->head)
932 /* first element in group */
933 current_group->head = new;
935 else if (pos == LISTBOX_APPEND_AFTER)
937 new->next = current->next;
938 current->next = new;
940 else if (pos == LISTBOX_APPEND_BEFORE && current == current_group->head)
942 /* should be inserted before first item */
943 new->next = current;
944 current_group->head = new;
946 else if (pos == LISTBOX_APPEND_BEFORE)
948 struct hotlist *p = current_group->head;
950 while (p->next != current)
951 p = p->next;
953 new->next = current;
954 p->next = new;
956 else
957 { /* append at the end */
958 struct hotlist *p = current_group->head;
960 while (p->next)
961 p = p->next;
963 p->next = new;
966 if (hotlist_state.running && type != HL_TYPE_COMMENT && type != HL_TYPE_DOTDOT)
968 if (type == HL_TYPE_GROUP)
970 char *lbl = g_strconcat ("->", new->label, (char *) NULL);
972 listbox_add_item (l_hotlist, pos, 0, lbl, new);
973 g_free (lbl);
975 else
976 listbox_add_item (l_hotlist, pos, 0, new->label, new);
977 listbox_select_entry (l_hotlist, l_hotlist->pos);
979 return new;
983 /* --------------------------------------------------------------------------------------------- */
985 * Support routine for add_new_entry_input()/add_new_group_input()
986 * Change positions of buttons (first three widgets).
988 * This is just a quick hack. Accurate procedure must take care of
989 * internationalized label lengths and total buttonbar length...assume
990 * 64 is longer anyway.
993 #ifdef ENABLE_NLS
994 static void
995 add_widgets_i18n (QuickWidget * qw, int len)
997 int i, l[3], space, cur_x;
999 for (i = 0; i < 3; i++)
1001 qw[i].u.button.text = _(qw[i].u.button.text);
1002 l[i] = str_term_width1 (qw[i].u.button.text) + 3;
1004 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
1006 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
1008 qw[i].relative_x = cur_x;
1009 qw[i].x_divisions = len;
1012 #endif /* ENABLE_NLS */
1014 /* --------------------------------------------------------------------------------------------- */
1016 static int
1017 add_new_entry_input (const char *header, const char *text1, const char *text2,
1018 const char *help, char **r1, char **r2)
1020 #define RELATIVE_Y_BUTTONS 4
1021 #define RELATIVE_Y_LABEL_PTH 3
1022 #define RELATIVE_Y_INPUT_PTH 4
1024 QuickWidget quick_widgets[] = {
1025 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL),
1026 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), B_INSERT, NULL),
1027 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), B_APPEND, NULL),
1028 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH, 0, *r2, 58, 2, "input-pth", r2),
1029 /* 4 */ QUICK_LABEL (4, 80, 3, 0, text2),
1030 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1, 58, 0, "input-lbl", r1),
1031 /* 6 */ QUICK_LABEL (4, 80, 2, 0, text1),
1032 QUICK_END
1035 int len;
1036 int i;
1037 int lines1, lines2;
1038 int cols1, cols2;
1040 #ifdef ENABLE_NLS
1041 static gboolean i18n_flag = FALSE;
1042 #endif /* ENABLE_NLS */
1044 len = str_term_width1 (header);
1045 str_msg_term_size (text1, &lines1, &cols1);
1046 str_msg_term_size (text2, &lines2, &cols2);
1047 len = max (len, cols1);
1048 len = max (max (len, cols2) + 4, 64);
1050 #ifdef ENABLE_NLS
1051 if (!i18n_flag)
1053 add_widgets_i18n (quick_widgets, len);
1054 i18n_flag = TRUE;
1056 #endif /* ENABLE_NLS */
1059 QuickDialog Quick_input = {
1060 len, lines1 + lines2 + 7, -1, -1, header,
1061 help, quick_widgets, NULL, FALSE
1064 for (i = 0; i < 7; i++)
1065 quick_widgets[i].y_divisions = Quick_input.ylen;
1067 quick_widgets[0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
1068 quick_widgets[1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
1069 quick_widgets[2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
1070 quick_widgets[3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
1071 quick_widgets[4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
1073 i = quick_dialog (&Quick_input);
1076 return (i != B_CANCEL) ? i : 0;
1078 #undef RELATIVE_Y_BUTTONS
1079 #undef RELATIVE_Y_LABEL_PTH
1080 #undef RELATIVE_Y_INPUT_PTH
1083 /* --------------------------------------------------------------------------------------------- */
1085 static void
1086 add_new_entry_cmd (void)
1088 char *title, *url, *to_free;
1089 int ret;
1091 /* Take current directory as default value for input fields */
1092 to_free = title = url = strip_password (g_strdup (current_panel->cwd), 1);
1094 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1095 _("Directory path:"), "[Hotlist]", &title, &url);
1096 g_free (to_free);
1098 if (!ret)
1099 return;
1100 if (!title || !*title || !url || !*url)
1102 g_free (title);
1103 g_free (url);
1104 return;
1107 if (ret == B_ENTER || ret == B_APPEND)
1108 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AFTER);
1109 else
1110 add2hotlist (title, url, HL_TYPE_ENTRY, LISTBOX_APPEND_BEFORE);
1112 hotlist_state.modified = 1;
1115 /* --------------------------------------------------------------------------------------------- */
1117 static int
1118 add_new_group_input (const char *header, const char *label, char **result)
1120 QuickWidget quick_widgets[] = {
1121 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
1122 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT, NULL),
1123 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND, NULL),
1124 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result),
1125 /* 4 */ QUICK_LABEL (4, 80, 2, 0, label),
1126 QUICK_END
1129 int len;
1130 int i;
1131 int lines, cols;
1132 int ret;
1134 #ifdef ENABLE_NLS
1135 static gboolean i18n_flag = FALSE;
1136 #endif /* ENABLE_NLS */
1138 len = str_term_width1 (header);
1139 str_msg_term_size (label, &lines, &cols);
1140 len = max (max (len, cols) + 4, 64);
1142 #ifdef ENABLE_NLS
1143 if (!i18n_flag)
1145 add_widgets_i18n (quick_widgets, len);
1146 i18n_flag = TRUE;
1148 #endif /* ENABLE_NLS */
1151 QuickDialog Quick_input = {
1152 len, lines + 6, -1, -1, header,
1153 "[Hotlist]", quick_widgets, NULL, FALSE
1156 int relative_y[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1157 quick_widgets variable above */
1159 for (i = 0; i < 5; i++)
1160 quick_widgets[i].y_divisions = Quick_input.ylen;
1162 for (i = 0; i < 4; i++)
1163 quick_widgets[i].relative_y = relative_y[i] + 2 + lines;
1165 ret = quick_dialog (&Quick_input);
1168 return (ret != B_CANCEL) ? ret : 0;
1171 /* --------------------------------------------------------------------------------------------- */
1173 static void
1174 add_new_group_cmd (void)
1176 char *label;
1177 int ret;
1179 ret = add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label);
1180 if (!ret || !label || !*label)
1181 return;
1183 if (ret == B_ENTER || ret == B_APPEND)
1184 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_AFTER);
1185 else
1186 add2hotlist (label, 0, HL_TYPE_GROUP, LISTBOX_APPEND_BEFORE);
1188 hotlist_state.modified = 1;
1191 /* --------------------------------------------------------------------------------------------- */
1193 static void
1194 remove_group (struct hotlist *grp)
1196 struct hotlist *current = grp->head;
1198 while (current)
1200 struct hotlist *next = current->next;
1202 if (current->type == HL_TYPE_GROUP)
1203 remove_group (current);
1205 g_free (current->label);
1206 g_free (current->directory);
1207 g_free (current);
1209 current = next;
1214 /* --------------------------------------------------------------------------------------------- */
1216 static void
1217 remove_from_hotlist (struct hotlist *entry)
1219 if (entry == NULL)
1220 return;
1222 if (entry->type == HL_TYPE_DOTDOT)
1223 return;
1225 if (confirm_directory_hotlist_delete)
1227 char *title;
1228 int result;
1230 title = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1232 if (safe_delete)
1233 query_set_sel (1);
1234 result = query_dialog (title,
1235 _("Are you sure you want to remove this entry?"),
1236 D_ERROR, 2, _("&Yes"), _("&No"));
1238 g_free (title);
1240 if (result != 0)
1241 return;
1244 if (entry->type == HL_TYPE_GROUP)
1246 if (entry->head)
1248 char *header;
1249 int result;
1251 header = g_strconcat (_("Remove:"), " ", str_trunc (entry->label, 30), (char *) NULL);
1252 result = query_dialog (header, _("Group not empty.\nRemove it?"),
1253 D_ERROR, 2, _("&Yes"), _("&No"));
1254 g_free (header);
1256 if (result != 0)
1257 return;
1260 remove_group (entry);
1263 unlink_entry (entry);
1265 g_free (entry->label);
1266 g_free (entry->directory);
1267 g_free (entry);
1268 /* now remove list entry from screen */
1269 listbox_remove_current (l_hotlist);
1270 hotlist_state.modified = 1;
1273 /* --------------------------------------------------------------------------------------------- */
1275 static void
1276 load_group (struct hotlist *grp)
1278 gchar **profile_keys, **keys;
1279 gsize len;
1280 char *group_section;
1281 struct hotlist *current = 0;
1283 group_section = find_group_section (grp);
1285 profile_keys = keys = mc_config_get_keys (mc_main_config, group_section, &len);
1287 current_group = grp;
1289 while (*profile_keys)
1291 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1292 g_strdup (*profile_keys), HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1293 profile_keys++;
1295 g_free (group_section);
1296 g_strfreev (keys);
1298 profile_keys = keys = mc_config_get_keys (mc_main_config, grp->directory, &len);
1300 while (*profile_keys)
1302 add2hotlist (mc_config_get_string (mc_main_config, group_section, *profile_keys, ""),
1303 g_strdup (*profile_keys), HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1304 profile_keys++;
1306 g_strfreev (keys);
1308 for (current = grp->head; current; current = current->next)
1309 load_group (current);
1312 /* --------------------------------------------------------------------------------------------- */
1314 static int
1315 hot_skip_blanks (void)
1317 int c;
1319 while ((c = getc (hotlist_file)) != EOF && c != '\n' && g_ascii_isspace (c))
1321 return c;
1325 /* --------------------------------------------------------------------------------------------- */
1327 static int
1328 hot_next_token (void)
1330 int c, ret = 0;
1331 size_t l;
1334 if (tkn_buf == NULL)
1335 tkn_buf = g_string_new ("");
1336 g_string_set_size (tkn_buf, 0);
1338 again:
1339 c = hot_skip_blanks ();
1340 switch (c)
1342 case EOF:
1343 ret = TKN_EOF;
1344 break;
1345 case '\n':
1346 ret = TKN_EOL;
1347 break;
1348 case '#':
1349 while ((c = getc (hotlist_file)) != EOF && c != '\n')
1351 g_string_append_c (tkn_buf, c);
1353 ret = TKN_COMMENT;
1354 break;
1355 case '"':
1356 while ((c = getc (hotlist_file)) != EOF && c != '"')
1358 if (c == '\\')
1360 c = getc (hotlist_file);
1361 if (c == EOF)
1363 g_string_free (tkn_buf, TRUE);
1364 return TKN_EOF;
1367 g_string_append_c (tkn_buf, c == '\n' ? ' ' : c);
1369 if (c == EOF)
1370 ret = TKN_EOF;
1371 else
1372 ret = TKN_STRING;
1373 break;
1374 case '\\':
1375 c = getc (hotlist_file);
1376 if (c == EOF)
1378 g_string_free (tkn_buf, TRUE);
1379 return TKN_EOF;
1381 if (c == '\n')
1382 goto again;
1384 /* fall through; it is taken as normal character */
1386 default:
1389 g_string_append_c (tkn_buf, g_ascii_toupper (c));
1391 while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c)));
1392 if (c != EOF)
1393 ungetc (c, hotlist_file);
1394 l = tkn_buf->len;
1395 if (strncmp (tkn_buf->str, "GROUP", l) == 0)
1396 ret = TKN_GROUP;
1397 else if (strncmp (tkn_buf->str, "ENTRY", l) == 0)
1398 ret = TKN_ENTRY;
1399 else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0)
1400 ret = TKN_ENDGROUP;
1401 else if (strncmp (tkn_buf->str, "URL", l) == 0)
1402 ret = TKN_URL;
1403 else
1404 ret = TKN_UNKNOWN;
1405 break;
1407 return ret;
1410 /* --------------------------------------------------------------------------------------------- */
1412 static void
1413 hot_load_group (struct hotlist *grp)
1415 int tkn;
1416 struct hotlist *new_grp;
1417 char *label, *url;
1419 current_group = grp;
1421 while ((tkn = hot_next_token ()) != TKN_ENDGROUP)
1422 switch (tkn)
1424 case TKN_GROUP:
1425 CHECK_TOKEN (TKN_STRING);
1426 new_grp =
1427 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1428 SKIP_TO_EOL;
1429 hot_load_group (new_grp);
1430 current_group = grp;
1431 break;
1432 case TKN_ENTRY:
1433 CHECK_TOKEN (TKN_STRING);
1434 label = g_strdup (tkn_buf->str);
1435 CHECK_TOKEN (TKN_URL);
1436 CHECK_TOKEN (TKN_STRING);
1437 url = g_strdup (tkn_buf->str);
1438 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1439 SKIP_TO_EOL;
1440 break;
1441 case TKN_COMMENT:
1442 label = g_strdup (tkn_buf->str);
1443 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1444 break;
1445 case TKN_EOF:
1446 hotlist_state.readonly = 1;
1447 hotlist_state.file_error = 1;
1448 return;
1449 break;
1450 case TKN_EOL:
1451 /* skip empty lines */
1452 break;
1453 default:
1454 hotlist_state.readonly = 1;
1455 hotlist_state.file_error = 1;
1456 SKIP_TO_EOL;
1457 break;
1459 SKIP_TO_EOL;
1462 /* --------------------------------------------------------------------------------------------- */
1464 static void
1465 hot_load_file (struct hotlist *grp)
1467 int tkn;
1468 struct hotlist *new_grp;
1469 char *label, *url;
1471 current_group = grp;
1473 while ((tkn = hot_next_token ()) != TKN_EOF)
1474 switch (tkn)
1476 case TKN_GROUP:
1477 CHECK_TOKEN (TKN_STRING);
1478 new_grp =
1479 add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, LISTBOX_APPEND_AT_END);
1480 SKIP_TO_EOL;
1481 hot_load_group (new_grp);
1482 current_group = grp;
1483 break;
1484 case TKN_ENTRY:
1485 CHECK_TOKEN (TKN_STRING);
1486 label = g_strdup (tkn_buf->str);
1487 CHECK_TOKEN (TKN_URL);
1488 CHECK_TOKEN (TKN_STRING);
1489 url = g_strdup (tkn_buf->str);
1490 add2hotlist (label, url, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1491 SKIP_TO_EOL;
1492 break;
1493 case TKN_COMMENT:
1494 label = g_strdup (tkn_buf->str);
1495 add2hotlist (label, 0, HL_TYPE_COMMENT, LISTBOX_APPEND_AT_END);
1496 break;
1497 case TKN_EOL:
1498 /* skip empty lines */
1499 break;
1500 default:
1501 hotlist_state.readonly = 1;
1502 hotlist_state.file_error = 1;
1503 SKIP_TO_EOL;
1504 break;
1508 /* --------------------------------------------------------------------------------------------- */
1510 static void
1511 clean_up_hotlist_groups (const char *section)
1513 char *grp_section;
1514 gchar **profile_keys, **keys;
1515 gsize len;
1517 grp_section = g_strconcat (section, ".Group", (char *) NULL);
1518 if (mc_config_has_group (mc_main_config, section))
1519 mc_config_del_group (mc_main_config, section);
1521 if (mc_config_has_group (mc_main_config, grp_section))
1523 profile_keys = keys = mc_config_get_keys (mc_main_config, grp_section, &len);
1525 while (*profile_keys)
1527 clean_up_hotlist_groups (*profile_keys);
1528 profile_keys++;
1530 g_strfreev (keys);
1531 mc_config_del_group (mc_main_config, grp_section);
1533 g_free (grp_section);
1536 /* --------------------------------------------------------------------------------------------- */
1538 static void
1539 load_hotlist (void)
1541 int remove_old_list = 0;
1542 struct stat stat_buf;
1544 if (hotlist_state.loaded)
1546 stat (hotlist_file_name, &stat_buf);
1547 if (hotlist_file_mtime < stat_buf.st_mtime)
1548 done_hotlist ();
1549 else
1550 return;
1553 if (!hotlist_file_name)
1554 hotlist_file_name = g_build_filename (mc_config_get_path (), MC_HOTLIST_FILE, NULL);
1556 hotlist = new_hotlist ();
1557 hotlist->type = HL_TYPE_GROUP;
1558 hotlist->label = g_strdup (_("Top level group"));
1559 hotlist->up = hotlist;
1561 * compatibility :-(
1563 hotlist->directory = g_strdup ("Hotlist");
1565 hotlist_file = fopen (hotlist_file_name, "r");
1566 if (hotlist_file == NULL)
1568 int result;
1570 load_group (hotlist);
1571 hotlist_state.loaded = 1;
1573 * just to be sure we got copy
1575 hotlist_state.modified = 1;
1576 result = save_hotlist ();
1577 hotlist_state.modified = 0;
1578 if (result)
1579 remove_old_list = 1;
1580 else
1581 message (D_ERROR, _("Hotlist Load"),
1583 ("MC was unable to write ~/%s file,\nyour old hotlist entries were not deleted"),
1584 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE);
1586 else
1588 hot_load_file (hotlist);
1589 fclose (hotlist_file);
1590 hotlist_state.loaded = 1;
1593 if (remove_old_list)
1595 GError *error = NULL;
1596 clean_up_hotlist_groups ("Hotlist");
1597 if (!mc_config_save_file (mc_main_config, &error))
1598 setup_save_config_show_error (mc_main_config->ini_path, &error);
1601 stat (hotlist_file_name, &stat_buf);
1602 hotlist_file_mtime = stat_buf.st_mtime;
1603 current_group = hotlist;
1606 /* --------------------------------------------------------------------------------------------- */
1608 static void
1609 hot_save_group (struct hotlist *grp)
1611 struct hotlist *current = grp->head;
1612 int i;
1613 char *s;
1615 #define INDENT(n) \
1616 do { \
1617 for (i = 0; i < n; i++) \
1618 putc (' ', hotlist_file); \
1619 } while (0)
1621 for (; current; current = current->next)
1622 switch (current->type)
1624 case HL_TYPE_GROUP:
1625 INDENT (list_level);
1626 fputs ("GROUP \"", hotlist_file);
1627 for (s = current->label; *s; s++)
1629 if (*s == '"' || *s == '\\')
1630 putc ('\\', hotlist_file);
1631 putc (*s, hotlist_file);
1633 fputs ("\"\n", hotlist_file);
1634 list_level += 2;
1635 hot_save_group (current);
1636 list_level -= 2;
1637 INDENT (list_level);
1638 fputs ("ENDGROUP\n", hotlist_file);
1639 break;
1640 case HL_TYPE_ENTRY:
1641 INDENT (list_level);
1642 fputs ("ENTRY \"", hotlist_file);
1643 for (s = current->label; *s; s++)
1645 if (*s == '"' || *s == '\\')
1646 putc ('\\', hotlist_file);
1647 putc (*s, hotlist_file);
1649 fputs ("\" URL \"", hotlist_file);
1650 for (s = current->directory; *s; s++)
1652 if (*s == '"' || *s == '\\')
1653 putc ('\\', hotlist_file);
1654 putc (*s, hotlist_file);
1656 fputs ("\"\n", hotlist_file);
1657 break;
1658 case HL_TYPE_COMMENT:
1659 fprintf (hotlist_file, "#%s\n", current->label);
1660 break;
1661 case HL_TYPE_DOTDOT:
1662 /* do nothing */
1663 break;
1667 /* --------------------------------------------------------------------------------------------- */
1669 static void
1670 add_dotdot_to_list (void)
1672 if (current_group != hotlist)
1674 if (hotlist_has_dot_dot != 0)
1675 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT, LISTBOX_APPEND_AT_END);
1679 /* --------------------------------------------------------------------------------------------- */
1680 /*** public functions ****************************************************************************/
1681 /* --------------------------------------------------------------------------------------------- */
1683 void
1684 add2hotlist_cmd (void)
1686 char *lc_prompt, *label;
1687 const char *cp = _("Label for \"%s\":");
1688 int l = str_term_width1 (cp);
1689 char *label_string = g_strdup (current_panel->cwd);
1691 strip_password (label_string, 1);
1693 lc_prompt = g_strdup_printf (cp, path_trunc (current_panel->cwd, COLS - 2 * UX - (l + 8)));
1694 label = input_dialog (_("Add to hotlist"), lc_prompt, MC_HISTORY_HOTLIST_ADD, label_string);
1695 g_free (lc_prompt);
1697 if (!label || !*label)
1699 g_free (label_string);
1700 g_free (label);
1701 return;
1703 add2hotlist (label, label_string, HL_TYPE_ENTRY, LISTBOX_APPEND_AT_END);
1704 hotlist_state.modified = 1;
1707 /* --------------------------------------------------------------------------------------------- */
1709 char *
1710 hotlist_cmd (int vfs_or_hotlist)
1712 char *target = NULL;
1714 hotlist_state.type = vfs_or_hotlist;
1715 load_hotlist ();
1717 init_hotlist (vfs_or_hotlist);
1719 /* display file info */
1720 tty_setcolor (SELECTED_COLOR);
1722 hotlist_state.running = 1;
1723 run_dlg (hotlist_dlg);
1724 hotlist_state.running = 0;
1725 save_hotlist ();
1727 switch (hotlist_dlg->ret_value)
1729 default:
1730 case B_CANCEL:
1731 break;
1733 case B_ENTER:
1735 char *text = NULL;
1736 struct hotlist *hlp = NULL;
1738 listbox_get_current (l_hotlist, &text, (void **) &hlp);
1739 target = g_strdup (hlp != NULL ? hlp->directory : text);
1740 break;
1742 } /* switch */
1744 hotlist_done ();
1745 return target;
1748 /* --------------------------------------------------------------------------------------------- */
1751 save_hotlist (void)
1753 int saved = 0;
1754 struct stat stat_buf;
1756 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name)
1758 mc_util_make_backup_if_possible (hotlist_file_name, ".bak");
1760 hotlist_file = fopen (hotlist_file_name, "w");
1761 if (hotlist_file != NULL)
1763 hot_save_group (hotlist);
1764 fclose (hotlist_file);
1765 stat (hotlist_file_name, &stat_buf);
1766 hotlist_file_mtime = stat_buf.st_mtime;
1767 saved = 1;
1768 hotlist_state.modified = 0;
1770 else
1771 mc_util_restore_from_backup_if_possible (hotlist_file_name, ".bak");
1774 return saved;
1777 /* --------------------------------------------------------------------------------------------- */
1779 * Unload list from memory.
1780 * Don't confuse with hotlist_done() for GUI.
1783 void
1784 done_hotlist (void)
1786 if (hotlist)
1788 remove_group (hotlist);
1789 g_free (hotlist->label);
1790 g_free (hotlist->directory);
1791 g_free (hotlist);
1792 hotlist = 0;
1795 hotlist_state.loaded = 0;
1797 g_free (hotlist_file_name);
1798 hotlist_file_name = 0;
1799 l_hotlist = 0;
1800 current_group = 0;
1802 if (tkn_buf)
1804 g_string_free (tkn_buf, TRUE);
1805 tkn_buf = NULL;
1809 /* --------------------------------------------------------------------------------------------- */