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>
37 #include <sys/param.h>
40 # include <sys/timeb.h> /* alex: for struct timeb, used in time.h */
41 #endif /* SCO_FLAVOR */
43 #ifndef HAS_NO_GRP_PWD_H
56 #include "dialog.h" /* For do_refresh() */
57 #include "setup.h" /* For profile_bname */
58 #include "profile.h" /* Load/save directories hotlist */
60 #include "../vfs/vfs.h"
61 /* Needed for the extern declarations of integer parameters */
64 #include "panel.h" /* Needed for the externs */
77 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
79 #define B_ADD_CURRENT B_USER
80 #define B_REMOVE (B_USER + 1)
81 #define B_NEW_GROUP (B_USER + 2)
82 #define B_NEW_ENTRY (B_USER + 3)
83 #define B_UP_GROUP (B_USER + 4)
84 #define B_INSERT (B_USER + 5)
85 #define B_APPEND (B_USER + 6)
86 #define B_MOVE (B_USER + 7)
88 static WListbox
*l_hotlist
;
89 static WListbox
*l_movelist
;
91 static Dlg_head
*hotlist_dlg
;
92 static Dlg_head
*movelist_dlg
;
94 static WLabel
*pname
, *pname_group
, *movelist_group
;
104 * these parameters are intended to be user configurable
106 int expanded
; /* expanded view of all groups at startup */
109 * these reflect run time state
112 int loaded
; /* hotlist is loaded */
113 int readonly
; /* hotlist readonly */
114 int file_error
; /* parse error while reading file */
115 int running
; /* we are running dlg (and have to
117 int moving
; /* we are in moving hotlist currently */
118 int modified
; /* hotlist was modified */
119 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
122 struct _hotlist_but
{
123 int ret_cmd
, flags
, y
, x
;
128 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), "move", LIST_HOTLIST
},
129 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"), "r", LIST_HOTLIST
},
130 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"), "e", LIST_MOVELIST
},
131 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"), "g", LIST_MOVELIST
},
132 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"), "e", LIST_HOTLIST
},
133 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"), "g", LIST_HOTLIST
},
134 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), "cc", LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
135 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"), "up", LIST_HOTLIST
|LIST_MOVELIST
},
136 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"),"ad", LIST_HOTLIST
},
137 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"), "ct", LIST_HOTLIST
|LIST_VFSLIST
|LIST_MOVELIST
},
140 /* Directory hotlist */
141 static struct hotlist
{
142 enum HotListType type
;
145 struct hotlist
*head
;
147 struct hotlist
*next
;
150 struct hotlist
*current_group
;
152 static void remove_from_hotlist (struct hotlist
*entry
);
153 void add_new_group_cmd (void);
155 #define new_hotlist() g_new0(struct hotlist, 1)
157 static void hotlist_refresh (Dlg_head
*dlg
)
159 dialog_repaint (dlg
, COLOR_NORMAL
, COLOR_HOT_NORMAL
);
160 attrset (COLOR_NORMAL
);
162 dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
164 if (!hotlist_state
.moving
)
165 draw_box (dlg
, dlg
->lines
-8, 5, 3, dlg
->cols
- (UX
*2));
168 /* If current->data is 0, then we are dealing with a VFS pathname */
169 static INLINE
void update_path_name ()
172 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
173 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
176 if (list
->current
->data
!= 0) {
177 struct hotlist
*hlp
= (struct hotlist
*)list
->current
->data
;
179 if (hlp
->type
== HL_TYPE_ENTRY
)
180 text
= hlp
->directory
;
182 text
= _("Subgroup - press ENTER to see list");
184 p
= g_strconcat (" ", current_group
->label
, " ", NULL
);
185 if (!hotlist_state
.moving
)
186 label_set_text (pname_group
, name_trunc (p
, dlg
->cols
- (UX
*2+4)));
188 label_set_text (movelist_group
, name_trunc (p
, dlg
->cols
- (UX
*2+4)));
191 text
= list
->current
->text
;
196 if (!hotlist_state
.moving
)
197 label_set_text (pname
, name_trunc (text
, dlg
->cols
- (UX
*2+4)));
201 #define CHECK_BUFFER \
205 if ((i = strlen (current->label) + 3) > buflen) { \
207 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
212 static void fill_listbox (void)
214 struct hotlist
*current
= current_group
->head
;
219 buf
= g_malloc (buflen
= 1024);
223 switch (current
->type
) {
227 strcat (strcat (buf
, "->"), current
->label
);
228 if (hotlist_state
.moving
)
229 listbox_add_item (l_movelist
, 0, 0, buf
, current
);
231 listbox_add_item (l_hotlist
, 0, 0, buf
, current
);
235 if (hotlist_state
.moving
)
236 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
238 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
243 current
= current
->next
;
250 unlink_entry (struct hotlist
*entry
)
252 struct hotlist
*current
= current_group
->head
;
254 if (current
== entry
)
255 current_group
->head
= entry
->next
;
257 while (current
&& current
->next
!= entry
)
258 current
= current
->next
;
260 current
->next
= entry
->next
;
265 static void add_new_entry_cmd (void);
266 static void init_movelist (int, struct hotlist
*);
268 static int hotlist_button_callback (int action
, void *data
)
273 struct hotlist
*saved
= current_group
;
274 struct hotlist
*item
;
275 struct hotlist
*moveto_item
= 0;
276 struct hotlist
*moveto_group
= 0;
279 if (!l_hotlist
->current
)
280 return 0; /* empty group - nothing to do */
281 item
= l_hotlist
->current
->data
;
282 hotlist_state
.moving
= 1;
283 init_movelist (LIST_MOVELIST
, item
);
284 run_dlg (movelist_dlg
);
285 ret
= movelist_dlg
->ret_value
;
286 hotlist_state
.moving
= 0;
287 if (l_movelist
->current
)
288 moveto_item
= l_movelist
->current
->data
;
289 moveto_group
= current_group
;
290 destroy_dlg (movelist_dlg
);
291 current_group
= saved
;
294 if (moveto_item
== item
)
295 return 0; /* If we insert/append a before/after a
296 it hardly changes anything ;) */
298 listbox_remove_current (l_hotlist
, 1);
299 item
->up
= moveto_group
;
300 if (!moveto_group
->head
)
301 moveto_group
->head
= item
;
302 else if (!moveto_item
) { /* we have group with just comments */
303 struct hotlist
*p
= moveto_group
->head
;
309 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
310 if (!moveto_item
->next
)
311 moveto_item
->next
= item
;
313 item
->next
= moveto_item
->next
;
314 moveto_item
->next
= item
;
316 else if (moveto_group
->head
== moveto_item
) {
317 moveto_group
->head
= item
;
318 item
->next
= moveto_item
;
320 struct hotlist
*p
= moveto_group
->head
;
322 while (p
->next
!= moveto_item
)
324 item
->next
= p
->next
;
327 listbox_remove_list (l_hotlist
);
330 hotlist_state
.modified
= 1;
335 if (l_hotlist
->current
)
336 remove_from_hotlist (l_hotlist
->current
->data
);
341 add_new_group_cmd ();
351 add_new_entry_cmd ();
357 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
359 if (list
->current
->data
) {
360 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
361 if (hlp
->type
== HL_TYPE_ENTRY
)
364 listbox_remove_list (list
);
373 /* Fall through if list empty - just go up */
377 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
378 listbox_remove_list (list
);
379 current_group
= current_group
->up
;
392 static int hotlist_callback (Dlg_head
* h
, int Par
, int Msg
)
399 case DLG_UNHANDLED_KEY
:
406 if (hotlist_button_callback (B_ENTER
, 0)) {
407 h
->ret_value
= B_ENTER
;
413 if (hotlist_state
.type
!= LIST_VFSLIST
)
414 return !hotlist_button_callback (B_UP_GROUP
, 0);
421 if (!hotlist_state
.moving
)
423 if (l_hotlist
->current
){
424 if (l_hotlist
->current
->data
) {
425 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
426 if (hlp
->type
== HL_TYPE_ENTRY
) {
427 char *tmp
= g_strconcat ( "cd ", hlp
->directory
, NULL
);
428 stuff (input_w (cmdline
), tmp
, 0);
431 h
->ret_value
= B_CANCEL
;
437 return 1; /* ignore key */
444 if (hotlist_state
.moving
)
445 dlg_select_widget (movelist_dlg
, l_movelist
);
447 dlg_select_widget (hotlist_dlg
, l_hotlist
);
448 /* always stay on hotlist */
452 attrset (MENU_ENTRY_COLOR
);
459 static int l_call (void *l
)
461 WListbox
*list
= (WListbox
*) l
;
462 Dlg_head
*dlg
= hotlist_state
.moving
? movelist_dlg
: hotlist_dlg
;
465 if (list
->current
->data
) {
466 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
467 if (hlp
->type
== HL_TYPE_ENTRY
) {
468 dlg
->ret_value
= B_ENTER
;
470 return listbox_finish
;
472 hotlist_button_callback (B_ENTER
, (void *)0);
473 hotlist_callback (dlg
, '\n', DLG_POST_KEY
);
474 return listbox_nothing
;
477 dlg
->ret_value
= B_ENTER
;
479 return listbox_finish
;
483 hotlist_button_callback (B_UP_GROUP
, (void *)0);
484 hotlist_callback (dlg
, 'u', DLG_POST_KEY
);
485 return listbox_nothing
;
488 static void add_name_to_list (char *path
)
490 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
494 * Expands all button names (once) and recalculates button positions.
495 * returns number of columns in the dialog box, which is 10 chars longer
498 * If common width of the window (i.e. in xterm) is less than returned
499 * width - sorry :) (anyway this did not handled in previous version too)
502 init_i18n_stuff(int list_type
, int cols
)
505 static char* cancel_but
= "&Cancel";
508 static int hotlist_i18n_flag
= 0;
510 if (!hotlist_i18n_flag
)
512 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
514 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
516 cancel_but
= _(cancel_but
);
517 hotlist_i18n_flag
= 1;
519 #endif /* ENABLE_NLS */
521 /* Dynamic resizing of buttonbars */
523 int len
[2], count
[2]; /* at most two lines of buttons */
526 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
527 len
[0] = len
[1] = count
[0] = count
[1] = 0;
529 /* Count len of buttonbars, assuming 2 extra space between buttons */
532 if (! (hotlist_but
[i
].type
& list_type
))
535 row
= hotlist_but
[i
].y
;
537 len
[row
] += strlen (hotlist_but
[i
].text
) + 5;
538 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
544 cols
= max(cols
, max(len
[0], len
[1]));
546 /* arrange buttons */
548 cur_x
[0] = cur_x
[1] = 0;
549 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
552 if (! (hotlist_but
[i
].type
& list_type
))
555 row
= hotlist_but
[i
].y
;
557 if (hotlist_but
[i
].x
!= 0)
559 /* not first int the row */
560 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
562 cols
- strlen (hotlist_but
[i
].text
) - 13;
564 hotlist_but
[i
].x
= cur_x
[row
];
567 cur_x
[row
] += strlen (hotlist_but
[i
].text
) + 2
568 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
575 static void init_hotlist (int list_type
)
578 int hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
582 hotlist_state
.expanded
= GetPrivateProfileInt ("HotlistConfig",
583 "expanded_view_of_groups", 0, profile_name
);
585 hotlist_dlg
= create_dlg (0, 0, LINES
-2, hotlist_cols
, dialog_colors
,
587 list_type
== LIST_VFSLIST
? "[vfshot]" : "[Hotlist]",
588 list_type
== LIST_VFSLIST
? "vfshot" : "hotlist",
589 DLG_CENTER
|DLG_GRID
);
590 x_set_dialog_title (hotlist_dlg
,
591 list_type
== LIST_VFSLIST
? _("Active VFS directories") : _("Directory hotlist"));
593 #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
595 for (i
= 0; i
< BUTTONS
; i
++){
596 if (hotlist_but
[i
].type
& list_type
)
597 add_widget (hotlist_dlg
, button_new (XTRACT (i
)));
601 /* We add the labels.
602 * pname will hold entry's pathname;
603 * pname_group will hold name of current group
605 pname
= label_new (UY
-11+LINES
, UX
+2, "", "the-lab");
606 add_widget (hotlist_dlg
, pname
);
607 if (!hotlist_state
.moving
) {
608 add_widget (hotlist_dlg
, label_new (UY
-12+LINES
, UX
+1, _(" Directory path "), NULL
));
610 /* This one holds the displayed pathname */
611 pname_group
= label_new (UY
, UX
+1, _(" Directory label "), NULL
);
612 add_widget (hotlist_dlg
, pname_group
);
614 /* get new listbox */
615 l_hotlist
= listbox_new (UY
+ 1, UX
+ 1, COLS
-2*UX
-8, LINES
-14, listbox_cback
, l_call
, "listbox");
617 /* Fill the hotlist with the active VFS or the hotlist */
618 if (list_type
== LIST_VFSLIST
){
619 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
620 vfs_fill_names (add_name_to_list
);
624 add_widget (hotlist_dlg
, l_hotlist
);
625 /* add listbox to the dialogs */
628 static void init_movelist (int list_type
, struct hotlist
*item
)
631 char *hdr
= g_strconcat (_("Moving "), item
->label
, NULL
);
632 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
636 movelist_dlg
= create_dlg (0, 0, LINES
-6, movelist_cols
, dialog_colors
,
637 hotlist_callback
, "[Hotlist]",
639 DLG_CENTER
|DLG_GRID
);
640 x_set_dialog_title (movelist_dlg
, hdr
);
643 #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
645 for (i
= 0; i
< BUTTONS
; i
++){
646 if (hotlist_but
[i
].type
& list_type
)
647 add_widget (movelist_dlg
, button_new (XTRACT (i
)));
652 /* We add the labels. We are interested in the last one,
653 * that one will hold the path name label
655 movelist_group
= label_new (UY
, UX
+1, _(" Directory label "), NULL
);
656 add_widget (movelist_dlg
, movelist_group
);
657 /* get new listbox */
658 l_movelist
= listbox_new (UY
+ 1, UX
+ 1,
659 movelist_dlg
->cols
- 2*UX
- 2, movelist_dlg
->lines
- 8,
660 listbox_cback
, l_call
, "listbox");
664 add_widget (movelist_dlg
, l_movelist
);
665 /* add listbox to the dialogs */
668 static void hotlist_done (void)
670 destroy_dlg (hotlist_dlg
);
672 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
677 find_group_section (struct hotlist
*grp
)
679 return g_strconcat (grp
->directory
, ".Group", NULL
);
684 /* 1.11.96 bor: added pos parameter to control placement of new item.
685 see widget.c, listbox_add_item()
686 now hotlist is in unsorted mode
688 static struct hotlist
*
689 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
691 struct hotlist
*current
;
694 if (l_hotlist
&& l_hotlist
->current
)
695 current
= l_hotlist
->current
->data
;
697 new = new_hotlist ();
701 new->directory
= directory
;
702 new->up
= current_group
;
704 if (!current_group
->head
) { /* first element in group */
705 current_group
->head
= new;
706 } else if (pos
== 2) { /* should be appended after current*/
707 new->next
= current
->next
;
709 } else if (pos
== 1 &&
710 current
== current_group
->head
) {
711 /* should be inserted before first item */
713 current_group
->head
= new;
714 } else if (pos
== 1) { /* befor current */
715 struct hotlist
*p
= current_group
->head
;
717 while (p
->next
!= current
)
722 } else { /* append at the end */
723 struct hotlist
*p
= current_group
->head
;
731 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
) {
732 if (type
== HL_TYPE_GROUP
) {
733 char *lbl
= g_strconcat ("->", new->label
, NULL
);
735 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
738 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
739 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
746 * Support routine for add_new_entry_input()/add_new_group_input()
747 * Change positions of buttons (first three widgets).
749 * This is just a quick hack. Accurate procedure must take care of
750 * internationalized label lengths and total buttonbar length...assume
751 * 64 is longer anyway.
753 static void add_widgets_i18n(QuickWidget
* qw
, int len
)
755 int i
, l
[3], space
, cur_x
;
757 for (i
= 0; i
< 3; i
++)
759 qw
[i
].text
= _(qw
[i
].text
);
760 l
[i
] = strlen (qw
[i
].text
) + 3;
762 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
764 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
766 qw
[i
].relative_x
= cur_x
;
767 qw
[i
].x_divisions
= len
;
771 static int add_new_entry_input (char *header
, char *text1
, char *text2
, char *help
, char **r1
, char **r2
)
773 #define RELATIVE_Y_BUTTONS 4
774 #define RELATIVE_Y_LABEL_PTH 3
775 #define RELATIVE_Y_INPUT_PTH 4
777 QuickDialog Quick_input
;
778 static QuickWidget quick_widgets
[] = {
779 { quick_button
, 55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), 0, B_CANCEL
,
780 0, 0, "button-cancel" },
781 { quick_button
, 30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), 0, B_INSERT
,
782 0, 0, "button-insert" },
783 { quick_button
, 10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), 0, B_APPEND
,
784 0, 0, "button-append" },
785 { quick_input
, 4, 80, RELATIVE_Y_INPUT_PTH
, 0, "",58, 0,
787 { quick_label
, RELATIVE_Y_LABEL_PTH
, 80, 3, 0, 0, 0, 0,
789 { quick_input
, 4, 80, 3, 0, "", 58, 0,
791 { quick_label
, 3, 80, 2, 0, 0, 0, 0,
798 char *my_str1
, *my_str2
;
801 static int i18n_flag
= 0;
802 #endif /* ENABLE_NLS */
804 len
= max (strlen (header
), msglen (text1
, &lines1
));
805 len
= max (len
, msglen (text2
, &lines2
)) + 4;
811 add_widgets_i18n(quick_widgets
, len
);
814 #endif /* ENABLE_NLS */
816 Quick_input
.xlen
= len
;
817 Quick_input
.xpos
= -1;
818 Quick_input
.title
= header
;
819 Quick_input
.help
= help
;
820 Quick_input
.class = "hotlist_new_entry";
821 Quick_input
.i18n
= 0;
822 quick_widgets
[6].text
= text1
;
823 quick_widgets
[4].text
= text2
;
824 quick_widgets
[5].text
= *r1
;
825 quick_widgets
[3].text
= *r2
;
827 for (i
= 0; i
< 7; i
++)
828 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
829 Quick_input
.ylen
= lines1
+ lines2
+ 7;
831 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
832 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
833 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
834 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
835 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
837 quick_widgets
[5].str_result
= &my_str1
;
838 quick_widgets
[3].str_result
= &my_str2
;
840 Quick_input
.widgets
= quick_widgets
;
841 if ((i
= quick_dialog (&Quick_input
)) != B_CANCEL
){
842 *r1
= *(quick_widgets
[5].str_result
);
843 *r2
= *(quick_widgets
[3].str_result
);
849 static void add_new_entry_cmd (void)
851 char *title
= 0, *url
= 0;
854 /* Take current directory as default value for input fields */
855 title
= url
= cpanel
->cwd
;
857 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
858 "[Hotlist]", &title
, &url
);
860 if (!ret
|| !title
|| !*title
|| !url
|| !*url
)
863 if (ret
== B_ENTER
|| ret
== B_APPEND
)
864 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 2);
866 add2hotlist (g_strdup (title
),g_strdup (url
), HL_TYPE_ENTRY
, 1);
868 hotlist_state
.modified
= 1;
871 static int add_new_group_input (char *header
, char *label
, char **result
)
874 QuickDialog Quick_input
;
875 static QuickWidget quick_widgets
[] = {
876 { quick_button
, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
878 { quick_button
, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT
, 0, 0,
880 { quick_button
, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND
, 0, 0,
882 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
883 { quick_label
, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
885 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
886 quick_widgets variable above */
893 static int i18n_flag
= 0;
894 #endif /* ENABLE_NLS */
896 len
= max (strlen (header
), msglen (label
, &lines
)) + 4;
902 add_widgets_i18n(quick_widgets
, len
);
905 #endif /* ENABLE_NLS */
907 Quick_input
.xlen
= len
;
908 Quick_input
.xpos
= -1;
909 Quick_input
.title
= header
;
910 Quick_input
.help
= "[Hotlist]";
911 Quick_input
.class = "hotlist_new_group";
912 Quick_input
.i18n
= 0;
913 quick_widgets
[4].text
= label
;
915 for (i
= 0; i
< 5; i
++)
916 quick_widgets
[i
].y_divisions
= lines
+6;
917 Quick_input
.ylen
= lines
+ 6;
919 for (i
= 0; i
< 4; i
++)
920 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
922 quick_widgets
[3].str_result
= &my_str
;
923 quick_widgets
[3].text
= "";
925 Quick_input
.widgets
= quick_widgets
;
926 if ((ret
= quick_dialog (&Quick_input
)) != B_CANCEL
){
927 *result
= *(quick_widgets
[3].str_result
);
933 void add_new_group_cmd (void)
938 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
939 if (!ret
|| !label
|| !*label
)
942 if (ret
== B_ENTER
|| ret
== B_APPEND
)
943 add2hotlist (label
, 0, HL_TYPE_GROUP
, 2);
945 add2hotlist (label
, 0, HL_TYPE_GROUP
, 1);
947 hotlist_state
.modified
= 1;
950 void add2hotlist_cmd (void)
952 char *prompt
, *label
;
953 char *cp
= _("Label for \"%s\":");
956 prompt
= g_strdup_printf (cp
, name_trunc (cpanel
->cwd
, COLS
-2*UX
-(l
+8)));
957 label
= input_dialog (_(" Add to hotlist "), prompt
, cpanel
->cwd
);
959 if (!label
|| !*label
)
962 add2hotlist (label
,g_strdup (cpanel
->cwd
), HL_TYPE_ENTRY
, 0);
963 hotlist_state
.modified
= 1;
966 static void remove_group (struct hotlist
*grp
)
968 struct hotlist
*current
= grp
->head
;
971 struct hotlist
*next
= current
->next
;
973 if (current
->type
== HL_TYPE_GROUP
)
974 remove_group (current
);
977 g_free (current
->label
);
978 if (current
->directory
)
979 g_free (current
->directory
);
987 static void remove_from_hotlist (struct hotlist
*entry
)
989 if (entry
->type
== HL_TYPE_GROUP
) {
994 header
= g_strconcat (_(" Remove: "),
995 name_trunc (entry
->label
, 30),
998 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1000 _("&No"), _("&Yes"));
1007 remove_group (entry
);
1010 unlink_entry (entry
);
1013 g_free (entry
->label
);
1014 if (entry
->directory
)
1015 g_free (entry
->directory
);
1017 /* now remove list entry from screen */
1018 listbox_remove_current (l_hotlist
, 1);
1019 hotlist_state
.modified
= 1;
1022 char *hotlist_cmd (int vfs_or_hotlist
)
1024 char *target
= NULL
;
1026 hotlist_state
.type
= vfs_or_hotlist
;
1029 init_hotlist (vfs_or_hotlist
);
1031 /* display file info */
1032 attrset (SELECTED_COLOR
);
1034 hotlist_state
.running
= 1;
1035 run_dlg (hotlist_dlg
);
1036 hotlist_state
.running
= 0;
1039 switch (hotlist_dlg
->ret_value
) {
1044 if (l_hotlist
->current
->data
) {
1045 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1046 target
= g_strdup (hlp
->directory
);
1048 target
= g_strdup (l_hotlist
->current
->text
);
1057 load_group (struct hotlist
*grp
)
1061 char *group_section
;
1062 struct hotlist
*current
= 0;
1064 group_section
= find_group_section (grp
);
1066 profile_keys
= profile_init_iterator (group_section
, profile_name
);
1068 current_group
= grp
;
1070 while (profile_keys
){
1071 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1072 add2hotlist (g_strdup (value
), g_strdup (key
), HL_TYPE_GROUP
, 0);
1074 g_free (group_section
);
1076 profile_keys
= profile_init_iterator (grp
->directory
, profile_name
);
1078 while (profile_keys
){
1079 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1080 add2hotlist (g_strdup (value
),g_strdup (key
), HL_TYPE_ENTRY
, 0);
1083 for (current
= grp
->head
; current
; current
= current
->next
)
1084 load_group (current
);
1089 #define TKN_STRING 2
1091 #define TKN_ENDGROUP 4
1092 #define TKN_COMMENT 5
1095 #define TKN_UNKNOWN 127
1097 static char *tkn_buf
;
1098 static int tkn_buf_length
;
1099 static int tkn_length
;
1101 static char *hotlist_file_name
;
1102 static FILE *hotlist_file
;
1103 static time_t hotlist_file_mtime
;
1105 static int hot_skip_blanks ()
1109 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && isspace (c
))
1115 static int hot_next_token ()
1119 #define CHECK_BUF() \
1121 if (tkn_length == tkn_buf_length) \
1122 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1123 : ( g_malloc (tkn_buf_length = 1024)); \
1129 c
= hot_skip_blanks ();
1138 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1143 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1147 tkn_buf
[tkn_length
] = '\0';
1151 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1153 if ((c
= getc (hotlist_file
)) == EOF
)
1156 tkn_buf
[tkn_length
++] = c
== '\n' ? ' ' : c
;
1161 tkn_buf
[tkn_length
] = '\0';
1165 if ((c
= getc (hotlist_file
)) == EOF
)
1170 /* fall through; it is taken as normal character */
1175 tkn_buf
[tkn_length
++] = toupper(c
);
1176 } while ((c
= fgetc (hotlist_file
)) != EOF
&& isalnum (c
));
1178 ungetc (c
, hotlist_file
);
1180 tkn_buf
[tkn_length
] = '\0';
1181 if (strncmp (tkn_buf
, "GROUP", tkn_length
) == 0)
1183 else if (strncmp (tkn_buf
, "ENTRY", tkn_length
) == 0)
1185 else if (strncmp (tkn_buf
, "ENDGROUP", tkn_length
) == 0)
1186 return TKN_ENDGROUP
;
1187 else if (strncmp (tkn_buf
, "URL", tkn_length
) == 0)
1195 #define SKIP_TO_EOL { \
1197 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1200 #define CHECK_TOKEN(_TKN_) \
1201 if ((tkn = hot_next_token ()) != _TKN_) { \
1202 hotlist_state.readonly = 1; \
1203 hotlist_state.file_error = 1; \
1204 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1205 tkn = hot_next_token (); \
1210 hot_load_group (struct hotlist
* grp
)
1213 struct hotlist
*new_grp
;
1216 current_group
= grp
;
1218 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1221 CHECK_TOKEN(TKN_STRING
);
1222 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1224 hot_load_group (new_grp
);
1225 current_group
= grp
;
1228 CHECK_TOKEN(TKN_STRING
);
1229 label
= g_strdup (tkn_buf
);
1230 CHECK_TOKEN(TKN_URL
);
1231 CHECK_TOKEN(TKN_STRING
);
1232 url
= g_strdup (tkn_buf
);
1233 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1237 label
= g_strdup (tkn_buf
);
1238 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1241 hotlist_state
.readonly
= 1;
1242 hotlist_state
.file_error
= 1;
1246 /* skip empty lines */
1249 hotlist_state
.readonly
= 1;
1250 hotlist_state
.file_error
= 1;
1258 hot_load_file (struct hotlist
* grp
)
1261 struct hotlist
*new_grp
;
1264 current_group
= grp
;
1266 while ((tkn
= hot_next_token())!= TKN_EOF
)
1269 CHECK_TOKEN(TKN_STRING
);
1270 new_grp
= add2hotlist (g_strdup (tkn_buf
), 0, HL_TYPE_GROUP
, 0);
1272 hot_load_group (new_grp
);
1273 current_group
= grp
;
1276 CHECK_TOKEN(TKN_STRING
);
1277 label
= g_strdup (tkn_buf
);
1278 CHECK_TOKEN(TKN_URL
);
1279 CHECK_TOKEN(TKN_STRING
);
1280 url
= g_strdup (tkn_buf
);
1281 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1285 label
= g_strdup (tkn_buf
);
1286 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1289 /* skip empty lines */
1292 hotlist_state
.readonly
= 1;
1293 hotlist_state
.file_error
= 1;
1300 clean_up_hotlist_groups (char *section
)
1306 grp_section
= g_strconcat (section
, ".Group", NULL
);
1307 if (profile_has_section (section
, profile_name
))
1308 profile_clean_section (section
, profile_name
);
1309 if (profile_has_section (grp_section
, profile_name
)) {
1310 profile_keys
= profile_init_iterator (grp_section
, profile_name
);
1312 while (profile_keys
) {
1313 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
1314 clean_up_hotlist_groups (key
);
1316 profile_clean_section (grp_section
, profile_name
);
1318 g_free (grp_section
);
1323 void load_hotlist (void)
1326 int has_old_list
= 0;
1327 int remove_old_list
= 0;
1328 struct stat stat_buf
;
1330 if (hotlist_state
.loaded
) {
1331 stat (hotlist_file_name
, &stat_buf
);
1332 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1338 if (!hotlist_file_name
)
1339 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1341 hotlist
= new_hotlist ();
1342 hotlist
->type
= HL_TYPE_GROUP
;
1343 hotlist
->label
= g_strdup (_(" Top level group "));
1344 hotlist
->up
= hotlist
;
1348 hotlist
->directory
= g_strdup ("Hotlist");
1350 grp_section
= g_strconcat ("Hotlist", ".Group", NULL
);
1351 has_old_list
= profile_has_section ("Hotlist", profile_name
) ||
1352 profile_has_section (grp_section
, profile_name
);
1353 g_free (grp_section
);
1355 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1359 msg
= g_strconcat (_("Hotlist is now kept in file ~/"),
1360 HOTLIST_FILENAME
, "\n",
1361 _("MC will load hotlist from ~/"),
1363 _("and then delete [Hotlist] section there"),
1365 message (0, _(" Hotlist Load "), msg
);
1368 load_group (hotlist
);
1369 hotlist_state
.loaded
= 1;
1371 * just to be sure we got copy
1373 hotlist_state
.modified
= 1;
1374 result
= save_hotlist ();
1375 hotlist_state
.modified
= 0;
1377 remove_old_list
= 1;
1381 msg
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1382 _(" file, your old hotlist entries were not deleted"), NULL
);
1384 message (D_ERROR
, _(" Hotlist Load "), msg
);
1388 hot_load_file (hotlist
);
1389 fclose (hotlist_file
);
1390 hotlist_state
.loaded
= 1;
1396 _("You have ~/"), HOTLIST_FILENAME
, _(" file and [Hotlist] section in ~/"), PROFILE_NAME
, "\n",
1397 _("Your ~/"), HOTLIST_FILENAME
, _(" most probably was created\n"),
1398 _("by an earlier development version of MC\nand is more actual than ~/"),
1399 PROFILE_NAME
, _(" entries\n\n"),
1400 _("You can choose between\n\n"
1401 " Remove - remove old hotlist entries from ~/"), PROFILE_NAME
, "\n",
1402 _(" Keep - keep your old entries; you will be asked\n"
1403 " the same question next time\n"
1404 " Merge - add old entries to hotlist as group \"Entries from ~/"),
1405 PROFILE_NAME
, "\"\n\n", NULL
);
1407 result
= query_dialog (_(" Hotlist Load "),
1408 msg
, D_ERROR
, 3, _("&Remove"), _("&Keep"), _("&Merge"));
1410 remove_old_list
= 1;
1411 else if (result
== 2) {
1412 struct hotlist
*grp
= hotlist
->head
;
1413 struct hotlist
*old
;
1416 load_group (hotlist
);
1418 old
= new_hotlist ();
1419 old
->type
= HL_TYPE_GROUP
;
1420 old
->label
= g_strconcat (_(" Entries from ~/"), PROFILE_NAME
, NULL
);
1422 old
->head
= hotlist
->head
;
1424 hotlist
->head
= old
;
1425 hotlist_state
.modified
= 1;
1426 if (!save_hotlist ()){
1429 str
= g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME
,
1430 _(" file your old hotlist entries were not deleted"), NULL
);
1432 message (D_ERROR
, _(" Hotlist Load "), str
);
1435 remove_old_list
= 1;
1436 hotlist_state
.modified
= 0;
1441 if (remove_old_list
) {
1442 clean_up_hotlist_groups ("Hotlist");
1446 stat (hotlist_file_name
, &stat_buf
);
1447 hotlist_file_mtime
= stat_buf
.st_mtime
;
1448 current_group
= hotlist
;
1452 save_group (struct hotlist
*grp
)
1454 struct hotlist
*current
= grp
->head
;
1455 char *group_section
;
1457 group_section
= find_group_section (grp
);
1459 profile_clean_section (group_section
, profile_name
);
1460 for (;current
&& current
->type
== HL_TYPE_GROUP
; current
= current
->next
){
1461 WritePrivateProfileString (group_section
,
1466 g_free (group_section
);
1468 for (current
= grp
->head
;
1469 current
&& current
->type
== HL_TYPE_GROUP
;
1470 current
= current
->next
)
1471 save_group (current
);
1473 profile_clean_section (grp
->directory
, profile_name
);
1474 for (;current
; current
= current
->next
){
1475 WritePrivateProfileString (grp
->directory
,
1482 static int list_level
= 0;
1485 hot_save_group (struct hotlist
*grp
)
1487 struct hotlist
*current
= grp
->head
;
1493 for (i = 0; i < n; i++) \
1494 putc (' ', hotlist_file); \
1497 for (;current
; current
= current
->next
)
1498 switch (current
->type
) {
1500 INDENT (list_level
);
1501 fputs ("GROUP \"", hotlist_file
);
1502 for (s
= current
->label
; *s
; s
++) {
1504 putc ('\\', hotlist_file
);
1505 else if (*s
== '\\')
1506 putc ('\\', hotlist_file
);
1507 putc (*s
, hotlist_file
);
1509 fputs ("\"\n", hotlist_file
);
1511 hot_save_group (current
);
1513 INDENT (list_level
);
1514 fputs ("ENDGROUP\n", hotlist_file
);
1518 fputs ("ENTRY \"", hotlist_file
);
1519 for (s
= current
->label
; *s
; s
++) {
1521 putc ('\\', hotlist_file
);
1522 else if (*s
== '\\')
1523 putc ('\\', hotlist_file
);
1524 putc (*s
, hotlist_file
);
1526 fputs ("\" URL \"", hotlist_file
);
1527 for (s
= current
->directory
; *s
; s
++) {
1529 putc ('\\', hotlist_file
);
1530 else if (*s
== '\\')
1531 putc ('\\', hotlist_file
);
1532 putc (*s
, hotlist_file
);
1534 fputs ("\"\n", hotlist_file
);
1536 case HL_TYPE_COMMENT
:
1537 fprintf (hotlist_file
, "#%s\n", current
->label
);
1543 int save_hotlist (void)
1546 struct stat stat_buf
;
1548 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1549 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", NULL
);
1551 rename (hotlist_file_name
, fbak
);
1552 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1553 if (stat (fbak
, &stat_buf
) == 0)
1554 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1556 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1557 hot_save_group (hotlist
);
1558 fclose (hotlist_file
);
1559 stat (hotlist_file_name
, &stat_buf
);
1560 hotlist_file_mtime
= stat_buf
.st_mtime
;
1562 hotlist_state
.modified
= 0;
1564 rename (fbak
, hotlist_file_name
);
1571 void done_hotlist (void)
1574 remove_group (hotlist
);
1576 g_free (hotlist
->label
);
1577 if (hotlist
->directory
)
1578 g_free (hotlist
->directory
);
1583 hotlist_state
.loaded
= 0;
1585 if (hotlist_file_name
){
1586 g_free (hotlist_file_name
);
1587 hotlist_file_name
= 0;