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" /* ctrl_pressed */
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 */
154 static inline void update_path_name (void)
157 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
158 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
161 if (list
->current
->data
!= 0) {
162 struct hotlist
*hlp
= (struct hotlist
*)list
->current
->data
;
164 if (hlp
->type
== HL_TYPE_ENTRY
)
165 text
= hlp
->directory
;
167 text
= _("Subgroup - press ENTER to see list");
169 p
= g_strconcat (" ", current_group
->label
, " ", NULL
);
170 if (!hotlist_state
.moving
)
171 label_set_text (pname_group
, name_trunc (p
, dlg
->cols
- (UX
*2+4)));
173 label_set_text (movelist_group
, name_trunc (p
, dlg
->cols
- (UX
*2+4)));
176 text
= list
->current
->text
;
181 if (!hotlist_state
.moving
)
182 label_set_text (pname
, name_trunc (text
, dlg
->cols
- (UX
*2+4)));
186 #define CHECK_BUFFER \
190 if ((i = strlen (current->label) + 3) > buflen) { \
192 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
197 static void fill_listbox (void)
199 struct hotlist
*current
= current_group
->head
;
204 buf
= g_malloc (buflen
= 1024);
208 switch (current
->type
) {
212 strcat (strcat (buf
, "->"), current
->label
);
213 if (hotlist_state
.moving
)
214 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
216 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
220 if (hotlist_state
.moving
)
221 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
223 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
228 current
= current
->next
;
235 unlink_entry (struct hotlist
*entry
)
237 struct hotlist
*current
= current_group
->head
;
239 if (current
== entry
)
240 current_group
->head
= entry
->next
;
242 while (current
&& current
->next
!= entry
)
243 current
= current
->next
;
245 current
->next
= entry
->next
;
250 static int hotlist_button_callback (int action
, void *data
)
255 struct hotlist
*saved
= current_group
;
256 struct hotlist
*item
;
257 struct hotlist
*moveto_item
= 0;
258 struct hotlist
*moveto_group
= 0;
261 if (!l_hotlist
->current
)
262 return 0; /* empty group - nothing to do */
263 item
= l_hotlist
->current
->data
;
264 hotlist_state
.moving
= 1;
265 init_movelist (LIST_MOVELIST
, item
);
266 run_dlg (movelist_dlg
);
267 ret
= movelist_dlg
->ret_value
;
268 hotlist_state
.moving
= 0;
269 if (l_movelist
->current
)
270 moveto_item
= l_movelist
->current
->data
;
271 moveto_group
= current_group
;
272 destroy_dlg (movelist_dlg
);
273 current_group
= saved
;
276 if (moveto_item
== item
)
277 return 0; /* If we insert/append a before/after a
278 it hardly changes anything ;) */
280 listbox_remove_current (l_hotlist
, 1);
281 item
->up
= moveto_group
;
282 if (!moveto_group
->head
)
283 moveto_group
->head
= item
;
284 else if (!moveto_item
) { /* we have group with just comments */
285 struct hotlist
*p
= moveto_group
->head
;
291 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
292 if (!moveto_item
->next
)
293 moveto_item
->next
= item
;
295 item
->next
= moveto_item
->next
;
296 moveto_item
->next
= item
;
298 else if (moveto_group
->head
== moveto_item
) {
299 moveto_group
->head
= item
;
300 item
->next
= moveto_item
;
302 struct hotlist
*p
= moveto_group
->head
;
304 while (p
->next
!= moveto_item
)
306 item
->next
= p
->next
;
309 listbox_remove_list (l_hotlist
);
312 hotlist_state
.modified
= 1;
317 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
318 remove_from_hotlist (l_hotlist
->current
->data
);
323 add_new_group_cmd ();
333 add_new_entry_cmd ();
339 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
341 if (list
->current
->data
) {
342 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
343 if (hlp
->type
== HL_TYPE_ENTRY
)
346 listbox_remove_list (list
);
355 /* Fall through if list empty - just go up */
359 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
360 listbox_remove_list (list
);
361 current_group
= current_group
->up
;
374 static int hotlist_callback (Dlg_head
* h
, int Par
, int Msg
)
381 case DLG_UNHANDLED_KEY
:
388 if (hotlist_button_callback (B_ENTER
, 0)) {
389 h
->ret_value
= B_ENTER
;
395 if (hotlist_state
.type
!= LIST_VFSLIST
)
396 return !hotlist_button_callback (B_UP_GROUP
, 0);
401 if (!hotlist_state
.moving
) {
402 hotlist_button_callback (B_REMOVE
, 0);
409 if (!hotlist_state
.moving
)
411 if (l_hotlist
->current
){
412 if (l_hotlist
->current
->data
) {
413 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
414 if (hlp
->type
== HL_TYPE_ENTRY
) {
415 char *tmp
= g_strconcat ( "cd ", hlp
->directory
, NULL
);
416 stuff (cmdline
, tmp
, 0);
419 h
->ret_value
= B_CANCEL
;
425 return 1; /* ignore key */
432 if (hotlist_state
.moving
)
433 dlg_select_widget (movelist_dlg
, l_movelist
);
435 dlg_select_widget (hotlist_dlg
, l_hotlist
);
436 /* always stay on hotlist */
440 attrset (MENU_ENTRY_COLOR
);
447 static int l_call (void *l
)
449 WListbox
*list
= (WListbox
*) l
;
450 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
453 if (list
->current
->data
) {
454 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
455 if (hlp
->type
== HL_TYPE_ENTRY
) {
456 dlg
->ret_value
= B_ENTER
;
458 return listbox_finish
;
460 hotlist_button_callback (B_ENTER
, (void *)0);
461 hotlist_callback (dlg
, '\n', DLG_POST_KEY
);
462 return listbox_nothing
;
465 dlg
->ret_value
= B_ENTER
;
467 return listbox_finish
;
471 hotlist_button_callback (B_UP_GROUP
, (void *)0);
472 hotlist_callback (dlg
, 'u', DLG_POST_KEY
);
473 return listbox_nothing
;
477 static void add_name_to_list (char *path
)
479 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
481 #endif /* !USE_VFS */
484 * Expands all button names (once) and recalculates button positions.
485 * returns number of columns in the dialog box, which is 10 chars longer
488 * If common width of the window (i.e. in xterm) is less than returned
489 * width - sorry :) (anyway this did not handled in previous version too)
492 init_i18n_stuff(int list_type
, int cols
)
495 static char* cancel_but
= N_("&Cancel");
498 static int hotlist_i18n_flag
= 0;
500 if (!hotlist_i18n_flag
)
502 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
504 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
506 cancel_but
= _(cancel_but
);
507 hotlist_i18n_flag
= 1;
509 #endif /* ENABLE_NLS */
511 /* Dynamic resizing of buttonbars */
513 int len
[2], count
[2]; /* at most two lines of buttons */
516 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
517 len
[0] = len
[1] = count
[0] = count
[1] = 0;
519 /* Count len of buttonbars, assuming 2 extra space between buttons */
522 if (! (hotlist_but
[i
].type
& list_type
))
525 row
= hotlist_but
[i
].y
;
527 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
528 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
534 cols
= max(cols
, max(len
[0], len
[1]));
536 /* arrange buttons */
538 cur_x
[0] = cur_x
[1] = 0;
539 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
542 if (! (hotlist_but
[i
].type
& list_type
))
545 row
= hotlist_but
[i
].y
;
547 if (hotlist_but
[i
].x
!= 0)
549 /* not first int the row */
550 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
552 cols
- strlen (hotlist_but
[i
].text
) - 13;
554 hotlist_but
[i
].x
= cur_x
[row
];
557 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
558 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
566 init_hotlist (int list_type
)
569 char *title
, *help_node
;
572 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
576 hotlist_state
.expanded
=
577 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
580 if (list_type
== LIST_VFSLIST
) {
581 title
= _("Active VFS directories");
582 help_node
= "[vfshot]"; /* FIXME - no such node */
584 title
= _("Directory hotlist");
585 help_node
= "[Hotlist]";
589 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
590 hotlist_callback
, help_node
, title
, DLG_CENTER
);
592 for (i
= 0; i
< BUTTONS
; i
++) {
593 if (hotlist_but
[i
].type
& list_type
)
594 add_widget (hotlist_dlg
,
595 button_new (BY
+ hotlist_but
[i
].y
,
596 BX
+ hotlist_but
[i
].x
,
597 hotlist_but
[i
].ret_cmd
,
598 hotlist_but
[i
].flags
,
600 hotlist_button_callback
, 0,
601 hotlist_but
[i
].tkname
));
604 /* We add the labels.
605 * pname will hold entry's pathname;
606 * pname_group will hold name of current group
608 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "", "the-lab");
609 add_widget (hotlist_dlg
, pname
);
610 if (!hotlist_state
.moving
) {
611 add_widget (hotlist_dlg
,
612 label_new (UY
- 12 + LINES
, UX
+ 1,
613 _(" Directory path "), NULL
));
615 /* This one holds the displayed pathname */
616 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "), NULL
);
617 add_widget (hotlist_dlg
, pname_group
);
619 /* get new listbox */
621 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
622 listbox_cback
, l_call
, "listbox");
624 /* Fill the hotlist with the active VFS or the hotlist */
626 if (list_type
== LIST_VFSLIST
) {
627 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
628 vfs_fill_names (add_name_to_list
);
630 #endif /* !USE_VFS */
633 add_widget (hotlist_dlg
, l_hotlist
);
634 /* add listbox to the dialogs */
638 init_movelist (int list_type
, struct hotlist
*item
)
641 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
642 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
647 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
648 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
);
651 for (i
= 0; i
< BUTTONS
; i
++) {
652 if (hotlist_but
[i
].type
& list_type
)
653 add_widget (movelist_dlg
,
654 button_new (BY
- 4 + hotlist_but
[i
].y
,
655 BX
+ hotlist_but
[i
].x
,
656 hotlist_but
[i
].ret_cmd
,
657 hotlist_but
[i
].flags
,
659 hotlist_button_callback
, 0,
660 hotlist_but
[i
].tkname
));
663 /* We add the labels. We are interested in the last one,
664 * that one will hold the path name label
666 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "), NULL
);
667 add_widget (movelist_dlg
, movelist_group
);
668 /* get new listbox */
670 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
671 movelist_dlg
->lines
- 8, listbox_cback
, l_call
,
676 add_widget (movelist_dlg
, l_movelist
);
677 /* add listbox to the dialogs */
681 * Destroy the list dialog.
682 * Don't confuse with done_hotlist() for the list in memory.
684 static void hotlist_done (void)
686 destroy_dlg (hotlist_dlg
);
689 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
694 find_group_section (struct hotlist
*grp
)
696 return g_strconcat (grp
->directory
, ".Group", NULL
);
701 /* 1.11.96 bor: added pos parameter to control placement of new item.
702 see widget.c, listbox_add_item()
703 now hotlist is in unsorted mode
705 static struct hotlist
*
706 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
709 struct hotlist
*current
= NULL
;
712 * Hotlist is neither loaded nor loading.
713 * Must be called by "Ctrl-x a" before using hotlist.
718 if (l_hotlist
&& l_hotlist
->current
)
719 current
= l_hotlist
->current
->data
;
721 new = new_hotlist ();
725 new->directory
= directory
;
726 new->up
= current_group
;
728 if (!current_group
->head
) { /* first element in group */
729 current_group
->head
= new;
730 } else if (pos
== 2) { /* should be appended after current*/
731 new->next
= current
->next
;
733 } else if (pos
== 1 &&
734 current
== current_group
->head
) {
735 /* should be inserted before first item */
737 current_group
->head
= new;
738 } else if (pos
== 1) { /* befor current */
739 struct hotlist
*p
= current_group
->head
;
741 while (p
->next
!= current
)
746 } else { /* append at the end */
747 struct hotlist
*p
= current_group
->head
;
755 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
) {
756 if (type
== HL_TYPE_GROUP
) {
757 char *lbl
= g_strconcat ("->", new->label
, NULL
);
759 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
762 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
763 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
771 * Support routine for add_new_entry_input()/add_new_group_input()
772 * Change positions of buttons (first three widgets).
774 * This is just a quick hack. Accurate procedure must take care of
775 * internationalized label lengths and total buttonbar length...assume
776 * 64 is longer anyway.
778 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
780 int i
, l
[3], space
, cur_x
;
782 for (i
= 0; i
< 3; i
++)
784 qw
[i
].text
= _(qw
[i
].text
);
785 l
[i
] = strlen (qw
[i
].text
) + 3;
787 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
789 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
791 qw
[i
].relative_x
= cur_x
;
792 qw
[i
].x_divisions
= len
;
795 #endif /* ENABLE_NLS */
797 static int add_new_entry_input (char *header
, char *text1
, char *text2
, char *help
, char **r1
, char **r2
)
799 #define RELATIVE_Y_BUTTONS 4
800 #define RELATIVE_Y_LABEL_PTH 3
801 #define RELATIVE_Y_INPUT_PTH 4
803 QuickDialog Quick_input
;
804 static QuickWidget quick_widgets
[] = {
805 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
806 0, 0, "button-cancel" },
807 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
808 0, 0, "button-insert" },
809 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
810 0, 0, "button-append" },
811 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
813 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
815 { quick_input
, 4, 80, 3, 0, "", 58, 0,
817 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
824 char *my_str1
, *my_str2
;
827 static int i18n_flag
= 0;
828 #endif /* ENABLE_NLS */
830 len
= max (strlen (header
), msglen (text1
, &lines1
));
831 len
= max (len
, msglen (text2
, &lines2
)) + 4;
837 add_widgets_i18n(quick_widgets
, len
);
840 #endif /* ENABLE_NLS */
842 Quick_input
.xlen
= len
;
843 Quick_input
.xpos
= -1;
844 Quick_input
.title
= header
;
845 Quick_input
.help
= help
;
846 Quick_input
.i18n
= 0;
847 quick_widgets
[6].text
= text1
;
848 quick_widgets
[4].text
= text2
;
849 quick_widgets
[5].text
= *r1
;
850 quick_widgets
[3].text
= *r2
;
852 for (i
= 0; i
< 7; i
++)
853 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
854 Quick_input
.ylen
= lines1
+ lines2
+ 7;
856 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
857 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
858 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
859 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
860 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
862 quick_widgets
[5].str_result
= &my_str1
;
863 quick_widgets
[3].str_result
= &my_str2
;
865 Quick_input
.widgets
= quick_widgets
;
866 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
867 *r1
= *(quick_widgets
[5].str_result
);
868 *r2
= *(quick_widgets
[3].str_result
);
874 static void add_new_entry_cmd (void)
876 char *title
= 0, *url
= 0;
879 /* Take current directory as default value for input fields */
880 title
= url
= cpanel
->cwd
;
882 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
883 "[Hotlist]", &title
, &url
);
885 if (!ret
|| !title
|| !*title
|| !url
|| !*url
)
888 if (ret
== B_ENTER
|| ret
== B_APPEND
)
889 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 2);
891 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 1);
893 hotlist_state
.modified
= 1;
896 static int add_new_group_input (char *header
, char *label
, char **result
)
899 QuickDialog Quick_input
;
900 static QuickWidget quick_widgets
[] = {
901 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
903 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
905 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
907 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
908 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
910 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
911 quick_widgets variable above */
918 static int i18n_flag
= 0;
919 #endif /* ENABLE_NLS */
921 len
= max (strlen (header
), msglen (label
, &lines
)) + 4;
927 add_widgets_i18n(quick_widgets
, len
);
930 #endif /* ENABLE_NLS */
932 Quick_input
.xlen
= len
;
933 Quick_input
.xpos
= -1;
934 Quick_input
.title
= header
;
935 Quick_input
.help
= "[Hotlist]";
936 Quick_input
.i18n
= 0;
937 quick_widgets
[4].text
= label
;
939 for (i
= 0; i
< 5; i
++)
940 quick_widgets
[i
].y_divisions
= lines
+6;
941 Quick_input
.ylen
= lines
+ 6;
943 for (i
= 0; i
< 4; i
++)
944 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
946 quick_widgets
[3].str_result
= &my_str
;
947 quick_widgets
[3].text
= "";
949 Quick_input
.widgets
= quick_widgets
;
950 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
951 *result
= *(quick_widgets
[3].str_result
);
957 static void add_new_group_cmd (void)
962 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
963 if (!ret
|| !label
|| !*label
)
966 if (ret
== B_ENTER
|| ret
== B_APPEND
)
967 add2hotlist (label
, 0, HL_TYPE_GROUP
, 2);
969 add2hotlist (label
, 0, HL_TYPE_GROUP
, 1);
971 hotlist_state
.modified
= 1;
974 void add2hotlist_cmd (void)
976 char *prompt
, *label
;
977 char *cp
= _("Label for \"%s\":");
980 prompt
= g_strdup_printf (cp
, name_trunc (cpanel
->cwd
, COLS
-2*UX
-(l
+8)));
981 label
= input_dialog (_(" Add to hotlist "), prompt
, cpanel
->cwd
);
983 if (!label
|| !*label
)
986 add2hotlist (label
,g_strdup (cpanel
->cwd
), HL_TYPE_ENTRY
, 0);
987 hotlist_state
.modified
= 1;
990 static void remove_group (struct hotlist
*grp
)
992 struct hotlist
*current
= grp
->head
;
995 struct hotlist
*next
= current
->next
;
997 if (current
->type
== HL_TYPE_GROUP
)
998 remove_group (current
);
1001 g_free (current
->label
);
1002 if (current
->directory
)
1003 g_free (current
->directory
);
1011 static void remove_from_hotlist (struct hotlist
*entry
)
1013 if (entry
->type
== HL_TYPE_GROUP
) {
1018 header
= g_strconcat (_(" Remove: "),
1019 name_trunc (entry
->label
, 30),
1022 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1024 _("&No"), _("&Yes"));
1031 remove_group (entry
);
1034 unlink_entry (entry
);
1037 g_free (entry
->label
);
1038 if (entry
->directory
)
1039 g_free (entry
->directory
);
1041 /* now remove list entry from screen */
1042 listbox_remove_current (l_hotlist
, 1);
1043 hotlist_state
.modified
= 1;
1046 char *hotlist_cmd (int vfs_or_hotlist
)
1048 char *target
= NULL
;
1050 hotlist_state
.type
= vfs_or_hotlist
;
1053 init_hotlist (vfs_or_hotlist
);
1055 /* display file info */
1056 attrset (SELECTED_COLOR
);
1058 hotlist_state
.running
= 1;
1059 run_dlg (hotlist_dlg
);
1060 hotlist_state
.running
= 0;
1063 switch (hotlist_dlg
->ret_value
) {
1068 if (l_hotlist
->current
->data
) {
1069 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1070 target
= g_strdup (hlp
->directory
);
1072 target
= g_strdup (l_hotlist
->current
->text
);
1081 load_group (struct hotlist
*grp
)
1085 char *group_section
;
1086 struct hotlist
*current
= 0;
1088 group_section
= find_group_section (grp
);
1090 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1092 current_group
= grp
;
1094 while (profile_keys
){
1095 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1096 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1098 g_free (group_section
);
1100 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1102 while (profile_keys
){
1103 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1104 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1107 for (current
= grp
->head
; current
; current
= current
->next
)
1108 load_group (current
);
1113 #define TKN_STRING 2
1115 #define TKN_ENDGROUP 4
1116 #define TKN_COMMENT 5
1119 #define TKN_UNKNOWN 127
1121 static char *tkn_buf
;
1122 static int tkn_buf_length
;
1123 static int tkn_length
;
1125 static char *hotlist_file_name
;
1126 static FILE *hotlist_file
;
1127 static time_t hotlist_file_mtime
;
1129 static int hot_skip_blanks (void)
1133 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1139 static int hot_next_token (void)
1143 #define CHECK_BUF() \
1145 if (tkn_length == tkn_buf_length) \
1146 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1147 : ( g_malloc (tkn_buf_length = 1024)); \
1153 c
= hot_skip_blanks ();
1162 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1167 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1171 tkn_buf
[tkn_length
] = '\0';
1175 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1177 if ((c
= getc (hotlist_file
)) == EOF
)
1180 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1185 tkn_buf
[tkn_length
] = '\0';
1189 if ((c
= getc (hotlist_file
)) == EOF
)
1194 /* fall through; it is taken as normal character */
1199 tkn_buf
[tkn_length
++] = toupper(c
);
1200 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1202 ungetc (c
, hotlist_file
);
1204 tkn_buf
[tkn_length
] = '\0';
1205 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1207 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1209 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1210 return TKN_ENDGROUP
;
1211 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1219 #define SKIP_TO_EOL { \
1221 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1224 #define CHECK_TOKEN(_TKN_) \
1225 if ((tkn = hot_next_token ()) != _TKN_) { \
1226 hotlist_state.readonly = 1; \
1227 hotlist_state.file_error = 1; \
1228 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1229 tkn = hot_next_token (); \
1234 hot_load_group (struct hotlist
* grp
)
1237 struct hotlist
*new_grp
;
1240 current_group
= grp
;
1242 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1245 CHECK_TOKEN(TKN_STRING
);
1246 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1248 hot_load_group (new_grp
);
1249 current_group
= grp
;
1252 CHECK_TOKEN(TKN_STRING
);
1253 label
= g_strdup (tkn_buf
);
1254 CHECK_TOKEN(TKN_URL
);
1255 CHECK_TOKEN(TKN_STRING
);
1256 url
= g_strdup (tkn_buf
);
1257 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1261 label
= g_strdup (tkn_buf
);
1262 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1265 hotlist_state
.readonly
= 1;
1266 hotlist_state
.file_error
= 1;
1270 /* skip empty lines */
1273 hotlist_state
.readonly
= 1;
1274 hotlist_state
.file_error
= 1;
1282 hot_load_file (struct hotlist
* grp
)
1285 struct hotlist
*new_grp
;
1288 current_group
= grp
;
1290 while ((tkn
= hot_next_token())!= TKN_EOF
)
1293 CHECK_TOKEN(TKN_STRING
);
1294 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1296 hot_load_group (new_grp
);
1297 current_group
= grp
;
1300 CHECK_TOKEN(TKN_STRING
);
1301 label
= g_strdup (tkn_buf
);
1302 CHECK_TOKEN(TKN_URL
);
1303 CHECK_TOKEN(TKN_STRING
);
1304 url
= g_strdup (tkn_buf
);
1305 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1309 label
= g_strdup (tkn_buf
);
1310 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1313 /* skip empty lines */
1316 hotlist_state
.readonly
= 1;
1317 hotlist_state
.file_error
= 1;
1324 clean_up_hotlist_groups (char *section
)
1330 grp_section
= g_strconcat (section
, ".Group", NULL
);
1331 if (profile_has_section (section
, profile_name
))
1332 profile_clean_section (section
, profile_name
);
1333 if (profile_has_section (grp_section
, profile_name
)) {
1334 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1336 while (profile_keys
) {
1337 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1338 clean_up_hotlist_groups (key
);
1340 profile_clean_section (grp_section
, profile_name
);
1342 g_free (grp_section
);
1347 void load_hotlist (void)
1349 int remove_old_list
= 0;
1350 struct stat stat_buf
;
1352 if (hotlist_state
.loaded
) {
1353 stat (hotlist_file_name
, &stat_buf
);
1354 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1360 if (!hotlist_file_name
)
1361 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1363 hotlist
= new_hotlist ();
1364 hotlist
->type
= HL_TYPE_GROUP
;
1365 hotlist
->label
= g_strdup (_(" Top level group "));
1366 hotlist
->up
= hotlist
;
1370 hotlist
->directory
= g_strdup ("Hotlist");
1372 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1375 load_group (hotlist
);
1376 hotlist_state
.loaded
= 1;
1378 * just to be sure we got copy
1380 hotlist_state
.modified
= 1;
1381 result
= save_hotlist ();
1382 hotlist_state
.modified
= 0;
1384 remove_old_list
= 1;
1388 msg
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1389 _(" file, your old hotlist entries were not deleted"), NULL
);
1391 message (D_ERROR
, _(" Hotlist Load "), msg
);
1395 hot_load_file (hotlist
);
1396 fclose (hotlist_file
);
1397 hotlist_state
.loaded
= 1;
1400 if (remove_old_list
) {
1401 clean_up_hotlist_groups ("Hotlist");
1405 stat (hotlist_file_name
, &stat_buf
);
1406 hotlist_file_mtime
= stat_buf
.st_mtime
;
1407 current_group
= hotlist
;
1411 save_group (struct hotlist
*grp
)
1413 struct hotlist
*current
= grp
->head
;
1414 char *group_section
;
1416 group_section
= find_group_section (grp
);
1418 profile_clean_section (group_section
, profile_name
);
1419 for (;current
&& current
->type
== HL_TYPE_GROUP
; current
= current
->next
){
1420 WritePrivateProfileString (group_section
,
1425 g_free (group_section
);
1427 for (current
= grp
->head
;
1428 current
&& current
->type
== HL_TYPE_GROUP
;
1429 current
= current
->next
)
1430 save_group (current
);
1432 profile_clean_section (grp
->directory
, profile_name
);
1433 for (;current
; current
= current
->next
){
1434 WritePrivateProfileString (grp
->directory
,
1441 static int list_level
= 0;
1444 hot_save_group (struct hotlist
*grp
)
1446 struct hotlist
*current
= grp
->head
;
1452 for (i = 0; i < n; i++) \
1453 putc (' ', hotlist_file); \
1456 for (;current
; current
= current
->next
)
1457 switch (current
->type
) {
1459 INDENT (list_level
);
1460 fputs ("GROUP \"", hotlist_file
);
1461 for (s
= current
->label
; *s
; s
++) {
1463 putc ('\\', hotlist_file
);
1464 else if (*s
== '\\')
1465 putc ('\\', hotlist_file
);
1466 putc (*s
, hotlist_file
);
1468 fputs ("\"\n", hotlist_file
);
1470 hot_save_group (current
);
1472 INDENT (list_level
);
1473 fputs ("ENDGROUP\n", hotlist_file
);
1477 fputs ("ENTRY \"", hotlist_file
);
1478 for (s
= current
->label
; *s
; s
++) {
1480 putc ('\\', hotlist_file
);
1481 else if (*s
== '\\')
1482 putc ('\\', hotlist_file
);
1483 putc (*s
, hotlist_file
);
1485 fputs ("\" URL \"", hotlist_file
);
1486 for (s
= current
->directory
; *s
; s
++) {
1488 putc ('\\', hotlist_file
);
1489 else if (*s
== '\\')
1490 putc ('\\', hotlist_file
);
1491 putc (*s
, hotlist_file
);
1493 fputs ("\"\n", hotlist_file
);
1495 case HL_TYPE_COMMENT
:
1496 fprintf (hotlist_file
, "#%s\n", current
->label
);
1502 int save_hotlist (void)
1505 struct stat stat_buf
;
1507 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1508 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", NULL
);
1510 rename (hotlist_file_name
, fbak
);
1511 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1512 if (stat (fbak
, &stat_buf
) == 0)
1513 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1515 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1516 hot_save_group (hotlist
);
1517 fclose (hotlist_file
);
1518 stat (hotlist_file_name
, &stat_buf
);
1519 hotlist_file_mtime
= stat_buf
.st_mtime
;
1521 hotlist_state
.modified
= 0;
1523 rename (fbak
, hotlist_file_name
);
1531 * Unload list from memory.
1532 * Don't confuse with hotlist_done() for GUI.
1534 void done_hotlist (void)
1537 remove_group (hotlist
);
1539 g_free (hotlist
->label
);
1540 if (hotlist
->directory
)
1541 g_free (hotlist
->directory
);
1546 hotlist_state
.loaded
= 0;
1548 if (hotlist_file_name
){
1549 g_free (hotlist_file_name
);
1550 hotlist_file_name
= 0;