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 "setup.h" /* For profile_bname */
45 #include "profile.h" /* Load/save directories hotlist */
46 #include "wtools.h" /* QuickDialog */
47 #include "panel.h" /* current_panel */
48 #include "main.h" /* repaint_screen */
50 #include "key.h" /* KEY_M_CTRL */
51 #include "command.h" /* cmdline */
52 #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
58 #define BY (LINES - 6)
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)
72 #include "../vfs/gc.h"
73 #define B_FREE_ALL_VFS (B_USER + 8)
74 #define B_REFRESH_VFS (B_USER + 9)
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
;
116 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), LIST_HOTLIST
},
117 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), LIST_HOTLIST
},
118 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), LIST_MOVELIST
},
119 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), LIST_MOVELIST
},
120 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), LIST_HOTLIST
},
121 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), LIST_HOTLIST
},
122 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
123 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), LIST_HOTLIST
|LIST_MOVELIST
},
124 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"), LIST_HOTLIST
},
126 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"), LIST_VFSLIST
},
127 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"), LIST_VFSLIST
},
129 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
132 /* Directory hotlist */
133 static struct hotlist
{
134 enum HotListType type
;
137 struct hotlist
*head
;
139 struct hotlist
*next
;
142 static struct hotlist
*current_group
;
144 static void init_movelist (int, struct hotlist
*);
145 static void add_new_group_cmd (void);
146 static void add_new_entry_cmd (void);
147 static void remove_from_hotlist (struct hotlist
*entry
);
148 static void load_hotlist (void);
150 #define new_hotlist() g_new0(struct hotlist, 1)
153 hotlist_refresh (Dlg_head
* dlg
)
155 common_dialog_repaint (dlg
);
156 attrset (COLOR_NORMAL
);
157 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
158 dlg
->cols
- (UX
* 2));
159 if (!hotlist_state
.moving
)
160 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
163 /* If current->data is 0, then we are dealing with a VFS pathname */
165 update_path_name (void)
169 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
170 Dlg_head
*dlg
= list
->widget
.parent
;
173 if (list
->current
->data
!= 0) {
174 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
176 if (hlp
->type
== HL_TYPE_ENTRY
)
177 text
= hlp
->directory
;
179 text
= _("Subgroup - press ENTER to see list");
181 text
= list
->current
->text
;
186 if (!hotlist_state
.moving
)
187 label_set_text (pname
,
188 name_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
190 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
191 if (!hotlist_state
.moving
)
192 label_set_text (pname_group
,
193 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
195 label_set_text (movelist_group
,
196 name_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
202 #define CHECK_BUFFER \
206 if ((i = strlen (current->label) + 3) > buflen) { \
208 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
213 static void fill_listbox (void)
215 struct hotlist
*current
= current_group
->head
;
220 buf
= g_malloc (buflen
= 1024);
224 switch (current
->type
) {
228 strcat (strcat (buf
, "->"), current
->label
);
229 if (hotlist_state
.moving
)
230 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
232 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
236 if (hotlist_state
.moving
)
237 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
239 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
244 current
= current
->next
;
251 unlink_entry (struct hotlist
*entry
)
253 struct hotlist
*current
= current_group
->head
;
255 if (current
== entry
)
256 current_group
->head
= entry
->next
;
258 while (current
&& current
->next
!= entry
)
259 current
= current
->next
;
261 current
->next
= entry
->next
;
268 static void add_name_to_list (const char *path
)
270 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
272 #endif /* !USE_VFS */
275 hotlist_button_callback (int action
)
280 struct hotlist
*saved
= current_group
;
281 struct hotlist
*item
;
282 struct hotlist
*moveto_item
= 0;
283 struct hotlist
*moveto_group
= 0;
286 if (!l_hotlist
->current
)
287 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
288 item
= l_hotlist
->current
->data
;
289 hotlist_state
.moving
= 1;
290 init_movelist (LIST_MOVELIST
, item
);
291 run_dlg (movelist_dlg
);
292 ret
= movelist_dlg
->ret_value
;
293 hotlist_state
.moving
= 0;
294 if (l_movelist
->current
)
295 moveto_item
= l_movelist
->current
->data
;
296 moveto_group
= current_group
;
297 destroy_dlg (movelist_dlg
);
298 current_group
= saved
;
300 return MSG_NOT_HANDLED
;
301 if (moveto_item
== item
)
302 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
303 it hardly changes anything ;) */
305 listbox_remove_current (l_hotlist
, 1);
306 item
->up
= moveto_group
;
307 if (!moveto_group
->head
)
308 moveto_group
->head
= item
;
309 else if (!moveto_item
) { /* we have group with just comments */
310 struct hotlist
*p
= moveto_group
->head
;
316 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
317 if (!moveto_item
->next
)
318 moveto_item
->next
= item
;
320 item
->next
= moveto_item
->next
;
321 moveto_item
->next
= item
;
322 } else if (moveto_group
->head
== moveto_item
) {
323 moveto_group
->head
= item
;
324 item
->next
= moveto_item
;
326 struct hotlist
*p
= moveto_group
->head
;
328 while (p
->next
!= moveto_item
)
330 item
->next
= p
->next
;
333 listbox_remove_list (l_hotlist
);
336 hotlist_state
.modified
= 1;
337 return MSG_NOT_HANDLED
;
341 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
342 remove_from_hotlist (l_hotlist
->current
->data
);
343 return MSG_NOT_HANDLED
;
347 add_new_group_cmd ();
348 return MSG_NOT_HANDLED
;
353 return MSG_NOT_HANDLED
;
357 add_new_entry_cmd ();
358 return MSG_NOT_HANDLED
;
363 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
365 if (list
->current
->data
) {
366 struct hotlist
*hlp
=
367 (struct hotlist
*) list
->current
->data
;
368 if (hlp
->type
== HL_TYPE_ENTRY
)
371 listbox_remove_list (list
);
374 return MSG_NOT_HANDLED
;
380 /* Fall through if list empty - just go up */
384 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
385 listbox_remove_list (list
);
386 current_group
= current_group
->up
;
388 return MSG_NOT_HANDLED
;
398 listbox_remove_list (l_hotlist
);
399 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
400 vfs_fill_names (add_name_to_list
);
401 return MSG_NOT_HANDLED
;
412 hotlist_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
419 case DLG_UNHANDLED_KEY
:
421 case KEY_M_CTRL
| '\n':
426 if (hotlist_button_callback (B_ENTER
)) {
427 h
->ret_value
= B_ENTER
;
433 if (hotlist_state
.type
!= LIST_VFSLIST
)
434 return !hotlist_button_callback (B_UP_GROUP
);
436 return MSG_NOT_HANDLED
;
439 if (!hotlist_state
.moving
) {
440 hotlist_button_callback (B_REMOVE
);
447 if (!hotlist_state
.moving
) {
448 if (l_hotlist
->current
) {
449 if (l_hotlist
->current
->data
) {
450 struct hotlist
*hlp
=
451 (struct hotlist
*) l_hotlist
->current
->data
;
452 if (hlp
->type
== HL_TYPE_ENTRY
) {
454 g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
455 stuff (cmdline
, tmp
, 0);
458 h
->ret_value
= B_CANCEL
;
464 return MSG_HANDLED
; /* ignore key */
466 return MSG_NOT_HANDLED
;
469 if (hotlist_state
.moving
)
470 dlg_select_widget (movelist_dlg
, l_movelist
);
472 dlg_select_widget (hotlist_dlg
, l_hotlist
);
473 /* always stay on hotlist */
477 attrset (MENU_ENTRY_COLOR
);
482 return default_dlg_callback (h
, msg
, parm
);
486 static int l_call (WListbox
*list
)
488 Dlg_head
*dlg
= list
->widget
.parent
;
491 if (list
->current
->data
) {
492 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
493 if (hlp
->type
== HL_TYPE_ENTRY
) {
494 dlg
->ret_value
= B_ENTER
;
498 hotlist_button_callback (B_ENTER
);
499 hotlist_callback (dlg
, DLG_POST_KEY
, '\n');
503 dlg
->ret_value
= B_ENTER
;
509 hotlist_button_callback (B_UP_GROUP
);
510 hotlist_callback (dlg
, DLG_POST_KEY
, 'u');
515 * Expands all button names (once) and recalculates button positions.
516 * returns number of columns in the dialog box, which is 10 chars longer
519 * If common width of the window (i.e. in xterm) is less than returned
520 * width - sorry :) (anyway this did not handled in previous version too)
523 init_i18n_stuff(int list_type
, int cols
)
526 static const char* cancel_but
= N_("&Cancel");
529 static int hotlist_i18n_flag
= 0;
531 if (!hotlist_i18n_flag
)
533 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
535 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
537 cancel_but
= _(cancel_but
);
538 hotlist_i18n_flag
= 1;
540 #endif /* ENABLE_NLS */
542 /* Dynamic resizing of buttonbars */
544 int len
[2], count
[2]; /* at most two lines of buttons */
547 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
548 len
[0] = len
[1] = count
[0] = count
[1] = 0;
550 /* Count len of buttonbars, assuming 2 extra space between buttons */
553 if (! (hotlist_but
[i
].type
& list_type
))
556 row
= hotlist_but
[i
].y
;
558 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
559 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
565 cols
= max(cols
, max(len
[0], len
[1]));
567 /* arrange buttons */
569 cur_x
[0] = cur_x
[1] = 0;
570 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
573 if (! (hotlist_but
[i
].type
& list_type
))
576 row
= hotlist_but
[i
].y
;
578 if (hotlist_but
[i
].x
!= 0)
580 /* not first int the row */
581 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
583 cols
- strlen (hotlist_but
[i
].text
) - 13;
585 hotlist_but
[i
].x
= cur_x
[row
];
588 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
589 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
597 init_hotlist (int list_type
)
600 const char *title
, *help_node
;
603 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
607 hotlist_state
.expanded
=
608 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
611 if (list_type
== LIST_VFSLIST
) {
612 title
= _("Active VFS directories");
613 help_node
= "[vfshot]"; /* FIXME - no such node */
615 title
= _("Directory hotlist");
616 help_node
= "[Hotlist]";
620 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
621 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
623 for (i
= 0; i
< BUTTONS
; i
++) {
624 if (hotlist_but
[i
].type
& list_type
)
625 add_widget (hotlist_dlg
,
626 button_new (BY
+ hotlist_but
[i
].y
,
627 BX
+ hotlist_but
[i
].x
,
628 hotlist_but
[i
].ret_cmd
,
629 hotlist_but
[i
].flags
,
631 hotlist_button_callback
));
634 /* We add the labels.
635 * pname will hold entry's pathname;
636 * pname_group will hold name of current group
638 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
639 add_widget (hotlist_dlg
, pname
);
640 if (!hotlist_state
.moving
) {
641 add_widget (hotlist_dlg
,
642 label_new (UY
- 12 + LINES
, UX
+ 1,
643 _(" Directory path ")));
645 /* This one holds the displayed pathname */
646 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
647 add_widget (hotlist_dlg
, pname_group
);
649 /* get new listbox */
651 listbox_new (UY
+ 1, UX
+ 1, COLS
- 2 * UX
- 8, LINES
- 14,
654 /* Fill the hotlist with the active VFS or the hotlist */
656 if (list_type
== LIST_VFSLIST
) {
657 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
658 vfs_fill_names (add_name_to_list
);
660 #endif /* !USE_VFS */
663 add_widget (hotlist_dlg
, l_hotlist
);
664 /* add listbox to the dialogs */
668 init_movelist (int list_type
, struct hotlist
*item
)
671 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
672 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
677 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
678 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
681 for (i
= 0; i
< BUTTONS
; i
++) {
682 if (hotlist_but
[i
].type
& list_type
)
683 add_widget (movelist_dlg
,
684 button_new (BY
- 4 + hotlist_but
[i
].y
,
685 BX
+ hotlist_but
[i
].x
,
686 hotlist_but
[i
].ret_cmd
,
687 hotlist_but
[i
].flags
,
689 hotlist_button_callback
));
692 /* We add the labels. We are interested in the last one,
693 * that one will hold the path name label
695 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
696 add_widget (movelist_dlg
, movelist_group
);
697 /* get new listbox */
699 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->cols
- 2 * UX
- 2,
700 movelist_dlg
->lines
- 8, l_call
);
704 add_widget (movelist_dlg
, l_movelist
);
705 /* add listbox to the dialogs */
709 * Destroy the list dialog.
710 * Don't confuse with done_hotlist() for the list in memory.
712 static void hotlist_done (void)
714 destroy_dlg (hotlist_dlg
);
717 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
722 find_group_section (struct hotlist
*grp
)
724 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
728 /* 1.11.96 bor: added pos parameter to control placement of new item.
729 see widget.c, listbox_add_item()
730 now hotlist is in unsorted mode
733 HL_BEFORE_CURRENT
= 1
734 ,HL_AFTER_CURRENT
= 2
737 static struct hotlist
*
738 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
741 struct hotlist
*current
= NULL
;
744 * Hotlist is neither loaded nor loading.
745 * Must be called by "Ctrl-x a" before using hotlist.
750 if (l_hotlist
&& l_hotlist
->current
)
751 current
= l_hotlist
->current
->data
;
753 new = new_hotlist ();
757 new->directory
= directory
;
758 new->up
= current_group
;
760 if (!current_group
->head
) { /* first element in group */
761 current_group
->head
= new;
762 } else if (pos
== HL_AFTER_CURRENT
) {
763 new->next
= current
->next
;
765 } else if (pos
== HL_BEFORE_CURRENT
&&
766 current
== current_group
->head
) {
767 /* should be inserted before first item */
769 current_group
->head
= new;
770 } else if (pos
== HL_BEFORE_CURRENT
) {
771 struct hotlist
*p
= current_group
->head
;
773 while (p
->next
!= current
)
778 } else { /* append at the end */
779 struct hotlist
*p
= current_group
->head
;
787 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
) {
788 if (type
== HL_TYPE_GROUP
) {
789 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
791 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
794 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
795 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
803 * Support routine for add_new_entry_input()/add_new_group_input()
804 * Change positions of buttons (first three widgets).
806 * This is just a quick hack. Accurate procedure must take care of
807 * internationalized label lengths and total buttonbar length...assume
808 * 64 is longer anyway.
810 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
812 int i
, l
[3], space
, cur_x
;
814 for (i
= 0; i
< 3; i
++)
816 qw
[i
].text
= _(qw
[i
].text
);
817 l
[i
] = strlen (qw
[i
].text
) + 3;
819 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
821 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
823 qw
[i
].relative_x
= cur_x
;
824 qw
[i
].x_divisions
= len
;
827 #endif /* ENABLE_NLS */
830 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
831 const char *help
, char **r1
, char **r2
)
833 #define RELATIVE_Y_BUTTONS 4
834 #define RELATIVE_Y_LABEL_PTH 3
835 #define RELATIVE_Y_INPUT_PTH 4
837 QuickDialog Quick_input
;
838 static QuickWidget quick_widgets
[] = {
839 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
840 0, 0, "button-cancel" },
841 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
842 0, 0, "button-insert" },
843 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
844 0, 0, "button-append" },
845 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
847 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
849 { quick_input
, 4, 80, 3, 0, "", 58, 0,
851 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
860 static int i18n_flag
= 0;
861 #endif /* ENABLE_NLS */
863 len
= max (strlen (header
), (size_t) msglen (text1
, &lines1
));
864 len
= max (len
, (size_t) msglen (text2
, &lines2
)) + 4;
870 add_widgets_i18n(quick_widgets
, len
);
873 #endif /* ENABLE_NLS */
875 Quick_input
.xlen
= len
;
876 Quick_input
.xpos
= -1;
877 Quick_input
.title
= header
;
878 Quick_input
.help
= help
;
879 Quick_input
.i18n
= 0;
880 quick_widgets
[6].text
= text1
;
881 quick_widgets
[4].text
= text2
;
882 quick_widgets
[5].text
= *r1
;
883 quick_widgets
[3].text
= *r2
;
885 for (i
= 0; i
< 7; i
++)
886 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
887 Quick_input
.ylen
= lines1
+ lines2
+ 7;
889 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
890 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
891 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
892 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
893 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
895 quick_widgets
[5].str_result
= r1
;
896 quick_widgets
[3].str_result
= r2
;
898 Quick_input
.widgets
= quick_widgets
;
899 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
905 static void add_new_entry_cmd (void)
907 char *title
, *url
, *to_free
;
910 /* Take current directory as default value for input fields */
911 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
913 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"),
914 _("Directory path"), "[Hotlist]", &title
, &url
);
919 if (!title
|| !*title
|| !url
|| !*url
) {
925 if (ret
== B_ENTER
|| ret
== B_APPEND
)
926 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_AFTER_CURRENT
);
928 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_BEFORE_CURRENT
);
930 hotlist_state
.modified
= 1;
934 add_new_group_input (const char *header
, const char *label
, char **result
)
937 QuickDialog Quick_input
;
938 static QuickWidget quick_widgets
[] = {
939 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
941 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
943 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
945 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
946 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
948 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
949 quick_widgets variable above */
955 static int i18n_flag
= 0;
956 #endif /* ENABLE_NLS */
958 len
= max (strlen (header
), (size_t) msglen (label
, &lines
)) + 4;
964 add_widgets_i18n(quick_widgets
, len
);
967 #endif /* ENABLE_NLS */
969 Quick_input
.xlen
= len
;
970 Quick_input
.xpos
= -1;
971 Quick_input
.title
= header
;
972 Quick_input
.help
= "[Hotlist]";
973 Quick_input
.i18n
= 0;
974 quick_widgets
[4].text
= label
;
976 for (i
= 0; i
< 5; i
++)
977 quick_widgets
[i
].y_divisions
= lines
+6;
978 Quick_input
.ylen
= lines
+ 6;
980 for (i
= 0; i
< 4; i
++)
981 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
983 quick_widgets
[3].str_result
= result
;
984 quick_widgets
[3].text
= "";
986 Quick_input
.widgets
= quick_widgets
;
987 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
993 static void add_new_group_cmd (void)
998 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
999 if (!ret
|| !label
|| !*label
)
1002 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1003 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_AFTER_CURRENT
);
1005 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_BEFORE_CURRENT
);
1007 hotlist_state
.modified
= 1;
1010 void add2hotlist_cmd (void)
1012 char *prompt
, *label
;
1013 const char *cp
= _("Label for \"%s\":");
1014 int l
= strlen (cp
);
1015 char *label_string
= g_strdup (current_panel
->cwd
);
1017 strip_password (label_string
, 1);
1019 prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
-2*UX
-(l
+8)));
1020 label
= input_dialog (_(" Add to hotlist "), prompt
, label_string
);
1023 if (!label
|| !*label
) {
1024 g_free (label_string
);
1028 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, 0);
1029 hotlist_state
.modified
= 1;
1032 static void remove_group (struct hotlist
*grp
)
1034 struct hotlist
*current
= grp
->head
;
1037 struct hotlist
*next
= current
->next
;
1039 if (current
->type
== HL_TYPE_GROUP
)
1040 remove_group (current
);
1042 g_free (current
->label
);
1043 g_free (current
->directory
);
1051 static void remove_from_hotlist (struct hotlist
*entry
)
1053 if (entry
->type
== HL_TYPE_GROUP
) {
1058 header
= g_strconcat (_(" Remove: "),
1059 name_trunc (entry
->label
, 30),
1062 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1064 _("&No"), _("&Yes"));
1071 remove_group (entry
);
1074 unlink_entry (entry
);
1076 g_free (entry
->label
);
1077 g_free (entry
->directory
);
1079 /* now remove list entry from screen */
1080 listbox_remove_current (l_hotlist
, 1);
1081 hotlist_state
.modified
= 1;
1084 char *hotlist_cmd (int vfs_or_hotlist
)
1086 char *target
= NULL
;
1088 hotlist_state
.type
= vfs_or_hotlist
;
1091 init_hotlist (vfs_or_hotlist
);
1093 /* display file info */
1094 attrset (SELECTED_COLOR
);
1096 hotlist_state
.running
= 1;
1097 run_dlg (hotlist_dlg
);
1098 hotlist_state
.running
= 0;
1101 switch (hotlist_dlg
->ret_value
) {
1106 if (l_hotlist
->current
->data
) {
1107 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1108 target
= g_strdup (hlp
->directory
);
1110 target
= g_strdup (l_hotlist
->current
->text
);
1119 load_group (struct hotlist
*grp
)
1123 char *group_section
;
1124 struct hotlist
*current
= 0;
1126 group_section
= find_group_section (grp
);
1128 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1130 current_group
= grp
;
1132 while (profile_keys
){
1133 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1134 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1136 g_free (group_section
);
1138 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1140 while (profile_keys
){
1141 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1142 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1145 for (current
= grp
->head
; current
; current
= current
->next
)
1146 load_group (current
);
1151 #define TKN_STRING 2
1153 #define TKN_ENDGROUP 4
1154 #define TKN_COMMENT 5
1157 #define TKN_UNKNOWN 127
1159 static char *tkn_buf
;
1160 static int tkn_buf_length
;
1161 static int tkn_length
;
1163 static char *hotlist_file_name
;
1164 static FILE *hotlist_file
;
1165 static time_t hotlist_file_mtime
;
1167 static int hot_skip_blanks (void)
1171 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1177 static int hot_next_token (void)
1181 #define CHECK_BUF() \
1183 if (tkn_length == tkn_buf_length) \
1184 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1185 : ( g_malloc (tkn_buf_length = 1024)); \
1191 c
= hot_skip_blanks ();
1200 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1205 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1209 tkn_buf
[tkn_length
] = '\0';
1213 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1215 if ((c
= getc (hotlist_file
)) == EOF
)
1218 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1223 tkn_buf
[tkn_length
] = '\0';
1227 if ((c
= getc (hotlist_file
)) == EOF
)
1232 /* fall through; it is taken as normal character */
1237 tkn_buf
[tkn_length
++] = toupper(c
);
1238 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1240 ungetc (c
, hotlist_file
);
1242 tkn_buf
[tkn_length
] = '\0';
1243 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1245 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1247 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1248 return TKN_ENDGROUP
;
1249 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1257 #define SKIP_TO_EOL { \
1259 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1262 #define CHECK_TOKEN(_TKN_) \
1263 if ((tkn = hot_next_token ()) != _TKN_) { \
1264 hotlist_state.readonly = 1; \
1265 hotlist_state.file_error = 1; \
1266 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1267 tkn = hot_next_token (); \
1272 hot_load_group (struct hotlist
* grp
)
1275 struct hotlist
*new_grp
;
1278 current_group
= grp
;
1280 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1283 CHECK_TOKEN(TKN_STRING
);
1284 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1286 hot_load_group (new_grp
);
1287 current_group
= grp
;
1290 CHECK_TOKEN(TKN_STRING
);
1291 label
= g_strdup (tkn_buf
);
1292 CHECK_TOKEN(TKN_URL
);
1293 CHECK_TOKEN(TKN_STRING
);
1294 url
= g_strdup (tkn_buf
);
1295 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1299 label
= g_strdup (tkn_buf
);
1300 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1303 hotlist_state
.readonly
= 1;
1304 hotlist_state
.file_error
= 1;
1308 /* skip empty lines */
1311 hotlist_state
.readonly
= 1;
1312 hotlist_state
.file_error
= 1;
1320 hot_load_file (struct hotlist
* grp
)
1323 struct hotlist
*new_grp
;
1326 current_group
= grp
;
1328 while ((tkn
= hot_next_token())!= TKN_EOF
)
1331 CHECK_TOKEN(TKN_STRING
);
1332 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1334 hot_load_group (new_grp
);
1335 current_group
= grp
;
1338 CHECK_TOKEN(TKN_STRING
);
1339 label
= g_strdup (tkn_buf
);
1340 CHECK_TOKEN(TKN_URL
);
1341 CHECK_TOKEN(TKN_STRING
);
1342 url
= g_strdup (tkn_buf
);
1343 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1347 label
= g_strdup (tkn_buf
);
1348 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1351 /* skip empty lines */
1354 hotlist_state
.readonly
= 1;
1355 hotlist_state
.file_error
= 1;
1362 clean_up_hotlist_groups (const char *section
)
1368 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1369 if (profile_has_section (section
, profile_name
))
1370 profile_clean_section (section
, profile_name
);
1371 if (profile_has_section (grp_section
, profile_name
)) {
1372 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1374 while (profile_keys
) {
1375 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1376 clean_up_hotlist_groups (key
);
1378 profile_clean_section (grp_section
, profile_name
);
1380 g_free (grp_section
);
1388 int remove_old_list
= 0;
1389 struct stat stat_buf
;
1391 if (hotlist_state
.loaded
) {
1392 stat (hotlist_file_name
, &stat_buf
);
1393 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1399 if (!hotlist_file_name
)
1400 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1402 hotlist
= new_hotlist ();
1403 hotlist
->type
= HL_TYPE_GROUP
;
1404 hotlist
->label
= g_strdup (_(" Top level group "));
1405 hotlist
->up
= hotlist
;
1409 hotlist
->directory
= g_strdup ("Hotlist");
1411 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1414 load_group (hotlist
);
1415 hotlist_state
.loaded
= 1;
1417 * just to be sure we got copy
1419 hotlist_state
.modified
= 1;
1420 result
= save_hotlist ();
1421 hotlist_state
.modified
= 0;
1423 remove_old_list
= 1;
1427 msg
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1428 _(" file, your old hotlist entries were not deleted"), (char *) NULL
);
1430 message (D_ERROR
, _(" Hotlist Load "), msg
);
1434 hot_load_file (hotlist
);
1435 fclose (hotlist_file
);
1436 hotlist_state
.loaded
= 1;
1439 if (remove_old_list
) {
1440 clean_up_hotlist_groups ("Hotlist");
1444 stat (hotlist_file_name
, &stat_buf
);
1445 hotlist_file_mtime
= stat_buf
.st_mtime
;
1446 current_group
= hotlist
;
1450 save_group (struct hotlist
*grp
)
1452 struct hotlist
*current
= grp
->head
;
1453 char *group_section
;
1455 group_section
= find_group_section (grp
);
1457 profile_clean_section (group_section
, profile_name
);
1458 for (;current
&& current
->type
== HL_TYPE_GROUP
; current
= current
->next
){
1459 WritePrivateProfileString (group_section
,
1464 g_free (group_section
);
1466 for (current
= grp
->head
;
1467 current
&& current
->type
== HL_TYPE_GROUP
;
1468 current
= current
->next
)
1469 save_group (current
);
1471 profile_clean_section (grp
->directory
, profile_name
);
1472 for (;current
; current
= current
->next
){
1473 WritePrivateProfileString (grp
->directory
,
1480 static int list_level
= 0;
1483 hot_save_group (struct hotlist
*grp
)
1485 struct hotlist
*current
= grp
->head
;
1491 for (i = 0; i < n; i++) \
1492 putc (' ', hotlist_file); \
1495 for (;current
; current
= current
->next
)
1496 switch (current
->type
) {
1498 INDENT (list_level
);
1499 fputs ("GROUP \"", hotlist_file
);
1500 for (s
= current
->label
; *s
; s
++) {
1501 if (*s
== '"' || *s
== '\\')
1502 putc ('\\', hotlist_file
);
1503 putc (*s
, hotlist_file
);
1505 fputs ("\"\n", hotlist_file
);
1507 hot_save_group (current
);
1509 INDENT (list_level
);
1510 fputs ("ENDGROUP\n", hotlist_file
);
1514 fputs ("ENTRY \"", hotlist_file
);
1515 for (s
= current
->label
; *s
; s
++) {
1516 if (*s
== '"' || *s
== '\\')
1517 putc ('\\', hotlist_file
);
1518 putc (*s
, hotlist_file
);
1520 fputs ("\" URL \"", hotlist_file
);
1521 for (s
= current
->directory
; *s
; s
++) {
1522 if (*s
== '"' || *s
== '\\')
1523 putc ('\\', hotlist_file
);
1524 putc (*s
, hotlist_file
);
1526 fputs ("\"\n", hotlist_file
);
1528 case HL_TYPE_COMMENT
:
1529 fprintf (hotlist_file
, "#%s\n", current
->label
);
1535 int save_hotlist (void)
1538 struct stat stat_buf
;
1540 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1541 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", (char *) NULL
);
1543 rename (hotlist_file_name
, fbak
);
1544 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1545 if (stat (fbak
, &stat_buf
) == 0)
1546 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1548 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1549 hot_save_group (hotlist
);
1550 fclose (hotlist_file
);
1551 stat (hotlist_file_name
, &stat_buf
);
1552 hotlist_file_mtime
= stat_buf
.st_mtime
;
1554 hotlist_state
.modified
= 0;
1556 rename (fbak
, hotlist_file_name
);
1564 * Unload list from memory.
1565 * Don't confuse with hotlist_done() for GUI.
1567 void done_hotlist (void)
1570 remove_group (hotlist
);
1571 g_free (hotlist
->label
);
1572 g_free (hotlist
->directory
);
1577 hotlist_state
.loaded
= 0;
1579 g_free (hotlist_file_name
);
1580 hotlist_file_name
= 0;