2 Directory hotlist -- for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
6 The Free Software Foundation, Inc.
10 Janne Kukonlehto, 1995
11 Andrej Borsenkow, 1996
13 Andrew Borodin <aborodin@vmail.ru>, 2012
15 Janne did the original Hotlist code, Andrej made the groupable
16 hotlist; the move hotlist and revamped the file format and made
19 This file is part of the Midnight Commander.
21 The Midnight Commander is free software: you can redistribute it
22 and/or modify it under the terms of the GNU General Public License as
23 published by the Free Software Foundation, either version 3 of the License,
24 or (at your option) any later version.
26 The Midnight Commander is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program. If not, see <http://www.gnu.org/licenses/>.
36 * \brief Source: directory hotlist
44 #include <sys/types.h>
48 #include "lib/global.h"
50 #include "lib/tty/tty.h" /* COLS */
51 #include "lib/tty/key.h" /* KEY_M_CTRL */
52 #include "lib/skin.h" /* colors */
53 #include "lib/mcconfig.h" /* Load/save directories hotlist */
54 #include "lib/fileloc.h"
55 #include "lib/strutil.h"
56 #include "lib/vfs/vfs.h"
58 #include "lib/widget.h"
60 #include "src/setup.h" /* For profile_bname */
61 #include "src/history.h"
63 #include "midnight.h" /* current_panel */
64 #include "command.h" /* cmdline */
68 /*** global variables ****************************************************************************/
70 /*** file scope macro definitions ****************************************************************/
76 #define BY (LINES - 6)
79 #define B_ADD_CURRENT B_USER
80 #define B_REMOVE (B_USER + 1)
81 #define B_NEW_GROUP (B_USER + 2)
82 #define B_NEW_ENTRY (B_USER + 3)
83 #define B_UP_GROUP (B_USER + 4)
84 #define B_INSERT (B_USER + 5)
85 #define B_APPEND (B_USER + 6)
86 #define B_MOVE (B_USER + 7)
89 #define B_FREE_ALL_VFS (B_USER + 8)
90 #define B_REFRESH_VFS (B_USER + 9)
97 #define TKN_ENDGROUP 4
101 #define TKN_UNKNOWN 127
103 #define SKIP_TO_EOL \
106 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
109 #define CHECK_TOKEN(_TKN_) \
110 tkn = hot_next_token (); \
113 hotlist_state.readonly = TRUE; \
114 hotlist_state.file_error = TRUE; \
115 while (tkn != TKN_EOL && tkn != TKN_EOF) \
116 tkn = hot_next_token (); \
120 /*** file scope type declarations ****************************************************************/
133 * these parameters are intended to be user configurable
135 int expanded
; /* expanded view of all groups at startup */
138 * these reflect run time state
141 gboolean loaded
; /* hotlist is loaded */
142 gboolean readonly
; /* hotlist readonly */
143 gboolean file_error
; /* parse error while reading file */
144 gboolean running
; /* we are running dlg (and have to
146 gboolean moving
; /* we are in moving hotlist currently */
147 gboolean modified
; /* hotlist was modified */
148 hotlist_t type
; /* LIST_HOTLIST || LIST_VFSLIST */
151 /* Directory hotlist */
154 enum HotListType type
;
157 struct hotlist
*head
;
159 struct hotlist
*next
;
162 /*** file scope variables ************************************************************************/
164 static gboolean hotlist_has_dot_dot
= TRUE
;
166 static Dlg_head
*hotlist_dlg
, *movelist_dlg
;
167 static WGroupbox
*hotlist_group
, *movelist_group
;
168 static WListbox
*l_hotlist
, *l_movelist
;
169 static WLabel
*pname
;
173 int ret_cmd
, flags
, y
, x
, len
;
176 widget_pos_flags_t pos_flags
;
180 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, 0, N_("Change &to"),
181 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
183 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, 0, N_("&Free VFSs now"),
184 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
185 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, 0, N_("&Refresh"),
186 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
188 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, 0, N_("&Add current"),
189 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
190 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, 0, N_("&Up"),
191 LIST_HOTLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
192 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, 0, N_("&Cancel"),
193 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_RIGHT
| WPOS_KEEP_BOTTOM
},
194 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, 0, N_("New &group"),
195 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
196 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, 0, N_("New &entry"),
197 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
198 { B_INSERT
, NORMAL_BUTTON
, 1, 0, 0, N_("&Insert"),
199 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
200 { B_APPEND
, NORMAL_BUTTON
, 1, 15, 0, N_("A&ppend"),
201 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
202 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, 0, N_("&Remove"),
203 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
204 { B_MOVE
, NORMAL_BUTTON
, 1, 42, 0, N_("&Move"),
205 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
}
209 static const size_t hotlist_but_num
= G_N_ELEMENTS (hotlist_but
);
211 static struct hotlist
*hotlist
= NULL
;
213 static struct hotlist
*current_group
;
215 static GString
*tkn_buf
= NULL
;
217 static char *hotlist_file_name
;
218 static FILE *hotlist_file
;
219 static time_t hotlist_file_mtime
;
221 static int list_level
= 0;
223 /* --------------------------------------------------------------------------------------------- */
224 /*** file scope functions ************************************************************************/
225 /* --------------------------------------------------------------------------------------------- */
227 static void init_movelist (struct hotlist
*item
);
228 static void add_new_group_cmd (void);
229 static void add_new_entry_cmd (void);
230 static void remove_from_hotlist (struct hotlist
*entry
);
231 static void load_hotlist (void);
232 static void add_dotdot_to_list (void);
234 /* --------------------------------------------------------------------------------------------- */
235 /** If current->data is 0, then we are dealing with a VFS pathname */
238 update_path_name (void)
240 const char *text
= "";
242 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
243 Widget
*w
= WIDGET (list
);
245 if (list
->count
!= 0)
250 listbox_get_current (list
, &ctext
, &cdata
);
255 struct hotlist
*hlp
= (struct hotlist
*) cdata
;
257 if (hlp
->type
== HL_TYPE_ENTRY
|| hlp
->type
== HL_TYPE_DOTDOT
)
258 text
= hlp
->directory
;
259 else if (hlp
->type
== HL_TYPE_GROUP
)
260 text
= _("Subgroup - press ENTER to see list");
264 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
265 if (hotlist_state
.moving
)
266 groupbox_set_title (movelist_group
, str_trunc (p
, w
->cols
- 2));
269 groupbox_set_title (hotlist_group
, str_trunc (p
, w
->cols
- 2));
270 label_set_text (pname
, str_trunc (text
, w
->cols
));
275 /* --------------------------------------------------------------------------------------------- */
278 fill_listbox (WListbox
* list
)
280 struct hotlist
*current
;
283 buff
= g_string_new ("");
285 for (current
= current_group
->head
; current
!= NULL
; current
= current
->next
)
286 switch (current
->type
)
291 g_string_truncate (buff
, 0);
292 g_string_append (buff
, "->");
293 g_string_append (buff
, current
->label
);
294 listbox_add_item (list
, LISTBOX_APPEND_AT_END
, 0, buff
->str
, current
);
299 listbox_add_item (list
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
);
304 g_string_free (buff
, TRUE
);
307 /* --------------------------------------------------------------------------------------------- */
310 unlink_entry (struct hotlist
*entry
)
312 struct hotlist
*current
= current_group
->head
;
314 if (current
== entry
)
315 current_group
->head
= entry
->next
;
318 while (current
!= NULL
&& current
->next
!= entry
)
319 current
= current
->next
;
321 current
->next
= entry
->next
;
323 entry
->next
= entry
->up
= NULL
;
326 /* --------------------------------------------------------------------------------------------- */
330 add_name_to_list (const char *path
)
332 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, path
, 0);
334 #endif /* !ENABLE_VFS */
336 /* --------------------------------------------------------------------------------------------- */
339 hotlist_button_callback (WButton
* button
, int action
)
347 struct hotlist
*saved
= current_group
;
348 struct hotlist
*item
= NULL
;
349 struct hotlist
*moveto_item
= NULL
;
350 struct hotlist
*moveto_group
= NULL
;
353 if (l_hotlist
->count
== 0)
354 return 0; /* empty group - nothing to do */
356 listbox_get_current (l_hotlist
, NULL
, (void **) &item
);
357 init_movelist (item
);
358 hotlist_state
.moving
= TRUE
;
359 ret
= run_dlg (movelist_dlg
);
360 hotlist_state
.moving
= FALSE
;
361 listbox_get_current (l_movelist
, NULL
, (void **) &moveto_item
);
362 moveto_group
= current_group
;
363 destroy_dlg (movelist_dlg
);
364 current_group
= saved
;
367 if (moveto_item
== item
)
368 return 0; /* If we insert/append a before/after a
369 it hardly changes anything ;) */
371 listbox_remove_current (l_hotlist
);
372 item
->up
= moveto_group
;
373 if (moveto_group
->head
== NULL
)
374 moveto_group
->head
= item
;
375 else if (moveto_item
== NULL
)
376 { /* we have group with just comments */
377 struct hotlist
*p
= moveto_group
->head
;
380 while (p
->next
!= NULL
)
384 else if (ret
== B_ENTER
|| ret
== B_APPEND
)
386 if (moveto_item
->next
== NULL
)
387 moveto_item
->next
= item
;
390 item
->next
= moveto_item
->next
;
391 moveto_item
->next
= item
;
394 else if (moveto_group
->head
== moveto_item
)
396 moveto_group
->head
= item
;
397 item
->next
= moveto_item
;
401 struct hotlist
*p
= moveto_group
->head
;
403 while (p
->next
!= moveto_item
)
405 item
->next
= p
->next
;
408 listbox_remove_list (l_hotlist
);
409 fill_listbox (l_hotlist
);
411 hotlist_state
.modified
= TRUE
;
416 struct hotlist
*entry
= NULL
;
418 listbox_get_current (l_hotlist
, NULL
, (void **) &entry
);
419 remove_from_hotlist (entry
);
424 add_new_group_cmd ();
432 add_new_entry_cmd ();
441 list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
442 listbox_get_current (list
, NULL
, &data
);
447 hlp
= (struct hotlist
*) data
;
449 if (hlp
->type
== HL_TYPE_ENTRY
)
451 if (hlp
->type
!= HL_TYPE_DOTDOT
)
453 listbox_remove_list (list
);
458 /* Fall through - go up */
460 /* Fall through if list empty - just go up */
464 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
466 listbox_remove_list (list
);
467 current_group
= current_group
->up
;
478 listbox_remove_list (l_hotlist
);
479 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), 0);
480 vfs_fill_names (add_name_to_list
);
482 #endif /* ENABLE_VFS */
489 /* --------------------------------------------------------------------------------------------- */
491 static inline cb_ret_t
492 hotlist_handle_key (Dlg_head
* h
, int key
)
496 case KEY_M_CTRL
| '\n':
502 if (hotlist_button_callback (NULL
, B_ENTER
) != 0)
504 h
->ret_value
= B_ENTER
;
510 if (hotlist_state
.type
== LIST_VFSLIST
)
511 return MSG_NOT_HANDLED
;
512 return hotlist_button_callback (NULL
, B_UP_GROUP
) == 0 ? MSG_HANDLED
: MSG_NOT_HANDLED
;
515 if (hotlist_state
.moving
)
516 return MSG_NOT_HANDLED
;
517 hotlist_button_callback (NULL
, B_REMOVE
);
523 if (!hotlist_state
.moving
)
527 listbox_get_current (l_hotlist
, NULL
, &ldata
);
531 struct hotlist
*hlp
= (struct hotlist
*) ldata
;
533 if (hlp
->type
== HL_TYPE_ENTRY
)
537 tmp
= g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
538 input_insert (cmdline
, tmp
, FALSE
);
540 h
->ret_value
= B_CANCEL
;
545 return MSG_HANDLED
; /* ignore key */
548 return MSG_NOT_HANDLED
;
552 /* --------------------------------------------------------------------------------------------- */
555 hotlist_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
559 case DLG_UNHANDLED_KEY
:
560 return hotlist_handle_key (h
, parm
);
563 dlg_select_widget (h
== hotlist_dlg
? l_hotlist
: l_movelist
);
564 /* always stay on hotlist */
572 /* simply call dlg_set_size() with new size */
573 dlg_set_size (h
, LINES
- (h
== hotlist_dlg
? 2 : 6), COLS
- 6);
577 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
581 /* --------------------------------------------------------------------------------------------- */
584 hotlist_listbox_callback (WListbox
* list
)
586 Dlg_head
*dlg
= WIDGET (list
)->owner
;
588 if (list
->count
!= 0)
592 listbox_get_current (list
, NULL
, &data
);
596 struct hotlist
*hlp
= (struct hotlist
*) data
;
598 if (hlp
->type
== HL_TYPE_ENTRY
)
600 dlg
->ret_value
= B_ENTER
;
606 hotlist_button_callback (NULL
, B_ENTER
);
607 hotlist_callback (dlg
, NULL
, DLG_POST_KEY
, '\n', NULL
);
613 dlg
->ret_value
= B_ENTER
;
619 hotlist_button_callback (NULL
, B_UP_GROUP
);
620 hotlist_callback (dlg
, NULL
, DLG_POST_KEY
, 'u', NULL
);
624 /* --------------------------------------------------------------------------------------------- */
626 * Expands all button names (once) and recalculates button positions.
627 * returns number of columns in the dialog box, which is 10 chars longer
630 * If common width of the window (i.e. in xterm) is less than returned
631 * width - sorry :) (anyway this did not handled in previous version too)
635 init_i18n_stuff (int list_type
, int cols
)
640 static gboolean i18n_flag
= FALSE
;
644 for (i
= 0; i
< hotlist_but_num
; i
++)
646 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
647 hotlist_but
[i
].len
= str_term_width1 (hotlist_but
[i
].text
) + 3;
648 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
649 hotlist_but
[i
].len
+= 2;
654 #endif /* ENABLE_NLS */
656 /* Dynamic resizing of buttonbars */
658 int len
[2], count
[2]; /* at most two lines of buttons */
662 count
[0] = count
[1] = 0;
663 cur_x
[0] = cur_x
[1] = 0;
665 /* Count len of buttonbars, assuming 1 extra space between buttons */
666 for (i
= 0; i
< hotlist_but_num
; i
++)
667 if ((hotlist_but
[i
].type
& list_type
) != 0)
671 row
= hotlist_but
[i
].y
;
673 len
[row
] += hotlist_but
[i
].len
+ 1;
679 cols
= max (cols
, max (len
[0], len
[1]));
681 /* arrange buttons */
682 for (i
= 0; i
< hotlist_but_num
; i
++)
683 if ((hotlist_but
[i
].type
& list_type
) != 0)
687 row
= hotlist_but
[i
].y
;
689 if (hotlist_but
[i
].x
!= 0)
691 /* not first int the row */
692 if (hotlist_but
[i
].ret_cmd
== B_CANCEL
)
693 hotlist_but
[i
].x
= cols
- hotlist_but
[i
].len
- 6;
695 hotlist_but
[i
].x
= cur_x
[row
];
698 cur_x
[row
] += hotlist_but
[i
].len
+ 1;
705 /* --------------------------------------------------------------------------------------------- */
708 init_hotlist (hotlist_t list_type
)
711 const char *title
, *help_node
;
716 Widget
*hotlist_widget
;
721 cols
= init_i18n_stuff (list_type
, COLS
- 6);
723 hotlist_state
.expanded
=
724 mc_config_get_int (mc_main_config
, "HotlistConfig", "expanded_view_of_groups", 0);
727 if (list_type
== LIST_VFSLIST
)
729 title
= _("Active VFS directories");
730 help_node
= "[vfshot]"; /* FIXME - no such node */
734 #endif /* !ENABLE_VFS */
736 title
= _("Directory hotlist");
737 help_node
= "[Hotlist]";
741 create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, hotlist_callback
, NULL
, help_node
,
745 hotlist_group
= groupbox_new (y
, UX
, lines
- 10 + dh
, cols
- 2 * UX
, _("Top level group"));
746 hotlist_widget
= WIDGET (hotlist_group
);
747 add_widget_autopos (hotlist_dlg
, hotlist_widget
, WPOS_KEEP_ALL
, NULL
);
750 listbox_new (y
+ 1, UX
+ 1, hotlist_widget
->lines
- 2, hotlist_widget
->cols
- 2, FALSE
,
751 hotlist_listbox_callback
);
753 /* Fill the hotlist with the active VFS or the hotlist */
755 if (list_type
== LIST_VFSLIST
)
757 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), 0);
758 vfs_fill_names (add_name_to_list
);
761 #endif /* !ENABLE_VFS */
762 fill_listbox (l_hotlist
);
764 /* insert before groupbox to view scrollbar */
765 add_widget_autopos (hotlist_dlg
, l_hotlist
, WPOS_KEEP_ALL
, NULL
);
767 y
+= hotlist_widget
->lines
;
769 path_box
= groupbox_new (y
, UX
, 3, hotlist_widget
->cols
, _("Directory path"));
770 add_widget_autopos (hotlist_dlg
, path_box
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_HORZ
, NULL
);
772 pname
= label_new (y
+ 1, UX
+ 2, "");
773 add_widget_autopos (hotlist_dlg
, pname
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
, NULL
);
774 y
+= WIDGET (path_box
)->lines
;
776 add_widget_autopos (hotlist_dlg
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
778 for (i
= 0; i
< hotlist_but_num
; i
++)
779 if ((hotlist_but
[i
].type
& list_type
) != 0)
780 add_widget_autopos (hotlist_dlg
,
781 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
782 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
783 hotlist_but
[i
].text
, hotlist_button_callback
),
784 hotlist_but
[i
].pos_flags
, NULL
);
786 dlg_select_widget (l_hotlist
);
789 /* --------------------------------------------------------------------------------------------- */
792 init_movelist ( struct hotlist
*item
)
798 Widget
*movelist_widget
;
803 cols
= init_i18n_stuff (LIST_MOVELIST
, COLS
- 6);
805 hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
808 create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, hotlist_callback
, NULL
, "[Hotlist]",
814 movelist_group
= groupbox_new (y
, UX
, lines
- 7, cols
- 2 * UX
, _("Directory label"));
815 movelist_widget
= WIDGET (movelist_group
);
816 add_widget_autopos (movelist_dlg
, movelist_widget
, WPOS_KEEP_ALL
, NULL
);
819 listbox_new (y
+ 1, UX
+ 1, movelist_widget
->lines
- 2, movelist_widget
->cols
- 2, FALSE
,
820 hotlist_listbox_callback
);
821 fill_listbox (l_movelist
);
822 /* insert before groupbox to view scrollbar */
823 add_widget_autopos (movelist_dlg
, l_movelist
, WPOS_KEEP_ALL
, NULL
);
825 y
+= movelist_widget
->lines
;
827 add_widget_autopos (movelist_dlg
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
829 for (i
= 0; i
< hotlist_but_num
; i
++)
830 if ((hotlist_but
[i
].type
& LIST_MOVELIST
) != 0)
831 add_widget_autopos (movelist_dlg
,
832 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
833 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
834 hotlist_but
[i
].text
, hotlist_button_callback
),
835 hotlist_but
[i
].pos_flags
, NULL
);
837 dlg_select_widget (l_movelist
);
840 /* --------------------------------------------------------------------------------------------- */
842 * Destroy the list dialog.
843 * Don't confuse with done_hotlist() for the list in memory.
849 destroy_dlg (hotlist_dlg
);
852 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
856 /* --------------------------------------------------------------------------------------------- */
859 find_group_section (struct hotlist
*grp
)
861 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
864 /* --------------------------------------------------------------------------------------------- */
866 static struct hotlist
*
867 add2hotlist (char *label
, char *directory
, enum HotListType type
, listbox_append_t pos
)
870 struct hotlist
*current
= NULL
;
873 * Hotlist is neither loaded nor loading.
874 * Must be called by "Ctrl-x a" before using hotlist.
876 if (current_group
== NULL
)
879 listbox_get_current (l_hotlist
, NULL
, (void **) ¤t
);
881 /* Make sure `..' stays at the top of the list. */
882 if ((current
!= NULL
) && (current
->type
== HL_TYPE_DOTDOT
))
883 pos
= LISTBOX_APPEND_AFTER
;
885 new = g_new0 (struct hotlist
, 1);
889 new->directory
= directory
;
890 new->up
= current_group
;
892 if (type
== HL_TYPE_GROUP
)
895 add_dotdot_to_list ();
896 current_group
= new->up
;
899 if (current_group
->head
== NULL
)
901 /* first element in group */
902 current_group
->head
= new;
904 else if (pos
== LISTBOX_APPEND_AFTER
)
906 new->next
= current
->next
;
909 else if (pos
== LISTBOX_APPEND_BEFORE
&& current
== current_group
->head
)
911 /* should be inserted before first item */
913 current_group
->head
= new;
915 else if (pos
== LISTBOX_APPEND_BEFORE
)
917 struct hotlist
*p
= current_group
->head
;
919 while (p
->next
!= current
)
926 { /* append at the end */
927 struct hotlist
*p
= current_group
->head
;
929 while (p
->next
!= NULL
)
935 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&& type
!= HL_TYPE_DOTDOT
)
937 if (type
== HL_TYPE_GROUP
)
941 lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
942 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
946 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
947 listbox_select_entry (l_hotlist
, l_hotlist
->pos
);
953 /* --------------------------------------------------------------------------------------------- */
956 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
957 const char *help
, char **r1
, char **r2
)
959 quick_widget_t quick_widgets
[] = {
961 QUICK_LABELED_INPUT (text1
, input_label_above
, *r1
, 0, "input-lbl", r1
, NULL
),
962 QUICK_SEPARATOR (FALSE
),
963 QUICK_LABELED_INPUT (text2
, input_label_above
, *r2
, 0, "input-lbl", r2
, NULL
),
964 QUICK_START_BUTTONS (TRUE
, TRUE
),
965 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
966 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
967 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
972 quick_dialog_t qdlg
= {
975 quick_widgets
, NULL
, NULL
980 ret
= quick_dialog (&qdlg
);
982 return (ret
!= B_CANCEL
) ? ret
: 0;
985 /* --------------------------------------------------------------------------------------------- */
988 add_new_entry_cmd (void)
990 char *title
, *url
, *to_free
;
993 /* Take current directory as default value for input fields */
994 to_free
= title
= url
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
996 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
997 _("Directory path:"), "[Hotlist]", &title
, &url
);
1002 if (title
== NULL
|| *title
== '\0' || url
== NULL
|| *url
== '\0')
1009 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1010 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AFTER
);
1012 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_BEFORE
);
1014 hotlist_state
.modified
= TRUE
;
1017 /* --------------------------------------------------------------------------------------------- */
1020 add_new_group_input (const char *header
, const char *label
, char **result
)
1022 quick_widget_t quick_widgets
[] = {
1024 QUICK_LABELED_INPUT (label
, input_label_above
, "", 0, "input", result
, NULL
),
1025 QUICK_START_BUTTONS (TRUE
, TRUE
),
1026 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
1027 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
1028 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
1033 quick_dialog_t qdlg
= {
1035 header
, "[Hotlist]",
1036 quick_widgets
, NULL
, NULL
1041 ret
= quick_dialog (&qdlg
);
1043 return (ret
!= B_CANCEL
) ? ret
: 0;
1046 /* --------------------------------------------------------------------------------------------- */
1049 add_new_group_cmd (void)
1054 ret
= add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label
);
1055 if (ret
== 0 || label
== NULL
|| *label
== '\0')
1058 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1059 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AFTER
);
1061 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_BEFORE
);
1063 hotlist_state
.modified
= TRUE
;
1066 /* --------------------------------------------------------------------------------------------- */
1069 remove_group (struct hotlist
*grp
)
1071 struct hotlist
*current
= grp
->head
;
1073 while (current
!= NULL
)
1075 struct hotlist
*next
= current
->next
;
1077 if (current
->type
== HL_TYPE_GROUP
)
1078 remove_group (current
);
1080 g_free (current
->label
);
1081 g_free (current
->directory
);
1088 /* --------------------------------------------------------------------------------------------- */
1091 remove_from_hotlist (struct hotlist
*entry
)
1096 if (entry
->type
== HL_TYPE_DOTDOT
)
1099 if (confirm_directory_hotlist_delete
)
1101 char text
[BUF_MEDIUM
];
1107 g_snprintf (text
, sizeof (text
), _("Are you sure you want to remove entry \"%s\"?"),
1108 str_trunc (entry
->label
, 30));
1109 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1110 _("&Yes"), _("&No"));
1115 if (entry
->type
== HL_TYPE_GROUP
)
1117 struct hotlist
*head
= entry
->head
;
1119 if (head
!= NULL
&& (head
->type
!= HL_TYPE_DOTDOT
|| head
->next
!= NULL
))
1121 char text
[BUF_MEDIUM
];
1124 g_snprintf (text
, sizeof (text
), _("Group \"%s\" is not empty.\nRemove it?"),
1125 str_trunc (entry
->label
, 30));
1126 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1127 _("&Yes"), _("&No"));
1132 remove_group (entry
);
1135 unlink_entry (entry
);
1137 g_free (entry
->label
);
1138 g_free (entry
->directory
);
1140 /* now remove list entry from screen */
1141 listbox_remove_current (l_hotlist
);
1142 hotlist_state
.modified
= TRUE
;
1145 /* --------------------------------------------------------------------------------------------- */
1148 load_group (struct hotlist
*grp
)
1150 gchar
**profile_keys
, **keys
;
1152 char *group_section
;
1153 struct hotlist
*current
= 0;
1155 group_section
= find_group_section (grp
);
1157 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, group_section
, &len
);
1159 current_group
= grp
;
1161 while (*profile_keys
!= NULL
)
1163 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1164 g_strdup (*profile_keys
), HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1167 g_free (group_section
);
1170 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp
->directory
, &len
);
1172 while (*profile_keys
!= NULL
)
1174 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1175 g_strdup (*profile_keys
), HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1180 for (current
= grp
->head
; current
; current
= current
->next
)
1181 load_group (current
);
1184 /* --------------------------------------------------------------------------------------------- */
1187 hot_skip_blanks (void)
1191 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1196 /* --------------------------------------------------------------------------------------------- */
1199 hot_next_token (void)
1204 if (tkn_buf
== NULL
)
1205 tkn_buf
= g_string_new ("");
1206 g_string_set_size (tkn_buf
, 0);
1209 c
= hot_skip_blanks ();
1219 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n')
1220 g_string_append_c (tkn_buf
, c
);
1224 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"')
1228 c
= getc (hotlist_file
);
1231 g_string_free (tkn_buf
, TRUE
);
1235 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1237 ret
= (c
== EOF
) ? TKN_EOF
: TKN_STRING
;
1240 c
= getc (hotlist_file
);
1243 g_string_free (tkn_buf
, TRUE
);
1249 /* fall through; it is taken as normal character */
1254 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1256 while ((c
= fgetc (hotlist_file
)) != EOF
&& (g_ascii_isalnum (c
) || !isascii (c
)));
1258 ungetc (c
, hotlist_file
);
1260 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1262 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1264 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1266 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1275 /* --------------------------------------------------------------------------------------------- */
1278 hot_load_group (struct hotlist
*grp
)
1281 struct hotlist
*new_grp
;
1284 current_group
= grp
;
1286 while ((tkn
= hot_next_token ()) != TKN_ENDGROUP
)
1290 CHECK_TOKEN (TKN_STRING
);
1292 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1293 LISTBOX_APPEND_AT_END
);
1295 hot_load_group (new_grp
);
1296 current_group
= grp
;
1300 CHECK_TOKEN (TKN_STRING
);
1301 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1302 CHECK_TOKEN (TKN_URL
);
1303 CHECK_TOKEN (TKN_STRING
);
1304 url
= tilde_expand (tkn_buf
->str
);
1305 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1310 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1311 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1314 hotlist_state
.readonly
= TRUE
;
1315 hotlist_state
.file_error
= TRUE
;
1318 /* skip empty lines */
1321 hotlist_state
.readonly
= TRUE
;
1322 hotlist_state
.file_error
= TRUE
;
1329 /* --------------------------------------------------------------------------------------------- */
1332 hot_load_file (struct hotlist
*grp
)
1335 struct hotlist
*new_grp
;
1338 current_group
= grp
;
1340 while ((tkn
= hot_next_token ()) != TKN_EOF
)
1344 CHECK_TOKEN (TKN_STRING
);
1346 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1347 LISTBOX_APPEND_AT_END
);
1349 hot_load_group (new_grp
);
1350 current_group
= grp
;
1354 CHECK_TOKEN (TKN_STRING
);
1355 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1356 CHECK_TOKEN (TKN_URL
);
1357 CHECK_TOKEN (TKN_STRING
);
1358 url
= tilde_expand (tkn_buf
->str
);
1359 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1364 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1365 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1368 /* skip empty lines */
1371 hotlist_state
.readonly
= TRUE
;
1372 hotlist_state
.file_error
= TRUE
;
1378 /* --------------------------------------------------------------------------------------------- */
1381 clean_up_hotlist_groups (const char *section
)
1384 gchar
**profile_keys
, **keys
;
1387 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1388 if (mc_config_has_group (mc_main_config
, section
))
1389 mc_config_del_group (mc_main_config
, section
);
1391 if (mc_config_has_group (mc_main_config
, grp_section
))
1393 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp_section
, &len
);
1395 while (*profile_keys
!= NULL
)
1397 clean_up_hotlist_groups (*profile_keys
);
1401 mc_config_del_group (mc_main_config
, grp_section
);
1403 g_free (grp_section
);
1406 /* --------------------------------------------------------------------------------------------- */
1411 gboolean remove_old_list
= FALSE
;
1412 struct stat stat_buf
;
1414 if (hotlist_state
.loaded
)
1416 stat (hotlist_file_name
, &stat_buf
);
1417 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1423 if (hotlist_file_name
== NULL
)
1424 hotlist_file_name
= mc_config_get_full_path (MC_HOTLIST_FILE
);
1426 hotlist
= g_new0 (struct hotlist
, 1);
1427 hotlist
->type
= HL_TYPE_GROUP
;
1428 hotlist
->label
= g_strdup (_("Top level group"));
1429 hotlist
->up
= hotlist
;
1433 hotlist
->directory
= g_strdup ("Hotlist");
1435 hotlist_file
= fopen (hotlist_file_name
, "r");
1436 if (hotlist_file
== NULL
)
1440 load_group (hotlist
);
1441 hotlist_state
.loaded
= TRUE
;
1443 * just to be sure we got copy
1445 hotlist_state
.modified
= TRUE
;
1446 result
= save_hotlist ();
1447 hotlist_state
.modified
= FALSE
;
1449 remove_old_list
= TRUE
;
1451 message (D_ERROR
, _("Hotlist Load"),
1453 ("MC was unable to write %s file,\nyour old hotlist entries were not deleted"),
1454 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE
);
1458 hot_load_file (hotlist
);
1459 fclose (hotlist_file
);
1460 hotlist_state
.loaded
= TRUE
;
1463 if (remove_old_list
)
1465 GError
*error
= NULL
;
1467 clean_up_hotlist_groups ("Hotlist");
1468 if (!mc_config_save_file (mc_main_config
, &error
))
1469 setup_save_config_show_error (mc_main_config
->ini_path
, &error
);
1472 stat (hotlist_file_name
, &stat_buf
);
1473 hotlist_file_mtime
= stat_buf
.st_mtime
;
1474 current_group
= hotlist
;
1477 /* --------------------------------------------------------------------------------------------- */
1480 hot_save_group (struct hotlist
*grp
)
1482 struct hotlist
*current
;
1488 for (i = 0; i < n; i++) \
1489 putc (' ', hotlist_file); \
1492 for (current
= grp
->head
; current
!= NULL
; current
= current
->next
)
1493 switch (current
->type
)
1496 INDENT (list_level
);
1497 fputs ("GROUP \"", hotlist_file
);
1498 for (s
= current
->label
; *s
!= '\0'; s
++)
1500 if (*s
== '"' || *s
== '\\')
1501 putc ('\\', hotlist_file
);
1502 putc (*s
, hotlist_file
);
1504 fputs ("\"\n", hotlist_file
);
1506 hot_save_group (current
);
1508 INDENT (list_level
);
1509 fputs ("ENDGROUP\n", hotlist_file
);
1512 INDENT (list_level
);
1513 fputs ("ENTRY \"", hotlist_file
);
1514 for (s
= current
->label
; *s
!= '\0'; s
++)
1516 if (*s
== '"' || *s
== '\\')
1517 putc ('\\', hotlist_file
);
1518 putc (*s
, hotlist_file
);
1520 fputs ("\" URL \"", hotlist_file
);
1521 for (s
= current
->directory
; *s
!= '\0'; s
++)
1523 if (*s
== '"' || *s
== '\\')
1524 putc ('\\', hotlist_file
);
1525 putc (*s
, hotlist_file
);
1527 fputs ("\"\n", hotlist_file
);
1529 case HL_TYPE_COMMENT
:
1530 fprintf (hotlist_file
, "#%s\n", current
->label
);
1532 case HL_TYPE_DOTDOT
:
1538 /* --------------------------------------------------------------------------------------------- */
1541 add_dotdot_to_list (void)
1543 if (current_group
!= hotlist
&& hotlist_has_dot_dot
)
1544 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, LISTBOX_APPEND_AT_END
);
1547 /* --------------------------------------------------------------------------------------------- */
1548 /*** public functions ****************************************************************************/
1549 /* --------------------------------------------------------------------------------------------- */
1552 add2hotlist_cmd (void)
1555 const char *cp
= N_("Label for \"%s\":");
1557 char *label_string
, *label
;
1563 l
= str_term_width1 (cp
);
1564 label_string
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1565 lc_prompt
= g_strdup_printf (cp
, str_trunc (label_string
, COLS
- 2 * UX
- (l
+ 8)));
1566 label
= input_dialog (_("Add to hotlist"), lc_prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1569 if (label
== NULL
|| *label
== '\0')
1571 g_free (label_string
);
1576 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1577 hotlist_state
.modified
= TRUE
;
1581 /* --------------------------------------------------------------------------------------------- */
1584 hotlist_show (hotlist_t list_type
)
1586 char *target
= NULL
;
1589 hotlist_state
.type
= list_type
;
1592 init_hotlist (list_type
);
1594 /* display file info */
1595 tty_setcolor (SELECTED_COLOR
);
1597 hotlist_state
.running
= TRUE
;
1598 res
= run_dlg (hotlist_dlg
);
1599 hotlist_state
.running
= FALSE
;
1605 struct hotlist
*hlp
= NULL
;
1607 listbox_get_current (l_hotlist
, &text
, (void **) &hlp
);
1608 target
= g_strdup (hlp
!= NULL
? hlp
->directory
: text
);
1615 /* --------------------------------------------------------------------------------------------- */
1620 gboolean saved
= FALSE
;
1621 struct stat stat_buf
;
1623 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
!= NULL
)
1625 mc_util_make_backup_if_possible (hotlist_file_name
, ".bak");
1627 hotlist_file
= fopen (hotlist_file_name
, "w");
1628 if (hotlist_file
== NULL
)
1629 mc_util_restore_from_backup_if_possible (hotlist_file_name
, ".bak");
1632 hot_save_group (hotlist
);
1633 fclose (hotlist_file
);
1634 stat (hotlist_file_name
, &stat_buf
);
1635 hotlist_file_mtime
= stat_buf
.st_mtime
;
1636 hotlist_state
.modified
= FALSE
;
1644 /* --------------------------------------------------------------------------------------------- */
1646 * Unload list from memory.
1647 * Don't confuse with hotlist_done() for GUI.
1653 if (hotlist
!= NULL
)
1655 remove_group (hotlist
);
1656 g_free (hotlist
->label
);
1657 g_free (hotlist
->directory
);
1662 hotlist_state
.loaded
= FALSE
;
1664 g_free (hotlist_file_name
);
1665 hotlist_file_name
= NULL
;
1667 current_group
= NULL
;
1669 if (tkn_buf
!= NULL
)
1671 g_string_free (tkn_buf
, TRUE
);
1676 /* --------------------------------------------------------------------------------------------- */