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>
43 #include "lib/global.h"
45 #include "lib/tty/tty.h" /* COLS */
46 #include "lib/tty/key.h" /* KEY_M_CTRL */
47 #include "lib/skin.h" /* colors */
48 #include "lib/mcconfig.h" /* Load/save directories hotlist */
49 #include "lib/fileloc.h"
50 #include "lib/strutil.h"
51 #include "lib/vfs/mc-vfs/vfs.h"
53 #include "lib/widget.h"
55 #include "setup.h" /* For profile_bname */
56 #include "panel.h" /* current_panel */
57 #include "main.h" /* update_panels() */
58 #include "layout.h" /* repaint_screen() */
60 #include "command.h" /* cmdline */
63 /*** global variables ****************************************************************************/
65 int hotlist_has_dot_dot
= 1;
67 /*** file scope macro definitions ****************************************************************/
73 #define BY (LINES - 6)
75 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
77 #define B_ADD_CURRENT B_USER
78 #define B_REMOVE (B_USER + 1)
79 #define B_NEW_GROUP (B_USER + 2)
80 #define B_NEW_ENTRY (B_USER + 3)
81 #define B_UP_GROUP (B_USER + 4)
82 #define B_INSERT (B_USER + 5)
83 #define B_APPEND (B_USER + 6)
84 #define B_MOVE (B_USER + 7)
87 #define B_FREE_ALL_VFS (B_USER + 8)
88 #define B_REFRESH_VFS (B_USER + 9)
91 #define new_hotlist() g_new0(struct hotlist, 1)
93 #define CHECK_BUFFER \
97 i = strlen (current->label); \
98 if (i + 3 > buflen) { \
100 buflen = 1024 * (i/1024 + 1); \
101 buf = g_malloc (buflen); \
110 #define TKN_ENDGROUP 4
111 #define TKN_COMMENT 5
114 #define TKN_UNKNOWN 127
116 #define SKIP_TO_EOL \
119 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
122 #define CHECK_TOKEN(_TKN_) \
123 tkn = hot_next_token (); \
126 hotlist_state.readonly = 1; \
127 hotlist_state.file_error = 1; \
128 while (tkn != TKN_EOL && tkn != TKN_EOF) \
129 tkn = hot_next_token (); \
133 /*** file scope type declarations ****************************************************************/
146 * these parameters are intended to be user configurable
148 int expanded
; /* expanded view of all groups at startup */
151 * these reflect run time state
154 int loaded
; /* hotlist is loaded */
155 int readonly
; /* hotlist readonly */
156 int file_error
; /* parse error while reading file */
157 int running
; /* we are running dlg (and have to
159 int moving
; /* we are in moving hotlist currently */
160 int modified
; /* hotlist was modified */
161 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
164 /* Directory hotlist */
167 enum HotListType type
;
170 struct hotlist
*head
;
172 struct hotlist
*next
;
175 /*** file scope variables ************************************************************************/
177 static WListbox
*l_hotlist
;
178 static WListbox
*l_movelist
;
180 static Dlg_head
*hotlist_dlg
;
181 static Dlg_head
*movelist_dlg
;
183 static WLabel
*pname
, *pname_group
, *movelist_group
;
185 static struct _hotlist_but
187 int ret_cmd
, flags
, y
, x
;
190 widget_pos_flags_t pos_flags
;
194 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
195 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"),
196 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
197 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"),
198 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
199 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"),
200 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
201 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &entry"),
202 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
203 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &group"),
204 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
205 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"),
206 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
207 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"),
208 LIST_HOTLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
209 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"),
210 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
212 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"),
213 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
214 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"),
215 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
217 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &to"),
218 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
}
222 static struct hotlist
*hotlist
= NULL
;
224 static struct hotlist
*current_group
;
226 static GString
*tkn_buf
= NULL
;
228 static char *hotlist_file_name
;
229 static FILE *hotlist_file
;
230 static time_t hotlist_file_mtime
;
232 static int list_level
= 0;
234 /*** file scope functions ************************************************************************/
235 /* --------------------------------------------------------------------------------------------- */
237 static void init_movelist (int, struct hotlist
*);
238 static void add_new_group_cmd (void);
239 static void add_new_entry_cmd (void);
240 static void remove_from_hotlist (struct hotlist
*entry
);
241 static void load_hotlist (void);
242 static void add_dotdot_to_list (void);
244 /* --------------------------------------------------------------------------------------------- */
247 hotlist_refresh (Dlg_head
* dlg
)
249 /* TODO: use groupboxes here */
250 common_dialog_repaint (dlg
);
251 tty_setcolor (COLOR_NORMAL
);
252 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10), dlg
->cols
- (UX
* 2), TRUE
);
253 if (!hotlist_state
.moving
)
254 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2), TRUE
);
257 /* --------------------------------------------------------------------------------------------- */
258 /** If current->data is 0, then we are dealing with a VFS pathname */
261 update_path_name (void)
263 const char *text
= "";
265 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
266 Dlg_head
*dlg
= list
->widget
.owner
;
268 if (list
->count
!= 0)
273 listbox_get_current (list
, &ctext
, &cdata
);
278 struct hotlist
*hlp
= (struct hotlist
*) cdata
;
280 if (hlp
->type
== HL_TYPE_ENTRY
|| hlp
->type
== HL_TYPE_DOTDOT
)
281 text
= hlp
->directory
;
282 else if (hlp
->type
== HL_TYPE_GROUP
)
283 text
= _("Subgroup - press ENTER to see list");
286 if (!hotlist_state
.moving
)
287 label_set_text (pname
, str_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
289 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
290 if (!hotlist_state
.moving
)
291 label_set_text (pname_group
, str_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
293 label_set_text (movelist_group
, str_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
299 /* --------------------------------------------------------------------------------------------- */
304 struct hotlist
*current
= current_group
->head
;
305 GString
*buff
= g_string_new ("");
309 switch (current
->type
)
314 g_string_truncate (buff
, 0);
315 g_string_append (buff
, "->");
316 g_string_append (buff
, current
->label
);
317 if (hotlist_state
.moving
)
318 listbox_add_item (l_movelist
, LISTBOX_APPEND_AT_END
, 0, buff
->str
, current
);
320 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, buff
->str
, current
);
325 if (hotlist_state
.moving
)
326 listbox_add_item (l_movelist
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
);
328 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
);
333 current
= current
->next
;
335 g_string_free (buff
, TRUE
);
338 /* --------------------------------------------------------------------------------------------- */
341 unlink_entry (struct hotlist
*entry
)
343 struct hotlist
*current
= current_group
->head
;
345 if (current
== entry
)
346 current_group
->head
= entry
->next
;
349 while (current
&& current
->next
!= entry
)
350 current
= current
->next
;
352 current
->next
= entry
->next
;
354 entry
->next
= entry
->up
= 0;
357 /* --------------------------------------------------------------------------------------------- */
361 add_name_to_list (const char *path
)
363 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, path
, 0);
365 #endif /* !ENABLE_VFS */
367 /* --------------------------------------------------------------------------------------------- */
370 hotlist_button_callback (WButton
* button
, int action
)
378 struct hotlist
*saved
= current_group
;
379 struct hotlist
*item
= NULL
;
380 struct hotlist
*moveto_item
= NULL
;
381 struct hotlist
*moveto_group
= NULL
;
384 if (l_hotlist
->count
== 0)
385 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
387 listbox_get_current (l_hotlist
, NULL
, (void **) &item
);
388 hotlist_state
.moving
= 1;
389 init_movelist (LIST_MOVELIST
, item
);
391 ret
= run_dlg (movelist_dlg
);
393 hotlist_state
.moving
= 0;
394 listbox_get_current (l_movelist
, NULL
, (void **) &moveto_item
);
395 moveto_group
= current_group
;
396 destroy_dlg (movelist_dlg
);
397 current_group
= saved
;
399 return MSG_NOT_HANDLED
;
400 if (moveto_item
== item
)
401 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
402 it hardly changes anything ;) */
404 listbox_remove_current (l_hotlist
);
405 item
->up
= moveto_group
;
406 if (!moveto_group
->head
)
407 moveto_group
->head
= item
;
408 else if (!moveto_item
)
409 { /* we have group with just comments */
410 struct hotlist
*p
= moveto_group
->head
;
417 else if (ret
== B_ENTER
|| ret
== B_APPEND
)
418 if (!moveto_item
->next
)
419 moveto_item
->next
= item
;
422 item
->next
= moveto_item
->next
;
423 moveto_item
->next
= item
;
425 else if (moveto_group
->head
== moveto_item
)
427 moveto_group
->head
= item
;
428 item
->next
= moveto_item
;
432 struct hotlist
*p
= moveto_group
->head
;
434 while (p
->next
!= moveto_item
)
436 item
->next
= p
->next
;
439 listbox_remove_list (l_hotlist
);
442 hotlist_state
.modified
= 1;
443 return MSG_NOT_HANDLED
;
447 struct hotlist
*entry
= NULL
;
448 listbox_get_current (l_hotlist
, NULL
, (void **) &entry
);
449 remove_from_hotlist (entry
);
451 return MSG_NOT_HANDLED
;
454 add_new_group_cmd ();
455 return MSG_NOT_HANDLED
;
459 return MSG_NOT_HANDLED
;
462 add_new_entry_cmd ();
463 return MSG_NOT_HANDLED
;
471 list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
472 listbox_get_current (list
, NULL
, &data
);
477 hlp
= (struct hotlist
*) data
;
479 if (hlp
->type
== HL_TYPE_ENTRY
)
481 if (hlp
->type
!= HL_TYPE_DOTDOT
)
483 listbox_remove_list (list
);
486 return MSG_NOT_HANDLED
;
488 /* Fall through - go up */
490 /* Fall through if list empty - just go up */
494 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
495 listbox_remove_list (list
);
496 current_group
= current_group
->up
;
498 return MSG_NOT_HANDLED
;
507 listbox_remove_list (l_hotlist
);
508 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, home_dir
, 0);
509 vfs_fill_names (add_name_to_list
);
510 return MSG_NOT_HANDLED
;
511 #endif /* ENABLE_VFS */
518 /* --------------------------------------------------------------------------------------------- */
520 static inline cb_ret_t
521 hotlist_handle_key (Dlg_head
* h
, int key
)
525 case KEY_M_CTRL
| '\n':
531 if (hotlist_button_callback (NULL
, B_ENTER
))
533 h
->ret_value
= B_ENTER
;
539 if (hotlist_state
.type
!= LIST_VFSLIST
)
540 return !hotlist_button_callback (NULL
, B_UP_GROUP
);
542 return MSG_NOT_HANDLED
;
545 if (hotlist_state
.moving
)
546 return MSG_NOT_HANDLED
;
549 hotlist_button_callback (NULL
, B_REMOVE
);
556 if (!hotlist_state
.moving
)
560 listbox_get_current (l_hotlist
, NULL
, &ldata
);
564 struct hotlist
*hlp
= (struct hotlist
*) ldata
;
566 if (hlp
->type
== HL_TYPE_ENTRY
)
570 tmp
= g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
571 input_insert (cmdline
, tmp
, FALSE
);
573 h
->ret_value
= B_CANCEL
;
578 return MSG_HANDLED
; /* ignore key */
581 return MSG_NOT_HANDLED
;
585 /* --------------------------------------------------------------------------------------------- */
588 hotlist_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
596 case DLG_UNHANDLED_KEY
:
597 return hotlist_handle_key (h
, parm
);
600 if (hotlist_state
.moving
)
601 dlg_select_widget (l_movelist
);
603 dlg_select_widget (l_hotlist
);
604 /* always stay on hotlist */
608 tty_setcolor (MENU_ENTRY_COLOR
);
613 /* simply call dlg_set_size() with new size */
614 dlg_set_size (h
, LINES
- 2, COLS
- 6);
618 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
622 /* --------------------------------------------------------------------------------------------- */
625 l_call (WListbox
* list
)
627 Dlg_head
*dlg
= list
->widget
.owner
;
629 if (list
->count
!= 0)
633 listbox_get_current (list
, NULL
, &data
);
637 struct hotlist
*hlp
= (struct hotlist
*) data
;
638 if (hlp
->type
== HL_TYPE_ENTRY
)
640 dlg
->ret_value
= B_ENTER
;
646 hotlist_button_callback (NULL
, B_ENTER
);
647 hotlist_callback (dlg
, NULL
, DLG_POST_KEY
, '\n', NULL
);
653 dlg
->ret_value
= B_ENTER
;
659 hotlist_button_callback (NULL
, B_UP_GROUP
);
660 hotlist_callback (dlg
, NULL
, DLG_POST_KEY
, 'u', NULL
);
664 /* --------------------------------------------------------------------------------------------- */
666 * Expands all button names (once) and recalculates button positions.
667 * returns number of columns in the dialog box, which is 10 chars longer
670 * If common width of the window (i.e. in xterm) is less than returned
671 * width - sorry :) (anyway this did not handled in previous version too)
675 init_i18n_stuff (int list_type
, int cols
)
678 static const char *cancel_but
= N_("&Cancel");
681 static int hotlist_i18n_flag
= 0;
683 if (!hotlist_i18n_flag
)
685 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
687 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
689 cancel_but
= _(cancel_but
);
690 hotlist_i18n_flag
= 1;
692 #endif /* ENABLE_NLS */
694 /* Dynamic resizing of buttonbars */
696 int len
[2], count
[2]; /* at most two lines of buttons */
699 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
700 len
[0] = len
[1] = count
[0] = count
[1] = 0;
702 /* Count len of buttonbars, assuming 2 extra space between buttons */
705 if (!(hotlist_but
[i
].type
& list_type
))
708 row
= hotlist_but
[i
].y
;
710 len
[row
] += str_term_width1 (hotlist_but
[i
].text
) + 5;
711 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
717 cols
= max (cols
, max (len
[0], len
[1]));
719 /* arrange buttons */
721 cur_x
[0] = cur_x
[1] = 0;
722 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
725 if (!(hotlist_but
[i
].type
& list_type
))
728 row
= hotlist_but
[i
].y
;
730 if (hotlist_but
[i
].x
!= 0)
732 /* not first int the row */
733 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
734 hotlist_but
[i
].x
= cols
- str_term_width1 (hotlist_but
[i
].text
) - 13;
736 hotlist_but
[i
].x
= cur_x
[row
];
739 cur_x
[row
] += str_term_width1 (hotlist_but
[i
].text
) + 2
740 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
747 /* --------------------------------------------------------------------------------------------- */
750 init_hotlist (int list_type
)
753 const char *title
, *help_node
;
756 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
760 hotlist_state
.expanded
=
761 mc_config_get_int (mc_main_config
, "HotlistConfig", "expanded_view_of_groups", 0);
763 if (list_type
== LIST_VFSLIST
)
765 title
= _("Active VFS directories");
766 help_node
= "[vfshot]"; /* FIXME - no such node */
770 title
= _("Directory hotlist");
771 help_node
= "[Hotlist]";
775 create_dlg (TRUE
, 0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
776 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
778 for (i
= 0; i
< BUTTONS
; i
++)
780 if (hotlist_but
[i
].type
& list_type
)
781 add_widget_autopos (hotlist_dlg
,
782 button_new (BY
+ hotlist_but
[i
].y
,
783 BX
+ hotlist_but
[i
].x
,
784 hotlist_but
[i
].ret_cmd
,
785 hotlist_but
[i
].flags
,
787 hotlist_button_callback
), hotlist_but
[i
].pos_flags
);
790 /* We add the labels.
791 * pname will hold entry's pathname;
792 * pname_group will hold name of current group
794 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
795 add_widget_autopos (hotlist_dlg
, pname
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
);
796 if (!hotlist_state
.moving
)
798 char label_text
[BUF_TINY
];
800 g_snprintf (label_text
, sizeof (label_text
), " %s ", _("Directory path"));
801 add_widget_autopos (hotlist_dlg
,
802 label_new (UY
- 12 + LINES
, UX
+ 2,
803 label_text
), WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
);
805 /* This one holds the displayed pathname */
806 pname_group
= label_new (UY
, UX
+ 2, _("Directory label"));
807 add_widget (hotlist_dlg
, pname_group
);
809 /* get new listbox */
810 l_hotlist
= listbox_new (UY
+ 1, UX
+ 1, LINES
- 14, COLS
- 2 * UX
- 8, FALSE
, l_call
);
812 /* Fill the hotlist with the active VFS or the hotlist */
814 if (list_type
== LIST_VFSLIST
)
816 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, home_dir
, 0);
817 vfs_fill_names (add_name_to_list
);
820 #endif /* !ENABLE_VFS */
823 add_widget_autopos (hotlist_dlg
, l_hotlist
, WPOS_KEEP_ALL
);
824 /* add listbox to the dialogs */
827 /* --------------------------------------------------------------------------------------------- */
830 init_movelist (int list_type
, struct hotlist
*item
)
833 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
834 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
839 create_dlg (TRUE
, 0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
840 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
843 for (i
= 0; i
< BUTTONS
; i
++)
845 if (hotlist_but
[i
].type
& list_type
)
846 add_widget (movelist_dlg
,
847 button_new (BY
- 4 + hotlist_but
[i
].y
,
848 BX
+ hotlist_but
[i
].x
,
849 hotlist_but
[i
].ret_cmd
,
850 hotlist_but
[i
].flags
,
851 hotlist_but
[i
].text
, hotlist_button_callback
));
854 /* We add the labels. We are interested in the last one,
855 * that one will hold the path name label
857 movelist_group
= label_new (UY
, UX
+ 2, _("Directory label"));
858 add_widget (movelist_dlg
, movelist_group
);
859 /* get new listbox */
861 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->lines
- 8,
862 movelist_dlg
->cols
- 2 * UX
- 2, FALSE
, l_call
);
866 add_widget (movelist_dlg
, l_movelist
);
867 /* add listbox to the dialogs */
870 /* --------------------------------------------------------------------------------------------- */
872 * Destroy the list dialog.
873 * Don't confuse with done_hotlist() for the list in memory.
879 destroy_dlg (hotlist_dlg
);
882 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
886 /* --------------------------------------------------------------------------------------------- */
889 find_group_section (struct hotlist
*grp
)
891 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
894 /* --------------------------------------------------------------------------------------------- */
896 static struct hotlist
*
897 add2hotlist (char *label
, char *directory
, enum HotListType type
, listbox_append_t pos
)
900 struct hotlist
*current
= NULL
;
903 * Hotlist is neither loaded nor loading.
904 * Must be called by "Ctrl-x a" before using hotlist.
909 listbox_get_current (l_hotlist
, NULL
, (void **) ¤t
);
911 /* Make sure `..' stays at the top of the list. */
912 if ((current
!= NULL
) && (current
->type
== HL_TYPE_DOTDOT
))
913 pos
= LISTBOX_APPEND_AFTER
;
915 new = new_hotlist ();
919 new->directory
= directory
;
920 new->up
= current_group
;
922 if (type
== HL_TYPE_GROUP
)
925 add_dotdot_to_list ();
926 current_group
= new->up
;
929 if (!current_group
->head
)
931 /* first element in group */
932 current_group
->head
= new;
934 else if (pos
== LISTBOX_APPEND_AFTER
)
936 new->next
= current
->next
;
939 else if (pos
== LISTBOX_APPEND_BEFORE
&& current
== current_group
->head
)
941 /* should be inserted before first item */
943 current_group
->head
= new;
945 else if (pos
== LISTBOX_APPEND_BEFORE
)
947 struct hotlist
*p
= current_group
->head
;
949 while (p
->next
!= current
)
956 { /* append at the end */
957 struct hotlist
*p
= current_group
->head
;
965 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&& type
!= HL_TYPE_DOTDOT
)
967 if (type
== HL_TYPE_GROUP
)
969 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
971 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
975 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
976 listbox_select_entry (l_hotlist
, l_hotlist
->pos
);
982 /* --------------------------------------------------------------------------------------------- */
984 * Support routine for add_new_entry_input()/add_new_group_input()
985 * Change positions of buttons (first three widgets).
987 * This is just a quick hack. Accurate procedure must take care of
988 * internationalized label lengths and total buttonbar length...assume
989 * 64 is longer anyway.
994 add_widgets_i18n (QuickWidget
* qw
, int len
)
996 int i
, l
[3], space
, cur_x
;
998 for (i
= 0; i
< 3; i
++)
1000 qw
[i
].u
.button
.text
= _(qw
[i
].u
.button
.text
);
1001 l
[i
] = str_term_width1 (qw
[i
].u
.button
.text
) + 3;
1003 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
1005 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
1007 qw
[i
].relative_x
= cur_x
;
1008 qw
[i
].x_divisions
= len
;
1011 #endif /* ENABLE_NLS */
1013 /* --------------------------------------------------------------------------------------------- */
1016 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
1017 const char *help
, char **r1
, char **r2
)
1019 #define RELATIVE_Y_BUTTONS 4
1020 #define RELATIVE_Y_LABEL_PTH 3
1021 #define RELATIVE_Y_INPUT_PTH 4
1023 QuickWidget quick_widgets
[] = {
1024 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), B_CANCEL
, NULL
),
1025 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), B_INSERT
, NULL
),
1026 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), B_APPEND
, NULL
),
1027 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH
, 0, *r2
, 58, 2, "input-pth", r2
),
1028 /* 4 */ QUICK_LABEL (4, 80, 3, 0, text2
),
1029 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1
, 58, 0, "input-lbl", r1
),
1030 /* 6 */ QUICK_LABEL (4, 80, 2, 0, text1
),
1040 static gboolean i18n_flag
= FALSE
;
1041 #endif /* ENABLE_NLS */
1043 len
= str_term_width1 (header
);
1044 str_msg_term_size (text1
, &lines1
, &cols1
);
1045 str_msg_term_size (text2
, &lines2
, &cols2
);
1046 len
= max (len
, cols1
);
1047 len
= max (max (len
, cols2
) + 4, 64);
1052 add_widgets_i18n (quick_widgets
, len
);
1055 #endif /* ENABLE_NLS */
1058 QuickDialog Quick_input
= {
1059 len
, lines1
+ lines2
+ 7, -1, -1, header
,
1060 help
, quick_widgets
, NULL
, FALSE
1063 for (i
= 0; i
< 7; i
++)
1064 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
1066 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
1067 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
1068 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
1069 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
1070 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
1072 i
= quick_dialog (&Quick_input
);
1075 return (i
!= B_CANCEL
) ? i
: 0;
1077 #undef RELATIVE_Y_BUTTONS
1078 #undef RELATIVE_Y_LABEL_PTH
1079 #undef RELATIVE_Y_INPUT_PTH
1082 /* --------------------------------------------------------------------------------------------- */
1085 add_new_entry_cmd (void)
1087 char *title
, *url
, *to_free
;
1090 /* Take current directory as default value for input fields */
1091 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
1093 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1094 _("Directory path:"), "[Hotlist]", &title
, &url
);
1099 if (!title
|| !*title
|| !url
|| !*url
)
1106 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1107 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AFTER
);
1109 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_BEFORE
);
1111 hotlist_state
.modified
= 1;
1114 /* --------------------------------------------------------------------------------------------- */
1117 add_new_group_input (const char *header
, const char *label
, char **result
)
1119 QuickWidget quick_widgets
[] = {
1120 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
1121 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT
, NULL
),
1122 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND
, NULL
),
1123 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result
),
1124 /* 4 */ QUICK_LABEL (4, 80, 2, 0, label
),
1134 static gboolean i18n_flag
= FALSE
;
1135 #endif /* ENABLE_NLS */
1137 len
= str_term_width1 (header
);
1138 str_msg_term_size (label
, &lines
, &cols
);
1139 len
= max (max (len
, cols
) + 4, 64);
1144 add_widgets_i18n (quick_widgets
, len
);
1147 #endif /* ENABLE_NLS */
1150 QuickDialog Quick_input
= {
1151 len
, lines
+ 6, -1, -1, header
,
1152 "[Hotlist]", quick_widgets
, NULL
, FALSE
1155 int relative_y
[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1156 quick_widgets variable above */
1158 for (i
= 0; i
< 5; i
++)
1159 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
1161 for (i
= 0; i
< 4; i
++)
1162 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
1164 ret
= quick_dialog (&Quick_input
);
1167 return (ret
!= B_CANCEL
) ? ret
: 0;
1170 /* --------------------------------------------------------------------------------------------- */
1173 add_new_group_cmd (void)
1178 ret
= add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label
);
1179 if (!ret
|| !label
|| !*label
)
1182 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1183 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AFTER
);
1185 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_BEFORE
);
1187 hotlist_state
.modified
= 1;
1190 /* --------------------------------------------------------------------------------------------- */
1193 remove_group (struct hotlist
*grp
)
1195 struct hotlist
*current
= grp
->head
;
1199 struct hotlist
*next
= current
->next
;
1201 if (current
->type
== HL_TYPE_GROUP
)
1202 remove_group (current
);
1204 g_free (current
->label
);
1205 g_free (current
->directory
);
1213 /* --------------------------------------------------------------------------------------------- */
1216 remove_from_hotlist (struct hotlist
*entry
)
1221 if (entry
->type
== HL_TYPE_DOTDOT
)
1224 if (confirm_directory_hotlist_delete
)
1229 title
= g_strconcat (_("Remove:"), " ", str_trunc (entry
->label
, 30), (char *) NULL
);
1233 result
= query_dialog (title
,
1234 _("Are you sure you want to remove this entry?"),
1235 D_ERROR
, 2, _("&Yes"), _("&No"));
1243 if (entry
->type
== HL_TYPE_GROUP
)
1250 header
= g_strconcat (_("Remove:"), " ", str_trunc (entry
->label
, 30), (char *) NULL
);
1251 result
= query_dialog (header
, _("Group not empty.\nRemove it?"),
1252 D_ERROR
, 2, _("&Yes"), _("&No"));
1259 remove_group (entry
);
1262 unlink_entry (entry
);
1264 g_free (entry
->label
);
1265 g_free (entry
->directory
);
1267 /* now remove list entry from screen */
1268 listbox_remove_current (l_hotlist
);
1269 hotlist_state
.modified
= 1;
1272 /* --------------------------------------------------------------------------------------------- */
1275 load_group (struct hotlist
*grp
)
1277 gchar
**profile_keys
, **keys
;
1279 char *group_section
;
1280 struct hotlist
*current
= 0;
1282 group_section
= find_group_section (grp
);
1284 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, group_section
, &len
);
1286 current_group
= grp
;
1288 while (*profile_keys
)
1290 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1291 g_strdup (*profile_keys
), HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1294 g_free (group_section
);
1297 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp
->directory
, &len
);
1299 while (*profile_keys
)
1301 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1302 g_strdup (*profile_keys
), HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1307 for (current
= grp
->head
; current
; current
= current
->next
)
1308 load_group (current
);
1311 /* --------------------------------------------------------------------------------------------- */
1314 hot_skip_blanks (void)
1318 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1324 /* --------------------------------------------------------------------------------------------- */
1327 hot_next_token (void)
1333 if (tkn_buf
== NULL
)
1334 tkn_buf
= g_string_new ("");
1335 g_string_set_size (tkn_buf
, 0);
1338 c
= hot_skip_blanks ();
1348 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n')
1350 g_string_append_c (tkn_buf
, c
);
1355 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"')
1359 c
= getc (hotlist_file
);
1362 g_string_free (tkn_buf
, TRUE
);
1366 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1374 c
= getc (hotlist_file
);
1377 g_string_free (tkn_buf
, TRUE
);
1383 /* fall through; it is taken as normal character */
1388 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1390 while ((c
= fgetc (hotlist_file
)) != EOF
&& (g_ascii_isalnum (c
) || !isascii (c
)));
1392 ungetc (c
, hotlist_file
);
1394 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1396 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1398 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1400 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1409 /* --------------------------------------------------------------------------------------------- */
1412 hot_load_group (struct hotlist
*grp
)
1415 struct hotlist
*new_grp
;
1418 current_group
= grp
;
1420 while ((tkn
= hot_next_token ()) != TKN_ENDGROUP
)
1424 CHECK_TOKEN (TKN_STRING
);
1426 add2hotlist (g_strdup (tkn_buf
->str
), 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1428 hot_load_group (new_grp
);
1429 current_group
= grp
;
1432 CHECK_TOKEN (TKN_STRING
);
1433 label
= g_strdup (tkn_buf
->str
);
1434 CHECK_TOKEN (TKN_URL
);
1435 CHECK_TOKEN (TKN_STRING
);
1436 url
= g_strdup (tkn_buf
->str
);
1437 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1441 label
= g_strdup (tkn_buf
->str
);
1442 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1445 hotlist_state
.readonly
= 1;
1446 hotlist_state
.file_error
= 1;
1450 /* skip empty lines */
1453 hotlist_state
.readonly
= 1;
1454 hotlist_state
.file_error
= 1;
1461 /* --------------------------------------------------------------------------------------------- */
1464 hot_load_file (struct hotlist
*grp
)
1467 struct hotlist
*new_grp
;
1470 current_group
= grp
;
1472 while ((tkn
= hot_next_token ()) != TKN_EOF
)
1476 CHECK_TOKEN (TKN_STRING
);
1478 add2hotlist (g_strdup (tkn_buf
->str
), 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1480 hot_load_group (new_grp
);
1481 current_group
= grp
;
1484 CHECK_TOKEN (TKN_STRING
);
1485 label
= g_strdup (tkn_buf
->str
);
1486 CHECK_TOKEN (TKN_URL
);
1487 CHECK_TOKEN (TKN_STRING
);
1488 url
= g_strdup (tkn_buf
->str
);
1489 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1493 label
= g_strdup (tkn_buf
->str
);
1494 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1497 /* skip empty lines */
1500 hotlist_state
.readonly
= 1;
1501 hotlist_state
.file_error
= 1;
1507 /* --------------------------------------------------------------------------------------------- */
1510 clean_up_hotlist_groups (const char *section
)
1513 gchar
**profile_keys
, **keys
;
1516 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1517 if (mc_config_has_group (mc_main_config
, section
))
1518 mc_config_del_group (mc_main_config
, section
);
1520 if (mc_config_has_group (mc_main_config
, grp_section
))
1522 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp_section
, &len
);
1524 while (*profile_keys
)
1526 clean_up_hotlist_groups (*profile_keys
);
1530 mc_config_del_group (mc_main_config
, grp_section
);
1532 g_free (grp_section
);
1535 /* --------------------------------------------------------------------------------------------- */
1540 int remove_old_list
= 0;
1541 struct stat stat_buf
;
1543 if (hotlist_state
.loaded
)
1545 stat (hotlist_file_name
, &stat_buf
);
1546 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1552 if (!hotlist_file_name
)
1553 hotlist_file_name
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_HOTLIST_FILE
, NULL
);
1555 hotlist
= new_hotlist ();
1556 hotlist
->type
= HL_TYPE_GROUP
;
1557 hotlist
->label
= g_strdup (_("Top level group"));
1558 hotlist
->up
= hotlist
;
1562 hotlist
->directory
= g_strdup ("Hotlist");
1564 hotlist_file
= fopen (hotlist_file_name
, "r");
1565 if (hotlist_file
== NULL
)
1569 load_group (hotlist
);
1570 hotlist_state
.loaded
= 1;
1572 * just to be sure we got copy
1574 hotlist_state
.modified
= 1;
1575 result
= save_hotlist ();
1576 hotlist_state
.modified
= 0;
1578 remove_old_list
= 1;
1580 message (D_ERROR
, _("Hotlist Load"),
1582 ("MC was unable to write ~/%s file,\nyour old hotlist entries were not deleted"),
1583 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE
);
1587 hot_load_file (hotlist
);
1588 fclose (hotlist_file
);
1589 hotlist_state
.loaded
= 1;
1592 if (remove_old_list
)
1594 GError
*error
= NULL
;
1595 clean_up_hotlist_groups ("Hotlist");
1596 if (!mc_config_save_file (mc_main_config
, &error
))
1597 setup_save_config_show_error (mc_main_config
->ini_path
, &error
);
1600 stat (hotlist_file_name
, &stat_buf
);
1601 hotlist_file_mtime
= stat_buf
.st_mtime
;
1602 current_group
= hotlist
;
1605 /* --------------------------------------------------------------------------------------------- */
1608 hot_save_group (struct hotlist
*grp
)
1610 struct hotlist
*current
= grp
->head
;
1616 for (i = 0; i < n; i++) \
1617 putc (' ', hotlist_file); \
1620 for (; current
; current
= current
->next
)
1621 switch (current
->type
)
1624 INDENT (list_level
);
1625 fputs ("GROUP \"", hotlist_file
);
1626 for (s
= current
->label
; *s
; s
++)
1628 if (*s
== '"' || *s
== '\\')
1629 putc ('\\', hotlist_file
);
1630 putc (*s
, hotlist_file
);
1632 fputs ("\"\n", hotlist_file
);
1634 hot_save_group (current
);
1636 INDENT (list_level
);
1637 fputs ("ENDGROUP\n", hotlist_file
);
1640 INDENT (list_level
);
1641 fputs ("ENTRY \"", hotlist_file
);
1642 for (s
= current
->label
; *s
; s
++)
1644 if (*s
== '"' || *s
== '\\')
1645 putc ('\\', hotlist_file
);
1646 putc (*s
, hotlist_file
);
1648 fputs ("\" URL \"", hotlist_file
);
1649 for (s
= current
->directory
; *s
; s
++)
1651 if (*s
== '"' || *s
== '\\')
1652 putc ('\\', hotlist_file
);
1653 putc (*s
, hotlist_file
);
1655 fputs ("\"\n", hotlist_file
);
1657 case HL_TYPE_COMMENT
:
1658 fprintf (hotlist_file
, "#%s\n", current
->label
);
1660 case HL_TYPE_DOTDOT
:
1666 /* --------------------------------------------------------------------------------------------- */
1669 add_dotdot_to_list (void)
1671 if (current_group
!= hotlist
)
1673 if (hotlist_has_dot_dot
!= 0)
1674 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, LISTBOX_APPEND_AT_END
);
1678 /* --------------------------------------------------------------------------------------------- */
1679 /*** public functions ****************************************************************************/
1680 /* --------------------------------------------------------------------------------------------- */
1683 add2hotlist_cmd (void)
1685 char *lc_prompt
, *label
;
1686 const char *cp
= _("Label for \"%s\":");
1687 int l
= str_term_width1 (cp
);
1688 char *label_string
= g_strdup (current_panel
->cwd
);
1690 strip_password (label_string
, 1);
1692 lc_prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
- 2 * UX
- (l
+ 8)));
1693 label
= input_dialog (_("Add to hotlist"), lc_prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1696 if (!label
|| !*label
)
1698 g_free (label_string
);
1702 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1703 hotlist_state
.modified
= 1;
1706 /* --------------------------------------------------------------------------------------------- */
1709 hotlist_cmd (int vfs_or_hotlist
)
1711 char *target
= NULL
;
1713 hotlist_state
.type
= vfs_or_hotlist
;
1716 init_hotlist (vfs_or_hotlist
);
1718 /* display file info */
1719 tty_setcolor (SELECTED_COLOR
);
1721 hotlist_state
.running
= 1;
1722 run_dlg (hotlist_dlg
);
1723 hotlist_state
.running
= 0;
1726 switch (hotlist_dlg
->ret_value
)
1735 struct hotlist
*hlp
= NULL
;
1737 listbox_get_current (l_hotlist
, &text
, (void **) &hlp
);
1738 target
= g_strdup (hlp
!= NULL
? hlp
->directory
: text
);
1747 /* --------------------------------------------------------------------------------------------- */
1753 struct stat stat_buf
;
1755 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
)
1757 mc_util_make_backup_if_possible (hotlist_file_name
, ".bak");
1759 hotlist_file
= fopen (hotlist_file_name
, "w");
1760 if (hotlist_file
!= NULL
)
1762 hot_save_group (hotlist
);
1763 fclose (hotlist_file
);
1764 stat (hotlist_file_name
, &stat_buf
);
1765 hotlist_file_mtime
= stat_buf
.st_mtime
;
1767 hotlist_state
.modified
= 0;
1770 mc_util_restore_from_backup_if_possible (hotlist_file_name
, ".bak");
1776 /* --------------------------------------------------------------------------------------------- */
1778 * Unload list from memory.
1779 * Don't confuse with hotlist_done() for GUI.
1787 remove_group (hotlist
);
1788 g_free (hotlist
->label
);
1789 g_free (hotlist
->directory
);
1794 hotlist_state
.loaded
= 0;
1796 g_free (hotlist_file_name
);
1797 hotlist_file_name
= 0;
1803 g_string_free (tkn_buf
, TRUE
);
1808 /* --------------------------------------------------------------------------------------------- */