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>
45 #include "dialog.h" /* For do_refresh() */
46 #include "setup.h" /* For profile_bname */
47 #include "profile.h" /* Load/save directories hotlist */
49 #include "../vfs/vfs.h"
50 /* Needed for the extern declarations of integer parameters */
53 #include "panel.h" /* Needed for the externs */
66 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
68 #define B_ADD_CURRENT B_USER
69 #define B_REMOVE (B_USER + 1)
70 #define B_NEW_GROUP (B_USER + 2)
71 #define B_NEW_ENTRY (B_USER + 3)
72 #define B_UP_GROUP (B_USER + 4)
73 #define B_INSERT (B_USER + 5)
74 #define B_APPEND (B_USER + 6)
75 #define B_MOVE (B_USER + 7)
77 static WListbox
*l_hotlist
;
78 static WListbox
*l_movelist
;
80 static Dlg_head
*hotlist_dlg
;
81 static Dlg_head
*movelist_dlg
;
83 static WLabel
*pname
, *pname_group
, *movelist_group
;
93 * these parameters are intended to be user configurable
95 int expanded
; /* expanded view of all groups at startup */
98 * these reflect run time state
101 int loaded
; /* hotlist is loaded */
102 int readonly
; /* hotlist readonly */
103 int file_error
; /* parse error while reading file */
104 int running
; /* we are running dlg (and have to
106 int moving
; /* we are in moving hotlist currently */
107 int modified
; /* hotlist was modified */
108 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
111 static struct _hotlist_but
{
112 int ret_cmd
, flags
, y
, x
;
117 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), "move", LIST_HOTLIST
},
118 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), "r", LIST_HOTLIST
},
119 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), "e", LIST_MOVELIST
},
120 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), "g", LIST_MOVELIST
},
121 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), "e", LIST_HOTLIST
},
122 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), "g", LIST_HOTLIST
},
123 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), "cc", LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
124 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), "up", LIST_HOTLIST
|LIST_MOVELIST
},
125 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"),"ad", LIST_HOTLIST
},
126 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), "ct", LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
129 /* Directory hotlist */
130 static struct hotlist
{
131 enum HotListType type
;
134 struct hotlist
*head
;
136 struct hotlist
*next
;
139 static struct hotlist
*current_group
;
141 static void init_movelist (int, struct hotlist
*);
142 static void add_new_group_cmd (void);
143 static void add_new_entry_cmd (void);
144 static void remove_from_hotlist (struct hotlist
*entry
);
146 #define new_hotlist() g_new0(struct hotlist, 1)
149 hotlist_refresh (Dlg_head
* dlg
)
151 common_dialog_repaint (dlg
);
152 attrset (COLOR_NORMAL
);
153 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
154 dlg
->cols
- (UX
* 2));
155 if (!hotlist_state
.moving
)
156 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
159 /* If current->data is 0, then we are dealing with a VFS pathname */
160 static inline void update_path_name (void)
163 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
164 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
167 if (list
->current
->data
!= 0) {
168 struct hotlist
*hlp
= (struct hotlist
*)list
->current
->data
;
170 if (hlp
->type
== HL_TYPE_ENTRY
)
171 text
= hlp
->directory
;
173 text
= _("Subgroup - press ENTER to see list");
175 p
= g_strconcat (" ", current_group
->label
, " ", NULL
);
176 if (!hotlist_state
.moving
)
177 label_set_text (pname_group
, name_trunc (p
, dlg
->cols
- (UX
*2+4)));
179 label_set_text (movelist_group
, name_trunc (p
, dlg
->cols
- (UX
*2+4)));
182 text
= list
->current
->text
;
187 if (!hotlist_state
.moving
)
188 label_set_text (pname
, name_trunc (text
, dlg
->cols
- (UX
*2+4)));
192 #define CHECK_BUFFER \
196 if ((i = strlen (current->label) + 3) > buflen) { \
198 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
203 static void fill_listbox (void)
205 struct hotlist
*current
= current_group
->head
;
210 buf
= g_malloc (buflen
= 1024);
214 switch (current
->type
) {
218 strcat (strcat (buf
, "->"), current
->label
);
219 if (hotlist_state
.moving
)
220 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
222 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
226 if (hotlist_state
.moving
)
227 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
229 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
234 current
= current
->next
;
241 unlink_entry (struct hotlist
*entry
)
243 struct hotlist
*current
= current_group
->head
;
245 if (current
== entry
)
246 current_group
->head
= entry
->next
;
248 while (current
&& current
->next
!= entry
)
249 current
= current
->next
;
251 current
->next
= entry
->next
;
256 static int hotlist_button_callback (int action
, void *data
)
261 struct hotlist
*saved
= current_group
;
262 struct hotlist
*item
;
263 struct hotlist
*moveto_item
= 0;
264 struct hotlist
*moveto_group
= 0;
267 if (!l_hotlist
->current
)
268 return 0; /* empty group - nothing to do */
269 item
= l_hotlist
->current
->data
;
270 hotlist_state
.moving
= 1;
271 init_movelist (LIST_MOVELIST
, item
);
272 run_dlg (movelist_dlg
);
273 ret
= movelist_dlg
->ret_value
;
274 hotlist_state
.moving
= 0;
275 if (l_movelist
->current
)
276 moveto_item
= l_movelist
->current
->data
;
277 moveto_group
= current_group
;
278 destroy_dlg (movelist_dlg
);
279 current_group
= saved
;
282 if (moveto_item
== item
)
283 return 0; /* If we insert/append a before/after a
284 it hardly changes anything ;) */
286 listbox_remove_current (l_hotlist
, 1);
287 item
->up
= moveto_group
;
288 if (!moveto_group
->head
)
289 moveto_group
->head
= item
;
290 else if (!moveto_item
) { /* we have group with just comments */
291 struct hotlist
*p
= moveto_group
->head
;
297 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
298 if (!moveto_item
->next
)
299 moveto_item
->next
= item
;
301 item
->next
= moveto_item
->next
;
302 moveto_item
->next
= item
;
304 else if (moveto_group
->head
== moveto_item
) {
305 moveto_group
->head
= item
;
306 item
->next
= moveto_item
;
308 struct hotlist
*p
= moveto_group
->head
;
310 while (p
->next
!= moveto_item
)
312 item
->next
= p
->next
;
315 listbox_remove_list (l_hotlist
);
318 hotlist_state
.modified
= 1;
323 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
324 remove_from_hotlist (l_hotlist
->current
->data
);
329 add_new_group_cmd ();
339 add_new_entry_cmd ();
345 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
347 if (list
->current
->data
) {
348 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
349 if (hlp
->type
== HL_TYPE_ENTRY
)
352 listbox_remove_list (list
);
361 /* Fall through if list empty - just go up */
365 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
366 listbox_remove_list (list
);
367 current_group
= current_group
->up
;
380 static int hotlist_callback (Dlg_head
* h
, int Par
, int Msg
)
387 case DLG_UNHANDLED_KEY
:
394 if (hotlist_button_callback (B_ENTER
, 0)) {
395 h
->ret_value
= B_ENTER
;
401 if (hotlist_state
.type
!= LIST_VFSLIST
)
402 return !hotlist_button_callback (B_UP_GROUP
, 0);
407 if (!hotlist_state
.moving
) {
408 hotlist_button_callback (B_REMOVE
, 0);
415 if (!hotlist_state
.moving
)
417 if (l_hotlist
->current
){
418 if (l_hotlist
->current
->data
) {
419 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
420 if (hlp
->type
== HL_TYPE_ENTRY
) {
421 char *tmp
= g_strconcat ( "cd ", hlp
->directory
, NULL
);
422 stuff (input_w (cmdline
), tmp
, 0);
425 h
->ret_value
= B_CANCEL
;
431 return 1; /* ignore key */
438 if (hotlist_state
.moving
)
439 dlg_select_widget (movelist_dlg
, l_movelist
);
441 dlg_select_widget (hotlist_dlg
, l_hotlist
);
442 /* always stay on hotlist */
446 attrset (MENU_ENTRY_COLOR
);
453 static int l_call (void *l
)
455 WListbox
*list
= (WListbox
*) l
;
456 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
459 if (list
->current
->data
) {
460 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
461 if (hlp
->type
== HL_TYPE_ENTRY
) {
462 dlg
->ret_value
= B_ENTER
;
464 return listbox_finish
;
466 hotlist_button_callback (B_ENTER
, (void *)0);
467 hotlist_callback (dlg
, '\n', DLG_POST_KEY
);
468 return listbox_nothing
;
471 dlg
->ret_value
= B_ENTER
;
473 return listbox_finish
;
477 hotlist_button_callback (B_UP_GROUP
, (void *)0);
478 hotlist_callback (dlg
, 'u', DLG_POST_KEY
);
479 return listbox_nothing
;
483 static void add_name_to_list (char *path
)
485 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
487 #endif /* !USE_VFS */
490 * Expands all button names (once) and recalculates button positions.
491 * returns number of columns in the dialog box, which is 10 chars longer
494 * If common width of the window (i.e. in xterm) is less than returned
495 * width - sorry :) (anyway this did not handled in previous version too)
498 init_i18n_stuff(int list_type
, int cols
)
501 static char* cancel_but
= N_("&Cancel");
504 static int hotlist_i18n_flag
= 0;
506 if (!hotlist_i18n_flag
)
508 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
510 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
512 cancel_but
= _(cancel_but
);
513 hotlist_i18n_flag
= 1;
515 #endif /* ENABLE_NLS */
517 /* Dynamic resizing of buttonbars */
519 int len
[2], count
[2]; /* at most two lines of buttons */
522 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
523 len
[0] = len
[1] = count
[0] = count
[1] = 0;
525 /* Count len of buttonbars, assuming 2 extra space between buttons */
528 if (! (hotlist_but
[i
].type
& list_type
))
531 row
= hotlist_but
[i
].y
;
533 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
534 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
540 cols
= max(cols
, max(len
[0], len
[1]));
542 /* arrange buttons */
544 cur_x
[0] = cur_x
[1] = 0;
545 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
548 if (! (hotlist_but
[i
].type
& list_type
))
551 row
= hotlist_but
[i
].y
;
553 if (hotlist_but
[i
].x
!= 0)
555 /* not first int the row */
556 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
558 cols
- strlen (hotlist_but
[i
].text
) - 13;
560 hotlist_but
[i
].x
= cur_x
[row
];
563 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
564 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
572 init_hotlist (int list_type
)
575 char *title
, *help_node
;
578 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
582 hotlist_state
.expanded
=
583 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
586 if (list_type
== LIST_VFSLIST
) {
587 title
= _("Active VFS directories");
588 help_node
= "[vfshot]"; /* FIXME - no such node */
590 title
= _("Directory hotlist");
591 help_node
= "[Hotlist]";
595 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
596 hotlist_callback
, help_node
, title
, DLG_CENTER
);
598 for (i
= 0; i
< BUTTONS
; i
++) {
599 if (hotlist_but
[i
].type
& list_type
)
600 add_widget (hotlist_dlg
,
601 button_new (BY
+ hotlist_but
[i
].y
,
602 BX
+ hotlist_but
[i
].x
,
603 hotlist_but
[i
].ret_cmd
,
604 hotlist_but
[i
].flags
,
606 hotlist_button_callback
, 0,
607 hotlist_but
[i
].tkname
));
610 /* We add the labels.
611 * pname will hold entry's pathname;
612 * pname_group will hold name of current group
614 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "", "the-lab");
615 add_widget (hotlist_dlg
, pname
);
616 if (!hotlist_state
.moving
) {
617 add_widget (hotlist_dlg
,
618 label_new (UY
- 12 + LINES
, UX
+ 1,
619 _(" Directory path "), NULL
));
621 /* This one holds the displayed pathname */
622 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "), NULL
);
623 add_widget (hotlist_dlg
, pname_group
);
625 /* get new listbox */
627 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
628 listbox_cback
, l_call
, "listbox");
630 /* Fill the hotlist with the active VFS or the hotlist */
632 if (list_type
== LIST_VFSLIST
) {
633 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
634 vfs_fill_names (add_name_to_list
);
636 #endif /* !USE_VFS */
639 add_widget (hotlist_dlg
, l_hotlist
);
640 /* add listbox to the dialogs */
644 init_movelist (int list_type
, struct hotlist
*item
)
647 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
648 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
653 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
654 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
);
657 for (i
= 0; i
< BUTTONS
; i
++) {
658 if (hotlist_but
[i
].type
& list_type
)
659 add_widget (movelist_dlg
,
660 button_new (BY
- 4 + hotlist_but
[i
].y
,
661 BX
+ hotlist_but
[i
].x
,
662 hotlist_but
[i
].ret_cmd
,
663 hotlist_but
[i
].flags
,
665 hotlist_button_callback
, 0,
666 hotlist_but
[i
].tkname
));
669 /* We add the labels. We are interested in the last one,
670 * that one will hold the path name label
672 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "), NULL
);
673 add_widget (movelist_dlg
, movelist_group
);
674 /* get new listbox */
676 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
677 movelist_dlg
->lines
- 8, listbox_cback
, l_call
,
682 add_widget (movelist_dlg
, l_movelist
);
683 /* add listbox to the dialogs */
687 * Destroy the list dialog.
688 * Don't confuse with done_hotlist() for the list in memory.
690 static void hotlist_done (void)
692 destroy_dlg (hotlist_dlg
);
695 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
700 find_group_section (struct hotlist
*grp
)
702 return g_strconcat (grp
->directory
, ".Group", NULL
);
707 /* 1.11.96 bor: added pos parameter to control placement of new item.
708 see widget.c, listbox_add_item()
709 now hotlist is in unsorted mode
711 static struct hotlist
*
712 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
715 struct hotlist
*current
= NULL
;
718 * Hotlist is neither loaded nor loading.
719 * Must be called by "Ctrl-x a" before using hotlist.
724 if (l_hotlist
&& l_hotlist
->current
)
725 current
= l_hotlist
->current
->data
;
727 new = new_hotlist ();
731 new->directory
= directory
;
732 new->up
= current_group
;
734 if (!current_group
->head
) { /* first element in group */
735 current_group
->head
= new;
736 } else if (pos
== 2) { /* should be appended after current*/
737 new->next
= current
->next
;
739 } else if (pos
== 1 &&
740 current
== current_group
->head
) {
741 /* should be inserted before first item */
743 current_group
->head
= new;
744 } else if (pos
== 1) { /* befor current */
745 struct hotlist
*p
= current_group
->head
;
747 while (p
->next
!= current
)
752 } else { /* append at the end */
753 struct hotlist
*p
= current_group
->head
;
761 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
) {
762 if (type
== HL_TYPE_GROUP
) {
763 char *lbl
= g_strconcat ("->", new->label
, NULL
);
765 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
768 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
769 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
777 * Support routine for add_new_entry_input()/add_new_group_input()
778 * Change positions of buttons (first three widgets).
780 * This is just a quick hack. Accurate procedure must take care of
781 * internationalized label lengths and total buttonbar length...assume
782 * 64 is longer anyway.
784 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
786 int i
, l
[3], space
, cur_x
;
788 for (i
= 0; i
< 3; i
++)
790 qw
[i
].text
= _(qw
[i
].text
);
791 l
[i
] = strlen (qw
[i
].text
) + 3;
793 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
795 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
797 qw
[i
].relative_x
= cur_x
;
798 qw
[i
].x_divisions
= len
;
801 #endif /* ENABLE_NLS */
803 static int add_new_entry_input (char *header
, char *text1
, char *text2
, char *help
, char **r1
, char **r2
)
805 #define RELATIVE_Y_BUTTONS 4
806 #define RELATIVE_Y_LABEL_PTH 3
807 #define RELATIVE_Y_INPUT_PTH 4
809 QuickDialog Quick_input
;
810 static QuickWidget quick_widgets
[] = {
811 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
812 0, 0, "button-cancel" },
813 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
814 0, 0, "button-insert" },
815 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
816 0, 0, "button-append" },
817 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
819 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
821 { quick_input
, 4, 80, 3, 0, "", 58, 0,
823 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
830 char *my_str1
, *my_str2
;
833 static int i18n_flag
= 0;
834 #endif /* ENABLE_NLS */
836 len
= max (strlen (header
), msglen (text1
, &lines1
));
837 len
= max (len
, msglen (text2
, &lines2
)) + 4;
843 add_widgets_i18n(quick_widgets
, len
);
846 #endif /* ENABLE_NLS */
848 Quick_input
.xlen
= len
;
849 Quick_input
.xpos
= -1;
850 Quick_input
.title
= header
;
851 Quick_input
.help
= help
;
852 Quick_input
.i18n
= 0;
853 quick_widgets
[6].text
= text1
;
854 quick_widgets
[4].text
= text2
;
855 quick_widgets
[5].text
= *r1
;
856 quick_widgets
[3].text
= *r2
;
858 for (i
= 0; i
< 7; i
++)
859 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
860 Quick_input
.ylen
= lines1
+ lines2
+ 7;
862 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
863 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
864 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
865 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
866 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
868 quick_widgets
[5].str_result
= &my_str1
;
869 quick_widgets
[3].str_result
= &my_str2
;
871 Quick_input
.widgets
= quick_widgets
;
872 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
873 *r1
= *(quick_widgets
[5].str_result
);
874 *r2
= *(quick_widgets
[3].str_result
);
880 static void add_new_entry_cmd (void)
882 char *title
= 0, *url
= 0;
885 /* Take current directory as default value for input fields */
886 title
= url
= cpanel
->cwd
;
888 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
889 "[Hotlist]", &title
, &url
);
891 if (!ret
|| !title
|| !*title
|| !url
|| !*url
)
894 if (ret
== B_ENTER
|| ret
== B_APPEND
)
895 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 2);
897 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 1);
899 hotlist_state
.modified
= 1;
902 static int add_new_group_input (char *header
, char *label
, char **result
)
905 QuickDialog Quick_input
;
906 static QuickWidget quick_widgets
[] = {
907 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
909 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
911 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
913 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
914 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
916 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
917 quick_widgets variable above */
924 static int i18n_flag
= 0;
925 #endif /* ENABLE_NLS */
927 len
= max (strlen (header
), msglen (label
, &lines
)) + 4;
933 add_widgets_i18n(quick_widgets
, len
);
936 #endif /* ENABLE_NLS */
938 Quick_input
.xlen
= len
;
939 Quick_input
.xpos
= -1;
940 Quick_input
.title
= header
;
941 Quick_input
.help
= "[Hotlist]";
942 Quick_input
.i18n
= 0;
943 quick_widgets
[4].text
= label
;
945 for (i
= 0; i
< 5; i
++)
946 quick_widgets
[i
].y_divisions
= lines
+6;
947 Quick_input
.ylen
= lines
+ 6;
949 for (i
= 0; i
< 4; i
++)
950 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
952 quick_widgets
[3].str_result
= &my_str
;
953 quick_widgets
[3].text
= "";
955 Quick_input
.widgets
= quick_widgets
;
956 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
957 *result
= *(quick_widgets
[3].str_result
);
963 static void add_new_group_cmd (void)
968 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
969 if (!ret
|| !label
|| !*label
)
972 if (ret
== B_ENTER
|| ret
== B_APPEND
)
973 add2hotlist (label
, 0, HL_TYPE_GROUP
, 2);
975 add2hotlist (label
, 0, HL_TYPE_GROUP
, 1);
977 hotlist_state
.modified
= 1;
980 void add2hotlist_cmd (void)
982 char *prompt
, *label
;
983 char *cp
= _("Label for \"%s\":");
986 prompt
= g_strdup_printf (cp
, name_trunc (cpanel
->cwd
, COLS
-2*UX
-(l
+8)));
987 label
= input_dialog (_(" Add to hotlist "), prompt
, cpanel
->cwd
);
989 if (!label
|| !*label
)
992 add2hotlist (label
,g_strdup (cpanel
->cwd
), HL_TYPE_ENTRY
, 0);
993 hotlist_state
.modified
= 1;
996 static void remove_group (struct hotlist
*grp
)
998 struct hotlist
*current
= grp
->head
;
1001 struct hotlist
*next
= current
->next
;
1003 if (current
->type
== HL_TYPE_GROUP
)
1004 remove_group (current
);
1007 g_free (current
->label
);
1008 if (current
->directory
)
1009 g_free (current
->directory
);
1017 static void remove_from_hotlist (struct hotlist
*entry
)
1019 if (entry
->type
== HL_TYPE_GROUP
) {
1024 header
= g_strconcat (_(" Remove: "),
1025 name_trunc (entry
->label
, 30),
1028 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1030 _("&No"), _("&Yes"));
1037 remove_group (entry
);
1040 unlink_entry (entry
);
1043 g_free (entry
->label
);
1044 if (entry
->directory
)
1045 g_free (entry
->directory
);
1047 /* now remove list entry from screen */
1048 listbox_remove_current (l_hotlist
, 1);
1049 hotlist_state
.modified
= 1;
1052 char *hotlist_cmd (int vfs_or_hotlist
)
1054 char *target
= NULL
;
1056 hotlist_state
.type
= vfs_or_hotlist
;
1059 init_hotlist (vfs_or_hotlist
);
1061 /* display file info */
1062 attrset (SELECTED_COLOR
);
1064 hotlist_state
.running
= 1;
1065 run_dlg (hotlist_dlg
);
1066 hotlist_state
.running
= 0;
1069 switch (hotlist_dlg
->ret_value
) {
1074 if (l_hotlist
->current
->data
) {
1075 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1076 target
= g_strdup (hlp
->directory
);
1078 target
= g_strdup (l_hotlist
->current
->text
);
1087 load_group (struct hotlist
*grp
)
1091 char *group_section
;
1092 struct hotlist
*current
= 0;
1094 group_section
= find_group_section (grp
);
1096 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1098 current_group
= grp
;
1100 while (profile_keys
){
1101 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1102 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1104 g_free (group_section
);
1106 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1108 while (profile_keys
){
1109 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1110 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1113 for (current
= grp
->head
; current
; current
= current
->next
)
1114 load_group (current
);
1119 #define TKN_STRING 2
1121 #define TKN_ENDGROUP 4
1122 #define TKN_COMMENT 5
1125 #define TKN_UNKNOWN 127
1127 static char *tkn_buf
;
1128 static int tkn_buf_length
;
1129 static int tkn_length
;
1131 static char *hotlist_file_name
;
1132 static FILE *hotlist_file
;
1133 static time_t hotlist_file_mtime
;
1135 static int hot_skip_blanks (void)
1139 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1145 static int hot_next_token (void)
1149 #define CHECK_BUF() \
1151 if (tkn_length == tkn_buf_length) \
1152 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1153 : ( g_malloc (tkn_buf_length = 1024)); \
1159 c
= hot_skip_blanks ();
1168 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1173 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1177 tkn_buf
[tkn_length
] = '\0';
1181 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1183 if ((c
= getc (hotlist_file
)) == EOF
)
1186 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1191 tkn_buf
[tkn_length
] = '\0';
1195 if ((c
= getc (hotlist_file
)) == EOF
)
1200 /* fall through; it is taken as normal character */
1205 tkn_buf
[tkn_length
++] = toupper(c
);
1206 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1208 ungetc (c
, hotlist_file
);
1210 tkn_buf
[tkn_length
] = '\0';
1211 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1213 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1215 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1216 return TKN_ENDGROUP
;
1217 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1225 #define SKIP_TO_EOL { \
1227 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1230 #define CHECK_TOKEN(_TKN_) \
1231 if ((tkn = hot_next_token ()) != _TKN_) { \
1232 hotlist_state.readonly = 1; \
1233 hotlist_state.file_error = 1; \
1234 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1235 tkn = hot_next_token (); \
1240 hot_load_group (struct hotlist
* grp
)
1243 struct hotlist
*new_grp
;
1246 current_group
= grp
;
1248 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1251 CHECK_TOKEN(TKN_STRING
);
1252 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1254 hot_load_group (new_grp
);
1255 current_group
= grp
;
1258 CHECK_TOKEN(TKN_STRING
);
1259 label
= g_strdup (tkn_buf
);
1260 CHECK_TOKEN(TKN_URL
);
1261 CHECK_TOKEN(TKN_STRING
);
1262 url
= g_strdup (tkn_buf
);
1263 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1267 label
= g_strdup (tkn_buf
);
1268 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1271 hotlist_state
.readonly
= 1;
1272 hotlist_state
.file_error
= 1;
1276 /* skip empty lines */
1279 hotlist_state
.readonly
= 1;
1280 hotlist_state
.file_error
= 1;
1288 hot_load_file (struct hotlist
* grp
)
1291 struct hotlist
*new_grp
;
1294 current_group
= grp
;
1296 while ((tkn
= hot_next_token())!= TKN_EOF
)
1299 CHECK_TOKEN(TKN_STRING
);
1300 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1302 hot_load_group (new_grp
);
1303 current_group
= grp
;
1306 CHECK_TOKEN(TKN_STRING
);
1307 label
= g_strdup (tkn_buf
);
1308 CHECK_TOKEN(TKN_URL
);
1309 CHECK_TOKEN(TKN_STRING
);
1310 url
= g_strdup (tkn_buf
);
1311 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1315 label
= g_strdup (tkn_buf
);
1316 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1319 /* skip empty lines */
1322 hotlist_state
.readonly
= 1;
1323 hotlist_state
.file_error
= 1;
1330 clean_up_hotlist_groups (char *section
)
1336 grp_section
= g_strconcat (section
, ".Group", NULL
);
1337 if (profile_has_section (section
, profile_name
))
1338 profile_clean_section (section
, profile_name
);
1339 if (profile_has_section (grp_section
, profile_name
)) {
1340 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1342 while (profile_keys
) {
1343 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1344 clean_up_hotlist_groups (key
);
1346 profile_clean_section (grp_section
, profile_name
);
1348 g_free (grp_section
);
1353 void load_hotlist (void)
1356 int has_old_list
= 0;
1357 int remove_old_list
= 0;
1358 struct stat stat_buf
;
1360 if (hotlist_state
.loaded
) {
1361 stat (hotlist_file_name
, &stat_buf
);
1362 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1368 if (!hotlist_file_name
)
1369 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1371 hotlist
= new_hotlist ();
1372 hotlist
->type
= HL_TYPE_GROUP
;
1373 hotlist
->label
= g_strdup (_(" Top level group "));
1374 hotlist
->up
= hotlist
;
1378 hotlist
->directory
= g_strdup ("Hotlist");
1380 grp_section
= g_strconcat ("Hotlist", ".Group", NULL
);
1381 has_old_list
= profile_has_section ("Hotlist", profile_name
) ||
1382 profile_has_section (grp_section
, profile_name
);
1383 g_free (grp_section
);
1385 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1388 load_group (hotlist
);
1389 hotlist_state
.loaded
= 1;
1391 * just to be sure we got copy
1393 hotlist_state
.modified
= 1;
1394 result
= save_hotlist ();
1395 hotlist_state
.modified
= 0;
1397 remove_old_list
= 1;
1401 msg
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1402 _(" file, your old hotlist entries were not deleted"), NULL
);
1404 message (D_ERROR
, _(" Hotlist Load "), msg
);
1408 hot_load_file (hotlist
);
1409 fclose (hotlist_file
);
1410 hotlist_state
.loaded
= 1;
1416 _("You have ~/"), HOTLIST_FILENAME
, _(" file and [Hotlist] section in ~/"), PROFILE_NAME
, "\n",
1417 _("Your ~/"), HOTLIST_FILENAME
, _(" most probably was created\n"),
1418 _("by an earlier development version of MC\nand is more actual than ~/"),
1419 PROFILE_NAME
, _(" entries\n\n"),
1420 _("You can choose between\n\n"
1421 " Remove - remove old hotlist entries from ~/"), PROFILE_NAME
, "\n",
1422 _(" Keep - keep your old entries; you will be asked\n"
1423 " the same question next time\n"
1424 " Merge - add old entries to hotlist as group \"Entries from ~/"),
1425 PROFILE_NAME
, "\"\n\n", NULL
);
1427 result
= query_dialog (_(" Hotlist Load "),
1428 msg
, D_ERROR
, 3, _("&Remove"), _("&Keep"), _("&Merge"));
1430 remove_old_list
= 1;
1431 else if (result
== 2) {
1432 struct hotlist
*grp
= hotlist
->head
;
1433 struct hotlist
*old
;
1436 load_group (hotlist
);
1438 old
= new_hotlist ();
1439 old
->type
= HL_TYPE_GROUP
;
1440 old
->label
= g_strconcat (_(" Entries from ~/"), PROFILE_NAME
, NULL
);
1442 old
->head
= hotlist
->head
;
1444 hotlist
->head
= old
;
1445 hotlist_state
.modified
= 1;
1446 if (!save_hotlist ()){
1449 str
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1450 _(" file, your old hotlist entries were not deleted"), NULL
);
1452 message (D_ERROR
, _(" Hotlist Load "), str
);
1455 remove_old_list
= 1;
1456 hotlist_state
.modified
= 0;
1461 if (remove_old_list
) {
1462 clean_up_hotlist_groups ("Hotlist");
1466 stat (hotlist_file_name
, &stat_buf
);
1467 hotlist_file_mtime
= stat_buf
.st_mtime
;
1468 current_group
= hotlist
;
1472 save_group (struct hotlist
*grp
)
1474 struct hotlist
*current
= grp
->head
;
1475 char *group_section
;
1477 group_section
= find_group_section (grp
);
1479 profile_clean_section (group_section
, profile_name
);
1480 for (;current
&& current
->type
== HL_TYPE_GROUP
; current
= current
->next
){
1481 WritePrivateProfileString (group_section
,
1486 g_free (group_section
);
1488 for (current
= grp
->head
;
1489 current
&& current
->type
== HL_TYPE_GROUP
;
1490 current
= current
->next
)
1491 save_group (current
);
1493 profile_clean_section (grp
->directory
, profile_name
);
1494 for (;current
; current
= current
->next
){
1495 WritePrivateProfileString (grp
->directory
,
1502 static int list_level
= 0;
1505 hot_save_group (struct hotlist
*grp
)
1507 struct hotlist
*current
= grp
->head
;
1513 for (i = 0; i < n; i++) \
1514 putc (' ', hotlist_file); \
1517 for (;current
; current
= current
->next
)
1518 switch (current
->type
) {
1520 INDENT (list_level
);
1521 fputs ("GROUP \"", hotlist_file
);
1522 for (s
= current
->label
; *s
; s
++) {
1524 putc ('\\', hotlist_file
);
1525 else if (*s
== '\\')
1526 putc ('\\', hotlist_file
);
1527 putc (*s
, hotlist_file
);
1529 fputs ("\"\n", hotlist_file
);
1531 hot_save_group (current
);
1533 INDENT (list_level
);
1534 fputs ("ENDGROUP\n", hotlist_file
);
1538 fputs ("ENTRY \"", hotlist_file
);
1539 for (s
= current
->label
; *s
; s
++) {
1541 putc ('\\', hotlist_file
);
1542 else if (*s
== '\\')
1543 putc ('\\', hotlist_file
);
1544 putc (*s
, hotlist_file
);
1546 fputs ("\" URL \"", hotlist_file
);
1547 for (s
= current
->directory
; *s
; s
++) {
1549 putc ('\\', hotlist_file
);
1550 else if (*s
== '\\')
1551 putc ('\\', hotlist_file
);
1552 putc (*s
, hotlist_file
);
1554 fputs ("\"\n", hotlist_file
);
1556 case HL_TYPE_COMMENT
:
1557 fprintf (hotlist_file
, "#%s\n", current
->label
);
1563 int save_hotlist (void)
1566 struct stat stat_buf
;
1568 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1569 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", NULL
);
1571 rename (hotlist_file_name
, fbak
);
1572 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1573 if (stat (fbak
, &stat_buf
) == 0)
1574 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1576 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1577 hot_save_group (hotlist
);
1578 fclose (hotlist_file
);
1579 stat (hotlist_file_name
, &stat_buf
);
1580 hotlist_file_mtime
= stat_buf
.st_mtime
;
1582 hotlist_state
.modified
= 0;
1584 rename (fbak
, hotlist_file_name
);
1592 * Unload list from memory.
1593 * Don't confuse with hotlist_done() for GUI.
1595 void done_hotlist (void)
1598 remove_group (hotlist
);
1600 g_free (hotlist
->label
);
1601 if (hotlist
->directory
)
1602 g_free (hotlist
->directory
);
1607 hotlist_state
.loaded
= 0;
1609 if (hotlist_file_name
){
1610 g_free (hotlist_file_name
);
1611 hotlist_file_name
= 0;