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.
36 #include <sys/types.h>
41 #include "tty.h" /* COLS */
42 #include "color.h" /* dialog_colors */
45 #include "setup.h" /* For profile_bname */
46 #include "profile.h" /* Load/save directories hotlist */
47 #include "wtools.h" /* QuickDialog */
48 #include "panel.h" /* current_panel */
49 #include "main.h" /* repaint_screen */
51 #include "key.h" /* KEY_M_CTRL */
52 #include "command.h" /* cmdline */
53 #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
60 #define BY (LINES - 6)
62 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
64 #define B_ADD_CURRENT B_USER
65 #define B_REMOVE (B_USER + 1)
66 #define B_NEW_GROUP (B_USER + 2)
67 #define B_NEW_ENTRY (B_USER + 3)
68 #define B_UP_GROUP (B_USER + 4)
69 #define B_INSERT (B_USER + 5)
70 #define B_APPEND (B_USER + 6)
71 #define B_MOVE (B_USER + 7)
74 #include "../vfs/gc.h"
75 #define B_FREE_ALL_VFS (B_USER + 8)
76 #define B_REFRESH_VFS (B_USER + 9)
79 int hotlist_has_dot_dot
= 1;
81 static WListbox
*l_hotlist
;
82 static WListbox
*l_movelist
;
84 static Dlg_head
*hotlist_dlg
;
85 static Dlg_head
*movelist_dlg
;
87 static WLabel
*pname
, *pname_group
, *movelist_group
;
98 * these parameters are intended to be user configurable
100 int expanded
; /* expanded view of all groups at startup */
103 * these reflect run time state
106 int loaded
; /* hotlist is loaded */
107 int readonly
; /* hotlist readonly */
108 int file_error
; /* parse error while reading file */
109 int running
; /* we are running dlg (and have to
111 int moving
; /* we are in moving hotlist currently */
112 int modified
; /* hotlist was modified */
113 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
116 static struct _hotlist_but
{
117 int ret_cmd
, flags
, y
, x
;
121 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), LIST_HOTLIST
},
122 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), LIST_HOTLIST
},
123 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), LIST_MOVELIST
},
124 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), LIST_MOVELIST
},
125 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), LIST_HOTLIST
},
126 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), LIST_HOTLIST
},
127 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
128 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), LIST_HOTLIST
|LIST_MOVELIST
},
129 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"), LIST_HOTLIST
},
131 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"), LIST_VFSLIST
},
132 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"), LIST_VFSLIST
},
134 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
137 /* Directory hotlist */
138 static struct hotlist
{
139 enum HotListType type
;
142 struct hotlist
*head
;
144 struct hotlist
*next
;
147 static struct hotlist
*current_group
;
149 static void init_movelist (int, struct hotlist
*);
150 static void add_new_group_cmd (void);
151 static void add_new_entry_cmd (void);
152 static void remove_from_hotlist (struct hotlist
*entry
);
153 static void load_hotlist (void);
154 static void add_dotdot_to_list (void);
156 #define new_hotlist() g_new0(struct hotlist, 1)
159 hotlist_refresh (Dlg_head
* dlg
)
161 common_dialog_repaint (dlg
);
162 attrset (COLOR_NORMAL
);
163 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
164 dlg
->cols
- (UX
* 2));
165 if (!hotlist_state
.moving
)
166 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
169 /* If current->data is 0, then we are dealing with a VFS pathname */
171 update_path_name (void)
173 const char *text
= "";
175 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
176 Dlg_head
*dlg
= list
->widget
.parent
;
179 if (list
->current
->data
!= 0) {
180 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
182 if (hlp
->type
== HL_TYPE_ENTRY
||
183 hlp
->type
== HL_TYPE_DOTDOT
)
184 text
= hlp
->directory
;
185 else if (hlp
->type
== HL_TYPE_GROUP
)
186 text
= _("Subgroup - press ENTER to see list");
188 text
= list
->current
->text
;
191 if (!hotlist_state
.moving
)
192 label_set_text (pname
,
193 name_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
195 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
196 if (!hotlist_state
.moving
)
197 label_set_text (pname_group
,
198 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
200 label_set_text (movelist_group
,
201 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
207 #define CHECK_BUFFER \
211 if ((i = strlen (current->label) + 3) > buflen) { \
213 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
218 static void fill_listbox (void)
220 struct hotlist
*current
= current_group
->head
;
225 buf
= g_malloc (buflen
= 1024);
229 switch (current
->type
) {
233 strcat (strcat (buf
, "->"), current
->label
);
234 if (hotlist_state
.moving
)
235 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
237 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
242 if (hotlist_state
.moving
)
243 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
245 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
250 current
= current
->next
;
257 unlink_entry (struct hotlist
*entry
)
259 struct hotlist
*current
= current_group
->head
;
261 if (current
== entry
)
262 current_group
->head
= entry
->next
;
264 while (current
&& current
->next
!= entry
)
265 current
= current
->next
;
267 current
->next
= entry
->next
;
274 static void add_name_to_list (const char *path
)
276 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
278 #endif /* !USE_VFS */
281 hotlist_button_callback (int action
)
286 struct hotlist
*saved
= current_group
;
287 struct hotlist
*item
;
288 struct hotlist
*moveto_item
= 0;
289 struct hotlist
*moveto_group
= 0;
292 if (!l_hotlist
->current
)
293 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
294 item
= l_hotlist
->current
->data
;
295 hotlist_state
.moving
= 1;
296 init_movelist (LIST_MOVELIST
, item
);
297 run_dlg (movelist_dlg
);
298 ret
= movelist_dlg
->ret_value
;
299 hotlist_state
.moving
= 0;
300 if (l_movelist
->current
)
301 moveto_item
= l_movelist
->current
->data
;
302 moveto_group
= current_group
;
303 destroy_dlg (movelist_dlg
);
304 current_group
= saved
;
306 return MSG_NOT_HANDLED
;
307 if (moveto_item
== item
)
308 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
309 it hardly changes anything ;) */
311 listbox_remove_current (l_hotlist
, 1);
312 item
->up
= moveto_group
;
313 if (!moveto_group
->head
)
314 moveto_group
->head
= item
;
315 else if (!moveto_item
) { /* we have group with just comments */
316 struct hotlist
*p
= moveto_group
->head
;
322 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
323 if (!moveto_item
->next
)
324 moveto_item
->next
= item
;
326 item
->next
= moveto_item
->next
;
327 moveto_item
->next
= item
;
328 } else if (moveto_group
->head
== moveto_item
) {
329 moveto_group
->head
= item
;
330 item
->next
= moveto_item
;
332 struct hotlist
*p
= moveto_group
->head
;
334 while (p
->next
!= moveto_item
)
336 item
->next
= p
->next
;
339 listbox_remove_list (l_hotlist
);
342 hotlist_state
.modified
= 1;
343 return MSG_NOT_HANDLED
;
347 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
348 remove_from_hotlist (l_hotlist
->current
->data
);
349 return MSG_NOT_HANDLED
;
353 add_new_group_cmd ();
354 return MSG_NOT_HANDLED
;
359 return MSG_NOT_HANDLED
;
363 add_new_entry_cmd ();
364 return MSG_NOT_HANDLED
;
369 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
371 if (list
->current
->data
) {
372 struct hotlist
*hlp
=
373 (struct hotlist
*) list
->current
->data
;
374 if (hlp
->type
== HL_TYPE_ENTRY
)
376 else if (hlp
->type
== HL_TYPE_DOTDOT
) {
377 /* Fall through - go up */
381 listbox_remove_list (list
);
384 return MSG_NOT_HANDLED
;
390 /* Fall through if list empty - just go up */
394 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
395 listbox_remove_list (list
);
396 current_group
= current_group
->up
;
398 return MSG_NOT_HANDLED
;
408 listbox_remove_list (l_hotlist
);
409 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
410 vfs_fill_names (add_name_to_list
);
411 return MSG_NOT_HANDLED
;
422 hotlist_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
429 case DLG_UNHANDLED_KEY
:
431 case KEY_M_CTRL
| '\n':
436 if (hotlist_button_callback (B_ENTER
)) {
437 h
->ret_value
= B_ENTER
;
443 if (hotlist_state
.type
!= LIST_VFSLIST
)
444 return !hotlist_button_callback (B_UP_GROUP
);
446 return MSG_NOT_HANDLED
;
449 if (!hotlist_state
.moving
) {
450 hotlist_button_callback (B_REMOVE
);
457 if (!hotlist_state
.moving
) {
458 if (l_hotlist
->current
) {
459 if (l_hotlist
->current
->data
) {
460 struct hotlist
*hlp
=
461 (struct hotlist
*) l_hotlist
->current
->data
;
462 if (hlp
->type
== HL_TYPE_ENTRY
) {
464 g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
465 stuff (cmdline
, tmp
, 0);
468 h
->ret_value
= B_CANCEL
;
474 return MSG_HANDLED
; /* ignore key */
476 return MSG_NOT_HANDLED
;
479 if (hotlist_state
.moving
)
480 dlg_select_widget (l_movelist
);
482 dlg_select_widget (l_hotlist
);
483 /* always stay on hotlist */
487 attrset (MENU_ENTRY_COLOR
);
492 return default_dlg_callback (h
, msg
, parm
);
496 static int l_call (WListbox
*list
)
498 Dlg_head
*dlg
= list
->widget
.parent
;
501 if (list
->current
->data
) {
502 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
503 if (hlp
->type
== HL_TYPE_ENTRY
) {
504 dlg
->ret_value
= B_ENTER
;
508 hotlist_button_callback (B_ENTER
);
509 hotlist_callback (dlg
, DLG_POST_KEY
, '\n');
513 dlg
->ret_value
= B_ENTER
;
519 hotlist_button_callback (B_UP_GROUP
);
520 hotlist_callback (dlg
, DLG_POST_KEY
, 'u');
525 * Expands all button names (once) and recalculates button positions.
526 * returns number of columns in the dialog box, which is 10 chars longer
529 * If common width of the window (i.e. in xterm) is less than returned
530 * width - sorry :) (anyway this did not handled in previous version too)
533 init_i18n_stuff(int list_type
, int cols
)
536 static const char* cancel_but
= N_("&Cancel");
539 static int hotlist_i18n_flag
= 0;
541 if (!hotlist_i18n_flag
)
543 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
545 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
547 cancel_but
= _(cancel_but
);
548 hotlist_i18n_flag
= 1;
550 #endif /* ENABLE_NLS */
552 /* Dynamic resizing of buttonbars */
554 int len
[2], count
[2]; /* at most two lines of buttons */
557 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
558 len
[0] = len
[1] = count
[0] = count
[1] = 0;
560 /* Count len of buttonbars, assuming 2 extra space between buttons */
563 if (! (hotlist_but
[i
].type
& list_type
))
566 row
= hotlist_but
[i
].y
;
568 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
569 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
575 cols
= max(cols
, max(len
[0], len
[1]));
577 /* arrange buttons */
579 cur_x
[0] = cur_x
[1] = 0;
580 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
583 if (! (hotlist_but
[i
].type
& list_type
))
586 row
= hotlist_but
[i
].y
;
588 if (hotlist_but
[i
].x
!= 0)
590 /* not first int the row */
591 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
593 cols
- strlen (hotlist_but
[i
].text
) - 13;
595 hotlist_but
[i
].x
= cur_x
[row
];
598 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
599 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
607 init_hotlist (int list_type
)
610 const char *title
, *help_node
;
613 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
617 hotlist_state
.expanded
=
618 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
621 if (list_type
== LIST_VFSLIST
) {
622 title
= _("Active VFS directories");
623 help_node
= "[vfshot]"; /* FIXME - no such node */
625 title
= _("Directory hotlist");
626 help_node
= "[Hotlist]";
630 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
631 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
633 for (i
= 0; i
< BUTTONS
; i
++) {
634 if (hotlist_but
[i
].type
& list_type
)
635 add_widget (hotlist_dlg
,
636 button_new (BY
+ hotlist_but
[i
].y
,
637 BX
+ hotlist_but
[i
].x
,
638 hotlist_but
[i
].ret_cmd
,
639 hotlist_but
[i
].flags
,
641 hotlist_button_callback
));
644 /* We add the labels.
645 * pname will hold entry's pathname;
646 * pname_group will hold name of current group
648 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
649 add_widget (hotlist_dlg
, pname
);
650 if (!hotlist_state
.moving
) {
651 add_widget (hotlist_dlg
,
652 label_new (UY
- 12 + LINES
, UX
+ 1,
653 _(" Directory path ")));
655 /* This one holds the displayed pathname */
656 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
657 add_widget (hotlist_dlg
, pname_group
);
659 /* get new listbox */
661 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
664 /* Fill the hotlist with the active VFS or the hotlist */
666 if (list_type
== LIST_VFSLIST
) {
667 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
668 vfs_fill_names (add_name_to_list
);
670 #endif /* !USE_VFS */
673 add_widget (hotlist_dlg
, l_hotlist
);
674 /* add listbox to the dialogs */
678 init_movelist (int list_type
, struct hotlist
*item
)
681 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
682 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
687 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
688 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
691 for (i
= 0; i
< BUTTONS
; i
++) {
692 if (hotlist_but
[i
].type
& list_type
)
693 add_widget (movelist_dlg
,
694 button_new (BY
- 4 + hotlist_but
[i
].y
,
695 BX
+ hotlist_but
[i
].x
,
696 hotlist_but
[i
].ret_cmd
,
697 hotlist_but
[i
].flags
,
699 hotlist_button_callback
));
702 /* We add the labels. We are interested in the last one,
703 * that one will hold the path name label
705 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
706 add_widget (movelist_dlg
, movelist_group
);
707 /* get new listbox */
709 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
710 movelist_dlg
->lines
- 8, l_call
);
714 add_widget (movelist_dlg
, l_movelist
);
715 /* add listbox to the dialogs */
719 * Destroy the list dialog.
720 * Don't confuse with done_hotlist() for the list in memory.
722 static void hotlist_done (void)
724 destroy_dlg (hotlist_dlg
);
727 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
732 find_group_section (struct hotlist
*grp
)
734 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
738 /* 1.11.96 bor: added pos parameter to control placement of new item.
739 see widget.c, listbox_add_item()
740 now hotlist is in unsorted mode
743 HL_BEFORE_CURRENT
= 1
744 ,HL_AFTER_CURRENT
= 2
747 static struct hotlist
*
748 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
751 struct hotlist
*current
= NULL
;
754 * Hotlist is neither loaded nor loading.
755 * Must be called by "Ctrl-x a" before using hotlist.
760 if (l_hotlist
&& l_hotlist
->current
) {
761 current
= l_hotlist
->current
->data
;
763 /* Make sure `..' stays at the top of the list. */
764 if (current
->type
== HL_TYPE_DOTDOT
)
765 pos
= HL_AFTER_CURRENT
;
768 new = new_hotlist ();
772 new->directory
= directory
;
773 new->up
= current_group
;
775 if (type
== HL_TYPE_GROUP
) {
777 add_dotdot_to_list ();
778 current_group
= new->up
;
781 if (!current_group
->head
) { /* first element in group */
782 current_group
->head
= new;
783 } else if (pos
== HL_AFTER_CURRENT
) {
784 new->next
= current
->next
;
786 } else if (pos
== HL_BEFORE_CURRENT
&&
787 current
== current_group
->head
) {
788 /* should be inserted before first item */
790 current_group
->head
= new;
791 } else if (pos
== HL_BEFORE_CURRENT
) {
792 struct hotlist
*p
= current_group
->head
;
794 while (p
->next
!= current
)
799 } else { /* append at the end */
800 struct hotlist
*p
= current_group
->head
;
808 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&&
809 type
!= HL_TYPE_DOTDOT
) {
810 if (type
== HL_TYPE_GROUP
) {
811 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
813 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
816 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
817 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
825 * Support routine for add_new_entry_input()/add_new_group_input()
826 * Change positions of buttons (first three widgets).
828 * This is just a quick hack. Accurate procedure must take care of
829 * internationalized label lengths and total buttonbar length...assume
830 * 64 is longer anyway.
832 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
834 int i
, l
[3], space
, cur_x
;
836 for (i
= 0; i
< 3; i
++)
838 qw
[i
].text
= _(qw
[i
].text
);
839 l
[i
] = strlen (qw
[i
].text
) + 3;
841 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
843 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
845 qw
[i
].relative_x
= cur_x
;
846 qw
[i
].x_divisions
= len
;
849 #endif /* ENABLE_NLS */
852 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
853 const char *help
, char **r1
, char **r2
)
855 #define RELATIVE_Y_BUTTONS 4
856 #define RELATIVE_Y_LABEL_PTH 3
857 #define RELATIVE_Y_INPUT_PTH 4
859 QuickDialog Quick_input
;
860 static QuickWidget quick_widgets
[] = {
861 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
863 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
865 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
867 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
869 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
871 { quick_input
, 4, 80, 3, 0, "", 58, 0,
873 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
883 static int i18n_flag
= 0;
884 #endif /* ENABLE_NLS */
886 msglen(text1
, &lines1
, &cols1
);
887 msglen(text2
, &lines2
, &cols2
);
888 len
= max ((int) strlen (header
), cols1
);
889 len
= max (len
, cols2
) + 4;
895 add_widgets_i18n(quick_widgets
, len
);
898 #endif /* ENABLE_NLS */
900 Quick_input
.xlen
= len
;
901 Quick_input
.xpos
= -1;
902 Quick_input
.title
= header
;
903 Quick_input
.help
= help
;
904 Quick_input
.i18n
= 0;
905 quick_widgets
[6].text
= text1
;
906 quick_widgets
[4].text
= text2
;
907 quick_widgets
[5].text
= *r1
;
908 quick_widgets
[3].text
= *r2
;
910 for (i
= 0; i
< 7; i
++)
911 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
912 Quick_input
.ylen
= lines1
+ lines2
+ 7;
914 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
915 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
916 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
917 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
918 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
920 quick_widgets
[5].str_result
= r1
;
921 quick_widgets
[3].str_result
= r2
;
923 Quick_input
.widgets
= quick_widgets
;
924 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
930 static void add_new_entry_cmd (void)
932 char *title
, *url
, *to_free
;
935 /* Take current directory as default value for input fields */
936 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
938 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"),
939 _("Directory path"), "[Hotlist]", &title
, &url
);
944 if (!title
|| !*title
|| !url
|| !*url
) {
950 if (ret
== B_ENTER
|| ret
== B_APPEND
)
951 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_AFTER_CURRENT
);
953 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_BEFORE_CURRENT
);
955 hotlist_state
.modified
= 1;
959 add_new_group_input (const char *header
, const char *label
, char **result
)
962 QuickDialog Quick_input
;
963 static QuickWidget quick_widgets
[] = {
964 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
966 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
968 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
970 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
971 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, NULL
},
973 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
974 quick_widgets variable above */
980 static int i18n_flag
= 0;
981 #endif /* ENABLE_NLS */
983 msglen (label
, &lines
, &cols
);
984 len
= max ((int) strlen (header
), cols
) + 4;
990 add_widgets_i18n(quick_widgets
, len
);
993 #endif /* ENABLE_NLS */
995 Quick_input
.xlen
= len
;
996 Quick_input
.xpos
= -1;
997 Quick_input
.title
= header
;
998 Quick_input
.help
= "[Hotlist]";
999 Quick_input
.i18n
= 0;
1000 quick_widgets
[4].text
= label
;
1002 for (i
= 0; i
< 5; i
++)
1003 quick_widgets
[i
].y_divisions
= lines
+6;
1004 Quick_input
.ylen
= lines
+ 6;
1006 for (i
= 0; i
< 4; i
++)
1007 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
1009 quick_widgets
[3].str_result
= result
;
1010 quick_widgets
[3].text
= "";
1012 Quick_input
.widgets
= quick_widgets
;
1013 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
1019 static void add_new_group_cmd (void)
1024 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
1025 if (!ret
|| !label
|| !*label
)
1028 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1029 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_AFTER_CURRENT
);
1031 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_BEFORE_CURRENT
);
1033 hotlist_state
.modified
= 1;
1036 void add2hotlist_cmd (void)
1038 char *prompt
, *label
;
1039 const char *cp
= _("Label for \"%s\":");
1040 int l
= strlen (cp
);
1041 char *label_string
= g_strdup (current_panel
->cwd
);
1043 strip_password (label_string
, 1);
1045 prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
-2*UX
-(l
+8)));
1046 label
= input_dialog (_(" Add to hotlist "), prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1049 if (!label
|| !*label
) {
1050 g_free (label_string
);
1054 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, 0);
1055 hotlist_state
.modified
= 1;
1058 static void remove_group (struct hotlist
*grp
)
1060 struct hotlist
*current
= grp
->head
;
1063 struct hotlist
*next
= current
->next
;
1065 if (current
->type
== HL_TYPE_GROUP
)
1066 remove_group (current
);
1068 g_free (current
->label
);
1069 g_free (current
->directory
);
1077 static void remove_from_hotlist (struct hotlist
*entry
)
1079 if (entry
->type
== HL_TYPE_DOTDOT
)
1082 if (confirm_directory_hotlist_delete
) {
1086 title
= g_strconcat (_(" Remove: "),
1087 name_trunc (entry
->label
, 30),
1093 result
= query_dialog (title
,
1094 _("\n Are you sure you want to remove this entry?"),
1095 D_ERROR
, 2, _("&Yes"), _("&No"));
1103 if (entry
->type
== HL_TYPE_GROUP
) {
1108 header
= g_strconcat (_(" Remove: "),
1109 name_trunc (entry
->label
, 30),
1112 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1114 _("&Yes"), _("&No"));
1121 remove_group (entry
);
1124 unlink_entry (entry
);
1126 g_free (entry
->label
);
1127 g_free (entry
->directory
);
1129 /* now remove list entry from screen */
1130 listbox_remove_current (l_hotlist
, 1);
1131 hotlist_state
.modified
= 1;
1134 char *hotlist_cmd (int vfs_or_hotlist
)
1136 char *target
= NULL
;
1138 hotlist_state
.type
= vfs_or_hotlist
;
1141 init_hotlist (vfs_or_hotlist
);
1143 /* display file info */
1144 attrset (SELECTED_COLOR
);
1146 hotlist_state
.running
= 1;
1147 run_dlg (hotlist_dlg
);
1148 hotlist_state
.running
= 0;
1151 switch (hotlist_dlg
->ret_value
) {
1156 if (l_hotlist
->current
->data
) {
1157 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1158 target
= g_strdup (hlp
->directory
);
1160 target
= g_strdup (l_hotlist
->current
->text
);
1169 load_group (struct hotlist
*grp
)
1173 char *group_section
;
1174 struct hotlist
*current
= 0;
1176 group_section
= find_group_section (grp
);
1178 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1180 current_group
= grp
;
1182 while (profile_keys
){
1183 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1184 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1186 g_free (group_section
);
1188 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1190 while (profile_keys
){
1191 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1192 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1195 for (current
= grp
->head
; current
; current
= current
->next
)
1196 load_group (current
);
1201 #define TKN_STRING 2
1203 #define TKN_ENDGROUP 4
1204 #define TKN_COMMENT 5
1207 #define TKN_UNKNOWN 127
1209 static char *tkn_buf
;
1210 static int tkn_buf_length
;
1211 static int tkn_length
;
1213 static char *hotlist_file_name
;
1214 static FILE *hotlist_file
;
1215 static time_t hotlist_file_mtime
;
1217 static int hot_skip_blanks (void)
1221 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1227 static int hot_next_token (void)
1231 #define CHECK_BUF() \
1233 if (tkn_length == tkn_buf_length) \
1234 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1235 : ( g_malloc (tkn_buf_length = 1024)); \
1241 c
= hot_skip_blanks ();
1250 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1255 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1259 tkn_buf
[tkn_length
] = '\0';
1263 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1265 if ((c
= getc (hotlist_file
)) == EOF
)
1268 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1273 tkn_buf
[tkn_length
] = '\0';
1277 if ((c
= getc (hotlist_file
)) == EOF
)
1282 /* fall through; it is taken as normal character */
1287 tkn_buf
[tkn_length
++] = toupper(c
);
1288 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1290 ungetc (c
, hotlist_file
);
1292 tkn_buf
[tkn_length
] = '\0';
1293 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1295 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1297 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1298 return TKN_ENDGROUP
;
1299 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1307 #define SKIP_TO_EOL { \
1309 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1312 #define CHECK_TOKEN(_TKN_) \
1313 if ((tkn = hot_next_token ()) != _TKN_) { \
1314 hotlist_state.readonly = 1; \
1315 hotlist_state.file_error = 1; \
1316 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1317 tkn = hot_next_token (); \
1322 hot_load_group (struct hotlist
* grp
)
1325 struct hotlist
*new_grp
;
1328 current_group
= grp
;
1330 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1333 CHECK_TOKEN(TKN_STRING
);
1334 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1336 hot_load_group (new_grp
);
1337 current_group
= grp
;
1340 CHECK_TOKEN(TKN_STRING
);
1341 label
= g_strdup (tkn_buf
);
1342 CHECK_TOKEN(TKN_URL
);
1343 CHECK_TOKEN(TKN_STRING
);
1344 url
= g_strdup (tkn_buf
);
1345 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1349 label
= g_strdup (tkn_buf
);
1350 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1353 hotlist_state
.readonly
= 1;
1354 hotlist_state
.file_error
= 1;
1358 /* skip empty lines */
1361 hotlist_state
.readonly
= 1;
1362 hotlist_state
.file_error
= 1;
1370 hot_load_file (struct hotlist
* grp
)
1373 struct hotlist
*new_grp
;
1376 current_group
= grp
;
1378 while ((tkn
= hot_next_token())!= TKN_EOF
)
1381 CHECK_TOKEN(TKN_STRING
);
1382 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1384 hot_load_group (new_grp
);
1385 current_group
= grp
;
1388 CHECK_TOKEN(TKN_STRING
);
1389 label
= g_strdup (tkn_buf
);
1390 CHECK_TOKEN(TKN_URL
);
1391 CHECK_TOKEN(TKN_STRING
);
1392 url
= g_strdup (tkn_buf
);
1393 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1397 label
= g_strdup (tkn_buf
);
1398 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1401 /* skip empty lines */
1404 hotlist_state
.readonly
= 1;
1405 hotlist_state
.file_error
= 1;
1412 clean_up_hotlist_groups (const char *section
)
1418 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1419 if (profile_has_section (section
, profile_name
))
1420 profile_clean_section (section
, profile_name
);
1421 if (profile_has_section (grp_section
, profile_name
)) {
1422 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1424 while (profile_keys
) {
1425 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1426 clean_up_hotlist_groups (key
);
1428 profile_clean_section (grp_section
, profile_name
);
1430 g_free (grp_section
);
1438 int remove_old_list
= 0;
1439 struct stat stat_buf
;
1441 if (hotlist_state
.loaded
) {
1442 stat (hotlist_file_name
, &stat_buf
);
1443 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1449 if (!hotlist_file_name
)
1450 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1452 hotlist
= new_hotlist ();
1453 hotlist
->type
= HL_TYPE_GROUP
;
1454 hotlist
->label
= g_strdup (_(" Top level group "));
1455 hotlist
->up
= hotlist
;
1459 hotlist
->directory
= g_strdup ("Hotlist");
1461 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1464 load_group (hotlist
);
1465 hotlist_state
.loaded
= 1;
1467 * just to be sure we got copy
1469 hotlist_state
.modified
= 1;
1470 result
= save_hotlist ();
1471 hotlist_state
.modified
= 0;
1473 remove_old_list
= 1;
1475 message (D_ERROR
, _(" Hotlist Load "),
1476 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1480 hot_load_file (hotlist
);
1481 fclose (hotlist_file
);
1482 hotlist_state
.loaded
= 1;
1485 if (remove_old_list
) {
1486 clean_up_hotlist_groups ("Hotlist");
1490 stat (hotlist_file_name
, &stat_buf
);
1491 hotlist_file_mtime
= stat_buf
.st_mtime
;
1492 current_group
= hotlist
;
1496 static int list_level
= 0;
1499 hot_save_group (struct hotlist
*grp
)
1501 struct hotlist
*current
= grp
->head
;
1507 for (i = 0; i < n; i++) \
1508 putc (' ', hotlist_file); \
1511 for (;current
; current
= current
->next
)
1512 switch (current
->type
) {
1514 INDENT (list_level
);
1515 fputs ("GROUP \"", hotlist_file
);
1516 for (s
= current
->label
; *s
; s
++) {
1517 if (*s
== '"' || *s
== '\\')
1518 putc ('\\', hotlist_file
);
1519 putc (*s
, hotlist_file
);
1521 fputs ("\"\n", hotlist_file
);
1523 hot_save_group (current
);
1525 INDENT (list_level
);
1526 fputs ("ENDGROUP\n", hotlist_file
);
1530 fputs ("ENTRY \"", hotlist_file
);
1531 for (s
= current
->label
; *s
; s
++) {
1532 if (*s
== '"' || *s
== '\\')
1533 putc ('\\', hotlist_file
);
1534 putc (*s
, hotlist_file
);
1536 fputs ("\" URL \"", hotlist_file
);
1537 for (s
= current
->directory
; *s
; s
++) {
1538 if (*s
== '"' || *s
== '\\')
1539 putc ('\\', hotlist_file
);
1540 putc (*s
, hotlist_file
);
1542 fputs ("\"\n", hotlist_file
);
1544 case HL_TYPE_COMMENT
:
1545 fprintf (hotlist_file
, "#%s\n", current
->label
);
1547 case HL_TYPE_DOTDOT
:
1553 int save_hotlist (void)
1556 struct stat stat_buf
;
1558 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1559 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", (char *) NULL
);
1561 rename (hotlist_file_name
, fbak
);
1562 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1563 if (stat (fbak
, &stat_buf
) == 0)
1564 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1566 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1567 hot_save_group (hotlist
);
1568 fclose (hotlist_file
);
1569 stat (hotlist_file_name
, &stat_buf
);
1570 hotlist_file_mtime
= stat_buf
.st_mtime
;
1572 hotlist_state
.modified
= 0;
1574 rename (fbak
, hotlist_file_name
);
1582 * Unload list from memory.
1583 * Don't confuse with hotlist_done() for GUI.
1585 void done_hotlist (void)
1588 remove_group (hotlist
);
1589 g_free (hotlist
->label
);
1590 g_free (hotlist
->directory
);
1595 hotlist_state
.loaded
= 0;
1597 g_free (hotlist_file_name
);
1598 hotlist_file_name
= 0;
1610 add_dotdot_to_list (void)
1612 if (current_group
!= hotlist
) {
1613 if (hotlist_has_dot_dot
!= 0)
1614 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, 0);