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 WDialog
*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 (WDialog
* 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 (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
557 WDialog
*h
= DIALOG (w
);
561 case MSG_UNHANDLED_KEY
:
562 return hotlist_handle_key (h
, parm
);
565 dlg_select_widget (h
== hotlist_dlg
? l_hotlist
: l_movelist
);
566 /* always stay on hotlist */
574 /* simply call dlg_set_size() with new size */
575 dlg_set_size (h
, LINES
- (h
== hotlist_dlg
? 2 : 6), COLS
- 6);
579 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
583 /* --------------------------------------------------------------------------------------------- */
586 hotlist_listbox_callback (WListbox
* list
)
588 WDialog
*dlg
= WIDGET (list
)->owner
;
590 if (list
->count
!= 0)
594 listbox_get_current (list
, NULL
, &data
);
598 struct hotlist
*hlp
= (struct hotlist
*) data
;
600 if (hlp
->type
== HL_TYPE_ENTRY
)
602 dlg
->ret_value
= B_ENTER
;
608 hotlist_button_callback (NULL
, B_ENTER
);
609 send_message (dlg
, NULL
, MSG_POST_KEY
, '\n', NULL
);
615 dlg
->ret_value
= B_ENTER
;
621 hotlist_button_callback (NULL
, B_UP_GROUP
);
622 send_message (dlg
, NULL
, MSG_POST_KEY
, 'u', NULL
);
626 /* --------------------------------------------------------------------------------------------- */
628 * Expands all button names (once) and recalculates button positions.
629 * returns number of columns in the dialog box, which is 10 chars longer
632 * If common width of the window (i.e. in xterm) is less than returned
633 * width - sorry :) (anyway this did not handled in previous version too)
637 init_i18n_stuff (int list_type
, int cols
)
642 static gboolean i18n_flag
= FALSE
;
646 for (i
= 0; i
< hotlist_but_num
; i
++)
648 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
649 hotlist_but
[i
].len
= str_term_width1 (hotlist_but
[i
].text
) + 3;
650 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
651 hotlist_but
[i
].len
+= 2;
656 #endif /* ENABLE_NLS */
658 /* Dynamic resizing of buttonbars */
660 int len
[2], count
[2]; /* at most two lines of buttons */
664 count
[0] = count
[1] = 0;
665 cur_x
[0] = cur_x
[1] = 0;
667 /* Count len of buttonbars, assuming 1 extra space between buttons */
668 for (i
= 0; i
< hotlist_but_num
; i
++)
669 if ((hotlist_but
[i
].type
& list_type
) != 0)
673 row
= hotlist_but
[i
].y
;
675 len
[row
] += hotlist_but
[i
].len
+ 1;
681 cols
= max (cols
, max (len
[0], len
[1]));
683 /* arrange buttons */
684 for (i
= 0; i
< hotlist_but_num
; i
++)
685 if ((hotlist_but
[i
].type
& list_type
) != 0)
689 row
= hotlist_but
[i
].y
;
691 if (hotlist_but
[i
].x
!= 0)
693 /* not first int the row */
694 if (hotlist_but
[i
].ret_cmd
== B_CANCEL
)
695 hotlist_but
[i
].x
= cols
- hotlist_but
[i
].len
- 6;
697 hotlist_but
[i
].x
= cur_x
[row
];
700 cur_x
[row
] += hotlist_but
[i
].len
+ 1;
707 /* --------------------------------------------------------------------------------------------- */
710 init_hotlist (hotlist_t list_type
)
713 const char *title
, *help_node
;
718 Widget
*hotlist_widget
;
723 cols
= init_i18n_stuff (list_type
, COLS
- 6);
725 hotlist_state
.expanded
=
726 mc_config_get_int (mc_main_config
, "HotlistConfig", "expanded_view_of_groups", 0);
729 if (list_type
== LIST_VFSLIST
)
731 title
= _("Active VFS directories");
732 help_node
= "[vfshot]"; /* FIXME - no such node */
736 #endif /* !ENABLE_VFS */
738 title
= _("Directory hotlist");
739 help_node
= "[Hotlist]";
743 create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, hotlist_callback
, NULL
, help_node
,
747 hotlist_group
= groupbox_new (y
, UX
, lines
- 10 + dh
, cols
- 2 * UX
, _("Top level group"));
748 hotlist_widget
= WIDGET (hotlist_group
);
749 add_widget_autopos (hotlist_dlg
, hotlist_widget
, WPOS_KEEP_ALL
, NULL
);
752 listbox_new (y
+ 1, UX
+ 1, hotlist_widget
->lines
- 2, hotlist_widget
->cols
- 2, FALSE
,
753 hotlist_listbox_callback
);
755 /* Fill the hotlist with the active VFS or the hotlist */
757 if (list_type
== LIST_VFSLIST
)
759 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), 0);
760 vfs_fill_names (add_name_to_list
);
763 #endif /* !ENABLE_VFS */
764 fill_listbox (l_hotlist
);
766 /* insert before groupbox to view scrollbar */
767 add_widget_autopos (hotlist_dlg
, l_hotlist
, WPOS_KEEP_ALL
, NULL
);
769 y
+= hotlist_widget
->lines
;
771 path_box
= groupbox_new (y
, UX
, 3, hotlist_widget
->cols
, _("Directory path"));
772 add_widget_autopos (hotlist_dlg
, path_box
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_HORZ
, NULL
);
774 pname
= label_new (y
+ 1, UX
+ 2, "");
775 add_widget_autopos (hotlist_dlg
, pname
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
, NULL
);
776 y
+= WIDGET (path_box
)->lines
;
778 add_widget_autopos (hotlist_dlg
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
780 for (i
= 0; i
< hotlist_but_num
; i
++)
781 if ((hotlist_but
[i
].type
& list_type
) != 0)
782 add_widget_autopos (hotlist_dlg
,
783 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
784 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
785 hotlist_but
[i
].text
, hotlist_button_callback
),
786 hotlist_but
[i
].pos_flags
, NULL
);
788 dlg_select_widget (l_hotlist
);
791 /* --------------------------------------------------------------------------------------------- */
794 init_movelist (struct hotlist
*item
)
800 Widget
*movelist_widget
;
805 cols
= init_i18n_stuff (LIST_MOVELIST
, COLS
- 6);
807 hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
810 create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, hotlist_callback
, NULL
, "[Hotlist]",
816 movelist_group
= groupbox_new (y
, UX
, lines
- 7, cols
- 2 * UX
, _("Directory label"));
817 movelist_widget
= WIDGET (movelist_group
);
818 add_widget_autopos (movelist_dlg
, movelist_widget
, WPOS_KEEP_ALL
, NULL
);
821 listbox_new (y
+ 1, UX
+ 1, movelist_widget
->lines
- 2, movelist_widget
->cols
- 2, FALSE
,
822 hotlist_listbox_callback
);
823 fill_listbox (l_movelist
);
824 /* insert before groupbox to view scrollbar */
825 add_widget_autopos (movelist_dlg
, l_movelist
, WPOS_KEEP_ALL
, NULL
);
827 y
+= movelist_widget
->lines
;
829 add_widget_autopos (movelist_dlg
, hline_new (y
++, -1, -1), WPOS_KEEP_BOTTOM
, NULL
);
831 for (i
= 0; i
< hotlist_but_num
; i
++)
832 if ((hotlist_but
[i
].type
& LIST_MOVELIST
) != 0)
833 add_widget_autopos (movelist_dlg
,
834 button_new (y
+ hotlist_but
[i
].y
, UX
+ hotlist_but
[i
].x
,
835 hotlist_but
[i
].ret_cmd
, hotlist_but
[i
].flags
,
836 hotlist_but
[i
].text
, hotlist_button_callback
),
837 hotlist_but
[i
].pos_flags
, NULL
);
839 dlg_select_widget (l_movelist
);
842 /* --------------------------------------------------------------------------------------------- */
844 * Destroy the list dialog.
845 * Don't confuse with done_hotlist() for the list in memory.
851 destroy_dlg (hotlist_dlg
);
854 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
858 /* --------------------------------------------------------------------------------------------- */
861 find_group_section (struct hotlist
*grp
)
863 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
866 /* --------------------------------------------------------------------------------------------- */
868 static struct hotlist
*
869 add2hotlist (char *label
, char *directory
, enum HotListType type
, listbox_append_t pos
)
872 struct hotlist
*current
= NULL
;
875 * Hotlist is neither loaded nor loading.
876 * Must be called by "Ctrl-x a" before using hotlist.
878 if (current_group
== NULL
)
881 listbox_get_current (l_hotlist
, NULL
, (void **) ¤t
);
883 /* Make sure `..' stays at the top of the list. */
884 if ((current
!= NULL
) && (current
->type
== HL_TYPE_DOTDOT
))
885 pos
= LISTBOX_APPEND_AFTER
;
887 new = g_new0 (struct hotlist
, 1);
891 new->directory
= directory
;
892 new->up
= current_group
;
894 if (type
== HL_TYPE_GROUP
)
897 add_dotdot_to_list ();
898 current_group
= new->up
;
901 if (current_group
->head
== NULL
)
903 /* first element in group */
904 current_group
->head
= new;
906 else if (pos
== LISTBOX_APPEND_AFTER
)
908 new->next
= current
->next
;
911 else if (pos
== LISTBOX_APPEND_BEFORE
&& current
== current_group
->head
)
913 /* should be inserted before first item */
915 current_group
->head
= new;
917 else if (pos
== LISTBOX_APPEND_BEFORE
)
919 struct hotlist
*p
= current_group
->head
;
921 while (p
->next
!= current
)
928 { /* append at the end */
929 struct hotlist
*p
= current_group
->head
;
931 while (p
->next
!= NULL
)
937 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&& type
!= HL_TYPE_DOTDOT
)
939 if (type
== HL_TYPE_GROUP
)
943 lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
944 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
948 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
949 listbox_select_entry (l_hotlist
, l_hotlist
->pos
);
955 /* --------------------------------------------------------------------------------------------- */
958 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
959 const char *help
, char **r1
, char **r2
)
961 quick_widget_t quick_widgets
[] = {
963 QUICK_LABELED_INPUT (text1
, input_label_above
, *r1
, "input-lbl", r1
, NULL
,
964 FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
965 QUICK_SEPARATOR (FALSE
),
966 QUICK_LABELED_INPUT (text2
, input_label_above
, *r2
, "input-lbl", r2
, NULL
,
967 FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
),
968 QUICK_START_BUTTONS (TRUE
, TRUE
),
969 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
970 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
971 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
976 quick_dialog_t qdlg
= {
979 quick_widgets
, NULL
, NULL
984 ret
= quick_dialog (&qdlg
);
986 return (ret
!= B_CANCEL
) ? ret
: 0;
989 /* --------------------------------------------------------------------------------------------- */
992 add_new_entry_cmd (void)
994 char *title
, *url
, *to_free
;
997 /* Take current directory as default value for input fields */
998 to_free
= title
= url
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1000 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1001 _("Directory path:"), "[Hotlist]", &title
, &url
);
1006 if (title
== NULL
|| *title
== '\0' || url
== NULL
|| *url
== '\0')
1013 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1014 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AFTER
);
1016 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_BEFORE
);
1018 hotlist_state
.modified
= TRUE
;
1021 /* --------------------------------------------------------------------------------------------- */
1024 add_new_group_input (const char *header
, const char *label
, char **result
)
1026 quick_widget_t quick_widgets
[] = {
1028 QUICK_LABELED_INPUT (label
, input_label_above
, "", "input", result
, NULL
,
1029 FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1030 QUICK_START_BUTTONS (TRUE
, TRUE
),
1031 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
1032 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
1033 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
1038 quick_dialog_t qdlg
= {
1040 header
, "[Hotlist]",
1041 quick_widgets
, NULL
, NULL
1046 ret
= quick_dialog (&qdlg
);
1048 return (ret
!= B_CANCEL
) ? ret
: 0;
1051 /* --------------------------------------------------------------------------------------------- */
1054 add_new_group_cmd (void)
1059 ret
= add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label
);
1060 if (ret
== 0 || label
== NULL
|| *label
== '\0')
1063 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1064 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AFTER
);
1066 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_BEFORE
);
1068 hotlist_state
.modified
= TRUE
;
1071 /* --------------------------------------------------------------------------------------------- */
1074 remove_group (struct hotlist
*grp
)
1076 struct hotlist
*current
= grp
->head
;
1078 while (current
!= NULL
)
1080 struct hotlist
*next
= current
->next
;
1082 if (current
->type
== HL_TYPE_GROUP
)
1083 remove_group (current
);
1085 g_free (current
->label
);
1086 g_free (current
->directory
);
1093 /* --------------------------------------------------------------------------------------------- */
1096 remove_from_hotlist (struct hotlist
*entry
)
1101 if (entry
->type
== HL_TYPE_DOTDOT
)
1104 if (confirm_directory_hotlist_delete
)
1106 char text
[BUF_MEDIUM
];
1112 g_snprintf (text
, sizeof (text
), _("Are you sure you want to remove entry \"%s\"?"),
1113 str_trunc (entry
->label
, 30));
1114 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1115 _("&Yes"), _("&No"));
1120 if (entry
->type
== HL_TYPE_GROUP
)
1122 struct hotlist
*head
= entry
->head
;
1124 if (head
!= NULL
&& (head
->type
!= HL_TYPE_DOTDOT
|| head
->next
!= NULL
))
1126 char text
[BUF_MEDIUM
];
1129 g_snprintf (text
, sizeof (text
), _("Group \"%s\" is not empty.\nRemove it?"),
1130 str_trunc (entry
->label
, 30));
1131 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1132 _("&Yes"), _("&No"));
1137 remove_group (entry
);
1140 unlink_entry (entry
);
1142 g_free (entry
->label
);
1143 g_free (entry
->directory
);
1145 /* now remove list entry from screen */
1146 listbox_remove_current (l_hotlist
);
1147 hotlist_state
.modified
= TRUE
;
1150 /* --------------------------------------------------------------------------------------------- */
1153 load_group (struct hotlist
*grp
)
1155 gchar
**profile_keys
, **keys
;
1157 char *group_section
;
1158 struct hotlist
*current
= 0;
1160 group_section
= find_group_section (grp
);
1162 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, group_section
, &len
);
1164 current_group
= grp
;
1166 while (*profile_keys
!= NULL
)
1168 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1169 g_strdup (*profile_keys
), HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1172 g_free (group_section
);
1175 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp
->directory
, &len
);
1177 while (*profile_keys
!= NULL
)
1179 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1180 g_strdup (*profile_keys
), HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1185 for (current
= grp
->head
; current
; current
= current
->next
)
1186 load_group (current
);
1189 /* --------------------------------------------------------------------------------------------- */
1192 hot_skip_blanks (void)
1196 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1201 /* --------------------------------------------------------------------------------------------- */
1204 hot_next_token (void)
1209 if (tkn_buf
== NULL
)
1210 tkn_buf
= g_string_new ("");
1211 g_string_set_size (tkn_buf
, 0);
1214 c
= hot_skip_blanks ();
1224 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n')
1225 g_string_append_c (tkn_buf
, c
);
1229 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"')
1233 c
= getc (hotlist_file
);
1236 g_string_free (tkn_buf
, TRUE
);
1240 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1242 ret
= (c
== EOF
) ? TKN_EOF
: TKN_STRING
;
1245 c
= getc (hotlist_file
);
1248 g_string_free (tkn_buf
, TRUE
);
1254 /* fall through; it is taken as normal character */
1259 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1261 while ((c
= fgetc (hotlist_file
)) != EOF
&& (g_ascii_isalnum (c
) || !isascii (c
)));
1263 ungetc (c
, hotlist_file
);
1265 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1267 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1269 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1271 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1280 /* --------------------------------------------------------------------------------------------- */
1283 hot_load_group (struct hotlist
*grp
)
1286 struct hotlist
*new_grp
;
1289 current_group
= grp
;
1291 while ((tkn
= hot_next_token ()) != TKN_ENDGROUP
)
1295 CHECK_TOKEN (TKN_STRING
);
1297 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1298 LISTBOX_APPEND_AT_END
);
1300 hot_load_group (new_grp
);
1301 current_group
= grp
;
1305 CHECK_TOKEN (TKN_STRING
);
1306 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1307 CHECK_TOKEN (TKN_URL
);
1308 CHECK_TOKEN (TKN_STRING
);
1309 url
= tilde_expand (tkn_buf
->str
);
1310 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1315 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1316 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1319 hotlist_state
.readonly
= TRUE
;
1320 hotlist_state
.file_error
= TRUE
;
1323 /* skip empty lines */
1326 hotlist_state
.readonly
= TRUE
;
1327 hotlist_state
.file_error
= TRUE
;
1334 /* --------------------------------------------------------------------------------------------- */
1337 hot_load_file (struct hotlist
*grp
)
1340 struct hotlist
*new_grp
;
1343 current_group
= grp
;
1345 while ((tkn
= hot_next_token ()) != TKN_EOF
)
1349 CHECK_TOKEN (TKN_STRING
);
1351 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1352 LISTBOX_APPEND_AT_END
);
1354 hot_load_group (new_grp
);
1355 current_group
= grp
;
1359 CHECK_TOKEN (TKN_STRING
);
1360 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1361 CHECK_TOKEN (TKN_URL
);
1362 CHECK_TOKEN (TKN_STRING
);
1363 url
= tilde_expand (tkn_buf
->str
);
1364 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1369 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1370 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1373 /* skip empty lines */
1376 hotlist_state
.readonly
= TRUE
;
1377 hotlist_state
.file_error
= TRUE
;
1383 /* --------------------------------------------------------------------------------------------- */
1386 clean_up_hotlist_groups (const char *section
)
1389 gchar
**profile_keys
, **keys
;
1392 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1393 if (mc_config_has_group (mc_main_config
, section
))
1394 mc_config_del_group (mc_main_config
, section
);
1396 if (mc_config_has_group (mc_main_config
, grp_section
))
1398 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp_section
, &len
);
1400 while (*profile_keys
!= NULL
)
1402 clean_up_hotlist_groups (*profile_keys
);
1406 mc_config_del_group (mc_main_config
, grp_section
);
1408 g_free (grp_section
);
1411 /* --------------------------------------------------------------------------------------------- */
1416 gboolean remove_old_list
= FALSE
;
1417 struct stat stat_buf
;
1419 if (hotlist_state
.loaded
)
1421 stat (hotlist_file_name
, &stat_buf
);
1422 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1428 if (hotlist_file_name
== NULL
)
1429 hotlist_file_name
= mc_config_get_full_path (MC_HOTLIST_FILE
);
1431 hotlist
= g_new0 (struct hotlist
, 1);
1432 hotlist
->type
= HL_TYPE_GROUP
;
1433 hotlist
->label
= g_strdup (_("Top level group"));
1434 hotlist
->up
= hotlist
;
1438 hotlist
->directory
= g_strdup ("Hotlist");
1440 hotlist_file
= fopen (hotlist_file_name
, "r");
1441 if (hotlist_file
== NULL
)
1445 load_group (hotlist
);
1446 hotlist_state
.loaded
= TRUE
;
1448 * just to be sure we got copy
1450 hotlist_state
.modified
= TRUE
;
1451 result
= save_hotlist ();
1452 hotlist_state
.modified
= FALSE
;
1454 remove_old_list
= TRUE
;
1456 message (D_ERROR
, _("Hotlist Load"),
1458 ("MC was unable to write %s file,\nyour old hotlist entries were not deleted"),
1459 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE
);
1463 hot_load_file (hotlist
);
1464 fclose (hotlist_file
);
1465 hotlist_state
.loaded
= TRUE
;
1468 if (remove_old_list
)
1470 GError
*error
= NULL
;
1472 clean_up_hotlist_groups ("Hotlist");
1473 if (!mc_config_save_file (mc_main_config
, &error
))
1474 setup_save_config_show_error (mc_main_config
->ini_path
, &error
);
1477 stat (hotlist_file_name
, &stat_buf
);
1478 hotlist_file_mtime
= stat_buf
.st_mtime
;
1479 current_group
= hotlist
;
1482 /* --------------------------------------------------------------------------------------------- */
1485 hot_save_group (struct hotlist
*grp
)
1487 struct hotlist
*current
;
1493 for (i = 0; i < n; i++) \
1494 putc (' ', hotlist_file); \
1497 for (current
= grp
->head
; current
!= NULL
; current
= current
->next
)
1498 switch (current
->type
)
1501 INDENT (list_level
);
1502 fputs ("GROUP \"", hotlist_file
);
1503 for (s
= current
->label
; *s
!= '\0'; s
++)
1505 if (*s
== '"' || *s
== '\\')
1506 putc ('\\', hotlist_file
);
1507 putc (*s
, hotlist_file
);
1509 fputs ("\"\n", hotlist_file
);
1511 hot_save_group (current
);
1513 INDENT (list_level
);
1514 fputs ("ENDGROUP\n", hotlist_file
);
1517 INDENT (list_level
);
1518 fputs ("ENTRY \"", 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 ("\" URL \"", hotlist_file
);
1526 for (s
= current
->directory
; *s
!= '\0'; s
++)
1528 if (*s
== '"' || *s
== '\\')
1529 putc ('\\', hotlist_file
);
1530 putc (*s
, hotlist_file
);
1532 fputs ("\"\n", hotlist_file
);
1534 case HL_TYPE_COMMENT
:
1535 fprintf (hotlist_file
, "#%s\n", current
->label
);
1537 case HL_TYPE_DOTDOT
:
1543 /* --------------------------------------------------------------------------------------------- */
1546 add_dotdot_to_list (void)
1548 if (current_group
!= hotlist
&& hotlist_has_dot_dot
)
1549 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, LISTBOX_APPEND_AT_END
);
1552 /* --------------------------------------------------------------------------------------------- */
1553 /*** public functions ****************************************************************************/
1554 /* --------------------------------------------------------------------------------------------- */
1557 add2hotlist_cmd (void)
1560 const char *cp
= N_("Label for \"%s\":");
1562 char *label_string
, *label
;
1568 l
= str_term_width1 (cp
);
1569 label_string
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1570 lc_prompt
= g_strdup_printf (cp
, str_trunc (label_string
, COLS
- 2 * UX
- (l
+ 8)));
1572 input_dialog (_("Add to hotlist"), lc_prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
,
1573 INPUT_COMPLETE_NONE
);
1576 if (label
== NULL
|| *label
== '\0')
1578 g_free (label_string
);
1583 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1584 hotlist_state
.modified
= TRUE
;
1588 /* --------------------------------------------------------------------------------------------- */
1591 hotlist_show (hotlist_t list_type
)
1593 char *target
= NULL
;
1596 hotlist_state
.type
= list_type
;
1599 init_hotlist (list_type
);
1601 /* display file info */
1602 tty_setcolor (SELECTED_COLOR
);
1604 hotlist_state
.running
= TRUE
;
1605 res
= run_dlg (hotlist_dlg
);
1606 hotlist_state
.running
= FALSE
;
1612 struct hotlist
*hlp
= NULL
;
1614 listbox_get_current (l_hotlist
, &text
, (void **) &hlp
);
1615 target
= g_strdup (hlp
!= NULL
? hlp
->directory
: text
);
1622 /* --------------------------------------------------------------------------------------------- */
1627 gboolean saved
= FALSE
;
1628 struct stat stat_buf
;
1630 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
!= NULL
)
1632 mc_util_make_backup_if_possible (hotlist_file_name
, ".bak");
1634 hotlist_file
= fopen (hotlist_file_name
, "w");
1635 if (hotlist_file
== NULL
)
1636 mc_util_restore_from_backup_if_possible (hotlist_file_name
, ".bak");
1639 hot_save_group (hotlist
);
1640 fclose (hotlist_file
);
1641 stat (hotlist_file_name
, &stat_buf
);
1642 hotlist_file_mtime
= stat_buf
.st_mtime
;
1643 hotlist_state
.modified
= FALSE
;
1651 /* --------------------------------------------------------------------------------------------- */
1653 * Unload list from memory.
1654 * Don't confuse with hotlist_done() for GUI.
1660 if (hotlist
!= NULL
)
1662 remove_group (hotlist
);
1663 g_free (hotlist
->label
);
1664 g_free (hotlist
->directory
);
1669 hotlist_state
.loaded
= FALSE
;
1671 g_free (hotlist_file_name
);
1672 hotlist_file_name
= NULL
;
1674 current_group
= NULL
;
1676 if (tkn_buf
!= NULL
)
1678 g_string_free (tkn_buf
, TRUE
);
1683 /* --------------------------------------------------------------------------------------------- */