1 /* Directory hotlist -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
11 Janne did the original Hotlist code, Andrej made the groupable
12 hotlist; the move hotlist and revamped the file format and made
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 #include <sys/types.h>
39 #include <mhl/memory.h>
40 #include <mhl/string.h>
43 #include "tty.h" /* COLS */
44 #include "color.h" /* dialog_colors */
47 #include "setup.h" /* For profile_bname */
48 #include "profile.h" /* Load/save directories hotlist */
49 #include "wtools.h" /* QuickDialog */
50 #include "panel.h" /* current_panel */
51 #include "main.h" /* repaint_screen */
53 #include "key.h" /* KEY_M_CTRL */
54 #include "command.h" /* cmdline */
55 #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
62 #define BY (LINES - 6)
64 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
66 #define B_ADD_CURRENT B_USER
67 #define B_REMOVE (B_USER + 1)
68 #define B_NEW_GROUP (B_USER + 2)
69 #define B_NEW_ENTRY (B_USER + 3)
70 #define B_UP_GROUP (B_USER + 4)
71 #define B_INSERT (B_USER + 5)
72 #define B_APPEND (B_USER + 6)
73 #define B_MOVE (B_USER + 7)
76 #include "../vfs/gc.h"
77 #define B_FREE_ALL_VFS (B_USER + 8)
78 #define B_REFRESH_VFS (B_USER + 9)
81 int hotlist_has_dot_dot
= 1;
83 static WListbox
*l_hotlist
;
84 static WListbox
*l_movelist
;
86 static Dlg_head
*hotlist_dlg
;
87 static Dlg_head
*movelist_dlg
;
89 static WLabel
*pname
, *pname_group
, *movelist_group
;
100 * these parameters are intended to be user configurable
102 int expanded
; /* expanded view of all groups at startup */
105 * these reflect run time state
108 int loaded
; /* hotlist is loaded */
109 int readonly
; /* hotlist readonly */
110 int file_error
; /* parse error while reading file */
111 int running
; /* we are running dlg (and have to
113 int moving
; /* we are in moving hotlist currently */
114 int modified
; /* hotlist was modified */
115 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
118 static struct _hotlist_but
{
119 int ret_cmd
, flags
, y
, x
;
123 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), LIST_HOTLIST
},
124 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), LIST_HOTLIST
},
125 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), LIST_MOVELIST
},
126 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), LIST_MOVELIST
},
127 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), LIST_HOTLIST
},
128 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), LIST_HOTLIST
},
129 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
130 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), LIST_HOTLIST
|LIST_MOVELIST
},
131 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"), LIST_HOTLIST
},
133 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"), LIST_VFSLIST
},
134 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"), LIST_VFSLIST
},
136 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
139 /* Directory hotlist */
140 static struct hotlist
{
141 enum HotListType type
;
144 struct hotlist
*head
;
146 struct hotlist
*next
;
149 static struct hotlist
*current_group
;
151 static void init_movelist (int, struct hotlist
*);
152 static void add_new_group_cmd (void);
153 static void add_new_entry_cmd (void);
154 static void remove_from_hotlist (struct hotlist
*entry
);
155 static void load_hotlist (void);
156 static void add_dotdot_to_list (void);
158 #define new_hotlist() g_new0(struct hotlist, 1)
161 hotlist_refresh (Dlg_head
* dlg
)
163 common_dialog_repaint (dlg
);
164 attrset (COLOR_NORMAL
);
165 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
166 dlg
->cols
- (UX
* 2));
167 if (!hotlist_state
.moving
)
168 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
171 /* If current->data is 0, then we are dealing with a VFS pathname */
173 update_path_name (void)
175 const char *text
= "";
177 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
178 Dlg_head
*dlg
= list
->widget
.parent
;
181 if (list
->current
->data
!= 0) {
182 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
184 if (hlp
->type
== HL_TYPE_ENTRY
||
185 hlp
->type
== HL_TYPE_DOTDOT
)
186 text
= hlp
->directory
;
187 else if (hlp
->type
== HL_TYPE_GROUP
)
188 text
= _("Subgroup - press ENTER to see list");
190 text
= list
->current
->text
;
193 if (!hotlist_state
.moving
)
194 label_set_text (pname
,
195 name_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
197 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
198 if (!hotlist_state
.moving
)
199 label_set_text (pname_group
,
200 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
202 label_set_text (movelist_group
,
203 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
209 #define CHECK_BUFFER \
213 if ((i = strlen (current->label) + 3) > buflen) { \
215 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
220 static void fill_listbox (void)
222 struct hotlist
*current
= current_group
->head
;
227 buf
= g_malloc (buflen
= 1024);
231 switch (current
->type
) {
235 strcat (strcat (buf
, "->"), current
->label
);
236 if (hotlist_state
.moving
)
237 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
239 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
244 if (hotlist_state
.moving
)
245 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
247 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
252 current
= current
->next
;
259 unlink_entry (struct hotlist
*entry
)
261 struct hotlist
*current
= current_group
->head
;
263 if (current
== entry
)
264 current_group
->head
= entry
->next
;
266 while (current
&& current
->next
!= entry
)
267 current
= current
->next
;
269 current
->next
= entry
->next
;
276 static void add_name_to_list (const char *path
)
278 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
280 #endif /* !USE_VFS */
283 hotlist_button_callback (int action
)
288 struct hotlist
*saved
= current_group
;
289 struct hotlist
*item
;
290 struct hotlist
*moveto_item
= 0;
291 struct hotlist
*moveto_group
= 0;
294 if (!l_hotlist
->current
)
295 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
296 item
= l_hotlist
->current
->data
;
297 hotlist_state
.moving
= 1;
298 init_movelist (LIST_MOVELIST
, item
);
299 run_dlg (movelist_dlg
);
300 ret
= movelist_dlg
->ret_value
;
301 hotlist_state
.moving
= 0;
302 if (l_movelist
->current
)
303 moveto_item
= l_movelist
->current
->data
;
304 moveto_group
= current_group
;
305 destroy_dlg (movelist_dlg
);
306 current_group
= saved
;
308 return MSG_NOT_HANDLED
;
309 if (moveto_item
== item
)
310 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
311 it hardly changes anything ;) */
313 listbox_remove_current (l_hotlist
, 1);
314 item
->up
= moveto_group
;
315 if (!moveto_group
->head
)
316 moveto_group
->head
= item
;
317 else if (!moveto_item
) { /* we have group with just comments */
318 struct hotlist
*p
= moveto_group
->head
;
324 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
325 if (!moveto_item
->next
)
326 moveto_item
->next
= item
;
328 item
->next
= moveto_item
->next
;
329 moveto_item
->next
= item
;
330 } else if (moveto_group
->head
== moveto_item
) {
331 moveto_group
->head
= item
;
332 item
->next
= moveto_item
;
334 struct hotlist
*p
= moveto_group
->head
;
336 while (p
->next
!= moveto_item
)
338 item
->next
= p
->next
;
341 listbox_remove_list (l_hotlist
);
344 hotlist_state
.modified
= 1;
345 return MSG_NOT_HANDLED
;
349 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
350 remove_from_hotlist (l_hotlist
->current
->data
);
351 return MSG_NOT_HANDLED
;
355 add_new_group_cmd ();
356 return MSG_NOT_HANDLED
;
361 return MSG_NOT_HANDLED
;
365 add_new_entry_cmd ();
366 return MSG_NOT_HANDLED
;
371 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
373 if (list
->current
->data
) {
374 struct hotlist
*hlp
=
375 (struct hotlist
*) list
->current
->data
;
376 if (hlp
->type
== HL_TYPE_ENTRY
)
378 else if (hlp
->type
== HL_TYPE_DOTDOT
) {
379 /* Fall through - go up */
383 listbox_remove_list (list
);
386 return MSG_NOT_HANDLED
;
392 /* Fall through if list empty - just go up */
396 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
397 listbox_remove_list (list
);
398 current_group
= current_group
->up
;
400 return MSG_NOT_HANDLED
;
410 listbox_remove_list (l_hotlist
);
411 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
412 vfs_fill_names (add_name_to_list
);
413 return MSG_NOT_HANDLED
;
424 hotlist_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
431 case DLG_UNHANDLED_KEY
:
433 case KEY_M_CTRL
| '\n':
438 if (hotlist_button_callback (B_ENTER
)) {
439 h
->ret_value
= B_ENTER
;
445 if (hotlist_state
.type
!= LIST_VFSLIST
)
446 return !hotlist_button_callback (B_UP_GROUP
);
448 return MSG_NOT_HANDLED
;
451 if (!hotlist_state
.moving
) {
452 hotlist_button_callback (B_REMOVE
);
459 if (!hotlist_state
.moving
) {
460 if (l_hotlist
->current
) {
461 if (l_hotlist
->current
->data
) {
462 struct hotlist
*hlp
=
463 (struct hotlist
*) l_hotlist
->current
->data
;
464 if (hlp
->type
== HL_TYPE_ENTRY
) {
466 g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
467 stuff (cmdline
, tmp
, 0);
470 h
->ret_value
= B_CANCEL
;
476 return MSG_HANDLED
; /* ignore key */
478 return MSG_NOT_HANDLED
;
481 if (hotlist_state
.moving
)
482 dlg_select_widget (l_movelist
);
484 dlg_select_widget (l_hotlist
);
485 /* always stay on hotlist */
489 attrset (MENU_ENTRY_COLOR
);
494 return default_dlg_callback (h
, msg
, parm
);
498 static int l_call (WListbox
*list
)
500 Dlg_head
*dlg
= list
->widget
.parent
;
503 if (list
->current
->data
) {
504 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
505 if (hlp
->type
== HL_TYPE_ENTRY
) {
506 dlg
->ret_value
= B_ENTER
;
510 hotlist_button_callback (B_ENTER
);
511 hotlist_callback (dlg
, DLG_POST_KEY
, '\n');
515 dlg
->ret_value
= B_ENTER
;
521 hotlist_button_callback (B_UP_GROUP
);
522 hotlist_callback (dlg
, DLG_POST_KEY
, 'u');
527 * Expands all button names (once) and recalculates button positions.
528 * returns number of columns in the dialog box, which is 10 chars longer
531 * If common width of the window (i.e. in xterm) is less than returned
532 * width - sorry :) (anyway this did not handled in previous version too)
535 init_i18n_stuff(int list_type
, int cols
)
538 static const char* cancel_but
= N_("&Cancel");
541 static int hotlist_i18n_flag
= 0;
543 if (!hotlist_i18n_flag
)
545 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
547 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
549 cancel_but
= _(cancel_but
);
550 hotlist_i18n_flag
= 1;
552 #endif /* ENABLE_NLS */
554 /* Dynamic resizing of buttonbars */
556 int len
[2], count
[2]; /* at most two lines of buttons */
559 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
560 len
[0] = len
[1] = count
[0] = count
[1] = 0;
562 /* Count len of buttonbars, assuming 2 extra space between buttons */
565 if (! (hotlist_but
[i
].type
& list_type
))
568 row
= hotlist_but
[i
].y
;
570 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
571 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
577 cols
= max(cols
, max(len
[0], len
[1]));
579 /* arrange buttons */
581 cur_x
[0] = cur_x
[1] = 0;
582 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
585 if (! (hotlist_but
[i
].type
& list_type
))
588 row
= hotlist_but
[i
].y
;
590 if (hotlist_but
[i
].x
!= 0)
592 /* not first int the row */
593 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
595 cols
- strlen (hotlist_but
[i
].text
) - 13;
597 hotlist_but
[i
].x
= cur_x
[row
];
600 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
601 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
609 init_hotlist (int list_type
)
612 const char *title
, *help_node
;
615 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
619 hotlist_state
.expanded
=
620 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
623 if (list_type
== LIST_VFSLIST
) {
624 title
= _("Active VFS directories");
625 help_node
= "[vfshot]"; /* FIXME - no such node */
627 title
= _("Directory hotlist");
628 help_node
= "[Hotlist]";
632 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
633 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
635 for (i
= 0; i
< BUTTONS
; i
++) {
636 if (hotlist_but
[i
].type
& list_type
)
637 add_widget (hotlist_dlg
,
638 button_new (BY
+ hotlist_but
[i
].y
,
639 BX
+ hotlist_but
[i
].x
,
640 hotlist_but
[i
].ret_cmd
,
641 hotlist_but
[i
].flags
,
643 hotlist_button_callback
));
646 /* We add the labels.
647 * pname will hold entry's pathname;
648 * pname_group will hold name of current group
650 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
651 add_widget (hotlist_dlg
, pname
);
652 if (!hotlist_state
.moving
) {
653 add_widget (hotlist_dlg
,
654 label_new (UY
- 12 + LINES
, UX
+ 1,
655 _(" Directory path ")));
657 /* This one holds the displayed pathname */
658 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
659 add_widget (hotlist_dlg
, pname_group
);
661 /* get new listbox */
663 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
666 /* Fill the hotlist with the active VFS or the hotlist */
668 if (list_type
== LIST_VFSLIST
) {
669 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
670 vfs_fill_names (add_name_to_list
);
672 #endif /* !USE_VFS */
675 add_widget (hotlist_dlg
, l_hotlist
);
676 /* add listbox to the dialogs */
680 init_movelist (int list_type
, struct hotlist
*item
)
683 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
684 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
689 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
690 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
693 for (i
= 0; i
< BUTTONS
; i
++) {
694 if (hotlist_but
[i
].type
& list_type
)
695 add_widget (movelist_dlg
,
696 button_new (BY
- 4 + hotlist_but
[i
].y
,
697 BX
+ hotlist_but
[i
].x
,
698 hotlist_but
[i
].ret_cmd
,
699 hotlist_but
[i
].flags
,
701 hotlist_button_callback
));
704 /* We add the labels. We are interested in the last one,
705 * that one will hold the path name label
707 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
708 add_widget (movelist_dlg
, movelist_group
);
709 /* get new listbox */
711 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
712 movelist_dlg
->lines
- 8, l_call
);
716 add_widget (movelist_dlg
, l_movelist
);
717 /* add listbox to the dialogs */
721 * Destroy the list dialog.
722 * Don't confuse with done_hotlist() for the list in memory.
724 static void hotlist_done (void)
726 destroy_dlg (hotlist_dlg
);
729 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
734 find_group_section (struct hotlist
*grp
)
736 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
740 /* 1.11.96 bor: added pos parameter to control placement of new item.
741 see widget.c, listbox_add_item()
742 now hotlist is in unsorted mode
745 HL_BEFORE_CURRENT
= 1
746 ,HL_AFTER_CURRENT
= 2
749 static struct hotlist
*
750 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
753 struct hotlist
*current
= NULL
;
756 * Hotlist is neither loaded nor loading.
757 * Must be called by "Ctrl-x a" before using hotlist.
762 if (l_hotlist
&& l_hotlist
->current
) {
763 current
= l_hotlist
->current
->data
;
765 /* Make sure `..' stays at the top of the list. */
766 if (current
->type
== HL_TYPE_DOTDOT
)
767 pos
= HL_AFTER_CURRENT
;
770 new = new_hotlist ();
774 new->directory
= directory
;
775 new->up
= current_group
;
777 if (type
== HL_TYPE_GROUP
) {
779 add_dotdot_to_list ();
780 current_group
= new->up
;
783 if (!current_group
->head
) { /* first element in group */
784 current_group
->head
= new;
785 } else if (pos
== HL_AFTER_CURRENT
) {
786 new->next
= current
->next
;
788 } else if (pos
== HL_BEFORE_CURRENT
&&
789 current
== current_group
->head
) {
790 /* should be inserted before first item */
792 current_group
->head
= new;
793 } else if (pos
== HL_BEFORE_CURRENT
) {
794 struct hotlist
*p
= current_group
->head
;
796 while (p
->next
!= current
)
801 } else { /* append at the end */
802 struct hotlist
*p
= current_group
->head
;
810 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&&
811 type
!= HL_TYPE_DOTDOT
) {
812 if (type
== HL_TYPE_GROUP
) {
813 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
815 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
818 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
819 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
827 * Support routine for add_new_entry_input()/add_new_group_input()
828 * Change positions of buttons (first three widgets).
830 * This is just a quick hack. Accurate procedure must take care of
831 * internationalized label lengths and total buttonbar length...assume
832 * 64 is longer anyway.
834 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
836 int i
, l
[3], space
, cur_x
;
838 for (i
= 0; i
< 3; i
++)
840 qw
[i
].text
= _(qw
[i
].text
);
841 l
[i
] = strlen (qw
[i
].text
) + 3;
843 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
845 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
847 qw
[i
].relative_x
= cur_x
;
848 qw
[i
].x_divisions
= len
;
851 #endif /* ENABLE_NLS */
854 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
855 const char *help
, char **r1
, char **r2
)
857 #define RELATIVE_Y_BUTTONS 4
858 #define RELATIVE_Y_LABEL_PTH 3
859 #define RELATIVE_Y_INPUT_PTH 4
861 QuickDialog Quick_input
;
862 static QuickWidget quick_widgets
[] = {
863 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
865 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
867 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
869 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
871 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
873 { quick_input
, 4, 80, 3, 0, "", 58, 0,
875 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
885 static int i18n_flag
= 0;
886 #endif /* ENABLE_NLS */
888 msglen(text1
, &lines1
, &cols1
);
889 msglen(text2
, &lines2
, &cols2
);
890 len
= max ((int) strlen (header
), cols1
);
891 len
= max (len
, cols2
) + 4;
897 add_widgets_i18n(quick_widgets
, len
);
900 #endif /* ENABLE_NLS */
902 Quick_input
.xlen
= len
;
903 Quick_input
.xpos
= -1;
904 Quick_input
.title
= header
;
905 Quick_input
.help
= help
;
906 Quick_input
.i18n
= 0;
907 quick_widgets
[6].text
= text1
;
908 quick_widgets
[4].text
= text2
;
909 quick_widgets
[5].text
= *r1
;
910 quick_widgets
[3].text
= *r2
;
912 for (i
= 0; i
< 7; i
++)
913 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
914 Quick_input
.ylen
= lines1
+ lines2
+ 7;
916 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
917 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
918 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
919 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
920 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
922 quick_widgets
[5].str_result
= r1
;
923 quick_widgets
[3].str_result
= r2
;
925 Quick_input
.widgets
= quick_widgets
;
926 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
932 static void add_new_entry_cmd (void)
934 char *title
, *url
, *to_free
;
937 /* Take current directory as default value for input fields */
938 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
940 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"),
941 _("Directory path"), "[Hotlist]", &title
, &url
);
946 if (!title
|| !*title
|| !url
|| !*url
) {
952 if (ret
== B_ENTER
|| ret
== B_APPEND
)
953 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_AFTER_CURRENT
);
955 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_BEFORE_CURRENT
);
957 hotlist_state
.modified
= 1;
961 add_new_group_input (const char *header
, const char *label
, char **result
)
964 QuickDialog Quick_input
;
965 static QuickWidget quick_widgets
[] = {
966 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
968 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
970 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
972 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
973 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, NULL
},
975 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
976 quick_widgets variable above */
982 static int i18n_flag
= 0;
983 #endif /* ENABLE_NLS */
985 msglen (label
, &lines
, &cols
);
986 len
= max ((int) strlen (header
), cols
) + 4;
992 add_widgets_i18n(quick_widgets
, len
);
995 #endif /* ENABLE_NLS */
997 Quick_input
.xlen
= len
;
998 Quick_input
.xpos
= -1;
999 Quick_input
.title
= header
;
1000 Quick_input
.help
= "[Hotlist]";
1001 Quick_input
.i18n
= 0;
1002 quick_widgets
[4].text
= label
;
1004 for (i
= 0; i
< 5; i
++)
1005 quick_widgets
[i
].y_divisions
= lines
+6;
1006 Quick_input
.ylen
= lines
+ 6;
1008 for (i
= 0; i
< 4; i
++)
1009 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
1011 quick_widgets
[3].str_result
= result
;
1012 quick_widgets
[3].text
= "";
1014 Quick_input
.widgets
= quick_widgets
;
1015 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
1021 static void add_new_group_cmd (void)
1026 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
1027 if (!ret
|| !label
|| !*label
)
1030 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1031 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_AFTER_CURRENT
);
1033 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_BEFORE_CURRENT
);
1035 hotlist_state
.modified
= 1;
1038 void add2hotlist_cmd (void)
1040 char *prompt
, *label
;
1041 const char *cp
= _("Label for \"%s\":");
1042 int l
= strlen (cp
);
1043 char *label_string
= g_strdup (current_panel
->cwd
);
1045 strip_password (label_string
, 1);
1047 prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
-2*UX
-(l
+8)));
1048 label
= input_dialog (_(" Add to hotlist "), prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1051 if (!label
|| !*label
) {
1052 g_free (label_string
);
1056 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, 0);
1057 hotlist_state
.modified
= 1;
1060 static void remove_group (struct hotlist
*grp
)
1062 struct hotlist
*current
= grp
->head
;
1065 struct hotlist
*next
= current
->next
;
1067 if (current
->type
== HL_TYPE_GROUP
)
1068 remove_group (current
);
1070 g_free (current
->label
);
1071 g_free (current
->directory
);
1079 static void remove_from_hotlist (struct hotlist
*entry
)
1081 if (entry
->type
== HL_TYPE_DOTDOT
)
1084 if (confirm_directory_hotlist_delete
) {
1088 title
= g_strconcat (_(" Remove: "),
1089 name_trunc (entry
->label
, 30),
1095 result
= query_dialog (title
,
1096 _("\n Are you sure you want to remove this entry?"),
1097 D_ERROR
, 2, _("&Yes"), _("&No"));
1105 if (entry
->type
== HL_TYPE_GROUP
) {
1110 header
= g_strconcat (_(" Remove: "),
1111 name_trunc (entry
->label
, 30),
1114 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1116 _("&Yes"), _("&No"));
1123 remove_group (entry
);
1126 unlink_entry (entry
);
1128 g_free (entry
->label
);
1129 g_free (entry
->directory
);
1131 /* now remove list entry from screen */
1132 listbox_remove_current (l_hotlist
, 1);
1133 hotlist_state
.modified
= 1;
1136 char *hotlist_cmd (int vfs_or_hotlist
)
1138 char *target
= NULL
;
1140 hotlist_state
.type
= vfs_or_hotlist
;
1143 init_hotlist (vfs_or_hotlist
);
1145 /* display file info */
1146 attrset (SELECTED_COLOR
);
1148 hotlist_state
.running
= 1;
1149 run_dlg (hotlist_dlg
);
1150 hotlist_state
.running
= 0;
1153 switch (hotlist_dlg
->ret_value
) {
1158 if (l_hotlist
->current
->data
) {
1159 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1160 target
= g_strdup (hlp
->directory
);
1162 target
= g_strdup (l_hotlist
->current
->text
);
1171 load_group (struct hotlist
*grp
)
1175 char *group_section
;
1176 struct hotlist
*current
= 0;
1178 group_section
= find_group_section (grp
);
1180 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1182 current_group
= grp
;
1184 while (profile_keys
){
1185 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1186 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1188 g_free (group_section
);
1190 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1192 while (profile_keys
){
1193 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1194 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1197 for (current
= grp
->head
; current
; current
= current
->next
)
1198 load_group (current
);
1203 #define TKN_STRING 2
1205 #define TKN_ENDGROUP 4
1206 #define TKN_COMMENT 5
1209 #define TKN_UNKNOWN 127
1211 static char *tkn_buf
;
1212 static int tkn_buf_length
;
1213 static int tkn_length
;
1215 static char *hotlist_file_name
;
1216 static FILE *hotlist_file
;
1217 static time_t hotlist_file_mtime
;
1219 static int hot_skip_blanks (void)
1223 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1229 static int hot_next_token (void)
1233 #define CHECK_BUF() \
1235 if (tkn_length == tkn_buf_length) \
1236 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1237 : ( g_malloc (tkn_buf_length = 1024)); \
1243 c
= hot_skip_blanks ();
1252 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1257 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1261 tkn_buf
[tkn_length
] = '\0';
1265 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1267 if ((c
= getc (hotlist_file
)) == EOF
)
1270 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1275 tkn_buf
[tkn_length
] = '\0';
1279 if ((c
= getc (hotlist_file
)) == EOF
)
1284 /* fall through; it is taken as normal character */
1289 tkn_buf
[tkn_length
++] = toupper(c
);
1290 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1292 ungetc (c
, hotlist_file
);
1294 tkn_buf
[tkn_length
] = '\0';
1295 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1297 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1299 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1300 return TKN_ENDGROUP
;
1301 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1309 #define SKIP_TO_EOL { \
1311 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1314 #define CHECK_TOKEN(_TKN_) \
1315 if ((tkn = hot_next_token ()) != _TKN_) { \
1316 hotlist_state.readonly = 1; \
1317 hotlist_state.file_error = 1; \
1318 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1319 tkn = hot_next_token (); \
1324 hot_load_group (struct hotlist
* grp
)
1327 struct hotlist
*new_grp
;
1330 current_group
= grp
;
1332 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1335 CHECK_TOKEN(TKN_STRING
);
1336 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1338 hot_load_group (new_grp
);
1339 current_group
= grp
;
1342 CHECK_TOKEN(TKN_STRING
);
1343 label
= g_strdup (tkn_buf
);
1344 CHECK_TOKEN(TKN_URL
);
1345 CHECK_TOKEN(TKN_STRING
);
1346 url
= g_strdup (tkn_buf
);
1347 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1351 label
= g_strdup (tkn_buf
);
1352 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1355 hotlist_state
.readonly
= 1;
1356 hotlist_state
.file_error
= 1;
1360 /* skip empty lines */
1363 hotlist_state
.readonly
= 1;
1364 hotlist_state
.file_error
= 1;
1372 hot_load_file (struct hotlist
* grp
)
1375 struct hotlist
*new_grp
;
1378 current_group
= grp
;
1380 while ((tkn
= hot_next_token())!= TKN_EOF
)
1383 CHECK_TOKEN(TKN_STRING
);
1384 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1386 hot_load_group (new_grp
);
1387 current_group
= grp
;
1390 CHECK_TOKEN(TKN_STRING
);
1391 label
= g_strdup (tkn_buf
);
1392 CHECK_TOKEN(TKN_URL
);
1393 CHECK_TOKEN(TKN_STRING
);
1394 url
= g_strdup (tkn_buf
);
1395 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1399 label
= g_strdup (tkn_buf
);
1400 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1403 /* skip empty lines */
1406 hotlist_state
.readonly
= 1;
1407 hotlist_state
.file_error
= 1;
1414 clean_up_hotlist_groups (const char *section
)
1420 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1421 if (profile_has_section (section
, profile_name
))
1422 profile_clean_section (section
, profile_name
);
1423 if (profile_has_section (grp_section
, profile_name
)) {
1424 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1426 while (profile_keys
) {
1427 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1428 clean_up_hotlist_groups (key
);
1430 profile_clean_section (grp_section
, profile_name
);
1432 g_free (grp_section
);
1440 int remove_old_list
= 0;
1441 struct stat stat_buf
;
1443 if (hotlist_state
.loaded
) {
1444 stat (hotlist_file_name
, &stat_buf
);
1445 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1451 if (!hotlist_file_name
)
1452 hotlist_file_name
= mhl_str_dir_plus_file (home_dir
, HOTLIST_FILENAME
);
1454 hotlist
= new_hotlist ();
1455 hotlist
->type
= HL_TYPE_GROUP
;
1456 hotlist
->label
= g_strdup (_(" Top level group "));
1457 hotlist
->up
= hotlist
;
1461 hotlist
->directory
= g_strdup ("Hotlist");
1463 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1466 load_group (hotlist
);
1467 hotlist_state
.loaded
= 1;
1469 * just to be sure we got copy
1471 hotlist_state
.modified
= 1;
1472 result
= save_hotlist ();
1473 hotlist_state
.modified
= 0;
1475 remove_old_list
= 1;
1477 message (D_ERROR
, _(" Hotlist Load "),
1478 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1482 hot_load_file (hotlist
);
1483 fclose (hotlist_file
);
1484 hotlist_state
.loaded
= 1;
1487 if (remove_old_list
) {
1488 clean_up_hotlist_groups ("Hotlist");
1492 stat (hotlist_file_name
, &stat_buf
);
1493 hotlist_file_mtime
= stat_buf
.st_mtime
;
1494 current_group
= hotlist
;
1498 static int list_level
= 0;
1501 hot_save_group (struct hotlist
*grp
)
1503 struct hotlist
*current
= grp
->head
;
1509 for (i = 0; i < n; i++) \
1510 putc (' ', hotlist_file); \
1513 for (;current
; current
= current
->next
)
1514 switch (current
->type
) {
1516 INDENT (list_level
);
1517 fputs ("GROUP \"", hotlist_file
);
1518 for (s
= current
->label
; *s
; s
++) {
1519 if (*s
== '"' || *s
== '\\')
1520 putc ('\\', hotlist_file
);
1521 putc (*s
, hotlist_file
);
1523 fputs ("\"\n", hotlist_file
);
1525 hot_save_group (current
);
1527 INDENT (list_level
);
1528 fputs ("ENDGROUP\n", hotlist_file
);
1532 fputs ("ENTRY \"", hotlist_file
);
1533 for (s
= current
->label
; *s
; s
++) {
1534 if (*s
== '"' || *s
== '\\')
1535 putc ('\\', hotlist_file
);
1536 putc (*s
, hotlist_file
);
1538 fputs ("\" URL \"", hotlist_file
);
1539 for (s
= current
->directory
; *s
; s
++) {
1540 if (*s
== '"' || *s
== '\\')
1541 putc ('\\', hotlist_file
);
1542 putc (*s
, hotlist_file
);
1544 fputs ("\"\n", hotlist_file
);
1546 case HL_TYPE_COMMENT
:
1547 fprintf (hotlist_file
, "#%s\n", current
->label
);
1549 case HL_TYPE_DOTDOT
:
1555 int save_hotlist (void)
1558 struct stat stat_buf
;
1560 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1561 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", (char *) NULL
);
1563 rename (hotlist_file_name
, fbak
);
1564 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1565 if (stat (fbak
, &stat_buf
) == 0)
1566 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1568 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1569 hot_save_group (hotlist
);
1570 fclose (hotlist_file
);
1571 stat (hotlist_file_name
, &stat_buf
);
1572 hotlist_file_mtime
= stat_buf
.st_mtime
;
1574 hotlist_state
.modified
= 0;
1576 rename (fbak
, hotlist_file_name
);
1584 * Unload list from memory.
1585 * Don't confuse with hotlist_done() for GUI.
1587 void done_hotlist (void)
1590 remove_group (hotlist
);
1591 g_free (hotlist
->label
);
1592 g_free (hotlist
->directory
);
1597 hotlist_state
.loaded
= 0;
1599 g_free (hotlist_file_name
);
1600 hotlist_file_name
= 0;
1612 add_dotdot_to_list (void)
1614 if (current_group
!= hotlist
) {
1615 if (hotlist_has_dot_dot
!= 0)
1616 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, 0);