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 "src/setup.h" /* For profile_bname */
56 #include "src/history.h"
58 #include "midnight.h" /* current_panel */
59 #include "layout.h" /* repaint_screen() */
60 #include "command.h" /* cmdline */
64 /*** global variables ****************************************************************************/
66 int hotlist_has_dot_dot
= 1;
68 /*** file scope macro definitions ****************************************************************/
74 #define BY (LINES - 6)
76 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
78 #define B_ADD_CURRENT B_USER
79 #define B_REMOVE (B_USER + 1)
80 #define B_NEW_GROUP (B_USER + 2)
81 #define B_NEW_ENTRY (B_USER + 3)
82 #define B_UP_GROUP (B_USER + 4)
83 #define B_INSERT (B_USER + 5)
84 #define B_APPEND (B_USER + 6)
85 #define B_MOVE (B_USER + 7)
88 #define B_FREE_ALL_VFS (B_USER + 8)
89 #define B_REFRESH_VFS (B_USER + 9)
92 #define new_hotlist() g_new0(struct hotlist, 1)
94 #define CHECK_BUFFER \
98 i = strlen (current->label); \
99 if (i + 3 > buflen) { \
101 buflen = 1024 * (i/1024 + 1); \
102 buf = g_malloc (buflen); \
111 #define TKN_ENDGROUP 4
112 #define TKN_COMMENT 5
115 #define TKN_UNKNOWN 127
117 #define SKIP_TO_EOL \
120 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
123 #define CHECK_TOKEN(_TKN_) \
124 tkn = hot_next_token (); \
127 hotlist_state.readonly = 1; \
128 hotlist_state.file_error = 1; \
129 while (tkn != TKN_EOL && tkn != TKN_EOF) \
130 tkn = hot_next_token (); \
134 /*** file scope type declarations ****************************************************************/
147 * these parameters are intended to be user configurable
149 int expanded
; /* expanded view of all groups at startup */
152 * these reflect run time state
155 int loaded
; /* hotlist is loaded */
156 int readonly
; /* hotlist readonly */
157 int file_error
; /* parse error while reading file */
158 int running
; /* we are running dlg (and have to
160 int moving
; /* we are in moving hotlist currently */
161 int modified
; /* hotlist was modified */
162 int type
; /* LIST_HOTLIST || LIST_VFSLIST */
165 /* Directory hotlist */
168 enum HotListType type
;
171 struct hotlist
*head
;
173 struct hotlist
*next
;
176 /*** file scope variables ************************************************************************/
178 static WListbox
*l_hotlist
;
179 static WListbox
*l_movelist
;
181 static Dlg_head
*hotlist_dlg
;
182 static Dlg_head
*movelist_dlg
;
184 static WLabel
*pname
, *pname_group
, *movelist_group
;
186 static struct _hotlist_but
188 int ret_cmd
, flags
, y
, x
;
191 widget_pos_flags_t pos_flags
;
195 { B_MOVE
, NORMAL_BUTTON
, 1, 42, N_("&Move"), LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
196 { B_REMOVE
, NORMAL_BUTTON
, 1, 30, N_("&Remove"),
197 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
198 { B_APPEND
, NORMAL_BUTTON
, 1, 15, N_("&Append"),
199 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
200 { B_INSERT
, NORMAL_BUTTON
, 1, 0, N_("&Insert"),
201 LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
202 { B_NEW_ENTRY
, NORMAL_BUTTON
, 1, 15, N_("New &entry"),
203 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
204 { B_NEW_GROUP
, NORMAL_BUTTON
, 1, 0, N_("New &group"),
205 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
206 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"),
207 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
208 { B_UP_GROUP
, NORMAL_BUTTON
, 0, 42, N_("&Up"),
209 LIST_HOTLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
210 { B_ADD_CURRENT
, NORMAL_BUTTON
, 0, 20, N_("&Add current"),
211 LIST_HOTLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
213 { B_REFRESH_VFS
, NORMAL_BUTTON
, 0, 43, N_("&Refresh"),
214 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
215 { B_FREE_ALL_VFS
, NORMAL_BUTTON
, 0, 20, N_("Fr&ee VFSs now"),
216 LIST_VFSLIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
},
218 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Change &to"),
219 LIST_HOTLIST
| LIST_VFSLIST
| LIST_MOVELIST
, WPOS_KEEP_LEFT
| WPOS_KEEP_BOTTOM
}
223 static struct hotlist
*hotlist
= NULL
;
225 static struct hotlist
*current_group
;
227 static GString
*tkn_buf
= NULL
;
229 static char *hotlist_file_name
;
230 static FILE *hotlist_file
;
231 static time_t hotlist_file_mtime
;
233 static int list_level
= 0;
235 /*** file scope functions ************************************************************************/
236 /* --------------------------------------------------------------------------------------------- */
238 static void init_movelist (int, struct hotlist
*);
239 static void add_new_group_cmd (void);
240 static void add_new_entry_cmd (void);
241 static void remove_from_hotlist (struct hotlist
*entry
);
242 static void load_hotlist (void);
243 static void add_dotdot_to_list (void);
245 /* --------------------------------------------------------------------------------------------- */
248 hotlist_refresh (Dlg_head
* dlg
)
250 /* TODO: use groupboxes here */
251 common_dialog_repaint (dlg
);
252 tty_setcolor (COLOR_NORMAL
);
253 draw_box (dlg
, 2, 5, dlg
->lines
- (hotlist_state
.moving
? 6 : 10), dlg
->cols
- (UX
* 2), TRUE
);
254 if (!hotlist_state
.moving
)
255 draw_box (dlg
, dlg
->lines
- 8, 5, 3, dlg
->cols
- (UX
* 2), TRUE
);
258 /* --------------------------------------------------------------------------------------------- */
259 /** If current->data is 0, then we are dealing with a VFS pathname */
262 update_path_name (void)
264 const char *text
= "";
266 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
267 Dlg_head
*dlg
= list
->widget
.owner
;
269 if (list
->count
!= 0)
274 listbox_get_current (list
, &ctext
, &cdata
);
279 struct hotlist
*hlp
= (struct hotlist
*) cdata
;
281 if (hlp
->type
== HL_TYPE_ENTRY
|| hlp
->type
== HL_TYPE_DOTDOT
)
282 text
= hlp
->directory
;
283 else if (hlp
->type
== HL_TYPE_GROUP
)
284 text
= _("Subgroup - press ENTER to see list");
287 if (!hotlist_state
.moving
)
288 label_set_text (pname
, str_trunc (text
, dlg
->cols
- (UX
* 2 + 4)));
290 p
= g_strconcat (" ", current_group
->label
, " ", (char *) NULL
);
291 if (!hotlist_state
.moving
)
292 label_set_text (pname_group
, str_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
294 label_set_text (movelist_group
, str_trunc (p
, dlg
->cols
- (UX
* 2 + 4)));
300 /* --------------------------------------------------------------------------------------------- */
305 struct hotlist
*current
= current_group
->head
;
306 GString
*buff
= g_string_new ("");
310 switch (current
->type
)
315 g_string_truncate (buff
, 0);
316 g_string_append (buff
, "->");
317 g_string_append (buff
, current
->label
);
318 if (hotlist_state
.moving
)
319 listbox_add_item (l_movelist
, LISTBOX_APPEND_AT_END
, 0, buff
->str
, current
);
321 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, buff
->str
, current
);
326 if (hotlist_state
.moving
)
327 listbox_add_item (l_movelist
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
);
329 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
);
334 current
= current
->next
;
336 g_string_free (buff
, TRUE
);
339 /* --------------------------------------------------------------------------------------------- */
342 unlink_entry (struct hotlist
*entry
)
344 struct hotlist
*current
= current_group
->head
;
346 if (current
== entry
)
347 current_group
->head
= entry
->next
;
350 while (current
&& current
->next
!= entry
)
351 current
= current
->next
;
353 current
->next
= entry
->next
;
355 entry
->next
= entry
->up
= 0;
358 /* --------------------------------------------------------------------------------------------- */
362 add_name_to_list (const char *path
)
364 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, path
, 0);
366 #endif /* !ENABLE_VFS */
368 /* --------------------------------------------------------------------------------------------- */
371 hotlist_button_callback (WButton
* button
, int action
)
379 struct hotlist
*saved
= current_group
;
380 struct hotlist
*item
= NULL
;
381 struct hotlist
*moveto_item
= NULL
;
382 struct hotlist
*moveto_group
= NULL
;
385 if (l_hotlist
->count
== 0)
386 return MSG_NOT_HANDLED
; /* empty group - nothing to do */
388 listbox_get_current (l_hotlist
, NULL
, (void **) &item
);
389 hotlist_state
.moving
= 1;
390 init_movelist (LIST_MOVELIST
, item
);
392 ret
= run_dlg (movelist_dlg
);
394 hotlist_state
.moving
= 0;
395 listbox_get_current (l_movelist
, NULL
, (void **) &moveto_item
);
396 moveto_group
= current_group
;
397 destroy_dlg (movelist_dlg
);
398 current_group
= saved
;
400 return MSG_NOT_HANDLED
;
401 if (moveto_item
== item
)
402 return MSG_NOT_HANDLED
; /* If we insert/append a before/after a
403 it hardly changes anything ;) */
405 listbox_remove_current (l_hotlist
);
406 item
->up
= moveto_group
;
407 if (!moveto_group
->head
)
408 moveto_group
->head
= item
;
409 else if (!moveto_item
)
410 { /* we have group with just comments */
411 struct hotlist
*p
= moveto_group
->head
;
418 else if (ret
== B_ENTER
|| ret
== B_APPEND
)
419 if (!moveto_item
->next
)
420 moveto_item
->next
= item
;
423 item
->next
= moveto_item
->next
;
424 moveto_item
->next
= item
;
426 else if (moveto_group
->head
== moveto_item
)
428 moveto_group
->head
= item
;
429 item
->next
= moveto_item
;
433 struct hotlist
*p
= moveto_group
->head
;
435 while (p
->next
!= moveto_item
)
437 item
->next
= p
->next
;
440 listbox_remove_list (l_hotlist
);
443 hotlist_state
.modified
= 1;
444 return MSG_NOT_HANDLED
;
448 struct hotlist
*entry
= NULL
;
449 listbox_get_current (l_hotlist
, NULL
, (void **) &entry
);
450 remove_from_hotlist (entry
);
452 return MSG_NOT_HANDLED
;
455 add_new_group_cmd ();
456 return MSG_NOT_HANDLED
;
460 return MSG_NOT_HANDLED
;
463 add_new_entry_cmd ();
464 return MSG_NOT_HANDLED
;
472 list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
473 listbox_get_current (list
, NULL
, &data
);
478 hlp
= (struct hotlist
*) data
;
480 if (hlp
->type
== HL_TYPE_ENTRY
)
482 if (hlp
->type
!= HL_TYPE_DOTDOT
)
484 listbox_remove_list (list
);
487 return MSG_NOT_HANDLED
;
489 /* Fall through - go up */
491 /* Fall through if list empty - just go up */
495 WListbox
*list
= hotlist_state
.moving
? l_movelist
: l_hotlist
;
496 listbox_remove_list (list
);
497 current_group
= current_group
->up
;
499 return MSG_NOT_HANDLED
;
508 listbox_remove_list (l_hotlist
);
509 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), 0);
510 vfs_fill_names (add_name_to_list
);
511 return MSG_NOT_HANDLED
;
512 #endif /* ENABLE_VFS */
519 /* --------------------------------------------------------------------------------------------- */
521 static inline cb_ret_t
522 hotlist_handle_key (Dlg_head
* h
, int key
)
526 case KEY_M_CTRL
| '\n':
532 if (hotlist_button_callback (NULL
, B_ENTER
))
534 h
->ret_value
= B_ENTER
;
540 if (hotlist_state
.type
!= LIST_VFSLIST
)
541 return !hotlist_button_callback (NULL
, B_UP_GROUP
);
543 return MSG_NOT_HANDLED
;
546 if (hotlist_state
.moving
)
547 return MSG_NOT_HANDLED
;
550 hotlist_button_callback (NULL
, B_REMOVE
);
557 if (!hotlist_state
.moving
)
561 listbox_get_current (l_hotlist
, NULL
, &ldata
);
565 struct hotlist
*hlp
= (struct hotlist
*) ldata
;
567 if (hlp
->type
== HL_TYPE_ENTRY
)
571 tmp
= g_strconcat ("cd ", hlp
->directory
, (char *) NULL
);
572 input_insert (cmdline
, tmp
, FALSE
);
574 h
->ret_value
= B_CANCEL
;
579 return MSG_HANDLED
; /* ignore key */
582 return MSG_NOT_HANDLED
;
586 /* --------------------------------------------------------------------------------------------- */
589 hotlist_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
597 case DLG_UNHANDLED_KEY
:
598 return hotlist_handle_key (h
, parm
);
601 if (hotlist_state
.moving
)
602 dlg_select_widget (l_movelist
);
604 dlg_select_widget (l_hotlist
);
605 /* always stay on hotlist */
609 tty_setcolor (MENU_ENTRY_COLOR
);
614 /* simply call dlg_set_size() with new size */
615 dlg_set_size (h
, LINES
- 2, COLS
- 6);
619 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
623 /* --------------------------------------------------------------------------------------------- */
626 l_call (WListbox
* list
)
628 Dlg_head
*dlg
= list
->widget
.owner
;
630 if (list
->count
!= 0)
634 listbox_get_current (list
, NULL
, &data
);
638 struct hotlist
*hlp
= (struct hotlist
*) data
;
639 if (hlp
->type
== HL_TYPE_ENTRY
)
641 dlg
->ret_value
= B_ENTER
;
647 hotlist_button_callback (NULL
, B_ENTER
);
648 hotlist_callback (dlg
, NULL
, DLG_POST_KEY
, '\n', NULL
);
654 dlg
->ret_value
= B_ENTER
;
660 hotlist_button_callback (NULL
, B_UP_GROUP
);
661 hotlist_callback (dlg
, NULL
, DLG_POST_KEY
, 'u', NULL
);
665 /* --------------------------------------------------------------------------------------------- */
667 * Expands all button names (once) and recalculates button positions.
668 * returns number of columns in the dialog box, which is 10 chars longer
671 * If common width of the window (i.e. in xterm) is less than returned
672 * width - sorry :) (anyway this did not handled in previous version too)
676 init_i18n_stuff (int list_type
, int cols
)
679 static const char *cancel_but
= N_("&Cancel");
682 static int hotlist_i18n_flag
= 0;
684 if (!hotlist_i18n_flag
)
686 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
688 hotlist_but
[i
].text
= _(hotlist_but
[i
].text
);
690 cancel_but
= _(cancel_but
);
691 hotlist_i18n_flag
= 1;
693 #endif /* ENABLE_NLS */
695 /* Dynamic resizing of buttonbars */
697 int len
[2], count
[2]; /* at most two lines of buttons */
700 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
701 len
[0] = len
[1] = count
[0] = count
[1] = 0;
703 /* Count len of buttonbars, assuming 2 extra space between buttons */
706 if (!(hotlist_but
[i
].type
& list_type
))
709 row
= hotlist_but
[i
].y
;
711 len
[row
] += str_term_width1 (hotlist_but
[i
].text
) + 5;
712 if (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
)
718 cols
= max (cols
, max (len
[0], len
[1]));
720 /* arrange buttons */
722 cur_x
[0] = cur_x
[1] = 0;
723 i
= sizeof (hotlist_but
) / sizeof (hotlist_but
[0]);
726 if (!(hotlist_but
[i
].type
& list_type
))
729 row
= hotlist_but
[i
].y
;
731 if (hotlist_but
[i
].x
!= 0)
733 /* not first int the row */
734 if (!strcmp (hotlist_but
[i
].text
, cancel_but
))
735 hotlist_but
[i
].x
= cols
- str_term_width1 (hotlist_but
[i
].text
) - 13;
737 hotlist_but
[i
].x
= cur_x
[row
];
740 cur_x
[row
] += str_term_width1 (hotlist_but
[i
].text
) + 2
741 + (hotlist_but
[i
].flags
== DEFPUSH_BUTTON
? 5 : 3);
748 /* --------------------------------------------------------------------------------------------- */
751 init_hotlist (int list_type
)
754 const char *title
, *help_node
;
757 hotlist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
761 hotlist_state
.expanded
=
762 mc_config_get_int (mc_main_config
, "HotlistConfig", "expanded_view_of_groups", 0);
764 if (list_type
== LIST_VFSLIST
)
766 title
= _("Active VFS directories");
767 help_node
= "[vfshot]"; /* FIXME - no such node */
771 title
= _("Directory hotlist");
772 help_node
= "[Hotlist]";
776 create_dlg (TRUE
, 0, 0, LINES
- 2, hotlist_cols
, dialog_colors
,
777 hotlist_callback
, help_node
, title
, DLG_CENTER
| DLG_REVERSE
);
779 for (i
= 0; i
< BUTTONS
; i
++)
781 if (hotlist_but
[i
].type
& list_type
)
782 add_widget_autopos (hotlist_dlg
,
783 button_new (BY
+ hotlist_but
[i
].y
,
784 BX
+ hotlist_but
[i
].x
,
785 hotlist_but
[i
].ret_cmd
,
786 hotlist_but
[i
].flags
,
788 hotlist_button_callback
), hotlist_but
[i
].pos_flags
);
791 /* We add the labels.
792 * pname will hold entry's pathname;
793 * pname_group will hold name of current group
795 pname
= label_new (UY
- 11 + LINES
, UX
+ 2, "");
796 add_widget_autopos (hotlist_dlg
, pname
, WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
);
797 if (!hotlist_state
.moving
)
799 char label_text
[BUF_TINY
];
801 g_snprintf (label_text
, sizeof (label_text
), " %s ", _("Directory path"));
802 add_widget_autopos (hotlist_dlg
,
803 label_new (UY
- 12 + LINES
, UX
+ 2,
804 label_text
), WPOS_KEEP_BOTTOM
| WPOS_KEEP_LEFT
);
806 /* This one holds the displayed pathname */
807 pname_group
= label_new (UY
, UX
+ 2, _("Directory label"));
808 add_widget (hotlist_dlg
, pname_group
);
810 /* get new listbox */
811 l_hotlist
= listbox_new (UY
+ 1, UX
+ 1, LINES
- 14, COLS
- 2 * UX
- 8, FALSE
, l_call
);
813 /* Fill the hotlist with the active VFS or the hotlist */
815 if (list_type
== LIST_VFSLIST
)
817 listbox_add_item (l_hotlist
, LISTBOX_APPEND_AT_END
, 0, mc_config_get_home_dir (), 0);
818 vfs_fill_names (add_name_to_list
);
821 #endif /* !ENABLE_VFS */
824 add_widget_autopos (hotlist_dlg
, l_hotlist
, WPOS_KEEP_ALL
);
825 /* add listbox to the dialogs */
828 /* --------------------------------------------------------------------------------------------- */
831 init_movelist (int list_type
, struct hotlist
*item
)
834 char *hdr
= g_strdup_printf (_("Moving %s"), item
->label
);
835 int movelist_cols
= init_i18n_stuff (list_type
, COLS
- 6);
840 create_dlg (TRUE
, 0, 0, LINES
- 6, movelist_cols
, dialog_colors
,
841 hotlist_callback
, "[Hotlist]", hdr
, DLG_CENTER
| DLG_REVERSE
);
844 for (i
= 0; i
< BUTTONS
; i
++)
846 if (hotlist_but
[i
].type
& list_type
)
847 add_widget (movelist_dlg
,
848 button_new (BY
- 4 + hotlist_but
[i
].y
,
849 BX
+ hotlist_but
[i
].x
,
850 hotlist_but
[i
].ret_cmd
,
851 hotlist_but
[i
].flags
,
852 hotlist_but
[i
].text
, hotlist_button_callback
));
855 /* We add the labels. We are interested in the last one,
856 * that one will hold the path name label
858 movelist_group
= label_new (UY
, UX
+ 2, _("Directory label"));
859 add_widget (movelist_dlg
, movelist_group
);
860 /* get new listbox */
862 listbox_new (UY
+ 1, UX
+ 1, movelist_dlg
->lines
- 8,
863 movelist_dlg
->cols
- 2 * UX
- 2, FALSE
, l_call
);
867 add_widget (movelist_dlg
, l_movelist
);
868 /* add listbox to the dialogs */
871 /* --------------------------------------------------------------------------------------------- */
873 * Destroy the list dialog.
874 * Don't confuse with done_hotlist() for the list in memory.
880 destroy_dlg (hotlist_dlg
);
883 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
887 /* --------------------------------------------------------------------------------------------- */
890 find_group_section (struct hotlist
*grp
)
892 return g_strconcat (grp
->directory
, ".Group", (char *) NULL
);
895 /* --------------------------------------------------------------------------------------------- */
897 static struct hotlist
*
898 add2hotlist (char *label
, char *directory
, enum HotListType type
, listbox_append_t pos
)
901 struct hotlist
*current
= NULL
;
904 * Hotlist is neither loaded nor loading.
905 * Must be called by "Ctrl-x a" before using hotlist.
910 listbox_get_current (l_hotlist
, NULL
, (void **) ¤t
);
912 /* Make sure `..' stays at the top of the list. */
913 if ((current
!= NULL
) && (current
->type
== HL_TYPE_DOTDOT
))
914 pos
= LISTBOX_APPEND_AFTER
;
916 new = new_hotlist ();
920 new->directory
= directory
;
921 new->up
= current_group
;
923 if (type
== HL_TYPE_GROUP
)
926 add_dotdot_to_list ();
927 current_group
= new->up
;
930 if (!current_group
->head
)
932 /* first element in group */
933 current_group
->head
= new;
935 else if (pos
== LISTBOX_APPEND_AFTER
)
937 new->next
= current
->next
;
940 else if (pos
== LISTBOX_APPEND_BEFORE
&& current
== current_group
->head
)
942 /* should be inserted before first item */
944 current_group
->head
= new;
946 else if (pos
== LISTBOX_APPEND_BEFORE
)
948 struct hotlist
*p
= current_group
->head
;
950 while (p
->next
!= current
)
957 { /* append at the end */
958 struct hotlist
*p
= current_group
->head
;
966 if (hotlist_state
.running
&& type
!= HL_TYPE_COMMENT
&& type
!= HL_TYPE_DOTDOT
)
968 if (type
== HL_TYPE_GROUP
)
970 char *lbl
= g_strconcat ("->", new->label
, (char *) NULL
);
972 listbox_add_item (l_hotlist
, pos
, 0, lbl
, new);
976 listbox_add_item (l_hotlist
, pos
, 0, new->label
, new);
977 listbox_select_entry (l_hotlist
, l_hotlist
->pos
);
983 /* --------------------------------------------------------------------------------------------- */
985 * Support routine for add_new_entry_input()/add_new_group_input()
986 * Change positions of buttons (first three widgets).
988 * This is just a quick hack. Accurate procedure must take care of
989 * internationalized label lengths and total buttonbar length...assume
990 * 64 is longer anyway.
995 add_widgets_i18n (QuickWidget
* qw
, int len
)
997 int i
, l
[3], space
, cur_x
;
999 for (i
= 0; i
< 3; i
++)
1001 qw
[i
].u
.button
.text
= _(qw
[i
].u
.button
.text
);
1002 l
[i
] = str_term_width1 (qw
[i
].u
.button
.text
) + 3;
1004 space
= (len
- 4 - l
[0] - l
[1] - l
[2]) / 4;
1006 for (cur_x
= 2 + space
, i
= 3; i
--; cur_x
+= l
[i
] + space
)
1008 qw
[i
].relative_x
= cur_x
;
1009 qw
[i
].x_divisions
= len
;
1012 #endif /* ENABLE_NLS */
1014 /* --------------------------------------------------------------------------------------------- */
1017 add_new_entry_input (const char *header
, const char *text1
, const char *text2
,
1018 const char *help
, char **r1
, char **r2
)
1020 #define RELATIVE_Y_BUTTONS 4
1021 #define RELATIVE_Y_LABEL_PTH 3
1022 #define RELATIVE_Y_INPUT_PTH 4
1024 QuickWidget quick_widgets
[] = {
1025 /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Cancel"), B_CANCEL
, NULL
),
1026 /* 1 */ QUICK_BUTTON (30, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Insert"), B_INSERT
, NULL
),
1027 /* 2 */ QUICK_BUTTON (10, 80, RELATIVE_Y_BUTTONS
, 0, N_("&Append"), B_APPEND
, NULL
),
1028 /* 3 */ QUICK_INPUT (4, 80, RELATIVE_Y_INPUT_PTH
, 0, *r2
, 58, 2, "input-pth", r2
),
1029 /* 4 */ QUICK_LABEL (4, 80, 3, 0, text2
),
1030 /* 5 */ QUICK_INPUT (4, 80, 3, 0, *r1
, 58, 0, "input-lbl", r1
),
1031 /* 6 */ QUICK_LABEL (4, 80, 2, 0, text1
),
1041 static gboolean i18n_flag
= FALSE
;
1042 #endif /* ENABLE_NLS */
1044 len
= str_term_width1 (header
);
1045 str_msg_term_size (text1
, &lines1
, &cols1
);
1046 str_msg_term_size (text2
, &lines2
, &cols2
);
1047 len
= max (len
, cols1
);
1048 len
= max (max (len
, cols2
) + 4, 64);
1053 add_widgets_i18n (quick_widgets
, len
);
1056 #endif /* ENABLE_NLS */
1059 QuickDialog Quick_input
= {
1060 len
, lines1
+ lines2
+ 7, -1, -1, header
,
1061 help
, quick_widgets
, NULL
, FALSE
1064 for (i
= 0; i
< 7; i
++)
1065 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
1067 quick_widgets
[0].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
1068 quick_widgets
[1].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
1069 quick_widgets
[2].relative_y
= RELATIVE_Y_BUTTONS
+ (lines1
+ lines2
);
1070 quick_widgets
[3].relative_y
= RELATIVE_Y_INPUT_PTH
+ (lines1
);
1071 quick_widgets
[4].relative_y
= RELATIVE_Y_LABEL_PTH
+ (lines1
);
1073 i
= quick_dialog (&Quick_input
);
1076 return (i
!= B_CANCEL
) ? i
: 0;
1078 #undef RELATIVE_Y_BUTTONS
1079 #undef RELATIVE_Y_LABEL_PTH
1080 #undef RELATIVE_Y_INPUT_PTH
1083 /* --------------------------------------------------------------------------------------------- */
1086 add_new_entry_cmd (void)
1088 char *title
, *url
, *to_free
;
1091 /* Take current directory as default value for input fields */
1092 to_free
= title
= url
= strip_password (g_strdup (current_panel
->cwd
), 1);
1094 ret
= add_new_entry_input (_("New hotlist entry"), _("Directory label:"),
1095 _("Directory path:"), "[Hotlist]", &title
, &url
);
1100 if (!title
|| !*title
|| !url
|| !*url
)
1107 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1108 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AFTER
);
1110 add2hotlist (title
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_BEFORE
);
1112 hotlist_state
.modified
= 1;
1115 /* --------------------------------------------------------------------------------------------- */
1118 add_new_group_input (const char *header
, const char *label
, char **result
)
1120 QuickWidget quick_widgets
[] = {
1121 /* 0 */ QUICK_BUTTON (55, 80, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
1122 /* 1 */ QUICK_BUTTON (30, 80, 1, 0, N_("&Insert"), B_INSERT
, NULL
),
1123 /* 2 */ QUICK_BUTTON (10, 80, 1, 0, N_("&Append"), B_APPEND
, NULL
),
1124 /* 3 */ QUICK_INPUT (4, 80, 0, 0, "", 58, 0, "input", result
),
1125 /* 4 */ QUICK_LABEL (4, 80, 2, 0, label
),
1135 static gboolean i18n_flag
= FALSE
;
1136 #endif /* ENABLE_NLS */
1138 len
= str_term_width1 (header
);
1139 str_msg_term_size (label
, &lines
, &cols
);
1140 len
= max (max (len
, cols
) + 4, 64);
1145 add_widgets_i18n (quick_widgets
, len
);
1148 #endif /* ENABLE_NLS */
1151 QuickDialog Quick_input
= {
1152 len
, lines
+ 6, -1, -1, header
,
1153 "[Hotlist]", quick_widgets
, NULL
, FALSE
1156 int relative_y
[] = { 1, 1, 1, 0, 2 }; /* the relative_x component from the
1157 quick_widgets variable above */
1159 for (i
= 0; i
< 5; i
++)
1160 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
1162 for (i
= 0; i
< 4; i
++)
1163 quick_widgets
[i
].relative_y
= relative_y
[i
] + 2 + lines
;
1165 ret
= quick_dialog (&Quick_input
);
1168 return (ret
!= B_CANCEL
) ? ret
: 0;
1171 /* --------------------------------------------------------------------------------------------- */
1174 add_new_group_cmd (void)
1179 ret
= add_new_group_input (_("New hotlist group"), _("Name of new group:"), &label
);
1180 if (!ret
|| !label
|| !*label
)
1183 if (ret
== B_ENTER
|| ret
== B_APPEND
)
1184 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AFTER
);
1186 add2hotlist (label
, 0, HL_TYPE_GROUP
, LISTBOX_APPEND_BEFORE
);
1188 hotlist_state
.modified
= 1;
1191 /* --------------------------------------------------------------------------------------------- */
1194 remove_group (struct hotlist
*grp
)
1196 struct hotlist
*current
= grp
->head
;
1200 struct hotlist
*next
= current
->next
;
1202 if (current
->type
== HL_TYPE_GROUP
)
1203 remove_group (current
);
1205 g_free (current
->label
);
1206 g_free (current
->directory
);
1214 /* --------------------------------------------------------------------------------------------- */
1217 remove_from_hotlist (struct hotlist
*entry
)
1222 if (entry
->type
== HL_TYPE_DOTDOT
)
1225 if (confirm_directory_hotlist_delete
)
1230 title
= g_strconcat (_("Remove:"), " ", str_trunc (entry
->label
, 30), (char *) NULL
);
1234 result
= query_dialog (title
,
1235 _("Are you sure you want to remove this entry?"),
1236 D_ERROR
, 2, _("&Yes"), _("&No"));
1244 if (entry
->type
== HL_TYPE_GROUP
)
1251 header
= g_strconcat (_("Remove:"), " ", str_trunc (entry
->label
, 30), (char *) NULL
);
1252 result
= query_dialog (header
, _("Group not empty.\nRemove it?"),
1253 D_ERROR
, 2, _("&Yes"), _("&No"));
1260 remove_group (entry
);
1263 unlink_entry (entry
);
1265 g_free (entry
->label
);
1266 g_free (entry
->directory
);
1268 /* now remove list entry from screen */
1269 listbox_remove_current (l_hotlist
);
1270 hotlist_state
.modified
= 1;
1273 /* --------------------------------------------------------------------------------------------- */
1276 load_group (struct hotlist
*grp
)
1278 gchar
**profile_keys
, **keys
;
1280 char *group_section
;
1281 struct hotlist
*current
= 0;
1283 group_section
= find_group_section (grp
);
1285 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, group_section
, &len
);
1287 current_group
= grp
;
1289 while (*profile_keys
)
1291 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1292 g_strdup (*profile_keys
), HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1295 g_free (group_section
);
1298 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp
->directory
, &len
);
1300 while (*profile_keys
)
1302 add2hotlist (mc_config_get_string (mc_main_config
, group_section
, *profile_keys
, ""),
1303 g_strdup (*profile_keys
), HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1308 for (current
= grp
->head
; current
; current
= current
->next
)
1309 load_group (current
);
1312 /* --------------------------------------------------------------------------------------------- */
1315 hot_skip_blanks (void)
1319 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n' && g_ascii_isspace (c
))
1325 /* --------------------------------------------------------------------------------------------- */
1328 hot_next_token (void)
1334 if (tkn_buf
== NULL
)
1335 tkn_buf
= g_string_new ("");
1336 g_string_set_size (tkn_buf
, 0);
1339 c
= hot_skip_blanks ();
1349 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '\n')
1351 g_string_append_c (tkn_buf
, c
);
1356 while ((c
= getc (hotlist_file
)) != EOF
&& c
!= '"')
1360 c
= getc (hotlist_file
);
1363 g_string_free (tkn_buf
, TRUE
);
1367 g_string_append_c (tkn_buf
, c
== '\n' ? ' ' : c
);
1375 c
= getc (hotlist_file
);
1378 g_string_free (tkn_buf
, TRUE
);
1384 /* fall through; it is taken as normal character */
1389 g_string_append_c (tkn_buf
, g_ascii_toupper (c
));
1391 while ((c
= fgetc (hotlist_file
)) != EOF
&& (g_ascii_isalnum (c
) || !isascii (c
)));
1393 ungetc (c
, hotlist_file
);
1395 if (strncmp (tkn_buf
->str
, "GROUP", l
) == 0)
1397 else if (strncmp (tkn_buf
->str
, "ENTRY", l
) == 0)
1399 else if (strncmp (tkn_buf
->str
, "ENDGROUP", l
) == 0)
1401 else if (strncmp (tkn_buf
->str
, "URL", l
) == 0)
1410 /* --------------------------------------------------------------------------------------------- */
1413 hot_load_group (struct hotlist
*grp
)
1416 struct hotlist
*new_grp
;
1419 current_group
= grp
;
1421 while ((tkn
= hot_next_token ()) != TKN_ENDGROUP
)
1425 CHECK_TOKEN (TKN_STRING
);
1427 add2hotlist (g_strdup (tkn_buf
->str
), 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1429 hot_load_group (new_grp
);
1430 current_group
= grp
;
1433 CHECK_TOKEN (TKN_STRING
);
1434 label
= g_strdup (tkn_buf
->str
);
1435 CHECK_TOKEN (TKN_URL
);
1436 CHECK_TOKEN (TKN_STRING
);
1437 url
= g_strdup (tkn_buf
->str
);
1438 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1442 label
= g_strdup (tkn_buf
->str
);
1443 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1446 hotlist_state
.readonly
= 1;
1447 hotlist_state
.file_error
= 1;
1451 /* skip empty lines */
1454 hotlist_state
.readonly
= 1;
1455 hotlist_state
.file_error
= 1;
1462 /* --------------------------------------------------------------------------------------------- */
1465 hot_load_file (struct hotlist
*grp
)
1468 struct hotlist
*new_grp
;
1471 current_group
= grp
;
1473 while ((tkn
= hot_next_token ()) != TKN_EOF
)
1477 CHECK_TOKEN (TKN_STRING
);
1479 add2hotlist (g_strdup (tkn_buf
->str
), 0, HL_TYPE_GROUP
, LISTBOX_APPEND_AT_END
);
1481 hot_load_group (new_grp
);
1482 current_group
= grp
;
1485 CHECK_TOKEN (TKN_STRING
);
1486 label
= g_strdup (tkn_buf
->str
);
1487 CHECK_TOKEN (TKN_URL
);
1488 CHECK_TOKEN (TKN_STRING
);
1489 url
= g_strdup (tkn_buf
->str
);
1490 add2hotlist (label
, url
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1494 label
= g_strdup (tkn_buf
->str
);
1495 add2hotlist (label
, 0, HL_TYPE_COMMENT
, LISTBOX_APPEND_AT_END
);
1498 /* skip empty lines */
1501 hotlist_state
.readonly
= 1;
1502 hotlist_state
.file_error
= 1;
1508 /* --------------------------------------------------------------------------------------------- */
1511 clean_up_hotlist_groups (const char *section
)
1514 gchar
**profile_keys
, **keys
;
1517 grp_section
= g_strconcat (section
, ".Group", (char *) NULL
);
1518 if (mc_config_has_group (mc_main_config
, section
))
1519 mc_config_del_group (mc_main_config
, section
);
1521 if (mc_config_has_group (mc_main_config
, grp_section
))
1523 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, grp_section
, &len
);
1525 while (*profile_keys
)
1527 clean_up_hotlist_groups (*profile_keys
);
1531 mc_config_del_group (mc_main_config
, grp_section
);
1533 g_free (grp_section
);
1536 /* --------------------------------------------------------------------------------------------- */
1541 int remove_old_list
= 0;
1542 struct stat stat_buf
;
1544 if (hotlist_state
.loaded
)
1546 stat (hotlist_file_name
, &stat_buf
);
1547 if (hotlist_file_mtime
< stat_buf
.st_mtime
)
1553 if (!hotlist_file_name
)
1554 hotlist_file_name
= g_build_filename (mc_config_get_path (), MC_HOTLIST_FILE
, NULL
);
1556 hotlist
= new_hotlist ();
1557 hotlist
->type
= HL_TYPE_GROUP
;
1558 hotlist
->label
= g_strdup (_("Top level group"));
1559 hotlist
->up
= hotlist
;
1563 hotlist
->directory
= g_strdup ("Hotlist");
1565 hotlist_file
= fopen (hotlist_file_name
, "r");
1566 if (hotlist_file
== NULL
)
1570 load_group (hotlist
);
1571 hotlist_state
.loaded
= 1;
1573 * just to be sure we got copy
1575 hotlist_state
.modified
= 1;
1576 result
= save_hotlist ();
1577 hotlist_state
.modified
= 0;
1579 remove_old_list
= 1;
1581 message (D_ERROR
, _("Hotlist Load"),
1583 ("MC was unable to write ~/%s file,\nyour old hotlist entries were not deleted"),
1584 MC_USERCONF_DIR PATH_SEP_STR MC_HOTLIST_FILE
);
1588 hot_load_file (hotlist
);
1589 fclose (hotlist_file
);
1590 hotlist_state
.loaded
= 1;
1593 if (remove_old_list
)
1595 GError
*error
= NULL
;
1596 clean_up_hotlist_groups ("Hotlist");
1597 if (!mc_config_save_file (mc_main_config
, &error
))
1598 setup_save_config_show_error (mc_main_config
->ini_path
, &error
);
1601 stat (hotlist_file_name
, &stat_buf
);
1602 hotlist_file_mtime
= stat_buf
.st_mtime
;
1603 current_group
= hotlist
;
1606 /* --------------------------------------------------------------------------------------------- */
1609 hot_save_group (struct hotlist
*grp
)
1611 struct hotlist
*current
= grp
->head
;
1617 for (i = 0; i < n; i++) \
1618 putc (' ', hotlist_file); \
1621 for (; current
; current
= current
->next
)
1622 switch (current
->type
)
1625 INDENT (list_level
);
1626 fputs ("GROUP \"", hotlist_file
);
1627 for (s
= current
->label
; *s
; s
++)
1629 if (*s
== '"' || *s
== '\\')
1630 putc ('\\', hotlist_file
);
1631 putc (*s
, hotlist_file
);
1633 fputs ("\"\n", hotlist_file
);
1635 hot_save_group (current
);
1637 INDENT (list_level
);
1638 fputs ("ENDGROUP\n", hotlist_file
);
1641 INDENT (list_level
);
1642 fputs ("ENTRY \"", hotlist_file
);
1643 for (s
= current
->label
; *s
; s
++)
1645 if (*s
== '"' || *s
== '\\')
1646 putc ('\\', hotlist_file
);
1647 putc (*s
, hotlist_file
);
1649 fputs ("\" URL \"", hotlist_file
);
1650 for (s
= current
->directory
; *s
; s
++)
1652 if (*s
== '"' || *s
== '\\')
1653 putc ('\\', hotlist_file
);
1654 putc (*s
, hotlist_file
);
1656 fputs ("\"\n", hotlist_file
);
1658 case HL_TYPE_COMMENT
:
1659 fprintf (hotlist_file
, "#%s\n", current
->label
);
1661 case HL_TYPE_DOTDOT
:
1667 /* --------------------------------------------------------------------------------------------- */
1670 add_dotdot_to_list (void)
1672 if (current_group
!= hotlist
)
1674 if (hotlist_has_dot_dot
!= 0)
1675 add2hotlist (g_strdup (".."), g_strdup (".."), HL_TYPE_DOTDOT
, LISTBOX_APPEND_AT_END
);
1679 /* --------------------------------------------------------------------------------------------- */
1680 /*** public functions ****************************************************************************/
1681 /* --------------------------------------------------------------------------------------------- */
1684 add2hotlist_cmd (void)
1686 char *lc_prompt
, *label
;
1687 const char *cp
= _("Label for \"%s\":");
1688 int l
= str_term_width1 (cp
);
1689 char *label_string
= g_strdup (current_panel
->cwd
);
1691 strip_password (label_string
, 1);
1693 lc_prompt
= g_strdup_printf (cp
, path_trunc (current_panel
->cwd
, COLS
- 2 * UX
- (l
+ 8)));
1694 label
= input_dialog (_("Add to hotlist"), lc_prompt
, MC_HISTORY_HOTLIST_ADD
, label_string
);
1697 if (!label
|| !*label
)
1699 g_free (label_string
);
1703 add2hotlist (label
, label_string
, HL_TYPE_ENTRY
, LISTBOX_APPEND_AT_END
);
1704 hotlist_state
.modified
= 1;
1707 /* --------------------------------------------------------------------------------------------- */
1710 hotlist_cmd (int vfs_or_hotlist
)
1712 char *target
= NULL
;
1714 hotlist_state
.type
= vfs_or_hotlist
;
1717 init_hotlist (vfs_or_hotlist
);
1719 /* display file info */
1720 tty_setcolor (SELECTED_COLOR
);
1722 hotlist_state
.running
= 1;
1723 run_dlg (hotlist_dlg
);
1724 hotlist_state
.running
= 0;
1727 switch (hotlist_dlg
->ret_value
)
1736 struct hotlist
*hlp
= NULL
;
1738 listbox_get_current (l_hotlist
, &text
, (void **) &hlp
);
1739 target
= g_strdup (hlp
!= NULL
? hlp
->directory
: text
);
1748 /* --------------------------------------------------------------------------------------------- */
1754 struct stat stat_buf
;
1756 if (!hotlist_state
.readonly
&& hotlist_state
.modified
&& hotlist_file_name
)
1758 mc_util_make_backup_if_possible (hotlist_file_name
, ".bak");
1760 hotlist_file
= fopen (hotlist_file_name
, "w");
1761 if (hotlist_file
!= NULL
)
1763 hot_save_group (hotlist
);
1764 fclose (hotlist_file
);
1765 stat (hotlist_file_name
, &stat_buf
);
1766 hotlist_file_mtime
= stat_buf
.st_mtime
;
1768 hotlist_state
.modified
= 0;
1771 mc_util_restore_from_backup_if_possible (hotlist_file_name
, ".bak");
1777 /* --------------------------------------------------------------------------------------------- */
1779 * Unload list from memory.
1780 * Don't confuse with hotlist_done() for GUI.
1788 remove_group (hotlist
);
1789 g_free (hotlist
->label
);
1790 g_free (hotlist
->directory
);
1795 hotlist_state
.loaded
= 0;
1797 g_free (hotlist_file_name
);
1798 hotlist_file_name
= 0;
1804 g_string_free (tkn_buf
, TRUE
);
1809 /* --------------------------------------------------------------------------------------------- */