2 Directory hotlist -- for the Midnight Commander
4 Copyright (C) 1994-2020
5 Free Software Foundation, Inc.
10 Andrej Borsenkow, 1996
12 Andrew Borodin <aborodin@vmail.ru>, 2012, 2013
14 Janne did the original Hotlist code, Andrej made the groupable
15 hotlist; the move hotlist and revamped the file format and made
18 This file is part of the Midnight Commander.
20 The Midnight Commander is free software: you can redistribute it
21 and/or modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation, either version 3 of the License,
23 or (at your option) any later version.
25 The Midnight Commander is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 * \brief Source: directory hotlist
43 #include <sys/types.h>
47 #include "lib/global.h"
49 #include "lib/tty/tty.h" /* COLS */
50 #include "lib/tty/key.h" /* KEY_M_CTRL */
51 #include "lib/skin.h" /* colors */
52 #include "lib/mcconfig.h" /* Load/save directories hotlist */
53 #include "lib/fileloc.h"
54 #include "lib/strutil.h"
55 #include "lib/vfs/vfs.h"
57 #include "lib/widget.h"
59 #include "src/setup.h" /* For profile_bname */
60 #include "src/history.h"
62 #include "midnight.h" /* current_panel */
63 #include "command.h" /* cmdline */
67 /*** global variables ****************************************************************************/
69 /*** file scope macro definitions ****************************************************************/
74 #define B_ADD_CURRENT B_USER
75 #define B_REMOVE (B_USER + 1)
76 #define B_NEW_GROUP (B_USER + 2)
77 #define B_NEW_ENTRY (B_USER + 3)
78 #define B_ENTER_GROUP (B_USER + 4)
79 #define B_UP_GROUP (B_USER + 5)
80 #define B_INSERT (B_USER + 6)
81 #define B_APPEND (B_USER + 7)
82 #define B_MOVE (B_USER + 8)
84 #define B_FREE_ALL_VFS (B_USER + 9)
85 #define B_REFRESH_VFS (B_USER + 10)
92 #define TKN_ENDGROUP 4
96 #define TKN_UNKNOWN 127
101 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
104 #define CHECK_TOKEN(_TKN_) \
105 tkn = hot_next_token (); \
108 hotlist_state.readonly = TRUE; \
109 hotlist_state.file_error = TRUE; \
110 while (tkn != TKN_EOL && tkn != TKN_EOF) \
111 tkn = hot_next_token (); \
115 /*** file scope type declarations ****************************************************************/
128 * these reflect run time state
131 gboolean loaded
; /* hotlist is loaded */
132 gboolean readonly
; /* hotlist readonly */
133 gboolean file_error
; /* parse error while reading file */
134 gboolean running
; /* we are running dlg (and have to
136 gboolean moving
; /* we are in moving hotlist currently */
137 gboolean modified
; /* hotlist was modified */
138 hotlist_t type
; /* LIST_HOTLIST || LIST_VFSLIST */
141 /* Directory hotlist */
144 enum HotListType type
;
147 struct hotlist
*head
;
149 struct hotlist
*next
;
152 /*** file scope variables ************************************************************************/
154 static gboolean hotlist_has_dot_dot
= TRUE
;
156 static WDialog
*hotlist_dlg
, *movelist_dlg
;
157 static WGroupbox
*hotlist_group
, *movelist_group
;
158 static WListbox
*l_hotlist
, *l_movelist
;
159 static WLabel
*pname
;
163 int ret_cmd
, flags
, y
, x
, len
;
166 widget_pos_flags_t pos_flags
;
170 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, 0, N_("Change &to"),
171 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
173 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, 0, N_("&Free VFSs now"),
174 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
175 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, 0, N_("&Refresh"),
176 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
178 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, 0, N_("&Add current"),
179 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
180 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, 0, N_("&Up"),
181 LIST_HOTLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
182 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, 0, N_("&Cancel"),
183 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_RIGHT
| WPOS_KEEP_BOTTOM
},
184 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, 0, N_("New &group"),
185 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
186 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, 0, N_("New &entry"),
187 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
188 { B_INSERT
, NORMAL_BUTTON
, 1, 0, 0, N_("&Insert"),
189 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
190 { B_APPEND
, NORMAL_BUTTON
, 1, 15, 0, N_("A&ppend"),
191 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
192 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, 0, N_("&Remove"),
193 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
194 { B_MOVE
, NORMAL_BUTTON
, 1, 42, 0, N_("&Move"),
195 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
}
199 static const size_t hotlist_but_num
= G_N_ELEMENTS (hotlist_but
);
201 static struct hotlist
*hotlist
= NULL
;
203 static struct hotlist
*current_group
;
205 static GString
*tkn_buf
= NULL
;
207 static char *hotlist_file_name
;
208 static FILE *hotlist_file
;
209 static time_t hotlist_file_mtime
;
211 static int list_level
= 0;
213 /* --------------------------------------------------------------------------------------------- */
214 /*** file scope functions ************************************************************************/
215 /* --------------------------------------------------------------------------------------------- */
217 static void init_movelist (struct hotlist
*item
);
218 static void add_new_group_cmd (void);
219 static void add_new_entry_cmd (void);
220 static void remove_from_hotlist (struct hotlist
*entry
);
221 static void load_hotlist (void);
222 static void add_dotdot_to_list (void);
224 /* --------------------------------------------------------------------------------------------- */
225 /** If current->data is 0, then we are dealing with a VFS pathname */
228 update_path_name (void)
230 const char *text
= "";
232 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
233 Widget
*w
= WIDGET (list
);
235 if (!listbox_is_empty (list
))
240 listbox_get_current (list
, &ctext
, &cdata
);
245 struct hotlist
*hlp
= (struct hotlist
*) cdata
;
247 if (hlp
->type
== HL_TYPE_ENTRY
|| hlp
->type
== HL_TYPE_DOTDOT
)
248 text
= hlp
->directory
;
249 else if (hlp
->type
== HL_TYPE_GROUP
)
250 text
= _("Subgroup - press ENTER to see list");
254 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
255 if (hotlist_state
.moving
)
256 groupbox_set_title (movelist_group
, str_trunc (p
, w
->cols
- 2));
259 groupbox_set_title (hotlist_group
, str_trunc (p
, w
->cols
- 2));
260 label_set_text (pname
, str_trunc (text
, w
->cols
));
265 /* --------------------------------------------------------------------------------------------- */
268 fill_listbox (WListbox
* list
)
270 struct hotlist
*current
;
273 buff
= g_string_new ("");
275 for (current
= current_group
->head
; current
!= NULL
; current
= current
->next
)
276 switch (current
->type
)
281 g_string_truncate (buff
, 0);
282 g_string_append (buff
, "->");
283 g_string_append (buff
, current
->label
);
284 listbox_add_item (list
, LISTBOX_APPEND_AT_END
, 0, buff
->str
, current
, FALSE
);
289 listbox_add_item (list
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
, FALSE
);
294 g_string_free (buff
, TRUE
);
297 /* --------------------------------------------------------------------------------------------- */
300 unlink_entry (struct hotlist
*entry
)
302 struct hotlist
*current
= current_group
->head
;
304 if (current
== entry
)
305 current_group
->head
= entry
->next
;
308 while (current
!= NULL
&& current
->next
!= entry
)
309 current
= current
->next
;
311 current
->next
= entry
->next
;
313 entry
->next
= entry
->up
= NULL
;
316 /* --------------------------------------------------------------------------------------------- */
320 add_name_to_list (const char *path
)
322 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, path
, NULL
, FALSE
);
324 #endif /* !ENABLE_VFS */
326 /* --------------------------------------------------------------------------------------------- */
329 hotlist_run_cmd (int action
)
335 struct hotlist
*saved
= current_group
;
336 struct hotlist
*item
= NULL
;
337 struct hotlist
*moveto_item
= NULL
;
338 struct hotlist
*moveto_group
= NULL
;
341 if (listbox_is_empty (l_hotlist
))
342 return 0; /* empty group - nothing to do */
344 listbox_get_current (l_hotlist
, NULL
, (void **) &item
);
345 init_movelist (item
);
346 hotlist_state
.moving
= TRUE
;
347 ret
= dlg_run (movelist_dlg
);
348 hotlist_state
.moving
= FALSE
;
349 listbox_get_current (l_movelist
, NULL
, (void **) &moveto_item
);
350 moveto_group
= current_group
;
351 dlg_destroy (movelist_dlg
);
352 current_group
= saved
;
355 if (moveto_item
== item
)
356 return 0; /* If we insert/append a before/after a
357 it hardly changes anything ;) */
359 listbox_remove_current (l_hotlist
);
360 item
->up
= moveto_group
;
361 if (moveto_group
->head
== NULL
)
362 moveto_group
->head
= item
;
363 else if (moveto_item
== NULL
)
364 { /* we have group with just comments */
365 struct hotlist
*p
= moveto_group
->head
;
368 while (p
->next
!= NULL
)
372 else if (ret
== B_ENTER
|| ret
== B_APPEND
)
374 if (moveto_item
->next
== NULL
)
375 moveto_item
->next
= item
;
378 item
->next
= moveto_item
->next
;
379 moveto_item
->next
= item
;
382 else if (moveto_group
->head
== moveto_item
)
384 moveto_group
->head
= item
;
385 item
->next
= moveto_item
;
389 struct hotlist
*p
= moveto_group
->head
;
391 while (p
->next
!= moveto_item
)
393 item
->next
= p
->next
;
396 listbox_remove_list (l_hotlist
);
397 fill_listbox (l_hotlist
);
399 hotlist_state
.modified
= TRUE
;
404 struct hotlist
*entry
= NULL
;
406 listbox_get_current (l_hotlist
, NULL
, (void **) &entry
);
407 remove_from_hotlist (entry
);
412 add_new_group_cmd ();
420 add_new_entry_cmd ();
430 list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
431 listbox_get_current (list
, NULL
, &data
);
436 hlp
= (struct hotlist
*) data
;
438 if (hlp
->type
== HL_TYPE_ENTRY
)
439 return (action
== B_ENTER
? 1 : 0);
440 if (hlp
->type
!= HL_TYPE_DOTDOT
)
442 listbox_remove_list (list
);
447 MC_FALLTHROUGH
; /* go up */
449 MC_FALLTHROUGH
; /* if list empty - just go up */
453 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
455 listbox_remove_list (list
);
456 current_group
= current_group
->up
;
467 listbox_remove_list (l_hotlist
);
468 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), NULL
,
470 vfs_fill_names (add_name_to_list
);
472 #endif /* ENABLE_VFS */
479 /* --------------------------------------------------------------------------------------------- */
482 hotlist_button_callback (WButton
* button
, int action
)
487 ret
= hotlist_run_cmd (action
);
492 /* --------------------------------------------------------------------------------------------- */
494 static inline cb_ret_t
495 hotlist_handle_key (WDialog
* h
, int key
)
499 case KEY_M_CTRL
| '\n':
504 if (hotlist_button_callback (NULL
, B_ENTER
) != 0)
506 h
->ret_value
= B_ENTER
;
512 /* enter to the group */
513 if (hotlist_state
.type
== LIST_VFSLIST
)
514 return MSG_NOT_HANDLED
;
515 return hotlist_button_callback (NULL
, B_ENTER_GROUP
) == 0 ? MSG_HANDLED
: MSG_NOT_HANDLED
;
518 /* leave the group */
519 if (hotlist_state
.type
== LIST_VFSLIST
)
520 return MSG_NOT_HANDLED
;
521 return hotlist_button_callback (NULL
, B_UP_GROUP
) == 0 ? MSG_HANDLED
: MSG_NOT_HANDLED
;
524 if (hotlist_state
.moving
)
525 return MSG_NOT_HANDLED
;
526 hotlist_button_callback (NULL
, B_REMOVE
);
532 if (!hotlist_state
.moving
)
536 listbox_get_current (l_hotlist
, NULL
, &ldata
);
540 struct hotlist
*hlp
= (struct hotlist
*) ldata
;
542 if (hlp
->type
== HL_TYPE_ENTRY
)
546 tmp
= g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
547 input_insert (cmdline
, tmp
, FALSE
);
549 h
->ret_value
= B_CANCEL
;
554 return MSG_HANDLED
; /* ignore key */
557 return MSG_NOT_HANDLED
;
561 /* --------------------------------------------------------------------------------------------- */
564 hotlist_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
566 WDialog
*h
= DIALOG (w
);
571 case MSG_NOTIFY
: /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
575 case MSG_UNHANDLED_KEY
:
576 return hotlist_handle_key (h
, parm
);
580 * The code here has two purposes:
582 * (1) Always stay on the hotlist.
584 * Activating a button using its hotkey (and even pressing ENTER, as
585 * there's a "default button") moves the focus to the button. But we
586 * want to stay on the hotlist, to be able to use the usual keys (up,
587 * down, etc.). So we do `widget_select (lst)`.
589 * (2) Refresh the hotlist.
591 * We may have run a command that changed the contents of the list.
592 * We therefore need to refresh it. So we do `widget_draw (lst)`.
597 lst
= WIDGET (h
== hotlist_dlg
? l_hotlist
: l_movelist
);
599 /* widget_select() already redraws the widget, but since it's a
600 * no-op if the widget is already selected ("focused"), we have
601 * to call widget_draw() separately. */
602 if (!widget_get_state (lst
, WST_FOCUSED
))
610 /* simply call dlg_set_size() with new size */
611 dlg_set_size (h
, LINES
- (h
== hotlist_dlg
? 2 : 6), COLS
- 6);
615 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
619 /* --------------------------------------------------------------------------------------------- */
622 hotlist_listbox_callback (WListbox
* list
)
624 WDialog
*dlg
= DIALOG (WIDGET (list
)->owner
);
626 if (!listbox_is_empty (list
))
630 listbox_get_current (list
, NULL
, &data
);
634 struct hotlist
*hlp
= (struct hotlist
*) data
;
636 if (hlp
->type
== HL_TYPE_ENTRY
)
638 dlg
->ret_value
= B_ENTER
;
644 hotlist_button_callback (NULL
, B_ENTER
);
645 send_message (dlg
, NULL
, MSG_POST_KEY
, '\n', NULL
);
651 dlg
->ret_value
= B_ENTER
;
657 hotlist_button_callback (NULL
, B_UP_GROUP
);
658 send_message (dlg
, NULL
, MSG_POST_KEY
, 'u', NULL
);
662 /* --------------------------------------------------------------------------------------------- */
664 * Expands all button names (once) and recalculates button positions.
665 * returns number of columns in the dialog box, which is 10 chars longer
668 * If common width of the window (i.e. in xterm) is less than returned
669 * width - sorry :) (anyway this did not handled in previous version too)
673 init_i18n_stuff (int list_type
, int cols
)
677 static gboolean i18n_flag
= FALSE
;
681 for (i
= 0; i
< hotlist_but_num
; i
++)
684 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
685 #endif /* ENABLE_NLS */
686 hotlist_but
[i
].len
= str_term_width1 (hotlist_but
[i
].text
) + 3;
687 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
688 hotlist_but
[i
].len
+= 2;
694 /* Dynamic resizing of buttonbars */
696 int len
[2], count
[2]; /* at most two lines of buttons */
700 count
[0] = count
[1] = 0;
701 cur_x
[0] = cur_x
[1] = 0;
703 /* Count len of buttonbars, assuming 1 extra space between buttons */
704 for (i
= 0; i
< hotlist_but_num
; i
++)
705 if ((hotlist_but
[i
].type
& list_type
) != 0)
709 row
= hotlist_but
[i
].y
;
711 len
[row
] += hotlist_but
[i
].len
+ 1;
717 cols
= MAX (cols
, MAX (len
[0], len
[1]));
719 /* arrange buttons */
720 for (i
= 0; i
< hotlist_but_num
; i
++)
721 if ((hotlist_but
[i
].type
& list_type
) != 0)
725 row
= hotlist_but
[i
].y
;
727 if (hotlist_but
[i
].x
!= 0)
729 /* not first int the row */
730 if (hotlist_but
[i
].ret_cmd
== B_CANCEL
)
731 hotlist_but
[i
].x
= cols
- hotlist_but
[i
].len
- 6;
733 hotlist_but
[i
].x
= cur_x
[row
];
736 cur_x
[row
] += hotlist_but
[i
].len
+ 1;
743 /* --------------------------------------------------------------------------------------------- */
746 init_hotlist (hotlist_t list_type
)
749 const char *title
, *help_node
;
755 Widget
*hotlist_widget
;
760 cols
= init_i18n_stuff (list_type
, COLS
- 6);
763 if (list_type
== LIST_VFSLIST
)
765 title
= _("Active VFS directories");
766 help_node
= "[vfshot]"; /* FIXME - no such node */
770 #endif /* !ENABLE_VFS */
772 title
= _("Directory hotlist");
773 help_node
= "[Hotlist]";
777 dlg_create (TRUE
, 0, 0, lines
, cols
, WPOS_CENTER
, FALSE
, dialog_colors
, hotlist_callback
,
778 NULL
, help_node
, title
);
779 g
= GROUP (hotlist_dlg
);
782 hotlist_group
= groupbox_new (y
, UX
, lines
- 10 + dh
, cols
- 2 * UX
, _("Top level group"));
783 hotlist_widget
= WIDGET (hotlist_group
);
784 group_add_widget_autopos (g
, hotlist_widget
, WPOS_KEEP_ALL
, NULL
);
787 listbox_new (y
+ 1, UX
+ 1, hotlist_widget
->lines
- 2, hotlist_widget
->cols
- 2, FALSE
,
788 hotlist_listbox_callback
);
790 /* Fill the hotlist with the active VFS or the hotlist */
792 if (list_type
== LIST_VFSLIST
)
794 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), NULL
,
796 vfs_fill_names (add_name_to_list
);
799 #endif /* !ENABLE_VFS */
800 fill_listbox (l_hotlist
);
802 /* insert before groupbox to view scrollbar */
803 group_add_widget_autopos (g
, l_hotlist
, WPOS_KEEP_ALL
, NULL
);
805 y
+= hotlist_widget
->lines
;
807 path_box
= groupbox_new (y
, UX
, 3, hotlist_widget
->cols
, _("Directory path"));
808 group_add_widget_autopos (g
, path_box
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_HORZ
, NULL
);
810 pname
= label_new (y
+ 1, UX
+ 2, "");
811 group_add_widget_autopos (g
, pname
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
, NULL
);
812 y
+= WIDGET (path_box
)->lines
;
814 group_add_widget_autopos (g
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
816 for (i
= 0; i
< hotlist_but_num
; i
++)
817 if ((hotlist_but
[i
].type
& list_type
) != 0)
818 group_add_widget_autopos (g
,
819 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
820 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
821 hotlist_but
[i
].text
, hotlist_button_callback
),
822 hotlist_but
[i
].pos_flags
, NULL
);
824 widget_select (WIDGET (l_hotlist
));
827 /* --------------------------------------------------------------------------------------------- */
830 init_movelist (struct hotlist
*item
)
837 Widget
*movelist_widget
;
842 cols
= init_i18n_stuff (LIST_MOVELIST
, COLS
- 6);
844 hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
847 dlg_create (TRUE
, 0, 0, lines
, cols
, WPOS_CENTER
, FALSE
, dialog_colors
, hotlist_callback
,
848 NULL
, "[Hotlist]", hdr
);
849 g
= GROUP (movelist_dlg
);
854 movelist_group
= groupbox_new (y
, UX
, lines
- 7, cols
- 2 * UX
, _("Directory label"));
855 movelist_widget
= WIDGET (movelist_group
);
856 group_add_widget_autopos (g
, movelist_widget
, WPOS_KEEP_ALL
, NULL
);
859 listbox_new (y
+ 1, UX
+ 1, movelist_widget
->lines
- 2, movelist_widget
->cols
- 2, FALSE
,
860 hotlist_listbox_callback
);
861 fill_listbox (l_movelist
);
862 /* insert before groupbox to view scrollbar */
863 group_add_widget_autopos (g
, l_movelist
, WPOS_KEEP_ALL
, NULL
);
865 y
+= movelist_widget
->lines
;
867 group_add_widget_autopos (g
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
869 for (i
= 0; i
< hotlist_but_num
; i
++)
870 if ((hotlist_but
[i
].type
& LIST_MOVELIST
) != 0)
871 group_add_widget_autopos (g
,
872 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
873 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
874 hotlist_but
[i
].text
, hotlist_button_callback
),
875 hotlist_but
[i
].pos_flags
, NULL
);
877 widget_select (WIDGET (l_movelist
));
880 /* --------------------------------------------------------------------------------------------- */
882 * Destroy the list dialog.
883 * Don't confuse with done_hotlist() for the list in memory.
889 dlg_destroy (hotlist_dlg
);
892 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
897 /* --------------------------------------------------------------------------------------------- */
900 find_group_section (struct hotlist
*grp
)
902 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
905 /* --------------------------------------------------------------------------------------------- */
907 static struct hotlist
*
908 add2hotlist (char *label
, char *directory
, enum HotListType type
, listbox_append_t pos
)
911 struct hotlist
*current
= NULL
;
914 * Hotlist is neither loaded nor loading.
915 * Must be called by "Ctrl-x a" before using hotlist.
917 if (current_group
== NULL
)
920 listbox_get_current (l_hotlist
, NULL
, (void **) ¤t
);
922 /* Make sure '..' stays at the top of the list. */
923 if ((current
!= NULL
) && (current
->type
== HL_TYPE_DOTDOT
))
924 pos
= LISTBOX_APPEND_AFTER
;
926 new = g_new0 (struct hotlist
, 1);
930 new->directory
= directory
;
931 new->up
= current_group
;
933 if (type
== HL_TYPE_GROUP
)
936 add_dotdot_to_list ();
937 current_group
= new->up
;
940 if (current_group
->head
== NULL
)
942 /* first element in group */
943 current_group
->head
= new;
945 else if (pos
== LISTBOX_APPEND_AFTER
)
947 new->next
= current
->next
;
950 else if (pos
== LISTBOX_APPEND_BEFORE
&& current
== current_group
->head
)
952 /* should be inserted before first item */
954 current_group
->head
= new;
956 else if (pos
== LISTBOX_APPEND_BEFORE
)
958 struct hotlist
*p
= current_group
->head
;
960 while (p
->next
!= current
)
967 { /* append at the end */
968 struct hotlist
*p
= current_group
->head
;
970 while (p
->next
!= NULL
)
976 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&& type
!= HL_TYPE_DOTDOT
)
978 if (type
== HL_TYPE_GROUP
)
982 lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
983 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new, FALSE
);
987 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new, FALSE
);
988 listbox_select_entry (l_hotlist
, l_hotlist
->pos
);
994 /* --------------------------------------------------------------------------------------------- */
997 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
998 const char *help
, char **r1
, char **r2
)
1000 quick_widget_t quick_widgets
[] = {
1002 QUICK_LABELED_INPUT (text1
, input_label_above
, *r1
, "input-lbl", r1
, NULL
,
1003 FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1004 QUICK_SEPARATOR (FALSE
),
1005 QUICK_LABELED_INPUT (text2
, input_label_above
, *r2
, "input-lbl", r2
, NULL
,
1006 FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
),
1007 QUICK_START_BUTTONS (TRUE
, TRUE
),
1008 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
1009 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
1010 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
1015 quick_dialog_t qdlg
= {
1018 quick_widgets
, NULL
, NULL
1023 ret
= quick_dialog (&qdlg
);
1025 return (ret
!= B_CANCEL
) ? ret
: 0;
1028 /* --------------------------------------------------------------------------------------------- */
1031 add_new_entry_cmd (void)
1033 char *title
, *url
, *to_free
;
1036 /* Take current directory as default value for input fields */
1037 to_free
= title
= url
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1039 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1040 _("Directory path:"), "[Hotlist]", &title
, &url
);
1045 if (title
== NULL
|| *title
== '\0' || url
== NULL
|| *url
== '\0')
1052 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1053 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AFTER
);
1055 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_BEFORE
);
1057 hotlist_state
.modified
= TRUE
;
1060 /* --------------------------------------------------------------------------------------------- */
1063 add_new_group_input (const char *header
, const char *label
, char **result
)
1065 quick_widget_t quick_widgets
[] = {
1067 QUICK_LABELED_INPUT (label
, input_label_above
, "", "input", result
, NULL
,
1068 FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1069 QUICK_START_BUTTONS (TRUE
, TRUE
),
1070 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
1071 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
1072 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
1077 quick_dialog_t qdlg
= {
1079 header
, "[Hotlist]",
1080 quick_widgets
, NULL
, NULL
1085 ret
= quick_dialog (&qdlg
);
1087 return (ret
!= B_CANCEL
) ? ret
: 0;
1090 /* --------------------------------------------------------------------------------------------- */
1093 add_new_group_cmd (void)
1098 ret
= add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label
);
1099 if (ret
== 0 || label
== NULL
|| *label
== '\0')
1102 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1103 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AFTER
);
1105 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_BEFORE
);
1107 hotlist_state
.modified
= TRUE
;
1110 /* --------------------------------------------------------------------------------------------- */
1113 remove_group (struct hotlist
*grp
)
1115 struct hotlist
*current
= grp
->head
;
1117 while (current
!= NULL
)
1119 struct hotlist
*next
= current
->next
;
1121 if (current
->type
== HL_TYPE_GROUP
)
1122 remove_group (current
);
1124 g_free (current
->label
);
1125 g_free (current
->directory
);
1132 /* --------------------------------------------------------------------------------------------- */
1135 remove_from_hotlist (struct hotlist
*entry
)
1140 if (entry
->type
== HL_TYPE_DOTDOT
)
1143 if (confirm_directory_hotlist_delete
)
1145 char text
[BUF_MEDIUM
];
1151 g_snprintf (text
, sizeof (text
), _("Are you sure you want to remove entry \"%s\"?"),
1152 str_trunc (entry
->label
, 30));
1153 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1154 _("&Yes"), _("&No"));
1159 if (entry
->type
== HL_TYPE_GROUP
)
1161 struct hotlist
*head
= entry
->head
;
1163 if (head
!= NULL
&& (head
->type
!= HL_TYPE_DOTDOT
|| head
->next
!= NULL
))
1165 char text
[BUF_MEDIUM
];
1168 g_snprintf (text
, sizeof (text
), _("Group \"%s\" is not empty.\nRemove it?"),
1169 str_trunc (entry
->label
, 30));
1170 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1171 _("&Yes"), _("&No"));
1176 remove_group (entry
);
1179 unlink_entry (entry
);
1181 g_free (entry
->label
);
1182 g_free (entry
->directory
);
1184 /* now remove list entry from screen */
1185 listbox_remove_current (l_hotlist
);
1186 hotlist_state
.modified
= TRUE
;
1189 /* --------------------------------------------------------------------------------------------- */
1192 load_group (struct hotlist
*grp
)
1194 gchar
**profile_keys
, **keys
;
1195 char *group_section
;
1196 struct hotlist
*current
= 0;
1198 group_section
= find_group_section (grp
);
1200 keys
= mc_config_get_keys (mc_global
.main_config
, group_section
, NULL
);
1202 current_group
= grp
;
1204 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1205 add2hotlist (mc_config_get_string (mc_global
.main_config
, group_section
, *profile_keys
, ""),
1206 g_strdup (*profile_keys
), HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1208 g_free (group_section
);
1211 keys
= mc_config_get_keys (mc_global
.main_config
, grp
->directory
, NULL
);
1213 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1214 add2hotlist (mc_config_get_string (mc_global
.main_config
, group_section
, *profile_keys
, ""),
1215 g_strdup (*profile_keys
), HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1219 for (current
= grp
->head
; current
; current
= current
->next
)
1220 load_group (current
);
1223 /* --------------------------------------------------------------------------------------------- */
1226 hot_skip_blanks (void)
1230 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1235 /* --------------------------------------------------------------------------------------------- */
1238 hot_next_token (void)
1243 if (tkn_buf
== NULL
)
1244 tkn_buf
= g_string_new ("");
1245 g_string_set_size (tkn_buf
, 0);
1248 c
= hot_skip_blanks ();
1258 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n')
1259 g_string_append_c (tkn_buf
, c
);
1263 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"')
1267 c
= getc (hotlist_file
);
1270 g_string_free (tkn_buf
, TRUE
);
1274 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1276 ret
= (c
== EOF
) ? TKN_EOF
: TKN_STRING
;
1279 c
= getc (hotlist_file
);
1282 g_string_free (tkn_buf
, TRUE
);
1288 MC_FALLTHROUGH
; /* it is taken as normal character */
1293 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1295 while ((c
= fgetc (hotlist_file
)) != EOF
&& (g_ascii_isalnum (c
) || !isascii (c
)));
1297 ungetc (c
, hotlist_file
);
1299 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1301 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1303 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1305 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1314 /* --------------------------------------------------------------------------------------------- */
1317 hot_load_group (struct hotlist
*grp
)
1320 struct hotlist
*new_grp
;
1323 current_group
= grp
;
1325 while ((tkn
= hot_next_token ()) != TKN_ENDGROUP
)
1329 CHECK_TOKEN (TKN_STRING
);
1331 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1332 LISTBOX_APPEND_AT_END
);
1334 hot_load_group (new_grp
);
1335 current_group
= grp
;
1339 CHECK_TOKEN (TKN_STRING
);
1340 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1341 CHECK_TOKEN (TKN_URL
);
1342 CHECK_TOKEN (TKN_STRING
);
1343 url
= tilde_expand (tkn_buf
->str
);
1344 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1349 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1350 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1353 hotlist_state
.readonly
= TRUE
;
1354 hotlist_state
.file_error
= TRUE
;
1357 /* skip empty lines */
1360 hotlist_state
.readonly
= TRUE
;
1361 hotlist_state
.file_error
= TRUE
;
1368 /* --------------------------------------------------------------------------------------------- */
1371 hot_load_file (struct hotlist
*grp
)
1374 struct hotlist
*new_grp
;
1377 current_group
= grp
;
1379 while ((tkn
= hot_next_token ()) != TKN_EOF
)
1383 CHECK_TOKEN (TKN_STRING
);
1385 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1386 LISTBOX_APPEND_AT_END
);
1388 hot_load_group (new_grp
);
1389 current_group
= grp
;
1393 CHECK_TOKEN (TKN_STRING
);
1394 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1395 CHECK_TOKEN (TKN_URL
);
1396 CHECK_TOKEN (TKN_STRING
);
1397 url
= tilde_expand (tkn_buf
->str
);
1398 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1403 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1404 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1407 /* skip empty lines */
1410 hotlist_state
.readonly
= TRUE
;
1411 hotlist_state
.file_error
= TRUE
;
1417 /* --------------------------------------------------------------------------------------------- */
1420 clean_up_hotlist_groups (const char *section
)
1424 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1425 if (mc_config_has_group (mc_global
.main_config
, section
))
1426 mc_config_del_group (mc_global
.main_config
, section
);
1428 if (mc_config_has_group (mc_global
.main_config
, grp_section
))
1430 char **profile_keys
, **keys
;
1432 keys
= mc_config_get_keys (mc_global
.main_config
, grp_section
, NULL
);
1434 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1435 clean_up_hotlist_groups (*profile_keys
);
1438 mc_config_del_group (mc_global
.main_config
, grp_section
);
1440 g_free (grp_section
);
1443 /* --------------------------------------------------------------------------------------------- */
1448 gboolean remove_old_list
= FALSE
;
1449 struct stat stat_buf
;
1451 if (hotlist_state
.loaded
)
1453 stat (hotlist_file_name
, &stat_buf
);
1454 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1460 if (hotlist_file_name
== NULL
)
1461 hotlist_file_name
= mc_config_get_full_path (MC_HOTLIST_FILE
);
1463 hotlist
= g_new0 (struct hotlist
, 1);
1464 hotlist
->type
= HL_TYPE_GROUP
;
1465 hotlist
->label
= g_strdup (_("Top level group"));
1466 hotlist
->up
= hotlist
;
1470 hotlist
->directory
= g_strdup ("Hotlist");
1472 hotlist_file
= fopen (hotlist_file_name
, "r");
1473 if (hotlist_file
== NULL
)
1477 load_group (hotlist
);
1478 hotlist_state
.loaded
= TRUE
;
1480 * just to be sure we got copy
1482 hotlist_state
.modified
= TRUE
;
1483 result
= save_hotlist ();
1484 hotlist_state
.modified
= FALSE
;
1486 remove_old_list
= TRUE
;
1488 message (D_ERROR
, _("Hotlist Load"),
1490 ("MC was unable to write %s file,\nyour old hotlist entries were not deleted"),
1491 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE
);
1495 hot_load_file (hotlist
);
1496 fclose (hotlist_file
);
1497 hotlist_state
.loaded
= TRUE
;
1500 if (remove_old_list
)
1502 GError
*mcerror
= NULL
;
1504 clean_up_hotlist_groups ("Hotlist");
1505 if (!mc_config_save_file (mc_global
.main_config
, &mcerror
))
1506 setup_save_config_show_error (mc_global
.main_config
->ini_path
, &mcerror
);
1508 mc_error_message (&mcerror
, NULL
);
1511 stat (hotlist_file_name
, &stat_buf
);
1512 hotlist_file_mtime
= stat_buf
.st_mtime
;
1513 current_group
= hotlist
;
1516 /* --------------------------------------------------------------------------------------------- */
1519 hot_save_group (struct hotlist
*grp
)
1521 struct hotlist
*current
;
1527 for (i = 0; i < n; i++) \
1528 putc (' ', hotlist_file); \
1531 for (current
= grp
->head
; current
!= NULL
; current
= current
->next
)
1532 switch (current
->type
)
1535 INDENT (list_level
);
1536 fputs ("GROUP \"", hotlist_file
);
1537 for (s
= current
->label
; *s
!= '\0'; s
++)
1539 if (*s
== '"' || *s
== '\\')
1540 putc ('\\', hotlist_file
);
1541 putc (*s
, hotlist_file
);
1543 fputs ("\"\n", hotlist_file
);
1545 hot_save_group (current
);
1547 INDENT (list_level
);
1548 fputs ("ENDGROUP\n", hotlist_file
);
1551 INDENT (list_level
);
1552 fputs ("ENTRY \"", hotlist_file
);
1553 for (s
= current
->label
; *s
!= '\0'; s
++)
1555 if (*s
== '"' || *s
== '\\')
1556 putc ('\\', hotlist_file
);
1557 putc (*s
, hotlist_file
);
1559 fputs ("\" URL \"", hotlist_file
);
1560 for (s
= current
->directory
; *s
!= '\0'; s
++)
1562 if (*s
== '"' || *s
== '\\')
1563 putc ('\\', hotlist_file
);
1564 putc (*s
, hotlist_file
);
1566 fputs ("\"\n", hotlist_file
);
1568 case HL_TYPE_COMMENT
:
1569 fprintf (hotlist_file
, "#%s\n", current
->label
);
1571 case HL_TYPE_DOTDOT
:
1579 /* --------------------------------------------------------------------------------------------- */
1582 add_dotdot_to_list (void)
1584 if (current_group
!= hotlist
&& hotlist_has_dot_dot
)
1585 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, LISTBOX_APPEND_AT_END
);
1588 /* --------------------------------------------------------------------------------------------- */
1589 /*** public functions ****************************************************************************/
1590 /* --------------------------------------------------------------------------------------------- */
1593 add2hotlist_cmd (void)
1596 const char *cp
= N_("Label for \"%s\":");
1598 char *label_string
, *label
;
1604 l
= str_term_width1 (cp
);
1605 label_string
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1606 lc_prompt
= g_strdup_printf (cp
, str_trunc (label_string
, COLS
- 2 * UX
- (l
+ 8)));
1608 input_dialog (_("Add to hotlist"), lc_prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
,
1609 INPUT_COMPLETE_NONE
);
1612 if (label
== NULL
|| *label
== '\0')
1614 g_free (label_string
);
1619 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1620 hotlist_state
.modified
= TRUE
;
1624 /* --------------------------------------------------------------------------------------------- */
1627 hotlist_show (hotlist_t list_type
)
1629 char *target
= NULL
;
1632 hotlist_state
.type
= list_type
;
1635 init_hotlist (list_type
);
1637 /* display file info */
1638 tty_setcolor (SELECTED_COLOR
);
1640 hotlist_state
.running
= TRUE
;
1641 res
= dlg_run (hotlist_dlg
);
1642 hotlist_state
.running
= FALSE
;
1648 struct hotlist
*hlp
= NULL
;
1650 listbox_get_current (l_hotlist
, &text
, (void **) &hlp
);
1651 target
= g_strdup (hlp
!= NULL
? hlp
->directory
: text
);
1658 /* --------------------------------------------------------------------------------------------- */
1663 gboolean saved
= FALSE
;
1664 struct stat stat_buf
;
1666 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
!= NULL
)
1668 mc_util_make_backup_if_possible (hotlist_file_name
, ".bak");
1670 hotlist_file
= fopen (hotlist_file_name
, "w");
1671 if (hotlist_file
== NULL
)
1672 mc_util_restore_from_backup_if_possible (hotlist_file_name
, ".bak");
1675 hot_save_group (hotlist
);
1676 fclose (hotlist_file
);
1677 stat (hotlist_file_name
, &stat_buf
);
1678 hotlist_file_mtime
= stat_buf
.st_mtime
;
1679 hotlist_state
.modified
= FALSE
;
1687 /* --------------------------------------------------------------------------------------------- */
1689 * Unload list from memory.
1690 * Don't confuse with hotlist_done() for GUI.
1696 if (hotlist
!= NULL
)
1698 remove_group (hotlist
);
1699 g_free (hotlist
->label
);
1700 g_free (hotlist
->directory
);
1701 MC_PTR_FREE (hotlist
);
1704 hotlist_state
.loaded
= FALSE
;
1706 MC_PTR_FREE (hotlist_file_name
);
1708 current_group
= NULL
;
1710 if (tkn_buf
!= NULL
)
1712 g_string_free (tkn_buf
, TRUE
);
1717 /* --------------------------------------------------------------------------------------------- */