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
, 0, "input-lbl", r1
, NULL
),
964 QUICK_SEPARATOR (FALSE
),
965 QUICK_LABELED_INPUT (text2
, input_label_above
, *r2
, 0, "input-lbl", r2
, NULL
),
966 QUICK_START_BUTTONS (TRUE
, TRUE
),
967 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
968 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
969 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
974 quick_dialog_t qdlg
= {
977 quick_widgets
, NULL
, NULL
982 ret
= quick_dialog (&qdlg
);
984 return (ret
!= B_CANCEL
) ? ret
: 0;
987 /* --------------------------------------------------------------------------------------------- */
990 add_new_entry_cmd (void)
992 char *title
, *url
, *to_free
;
995 /* Take current directory as default value for input fields */
996 to_free
= title
= url
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
998 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
999 _("Directory path:"), "[Hotlist]", &title
, &url
);
1004 if (title
== NULL
|| *title
== '\0' || url
== NULL
|| *url
== '\0')
1011 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1012 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AFTER
);
1014 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_BEFORE
);
1016 hotlist_state
.modified
= TRUE
;
1019 /* --------------------------------------------------------------------------------------------- */
1022 add_new_group_input (const char *header
, const char *label
, char **result
)
1024 quick_widget_t quick_widgets
[] = {
1026 QUICK_LABELED_INPUT (label
, input_label_above
, "", 0, "input", result
, NULL
),
1027 QUICK_START_BUTTONS (TRUE
, TRUE
),
1028 QUICK_BUTTON (N_("&Append"), B_APPEND
, NULL
, NULL
),
1029 QUICK_BUTTON (N_("&Insert"), B_INSERT
, NULL
, NULL
),
1030 QUICK_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
1035 quick_dialog_t qdlg
= {
1037 header
, "[Hotlist]",
1038 quick_widgets
, NULL
, NULL
1043 ret
= quick_dialog (&qdlg
);
1045 return (ret
!= B_CANCEL
) ? ret
: 0;
1048 /* --------------------------------------------------------------------------------------------- */
1051 add_new_group_cmd (void)
1056 ret
= add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label
);
1057 if (ret
== 0 || label
== NULL
|| *label
== '\0')
1060 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1061 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AFTER
);
1063 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_BEFORE
);
1065 hotlist_state
.modified
= TRUE
;
1068 /* --------------------------------------------------------------------------------------------- */
1071 remove_group (struct hotlist
*grp
)
1073 struct hotlist
*current
= grp
->head
;
1075 while (current
!= NULL
)
1077 struct hotlist
*next
= current
->next
;
1079 if (current
->type
== HL_TYPE_GROUP
)
1080 remove_group (current
);
1082 g_free (current
->label
);
1083 g_free (current
->directory
);
1090 /* --------------------------------------------------------------------------------------------- */
1093 remove_from_hotlist (struct hotlist
*entry
)
1098 if (entry
->type
== HL_TYPE_DOTDOT
)
1101 if (confirm_directory_hotlist_delete
)
1103 char text
[BUF_MEDIUM
];
1109 g_snprintf (text
, sizeof (text
), _("Are you sure you want to remove entry \"%s\"?"),
1110 str_trunc (entry
->label
, 30));
1111 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1112 _("&Yes"), _("&No"));
1117 if (entry
->type
== HL_TYPE_GROUP
)
1119 struct hotlist
*head
= entry
->head
;
1121 if (head
!= NULL
&& (head
->type
!= HL_TYPE_DOTDOT
|| head
->next
!= NULL
))
1123 char text
[BUF_MEDIUM
];
1126 g_snprintf (text
, sizeof (text
), _("Group \"%s\" is not empty.\nRemove it?"),
1127 str_trunc (entry
->label
, 30));
1128 result
= query_dialog (Q_ ("DialogTitle|Delete"), text
, D_ERROR
| D_CENTER
, 2,
1129 _("&Yes"), _("&No"));
1134 remove_group (entry
);
1137 unlink_entry (entry
);
1139 g_free (entry
->label
);
1140 g_free (entry
->directory
);
1142 /* now remove list entry from screen */
1143 listbox_remove_current (l_hotlist
);
1144 hotlist_state
.modified
= TRUE
;
1147 /* --------------------------------------------------------------------------------------------- */
1150 load_group (struct hotlist
*grp
)
1152 gchar
**profile_keys
, **keys
;
1154 char *group_section
;
1155 struct hotlist
*current
= 0;
1157 group_section
= find_group_section (grp
);
1159 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, group_section
, &len
);
1161 current_group
= grp
;
1163 while (*profile_keys
!= NULL
)
1165 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1166 g_strdup (*profile_keys
), HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1169 g_free (group_section
);
1172 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp
->directory
, &len
);
1174 while (*profile_keys
!= NULL
)
1176 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1177 g_strdup (*profile_keys
), HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1182 for (current
= grp
->head
; current
; current
= current
->next
)
1183 load_group (current
);
1186 /* --------------------------------------------------------------------------------------------- */
1189 hot_skip_blanks (void)
1193 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1198 /* --------------------------------------------------------------------------------------------- */
1201 hot_next_token (void)
1206 if (tkn_buf
== NULL
)
1207 tkn_buf
= g_string_new ("");
1208 g_string_set_size (tkn_buf
, 0);
1211 c
= hot_skip_blanks ();
1221 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n')
1222 g_string_append_c (tkn_buf
, c
);
1226 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"')
1230 c
= getc (hotlist_file
);
1233 g_string_free (tkn_buf
, TRUE
);
1237 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1239 ret
= (c
== EOF
) ? TKN_EOF
: TKN_STRING
;
1242 c
= getc (hotlist_file
);
1245 g_string_free (tkn_buf
, TRUE
);
1251 /* fall through; it is taken as normal character */
1256 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1258 while ((c
= fgetc (hotlist_file
)) != EOF
&& (g_ascii_isalnum (c
) || !isascii (c
)));
1260 ungetc (c
, hotlist_file
);
1262 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1264 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1266 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1268 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1277 /* --------------------------------------------------------------------------------------------- */
1280 hot_load_group (struct hotlist
*grp
)
1283 struct hotlist
*new_grp
;
1286 current_group
= grp
;
1288 while ((tkn
= hot_next_token ()) != TKN_ENDGROUP
)
1292 CHECK_TOKEN (TKN_STRING
);
1294 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1295 LISTBOX_APPEND_AT_END
);
1297 hot_load_group (new_grp
);
1298 current_group
= grp
;
1302 CHECK_TOKEN (TKN_STRING
);
1303 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1304 CHECK_TOKEN (TKN_URL
);
1305 CHECK_TOKEN (TKN_STRING
);
1306 url
= tilde_expand (tkn_buf
->str
);
1307 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1312 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1313 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1316 hotlist_state
.readonly
= TRUE
;
1317 hotlist_state
.file_error
= TRUE
;
1320 /* skip empty lines */
1323 hotlist_state
.readonly
= TRUE
;
1324 hotlist_state
.file_error
= TRUE
;
1331 /* --------------------------------------------------------------------------------------------- */
1334 hot_load_file (struct hotlist
*grp
)
1337 struct hotlist
*new_grp
;
1340 current_group
= grp
;
1342 while ((tkn
= hot_next_token ()) != TKN_EOF
)
1346 CHECK_TOKEN (TKN_STRING
);
1348 add2hotlist (g_strndup (tkn_buf
->str
, tkn_buf
->len
), 0, HL_TYPE_GROUP
,
1349 LISTBOX_APPEND_AT_END
);
1351 hot_load_group (new_grp
);
1352 current_group
= grp
;
1356 CHECK_TOKEN (TKN_STRING
);
1357 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1358 CHECK_TOKEN (TKN_URL
);
1359 CHECK_TOKEN (TKN_STRING
);
1360 url
= tilde_expand (tkn_buf
->str
);
1361 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1366 label
= g_strndup (tkn_buf
->str
, tkn_buf
->len
);
1367 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1370 /* skip empty lines */
1373 hotlist_state
.readonly
= TRUE
;
1374 hotlist_state
.file_error
= TRUE
;
1380 /* --------------------------------------------------------------------------------------------- */
1383 clean_up_hotlist_groups (const char *section
)
1386 gchar
**profile_keys
, **keys
;
1389 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1390 if (mc_config_has_group (mc_main_config
, section
))
1391 mc_config_del_group (mc_main_config
, section
);
1393 if (mc_config_has_group (mc_main_config
, grp_section
))
1395 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp_section
, &len
);
1397 while (*profile_keys
!= NULL
)
1399 clean_up_hotlist_groups (*profile_keys
);
1403 mc_config_del_group (mc_main_config
, grp_section
);
1405 g_free (grp_section
);
1408 /* --------------------------------------------------------------------------------------------- */
1413 gboolean remove_old_list
= FALSE
;
1414 struct stat stat_buf
;
1416 if (hotlist_state
.loaded
)
1418 stat (hotlist_file_name
, &stat_buf
);
1419 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1425 if (hotlist_file_name
== NULL
)
1426 hotlist_file_name
= mc_config_get_full_path (MC_HOTLIST_FILE
);
1428 hotlist
= g_new0 (struct hotlist
, 1);
1429 hotlist
->type
= HL_TYPE_GROUP
;
1430 hotlist
->label
= g_strdup (_("Top level group"));
1431 hotlist
->up
= hotlist
;
1435 hotlist
->directory
= g_strdup ("Hotlist");
1437 hotlist_file
= fopen (hotlist_file_name
, "r");
1438 if (hotlist_file
== NULL
)
1442 load_group (hotlist
);
1443 hotlist_state
.loaded
= TRUE
;
1445 * just to be sure we got copy
1447 hotlist_state
.modified
= TRUE
;
1448 result
= save_hotlist ();
1449 hotlist_state
.modified
= FALSE
;
1451 remove_old_list
= TRUE
;
1453 message (D_ERROR
, _("Hotlist Load"),
1455 ("MC was unable to write %s file,\nyour old hotlist entries were not deleted"),
1456 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE
);
1460 hot_load_file (hotlist
);
1461 fclose (hotlist_file
);
1462 hotlist_state
.loaded
= TRUE
;
1465 if (remove_old_list
)
1467 GError
*error
= NULL
;
1469 clean_up_hotlist_groups ("Hotlist");
1470 if (!mc_config_save_file (mc_main_config
, &error
))
1471 setup_save_config_show_error (mc_main_config
->ini_path
, &error
);
1474 stat (hotlist_file_name
, &stat_buf
);
1475 hotlist_file_mtime
= stat_buf
.st_mtime
;
1476 current_group
= hotlist
;
1479 /* --------------------------------------------------------------------------------------------- */
1482 hot_save_group (struct hotlist
*grp
)
1484 struct hotlist
*current
;
1490 for (i = 0; i < n; i++) \
1491 putc (' ', hotlist_file); \
1494 for (current
= grp
->head
; current
!= NULL
; current
= current
->next
)
1495 switch (current
->type
)
1498 INDENT (list_level
);
1499 fputs ("GROUP \"", hotlist_file
);
1500 for (s
= current
->label
; *s
!= '\0'; s
++)
1502 if (*s
== '"' || *s
== '\\')
1503 putc ('\\', hotlist_file
);
1504 putc (*s
, hotlist_file
);
1506 fputs ("\"\n", hotlist_file
);
1508 hot_save_group (current
);
1510 INDENT (list_level
);
1511 fputs ("ENDGROUP\n", hotlist_file
);
1514 INDENT (list_level
);
1515 fputs ("ENTRY \"", hotlist_file
);
1516 for (s
= current
->label
; *s
!= '\0'; s
++)
1518 if (*s
== '"' || *s
== '\\')
1519 putc ('\\', hotlist_file
);
1520 putc (*s
, hotlist_file
);
1522 fputs ("\" URL \"", hotlist_file
);
1523 for (s
= current
->directory
; *s
!= '\0'; s
++)
1525 if (*s
== '"' || *s
== '\\')
1526 putc ('\\', hotlist_file
);
1527 putc (*s
, hotlist_file
);
1529 fputs ("\"\n", hotlist_file
);
1531 case HL_TYPE_COMMENT
:
1532 fprintf (hotlist_file
, "#%s\n", current
->label
);
1534 case HL_TYPE_DOTDOT
:
1540 /* --------------------------------------------------------------------------------------------- */
1543 add_dotdot_to_list (void)
1545 if (current_group
!= hotlist
&& hotlist_has_dot_dot
)
1546 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, LISTBOX_APPEND_AT_END
);
1549 /* --------------------------------------------------------------------------------------------- */
1550 /*** public functions ****************************************************************************/
1551 /* --------------------------------------------------------------------------------------------- */
1554 add2hotlist_cmd (void)
1557 const char *cp
= N_("Label for \"%s\":");
1559 char *label_string
, *label
;
1565 l
= str_term_width1 (cp
);
1566 label_string
= vfs_path_to_str_flags (current_panel
->cwd_vpath
, 0, VPF_STRIP_PASSWORD
);
1567 lc_prompt
= g_strdup_printf (cp
, str_trunc (label_string
, COLS
- 2 * UX
- (l
+ 8)));
1568 label
= input_dialog (_("Add to hotlist"), lc_prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1571 if (label
== NULL
|| *label
== '\0')
1573 g_free (label_string
);
1578 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1579 hotlist_state
.modified
= TRUE
;
1583 /* --------------------------------------------------------------------------------------------- */
1586 hotlist_show (hotlist_t list_type
)
1588 char *target
= NULL
;
1591 hotlist_state
.type
= list_type
;
1594 init_hotlist (list_type
);
1596 /* display file info */
1597 tty_setcolor (SELECTED_COLOR
);
1599 hotlist_state
.running
= TRUE
;
1600 res
= run_dlg (hotlist_dlg
);
1601 hotlist_state
.running
= FALSE
;
1607 struct hotlist
*hlp
= NULL
;
1609 listbox_get_current (l_hotlist
, &text
, (void **) &hlp
);
1610 target
= g_strdup (hlp
!= NULL
? hlp
->directory
: text
);
1617 /* --------------------------------------------------------------------------------------------- */
1622 gboolean saved
= FALSE
;
1623 struct stat stat_buf
;
1625 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
!= NULL
)
1627 mc_util_make_backup_if_possible (hotlist_file_name
, ".bak");
1629 hotlist_file
= fopen (hotlist_file_name
, "w");
1630 if (hotlist_file
== NULL
)
1631 mc_util_restore_from_backup_if_possible (hotlist_file_name
, ".bak");
1634 hot_save_group (hotlist
);
1635 fclose (hotlist_file
);
1636 stat (hotlist_file_name
, &stat_buf
);
1637 hotlist_file_mtime
= stat_buf
.st_mtime
;
1638 hotlist_state
.modified
= FALSE
;
1646 /* --------------------------------------------------------------------------------------------- */
1648 * Unload list from memory.
1649 * Don't confuse with hotlist_done() for GUI.
1655 if (hotlist
!= NULL
)
1657 remove_group (hotlist
);
1658 g_free (hotlist
->label
);
1659 g_free (hotlist
->directory
);
1664 hotlist_state
.loaded
= FALSE
;
1666 g_free (hotlist_file_name
);
1667 hotlist_file_name
= NULL
;
1669 current_group
= NULL
;
1671 if (tkn_buf
!= NULL
)
1673 g_string_free (tkn_buf
, TRUE
);
1678 /* --------------------------------------------------------------------------------------------- */