1 /* Directory hotlist -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
11 Janne did the original Hotlist code, Andrej made the groupable
12 hotlist; the move hotlist and revamped the file format and made
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 * \brief Source: directory hotlist
39 #include <sys/types.h>
45 #include "../src/tty/tty.h" /* COLS */
46 #include "../src/skin/skin.h"
47 #include "../src/tty/key.h" /* KEY_M_CTRL */
51 #include "setup.h" /* For profile_bname */
52 #include "../src/mcconfig/mcconfig.h" /* Load/save directories hotlist */
53 #include "wtools.h" /* QuickDialog */
54 #include "panel.h" /* current_panel */
55 #include "main.h" /* update_panels() */
56 #include "layout.h" /* repaint_screen() */
58 #include "command.h" /* cmdline */
59 #include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
67 #define BY (LINES - 6)
69 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
71 #define B_ADD_CURRENT B_USER
72 #define B_REMOVE (B_USER + 1)
73 #define B_NEW_GROUP (B_USER + 2)
74 #define B_NEW_ENTRY (B_USER + 3)
75 #define B_UP_GROUP (B_USER + 4)
76 #define B_INSERT (B_USER + 5)
77 #define B_APPEND (B_USER + 6)
78 #define B_MOVE (B_USER + 7)
81 #include "../vfs/gc.h"
82 #define B_FREE_ALL_VFS (B_USER + 8)
83 #define B_REFRESH_VFS (B_USER + 9)
86 int hotlist_has_dot_dot
= 1;
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
;
105 * these parameters are intended to be user configurable
107 int expanded
; /* expanded view of all groups at startup */
110 * these reflect run time state
113 int loaded
; /* hotlist is loaded */
114 int readonly
; /* hotlist readonly */
115 int file_error
; /* parse error while reading file */
116 int running
; /* we are running dlg (and have to
118 int moving
; /* we are in moving hotlist currently */
119 int modified
; /* hotlist was modified */
120 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
123 static struct _hotlist_but
{
124 int ret_cmd
, flags
, y
, x
;
127 widget_pos_flags_t pos_flags
;
129 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"),
130 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
131 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"),
132 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
133 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"),
134 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
135 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"),
136 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
137 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &Entry"),
138 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
139 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &Group"),
140 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
141 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"),
142 LIST_HOTLIST
| LIST_VFSLIST
|LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
143 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"),
144 LIST_HOTLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
145 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"),
146 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
148 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"),
149 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
150 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"),
151 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
153 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &To"),
154 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
}
157 /* Directory hotlist */
158 static struct hotlist
{
159 enum HotListType type
;
162 struct hotlist
*head
;
164 struct hotlist
*next
;
167 static struct hotlist
*current_group
;
169 static void init_movelist (int, struct hotlist
*);
170 static void add_new_group_cmd (void);
171 static void add_new_entry_cmd (void);
172 static void remove_from_hotlist (struct hotlist
*entry
);
173 static void load_hotlist (void);
174 static void add_dotdot_to_list (void);
176 #define new_hotlist() g_new0(struct hotlist, 1)
179 hotlist_refresh (Dlg_head
* dlg
)
181 common_dialog_repaint (dlg
);
182 tty_setcolor (COLOR_NORMAL
);
183 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10),
184 dlg
->cols
- (UX
* 2));
185 if (!hotlist_state
.moving
)
186 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2));
189 /* If current->data is 0, then we are dealing with a VFS pathname */
191 update_path_name (void)
193 const char *text
= "";
195 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
196 Dlg_head
*dlg
= list
->widget
.parent
;
199 if (list
->current
->data
!= 0) {
200 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
202 if (hlp
->type
== HL_TYPE_ENTRY
||
203 hlp
->type
== HL_TYPE_DOTDOT
)
204 text
= hlp
->directory
;
205 else if (hlp
->type
== HL_TYPE_GROUP
)
206 text
= _("Subgroup - press ENTER to see list");
208 text
= list
->current
->text
;
211 if (!hotlist_state
.moving
)
212 label_set_text (pname
,
213 str_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
215 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
216 if (!hotlist_state
.moving
)
217 label_set_text (pname_group
,
218 str_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
220 label_set_text (movelist_group
,
221 str_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
227 #define CHECK_BUFFER \
231 if ((i = strlen (current->label) + 3) > buflen) { \
233 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
238 static void fill_listbox (void)
240 struct hotlist
*current
= current_group
->head
;
241 GString
*buff
= g_string_new ("");
244 switch (current
->type
) {
248 g_string_truncate(buff
, 0);
249 g_string_append(buff
,"->");
250 g_string_append(buff
,current
->label
);
251 if (hotlist_state
.moving
)
252 listbox_add_item (l_movelist
, 0, 0, buff
->str
, current
);
254 listbox_add_item (l_hotlist
, 0, 0, buff
->str
, current
);
259 if (hotlist_state
.moving
)
260 listbox_add_item (l_movelist
, 0, 0, current
->label
, current
);
262 listbox_add_item (l_hotlist
, 0, 0, current
->label
, current
);
267 current
= current
->next
;
269 g_string_free (buff
, TRUE
);
273 unlink_entry (struct hotlist
*entry
)
275 struct hotlist
*current
= current_group
->head
;
277 if (current
== entry
)
278 current_group
->head
= entry
->next
;
280 while (current
&& current
->next
!= entry
)
281 current
= current
->next
;
283 current
->next
= entry
->next
;
290 static void add_name_to_list (const char *path
)
292 listbox_add_item (l_hotlist
, 0, 0, path
, 0);
294 #endif /* !USE_VFS */
297 hotlist_button_callback (int action
)
302 struct hotlist
*saved
= current_group
;
303 struct hotlist
*item
;
304 struct hotlist
*moveto_item
= 0;
305 struct hotlist
*moveto_group
= 0;
308 if (!l_hotlist
->current
)
309 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
310 item
= l_hotlist
->current
->data
;
311 hotlist_state
.moving
= 1;
312 init_movelist (LIST_MOVELIST
, item
);
313 run_dlg (movelist_dlg
);
314 ret
= movelist_dlg
->ret_value
;
315 hotlist_state
.moving
= 0;
316 if (l_movelist
->current
)
317 moveto_item
= l_movelist
->current
->data
;
318 moveto_group
= current_group
;
319 destroy_dlg (movelist_dlg
);
320 current_group
= saved
;
322 return MSG_NOT_HANDLED
;
323 if (moveto_item
== item
)
324 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
325 it hardly changes anything ;) */
327 listbox_remove_current (l_hotlist
, 1);
328 item
->up
= moveto_group
;
329 if (!moveto_group
->head
)
330 moveto_group
->head
= item
;
331 else if (!moveto_item
) { /* we have group with just comments */
332 struct hotlist
*p
= moveto_group
->head
;
338 } else if (ret
== B_ENTER
|| ret
== B_APPEND
)
339 if (!moveto_item
->next
)
340 moveto_item
->next
= item
;
342 item
->next
= moveto_item
->next
;
343 moveto_item
->next
= item
;
344 } else if (moveto_group
->head
== moveto_item
) {
345 moveto_group
->head
= item
;
346 item
->next
= moveto_item
;
348 struct hotlist
*p
= moveto_group
->head
;
350 while (p
->next
!= moveto_item
)
352 item
->next
= p
->next
;
355 listbox_remove_list (l_hotlist
);
358 hotlist_state
.modified
= 1;
359 return MSG_NOT_HANDLED
;
363 if (l_hotlist
->current
&& l_hotlist
->current
->data
)
364 remove_from_hotlist (l_hotlist
->current
->data
);
365 return MSG_NOT_HANDLED
;
369 add_new_group_cmd ();
370 return MSG_NOT_HANDLED
;
375 return MSG_NOT_HANDLED
;
379 add_new_entry_cmd ();
380 return MSG_NOT_HANDLED
;
385 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
387 if (list
->current
->data
) {
388 struct hotlist
*hlp
=
389 (struct hotlist
*) list
->current
->data
;
390 if (hlp
->type
== HL_TYPE_ENTRY
)
392 else if (hlp
->type
== HL_TYPE_DOTDOT
) {
393 /* Fall through - go up */
397 listbox_remove_list (list
);
400 return MSG_NOT_HANDLED
;
406 /* Fall through if list empty - just go up */
410 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
411 listbox_remove_list (list
);
412 current_group
= current_group
->up
;
414 return MSG_NOT_HANDLED
;
424 listbox_remove_list (l_hotlist
);
425 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
426 vfs_fill_names (add_name_to_list
);
427 return MSG_NOT_HANDLED
;
438 hotlist_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
445 case DLG_UNHANDLED_KEY
:
447 case KEY_M_CTRL
| '\n':
452 if (hotlist_button_callback (B_ENTER
)) {
453 h
->ret_value
= B_ENTER
;
459 if (hotlist_state
.type
!= LIST_VFSLIST
)
460 return !hotlist_button_callback (B_UP_GROUP
);
462 return MSG_NOT_HANDLED
;
465 if (!hotlist_state
.moving
) {
466 hotlist_button_callback (B_REMOVE
);
473 if (!hotlist_state
.moving
) {
474 if (l_hotlist
->current
) {
475 if (l_hotlist
->current
->data
) {
476 struct hotlist
*hlp
=
477 (struct hotlist
*) l_hotlist
->current
->data
;
478 if (hlp
->type
== HL_TYPE_ENTRY
) {
480 g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
481 stuff (cmdline
, tmp
, 0);
484 h
->ret_value
= B_CANCEL
;
490 return MSG_HANDLED
; /* ignore key */
492 return MSG_NOT_HANDLED
;
495 if (hotlist_state
.moving
)
496 dlg_select_widget (l_movelist
);
498 dlg_select_widget (l_hotlist
);
499 /* always stay on hotlist */
503 tty_setcolor (MENU_ENTRY_COLOR
);
508 /* simply call dlg_set_size() with new size */
509 dlg_set_size (h
, LINES
- 2, COLS
- 6);
513 return default_dlg_callback (h
, msg
, parm
);
517 static int l_call (WListbox
*list
)
519 Dlg_head
*dlg
= list
->widget
.parent
;
522 if (list
->current
->data
) {
523 struct hotlist
*hlp
= (struct hotlist
*) list
->current
->data
;
524 if (hlp
->type
== HL_TYPE_ENTRY
) {
525 dlg
->ret_value
= B_ENTER
;
529 hotlist_button_callback (B_ENTER
);
530 hotlist_callback (dlg
, DLG_POST_KEY
, '\n');
534 dlg
->ret_value
= B_ENTER
;
540 hotlist_button_callback (B_UP_GROUP
);
541 hotlist_callback (dlg
, DLG_POST_KEY
, 'u');
546 * Expands all button names (once) and recalculates button positions.
547 * returns number of columns in the dialog box, which is 10 chars longer
550 * If common width of the window (i.e. in xterm) is less than returned
551 * width - sorry :) (anyway this did not handled in previous version too)
554 init_i18n_stuff(int list_type
, int cols
)
557 static const char* cancel_but
= N_("&Cancel");
560 static int hotlist_i18n_flag
= 0;
562 if (!hotlist_i18n_flag
)
564 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
566 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
568 cancel_but
= _(cancel_but
);
569 hotlist_i18n_flag
= 1;
571 #endif /* ENABLE_NLS */
573 /* Dynamic resizing of buttonbars */
575 int len
[2], count
[2]; /* at most two lines of buttons */
578 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
579 len
[0] = len
[1] = count
[0] = count
[1] = 0;
581 /* Count len of buttonbars, assuming 2 extra space between buttons */
584 if (! (hotlist_but
[i
].type
& list_type
))
587 row
= hotlist_but
[i
].y
;
589 len
[row
] += str_term_width1 (hotlist_but
[i
].text
) + 5;
590 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
596 cols
= max(cols
, max(len
[0], len
[1]));
598 /* arrange buttons */
600 cur_x
[0] = cur_x
[1] = 0;
601 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
604 if (! (hotlist_but
[i
].type
& list_type
))
607 row
= hotlist_but
[i
].y
;
609 if (hotlist_but
[i
].x
!= 0)
611 /* not first int the row */
612 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
614 cols
- str_term_width1 (hotlist_but
[i
].text
) - 13;
616 hotlist_but
[i
].x
= cur_x
[row
];
619 cur_x
[row
] += str_term_width1 (hotlist_but
[i
].text
) + 2
620 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
628 init_hotlist (int list_type
)
631 const char *title
, *help_node
;
634 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
638 hotlist_state
.expanded
=
639 mc_config_get_int (mc_main_config
, "HotlistConfig", "expanded_view_of_groups", 0);
641 if (list_type
== LIST_VFSLIST
) {
642 title
= _("Active VFS directories");
643 help_node
= "[vfshot]"; /* FIXME - no such node */
645 title
= _("Directory hotlist");
646 help_node
= "[Hotlist]";
650 create_dlg (0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
651 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
653 for (i
= 0; i
< BUTTONS
; i
++) {
654 if (hotlist_but
[i
].type
& list_type
)
655 add_widget_autopos (hotlist_dlg
,
656 button_new (BY
+ hotlist_but
[i
].y
,
657 BX
+ hotlist_but
[i
].x
,
658 hotlist_but
[i
].ret_cmd
,
659 hotlist_but
[i
].flags
,
661 hotlist_button_callback
),
662 hotlist_but
[i
].pos_flags
);
665 /* We add the labels.
666 * pname will hold entry's pathname;
667 * pname_group will hold name of current group
669 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
670 add_widget_autopos (hotlist_dlg
, pname
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
);
671 if (!hotlist_state
.moving
) {
672 add_widget_autopos (hotlist_dlg
,
673 label_new (UY
- 12 + LINES
, UX
+ 1,
674 _(" Directory path ")),
675 WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
);
677 /* This one holds the displayed pathname */
678 pname_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
679 add_widget (hotlist_dlg
, pname_group
);
681 /* get new listbox */
683 listbox_new (UY
+ 1, UX
+ 1, LINES
- 14, COLS
- 2 * UX
- 8,
686 /* Fill the hotlist with the active VFS or the hotlist */
688 if (list_type
== LIST_VFSLIST
) {
689 listbox_add_item (l_hotlist
, 0, 0, home_dir
, 0);
690 vfs_fill_names (add_name_to_list
);
692 #endif /* !USE_VFS */
695 add_widget_autopos (hotlist_dlg
, l_hotlist
, WPOS_KEEP_ALL
);
696 /* add listbox to the dialogs */
700 init_movelist (int list_type
, struct hotlist
*item
)
703 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
704 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
709 create_dlg (0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
710 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
713 for (i
= 0; i
< BUTTONS
; i
++) {
714 if (hotlist_but
[i
].type
& list_type
)
715 add_widget (movelist_dlg
,
716 button_new (BY
- 4 + hotlist_but
[i
].y
,
717 BX
+ hotlist_but
[i
].x
,
718 hotlist_but
[i
].ret_cmd
,
719 hotlist_but
[i
].flags
,
721 hotlist_button_callback
));
724 /* We add the labels. We are interested in the last one,
725 * that one will hold the path name label
727 movelist_group
= label_new (UY
, UX
+ 1, _(" Directory label "));
728 add_widget (movelist_dlg
, movelist_group
);
729 /* get new listbox */
731 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->lines
- 8,
732 movelist_dlg
->cols
- 2 * UX
- 2, l_call
);
736 add_widget (movelist_dlg
, l_movelist
);
737 /* add listbox to the dialogs */
741 * Destroy the list dialog.
742 * Don't confuse with done_hotlist() for the list in memory.
744 static void hotlist_done (void)
746 destroy_dlg (hotlist_dlg
);
749 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
754 find_group_section (struct hotlist
*grp
)
756 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
760 /* 1.11.96 bor: added pos parameter to control placement of new item.
761 see widget.c, listbox_add_item()
762 now hotlist is in unsorted mode
765 HL_BEFORE_CURRENT
= 1
766 ,HL_AFTER_CURRENT
= 2
769 static struct hotlist
*
770 add2hotlist (char *label
, char *directory
, enum HotListType type
, int pos
)
773 struct hotlist
*current
= NULL
;
776 * Hotlist is neither loaded nor loading.
777 * Must be called by "Ctrl-x a" before using hotlist.
782 if (l_hotlist
&& l_hotlist
->current
) {
783 current
= l_hotlist
->current
->data
;
785 /* Make sure `..' stays at the top of the list. */
786 if (current
->type
== HL_TYPE_DOTDOT
)
787 pos
= HL_AFTER_CURRENT
;
790 new = new_hotlist ();
794 new->directory
= directory
;
795 new->up
= current_group
;
797 if (type
== HL_TYPE_GROUP
) {
799 add_dotdot_to_list ();
800 current_group
= new->up
;
803 if (!current_group
->head
) { /* first element in group */
804 current_group
->head
= new;
805 } else if (pos
== HL_AFTER_CURRENT
) {
806 new->next
= current
->next
;
808 } else if (pos
== HL_BEFORE_CURRENT
&&
809 current
== current_group
->head
) {
810 /* should be inserted before first item */
812 current_group
->head
= new;
813 } else if (pos
== HL_BEFORE_CURRENT
) {
814 struct hotlist
*p
= current_group
->head
;
816 while (p
->next
!= current
)
821 } else { /* append at the end */
822 struct hotlist
*p
= current_group
->head
;
830 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&&
831 type
!= HL_TYPE_DOTDOT
) {
832 if (type
== HL_TYPE_GROUP
) {
833 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
835 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
838 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
839 listbox_select_entry (l_hotlist
, l_hotlist
->current
);
847 * Support routine for add_new_entry_input()/add_new_group_input()
848 * Change positions of buttons (first three widgets).
850 * This is just a quick hack. Accurate procedure must take care of
851 * internationalized label lengths and total buttonbar length...assume
852 * 64 is longer anyway.
855 add_widgets_i18n (QuickWidget
* qw
, int len
)
857 int i
, l
[3], space
, cur_x
;
859 for (i
= 0; i
< 3; i
++) {
860 qw
[i
].u
.button
.text
= _(qw
[i
].u
.button
.text
);
861 l
[i
] = str_term_width1 (qw
[i
].u
.button
.text
) + 3;
863 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
865 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
) {
866 qw
[i
].relative_x
= cur_x
;
867 qw
[i
].x_divisions
= len
;
870 #endif /* ENABLE_NLS */
873 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
874 const char *help
, char **r1
, char **r2
)
876 #define RELATIVE_Y_BUTTONS 4
877 #define RELATIVE_Y_LABEL_PTH 3
878 #define RELATIVE_Y_INPUT_PTH 4
880 QuickWidget quick_widgets
[] =
882 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), B_CANCEL
, NULL
),
883 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), B_INSERT
, NULL
),
884 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), B_APPEND
, NULL
),
885 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH
, 0, *r2
, 58, 2, "input-pth", r2
),
886 /* 4 */ QUICK_LABEL (RELATIVE_Y_LABEL_PTH
, 80, 3, 0, text2
),
887 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1
, 58, 0, "input-lbl", r1
),
888 /* 6 */ QUICK_LABEL (3, 80, 2, 0, text1
),
898 static gboolean i18n_flag
= FALSE
;
899 #endif /* ENABLE_NLS */
901 msglen(text1
, &lines1
, &cols1
);
902 msglen(text2
, &lines2
, &cols2
);
903 len
= max (str_term_width1 (header
), cols1
);
904 len
= max (max (len
, cols2
) + 4, 64);
908 add_widgets_i18n (quick_widgets
, len
);
911 #endif /* ENABLE_NLS */
914 QuickDialog Quick_input
=
916 len
, lines1
+ lines2
+ 7, -1, -1, header
,
917 help
, quick_widgets
, FALSE
920 for (i
= 0; i
< 7; i
++)
921 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
923 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
924 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
925 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
926 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
927 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
929 i
= quick_dialog (&Quick_input
);
932 return (i
!= B_CANCEL
) ? i
: 0;
934 #undef RELATIVE_Y_BUTTONS
935 #undef RELATIVE_Y_LABEL_PTH
936 #undef RELATIVE_Y_INPUT_PTH
939 static void add_new_entry_cmd (void)
941 char *title
, *url
, *to_free
;
944 /* Take current directory as default value for input fields */
945 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
947 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label"),
948 _("Directory path"), "[Hotlist]", &title
, &url
);
953 if (!title
|| !*title
|| !url
|| !*url
) {
959 if (ret
== B_ENTER
|| ret
== B_APPEND
)
960 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_AFTER_CURRENT
);
962 add2hotlist (title
, url
, HL_TYPE_ENTRY
, HL_BEFORE_CURRENT
);
964 hotlist_state
.modified
= 1;
968 add_new_group_input (const char *header
, const char *label
, char **result
)
970 QuickWidget quick_widgets
[] =
972 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
973 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT
, NULL
),
974 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND
, NULL
),
975 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input" , result
),
976 /* 4 */ QUICK_LABEL (3, 80, 2, 0, label
),
986 static gboolean i18n_flag
= FALSE
;
987 #endif /* ENABLE_NLS */
989 msglen (label
, &lines
, &cols
);
990 len
= max (max (str_term_width1 (header
), cols
) + 4, 64);
994 add_widgets_i18n (quick_widgets
, len
);
997 #endif /* ENABLE_NLS */
1000 QuickDialog Quick_input
=
1002 len
, lines
+ 6, -1, -1, header
,
1003 "[Hotlist]", quick_widgets
, FALSE
1006 int relative_y
[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
1007 quick_widgets variable above */
1009 for (i
= 0; i
< 5; i
++)
1010 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
1012 for (i
= 0; i
< 4; i
++)
1013 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
1015 ret
= quick_dialog (&Quick_input
);
1018 return (ret
!= B_CANCEL
) ? ret
: 0;
1021 static void add_new_group_cmd (void)
1026 ret
= add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label
);
1027 if (!ret
|| !label
|| !*label
)
1030 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1031 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_AFTER_CURRENT
);
1033 add2hotlist (label
, 0, HL_TYPE_GROUP
, HL_BEFORE_CURRENT
);
1035 hotlist_state
.modified
= 1;
1038 void add2hotlist_cmd (void)
1040 char *prompt
, *label
;
1041 const char *cp
= _("Label for \"%s\":");
1042 int l
= str_term_width1 (cp
);
1043 char *label_string
= g_strdup (current_panel
->cwd
);
1045 strip_password (label_string
, 1);
1047 prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
-2*UX
-(l
+8)));
1048 label
= input_dialog (_(" Add to hotlist "), prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1051 if (!label
|| !*label
) {
1052 g_free (label_string
);
1056 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, 0);
1057 hotlist_state
.modified
= 1;
1060 static void remove_group (struct hotlist
*grp
)
1062 struct hotlist
*current
= grp
->head
;
1065 struct hotlist
*next
= current
->next
;
1067 if (current
->type
== HL_TYPE_GROUP
)
1068 remove_group (current
);
1070 g_free (current
->label
);
1071 g_free (current
->directory
);
1079 static void remove_from_hotlist (struct hotlist
*entry
)
1081 if (entry
->type
== HL_TYPE_DOTDOT
)
1084 if (confirm_directory_hotlist_delete
) {
1088 title
= g_strconcat (_(" Remove: "),
1089 str_trunc (entry
->label
, 30),
1095 result
= query_dialog (title
,
1096 _("\n Are you sure you want to remove this entry?"),
1097 D_ERROR
, 2, _("&Yes"), _("&No"));
1105 if (entry
->type
== HL_TYPE_GROUP
) {
1110 header
= g_strconcat (_(" Remove: "),
1111 str_trunc (entry
->label
, 30),
1114 result
= query_dialog (header
, _("\n Group not empty.\n Remove it?"),
1116 _("&Yes"), _("&No"));
1123 remove_group (entry
);
1126 unlink_entry (entry
);
1128 g_free (entry
->label
);
1129 g_free (entry
->directory
);
1131 /* now remove list entry from screen */
1132 listbox_remove_current (l_hotlist
, 1);
1133 hotlist_state
.modified
= 1;
1136 char *hotlist_cmd (int vfs_or_hotlist
)
1138 char *target
= NULL
;
1140 hotlist_state
.type
= vfs_or_hotlist
;
1143 init_hotlist (vfs_or_hotlist
);
1145 /* display file info */
1146 tty_setcolor (SELECTED_COLOR
);
1148 hotlist_state
.running
= 1;
1149 run_dlg (hotlist_dlg
);
1150 hotlist_state
.running
= 0;
1153 switch (hotlist_dlg
->ret_value
) {
1158 if (l_hotlist
->current
->data
) {
1159 struct hotlist
*hlp
= (struct hotlist
*) l_hotlist
->current
->data
;
1160 target
= g_strdup (hlp
->directory
);
1162 target
= g_strdup (l_hotlist
->current
->text
);
1171 load_group (struct hotlist
*grp
)
1173 gchar
**profile_keys
, **keys
;
1175 char *group_section
;
1176 struct hotlist
*current
= 0;
1178 group_section
= find_group_section (grp
);
1180 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, group_section
, &len
);
1182 current_group
= grp
;
1184 while (*profile_keys
){
1186 mc_config_get_string(mc_main_config
, group_section
, *profile_keys
, ""),
1187 g_strdup (*profile_keys
),
1192 g_free (group_section
);
1195 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp
->directory
,&len
);
1197 while (*profile_keys
){
1199 mc_config_get_string(mc_main_config
,group_section
,*profile_keys
,""),
1200 g_strdup (*profile_keys
),
1207 for (current
= grp
->head
; current
; current
= current
->next
)
1208 load_group (current
);
1213 #define TKN_STRING 2
1215 #define TKN_ENDGROUP 4
1216 #define TKN_COMMENT 5
1219 #define TKN_UNKNOWN 127
1221 static GString
*tkn_buf
= NULL
;
1223 static char *hotlist_file_name
;
1224 static FILE *hotlist_file
;
1225 static time_t hotlist_file_mtime
;
1227 static int hot_skip_blanks (void)
1231 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1237 static int hot_next_token (void)
1243 if (tkn_buf
== NULL
) tkn_buf
= g_string_new ("");
1244 g_string_set_size(tkn_buf
,0);
1247 c
= hot_skip_blanks ();
1256 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n') {
1257 g_string_append_c (tkn_buf
, c
);
1262 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"') {
1264 if ((c
= getc (hotlist_file
)) == EOF
){
1265 g_string_free (tkn_buf
, TRUE
);
1268 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1276 if ((c
= getc (hotlist_file
)) == EOF
){
1277 g_string_free (tkn_buf
, TRUE
);
1283 /* fall through; it is taken as normal character */
1287 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1288 } while ((c
= fgetc (hotlist_file
)) != EOF
&&
1289 (g_ascii_isalnum (c
) || !isascii (c
)));
1291 ungetc (c
, hotlist_file
);
1293 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1295 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1297 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1299 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1308 #define SKIP_TO_EOL { \
1310 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1313 #define CHECK_TOKEN(_TKN_) \
1314 if ((tkn = hot_next_token ()) != _TKN_) { \
1315 hotlist_state.readonly = 1; \
1316 hotlist_state.file_error = 1; \
1317 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1318 tkn = hot_next_token (); \
1323 hot_load_group (struct hotlist
* grp
)
1326 struct hotlist
*new_grp
;
1329 current_group
= grp
;
1331 while ((tkn
= hot_next_token()) != TKN_ENDGROUP
)
1334 CHECK_TOKEN(TKN_STRING
);
1335 new_grp
= add2hotlist (g_strdup (tkn_buf
->str
), 0, HL_TYPE_GROUP
, 0);
1337 hot_load_group (new_grp
);
1338 current_group
= grp
;
1341 CHECK_TOKEN(TKN_STRING
);
1342 label
= g_strdup (tkn_buf
->str
);
1343 CHECK_TOKEN(TKN_URL
);
1344 CHECK_TOKEN(TKN_STRING
);
1345 url
= g_strdup (tkn_buf
->str
);
1346 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1350 label
= g_strdup (tkn_buf
->str
);
1351 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1354 hotlist_state
.readonly
= 1;
1355 hotlist_state
.file_error
= 1;
1359 /* skip empty lines */
1362 hotlist_state
.readonly
= 1;
1363 hotlist_state
.file_error
= 1;
1371 hot_load_file (struct hotlist
* grp
)
1374 struct hotlist
*new_grp
;
1377 current_group
= grp
;
1379 while ((tkn
= hot_next_token())!= TKN_EOF
)
1382 CHECK_TOKEN(TKN_STRING
);
1383 new_grp
= add2hotlist (g_strdup (tkn_buf
->str
), 0, HL_TYPE_GROUP
, 0);
1385 hot_load_group (new_grp
);
1386 current_group
= grp
;
1389 CHECK_TOKEN(TKN_STRING
);
1390 label
= g_strdup (tkn_buf
->str
);
1391 CHECK_TOKEN(TKN_URL
);
1392 CHECK_TOKEN(TKN_STRING
);
1393 url
= g_strdup (tkn_buf
->str
);
1394 add2hotlist (label
, url
, HL_TYPE_ENTRY
, 0);
1398 label
= g_strdup (tkn_buf
->str
);
1399 add2hotlist (label
, 0, HL_TYPE_COMMENT
, 0);
1402 /* skip empty lines */
1405 hotlist_state
.readonly
= 1;
1406 hotlist_state
.file_error
= 1;
1413 clean_up_hotlist_groups (const char *section
)
1416 gchar
**profile_keys
, **keys
;
1419 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1420 if (mc_config_has_group(mc_main_config
, section
))
1421 mc_config_del_group (mc_main_config
, section
);
1423 if (mc_config_has_group (mc_main_config
, grp_section
)) {
1424 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp_section
,&len
);
1426 while (*profile_keys
) {
1427 clean_up_hotlist_groups (*profile_keys
);
1431 mc_config_del_group (mc_main_config
, grp_section
);
1433 g_free (grp_section
);
1441 int remove_old_list
= 0;
1442 struct stat stat_buf
;
1444 if (hotlist_state
.loaded
) {
1445 stat (hotlist_file_name
, &stat_buf
);
1446 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1452 if (!hotlist_file_name
)
1453 hotlist_file_name
= concat_dir_and_file (home_dir
, HOTLIST_FILENAME
);
1455 hotlist
= new_hotlist ();
1456 hotlist
->type
= HL_TYPE_GROUP
;
1457 hotlist
->label
= g_strdup (_(" Top level group "));
1458 hotlist
->up
= hotlist
;
1462 hotlist
->directory
= g_strdup ("Hotlist");
1464 if ((hotlist_file
= fopen (hotlist_file_name
, "r")) == 0) {
1467 load_group (hotlist
);
1468 hotlist_state
.loaded
= 1;
1470 * just to be sure we got copy
1472 hotlist_state
.modified
= 1;
1473 result
= save_hotlist ();
1474 hotlist_state
.modified
= 0;
1476 remove_old_list
= 1;
1478 message (D_ERROR
, _(" Hotlist Load "),
1479 _("MC was unable to write ~/%s file, your old hotlist entries were not deleted"),
1483 hot_load_file (hotlist
);
1484 fclose (hotlist_file
);
1485 hotlist_state
.loaded
= 1;
1488 if (remove_old_list
) {
1489 clean_up_hotlist_groups ("Hotlist");
1490 mc_config_save_file (mc_main_config
);
1493 stat (hotlist_file_name
, &stat_buf
);
1494 hotlist_file_mtime
= stat_buf
.st_mtime
;
1495 current_group
= hotlist
;
1499 static int list_level
= 0;
1502 hot_save_group (struct hotlist
*grp
)
1504 struct hotlist
*current
= grp
->head
;
1510 for (i = 0; i < n; i++) \
1511 putc (' ', hotlist_file); \
1514 for (;current
; current
= current
->next
)
1515 switch (current
->type
) {
1517 INDENT (list_level
);
1518 fputs ("GROUP \"", hotlist_file
);
1519 for (s
= current
->label
; *s
; s
++) {
1520 if (*s
== '"' || *s
== '\\')
1521 putc ('\\', hotlist_file
);
1522 putc (*s
, hotlist_file
);
1524 fputs ("\"\n", hotlist_file
);
1526 hot_save_group (current
);
1528 INDENT (list_level
);
1529 fputs ("ENDGROUP\n", hotlist_file
);
1533 fputs ("ENTRY \"", hotlist_file
);
1534 for (s
= current
->label
; *s
; s
++) {
1535 if (*s
== '"' || *s
== '\\')
1536 putc ('\\', hotlist_file
);
1537 putc (*s
, hotlist_file
);
1539 fputs ("\" URL \"", hotlist_file
);
1540 for (s
= current
->directory
; *s
; s
++) {
1541 if (*s
== '"' || *s
== '\\')
1542 putc ('\\', hotlist_file
);
1543 putc (*s
, hotlist_file
);
1545 fputs ("\"\n", hotlist_file
);
1547 case HL_TYPE_COMMENT
:
1548 fprintf (hotlist_file
, "#%s\n", current
->label
);
1550 case HL_TYPE_DOTDOT
:
1556 int save_hotlist (void)
1559 struct stat stat_buf
;
1561 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
) {
1562 char *fbak
= g_strconcat (hotlist_file_name
, ".bak", (char *) NULL
);
1564 rename (hotlist_file_name
, fbak
);
1565 if ((hotlist_file
= fopen (hotlist_file_name
, "w")) != 0) {
1566 if (stat (fbak
, &stat_buf
) == 0)
1567 chmod (hotlist_file_name
, stat_buf
.st_mode
);
1569 chmod (hotlist_file_name
, S_IRUSR
| S_IWUSR
);
1570 hot_save_group (hotlist
);
1571 fclose (hotlist_file
);
1572 stat (hotlist_file_name
, &stat_buf
);
1573 hotlist_file_mtime
= stat_buf
.st_mtime
;
1575 hotlist_state
.modified
= 0;
1577 rename (fbak
, hotlist_file_name
);
1585 * Unload list from memory.
1586 * Don't confuse with hotlist_done() for GUI.
1588 void done_hotlist (void)
1591 remove_group (hotlist
);
1592 g_free (hotlist
->label
);
1593 g_free (hotlist
->directory
);
1598 hotlist_state
.loaded
= 0;
1600 g_free (hotlist_file_name
);
1601 hotlist_file_name
= 0;
1606 g_string_free (tkn_buf
, TRUE
);
1612 add_dotdot_to_list (void)
1614 if (current_group
!= hotlist
) {
1615 if (hotlist_has_dot_dot
!= 0)
1616 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, 0);