2 Directory hotlist -- for the Midnight Commander
4 Copyright (C) 1994-2016
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 ****************************************************************/
75 #define BY (LINES - 6)
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_ENTER_GROUP (B_USER + 4)
83 #define B_UP_GROUP (B_USER + 5)
84 #define B_INSERT (B_USER + 6)
85 #define B_APPEND (B_USER + 7)
86 #define B_MOVE (B_USER + 8)
88 #define B_FREE_ALL_VFS (B_USER + 9)
89 #define B_REFRESH_VFS (B_USER + 10)
96 #define TKN_ENDGROUP 4
100 #define TKN_UNKNOWN 127
102 #define SKIP_TO_EOL \
105 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
108 #define CHECK_TOKEN(_TKN_) \
109 tkn = hot_next_token (); \
112 hotlist_state.readonly = TRUE; \
113 hotlist_state.file_error = TRUE; \
114 while (tkn != TKN_EOL && tkn != TKN_EOF) \
115 tkn = hot_next_token (); \
119 /*** file scope type declarations ****************************************************************/
132 * these parameters are intended to be user configurable
134 int expanded
; /* expanded view of all groups at startup */
137 * these reflect run time state
140 gboolean loaded
; /* hotlist is loaded */
141 gboolean readonly
; /* hotlist readonly */
142 gboolean file_error
; /* parse error while reading file */
143 gboolean running
; /* we are running dlg (and have to
145 gboolean moving
; /* we are in moving hotlist currently */
146 gboolean modified
; /* hotlist was modified */
147 hotlist_t type
; /* LIST_HOTLIST || LIST_VFSLIST */
150 /* Directory hotlist */
153 enum HotListType type
;
156 struct hotlist
*head
;
158 struct hotlist
*next
;
161 /*** file scope variables ************************************************************************/
163 static gboolean hotlist_has_dot_dot
= TRUE
;
165 static WDialog
*hotlist_dlg
, *movelist_dlg
;
166 static WGroupbox
*hotlist_group
, *movelist_group
;
167 static WListbox
*l_hotlist
, *l_movelist
;
168 static WLabel
*pname
;
172 int ret_cmd
, flags
, y
, x
, len
;
175 widget_pos_flags_t pos_flags
;
179 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, 0, N_("Change &to"),
180 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
182 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, 0, N_("&Free VFSs now"),
183 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
184 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, 0, N_("&Refresh"),
185 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
187 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, 0, N_("&Add current"),
188 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
189 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, 0, N_("&Up"),
190 LIST_HOTLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
191 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, 0, N_("&Cancel"),
192 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_RIGHT
| WPOS_KEEP_BOTTOM
},
193 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, 0, N_("New &group"),
194 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
195 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, 0, N_("New &entry"),
196 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
197 { B_INSERT
, NORMAL_BUTTON
, 1, 0, 0, N_("&Insert"),
198 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
199 { B_APPEND
, NORMAL_BUTTON
, 1, 15, 0, N_("A&ppend"),
200 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
201 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, 0, N_("&Remove"),
202 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
203 { B_MOVE
, NORMAL_BUTTON
, 1, 42, 0, N_("&Move"),
204 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
}
208 static const size_t hotlist_but_num
= G_N_ELEMENTS (hotlist_but
);
210 static struct hotlist
*hotlist
= NULL
;
212 static struct hotlist
*current_group
;
214 static GString
*tkn_buf
= NULL
;
216 static char *hotlist_file_name
;
217 static FILE *hotlist_file
;
218 static time_t hotlist_file_mtime
;
220 static int list_level
= 0;
222 /* --------------------------------------------------------------------------------------------- */
223 /*** file scope functions ************************************************************************/
224 /* --------------------------------------------------------------------------------------------- */
226 static void init_movelist (struct hotlist
*item
);
227 static void add_new_group_cmd (void);
228 static void add_new_entry_cmd (void);
229 static void remove_from_hotlist (struct hotlist
*entry
);
230 static void load_hotlist (void);
231 static void add_dotdot_to_list (void);
233 /* --------------------------------------------------------------------------------------------- */
234 /** If current->data is 0, then we are dealing with a VFS pathname */
237 update_path_name (void)
239 const char *text
= "";
241 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
242 Widget
*w
= WIDGET (list
);
244 if (!listbox_is_empty (list
))
249 listbox_get_current (list
, &ctext
, &cdata
);
254 struct hotlist
*hlp
= (struct hotlist
*) cdata
;
256 if (hlp
->type
== HL_TYPE_ENTRY
|| hlp
->type
== HL_TYPE_DOTDOT
)
257 text
= hlp
->directory
;
258 else if (hlp
->type
== HL_TYPE_GROUP
)
259 text
= _("Subgroup - press ENTER to see list");
263 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
264 if (hotlist_state
.moving
)
265 groupbox_set_title (movelist_group
, str_trunc (p
, w
->cols
- 2));
268 groupbox_set_title (hotlist_group
, str_trunc (p
, w
->cols
- 2));
269 label_set_text (pname
, str_trunc (text
, w
->cols
));
274 /* --------------------------------------------------------------------------------------------- */
277 fill_listbox (WListbox
* list
)
279 struct hotlist
*current
;
282 buff
= g_string_new ("");
284 for (current
= current_group
->head
; current
!= NULL
; current
= current
->next
)
285 switch (current
->type
)
290 g_string_truncate (buff
, 0);
291 g_string_append (buff
, "->");
292 g_string_append (buff
, current
->label
);
293 listbox_add_item (list
, LISTBOX_APPEND_AT_END
, 0, buff
->str
, current
, FALSE
);
298 listbox_add_item (list
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
, FALSE
);
303 g_string_free (buff
, TRUE
);
306 /* --------------------------------------------------------------------------------------------- */
309 unlink_entry (struct hotlist
*entry
)
311 struct hotlist
*current
= current_group
->head
;
313 if (current
== entry
)
314 current_group
->head
= entry
->next
;
317 while (current
!= NULL
&& current
->next
!= entry
)
318 current
= current
->next
;
320 current
->next
= entry
->next
;
322 entry
->next
= entry
->up
= NULL
;
325 /* --------------------------------------------------------------------------------------------- */
329 add_name_to_list (const char *path
)
331 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, path
, NULL
, FALSE
);
333 #endif /* !ENABLE_VFS */
335 /* --------------------------------------------------------------------------------------------- */
338 hotlist_run_cmd (int action
)
344 struct hotlist
*saved
= current_group
;
345 struct hotlist
*item
= NULL
;
346 struct hotlist
*moveto_item
= NULL
;
347 struct hotlist
*moveto_group
= NULL
;
350 if (listbox_is_empty (l_hotlist
))
351 return 0; /* empty group - nothing to do */
353 listbox_get_current (l_hotlist
, NULL
, (void **) &item
);
354 init_movelist (item
);
355 hotlist_state
.moving
= TRUE
;
356 ret
= dlg_run (movelist_dlg
);
357 hotlist_state
.moving
= FALSE
;
358 listbox_get_current (l_movelist
, NULL
, (void **) &moveto_item
);
359 moveto_group
= current_group
;
360 dlg_destroy (movelist_dlg
);
361 current_group
= saved
;
364 if (moveto_item
== item
)
365 return 0; /* If we insert/append a before/after a
366 it hardly changes anything ;) */
368 listbox_remove_current (l_hotlist
);
369 item
->up
= moveto_group
;
370 if (moveto_group
->head
== NULL
)
371 moveto_group
->head
= item
;
372 else if (moveto_item
== NULL
)
373 { /* we have group with just comments */
374 struct hotlist
*p
= moveto_group
->head
;
377 while (p
->next
!= NULL
)
381 else if (ret
== B_ENTER
|| ret
== B_APPEND
)
383 if (moveto_item
->next
== NULL
)
384 moveto_item
->next
= item
;
387 item
->next
= moveto_item
->next
;
388 moveto_item
->next
= item
;
391 else if (moveto_group
->head
== moveto_item
)
393 moveto_group
->head
= item
;
394 item
->next
= moveto_item
;
398 struct hotlist
*p
= moveto_group
->head
;
400 while (p
->next
!= moveto_item
)
402 item
->next
= p
->next
;
405 listbox_remove_list (l_hotlist
);
406 fill_listbox (l_hotlist
);
408 hotlist_state
.modified
= TRUE
;
413 struct hotlist
*entry
= NULL
;
415 listbox_get_current (l_hotlist
, NULL
, (void **) &entry
);
416 remove_from_hotlist (entry
);
421 add_new_group_cmd ();
429 add_new_entry_cmd ();
439 list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
440 listbox_get_current (list
, NULL
, &data
);
445 hlp
= (struct hotlist
*) data
;
447 if (hlp
->type
== HL_TYPE_ENTRY
)
448 return (action
== B_ENTER
? 1 : 0);
449 if (hlp
->type
!= HL_TYPE_DOTDOT
)
451 listbox_remove_list (list
);
456 /* Fall through - go up */
458 /* Fall through if list empty - just go up */
462 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
464 listbox_remove_list (list
);
465 current_group
= current_group
->up
;
476 listbox_remove_list (l_hotlist
);
477 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), NULL
,
479 vfs_fill_names (add_name_to_list
);
481 #endif /* ENABLE_VFS */
488 /* --------------------------------------------------------------------------------------------- */
491 hotlist_button_callback (WButton
* button
, int action
)
496 ret
= hotlist_run_cmd (action
);
501 /* --------------------------------------------------------------------------------------------- */
503 static inline cb_ret_t
504 hotlist_handle_key (WDialog
* h
, int key
)
508 case KEY_M_CTRL
| '\n':
513 if (hotlist_button_callback (NULL
, B_ENTER
) != 0)
515 h
->ret_value
= B_ENTER
;
521 /* enter to the group */
522 if (hotlist_state
.type
== LIST_VFSLIST
)
523 return MSG_NOT_HANDLED
;
524 return hotlist_button_callback (NULL
, B_ENTER_GROUP
) == 0 ? MSG_HANDLED
: MSG_NOT_HANDLED
;
527 /* leave the group */
528 if (hotlist_state
.type
== LIST_VFSLIST
)
529 return MSG_NOT_HANDLED
;
530 return hotlist_button_callback (NULL
, B_UP_GROUP
) == 0 ? MSG_HANDLED
: MSG_NOT_HANDLED
;
533 if (hotlist_state
.moving
)
534 return MSG_NOT_HANDLED
;
535 hotlist_button_callback (NULL
, B_REMOVE
);
541 if (!hotlist_state
.moving
)
545 listbox_get_current (l_hotlist
, NULL
, &ldata
);
549 struct hotlist
*hlp
= (struct hotlist
*) ldata
;
551 if (hlp
->type
== HL_TYPE_ENTRY
)
555 tmp
= g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
556 input_insert (cmdline
, tmp
, FALSE
);
558 h
->ret_value
= B_CANCEL
;
563 return MSG_HANDLED
; /* ignore key */
566 return MSG_NOT_HANDLED
;
570 /* --------------------------------------------------------------------------------------------- */
573 hotlist_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
575 WDialog
*h
= DIALOG (w
);
580 case MSG_NOTIFY
: /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
584 case MSG_UNHANDLED_KEY
:
585 return hotlist_handle_key (h
, parm
);
588 /* always stay on hotlist */
589 dlg_select_widget (h
== hotlist_dlg
? l_hotlist
: l_movelist
);
593 /* simply call dlg_set_size() with new size */
594 dlg_set_size (h
, LINES
- (h
== hotlist_dlg
? 2 : 6), COLS
- 6);
598 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
602 /* --------------------------------------------------------------------------------------------- */
605 hotlist_listbox_callback (WListbox
* list
)
607 WDialog
*dlg
= WIDGET (list
)->owner
;
609 if (!listbox_is_empty (list
))
613 listbox_get_current (list
, NULL
, &data
);
617 struct hotlist
*hlp
= (struct hotlist
*) data
;
619 if (hlp
->type
== HL_TYPE_ENTRY
)
621 dlg
->ret_value
= B_ENTER
;
627 hotlist_button_callback (NULL
, B_ENTER
);
628 send_message (dlg
, NULL
, MSG_POST_KEY
, '\n', NULL
);
634 dlg
->ret_value
= B_ENTER
;
640 hotlist_button_callback (NULL
, B_UP_GROUP
);
641 send_message (dlg
, NULL
, MSG_POST_KEY
, 'u', NULL
);
645 /* --------------------------------------------------------------------------------------------- */
647 * Expands all button names (once) and recalculates button positions.
648 * returns number of columns in the dialog box, which is 10 chars longer
651 * If common width of the window (i.e. in xterm) is less than returned
652 * width - sorry :) (anyway this did not handled in previous version too)
656 init_i18n_stuff (int list_type
, int cols
)
660 static gboolean i18n_flag
= FALSE
;
664 for (i
= 0; i
< hotlist_but_num
; i
++)
667 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
668 #endif /* ENABLE_NLS */
669 hotlist_but
[i
].len
= str_term_width1 (hotlist_but
[i
].text
) + 3;
670 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
671 hotlist_but
[i
].len
+= 2;
677 /* Dynamic resizing of buttonbars */
679 int len
[2], count
[2]; /* at most two lines of buttons */
683 count
[0] = count
[1] = 0;
684 cur_x
[0] = cur_x
[1] = 0;
686 /* Count len of buttonbars, assuming 1 extra space between buttons */
687 for (i
= 0; i
< hotlist_but_num
; i
++)
688 if ((hotlist_but
[i
].type
& list_type
) != 0)
692 row
= hotlist_but
[i
].y
;
694 len
[row
] += hotlist_but
[i
].len
+ 1;
700 cols
= MAX (cols
, MAX (len
[0], len
[1]));
702 /* arrange buttons */
703 for (i
= 0; i
< hotlist_but_num
; i
++)
704 if ((hotlist_but
[i
].type
& list_type
) != 0)
708 row
= hotlist_but
[i
].y
;
710 if (hotlist_but
[i
].x
!= 0)
712 /* not first int the row */
713 if (hotlist_but
[i
].ret_cmd
== B_CANCEL
)
714 hotlist_but
[i
].x
= cols
- hotlist_but
[i
].len
- 6;
716 hotlist_but
[i
].x
= cur_x
[row
];
719 cur_x
[row
] += hotlist_but
[i
].len
+ 1;
726 /* --------------------------------------------------------------------------------------------- */
729 init_hotlist (hotlist_t list_type
)
732 const char *title
, *help_node
;
737 Widget
*hotlist_widget
;
742 cols
= init_i18n_stuff (list_type
, COLS
- 6);
744 hotlist_state
.expanded
=
745 mc_config_get_int (mc_main_config
, "HotlistConfig", "expanded_view_of_groups", 0);
748 if (list_type
== LIST_VFSLIST
)
750 title
= _("Active VFS directories");
751 help_node
= "[vfshot]"; /* FIXME - no such node */
755 #endif /* !ENABLE_VFS */
757 title
= _("Directory hotlist");
758 help_node
= "[Hotlist]";
762 dlg_create (TRUE
, 0, 0, lines
, cols
, dialog_colors
, hotlist_callback
, NULL
, help_node
,
766 hotlist_group
= groupbox_new (y
, UX
, lines
- 10 + dh
, cols
- 2 * UX
, _("Top level group"));
767 hotlist_widget
= WIDGET (hotlist_group
);
768 add_widget_autopos (hotlist_dlg
, hotlist_widget
, WPOS_KEEP_ALL
, NULL
);
771 listbox_new (y
+ 1, UX
+ 1, hotlist_widget
->lines
- 2, hotlist_widget
->cols
- 2, FALSE
,
772 hotlist_listbox_callback
);
774 /* Fill the hotlist with the active VFS or the hotlist */
776 if (list_type
== LIST_VFSLIST
)
778 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), NULL
,
780 vfs_fill_names (add_name_to_list
);
783 #endif /* !ENABLE_VFS */
784 fill_listbox (l_hotlist
);
786 /* insert before groupbox to view scrollbar */
787 add_widget_autopos (hotlist_dlg
, l_hotlist
, WPOS_KEEP_ALL
, NULL
);
789 y
+= hotlist_widget
->lines
;
791 path_box
= groupbox_new (y
, UX
, 3, hotlist_widget
->cols
, _("Directory path"));
792 add_widget_autopos (hotlist_dlg
, path_box
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_HORZ
, NULL
);
794 pname
= label_new (y
+ 1, UX
+ 2, "");
795 add_widget_autopos (hotlist_dlg
, pname
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
, NULL
);
796 y
+= WIDGET (path_box
)->lines
;
798 add_widget_autopos (hotlist_dlg
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
800 for (i
= 0; i
< hotlist_but_num
; i
++)
801 if ((hotlist_but
[i
].type
& list_type
) != 0)
802 add_widget_autopos (hotlist_dlg
,
803 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
804 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
805 hotlist_but
[i
].text
, hotlist_button_callback
),
806 hotlist_but
[i
].pos_flags
, NULL
);
808 dlg_select_widget (l_hotlist
);
811 /* --------------------------------------------------------------------------------------------- */
814 init_movelist (struct hotlist
*item
)
820 Widget
*movelist_widget
;
825 cols
= init_i18n_stuff (LIST_MOVELIST
, COLS
- 6);
827 hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
830 dlg_create (TRUE
, 0, 0, lines
, cols
, dialog_colors
, hotlist_callback
, NULL
, "[Hotlist]",
836 movelist_group
= groupbox_new (y
, UX
, lines
- 7, cols
- 2 * UX
, _("Directory label"));
837 movelist_widget
= WIDGET (movelist_group
);
838 add_widget_autopos (movelist_dlg
, movelist_widget
, WPOS_KEEP_ALL
, NULL
);
841 listbox_new (y
+ 1, UX
+ 1, movelist_widget
->lines
- 2, movelist_widget
->cols
- 2, FALSE
,
842 hotlist_listbox_callback
);
843 fill_listbox (l_movelist
);
844 /* insert before groupbox to view scrollbar */
845 add_widget_autopos (movelist_dlg
, l_movelist
, WPOS_KEEP_ALL
, NULL
);
847 y
+= movelist_widget
->lines
;
849 add_widget_autopos (movelist_dlg
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
851 for (i
= 0; i
< hotlist_but_num
; i
++)
852 if ((hotlist_but
[i
].type
& LIST_MOVELIST
) != 0)
853 add_widget_autopos (movelist_dlg
,
854 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
855 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
856 hotlist_but
[i
].text
, hotlist_button_callback
),
857 hotlist_but
[i
].pos_flags
, NULL
);
859 dlg_select_widget (l_movelist
);
862 /* --------------------------------------------------------------------------------------------- */
864 * Destroy the list dialog.
865 * Don't confuse with done_hotlist() for the list in memory.
871 dlg_destroy (hotlist_dlg
);
874 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
879 /* --------------------------------------------------------------------------------------------- */
882 find_group_section (struct hotlist
*grp
)
884 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
887 /* --------------------------------------------------------------------------------------------- */
889 static struct hotlist
*
890 add2hotlist (char *label
, char *directory
, enum HotListType type
, listbox_append_t pos
)
893 struct hotlist
*current
= NULL
;
896 * Hotlist is neither loaded nor loading.
897 * Must be called by "Ctrl-x a" before using hotlist.
899 if (current_group
== NULL
)
902 listbox_get_current (l_hotlist
, NULL
, (void **) ¤t
);
904 /* Make sure '..' stays at the top of the list. */
905 if ((current
!= NULL
) && (current
->type
== HL_TYPE_DOTDOT
))
906 pos
= LISTBOX_APPEND_AFTER
;
908 new = g_new0 (struct hotlist
, 1);
912 new->directory
= directory
;
913 new->up
= current_group
;
915 if (type
== HL_TYPE_GROUP
)
918 add_dotdot_to_list ();
919 current_group
= new->up
;
922 if (current_group
->head
== NULL
)
924 /* first element in group */
925 current_group
->head
= new;
927 else if (pos
== LISTBOX_APPEND_AFTER
)
929 new->next
= current
->next
;
932 else if (pos
== LISTBOX_APPEND_BEFORE
&& current
== current_group
->head
)
934 /* should be inserted before first item */
936 current_group
->head
= new;
938 else if (pos
== LISTBOX_APPEND_BEFORE
)
940 struct hotlist
*p
= current_group
->head
;
942 while (p
->next
!= current
)
949 { /* append at the end */
950 struct hotlist
*p
= current_group
->head
;
952 while (p
->next
!= NULL
)
958 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&& type
!= HL_TYPE_DOTDOT
)
960 if (type
== HL_TYPE_GROUP
)
964 lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
965 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new, FALSE
);
969 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new, FALSE
);
970 listbox_select_entry (l_hotlist
, l_hotlist
->pos
);
976 /* --------------------------------------------------------------------------------------------- */
979 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
980 const char *help
, char **r1
, char **r2
)
982 quick_widget_t quick_widgets
[] = {
984 QUICK_LABELED_INPUT (text1
, input_label_above
, *r1
, "input-lbl", r1
, NULL
,
985 FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
986 QUICK_SEPARATOR (FALSE
),
987 QUICK_LABELED_INPUT (text2
, input_label_above
, *r2
, "input-lbl", r2
, NULL
,
988 FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
),
989 QUICK_START_BUTTONS (TRUE
, TRUE
),
990 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
991 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
992 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
997 quick_dialog_t qdlg
= {
1000 quick_widgets
, NULL
, NULL
1005 ret
= quick_dialog (&qdlg
);
1007 return (ret
!= B_CANCEL
) ? ret
: 0;
1010 /* --------------------------------------------------------------------------------------------- */
1013 add_new_entry_cmd (void)
1015 char *title
, *url
, *to_free
;
1018 /* Take current directory as default value for input fields */
1019 to_free
= title
= url
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1021 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1022 _("Directory path:"), "[Hotlist]", &title
, &url
);
1027 if (title
== NULL
|| *title
== '\0' || url
== NULL
|| *url
== '\0')
1034 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1035 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AFTER
);
1037 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_BEFORE
);
1039 hotlist_state
.modified
= TRUE
;
1042 /* --------------------------------------------------------------------------------------------- */
1045 add_new_group_input (const char *header
, const char *label
, char **result
)
1047 quick_widget_t quick_widgets
[] = {
1049 QUICK_LABELED_INPUT (label
, input_label_above
, "", "input", result
, NULL
,
1050 FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1051 QUICK_START_BUTTONS (TRUE
, TRUE
),
1052 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
1053 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
1054 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
1059 quick_dialog_t qdlg
= {
1061 header
, "[Hotlist]",
1062 quick_widgets
, NULL
, NULL
1067 ret
= quick_dialog (&qdlg
);
1069 return (ret
!= B_CANCEL
) ? ret
: 0;
1072 /* --------------------------------------------------------------------------------------------- */
1075 add_new_group_cmd (void)
1080 ret
= add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label
);
1081 if (ret
== 0 || label
== NULL
|| *label
== '\0')
1084 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1085 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AFTER
);
1087 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_BEFORE
);
1089 hotlist_state
.modified
= TRUE
;
1092 /* --------------------------------------------------------------------------------------------- */
1095 remove_group (struct hotlist
*grp
)
1097 struct hotlist
*current
= grp
->head
;
1099 while (current
!= NULL
)
1101 struct hotlist
*next
= current
->next
;
1103 if (current
->type
== HL_TYPE_GROUP
)
1104 remove_group (current
);
1106 g_free (current
->label
);
1107 g_free (current
->directory
);
1114 /* --------------------------------------------------------------------------------------------- */
1117 remove_from_hotlist (struct hotlist
*entry
)
1122 if (entry
->type
== HL_TYPE_DOTDOT
)
1125 if (confirm_directory_hotlist_delete
)
1127 char text
[BUF_MEDIUM
];
1133 g_snprintf (text
, sizeof (text
), _("Are you sure you want to remove entry \"%s\"?"),
1134 str_trunc (entry
->label
, 30));
1135 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1136 _("&Yes"), _("&No"));
1141 if (entry
->type
== HL_TYPE_GROUP
)
1143 struct hotlist
*head
= entry
->head
;
1145 if (head
!= NULL
&& (head
->type
!= HL_TYPE_DOTDOT
|| head
->next
!= NULL
))
1147 char text
[BUF_MEDIUM
];
1150 g_snprintf (text
, sizeof (text
), _("Group \"%s\" is not empty.\nRemove it?"),
1151 str_trunc (entry
->label
, 30));
1152 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1153 _("&Yes"), _("&No"));
1158 remove_group (entry
);
1161 unlink_entry (entry
);
1163 g_free (entry
->label
);
1164 g_free (entry
->directory
);
1166 /* now remove list entry from screen */
1167 listbox_remove_current (l_hotlist
);
1168 hotlist_state
.modified
= TRUE
;
1171 /* --------------------------------------------------------------------------------------------- */
1174 load_group (struct hotlist
*grp
)
1176 gchar
**profile_keys
, **keys
;
1177 char *group_section
;
1178 struct hotlist
*current
= 0;
1180 group_section
= find_group_section (grp
);
1182 keys
= mc_config_get_keys (mc_main_config
, group_section
, NULL
);
1184 current_group
= grp
;
1186 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1187 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1188 g_strdup (*profile_keys
), HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1190 g_free (group_section
);
1193 keys
= mc_config_get_keys (mc_main_config
, grp
->directory
, NULL
);
1195 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1196 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1197 g_strdup (*profile_keys
), HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1201 for (current
= grp
->head
; current
; current
= current
->next
)
1202 load_group (current
);
1205 /* --------------------------------------------------------------------------------------------- */
1208 hot_skip_blanks (void)
1212 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1217 /* --------------------------------------------------------------------------------------------- */
1220 hot_next_token (void)
1225 if (tkn_buf
== NULL
)
1226 tkn_buf
= g_string_new ("");
1227 g_string_set_size (tkn_buf
, 0);
1230 c
= hot_skip_blanks ();
1240 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n')
1241 g_string_append_c (tkn_buf
, c
);
1245 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"')
1249 c
= getc (hotlist_file
);
1252 g_string_free (tkn_buf
, TRUE
);
1256 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1258 ret
= (c
== EOF
) ? TKN_EOF
: TKN_STRING
;
1261 c
= getc (hotlist_file
);
1264 g_string_free (tkn_buf
, TRUE
);
1270 /* fall through; it is taken as normal character */
1275 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1277 while ((c
= fgetc (hotlist_file
)) != EOF
&& (g_ascii_isalnum (c
) || !isascii (c
)));
1279 ungetc (c
, hotlist_file
);
1281 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1283 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1285 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1287 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1296 /* --------------------------------------------------------------------------------------------- */
1299 hot_load_group (struct hotlist
*grp
)
1302 struct hotlist
*new_grp
;
1305 current_group
= grp
;
1307 while ((tkn
= hot_next_token ()) != TKN_ENDGROUP
)
1311 CHECK_TOKEN (TKN_STRING
);
1313 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1314 LISTBOX_APPEND_AT_END
);
1316 hot_load_group (new_grp
);
1317 current_group
= grp
;
1321 CHECK_TOKEN (TKN_STRING
);
1322 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1323 CHECK_TOKEN (TKN_URL
);
1324 CHECK_TOKEN (TKN_STRING
);
1325 url
= tilde_expand (tkn_buf
->str
);
1326 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1331 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1332 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1335 hotlist_state
.readonly
= TRUE
;
1336 hotlist_state
.file_error
= TRUE
;
1339 /* skip empty lines */
1342 hotlist_state
.readonly
= TRUE
;
1343 hotlist_state
.file_error
= TRUE
;
1350 /* --------------------------------------------------------------------------------------------- */
1353 hot_load_file (struct hotlist
*grp
)
1356 struct hotlist
*new_grp
;
1359 current_group
= grp
;
1361 while ((tkn
= hot_next_token ()) != TKN_EOF
)
1365 CHECK_TOKEN (TKN_STRING
);
1367 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1368 LISTBOX_APPEND_AT_END
);
1370 hot_load_group (new_grp
);
1371 current_group
= grp
;
1375 CHECK_TOKEN (TKN_STRING
);
1376 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1377 CHECK_TOKEN (TKN_URL
);
1378 CHECK_TOKEN (TKN_STRING
);
1379 url
= tilde_expand (tkn_buf
->str
);
1380 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1385 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1386 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1389 /* skip empty lines */
1392 hotlist_state
.readonly
= TRUE
;
1393 hotlist_state
.file_error
= TRUE
;
1399 /* --------------------------------------------------------------------------------------------- */
1402 clean_up_hotlist_groups (const char *section
)
1406 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1407 if (mc_config_has_group (mc_main_config
, section
))
1408 mc_config_del_group (mc_main_config
, section
);
1410 if (mc_config_has_group (mc_main_config
, grp_section
))
1412 char **profile_keys
, **keys
;
1414 keys
= mc_config_get_keys (mc_main_config
, grp_section
, NULL
);
1416 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1417 clean_up_hotlist_groups (*profile_keys
);
1420 mc_config_del_group (mc_main_config
, grp_section
);
1422 g_free (grp_section
);
1425 /* --------------------------------------------------------------------------------------------- */
1430 gboolean remove_old_list
= FALSE
;
1431 struct stat stat_buf
;
1433 if (hotlist_state
.loaded
)
1435 stat (hotlist_file_name
, &stat_buf
);
1436 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1442 if (hotlist_file_name
== NULL
)
1443 hotlist_file_name
= mc_config_get_full_path (MC_HOTLIST_FILE
);
1445 hotlist
= g_new0 (struct hotlist
, 1);
1446 hotlist
->type
= HL_TYPE_GROUP
;
1447 hotlist
->label
= g_strdup (_("Top level group"));
1448 hotlist
->up
= hotlist
;
1452 hotlist
->directory
= g_strdup ("Hotlist");
1454 hotlist_file
= fopen (hotlist_file_name
, "r");
1455 if (hotlist_file
== NULL
)
1459 load_group (hotlist
);
1460 hotlist_state
.loaded
= TRUE
;
1462 * just to be sure we got copy
1464 hotlist_state
.modified
= TRUE
;
1465 result
= save_hotlist ();
1466 hotlist_state
.modified
= FALSE
;
1468 remove_old_list
= TRUE
;
1470 message (D_ERROR
, _("Hotlist Load"),
1472 ("MC was unable to write %s file,\nyour old hotlist entries were not deleted"),
1473 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE
);
1477 hot_load_file (hotlist
);
1478 fclose (hotlist_file
);
1479 hotlist_state
.loaded
= TRUE
;
1482 if (remove_old_list
)
1484 GError
*mcerror
= NULL
;
1486 clean_up_hotlist_groups ("Hotlist");
1487 if (!mc_config_save_file (mc_main_config
, &mcerror
))
1488 setup_save_config_show_error (mc_main_config
->ini_path
, &mcerror
);
1490 mc_error_message (&mcerror
, NULL
);
1493 stat (hotlist_file_name
, &stat_buf
);
1494 hotlist_file_mtime
= stat_buf
.st_mtime
;
1495 current_group
= hotlist
;
1498 /* --------------------------------------------------------------------------------------------- */
1501 hot_save_group (struct hotlist
*grp
)
1503 struct hotlist
*current
;
1509 for (i = 0; i < n; i++) \
1510 putc (' ', hotlist_file); \
1513 for (current
= grp
->head
; current
!= NULL
; current
= current
->next
)
1514 switch (current
->type
)
1517 INDENT (list_level
);
1518 fputs ("GROUP \"", hotlist_file
);
1519 for (s
= current
->label
; *s
!= '\0'; s
++)
1521 if (*s
== '"' || *s
== '\\')
1522 putc ('\\', hotlist_file
);
1523 putc (*s
, hotlist_file
);
1525 fputs ("\"\n", hotlist_file
);
1527 hot_save_group (current
);
1529 INDENT (list_level
);
1530 fputs ("ENDGROUP\n", hotlist_file
);
1533 INDENT (list_level
);
1534 fputs ("ENTRY \"", hotlist_file
);
1535 for (s
= current
->label
; *s
!= '\0'; s
++)
1537 if (*s
== '"' || *s
== '\\')
1538 putc ('\\', hotlist_file
);
1539 putc (*s
, hotlist_file
);
1541 fputs ("\" URL \"", hotlist_file
);
1542 for (s
= current
->directory
; *s
!= '\0'; s
++)
1544 if (*s
== '"' || *s
== '\\')
1545 putc ('\\', hotlist_file
);
1546 putc (*s
, hotlist_file
);
1548 fputs ("\"\n", hotlist_file
);
1550 case HL_TYPE_COMMENT
:
1551 fprintf (hotlist_file
, "#%s\n", current
->label
);
1553 case HL_TYPE_DOTDOT
:
1561 /* --------------------------------------------------------------------------------------------- */
1564 add_dotdot_to_list (void)
1566 if (current_group
!= hotlist
&& hotlist_has_dot_dot
)
1567 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, LISTBOX_APPEND_AT_END
);
1570 /* --------------------------------------------------------------------------------------------- */
1571 /*** public functions ****************************************************************************/
1572 /* --------------------------------------------------------------------------------------------- */
1575 add2hotlist_cmd (void)
1578 const char *cp
= N_("Label for \"%s\":");
1580 char *label_string
, *label
;
1586 l
= str_term_width1 (cp
);
1587 label_string
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1588 lc_prompt
= g_strdup_printf (cp
, str_trunc (label_string
, COLS
- 2 * UX
- (l
+ 8)));
1590 input_dialog (_("Add to hotlist"), lc_prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
,
1591 INPUT_COMPLETE_NONE
);
1594 if (label
== NULL
|| *label
== '\0')
1596 g_free (label_string
);
1601 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1602 hotlist_state
.modified
= TRUE
;
1606 /* --------------------------------------------------------------------------------------------- */
1609 hotlist_show (hotlist_t list_type
)
1611 char *target
= NULL
;
1614 hotlist_state
.type
= list_type
;
1617 init_hotlist (list_type
);
1619 /* display file info */
1620 tty_setcolor (SELECTED_COLOR
);
1622 hotlist_state
.running
= TRUE
;
1623 res
= dlg_run (hotlist_dlg
);
1624 hotlist_state
.running
= FALSE
;
1630 struct hotlist
*hlp
= NULL
;
1632 listbox_get_current (l_hotlist
, &text
, (void **) &hlp
);
1633 target
= g_strdup (hlp
!= NULL
? hlp
->directory
: text
);
1640 /* --------------------------------------------------------------------------------------------- */
1645 gboolean saved
= FALSE
;
1646 struct stat stat_buf
;
1648 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
!= NULL
)
1650 mc_util_make_backup_if_possible (hotlist_file_name
, ".bak");
1652 hotlist_file
= fopen (hotlist_file_name
, "w");
1653 if (hotlist_file
== NULL
)
1654 mc_util_restore_from_backup_if_possible (hotlist_file_name
, ".bak");
1657 hot_save_group (hotlist
);
1658 fclose (hotlist_file
);
1659 stat (hotlist_file_name
, &stat_buf
);
1660 hotlist_file_mtime
= stat_buf
.st_mtime
;
1661 hotlist_state
.modified
= FALSE
;
1669 /* --------------------------------------------------------------------------------------------- */
1671 * Unload list from memory.
1672 * Don't confuse with hotlist_done() for GUI.
1678 if (hotlist
!= NULL
)
1680 remove_group (hotlist
);
1681 g_free (hotlist
->label
);
1682 g_free (hotlist
->directory
);
1683 MC_PTR_FREE (hotlist
);
1686 hotlist_state
.loaded
= FALSE
;
1688 MC_PTR_FREE (hotlist_file_name
);
1690 current_group
= NULL
;
1692 if (tkn_buf
!= NULL
)
1694 g_string_free (tkn_buf
, TRUE
);
1699 /* --------------------------------------------------------------------------------------------- */