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)
148 static void hotlist_refresh (Dlg_head
*dlg
)
150 dialog_repaint (dlg
, COLOR_NORMAL
, COLOR_HOT_NORMAL
);
151 attrset (COLOR_NORMAL
);
153 dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
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);
571 static void init_hotlist (int list_type
)
574 int hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
578 hotlist_state
.expanded
= GetPrivateProfileInt ("HotlistConfig",
579 "expanded_view_of_groups", 0, profile_name
);
581 hotlist_dlg
= create_dlg (0, 0, LINES
-2, hotlist_cols
, dialog_colors
,
583 list_type
== LIST_VFSLIST
? "[vfshot]" : "[Hotlist]",
584 list_type
== LIST_VFSLIST
? "vfshot" : "hotlist",
586 x_set_dialog_title (hotlist_dlg
,
587 list_type
== LIST_VFSLIST
? _("Active VFS directories") : _("Directory hotlist"));
589 #define XTRACT(i) BY+hotlist_but[i].y, BX+hotlist_but[i].x, hotlist_but[i].ret_cmd, hotlist_but[i].flags, hotlist_but[i].text, hotlist_button_callback, 0, hotlist_but[i].tkname
591 for (i
= 0; i
< BUTTONS
; i
++){
592 if (hotlist_but
[i
].type
& list_type
)
593 add_widget (hotlist_dlg
, button_new (XTRACT (i
)));
597 /* We add the labels.
598 * pname will hold entry's pathname;
599 * pname_group will hold name of current group
601 pname
= label_new (UY
-11+LINES
, UX
+2, "", "the-lab");
602 add_widget (hotlist_dlg
, pname
);
603 if (!hotlist_state
.moving
) {
604 add_widget (hotlist_dlg
, label_new (UY
-12+LINES
, UX
+1, _(" Directory path "), NULL
));
606 /* This one holds the displayed pathname */
607 pname_group
= label_new (UY
, UX
+1, _(" Directory label "), NULL
);
608 add_widget (hotlist_dlg
, pname_group
);
610 /* get new listbox */
611 l_hotlist
= listbox_new (UY
+ 1, UX
+ 1, COLS
-2*UX
-8, LINES
-14, listbox_cback
, l_call
, "listbox");
613 /* Fill the hotlist with the active VFS or the hotlist */
615 if (list_type
== LIST_VFSLIST
){
616 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
617 vfs_fill_names (add_name_to_list
);
619 #endif /* !USE_VFS */
622 add_widget (hotlist_dlg
, l_hotlist
);
623 /* add listbox to the dialogs */
626 static void init_movelist (int list_type
, struct hotlist
*item
)
629 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
630 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
634 movelist_dlg
= create_dlg (0, 0, LINES
-6, movelist_cols
, dialog_colors
,
635 hotlist_callback
, "[Hotlist]",
638 x_set_dialog_title (movelist_dlg
, hdr
);
641 #define XTRACT(i) BY-4+hotlist_but[i].y, BX+hotlist_but[i].x, hotlist_but[i].ret_cmd, hotlist_but[i].flags, hotlist_but[i].text, hotlist_button_callback, 0, hotlist_but[i].tkname
643 for (i
= 0; i
< BUTTONS
; i
++){
644 if (hotlist_but
[i
].type
& list_type
)
645 add_widget (movelist_dlg
, button_new (XTRACT (i
)));
650 /* We add the labels. We are interested in the last one,
651 * that one will hold the path name label
653 movelist_group
= label_new (UY
, UX
+1, _(" Directory label "), NULL
);
654 add_widget (movelist_dlg
, movelist_group
);
655 /* get new listbox */
656 l_movelist
= listbox_new (UY
+ 1, UX
+ 1,
657 movelist_dlg
->cols
- 2*UX
- 2, movelist_dlg
->lines
- 8,
658 listbox_cback
, l_call
, "listbox");
662 add_widget (movelist_dlg
, l_movelist
);
663 /* add listbox to the dialogs */
667 * Destroy the list dialog.
668 * Don't confuse with done_hotlist() for the list in memory.
670 static void hotlist_done (void)
672 destroy_dlg (hotlist_dlg
);
675 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
680 find_group_section (struct hotlist
*grp
)
682 return g_strconcat (grp
->directory
, ".Group", NULL
);
687 /* 1.11.96 bor: added pos parameter to control placement of new item.
688 see widget.c, listbox_add_item()
689 now hotlist is in unsorted mode
691 static struct hotlist
*
692 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
695 struct hotlist
*current
= NULL
;
698 * Hotlist is neither loaded nor loading.
699 * Must be called by "Ctrl-x a" before using hotlist.
704 if (l_hotlist
&& l_hotlist
->current
)
705 current
= l_hotlist
->current
->data
;
707 new = new_hotlist ();
711 new->directory
= directory
;
712 new->up
= current_group
;
714 if (!current_group
->head
) { /* first element in group */
715 current_group
->head
= new;
716 } else if (pos
== 2) { /* should be appended after current*/
717 new->next
= current
->next
;
719 } else if (pos
== 1 &&
720 current
== current_group
->head
) {
721 /* should be inserted before first item */
723 current_group
->head
= new;
724 } else if (pos
== 1) { /* befor current */
725 struct hotlist
*p
= current_group
->head
;
727 while (p
->next
!= current
)
732 } else { /* append at the end */
733 struct hotlist
*p
= current_group
->head
;
741 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
) {
742 if (type
== HL_TYPE_GROUP
) {
743 char *lbl
= g_strconcat ("->", new->label
, NULL
);
745 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
748 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
749 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
757 * Support routine for add_new_entry_input()/add_new_group_input()
758 * Change positions of buttons (first three widgets).
760 * This is just a quick hack. Accurate procedure must take care of
761 * internationalized label lengths and total buttonbar length...assume
762 * 64 is longer anyway.
764 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
766 int i
, l
[3], space
, cur_x
;
768 for (i
= 0; i
< 3; i
++)
770 qw
[i
].text
= _(qw
[i
].text
);
771 l
[i
] = strlen (qw
[i
].text
) + 3;
773 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
775 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
777 qw
[i
].relative_x
= cur_x
;
778 qw
[i
].x_divisions
= len
;
781 #endif /* ENABLE_NLS */
783 static int add_new_entry_input (char *header
, char *text1
, char *text2
, char *help
, char **r1
, char **r2
)
785 #define RELATIVE_Y_BUTTONS 4
786 #define RELATIVE_Y_LABEL_PTH 3
787 #define RELATIVE_Y_INPUT_PTH 4
789 QuickDialog Quick_input
;
790 static QuickWidget quick_widgets
[] = {
791 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
792 0, 0, "button-cancel" },
793 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
794 0, 0, "button-insert" },
795 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
796 0, 0, "button-append" },
797 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
799 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
801 { quick_input
, 4, 80, 3, 0, "", 58, 0,
803 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
810 char *my_str1
, *my_str2
;
813 static int i18n_flag
= 0;
814 #endif /* ENABLE_NLS */
816 len
= max (strlen (header
), msglen (text1
, &lines1
));
817 len
= max (len
, msglen (text2
, &lines2
)) + 4;
823 add_widgets_i18n(quick_widgets
, len
);
826 #endif /* ENABLE_NLS */
828 Quick_input
.xlen
= len
;
829 Quick_input
.xpos
= -1;
830 Quick_input
.title
= header
;
831 Quick_input
.help
= help
;
832 Quick_input
.class = "hotlist_new_entry";
833 Quick_input
.i18n
= 0;
834 quick_widgets
[6].text
= text1
;
835 quick_widgets
[4].text
= text2
;
836 quick_widgets
[5].text
= *r1
;
837 quick_widgets
[3].text
= *r2
;
839 for (i
= 0; i
< 7; i
++)
840 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
841 Quick_input
.ylen
= lines1
+ lines2
+ 7;
843 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
844 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
845 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
846 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
847 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
849 quick_widgets
[5].str_result
= &my_str1
;
850 quick_widgets
[3].str_result
= &my_str2
;
852 Quick_input
.widgets
= quick_widgets
;
853 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
854 *r1
= *(quick_widgets
[5].str_result
);
855 *r2
= *(quick_widgets
[3].str_result
);
861 static void add_new_entry_cmd (void)
863 char *title
= 0, *url
= 0;
866 /* Take current directory as default value for input fields */
867 title
= url
= cpanel
->cwd
;
869 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
870 "[Hotlist]", &title
, &url
);
872 if (!ret
|| !title
|| !*title
|| !url
|| !*url
)
875 if (ret
== B_ENTER
|| ret
== B_APPEND
)
876 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 2);
878 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 1);
880 hotlist_state
.modified
= 1;
883 static int add_new_group_input (char *header
, char *label
, char **result
)
886 QuickDialog Quick_input
;
887 static QuickWidget quick_widgets
[] = {
888 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
890 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
892 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
894 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
895 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
897 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
898 quick_widgets variable above */
905 static int i18n_flag
= 0;
906 #endif /* ENABLE_NLS */
908 len
= max (strlen (header
), msglen (label
, &lines
)) + 4;
914 add_widgets_i18n(quick_widgets
, len
);
917 #endif /* ENABLE_NLS */
919 Quick_input
.xlen
= len
;
920 Quick_input
.xpos
= -1;
921 Quick_input
.title
= header
;
922 Quick_input
.help
= "[Hotlist]";
923 Quick_input
.class = "hotlist_new_group";
924 Quick_input
.i18n
= 0;
925 quick_widgets
[4].text
= label
;
927 for (i
= 0; i
< 5; i
++)
928 quick_widgets
[i
].y_divisions
= lines
+6;
929 Quick_input
.ylen
= lines
+ 6;
931 for (i
= 0; i
< 4; i
++)
932 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
934 quick_widgets
[3].str_result
= &my_str
;
935 quick_widgets
[3].text
= "";
937 Quick_input
.widgets
= quick_widgets
;
938 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
939 *result
= *(quick_widgets
[3].str_result
);
945 static void add_new_group_cmd (void)
950 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
951 if (!ret
|| !label
|| !*label
)
954 if (ret
== B_ENTER
|| ret
== B_APPEND
)
955 add2hotlist (label
, 0, HL_TYPE_GROUP
, 2);
957 add2hotlist (label
, 0, HL_TYPE_GROUP
, 1);
959 hotlist_state
.modified
= 1;
962 void add2hotlist_cmd (void)
964 char *prompt
, *label
;
965 char *cp
= _("Label for \"%s\":");
968 prompt
= g_strdup_printf (cp
, name_trunc (cpanel
->cwd
, COLS
-2*UX
-(l
+8)));
969 label
= input_dialog (_(" Add to hotlist "), prompt
, cpanel
->cwd
);
971 if (!label
|| !*label
)
974 add2hotlist (label
,g_strdup (cpanel
->cwd
), HL_TYPE_ENTRY
, 0);
975 hotlist_state
.modified
= 1;
978 static void remove_group (struct hotlist
*grp
)
980 struct hotlist
*current
= grp
->head
;
983 struct hotlist
*next
= current
->next
;
985 if (current
->type
== HL_TYPE_GROUP
)
986 remove_group (current
);
989 g_free (current
->label
);
990 if (current
->directory
)
991 g_free (current
->directory
);
999 static void remove_from_hotlist (struct hotlist
*entry
)
1001 if (entry
->type
== HL_TYPE_GROUP
) {
1006 header
= g_strconcat (_(" Remove: "),
1007 name_trunc (entry
->label
, 30),
1010 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1012 _("&No"), _("&Yes"));
1019 remove_group (entry
);
1022 unlink_entry (entry
);
1025 g_free (entry
->label
);
1026 if (entry
->directory
)
1027 g_free (entry
->directory
);
1029 /* now remove list entry from screen */
1030 listbox_remove_current (l_hotlist
, 1);
1031 hotlist_state
.modified
= 1;
1034 char *hotlist_cmd (int vfs_or_hotlist
)
1036 char *target
= NULL
;
1038 hotlist_state
.type
= vfs_or_hotlist
;
1041 init_hotlist (vfs_or_hotlist
);
1043 /* display file info */
1044 attrset (SELECTED_COLOR
);
1046 hotlist_state
.running
= 1;
1047 run_dlg (hotlist_dlg
);
1048 hotlist_state
.running
= 0;
1051 switch (hotlist_dlg
->ret_value
) {
1056 if (l_hotlist
->current
->data
) {
1057 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1058 target
= g_strdup (hlp
->directory
);
1060 target
= g_strdup (l_hotlist
->current
->text
);
1069 load_group (struct hotlist
*grp
)
1073 char *group_section
;
1074 struct hotlist
*current
= 0;
1076 group_section
= find_group_section (grp
);
1078 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1080 current_group
= grp
;
1082 while (profile_keys
){
1083 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1084 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1086 g_free (group_section
);
1088 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1090 while (profile_keys
){
1091 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1092 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1095 for (current
= grp
->head
; current
; current
= current
->next
)
1096 load_group (current
);
1101 #define TKN_STRING 2
1103 #define TKN_ENDGROUP 4
1104 #define TKN_COMMENT 5
1107 #define TKN_UNKNOWN 127
1109 static char *tkn_buf
;
1110 static int tkn_buf_length
;
1111 static int tkn_length
;
1113 static char *hotlist_file_name
;
1114 static FILE *hotlist_file
;
1115 static time_t hotlist_file_mtime
;
1117 static int hot_skip_blanks (void)
1121 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1127 static int hot_next_token (void)
1131 #define CHECK_BUF() \
1133 if (tkn_length == tkn_buf_length) \
1134 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1135 : ( g_malloc (tkn_buf_length = 1024)); \
1141 c
= hot_skip_blanks ();
1150 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1155 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1159 tkn_buf
[tkn_length
] = '\0';
1163 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1165 if ((c
= getc (hotlist_file
)) == EOF
)
1168 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1173 tkn_buf
[tkn_length
] = '\0';
1177 if ((c
= getc (hotlist_file
)) == EOF
)
1182 /* fall through; it is taken as normal character */
1187 tkn_buf
[tkn_length
++] = toupper(c
);
1188 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1190 ungetc (c
, hotlist_file
);
1192 tkn_buf
[tkn_length
] = '\0';
1193 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1195 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1197 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1198 return TKN_ENDGROUP
;
1199 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1207 #define SKIP_TO_EOL { \
1209 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1212 #define CHECK_TOKEN(_TKN_) \
1213 if ((tkn = hot_next_token ()) != _TKN_) { \
1214 hotlist_state.readonly = 1; \
1215 hotlist_state.file_error = 1; \
1216 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1217 tkn = hot_next_token (); \
1222 hot_load_group (struct hotlist
* grp
)
1225 struct hotlist
*new_grp
;
1228 current_group
= grp
;
1230 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1233 CHECK_TOKEN(TKN_STRING
);
1234 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1236 hot_load_group (new_grp
);
1237 current_group
= grp
;
1240 CHECK_TOKEN(TKN_STRING
);
1241 label
= g_strdup (tkn_buf
);
1242 CHECK_TOKEN(TKN_URL
);
1243 CHECK_TOKEN(TKN_STRING
);
1244 url
= g_strdup (tkn_buf
);
1245 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1249 label
= g_strdup (tkn_buf
);
1250 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1253 hotlist_state
.readonly
= 1;
1254 hotlist_state
.file_error
= 1;
1258 /* skip empty lines */
1261 hotlist_state
.readonly
= 1;
1262 hotlist_state
.file_error
= 1;
1270 hot_load_file (struct hotlist
* grp
)
1273 struct hotlist
*new_grp
;
1276 current_group
= grp
;
1278 while ((tkn
= hot_next_token())!= TKN_EOF
)
1281 CHECK_TOKEN(TKN_STRING
);
1282 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1284 hot_load_group (new_grp
);
1285 current_group
= grp
;
1288 CHECK_TOKEN(TKN_STRING
);
1289 label
= g_strdup (tkn_buf
);
1290 CHECK_TOKEN(TKN_URL
);
1291 CHECK_TOKEN(TKN_STRING
);
1292 url
= g_strdup (tkn_buf
);
1293 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1297 label
= g_strdup (tkn_buf
);
1298 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1301 /* skip empty lines */
1304 hotlist_state
.readonly
= 1;
1305 hotlist_state
.file_error
= 1;
1312 clean_up_hotlist_groups (char *section
)
1318 grp_section
= g_strconcat (section
, ".Group", NULL
);
1319 if (profile_has_section (section
, profile_name
))
1320 profile_clean_section (section
, profile_name
);
1321 if (profile_has_section (grp_section
, profile_name
)) {
1322 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1324 while (profile_keys
) {
1325 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1326 clean_up_hotlist_groups (key
);
1328 profile_clean_section (grp_section
, profile_name
);
1330 g_free (grp_section
);
1335 void load_hotlist (void)
1338 int has_old_list
= 0;
1339 int remove_old_list
= 0;
1340 struct stat stat_buf
;
1342 if (hotlist_state
.loaded
) {
1343 stat (hotlist_file_name
, &stat_buf
);
1344 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1350 if (!hotlist_file_name
)
1351 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1353 hotlist
= new_hotlist ();
1354 hotlist
->type
= HL_TYPE_GROUP
;
1355 hotlist
->label
= g_strdup (_(" Top level group "));
1356 hotlist
->up
= hotlist
;
1360 hotlist
->directory
= g_strdup ("Hotlist");
1362 grp_section
= g_strconcat ("Hotlist", ".Group", NULL
);
1363 has_old_list
= profile_has_section ("Hotlist", profile_name
) ||
1364 profile_has_section (grp_section
, profile_name
);
1365 g_free (grp_section
);
1367 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1370 load_group (hotlist
);
1371 hotlist_state
.loaded
= 1;
1373 * just to be sure we got copy
1375 hotlist_state
.modified
= 1;
1376 result
= save_hotlist ();
1377 hotlist_state
.modified
= 0;
1379 remove_old_list
= 1;
1383 msg
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1384 _(" file, your old hotlist entries were not deleted"), NULL
);
1386 message (D_ERROR
, _(" Hotlist Load "), msg
);
1390 hot_load_file (hotlist
);
1391 fclose (hotlist_file
);
1392 hotlist_state
.loaded
= 1;
1398 _("You have ~/"), HOTLIST_FILENAME
, _(" file and [Hotlist] section in ~/"), PROFILE_NAME
, "\n",
1399 _("Your ~/"), HOTLIST_FILENAME
, _(" most probably was created\n"),
1400 _("by an earlier development version of MC\nand is more actual than ~/"),
1401 PROFILE_NAME
, _(" entries\n\n"),
1402 _("You can choose between\n\n"
1403 " Remove - remove old hotlist entries from ~/"), PROFILE_NAME
, "\n",
1404 _(" Keep - keep your old entries; you will be asked\n"
1405 " the same question next time\n"
1406 " Merge - add old entries to hotlist as group \"Entries from ~/"),
1407 PROFILE_NAME
, "\"\n\n", NULL
);
1409 result
= query_dialog (_(" Hotlist Load "),
1410 msg
, D_ERROR
, 3, _("&Remove"), _("&Keep"), _("&Merge"));
1412 remove_old_list
= 1;
1413 else if (result
== 2) {
1414 struct hotlist
*grp
= hotlist
->head
;
1415 struct hotlist
*old
;
1418 load_group (hotlist
);
1420 old
= new_hotlist ();
1421 old
->type
= HL_TYPE_GROUP
;
1422 old
->label
= g_strconcat (_(" Entries from ~/"), PROFILE_NAME
, NULL
);
1424 old
->head
= hotlist
->head
;
1426 hotlist
->head
= old
;
1427 hotlist_state
.modified
= 1;
1428 if (!save_hotlist ()){
1431 str
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1432 _(" file your old hotlist entries were not deleted"), NULL
);
1434 message (D_ERROR
, _(" Hotlist Load "), str
);
1437 remove_old_list
= 1;
1438 hotlist_state
.modified
= 0;
1443 if (remove_old_list
) {
1444 clean_up_hotlist_groups ("Hotlist");
1448 stat (hotlist_file_name
, &stat_buf
);
1449 hotlist_file_mtime
= stat_buf
.st_mtime
;
1450 current_group
= hotlist
;
1454 save_group (struct hotlist
*grp
)
1456 struct hotlist
*current
= grp
->head
;
1457 char *group_section
;
1459 group_section
= find_group_section (grp
);
1461 profile_clean_section (group_section
, profile_name
);
1462 for (;current
&& current
->type
== HL_TYPE_GROUP
; current
= current
->next
){
1463 WritePrivateProfileString (group_section
,
1468 g_free (group_section
);
1470 for (current
= grp
->head
;
1471 current
&& current
->type
== HL_TYPE_GROUP
;
1472 current
= current
->next
)
1473 save_group (current
);
1475 profile_clean_section (grp
->directory
, profile_name
);
1476 for (;current
; current
= current
->next
){
1477 WritePrivateProfileString (grp
->directory
,
1484 static int list_level
= 0;
1487 hot_save_group (struct hotlist
*grp
)
1489 struct hotlist
*current
= grp
->head
;
1495 for (i = 0; i < n; i++) \
1496 putc (' ', hotlist_file); \
1499 for (;current
; current
= current
->next
)
1500 switch (current
->type
) {
1502 INDENT (list_level
);
1503 fputs ("GROUP \"", hotlist_file
);
1504 for (s
= current
->label
; *s
; s
++) {
1506 putc ('\\', hotlist_file
);
1507 else if (*s
== '\\')
1508 putc ('\\', hotlist_file
);
1509 putc (*s
, hotlist_file
);
1511 fputs ("\"\n", hotlist_file
);
1513 hot_save_group (current
);
1515 INDENT (list_level
);
1516 fputs ("ENDGROUP\n", hotlist_file
);
1520 fputs ("ENTRY \"", hotlist_file
);
1521 for (s
= current
->label
; *s
; s
++) {
1523 putc ('\\', hotlist_file
);
1524 else if (*s
== '\\')
1525 putc ('\\', hotlist_file
);
1526 putc (*s
, hotlist_file
);
1528 fputs ("\" URL \"", hotlist_file
);
1529 for (s
= current
->directory
; *s
; s
++) {
1531 putc ('\\', hotlist_file
);
1532 else if (*s
== '\\')
1533 putc ('\\', hotlist_file
);
1534 putc (*s
, hotlist_file
);
1536 fputs ("\"\n", hotlist_file
);
1538 case HL_TYPE_COMMENT
:
1539 fprintf (hotlist_file
, "#%s\n", current
->label
);
1545 int save_hotlist (void)
1548 struct stat stat_buf
;
1550 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1551 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", NULL
);
1553 rename (hotlist_file_name
, fbak
);
1554 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1555 if (stat (fbak
, &stat_buf
) == 0)
1556 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1558 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1559 hot_save_group (hotlist
);
1560 fclose (hotlist_file
);
1561 stat (hotlist_file_name
, &stat_buf
);
1562 hotlist_file_mtime
= stat_buf
.st_mtime
;
1564 hotlist_state
.modified
= 0;
1566 rename (fbak
, hotlist_file_name
);
1574 * Unload list from memory.
1575 * Don't confuse with hotlist_done() for GUI.
1577 void done_hotlist (void)
1580 remove_group (hotlist
);
1582 g_free (hotlist
->label
);
1583 if (hotlist
->directory
)
1584 g_free (hotlist
->directory
);
1589 hotlist_state
.loaded
= 0;
1591 if (hotlist_file_name
){
1592 g_free (hotlist_file_name
);
1593 hotlist_file_name
= 0;