1 /* Directory hotlist -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997 the Free Software Foundation.
10 Janne did the original Hotlist code, Andrej made the groupable
11 hotlist; the move hotlist and revamped the file format and made
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include <sys/types.h>
40 #include "tty.h" /* COLS */
41 #include "color.h" /* dialog_colors */
44 #include "dialog.h" /* do_refresh() */
45 #include "setup.h" /* For profile_bname */
46 #include "profile.h" /* Load/save directories hotlist */
47 #include "wtools.h" /* QuickDialog */
48 #include "panel.h" /* cpanel */
49 #include "main.h" /* repaint_screen */
51 #include "key.h" /* KEY_M_CTRL */
52 #include "command.h" /* cmdline */
60 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
62 #define B_ADD_CURRENT B_USER
63 #define B_REMOVE (B_USER + 1)
64 #define B_NEW_GROUP (B_USER + 2)
65 #define B_NEW_ENTRY (B_USER + 3)
66 #define B_UP_GROUP (B_USER + 4)
67 #define B_INSERT (B_USER + 5)
68 #define B_APPEND (B_USER + 6)
69 #define B_MOVE (B_USER + 7)
71 static WListbox
*l_hotlist
;
72 static WListbox
*l_movelist
;
74 static Dlg_head
*hotlist_dlg
;
75 static Dlg_head
*movelist_dlg
;
77 static WLabel
*pname
, *pname_group
, *movelist_group
;
87 * these parameters are intended to be user configurable
89 int expanded
; /* expanded view of all groups at startup */
92 * these reflect run time state
95 int loaded
; /* hotlist is loaded */
96 int readonly
; /* hotlist readonly */
97 int file_error
; /* parse error while reading file */
98 int running
; /* we are running dlg (and have to
100 int moving
; /* we are in moving hotlist currently */
101 int modified
; /* hotlist was modified */
102 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
105 static struct _hotlist_but
{
106 int ret_cmd
, flags
, y
, x
;
111 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), "move", LIST_HOTLIST
},
112 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), "r", LIST_HOTLIST
},
113 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), "e", LIST_MOVELIST
},
114 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), "g", LIST_MOVELIST
},
115 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), "e", LIST_HOTLIST
},
116 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), "g", LIST_HOTLIST
},
117 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), "cc", LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
118 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), "up", LIST_HOTLIST
|LIST_MOVELIST
},
119 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"),"ad", LIST_HOTLIST
},
120 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), "ct", LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
123 /* Directory hotlist */
124 static struct hotlist
{
125 enum HotListType type
;
128 struct hotlist
*head
;
130 struct hotlist
*next
;
133 static struct hotlist
*current_group
;
135 static void init_movelist (int, struct hotlist
*);
136 static void add_new_group_cmd (void);
137 static void add_new_entry_cmd (void);
138 static void remove_from_hotlist (struct hotlist
*entry
);
140 #define new_hotlist() g_new0(struct hotlist, 1)
143 hotlist_refresh (Dlg_head
* dlg
)
145 common_dialog_repaint (dlg
);
146 attrset (COLOR_NORMAL
);
147 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
148 dlg
->cols
- (UX
* 2));
149 if (!hotlist_state
.moving
)
150 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
153 /* If current->data is 0, then we are dealing with a VFS pathname */
155 update_path_name (void)
158 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
159 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
162 if (list
->current
->data
!= 0) {
163 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
165 if (hlp
->type
== HL_TYPE_ENTRY
)
166 text
= hlp
->directory
;
168 text
= _("Subgroup - press ENTER to see list");
170 text
= list
->current
->text
;
175 if (!hotlist_state
.moving
)
176 label_set_text (pname
,
177 name_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
179 p
= g_strconcat (" ", current_group
->label
, " ", NULL
);
180 if (!hotlist_state
.moving
)
181 label_set_text (pname_group
,
182 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
184 label_set_text (movelist_group
,
185 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
191 #define CHECK_BUFFER \
195 if ((i = strlen (current->label) + 3) > buflen) { \
197 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
202 static void fill_listbox (void)
204 struct hotlist
*current
= current_group
->head
;
209 buf
= g_malloc (buflen
= 1024);
213 switch (current
->type
) {
217 strcat (strcat (buf
, "->"), current
->label
);
218 if (hotlist_state
.moving
)
219 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
221 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
225 if (hotlist_state
.moving
)
226 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
228 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
233 current
= current
->next
;
240 unlink_entry (struct hotlist
*entry
)
242 struct hotlist
*current
= current_group
->head
;
244 if (current
== entry
)
245 current_group
->head
= entry
->next
;
247 while (current
&& current
->next
!= entry
)
248 current
= current
->next
;
250 current
->next
= entry
->next
;
255 static int hotlist_button_callback (int action
, void *data
)
260 struct hotlist
*saved
= current_group
;
261 struct hotlist
*item
;
262 struct hotlist
*moveto_item
= 0;
263 struct hotlist
*moveto_group
= 0;
266 if (!l_hotlist
->current
)
267 return 0; /* empty group - nothing to do */
268 item
= l_hotlist
->current
->data
;
269 hotlist_state
.moving
= 1;
270 init_movelist (LIST_MOVELIST
, item
);
271 run_dlg (movelist_dlg
);
272 ret
= movelist_dlg
->ret_value
;
273 hotlist_state
.moving
= 0;
274 if (l_movelist
->current
)
275 moveto_item
= l_movelist
->current
->data
;
276 moveto_group
= current_group
;
277 destroy_dlg (movelist_dlg
);
278 current_group
= saved
;
281 if (moveto_item
== item
)
282 return 0; /* If we insert/append a before/after a
283 it hardly changes anything ;) */
285 listbox_remove_current (l_hotlist
, 1);
286 item
->up
= moveto_group
;
287 if (!moveto_group
->head
)
288 moveto_group
->head
= item
;
289 else if (!moveto_item
) { /* we have group with just comments */
290 struct hotlist
*p
= moveto_group
->head
;
296 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
297 if (!moveto_item
->next
)
298 moveto_item
->next
= item
;
300 item
->next
= moveto_item
->next
;
301 moveto_item
->next
= item
;
303 else if (moveto_group
->head
== moveto_item
) {
304 moveto_group
->head
= item
;
305 item
->next
= moveto_item
;
307 struct hotlist
*p
= moveto_group
->head
;
309 while (p
->next
!= moveto_item
)
311 item
->next
= p
->next
;
314 listbox_remove_list (l_hotlist
);
317 hotlist_state
.modified
= 1;
322 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
323 remove_from_hotlist (l_hotlist
->current
->data
);
328 add_new_group_cmd ();
338 add_new_entry_cmd ();
344 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
346 if (list
->current
->data
) {
347 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
348 if (hlp
->type
== HL_TYPE_ENTRY
)
351 listbox_remove_list (list
);
360 /* Fall through if list empty - just go up */
364 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
365 listbox_remove_list (list
);
366 current_group
= current_group
->up
;
379 static int hotlist_callback (Dlg_head
* h
, int Par
, int Msg
)
386 case DLG_UNHANDLED_KEY
:
388 case KEY_M_CTRL
| '\n':
393 if (hotlist_button_callback (B_ENTER
, 0)) {
394 h
->ret_value
= B_ENTER
;
400 if (hotlist_state
.type
!= LIST_VFSLIST
)
401 return !hotlist_button_callback (B_UP_GROUP
, 0);
406 if (!hotlist_state
.moving
) {
407 hotlist_button_callback (B_REMOVE
, 0);
414 if (!hotlist_state
.moving
)
416 if (l_hotlist
->current
){
417 if (l_hotlist
->current
->data
) {
418 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
419 if (hlp
->type
== HL_TYPE_ENTRY
) {
420 char *tmp
= g_strconcat ( "cd ", hlp
->directory
, NULL
);
421 stuff (cmdline
, tmp
, 0);
424 h
->ret_value
= B_CANCEL
;
430 return 1; /* ignore key */
437 if (hotlist_state
.moving
)
438 dlg_select_widget (movelist_dlg
, l_movelist
);
440 dlg_select_widget (hotlist_dlg
, l_hotlist
);
441 /* always stay on hotlist */
445 attrset (MENU_ENTRY_COLOR
);
452 static int l_call (void *l
)
454 WListbox
*list
= (WListbox
*) l
;
455 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
458 if (list
->current
->data
) {
459 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
460 if (hlp
->type
== HL_TYPE_ENTRY
) {
461 dlg
->ret_value
= B_ENTER
;
463 return listbox_finish
;
465 hotlist_button_callback (B_ENTER
, (void *)0);
466 hotlist_callback (dlg
, '\n', DLG_POST_KEY
);
467 return listbox_nothing
;
470 dlg
->ret_value
= B_ENTER
;
472 return listbox_finish
;
476 hotlist_button_callback (B_UP_GROUP
, (void *)0);
477 hotlist_callback (dlg
, 'u', DLG_POST_KEY
);
478 return listbox_nothing
;
482 static void add_name_to_list (char *path
)
484 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
486 #endif /* !USE_VFS */
489 * Expands all button names (once) and recalculates button positions.
490 * returns number of columns in the dialog box, which is 10 chars longer
493 * If common width of the window (i.e. in xterm) is less than returned
494 * width - sorry :) (anyway this did not handled in previous version too)
497 init_i18n_stuff(int list_type
, int cols
)
500 static char* cancel_but
= N_("&Cancel");
503 static int hotlist_i18n_flag
= 0;
505 if (!hotlist_i18n_flag
)
507 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
509 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
511 cancel_but
= _(cancel_but
);
512 hotlist_i18n_flag
= 1;
514 #endif /* ENABLE_NLS */
516 /* Dynamic resizing of buttonbars */
518 int len
[2], count
[2]; /* at most two lines of buttons */
521 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
522 len
[0] = len
[1] = count
[0] = count
[1] = 0;
524 /* Count len of buttonbars, assuming 2 extra space between buttons */
527 if (! (hotlist_but
[i
].type
& list_type
))
530 row
= hotlist_but
[i
].y
;
532 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
533 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
539 cols
= max(cols
, max(len
[0], len
[1]));
541 /* arrange buttons */
543 cur_x
[0] = cur_x
[1] = 0;
544 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
547 if (! (hotlist_but
[i
].type
& list_type
))
550 row
= hotlist_but
[i
].y
;
552 if (hotlist_but
[i
].x
!= 0)
554 /* not first int the row */
555 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
557 cols
- strlen (hotlist_but
[i
].text
) - 13;
559 hotlist_but
[i
].x
= cur_x
[row
];
562 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
563 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
571 init_hotlist (int list_type
)
574 char *title
, *help_node
;
577 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
581 hotlist_state
.expanded
=
582 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
585 if (list_type
== LIST_VFSLIST
) {
586 title
= _("Active VFS directories");
587 help_node
= "[vfshot]"; /* FIXME - no such node */
589 title
= _("Directory hotlist");
590 help_node
= "[Hotlist]";
594 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
595 hotlist_callback
, help_node
, title
, DLG_CENTER
);
597 for (i
= 0; i
< BUTTONS
; i
++) {
598 if (hotlist_but
[i
].type
& list_type
)
599 add_widget (hotlist_dlg
,
600 button_new (BY
+ hotlist_but
[i
].y
,
601 BX
+ hotlist_but
[i
].x
,
602 hotlist_but
[i
].ret_cmd
,
603 hotlist_but
[i
].flags
,
605 hotlist_button_callback
, 0,
606 hotlist_but
[i
].tkname
));
609 /* We add the labels.
610 * pname will hold entry's pathname;
611 * pname_group will hold name of current group
613 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "", "the-lab");
614 add_widget (hotlist_dlg
, pname
);
615 if (!hotlist_state
.moving
) {
616 add_widget (hotlist_dlg
,
617 label_new (UY
- 12 + LINES
, UX
+ 1,
618 _(" Directory path "), NULL
));
620 /* This one holds the displayed pathname */
621 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "), NULL
);
622 add_widget (hotlist_dlg
, pname_group
);
624 /* get new listbox */
626 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
627 listbox_cback
, l_call
, "listbox");
629 /* Fill the hotlist with the active VFS or the hotlist */
631 if (list_type
== LIST_VFSLIST
) {
632 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
633 vfs_fill_names (add_name_to_list
);
635 #endif /* !USE_VFS */
638 add_widget (hotlist_dlg
, l_hotlist
);
639 /* add listbox to the dialogs */
643 init_movelist (int list_type
, struct hotlist
*item
)
646 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
647 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
652 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
653 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
);
656 for (i
= 0; i
< BUTTONS
; i
++) {
657 if (hotlist_but
[i
].type
& list_type
)
658 add_widget (movelist_dlg
,
659 button_new (BY
- 4 + hotlist_but
[i
].y
,
660 BX
+ hotlist_but
[i
].x
,
661 hotlist_but
[i
].ret_cmd
,
662 hotlist_but
[i
].flags
,
664 hotlist_button_callback
, 0,
665 hotlist_but
[i
].tkname
));
668 /* We add the labels. We are interested in the last one,
669 * that one will hold the path name label
671 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "), NULL
);
672 add_widget (movelist_dlg
, movelist_group
);
673 /* get new listbox */
675 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
676 movelist_dlg
->lines
- 8, listbox_cback
, l_call
,
681 add_widget (movelist_dlg
, l_movelist
);
682 /* add listbox to the dialogs */
686 * Destroy the list dialog.
687 * Don't confuse with done_hotlist() for the list in memory.
689 static void hotlist_done (void)
691 destroy_dlg (hotlist_dlg
);
694 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
699 find_group_section (struct hotlist
*grp
)
701 return g_strconcat (grp
->directory
, ".Group", NULL
);
706 /* 1.11.96 bor: added pos parameter to control placement of new item.
707 see widget.c, listbox_add_item()
708 now hotlist is in unsorted mode
710 static struct hotlist
*
711 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
714 struct hotlist
*current
= NULL
;
717 * Hotlist is neither loaded nor loading.
718 * Must be called by "Ctrl-x a" before using hotlist.
723 if (l_hotlist
&& l_hotlist
->current
)
724 current
= l_hotlist
->current
->data
;
726 new = new_hotlist ();
730 new->directory
= directory
;
731 new->up
= current_group
;
733 if (!current_group
->head
) { /* first element in group */
734 current_group
->head
= new;
735 } else if (pos
== 2) { /* should be appended after current*/
736 new->next
= current
->next
;
738 } else if (pos
== 1 &&
739 current
== current_group
->head
) {
740 /* should be inserted before first item */
742 current_group
->head
= new;
743 } else if (pos
== 1) { /* befor current */
744 struct hotlist
*p
= current_group
->head
;
746 while (p
->next
!= current
)
751 } else { /* append at the end */
752 struct hotlist
*p
= current_group
->head
;
760 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
) {
761 if (type
== HL_TYPE_GROUP
) {
762 char *lbl
= g_strconcat ("->", new->label
, NULL
);
764 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
767 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
768 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
776 * Support routine for add_new_entry_input()/add_new_group_input()
777 * Change positions of buttons (first three widgets).
779 * This is just a quick hack. Accurate procedure must take care of
780 * internationalized label lengths and total buttonbar length...assume
781 * 64 is longer anyway.
783 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
785 int i
, l
[3], space
, cur_x
;
787 for (i
= 0; i
< 3; i
++)
789 qw
[i
].text
= _(qw
[i
].text
);
790 l
[i
] = strlen (qw
[i
].text
) + 3;
792 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
794 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
796 qw
[i
].relative_x
= cur_x
;
797 qw
[i
].x_divisions
= len
;
800 #endif /* ENABLE_NLS */
802 static int add_new_entry_input (char *header
, char *text1
, char *text2
, char *help
, char **r1
, char **r2
)
804 #define RELATIVE_Y_BUTTONS 4
805 #define RELATIVE_Y_LABEL_PTH 3
806 #define RELATIVE_Y_INPUT_PTH 4
808 QuickDialog Quick_input
;
809 static QuickWidget quick_widgets
[] = {
810 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
811 0, 0, "button-cancel" },
812 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
813 0, 0, "button-insert" },
814 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
815 0, 0, "button-append" },
816 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
818 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
820 { quick_input
, 4, 80, 3, 0, "", 58, 0,
822 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
829 char *my_str1
, *my_str2
;
832 static int i18n_flag
= 0;
833 #endif /* ENABLE_NLS */
835 len
= max (strlen (header
), msglen (text1
, &lines1
));
836 len
= max (len
, msglen (text2
, &lines2
)) + 4;
842 add_widgets_i18n(quick_widgets
, len
);
845 #endif /* ENABLE_NLS */
847 Quick_input
.xlen
= len
;
848 Quick_input
.xpos
= -1;
849 Quick_input
.title
= header
;
850 Quick_input
.help
= help
;
851 Quick_input
.i18n
= 0;
852 quick_widgets
[6].text
= text1
;
853 quick_widgets
[4].text
= text2
;
854 quick_widgets
[5].text
= *r1
;
855 quick_widgets
[3].text
= *r2
;
857 for (i
= 0; i
< 7; i
++)
858 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
859 Quick_input
.ylen
= lines1
+ lines2
+ 7;
861 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
862 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
863 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
864 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
865 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
867 quick_widgets
[5].str_result
= &my_str1
;
868 quick_widgets
[3].str_result
= &my_str2
;
870 Quick_input
.widgets
= quick_widgets
;
871 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
872 *r1
= *(quick_widgets
[5].str_result
);
873 *r2
= *(quick_widgets
[3].str_result
);
879 static void add_new_entry_cmd (void)
881 char *title
= 0, *url
= 0;
884 /* Take current directory as default value for input fields */
885 title
= url
= cpanel
->cwd
;
887 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
888 "[Hotlist]", &title
, &url
);
890 if (!ret
|| !title
|| !*title
|| !url
|| !*url
)
893 if (ret
== B_ENTER
|| ret
== B_APPEND
)
894 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 2);
896 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 1);
898 hotlist_state
.modified
= 1;
901 static int add_new_group_input (char *header
, char *label
, char **result
)
904 QuickDialog Quick_input
;
905 static QuickWidget quick_widgets
[] = {
906 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
908 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
910 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
912 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
913 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
915 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
916 quick_widgets variable above */
923 static int i18n_flag
= 0;
924 #endif /* ENABLE_NLS */
926 len
= max (strlen (header
), msglen (label
, &lines
)) + 4;
932 add_widgets_i18n(quick_widgets
, len
);
935 #endif /* ENABLE_NLS */
937 Quick_input
.xlen
= len
;
938 Quick_input
.xpos
= -1;
939 Quick_input
.title
= header
;
940 Quick_input
.help
= "[Hotlist]";
941 Quick_input
.i18n
= 0;
942 quick_widgets
[4].text
= label
;
944 for (i
= 0; i
< 5; i
++)
945 quick_widgets
[i
].y_divisions
= lines
+6;
946 Quick_input
.ylen
= lines
+ 6;
948 for (i
= 0; i
< 4; i
++)
949 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
951 quick_widgets
[3].str_result
= &my_str
;
952 quick_widgets
[3].text
= "";
954 Quick_input
.widgets
= quick_widgets
;
955 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
956 *result
= *(quick_widgets
[3].str_result
);
962 static void add_new_group_cmd (void)
967 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
968 if (!ret
|| !label
|| !*label
)
971 if (ret
== B_ENTER
|| ret
== B_APPEND
)
972 add2hotlist (label
, 0, HL_TYPE_GROUP
, 2);
974 add2hotlist (label
, 0, HL_TYPE_GROUP
, 1);
976 hotlist_state
.modified
= 1;
979 void add2hotlist_cmd (void)
981 char *prompt
, *label
;
982 char *cp
= _("Label for \"%s\":");
985 prompt
= g_strdup_printf (cp
, name_trunc (cpanel
->cwd
, COLS
-2*UX
-(l
+8)));
986 label
= input_dialog (_(" Add to hotlist "), prompt
, cpanel
->cwd
);
988 if (!label
|| !*label
)
991 add2hotlist (label
,g_strdup (cpanel
->cwd
), HL_TYPE_ENTRY
, 0);
992 hotlist_state
.modified
= 1;
995 static void remove_group (struct hotlist
*grp
)
997 struct hotlist
*current
= grp
->head
;
1000 struct hotlist
*next
= current
->next
;
1002 if (current
->type
== HL_TYPE_GROUP
)
1003 remove_group (current
);
1006 g_free (current
->label
);
1007 if (current
->directory
)
1008 g_free (current
->directory
);
1016 static void remove_from_hotlist (struct hotlist
*entry
)
1018 if (entry
->type
== HL_TYPE_GROUP
) {
1023 header
= g_strconcat (_(" Remove: "),
1024 name_trunc (entry
->label
, 30),
1027 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1029 _("&No"), _("&Yes"));
1036 remove_group (entry
);
1039 unlink_entry (entry
);
1042 g_free (entry
->label
);
1043 if (entry
->directory
)
1044 g_free (entry
->directory
);
1046 /* now remove list entry from screen */
1047 listbox_remove_current (l_hotlist
, 1);
1048 hotlist_state
.modified
= 1;
1051 char *hotlist_cmd (int vfs_or_hotlist
)
1053 char *target
= NULL
;
1055 hotlist_state
.type
= vfs_or_hotlist
;
1058 init_hotlist (vfs_or_hotlist
);
1060 /* display file info */
1061 attrset (SELECTED_COLOR
);
1063 hotlist_state
.running
= 1;
1064 run_dlg (hotlist_dlg
);
1065 hotlist_state
.running
= 0;
1068 switch (hotlist_dlg
->ret_value
) {
1073 if (l_hotlist
->current
->data
) {
1074 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1075 target
= g_strdup (hlp
->directory
);
1077 target
= g_strdup (l_hotlist
->current
->text
);
1086 load_group (struct hotlist
*grp
)
1090 char *group_section
;
1091 struct hotlist
*current
= 0;
1093 group_section
= find_group_section (grp
);
1095 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1097 current_group
= grp
;
1099 while (profile_keys
){
1100 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1101 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1103 g_free (group_section
);
1105 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1107 while (profile_keys
){
1108 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1109 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1112 for (current
= grp
->head
; current
; current
= current
->next
)
1113 load_group (current
);
1118 #define TKN_STRING 2
1120 #define TKN_ENDGROUP 4
1121 #define TKN_COMMENT 5
1124 #define TKN_UNKNOWN 127
1126 static char *tkn_buf
;
1127 static int tkn_buf_length
;
1128 static int tkn_length
;
1130 static char *hotlist_file_name
;
1131 static FILE *hotlist_file
;
1132 static time_t hotlist_file_mtime
;
1134 static int hot_skip_blanks (void)
1138 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1144 static int hot_next_token (void)
1148 #define CHECK_BUF() \
1150 if (tkn_length == tkn_buf_length) \
1151 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1152 : ( g_malloc (tkn_buf_length = 1024)); \
1158 c
= hot_skip_blanks ();
1167 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1172 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1176 tkn_buf
[tkn_length
] = '\0';
1180 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1182 if ((c
= getc (hotlist_file
)) == EOF
)
1185 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1190 tkn_buf
[tkn_length
] = '\0';
1194 if ((c
= getc (hotlist_file
)) == EOF
)
1199 /* fall through; it is taken as normal character */
1204 tkn_buf
[tkn_length
++] = toupper(c
);
1205 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1207 ungetc (c
, hotlist_file
);
1209 tkn_buf
[tkn_length
] = '\0';
1210 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1212 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1214 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1215 return TKN_ENDGROUP
;
1216 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1224 #define SKIP_TO_EOL { \
1226 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1229 #define CHECK_TOKEN(_TKN_) \
1230 if ((tkn = hot_next_token ()) != _TKN_) { \
1231 hotlist_state.readonly = 1; \
1232 hotlist_state.file_error = 1; \
1233 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1234 tkn = hot_next_token (); \
1239 hot_load_group (struct hotlist
* grp
)
1242 struct hotlist
*new_grp
;
1245 current_group
= grp
;
1247 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1250 CHECK_TOKEN(TKN_STRING
);
1251 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1253 hot_load_group (new_grp
);
1254 current_group
= grp
;
1257 CHECK_TOKEN(TKN_STRING
);
1258 label
= g_strdup (tkn_buf
);
1259 CHECK_TOKEN(TKN_URL
);
1260 CHECK_TOKEN(TKN_STRING
);
1261 url
= g_strdup (tkn_buf
);
1262 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1266 label
= g_strdup (tkn_buf
);
1267 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1270 hotlist_state
.readonly
= 1;
1271 hotlist_state
.file_error
= 1;
1275 /* skip empty lines */
1278 hotlist_state
.readonly
= 1;
1279 hotlist_state
.file_error
= 1;
1287 hot_load_file (struct hotlist
* grp
)
1290 struct hotlist
*new_grp
;
1293 current_group
= grp
;
1295 while ((tkn
= hot_next_token())!= TKN_EOF
)
1298 CHECK_TOKEN(TKN_STRING
);
1299 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1301 hot_load_group (new_grp
);
1302 current_group
= grp
;
1305 CHECK_TOKEN(TKN_STRING
);
1306 label
= g_strdup (tkn_buf
);
1307 CHECK_TOKEN(TKN_URL
);
1308 CHECK_TOKEN(TKN_STRING
);
1309 url
= g_strdup (tkn_buf
);
1310 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1314 label
= g_strdup (tkn_buf
);
1315 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1318 /* skip empty lines */
1321 hotlist_state
.readonly
= 1;
1322 hotlist_state
.file_error
= 1;
1329 clean_up_hotlist_groups (char *section
)
1335 grp_section
= g_strconcat (section
, ".Group", NULL
);
1336 if (profile_has_section (section
, profile_name
))
1337 profile_clean_section (section
, profile_name
);
1338 if (profile_has_section (grp_section
, profile_name
)) {
1339 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1341 while (profile_keys
) {
1342 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1343 clean_up_hotlist_groups (key
);
1345 profile_clean_section (grp_section
, profile_name
);
1347 g_free (grp_section
);
1352 void load_hotlist (void)
1354 int remove_old_list
= 0;
1355 struct stat stat_buf
;
1357 if (hotlist_state
.loaded
) {
1358 stat (hotlist_file_name
, &stat_buf
);
1359 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1365 if (!hotlist_file_name
)
1366 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1368 hotlist
= new_hotlist ();
1369 hotlist
->type
= HL_TYPE_GROUP
;
1370 hotlist
->label
= g_strdup (_(" Top level group "));
1371 hotlist
->up
= hotlist
;
1375 hotlist
->directory
= g_strdup ("Hotlist");
1377 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1380 load_group (hotlist
);
1381 hotlist_state
.loaded
= 1;
1383 * just to be sure we got copy
1385 hotlist_state
.modified
= 1;
1386 result
= save_hotlist ();
1387 hotlist_state
.modified
= 0;
1389 remove_old_list
= 1;
1393 msg
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1394 _(" file, your old hotlist entries were not deleted"), NULL
);
1396 message (D_ERROR
, _(" Hotlist Load "), msg
);
1400 hot_load_file (hotlist
);
1401 fclose (hotlist_file
);
1402 hotlist_state
.loaded
= 1;
1405 if (remove_old_list
) {
1406 clean_up_hotlist_groups ("Hotlist");
1410 stat (hotlist_file_name
, &stat_buf
);
1411 hotlist_file_mtime
= stat_buf
.st_mtime
;
1412 current_group
= hotlist
;
1416 save_group (struct hotlist
*grp
)
1418 struct hotlist
*current
= grp
->head
;
1419 char *group_section
;
1421 group_section
= find_group_section (grp
);
1423 profile_clean_section (group_section
, profile_name
);
1424 for (;current
&& current
->type
== HL_TYPE_GROUP
; current
= current
->next
){
1425 WritePrivateProfileString (group_section
,
1430 g_free (group_section
);
1432 for (current
= grp
->head
;
1433 current
&& current
->type
== HL_TYPE_GROUP
;
1434 current
= current
->next
)
1435 save_group (current
);
1437 profile_clean_section (grp
->directory
, profile_name
);
1438 for (;current
; current
= current
->next
){
1439 WritePrivateProfileString (grp
->directory
,
1446 static int list_level
= 0;
1449 hot_save_group (struct hotlist
*grp
)
1451 struct hotlist
*current
= grp
->head
;
1457 for (i = 0; i < n; i++) \
1458 putc (' ', hotlist_file); \
1461 for (;current
; current
= current
->next
)
1462 switch (current
->type
) {
1464 INDENT (list_level
);
1465 fputs ("GROUP \"", hotlist_file
);
1466 for (s
= current
->label
; *s
; s
++) {
1468 putc ('\\', hotlist_file
);
1469 else if (*s
== '\\')
1470 putc ('\\', hotlist_file
);
1471 putc (*s
, hotlist_file
);
1473 fputs ("\"\n", hotlist_file
);
1475 hot_save_group (current
);
1477 INDENT (list_level
);
1478 fputs ("ENDGROUP\n", hotlist_file
);
1482 fputs ("ENTRY \"", hotlist_file
);
1483 for (s
= current
->label
; *s
; s
++) {
1485 putc ('\\', hotlist_file
);
1486 else if (*s
== '\\')
1487 putc ('\\', hotlist_file
);
1488 putc (*s
, hotlist_file
);
1490 fputs ("\" URL \"", hotlist_file
);
1491 for (s
= current
->directory
; *s
; s
++) {
1493 putc ('\\', hotlist_file
);
1494 else if (*s
== '\\')
1495 putc ('\\', hotlist_file
);
1496 putc (*s
, hotlist_file
);
1498 fputs ("\"\n", hotlist_file
);
1500 case HL_TYPE_COMMENT
:
1501 fprintf (hotlist_file
, "#%s\n", current
->label
);
1507 int save_hotlist (void)
1510 struct stat stat_buf
;
1512 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1513 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", NULL
);
1515 rename (hotlist_file_name
, fbak
);
1516 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1517 if (stat (fbak
, &stat_buf
) == 0)
1518 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1520 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1521 hot_save_group (hotlist
);
1522 fclose (hotlist_file
);
1523 stat (hotlist_file_name
, &stat_buf
);
1524 hotlist_file_mtime
= stat_buf
.st_mtime
;
1526 hotlist_state
.modified
= 0;
1528 rename (fbak
, hotlist_file_name
);
1536 * Unload list from memory.
1537 * Don't confuse with hotlist_done() for GUI.
1539 void done_hotlist (void)
1542 remove_group (hotlist
);
1544 g_free (hotlist
->label
);
1545 if (hotlist
->directory
)
1546 g_free (hotlist
->directory
);
1551 hotlist_state
.loaded
= 0;
1553 if (hotlist_file_name
){
1554 g_free (hotlist_file_name
);
1555 hotlist_file_name
= 0;