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 */
59 #define BY (LINES - 6)
61 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
63 #define B_ADD_CURRENT B_USER
64 #define B_REMOVE (B_USER + 1)
65 #define B_NEW_GROUP (B_USER + 2)
66 #define B_NEW_ENTRY (B_USER + 3)
67 #define B_UP_GROUP (B_USER + 4)
68 #define B_INSERT (B_USER + 5)
69 #define B_APPEND (B_USER + 6)
70 #define B_MOVE (B_USER + 7)
73 #include "../vfs/gc.h"
74 #define B_FREE_ALL_VFS (B_USER + 8)
75 #define B_REFRESH_VFS (B_USER + 9)
78 int hotlist_has_dot_dot
= 1;
80 static WListbox
*l_hotlist
;
81 static WListbox
*l_movelist
;
83 static Dlg_head
*hotlist_dlg
;
84 static Dlg_head
*movelist_dlg
;
86 static WLabel
*pname
, *pname_group
, *movelist_group
;
97 * these parameters are intended to be user configurable
99 int expanded
; /* expanded view of all groups at startup */
102 * these reflect run time state
105 int loaded
; /* hotlist is loaded */
106 int readonly
; /* hotlist readonly */
107 int file_error
; /* parse error while reading file */
108 int running
; /* we are running dlg (and have to
110 int moving
; /* we are in moving hotlist currently */
111 int modified
; /* hotlist was modified */
112 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
115 static struct _hotlist_but
{
116 int ret_cmd
, flags
, y
, x
;
120 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), LIST_HOTLIST
},
121 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), LIST_HOTLIST
},
122 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), LIST_MOVELIST
},
123 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), LIST_MOVELIST
},
124 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), LIST_HOTLIST
},
125 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), LIST_HOTLIST
},
126 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
127 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), LIST_HOTLIST
|LIST_MOVELIST
},
128 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"), LIST_HOTLIST
},
130 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"), LIST_VFSLIST
},
131 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"), LIST_VFSLIST
},
133 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
136 /* Directory hotlist */
137 static struct hotlist
{
138 enum HotListType type
;
141 struct hotlist
*head
;
143 struct hotlist
*next
;
146 static struct hotlist
*current_group
;
148 static void init_movelist (int, struct hotlist
*);
149 static void add_new_group_cmd (void);
150 static void add_new_entry_cmd (void);
151 static void remove_from_hotlist (struct hotlist
*entry
);
152 static void load_hotlist (void);
153 static void add_dotdot_to_list (void);
155 #define new_hotlist() g_new0(struct hotlist, 1)
158 hotlist_refresh (Dlg_head
* dlg
)
160 common_dialog_repaint (dlg
);
161 attrset (COLOR_NORMAL
);
162 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
163 dlg
->cols
- (UX
* 2));
164 if (!hotlist_state
.moving
)
165 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
168 /* If current->data is 0, then we are dealing with a VFS pathname */
170 update_path_name (void)
172 const char *text
= "";
174 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
175 Dlg_head
*dlg
= list
->widget
.parent
;
178 if (list
->current
->data
!= 0) {
179 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
181 if (hlp
->type
== HL_TYPE_ENTRY
||
182 hlp
->type
== HL_TYPE_DOTDOT
)
183 text
= hlp
->directory
;
184 else if (hlp
->type
== HL_TYPE_GROUP
)
185 text
= _("Subgroup - press ENTER to see list");
187 text
= list
->current
->text
;
190 if (!hotlist_state
.moving
)
191 label_set_text (pname
,
192 name_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
194 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
195 if (!hotlist_state
.moving
)
196 label_set_text (pname_group
,
197 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
199 label_set_text (movelist_group
,
200 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
206 #define CHECK_BUFFER \
210 if ((i = strlen (current->label) + 3) > buflen) { \
212 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
217 static void fill_listbox (void)
219 struct hotlist
*current
= current_group
->head
;
224 buf
= g_malloc (buflen
= 1024);
228 switch (current
->type
) {
232 strcat (strcat (buf
, "->"), current
->label
);
233 if (hotlist_state
.moving
)
234 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
236 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
241 if (hotlist_state
.moving
)
242 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
244 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
249 current
= current
->next
;
256 unlink_entry (struct hotlist
*entry
)
258 struct hotlist
*current
= current_group
->head
;
260 if (current
== entry
)
261 current_group
->head
= entry
->next
;
263 while (current
&& current
->next
!= entry
)
264 current
= current
->next
;
266 current
->next
= entry
->next
;
273 static void add_name_to_list (const char *path
)
275 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
277 #endif /* !USE_VFS */
280 hotlist_button_callback (int action
)
285 struct hotlist
*saved
= current_group
;
286 struct hotlist
*item
;
287 struct hotlist
*moveto_item
= 0;
288 struct hotlist
*moveto_group
= 0;
291 if (!l_hotlist
->current
)
292 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
293 item
= l_hotlist
->current
->data
;
294 hotlist_state
.moving
= 1;
295 init_movelist (LIST_MOVELIST
, item
);
296 run_dlg (movelist_dlg
);
297 ret
= movelist_dlg
->ret_value
;
298 hotlist_state
.moving
= 0;
299 if (l_movelist
->current
)
300 moveto_item
= l_movelist
->current
->data
;
301 moveto_group
= current_group
;
302 destroy_dlg (movelist_dlg
);
303 current_group
= saved
;
305 return MSG_NOT_HANDLED
;
306 if (moveto_item
== item
)
307 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
308 it hardly changes anything ;) */
310 listbox_remove_current (l_hotlist
, 1);
311 item
->up
= moveto_group
;
312 if (!moveto_group
->head
)
313 moveto_group
->head
= item
;
314 else if (!moveto_item
) { /* we have group with just comments */
315 struct hotlist
*p
= moveto_group
->head
;
321 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
322 if (!moveto_item
->next
)
323 moveto_item
->next
= item
;
325 item
->next
= moveto_item
->next
;
326 moveto_item
->next
= item
;
327 } else if (moveto_group
->head
== moveto_item
) {
328 moveto_group
->head
= item
;
329 item
->next
= moveto_item
;
331 struct hotlist
*p
= moveto_group
->head
;
333 while (p
->next
!= moveto_item
)
335 item
->next
= p
->next
;
338 listbox_remove_list (l_hotlist
);
341 hotlist_state
.modified
= 1;
342 return MSG_NOT_HANDLED
;
346 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
347 remove_from_hotlist (l_hotlist
->current
->data
);
348 return MSG_NOT_HANDLED
;
352 add_new_group_cmd ();
353 return MSG_NOT_HANDLED
;
358 return MSG_NOT_HANDLED
;
362 add_new_entry_cmd ();
363 return MSG_NOT_HANDLED
;
368 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
370 if (list
->current
->data
) {
371 struct hotlist
*hlp
=
372 (struct hotlist
*) list
->current
->data
;
373 if (hlp
->type
== HL_TYPE_ENTRY
)
375 else if (hlp
->type
== HL_TYPE_DOTDOT
) {
376 /* Fall through - go up */
380 listbox_remove_list (list
);
383 return MSG_NOT_HANDLED
;
389 /* Fall through if list empty - just go up */
393 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
394 listbox_remove_list (list
);
395 current_group
= current_group
->up
;
397 return MSG_NOT_HANDLED
;
407 listbox_remove_list (l_hotlist
);
408 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
409 vfs_fill_names (add_name_to_list
);
410 return MSG_NOT_HANDLED
;
421 hotlist_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
428 case DLG_UNHANDLED_KEY
:
430 case KEY_M_CTRL
| '\n':
435 if (hotlist_button_callback (B_ENTER
)) {
436 h
->ret_value
= B_ENTER
;
442 if (hotlist_state
.type
!= LIST_VFSLIST
)
443 return !hotlist_button_callback (B_UP_GROUP
);
445 return MSG_NOT_HANDLED
;
448 if (!hotlist_state
.moving
) {
449 hotlist_button_callback (B_REMOVE
);
456 if (!hotlist_state
.moving
) {
457 if (l_hotlist
->current
) {
458 if (l_hotlist
->current
->data
) {
459 struct hotlist
*hlp
=
460 (struct hotlist
*) l_hotlist
->current
->data
;
461 if (hlp
->type
== HL_TYPE_ENTRY
) {
463 g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
464 stuff (cmdline
, tmp
, 0);
467 h
->ret_value
= B_CANCEL
;
473 return MSG_HANDLED
; /* ignore key */
475 return MSG_NOT_HANDLED
;
478 if (hotlist_state
.moving
)
479 dlg_select_widget (l_movelist
);
481 dlg_select_widget (l_hotlist
);
482 /* always stay on hotlist */
486 attrset (MENU_ENTRY_COLOR
);
491 return default_dlg_callback (h
, msg
, parm
);
495 static int l_call (WListbox
*list
)
497 Dlg_head
*dlg
= list
->widget
.parent
;
500 if (list
->current
->data
) {
501 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
502 if (hlp
->type
== HL_TYPE_ENTRY
) {
503 dlg
->ret_value
= B_ENTER
;
507 hotlist_button_callback (B_ENTER
);
508 hotlist_callback (dlg
, DLG_POST_KEY
, '\n');
512 dlg
->ret_value
= B_ENTER
;
518 hotlist_button_callback (B_UP_GROUP
);
519 hotlist_callback (dlg
, DLG_POST_KEY
, 'u');
524 * Expands all button names (once) and recalculates button positions.
525 * returns number of columns in the dialog box, which is 10 chars longer
528 * If common width of the window (i.e. in xterm) is less than returned
529 * width - sorry :) (anyway this did not handled in previous version too)
532 init_i18n_stuff(int list_type
, int cols
)
535 static const char* cancel_but
= N_("&Cancel");
538 static int hotlist_i18n_flag
= 0;
540 if (!hotlist_i18n_flag
)
542 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
544 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
546 cancel_but
= _(cancel_but
);
547 hotlist_i18n_flag
= 1;
549 #endif /* ENABLE_NLS */
551 /* Dynamic resizing of buttonbars */
553 int len
[2], count
[2]; /* at most two lines of buttons */
556 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
557 len
[0] = len
[1] = count
[0] = count
[1] = 0;
559 /* Count len of buttonbars, assuming 2 extra space between buttons */
562 if (! (hotlist_but
[i
].type
& list_type
))
565 row
= hotlist_but
[i
].y
;
567 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
568 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
574 cols
= max(cols
, max(len
[0], len
[1]));
576 /* arrange buttons */
578 cur_x
[0] = cur_x
[1] = 0;
579 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
582 if (! (hotlist_but
[i
].type
& list_type
))
585 row
= hotlist_but
[i
].y
;
587 if (hotlist_but
[i
].x
!= 0)
589 /* not first int the row */
590 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
592 cols
- strlen (hotlist_but
[i
].text
) - 13;
594 hotlist_but
[i
].x
= cur_x
[row
];
597 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
598 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
606 init_hotlist (int list_type
)
609 const char *title
, *help_node
;
612 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
616 hotlist_state
.expanded
=
617 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
620 if (list_type
== LIST_VFSLIST
) {
621 title
= _("Active VFS directories");
622 help_node
= "[vfshot]"; /* FIXME - no such node */
624 title
= _("Directory hotlist");
625 help_node
= "[Hotlist]";
629 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
630 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
632 for (i
= 0; i
< BUTTONS
; i
++) {
633 if (hotlist_but
[i
].type
& list_type
)
634 add_widget (hotlist_dlg
,
635 button_new (BY
+ hotlist_but
[i
].y
,
636 BX
+ hotlist_but
[i
].x
,
637 hotlist_but
[i
].ret_cmd
,
638 hotlist_but
[i
].flags
,
640 hotlist_button_callback
));
643 /* We add the labels.
644 * pname will hold entry's pathname;
645 * pname_group will hold name of current group
647 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
648 add_widget (hotlist_dlg
, pname
);
649 if (!hotlist_state
.moving
) {
650 add_widget (hotlist_dlg
,
651 label_new (UY
- 12 + LINES
, UX
+ 1,
652 _(" Directory path ")));
654 /* This one holds the displayed pathname */
655 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
656 add_widget (hotlist_dlg
, pname_group
);
658 /* get new listbox */
660 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
663 /* Fill the hotlist with the active VFS or the hotlist */
665 if (list_type
== LIST_VFSLIST
) {
666 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
667 vfs_fill_names (add_name_to_list
);
669 #endif /* !USE_VFS */
672 add_widget (hotlist_dlg
, l_hotlist
);
673 /* add listbox to the dialogs */
677 init_movelist (int list_type
, struct hotlist
*item
)
680 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
681 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
686 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
687 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
690 for (i
= 0; i
< BUTTONS
; i
++) {
691 if (hotlist_but
[i
].type
& list_type
)
692 add_widget (movelist_dlg
,
693 button_new (BY
- 4 + hotlist_but
[i
].y
,
694 BX
+ hotlist_but
[i
].x
,
695 hotlist_but
[i
].ret_cmd
,
696 hotlist_but
[i
].flags
,
698 hotlist_button_callback
));
701 /* We add the labels. We are interested in the last one,
702 * that one will hold the path name label
704 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
705 add_widget (movelist_dlg
, movelist_group
);
706 /* get new listbox */
708 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
709 movelist_dlg
->lines
- 8, l_call
);
713 add_widget (movelist_dlg
, l_movelist
);
714 /* add listbox to the dialogs */
718 * Destroy the list dialog.
719 * Don't confuse with done_hotlist() for the list in memory.
721 static void hotlist_done (void)
723 destroy_dlg (hotlist_dlg
);
726 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
731 find_group_section (struct hotlist
*grp
)
733 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
737 /* 1.11.96 bor: added pos parameter to control placement of new item.
738 see widget.c, listbox_add_item()
739 now hotlist is in unsorted mode
742 HL_BEFORE_CURRENT
= 1
743 ,HL_AFTER_CURRENT
= 2
746 static struct hotlist
*
747 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
750 struct hotlist
*current
= NULL
;
753 * Hotlist is neither loaded nor loading.
754 * Must be called by "Ctrl-x a" before using hotlist.
759 if (l_hotlist
&& l_hotlist
->current
) {
760 current
= l_hotlist
->current
->data
;
762 /* Make sure `..' stays at the top of the list. */
763 if (current
->type
== HL_TYPE_DOTDOT
)
764 pos
= HL_AFTER_CURRENT
;
767 new = new_hotlist ();
771 new->directory
= directory
;
772 new->up
= current_group
;
774 if (type
== HL_TYPE_GROUP
) {
776 add_dotdot_to_list ();
777 current_group
= new->up
;
780 if (!current_group
->head
) { /* first element in group */
781 current_group
->head
= new;
782 } else if (pos
== HL_AFTER_CURRENT
) {
783 new->next
= current
->next
;
785 } else if (pos
== HL_BEFORE_CURRENT
&&
786 current
== current_group
->head
) {
787 /* should be inserted before first item */
789 current_group
->head
= new;
790 } else if (pos
== HL_BEFORE_CURRENT
) {
791 struct hotlist
*p
= current_group
->head
;
793 while (p
->next
!= current
)
798 } else { /* append at the end */
799 struct hotlist
*p
= current_group
->head
;
807 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&&
808 type
!= HL_TYPE_DOTDOT
) {
809 if (type
== HL_TYPE_GROUP
) {
810 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
812 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
815 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
816 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
824 * Support routine for add_new_entry_input()/add_new_group_input()
825 * Change positions of buttons (first three widgets).
827 * This is just a quick hack. Accurate procedure must take care of
828 * internationalized label lengths and total buttonbar length...assume
829 * 64 is longer anyway.
831 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
833 int i
, l
[3], space
, cur_x
;
835 for (i
= 0; i
< 3; i
++)
837 qw
[i
].text
= _(qw
[i
].text
);
838 l
[i
] = strlen (qw
[i
].text
) + 3;
840 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
842 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
844 qw
[i
].relative_x
= cur_x
;
845 qw
[i
].x_divisions
= len
;
848 #endif /* ENABLE_NLS */
851 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
852 const char *help
, char **r1
, char **r2
)
854 #define RELATIVE_Y_BUTTONS 4
855 #define RELATIVE_Y_LABEL_PTH 3
856 #define RELATIVE_Y_INPUT_PTH 4
858 QuickDialog Quick_input
;
859 static QuickWidget quick_widgets
[] = {
860 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
862 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
864 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
866 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
868 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
870 { quick_input
, 4, 80, 3, 0, "", 58, 0,
872 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
882 static int i18n_flag
= 0;
883 #endif /* ENABLE_NLS */
885 msglen(text1
, &lines1
, &cols1
);
886 msglen(text2
, &lines2
, &cols2
);
887 len
= max ((int) strlen (header
), cols1
);
888 len
= max (len
, cols2
) + 4;
894 add_widgets_i18n(quick_widgets
, len
);
897 #endif /* ENABLE_NLS */
899 Quick_input
.xlen
= len
;
900 Quick_input
.xpos
= -1;
901 Quick_input
.title
= header
;
902 Quick_input
.help
= help
;
903 Quick_input
.i18n
= 0;
904 quick_widgets
[6].text
= text1
;
905 quick_widgets
[4].text
= text2
;
906 quick_widgets
[5].text
= *r1
;
907 quick_widgets
[3].text
= *r2
;
909 for (i
= 0; i
< 7; i
++)
910 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
911 Quick_input
.ylen
= lines1
+ lines2
+ 7;
913 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
914 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
915 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
916 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
917 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
919 quick_widgets
[5].str_result
= r1
;
920 quick_widgets
[3].str_result
= r2
;
922 Quick_input
.widgets
= quick_widgets
;
923 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
929 static void add_new_entry_cmd (void)
931 char *title
, *url
, *to_free
;
934 /* Take current directory as default value for input fields */
935 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
937 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"),
938 _("Directory path"), "[Hotlist]", &title
, &url
);
943 if (!title
|| !*title
|| !url
|| !*url
) {
949 if (ret
== B_ENTER
|| ret
== B_APPEND
)
950 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_AFTER_CURRENT
);
952 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_BEFORE_CURRENT
);
954 hotlist_state
.modified
= 1;
958 add_new_group_input (const char *header
, const char *label
, char **result
)
961 QuickDialog Quick_input
;
962 static QuickWidget quick_widgets
[] = {
963 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
965 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
967 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
969 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
970 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, NULL
},
972 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
973 quick_widgets variable above */
979 static int i18n_flag
= 0;
980 #endif /* ENABLE_NLS */
982 msglen (label
, &lines
, &cols
);
983 len
= max ((int) strlen (header
), cols
) + 4;
989 add_widgets_i18n(quick_widgets
, len
);
992 #endif /* ENABLE_NLS */
994 Quick_input
.xlen
= len
;
995 Quick_input
.xpos
= -1;
996 Quick_input
.title
= header
;
997 Quick_input
.help
= "[Hotlist]";
998 Quick_input
.i18n
= 0;
999 quick_widgets
[4].text
= label
;
1001 for (i
= 0; i
< 5; i
++)
1002 quick_widgets
[i
].y_divisions
= lines
+6;
1003 Quick_input
.ylen
= lines
+ 6;
1005 for (i
= 0; i
< 4; i
++)
1006 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
1008 quick_widgets
[3].str_result
= result
;
1009 quick_widgets
[3].text
= "";
1011 Quick_input
.widgets
= quick_widgets
;
1012 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
1018 static void add_new_group_cmd (void)
1023 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
1024 if (!ret
|| !label
|| !*label
)
1027 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1028 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_AFTER_CURRENT
);
1030 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_BEFORE_CURRENT
);
1032 hotlist_state
.modified
= 1;
1035 void add2hotlist_cmd (void)
1037 char *prompt
, *label
;
1038 const char *cp
= _("Label for \"%s\":");
1039 int l
= strlen (cp
);
1040 char *label_string
= g_strdup (current_panel
->cwd
);
1042 strip_password (label_string
, 1);
1044 prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
-2*UX
-(l
+8)));
1045 label
= input_dialog (_(" Add to hotlist "), prompt
, label_string
);
1048 if (!label
|| !*label
) {
1049 g_free (label_string
);
1053 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, 0);
1054 hotlist_state
.modified
= 1;
1057 static void remove_group (struct hotlist
*grp
)
1059 struct hotlist
*current
= grp
->head
;
1062 struct hotlist
*next
= current
->next
;
1064 if (current
->type
== HL_TYPE_GROUP
)
1065 remove_group (current
);
1067 g_free (current
->label
);
1068 g_free (current
->directory
);
1076 static void remove_from_hotlist (struct hotlist
*entry
)
1078 if (entry
->type
== HL_TYPE_DOTDOT
)
1081 if (confirm_directory_hotlist_delete
) {
1085 title
= g_strconcat (_(" Remove: "),
1086 name_trunc (entry
->label
, 30),
1092 result
= query_dialog (title
,
1093 _("\n Are you sure you want to remove this entry?"),
1094 D_ERROR
, 2, _("&Yes"), _("&No"));
1102 if (entry
->type
== HL_TYPE_GROUP
) {
1107 header
= g_strconcat (_(" Remove: "),
1108 name_trunc (entry
->label
, 30),
1111 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1113 _("&Yes"), _("&No"));
1120 remove_group (entry
);
1123 unlink_entry (entry
);
1125 g_free (entry
->label
);
1126 g_free (entry
->directory
);
1128 /* now remove list entry from screen */
1129 listbox_remove_current (l_hotlist
, 1);
1130 hotlist_state
.modified
= 1;
1133 char *hotlist_cmd (int vfs_or_hotlist
)
1135 char *target
= NULL
;
1137 hotlist_state
.type
= vfs_or_hotlist
;
1140 init_hotlist (vfs_or_hotlist
);
1142 /* display file info */
1143 attrset (SELECTED_COLOR
);
1145 hotlist_state
.running
= 1;
1146 run_dlg (hotlist_dlg
);
1147 hotlist_state
.running
= 0;
1150 switch (hotlist_dlg
->ret_value
) {
1155 if (l_hotlist
->current
->data
) {
1156 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1157 target
= g_strdup (hlp
->directory
);
1159 target
= g_strdup (l_hotlist
->current
->text
);
1168 load_group (struct hotlist
*grp
)
1172 char *group_section
;
1173 struct hotlist
*current
= 0;
1175 group_section
= find_group_section (grp
);
1177 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1179 current_group
= grp
;
1181 while (profile_keys
){
1182 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1183 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1185 g_free (group_section
);
1187 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1189 while (profile_keys
){
1190 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1191 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1194 for (current
= grp
->head
; current
; current
= current
->next
)
1195 load_group (current
);
1200 #define TKN_STRING 2
1202 #define TKN_ENDGROUP 4
1203 #define TKN_COMMENT 5
1206 #define TKN_UNKNOWN 127
1208 static char *tkn_buf
;
1209 static int tkn_buf_length
;
1210 static int tkn_length
;
1212 static char *hotlist_file_name
;
1213 static FILE *hotlist_file
;
1214 static time_t hotlist_file_mtime
;
1216 static int hot_skip_blanks (void)
1220 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1226 static int hot_next_token (void)
1230 #define CHECK_BUF() \
1232 if (tkn_length == tkn_buf_length) \
1233 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1234 : ( g_malloc (tkn_buf_length = 1024)); \
1240 c
= hot_skip_blanks ();
1249 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1254 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1258 tkn_buf
[tkn_length
] = '\0';
1262 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1264 if ((c
= getc (hotlist_file
)) == EOF
)
1267 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1272 tkn_buf
[tkn_length
] = '\0';
1276 if ((c
= getc (hotlist_file
)) == EOF
)
1281 /* fall through; it is taken as normal character */
1286 tkn_buf
[tkn_length
++] = toupper(c
);
1287 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1289 ungetc (c
, hotlist_file
);
1291 tkn_buf
[tkn_length
] = '\0';
1292 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1294 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1296 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1297 return TKN_ENDGROUP
;
1298 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1306 #define SKIP_TO_EOL { \
1308 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1311 #define CHECK_TOKEN(_TKN_) \
1312 if ((tkn = hot_next_token ()) != _TKN_) { \
1313 hotlist_state.readonly = 1; \
1314 hotlist_state.file_error = 1; \
1315 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1316 tkn = hot_next_token (); \
1321 hot_load_group (struct hotlist
* grp
)
1324 struct hotlist
*new_grp
;
1327 current_group
= grp
;
1329 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1332 CHECK_TOKEN(TKN_STRING
);
1333 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1335 hot_load_group (new_grp
);
1336 current_group
= grp
;
1339 CHECK_TOKEN(TKN_STRING
);
1340 label
= g_strdup (tkn_buf
);
1341 CHECK_TOKEN(TKN_URL
);
1342 CHECK_TOKEN(TKN_STRING
);
1343 url
= g_strdup (tkn_buf
);
1344 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1348 label
= g_strdup (tkn_buf
);
1349 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1352 hotlist_state
.readonly
= 1;
1353 hotlist_state
.file_error
= 1;
1357 /* skip empty lines */
1360 hotlist_state
.readonly
= 1;
1361 hotlist_state
.file_error
= 1;
1369 hot_load_file (struct hotlist
* grp
)
1372 struct hotlist
*new_grp
;
1375 current_group
= grp
;
1377 while ((tkn
= hot_next_token())!= TKN_EOF
)
1380 CHECK_TOKEN(TKN_STRING
);
1381 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1383 hot_load_group (new_grp
);
1384 current_group
= grp
;
1387 CHECK_TOKEN(TKN_STRING
);
1388 label
= g_strdup (tkn_buf
);
1389 CHECK_TOKEN(TKN_URL
);
1390 CHECK_TOKEN(TKN_STRING
);
1391 url
= g_strdup (tkn_buf
);
1392 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1396 label
= g_strdup (tkn_buf
);
1397 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1400 /* skip empty lines */
1403 hotlist_state
.readonly
= 1;
1404 hotlist_state
.file_error
= 1;
1411 clean_up_hotlist_groups (const char *section
)
1417 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1418 if (profile_has_section (section
, profile_name
))
1419 profile_clean_section (section
, profile_name
);
1420 if (profile_has_section (grp_section
, profile_name
)) {
1421 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1423 while (profile_keys
) {
1424 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1425 clean_up_hotlist_groups (key
);
1427 profile_clean_section (grp_section
, profile_name
);
1429 g_free (grp_section
);
1437 int remove_old_list
= 0;
1438 struct stat stat_buf
;
1440 if (hotlist_state
.loaded
) {
1441 stat (hotlist_file_name
, &stat_buf
);
1442 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1448 if (!hotlist_file_name
)
1449 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1451 hotlist
= new_hotlist ();
1452 hotlist
->type
= HL_TYPE_GROUP
;
1453 hotlist
->label
= g_strdup (_(" Top level group "));
1454 hotlist
->up
= hotlist
;
1458 hotlist
->directory
= g_strdup ("Hotlist");
1460 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1463 load_group (hotlist
);
1464 hotlist_state
.loaded
= 1;
1466 * just to be sure we got copy
1468 hotlist_state
.modified
= 1;
1469 result
= save_hotlist ();
1470 hotlist_state
.modified
= 0;
1472 remove_old_list
= 1;
1474 message (D_ERROR
, _(" Hotlist Load "),
1475 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1479 hot_load_file (hotlist
);
1480 fclose (hotlist_file
);
1481 hotlist_state
.loaded
= 1;
1484 if (remove_old_list
) {
1485 clean_up_hotlist_groups ("Hotlist");
1489 stat (hotlist_file_name
, &stat_buf
);
1490 hotlist_file_mtime
= stat_buf
.st_mtime
;
1491 current_group
= hotlist
;
1495 save_group (struct hotlist
*grp
)
1497 struct hotlist
*current
= grp
->head
;
1498 char *group_section
;
1500 group_section
= find_group_section (grp
);
1502 profile_clean_section (group_section
, profile_name
);
1503 for (;current
&& current
->type
== HL_TYPE_GROUP
; current
= current
->next
){
1504 WritePrivateProfileString (group_section
,
1509 g_free (group_section
);
1511 for (current
= grp
->head
;
1512 current
&& current
->type
== HL_TYPE_GROUP
;
1513 current
= current
->next
)
1514 save_group (current
);
1516 profile_clean_section (grp
->directory
, profile_name
);
1517 for (;current
; current
= current
->next
){
1518 WritePrivateProfileString (grp
->directory
,
1525 static int list_level
= 0;
1528 hot_save_group (struct hotlist
*grp
)
1530 struct hotlist
*current
= grp
->head
;
1536 for (i = 0; i < n; i++) \
1537 putc (' ', hotlist_file); \
1540 for (;current
; current
= current
->next
)
1541 switch (current
->type
) {
1543 INDENT (list_level
);
1544 fputs ("GROUP \"", hotlist_file
);
1545 for (s
= current
->label
; *s
; s
++) {
1546 if (*s
== '"' || *s
== '\\')
1547 putc ('\\', hotlist_file
);
1548 putc (*s
, hotlist_file
);
1550 fputs ("\"\n", hotlist_file
);
1552 hot_save_group (current
);
1554 INDENT (list_level
);
1555 fputs ("ENDGROUP\n", hotlist_file
);
1559 fputs ("ENTRY \"", hotlist_file
);
1560 for (s
= current
->label
; *s
; s
++) {
1561 if (*s
== '"' || *s
== '\\')
1562 putc ('\\', hotlist_file
);
1563 putc (*s
, hotlist_file
);
1565 fputs ("\" URL \"", hotlist_file
);
1566 for (s
= current
->directory
; *s
; s
++) {
1567 if (*s
== '"' || *s
== '\\')
1568 putc ('\\', hotlist_file
);
1569 putc (*s
, hotlist_file
);
1571 fputs ("\"\n", hotlist_file
);
1573 case HL_TYPE_COMMENT
:
1574 fprintf (hotlist_file
, "#%s\n", current
->label
);
1576 case HL_TYPE_DOTDOT
:
1582 int save_hotlist (void)
1585 struct stat stat_buf
;
1587 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1588 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", (char *) NULL
);
1590 rename (hotlist_file_name
, fbak
);
1591 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1592 if (stat (fbak
, &stat_buf
) == 0)
1593 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1595 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1596 hot_save_group (hotlist
);
1597 fclose (hotlist_file
);
1598 stat (hotlist_file_name
, &stat_buf
);
1599 hotlist_file_mtime
= stat_buf
.st_mtime
;
1601 hotlist_state
.modified
= 0;
1603 rename (fbak
, hotlist_file_name
);
1611 * Unload list from memory.
1612 * Don't confuse with hotlist_done() for GUI.
1614 void done_hotlist (void)
1617 remove_group (hotlist
);
1618 g_free (hotlist
->label
);
1619 g_free (hotlist
->directory
);
1624 hotlist_state
.loaded
= 0;
1626 g_free (hotlist_file_name
);
1627 hotlist_file_name
= 0;
1639 add_dotdot_to_list (void)
1641 if (current_group
!= hotlist
) {
1642 if (hotlist_has_dot_dot
!= 0)
1643 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, 0);