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/string.h>
42 #include "tty.h" /* COLS */
43 #include "color.h" /* dialog_colors */
46 #include "setup.h" /* For profile_bname */
47 #include "profile.h" /* Load/save directories hotlist */
48 #include "wtools.h" /* QuickDialog */
49 #include "panel.h" /* current_panel */
50 #include "main.h" /* repaint_screen */
52 #include "key.h" /* KEY_M_CTRL */
53 #include "command.h" /* cmdline */
54 #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
61 #define BY (LINES - 6)
63 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
65 #define B_ADD_CURRENT B_USER
66 #define B_REMOVE (B_USER + 1)
67 #define B_NEW_GROUP (B_USER + 2)
68 #define B_NEW_ENTRY (B_USER + 3)
69 #define B_UP_GROUP (B_USER + 4)
70 #define B_INSERT (B_USER + 5)
71 #define B_APPEND (B_USER + 6)
72 #define B_MOVE (B_USER + 7)
75 #include "../vfs/gc.h"
76 #define B_FREE_ALL_VFS (B_USER + 8)
77 #define B_REFRESH_VFS (B_USER + 9)
80 int hotlist_has_dot_dot
= 1;
82 static WListbox
*l_hotlist
;
83 static WListbox
*l_movelist
;
85 static Dlg_head
*hotlist_dlg
;
86 static Dlg_head
*movelist_dlg
;
88 static WLabel
*pname
, *pname_group
, *movelist_group
;
99 * these parameters are intended to be user configurable
101 int expanded
; /* expanded view of all groups at startup */
104 * these reflect run time state
107 int loaded
; /* hotlist is loaded */
108 int readonly
; /* hotlist readonly */
109 int file_error
; /* parse error while reading file */
110 int running
; /* we are running dlg (and have to
112 int moving
; /* we are in moving hotlist currently */
113 int modified
; /* hotlist was modified */
114 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
117 static struct _hotlist_but
{
118 int ret_cmd
, flags
, y
, x
;
122 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), LIST_HOTLIST
},
123 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), LIST_HOTLIST
},
124 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), LIST_MOVELIST
},
125 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), LIST_MOVELIST
},
126 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), LIST_HOTLIST
},
127 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), LIST_HOTLIST
},
128 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
129 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), LIST_HOTLIST
|LIST_MOVELIST
},
130 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"), LIST_HOTLIST
},
132 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"), LIST_VFSLIST
},
133 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"), LIST_VFSLIST
},
135 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
138 /* Directory hotlist */
139 static struct hotlist
{
140 enum HotListType type
;
143 struct hotlist
*head
;
145 struct hotlist
*next
;
148 static struct hotlist
*current_group
;
150 static void init_movelist (int, struct hotlist
*);
151 static void add_new_group_cmd (void);
152 static void add_new_entry_cmd (void);
153 static void remove_from_hotlist (struct hotlist
*entry
);
154 static void load_hotlist (void);
155 static void add_dotdot_to_list (void);
157 #define new_hotlist() g_new0(struct hotlist, 1)
160 hotlist_refresh (Dlg_head
* dlg
)
162 common_dialog_repaint (dlg
);
163 attrset (COLOR_NORMAL
);
164 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
165 dlg
->cols
- (UX
* 2));
166 if (!hotlist_state
.moving
)
167 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
170 /* If current->data is 0, then we are dealing with a VFS pathname */
172 update_path_name (void)
174 const char *text
= "";
176 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
177 Dlg_head
*dlg
= list
->widget
.parent
;
180 if (list
->current
->data
!= 0) {
181 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
183 if (hlp
->type
== HL_TYPE_ENTRY
||
184 hlp
->type
== HL_TYPE_DOTDOT
)
185 text
= hlp
->directory
;
186 else if (hlp
->type
== HL_TYPE_GROUP
)
187 text
= _("Subgroup - press ENTER to see list");
189 text
= list
->current
->text
;
192 if (!hotlist_state
.moving
)
193 label_set_text (pname
,
194 name_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
196 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
197 if (!hotlist_state
.moving
)
198 label_set_text (pname_group
,
199 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
201 label_set_text (movelist_group
,
202 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
208 #define CHECK_BUFFER \
212 if ((i = strlen (current->label) + 3) > buflen) { \
214 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
219 static void fill_listbox (void)
221 struct hotlist
*current
= current_group
->head
;
226 buf
= g_malloc (buflen
= 1024);
230 switch (current
->type
) {
234 strcat (strcat (buf
, "->"), current
->label
);
235 if (hotlist_state
.moving
)
236 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
238 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
243 if (hotlist_state
.moving
)
244 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
246 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
251 current
= current
->next
;
258 unlink_entry (struct hotlist
*entry
)
260 struct hotlist
*current
= current_group
->head
;
262 if (current
== entry
)
263 current_group
->head
= entry
->next
;
265 while (current
&& current
->next
!= entry
)
266 current
= current
->next
;
268 current
->next
= entry
->next
;
275 static void add_name_to_list (const char *path
)
277 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
279 #endif /* !USE_VFS */
282 hotlist_button_callback (int action
)
287 struct hotlist
*saved
= current_group
;
288 struct hotlist
*item
;
289 struct hotlist
*moveto_item
= 0;
290 struct hotlist
*moveto_group
= 0;
293 if (!l_hotlist
->current
)
294 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
295 item
= l_hotlist
->current
->data
;
296 hotlist_state
.moving
= 1;
297 init_movelist (LIST_MOVELIST
, item
);
298 run_dlg (movelist_dlg
);
299 ret
= movelist_dlg
->ret_value
;
300 hotlist_state
.moving
= 0;
301 if (l_movelist
->current
)
302 moveto_item
= l_movelist
->current
->data
;
303 moveto_group
= current_group
;
304 destroy_dlg (movelist_dlg
);
305 current_group
= saved
;
307 return MSG_NOT_HANDLED
;
308 if (moveto_item
== item
)
309 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
310 it hardly changes anything ;) */
312 listbox_remove_current (l_hotlist
, 1);
313 item
->up
= moveto_group
;
314 if (!moveto_group
->head
)
315 moveto_group
->head
= item
;
316 else if (!moveto_item
) { /* we have group with just comments */
317 struct hotlist
*p
= moveto_group
->head
;
323 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
324 if (!moveto_item
->next
)
325 moveto_item
->next
= item
;
327 item
->next
= moveto_item
->next
;
328 moveto_item
->next
= item
;
329 } else if (moveto_group
->head
== moveto_item
) {
330 moveto_group
->head
= item
;
331 item
->next
= moveto_item
;
333 struct hotlist
*p
= moveto_group
->head
;
335 while (p
->next
!= moveto_item
)
337 item
->next
= p
->next
;
340 listbox_remove_list (l_hotlist
);
343 hotlist_state
.modified
= 1;
344 return MSG_NOT_HANDLED
;
348 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
349 remove_from_hotlist (l_hotlist
->current
->data
);
350 return MSG_NOT_HANDLED
;
354 add_new_group_cmd ();
355 return MSG_NOT_HANDLED
;
360 return MSG_NOT_HANDLED
;
364 add_new_entry_cmd ();
365 return MSG_NOT_HANDLED
;
370 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
372 if (list
->current
->data
) {
373 struct hotlist
*hlp
=
374 (struct hotlist
*) list
->current
->data
;
375 if (hlp
->type
== HL_TYPE_ENTRY
)
377 else if (hlp
->type
== HL_TYPE_DOTDOT
) {
378 /* Fall through - go up */
382 listbox_remove_list (list
);
385 return MSG_NOT_HANDLED
;
391 /* Fall through if list empty - just go up */
395 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
396 listbox_remove_list (list
);
397 current_group
= current_group
->up
;
399 return MSG_NOT_HANDLED
;
409 listbox_remove_list (l_hotlist
);
410 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
411 vfs_fill_names (add_name_to_list
);
412 return MSG_NOT_HANDLED
;
423 hotlist_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
430 case DLG_UNHANDLED_KEY
:
432 case KEY_M_CTRL
| '\n':
437 if (hotlist_button_callback (B_ENTER
)) {
438 h
->ret_value
= B_ENTER
;
444 if (hotlist_state
.type
!= LIST_VFSLIST
)
445 return !hotlist_button_callback (B_UP_GROUP
);
447 return MSG_NOT_HANDLED
;
450 if (!hotlist_state
.moving
) {
451 hotlist_button_callback (B_REMOVE
);
458 if (!hotlist_state
.moving
) {
459 if (l_hotlist
->current
) {
460 if (l_hotlist
->current
->data
) {
461 struct hotlist
*hlp
=
462 (struct hotlist
*) l_hotlist
->current
->data
;
463 if (hlp
->type
== HL_TYPE_ENTRY
) {
465 g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
466 stuff (cmdline
, tmp
, 0);
469 h
->ret_value
= B_CANCEL
;
475 return MSG_HANDLED
; /* ignore key */
477 return MSG_NOT_HANDLED
;
480 if (hotlist_state
.moving
)
481 dlg_select_widget (l_movelist
);
483 dlg_select_widget (l_hotlist
);
484 /* always stay on hotlist */
488 attrset (MENU_ENTRY_COLOR
);
493 return default_dlg_callback (h
, msg
, parm
);
497 static int l_call (WListbox
*list
)
499 Dlg_head
*dlg
= list
->widget
.parent
;
502 if (list
->current
->data
) {
503 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
504 if (hlp
->type
== HL_TYPE_ENTRY
) {
505 dlg
->ret_value
= B_ENTER
;
509 hotlist_button_callback (B_ENTER
);
510 hotlist_callback (dlg
, DLG_POST_KEY
, '\n');
514 dlg
->ret_value
= B_ENTER
;
520 hotlist_button_callback (B_UP_GROUP
);
521 hotlist_callback (dlg
, DLG_POST_KEY
, 'u');
526 * Expands all button names (once) and recalculates button positions.
527 * returns number of columns in the dialog box, which is 10 chars longer
530 * If common width of the window (i.e. in xterm) is less than returned
531 * width - sorry :) (anyway this did not handled in previous version too)
534 init_i18n_stuff(int list_type
, int cols
)
537 static const char* cancel_but
= N_("&Cancel");
540 static int hotlist_i18n_flag
= 0;
542 if (!hotlist_i18n_flag
)
544 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
546 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
548 cancel_but
= _(cancel_but
);
549 hotlist_i18n_flag
= 1;
551 #endif /* ENABLE_NLS */
553 /* Dynamic resizing of buttonbars */
555 int len
[2], count
[2]; /* at most two lines of buttons */
558 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
559 len
[0] = len
[1] = count
[0] = count
[1] = 0;
561 /* Count len of buttonbars, assuming 2 extra space between buttons */
564 if (! (hotlist_but
[i
].type
& list_type
))
567 row
= hotlist_but
[i
].y
;
569 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
570 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
576 cols
= max(cols
, max(len
[0], len
[1]));
578 /* arrange buttons */
580 cur_x
[0] = cur_x
[1] = 0;
581 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
584 if (! (hotlist_but
[i
].type
& list_type
))
587 row
= hotlist_but
[i
].y
;
589 if (hotlist_but
[i
].x
!= 0)
591 /* not first int the row */
592 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
594 cols
- strlen (hotlist_but
[i
].text
) - 13;
596 hotlist_but
[i
].x
= cur_x
[row
];
599 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
600 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
608 init_hotlist (int list_type
)
611 const char *title
, *help_node
;
614 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
618 hotlist_state
.expanded
=
619 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
622 if (list_type
== LIST_VFSLIST
) {
623 title
= _("Active VFS directories");
624 help_node
= "[vfshot]"; /* FIXME - no such node */
626 title
= _("Directory hotlist");
627 help_node
= "[Hotlist]";
631 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
632 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
634 for (i
= 0; i
< BUTTONS
; i
++) {
635 if (hotlist_but
[i
].type
& list_type
)
636 add_widget (hotlist_dlg
,
637 button_new (BY
+ hotlist_but
[i
].y
,
638 BX
+ hotlist_but
[i
].x
,
639 hotlist_but
[i
].ret_cmd
,
640 hotlist_but
[i
].flags
,
642 hotlist_button_callback
));
645 /* We add the labels.
646 * pname will hold entry's pathname;
647 * pname_group will hold name of current group
649 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
650 add_widget (hotlist_dlg
, pname
);
651 if (!hotlist_state
.moving
) {
652 add_widget (hotlist_dlg
,
653 label_new (UY
- 12 + LINES
, UX
+ 1,
654 _(" Directory path ")));
656 /* This one holds the displayed pathname */
657 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
658 add_widget (hotlist_dlg
, pname_group
);
660 /* get new listbox */
662 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
665 /* Fill the hotlist with the active VFS or the hotlist */
667 if (list_type
== LIST_VFSLIST
) {
668 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
669 vfs_fill_names (add_name_to_list
);
671 #endif /* !USE_VFS */
674 add_widget (hotlist_dlg
, l_hotlist
);
675 /* add listbox to the dialogs */
679 init_movelist (int list_type
, struct hotlist
*item
)
682 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
683 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
688 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
689 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
692 for (i
= 0; i
< BUTTONS
; i
++) {
693 if (hotlist_but
[i
].type
& list_type
)
694 add_widget (movelist_dlg
,
695 button_new (BY
- 4 + hotlist_but
[i
].y
,
696 BX
+ hotlist_but
[i
].x
,
697 hotlist_but
[i
].ret_cmd
,
698 hotlist_but
[i
].flags
,
700 hotlist_button_callback
));
703 /* We add the labels. We are interested in the last one,
704 * that one will hold the path name label
706 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
707 add_widget (movelist_dlg
, movelist_group
);
708 /* get new listbox */
710 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
711 movelist_dlg
->lines
- 8, l_call
);
715 add_widget (movelist_dlg
, l_movelist
);
716 /* add listbox to the dialogs */
720 * Destroy the list dialog.
721 * Don't confuse with done_hotlist() for the list in memory.
723 static void hotlist_done (void)
725 destroy_dlg (hotlist_dlg
);
728 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
733 find_group_section (struct hotlist
*grp
)
735 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
739 /* 1.11.96 bor: added pos parameter to control placement of new item.
740 see widget.c, listbox_add_item()
741 now hotlist is in unsorted mode
744 HL_BEFORE_CURRENT
= 1
745 ,HL_AFTER_CURRENT
= 2
748 static struct hotlist
*
749 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
752 struct hotlist
*current
= NULL
;
755 * Hotlist is neither loaded nor loading.
756 * Must be called by "Ctrl-x a" before using hotlist.
761 if (l_hotlist
&& l_hotlist
->current
) {
762 current
= l_hotlist
->current
->data
;
764 /* Make sure `..' stays at the top of the list. */
765 if (current
->type
== HL_TYPE_DOTDOT
)
766 pos
= HL_AFTER_CURRENT
;
769 new = new_hotlist ();
773 new->directory
= directory
;
774 new->up
= current_group
;
776 if (type
== HL_TYPE_GROUP
) {
778 add_dotdot_to_list ();
779 current_group
= new->up
;
782 if (!current_group
->head
) { /* first element in group */
783 current_group
->head
= new;
784 } else if (pos
== HL_AFTER_CURRENT
) {
785 new->next
= current
->next
;
787 } else if (pos
== HL_BEFORE_CURRENT
&&
788 current
== current_group
->head
) {
789 /* should be inserted before first item */
791 current_group
->head
= new;
792 } else if (pos
== HL_BEFORE_CURRENT
) {
793 struct hotlist
*p
= current_group
->head
;
795 while (p
->next
!= current
)
800 } else { /* append at the end */
801 struct hotlist
*p
= current_group
->head
;
809 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&&
810 type
!= HL_TYPE_DOTDOT
) {
811 if (type
== HL_TYPE_GROUP
) {
812 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
814 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
817 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
818 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
826 * Support routine for add_new_entry_input()/add_new_group_input()
827 * Change positions of buttons (first three widgets).
829 * This is just a quick hack. Accurate procedure must take care of
830 * internationalized label lengths and total buttonbar length...assume
831 * 64 is longer anyway.
833 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
835 int i
, l
[3], space
, cur_x
;
837 for (i
= 0; i
< 3; i
++)
839 qw
[i
].text
= _(qw
[i
].text
);
840 l
[i
] = strlen (qw
[i
].text
) + 3;
842 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
844 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
846 qw
[i
].relative_x
= cur_x
;
847 qw
[i
].x_divisions
= len
;
850 #endif /* ENABLE_NLS */
853 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
854 const char *help
, char **r1
, char **r2
)
856 #define RELATIVE_Y_BUTTONS 4
857 #define RELATIVE_Y_LABEL_PTH 3
858 #define RELATIVE_Y_INPUT_PTH 4
860 QuickDialog Quick_input
;
861 static QuickWidget quick_widgets
[] = {
862 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
864 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
866 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
868 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
870 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
872 { quick_input
, 4, 80, 3, 0, "", 58, 0,
874 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
884 static int i18n_flag
= 0;
885 #endif /* ENABLE_NLS */
887 msglen(text1
, &lines1
, &cols1
);
888 msglen(text2
, &lines2
, &cols2
);
889 len
= max ((int) strlen (header
), cols1
);
890 len
= max (len
, cols2
) + 4;
896 add_widgets_i18n(quick_widgets
, len
);
899 #endif /* ENABLE_NLS */
901 Quick_input
.xlen
= len
;
902 Quick_input
.xpos
= -1;
903 Quick_input
.title
= header
;
904 Quick_input
.help
= help
;
905 Quick_input
.i18n
= 0;
906 quick_widgets
[6].text
= text1
;
907 quick_widgets
[4].text
= text2
;
908 quick_widgets
[5].text
= *r1
;
909 quick_widgets
[3].text
= *r2
;
911 for (i
= 0; i
< 7; i
++)
912 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
913 Quick_input
.ylen
= lines1
+ lines2
+ 7;
915 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
916 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
917 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
918 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
919 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
921 quick_widgets
[5].str_result
= r1
;
922 quick_widgets
[3].str_result
= r2
;
924 Quick_input
.widgets
= quick_widgets
;
925 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
931 static void add_new_entry_cmd (void)
933 char *title
, *url
, *to_free
;
936 /* Take current directory as default value for input fields */
937 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
939 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"),
940 _("Directory path"), "[Hotlist]", &title
, &url
);
945 if (!title
|| !*title
|| !url
|| !*url
) {
951 if (ret
== B_ENTER
|| ret
== B_APPEND
)
952 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_AFTER_CURRENT
);
954 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_BEFORE_CURRENT
);
956 hotlist_state
.modified
= 1;
960 add_new_group_input (const char *header
, const char *label
, char **result
)
963 QuickDialog Quick_input
;
964 static QuickWidget quick_widgets
[] = {
965 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
967 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
969 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
971 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
972 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, NULL
},
974 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
975 quick_widgets variable above */
981 static int i18n_flag
= 0;
982 #endif /* ENABLE_NLS */
984 msglen (label
, &lines
, &cols
);
985 len
= max ((int) strlen (header
), cols
) + 4;
991 add_widgets_i18n(quick_widgets
, len
);
994 #endif /* ENABLE_NLS */
996 Quick_input
.xlen
= len
;
997 Quick_input
.xpos
= -1;
998 Quick_input
.title
= header
;
999 Quick_input
.help
= "[Hotlist]";
1000 Quick_input
.i18n
= 0;
1001 quick_widgets
[4].text
= label
;
1003 for (i
= 0; i
< 5; i
++)
1004 quick_widgets
[i
].y_divisions
= lines
+6;
1005 Quick_input
.ylen
= lines
+ 6;
1007 for (i
= 0; i
< 4; i
++)
1008 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
1010 quick_widgets
[3].str_result
= result
;
1011 quick_widgets
[3].text
= "";
1013 Quick_input
.widgets
= quick_widgets
;
1014 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
1020 static void add_new_group_cmd (void)
1025 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
1026 if (!ret
|| !label
|| !*label
)
1029 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1030 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_AFTER_CURRENT
);
1032 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_BEFORE_CURRENT
);
1034 hotlist_state
.modified
= 1;
1037 void add2hotlist_cmd (void)
1039 char *prompt
, *label
;
1040 const char *cp
= _("Label for \"%s\":");
1041 int l
= strlen (cp
);
1042 char *label_string
= g_strdup (current_panel
->cwd
);
1044 strip_password (label_string
, 1);
1046 prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
-2*UX
-(l
+8)));
1047 label
= input_dialog (_(" Add to hotlist "), prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1050 if (!label
|| !*label
) {
1051 g_free (label_string
);
1055 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, 0);
1056 hotlist_state
.modified
= 1;
1059 static void remove_group (struct hotlist
*grp
)
1061 struct hotlist
*current
= grp
->head
;
1064 struct hotlist
*next
= current
->next
;
1066 if (current
->type
== HL_TYPE_GROUP
)
1067 remove_group (current
);
1069 g_free (current
->label
);
1070 g_free (current
->directory
);
1078 static void remove_from_hotlist (struct hotlist
*entry
)
1080 if (entry
->type
== HL_TYPE_DOTDOT
)
1083 if (confirm_directory_hotlist_delete
) {
1087 title
= g_strconcat (_(" Remove: "),
1088 name_trunc (entry
->label
, 30),
1094 result
= query_dialog (title
,
1095 _("\n Are you sure you want to remove this entry?"),
1096 D_ERROR
, 2, _("&Yes"), _("&No"));
1104 if (entry
->type
== HL_TYPE_GROUP
) {
1109 header
= g_strconcat (_(" Remove: "),
1110 name_trunc (entry
->label
, 30),
1113 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1115 _("&Yes"), _("&No"));
1122 remove_group (entry
);
1125 unlink_entry (entry
);
1127 g_free (entry
->label
);
1128 g_free (entry
->directory
);
1130 /* now remove list entry from screen */
1131 listbox_remove_current (l_hotlist
, 1);
1132 hotlist_state
.modified
= 1;
1135 char *hotlist_cmd (int vfs_or_hotlist
)
1137 char *target
= NULL
;
1139 hotlist_state
.type
= vfs_or_hotlist
;
1142 init_hotlist (vfs_or_hotlist
);
1144 /* display file info */
1145 attrset (SELECTED_COLOR
);
1147 hotlist_state
.running
= 1;
1148 run_dlg (hotlist_dlg
);
1149 hotlist_state
.running
= 0;
1152 switch (hotlist_dlg
->ret_value
) {
1157 if (l_hotlist
->current
->data
) {
1158 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1159 target
= g_strdup (hlp
->directory
);
1161 target
= g_strdup (l_hotlist
->current
->text
);
1170 load_group (struct hotlist
*grp
)
1174 char *group_section
;
1175 struct hotlist
*current
= 0;
1177 group_section
= find_group_section (grp
);
1179 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1181 current_group
= grp
;
1183 while (profile_keys
){
1184 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1185 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1187 g_free (group_section
);
1189 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1191 while (profile_keys
){
1192 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1193 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1196 for (current
= grp
->head
; current
; current
= current
->next
)
1197 load_group (current
);
1202 #define TKN_STRING 2
1204 #define TKN_ENDGROUP 4
1205 #define TKN_COMMENT 5
1208 #define TKN_UNKNOWN 127
1210 static char *tkn_buf
;
1211 static int tkn_buf_length
;
1212 static int tkn_length
;
1214 static char *hotlist_file_name
;
1215 static FILE *hotlist_file
;
1216 static time_t hotlist_file_mtime
;
1218 static int hot_skip_blanks (void)
1222 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1228 static int hot_next_token (void)
1232 #define CHECK_BUF() \
1234 if (tkn_length == tkn_buf_length) \
1235 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1236 : ( g_malloc (tkn_buf_length = 1024)); \
1242 c
= hot_skip_blanks ();
1251 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1256 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1260 tkn_buf
[tkn_length
] = '\0';
1264 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1266 if ((c
= getc (hotlist_file
)) == EOF
)
1269 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1274 tkn_buf
[tkn_length
] = '\0';
1278 if ((c
= getc (hotlist_file
)) == EOF
)
1283 /* fall through; it is taken as normal character */
1288 tkn_buf
[tkn_length
++] = toupper(c
);
1289 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1291 ungetc (c
, hotlist_file
);
1293 tkn_buf
[tkn_length
] = '\0';
1294 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1296 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1298 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1299 return TKN_ENDGROUP
;
1300 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1308 #define SKIP_TO_EOL { \
1310 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1313 #define CHECK_TOKEN(_TKN_) \
1314 if ((tkn = hot_next_token ()) != _TKN_) { \
1315 hotlist_state.readonly = 1; \
1316 hotlist_state.file_error = 1; \
1317 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1318 tkn = hot_next_token (); \
1323 hot_load_group (struct hotlist
* grp
)
1326 struct hotlist
*new_grp
;
1329 current_group
= grp
;
1331 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1334 CHECK_TOKEN(TKN_STRING
);
1335 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1337 hot_load_group (new_grp
);
1338 current_group
= grp
;
1341 CHECK_TOKEN(TKN_STRING
);
1342 label
= g_strdup (tkn_buf
);
1343 CHECK_TOKEN(TKN_URL
);
1344 CHECK_TOKEN(TKN_STRING
);
1345 url
= g_strdup (tkn_buf
);
1346 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1350 label
= g_strdup (tkn_buf
);
1351 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1354 hotlist_state
.readonly
= 1;
1355 hotlist_state
.file_error
= 1;
1359 /* skip empty lines */
1362 hotlist_state
.readonly
= 1;
1363 hotlist_state
.file_error
= 1;
1371 hot_load_file (struct hotlist
* grp
)
1374 struct hotlist
*new_grp
;
1377 current_group
= grp
;
1379 while ((tkn
= hot_next_token())!= TKN_EOF
)
1382 CHECK_TOKEN(TKN_STRING
);
1383 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1385 hot_load_group (new_grp
);
1386 current_group
= grp
;
1389 CHECK_TOKEN(TKN_STRING
);
1390 label
= g_strdup (tkn_buf
);
1391 CHECK_TOKEN(TKN_URL
);
1392 CHECK_TOKEN(TKN_STRING
);
1393 url
= g_strdup (tkn_buf
);
1394 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1398 label
= g_strdup (tkn_buf
);
1399 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1402 /* skip empty lines */
1405 hotlist_state
.readonly
= 1;
1406 hotlist_state
.file_error
= 1;
1413 clean_up_hotlist_groups (const char *section
)
1419 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1420 if (profile_has_section (section
, profile_name
))
1421 profile_clean_section (section
, profile_name
);
1422 if (profile_has_section (grp_section
, profile_name
)) {
1423 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1425 while (profile_keys
) {
1426 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1427 clean_up_hotlist_groups (key
);
1429 profile_clean_section (grp_section
, profile_name
);
1431 g_free (grp_section
);
1439 int remove_old_list
= 0;
1440 struct stat stat_buf
;
1442 if (hotlist_state
.loaded
) {
1443 stat (hotlist_file_name
, &stat_buf
);
1444 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1450 if (!hotlist_file_name
)
1451 hotlist_file_name
= mhl_str_dir_plus_file (home_dir
, HOTLIST_FILENAME
);
1453 hotlist
= new_hotlist ();
1454 hotlist
->type
= HL_TYPE_GROUP
;
1455 hotlist
->label
= g_strdup (_(" Top level group "));
1456 hotlist
->up
= hotlist
;
1460 hotlist
->directory
= g_strdup ("Hotlist");
1462 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1465 load_group (hotlist
);
1466 hotlist_state
.loaded
= 1;
1468 * just to be sure we got copy
1470 hotlist_state
.modified
= 1;
1471 result
= save_hotlist ();
1472 hotlist_state
.modified
= 0;
1474 remove_old_list
= 1;
1476 message (D_ERROR
, _(" Hotlist Load "),
1477 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1481 hot_load_file (hotlist
);
1482 fclose (hotlist_file
);
1483 hotlist_state
.loaded
= 1;
1486 if (remove_old_list
) {
1487 clean_up_hotlist_groups ("Hotlist");
1491 stat (hotlist_file_name
, &stat_buf
);
1492 hotlist_file_mtime
= stat_buf
.st_mtime
;
1493 current_group
= hotlist
;
1497 static int list_level
= 0;
1500 hot_save_group (struct hotlist
*grp
)
1502 struct hotlist
*current
= grp
->head
;
1508 for (i = 0; i < n; i++) \
1509 putc (' ', hotlist_file); \
1512 for (;current
; current
= current
->next
)
1513 switch (current
->type
) {
1515 INDENT (list_level
);
1516 fputs ("GROUP \"", hotlist_file
);
1517 for (s
= current
->label
; *s
; s
++) {
1518 if (*s
== '"' || *s
== '\\')
1519 putc ('\\', hotlist_file
);
1520 putc (*s
, hotlist_file
);
1522 fputs ("\"\n", hotlist_file
);
1524 hot_save_group (current
);
1526 INDENT (list_level
);
1527 fputs ("ENDGROUP\n", hotlist_file
);
1531 fputs ("ENTRY \"", hotlist_file
);
1532 for (s
= current
->label
; *s
; s
++) {
1533 if (*s
== '"' || *s
== '\\')
1534 putc ('\\', hotlist_file
);
1535 putc (*s
, hotlist_file
);
1537 fputs ("\" URL \"", hotlist_file
);
1538 for (s
= current
->directory
; *s
; s
++) {
1539 if (*s
== '"' || *s
== '\\')
1540 putc ('\\', hotlist_file
);
1541 putc (*s
, hotlist_file
);
1543 fputs ("\"\n", hotlist_file
);
1545 case HL_TYPE_COMMENT
:
1546 fprintf (hotlist_file
, "#%s\n", current
->label
);
1548 case HL_TYPE_DOTDOT
:
1554 int save_hotlist (void)
1557 struct stat stat_buf
;
1559 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1560 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", (char *) NULL
);
1562 rename (hotlist_file_name
, fbak
);
1563 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1564 if (stat (fbak
, &stat_buf
) == 0)
1565 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1567 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1568 hot_save_group (hotlist
);
1569 fclose (hotlist_file
);
1570 stat (hotlist_file_name
, &stat_buf
);
1571 hotlist_file_mtime
= stat_buf
.st_mtime
;
1573 hotlist_state
.modified
= 0;
1575 rename (fbak
, hotlist_file_name
);
1583 * Unload list from memory.
1584 * Don't confuse with hotlist_done() for GUI.
1586 void done_hotlist (void)
1589 remove_group (hotlist
);
1590 g_free (hotlist
->label
);
1591 g_free (hotlist
->directory
);
1596 hotlist_state
.loaded
= 0;
1598 g_free (hotlist_file_name
);
1599 hotlist_file_name
= 0;
1611 add_dotdot_to_list (void)
1613 if (current_group
!= hotlist
) {
1614 if (hotlist_has_dot_dot
!= 0)
1615 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, 0);