Last fuzzy updated...
[midnight-commander.git] / src / hotlist.c
blobd80967744253b4b0f6c3f9a41fd063667485ae5b
1 /* Directory hotlist -- for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997 the Free Software Foundation.
4 Written by:
5 1994 Radek Doulik
6 1995 Janne Kukonlehto
7 1996 Andrej Borsenkow
8 1997 Norbert Warmuth
10 Janne did the original Hotlist code, Andrej made the groupable
11 hotlist; the move hotlist and revamped the file format and made
12 it stronger.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <config.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <string.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
39 #include "global.h"
40 #include "tty.h" /* COLS */
41 #include "color.h" /* dialog_colors */
42 #include "dlg.h"
43 #include "widget.h"
44 #include "dialog.h" /* do_refresh() */
45 #include "setup.h" /* For profile_bname */
46 #include "profile.h" /* Load/save directories hotlist */
47 #include "wtools.h" /* QuickDialog */
48 #include "panel.h" /* cpanel */
49 #include "main.h" /* repaint_screen */
50 #include "hotlist.h"
51 #include "key.h" /* ctrl_pressed */
52 #include "command.h" /* cmdline */
54 #define UX 5
55 #define UY 2
57 #define BX UX
58 #define BY LINES-6
60 #define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but))
61 #define LABELS 3
62 #define B_ADD_CURRENT B_USER
63 #define B_REMOVE (B_USER + 1)
64 #define B_NEW_GROUP (B_USER + 2)
65 #define B_NEW_ENTRY (B_USER + 3)
66 #define B_UP_GROUP (B_USER + 4)
67 #define B_INSERT (B_USER + 5)
68 #define B_APPEND (B_USER + 6)
69 #define B_MOVE (B_USER + 7)
71 static WListbox *l_hotlist;
72 static WListbox *l_movelist;
74 static Dlg_head *hotlist_dlg;
75 static Dlg_head *movelist_dlg;
77 static WLabel *pname, *pname_group, *movelist_group;
79 enum HotListType {
80 HL_TYPE_GROUP,
81 HL_TYPE_ENTRY,
82 HL_TYPE_COMMENT
85 static struct {
87 * these parameters are intended to be user configurable
89 int expanded; /* expanded view of all groups at startup */
92 * these reflect run time state
95 int loaded; /* hotlist is loaded */
96 int readonly; /* hotlist readonly */
97 int file_error; /* parse error while reading file */
98 int running; /* we are running dlg (and have to
99 update listbox */
100 int moving; /* we are in moving hotlist currently */
101 int modified; /* hotlist was modified */
102 int type; /* LIST_HOTLIST || LIST_VFSLIST */
103 } hotlist_state;
105 static struct _hotlist_but {
106 int ret_cmd, flags, y, x;
107 char *text;
108 char *tkname;
109 int type;
110 } hotlist_but[] = {
111 { B_MOVE, NORMAL_BUTTON, 1, 42, N_("&Move"), "move", LIST_HOTLIST},
112 { B_REMOVE, NORMAL_BUTTON, 1, 30, N_("&Remove"), "r", LIST_HOTLIST},
113 { B_APPEND, NORMAL_BUTTON, 1, 15, N_("&Append"), "e", LIST_MOVELIST},
114 { B_INSERT, NORMAL_BUTTON, 1, 0, N_("&Insert"), "g", LIST_MOVELIST},
115 { B_NEW_ENTRY, NORMAL_BUTTON, 1, 15, N_("New &Entry"), "e", LIST_HOTLIST},
116 { B_NEW_GROUP, NORMAL_BUTTON, 1, 0, N_("New &Group"), "g", LIST_HOTLIST},
117 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"), "cc", LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
118 { B_UP_GROUP, NORMAL_BUTTON, 0, 42, N_("&Up"), "up", LIST_HOTLIST|LIST_MOVELIST},
119 { B_ADD_CURRENT, NORMAL_BUTTON, 0, 20, N_("&Add current"),"ad", LIST_HOTLIST},
120 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Change &To"), "ct", LIST_HOTLIST|LIST_VFSLIST|LIST_MOVELIST},
123 /* Directory hotlist */
124 static struct hotlist{
125 enum HotListType type;
126 char *directory;
127 char *label;
128 struct hotlist *head;
129 struct hotlist *up;
130 struct hotlist *next;
131 } *hotlist = NULL;
133 static struct hotlist *current_group;
135 static void init_movelist (int, struct hotlist *);
136 static void add_new_group_cmd (void);
137 static void add_new_entry_cmd (void);
138 static void remove_from_hotlist (struct hotlist *entry);
140 #define new_hotlist() g_new0(struct hotlist, 1)
142 static void
143 hotlist_refresh (Dlg_head * dlg)
145 common_dialog_repaint (dlg);
146 attrset (COLOR_NORMAL);
147 draw_box (dlg, 2, 5, dlg->lines - (hotlist_state.moving ? 6 : 10),
148 dlg->cols - (UX * 2));
149 if (!hotlist_state.moving)
150 draw_box (dlg, dlg->lines - 8, 5, 3, dlg->cols - (UX * 2));
153 /* If current->data is 0, then we are dealing with a VFS pathname */
154 static inline void update_path_name (void)
156 char *text, *p;
157 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
158 Dlg_head *dlg = hotlist_state.moving ? movelist_dlg : hotlist_dlg;
160 if (list->current){
161 if (list->current->data != 0) {
162 struct hotlist *hlp = (struct hotlist *)list->current->data;
164 if (hlp->type == HL_TYPE_ENTRY)
165 text = hlp->directory;
166 else
167 text = _("Subgroup - press ENTER to see list");
169 p = g_strconcat (" ", current_group->label, " ", NULL);
170 if (!hotlist_state.moving)
171 label_set_text (pname_group, name_trunc (p, dlg->cols - (UX*2+4)));
172 else
173 label_set_text (movelist_group, name_trunc (p, dlg->cols - (UX*2+4)));
174 g_free (p);
175 } else {
176 text = list->current->text;
178 } else {
179 text = "";
181 if (!hotlist_state.moving)
182 label_set_text (pname, name_trunc (text, dlg->cols - (UX*2+4)));
183 dlg_redraw (dlg);
186 #define CHECK_BUFFER \
187 do { \
188 int i; \
190 if ((i = strlen (current->label) + 3) > buflen) { \
191 g_free (buf); \
192 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
194 buf[0] = '\0'; \
195 } while (0)
197 static void fill_listbox (void)
199 struct hotlist *current = current_group->head;
200 static char *buf;
201 static int buflen;
203 if (!buf)
204 buf = g_malloc (buflen = 1024);
205 buf[0] = '\0';
207 while (current){
208 switch (current->type) {
209 case HL_TYPE_GROUP:
211 CHECK_BUFFER;
212 strcat (strcat (buf, "->"), current->label);
213 if (hotlist_state.moving)
214 listbox_add_item (l_movelist, 0, 0, buf, current);
215 else
216 listbox_add_item (l_hotlist, 0, 0, buf, current);
218 break;
219 case HL_TYPE_ENTRY:
220 if (hotlist_state.moving)
221 listbox_add_item (l_movelist, 0, 0, current->label, current);
222 else
223 listbox_add_item (l_hotlist, 0, 0, current->label, current);
224 break;
225 default:
226 break;
228 current = current->next;
232 #undef CHECK_BUFFER
234 static void
235 unlink_entry (struct hotlist *entry)
237 struct hotlist *current = current_group->head;
239 if (current == entry)
240 current_group->head = entry->next;
241 else
242 while (current && current->next != entry)
243 current = current->next;
244 if (current)
245 current->next = entry->next;
246 entry->next =
247 entry->up = 0;
250 static int hotlist_button_callback (int action, void *data)
252 switch (action) {
253 case B_MOVE:
255 struct hotlist *saved = current_group;
256 struct hotlist *item;
257 struct hotlist *moveto_item = 0;
258 struct hotlist *moveto_group = 0;
259 int ret;
261 if (!l_hotlist->current)
262 return 0; /* empty group - nothing to do */
263 item = l_hotlist->current->data;
264 hotlist_state.moving = 1;
265 init_movelist (LIST_MOVELIST, item);
266 run_dlg (movelist_dlg);
267 ret = movelist_dlg->ret_value;
268 hotlist_state.moving = 0;
269 if (l_movelist->current)
270 moveto_item = l_movelist->current->data;
271 moveto_group = current_group;
272 destroy_dlg (movelist_dlg);
273 current_group = saved;
274 if (ret == B_CANCEL)
275 return 0;
276 if (moveto_item == item)
277 return 0; /* If we insert/append a before/after a
278 it hardly changes anything ;) */
279 unlink_entry (item);
280 listbox_remove_current (l_hotlist, 1);
281 item->up = moveto_group;
282 if (!moveto_group->head)
283 moveto_group->head = item;
284 else if (!moveto_item) { /* we have group with just comments */
285 struct hotlist *p = moveto_group->head;
287 /* skip comments */
288 while (p->next)
289 p = p->next;
290 p->next = item;
291 } else if (ret == B_ENTER || ret == B_APPEND)
292 if (!moveto_item->next)
293 moveto_item->next = item;
294 else {
295 item->next = moveto_item->next;
296 moveto_item->next = item;
298 else if (moveto_group->head == moveto_item) {
299 moveto_group->head = item;
300 item->next = moveto_item;
301 } else {
302 struct hotlist *p = moveto_group->head;
304 while (p->next != moveto_item)
305 p = p->next;
306 item->next = p->next;
307 p->next = item;
309 listbox_remove_list (l_hotlist);
310 fill_listbox ();
311 repaint_screen ();
312 hotlist_state.modified = 1;
313 return 0;
314 break;
316 case B_REMOVE:
317 if (l_hotlist->current && l_hotlist->current->data)
318 remove_from_hotlist (l_hotlist->current->data);
319 return 0;
320 break;
322 case B_NEW_GROUP:
323 add_new_group_cmd ();
324 return 0;
325 break;
327 case B_ADD_CURRENT:
328 add2hotlist_cmd ();
329 return 0;
330 break;
332 case B_NEW_ENTRY:
333 add_new_entry_cmd ();
334 return 0;
335 break;
337 case B_ENTER:
339 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
340 if (list->current){
341 if (list->current->data) {
342 struct hotlist *hlp = (struct hotlist*) list->current->data;
343 if (hlp->type == HL_TYPE_ENTRY)
344 return 1;
345 else {
346 listbox_remove_list (list);
347 current_group = hlp;
348 fill_listbox ();
349 return 0;
351 } else
352 return 1;
355 /* Fall through if list empty - just go up */
357 case B_UP_GROUP:
359 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
360 listbox_remove_list (list);
361 current_group = current_group->up;
362 fill_listbox ();
363 return 0;
364 break;
367 default:
368 return 1;
369 break;
374 static int hotlist_callback (Dlg_head * h, int Par, int Msg)
376 switch (Msg) {
377 case DLG_DRAW:
378 hotlist_refresh (h);
379 break;
381 case DLG_UNHANDLED_KEY:
382 switch (Par) {
383 case '\n':
384 if (ctrl_pressed())
385 goto l1;
386 case KEY_ENTER:
387 case KEY_RIGHT:
388 if (hotlist_button_callback (B_ENTER, 0)) {
389 h->ret_value = B_ENTER;
390 dlg_stop (h);
392 return 1;
393 break;
394 case KEY_LEFT:
395 if (hotlist_state.type != LIST_VFSLIST )
396 return !hotlist_button_callback (B_UP_GROUP, 0);
397 else
398 return 0;
399 break;
400 case KEY_DC:
401 if (!hotlist_state.moving) {
402 hotlist_button_callback (B_REMOVE, 0);
403 return 1;
405 break;
407 case ALT('\n'):
408 case ALT('\r'):
409 if (!hotlist_state.moving)
411 if (l_hotlist->current){
412 if (l_hotlist->current->data) {
413 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
414 if (hlp->type == HL_TYPE_ENTRY) {
415 char *tmp = g_strconcat ( "cd ", hlp->directory, NULL);
416 stuff (cmdline, tmp, 0);
417 g_free (tmp);
418 dlg_stop (h);
419 h->ret_value = B_CANCEL;
420 return 1;
425 return 1; /* ignore key */
426 default:
427 return 0;
429 break;
431 case DLG_POST_KEY:
432 if (hotlist_state.moving)
433 dlg_select_widget (movelist_dlg, l_movelist);
434 else
435 dlg_select_widget (hotlist_dlg, l_hotlist);
436 /* always stay on hotlist */
437 /* fall through */
439 case DLG_INIT:
440 attrset (MENU_ENTRY_COLOR);
441 update_path_name ();
442 break;
444 return 0;
447 static int l_call (void *l)
449 WListbox *list = (WListbox *) l;
450 Dlg_head *dlg = hotlist_state.moving ? movelist_dlg : hotlist_dlg;
452 if (list->current){
453 if (list->current->data) {
454 struct hotlist *hlp = (struct hotlist*) list->current->data;
455 if (hlp->type == HL_TYPE_ENTRY) {
456 dlg->ret_value = B_ENTER;
457 dlg_stop (dlg);
458 return listbox_finish;
459 } else {
460 hotlist_button_callback (B_ENTER, (void *)0);
461 hotlist_callback (dlg, '\n', DLG_POST_KEY);
462 return listbox_nothing;
464 } else {
465 dlg->ret_value = B_ENTER;
466 dlg_stop (dlg);
467 return listbox_finish;
471 hotlist_button_callback (B_UP_GROUP, (void *)0);
472 hotlist_callback (dlg, 'u', DLG_POST_KEY);
473 return listbox_nothing;
476 #ifdef USE_VFS
477 static void add_name_to_list (char *path)
479 listbox_add_item (l_hotlist, 0, 0, path, 0);
481 #endif /* !USE_VFS */
484 * Expands all button names (once) and recalculates button positions.
485 * returns number of columns in the dialog box, which is 10 chars longer
486 * then buttonbar.
488 * If common width of the window (i.e. in xterm) is less than returned
489 * width - sorry :) (anyway this did not handled in previous version too)
491 static int
492 init_i18n_stuff(int list_type, int cols)
494 register int i;
495 static char* cancel_but = N_("&Cancel");
497 #ifdef ENABLE_NLS
498 static int hotlist_i18n_flag = 0;
500 if (!hotlist_i18n_flag)
502 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
503 while (i--)
504 hotlist_but [i].text = _(hotlist_but [i].text);
506 cancel_but = _(cancel_but);
507 hotlist_i18n_flag = 1;
509 #endif /* ENABLE_NLS */
511 /* Dynamic resizing of buttonbars */
513 int len[2], count[2]; /* at most two lines of buttons */
514 int cur_x[2], row;
516 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
517 len[0] = len[1] = count[0] = count[1] = 0;
519 /* Count len of buttonbars, assuming 2 extra space between buttons */
520 while (i--)
522 if (! (hotlist_but[i].type & list_type))
523 continue;
525 row = hotlist_but [i].y;
526 ++count [row];
527 len [row] += strlen (hotlist_but [i].text) + 5;
528 if (hotlist_but [i].flags == DEFPUSH_BUTTON)
529 len [row] += 2;
531 len[0] -= 2;
532 len[1] -= 2;
534 cols = max(cols, max(len[0], len[1]));
536 /* arrange buttons */
538 cur_x[0] = cur_x[1] = 0;
539 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
540 while (i--)
542 if (! (hotlist_but[i].type & list_type))
543 continue;
545 row = hotlist_but [i].y;
547 if (hotlist_but [i].x != 0)
549 /* not first int the row */
550 if (!strcmp (hotlist_but [i].text, cancel_but))
551 hotlist_but [i].x =
552 cols - strlen (hotlist_but [i].text) - 13;
553 else
554 hotlist_but [i].x = cur_x [row];
557 cur_x [row] += strlen (hotlist_but [i].text) + 2
558 + (hotlist_but [i].flags == DEFPUSH_BUTTON ? 5 : 3);
562 return cols;
565 static void
566 init_hotlist (int list_type)
568 int i;
569 char *title, *help_node;
570 int hotlist_cols;
572 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
574 do_refresh ();
576 hotlist_state.expanded =
577 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
578 0, profile_name);
580 if (list_type == LIST_VFSLIST) {
581 title = _("Active VFS directories");
582 help_node = "[vfshot]"; /* FIXME - no such node */
583 } else {
584 title = _("Directory hotlist");
585 help_node = "[Hotlist]";
588 hotlist_dlg =
589 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
590 hotlist_callback, help_node, title, DLG_CENTER);
592 for (i = 0; i < BUTTONS; i++) {
593 if (hotlist_but[i].type & list_type)
594 add_widget (hotlist_dlg,
595 button_new (BY + hotlist_but[i].y,
596 BX + hotlist_but[i].x,
597 hotlist_but[i].ret_cmd,
598 hotlist_but[i].flags,
599 hotlist_but[i].text,
600 hotlist_button_callback, 0,
601 hotlist_but[i].tkname));
604 /* We add the labels.
605 * pname will hold entry's pathname;
606 * pname_group will hold name of current group
608 pname = label_new (UY - 11 + LINES, UX + 2, "", "the-lab");
609 add_widget (hotlist_dlg, pname);
610 if (!hotlist_state.moving) {
611 add_widget (hotlist_dlg,
612 label_new (UY - 12 + LINES, UX + 1,
613 _(" Directory path "), NULL));
615 /* This one holds the displayed pathname */
616 pname_group = label_new (UY, UX + 1, _(" Directory label "), NULL);
617 add_widget (hotlist_dlg, pname_group);
619 /* get new listbox */
620 l_hotlist =
621 listbox_new (UY + 1, UX + 1, COLS - 2 * UX - 8, LINES - 14,
622 listbox_cback, l_call, "listbox");
624 /* Fill the hotlist with the active VFS or the hotlist */
625 #ifdef USE_VFS
626 if (list_type == LIST_VFSLIST) {
627 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
628 vfs_fill_names (add_name_to_list);
629 } else
630 #endif /* !USE_VFS */
631 fill_listbox ();
633 add_widget (hotlist_dlg, l_hotlist);
634 /* add listbox to the dialogs */
637 static void
638 init_movelist (int list_type, struct hotlist *item)
640 int i;
641 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
642 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
644 do_refresh ();
646 movelist_dlg =
647 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
648 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER);
649 g_free (hdr);
651 for (i = 0; i < BUTTONS; i++) {
652 if (hotlist_but[i].type & list_type)
653 add_widget (movelist_dlg,
654 button_new (BY - 4 + hotlist_but[i].y,
655 BX + hotlist_but[i].x,
656 hotlist_but[i].ret_cmd,
657 hotlist_but[i].flags,
658 hotlist_but[i].text,
659 hotlist_button_callback, 0,
660 hotlist_but[i].tkname));
663 /* We add the labels. We are interested in the last one,
664 * that one will hold the path name label
666 movelist_group = label_new (UY, UX + 1, _(" Directory label "), NULL);
667 add_widget (movelist_dlg, movelist_group);
668 /* get new listbox */
669 l_movelist =
670 listbox_new (UY + 1, UX + 1, movelist_dlg->cols - 2 * UX - 2,
671 movelist_dlg->lines - 8, listbox_cback, l_call,
672 "listbox");
674 fill_listbox ();
676 add_widget (movelist_dlg, l_movelist);
677 /* add listbox to the dialogs */
681 * Destroy the list dialog.
682 * Don't confuse with done_hotlist() for the list in memory.
684 static void hotlist_done (void)
686 destroy_dlg (hotlist_dlg);
687 l_hotlist = NULL;
688 if (0)
689 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
690 repaint_screen ();
693 static char *
694 find_group_section (struct hotlist *grp)
696 return g_strconcat (grp->directory, ".Group", NULL);
701 /* 1.11.96 bor: added pos parameter to control placement of new item.
702 see widget.c, listbox_add_item()
703 now hotlist is in unsorted mode
705 static struct hotlist *
706 add2hotlist (char *label, char *directory, enum HotListType type, int pos)
708 struct hotlist *new;
709 struct hotlist *current = NULL;
712 * Hotlist is neither loaded nor loading.
713 * Must be called by "Ctrl-x a" before using hotlist.
715 if (!current_group)
716 load_hotlist ();
718 if (l_hotlist && l_hotlist->current)
719 current = l_hotlist->current->data;
721 new = new_hotlist ();
723 new->type = type;
724 new->label = label;
725 new->directory = directory;
726 new->up = current_group;
728 if (!current_group->head) { /* first element in group */
729 current_group->head = new;
730 } else if (pos == 2) { /* should be appended after current*/
731 new->next = current->next;
732 current->next = new;
733 } else if (pos == 1 &&
734 current == current_group->head) {
735 /* should be inserted before first item */
736 new->next = current;
737 current_group->head = new;
738 } else if (pos == 1) { /* befor current */
739 struct hotlist *p = current_group->head;
741 while (p->next != current)
742 p = p->next;
744 new->next = current;
745 p->next = new;
746 } else { /* append at the end */
747 struct hotlist *p = current_group->head;
749 while (p->next)
750 p = p->next;
752 p->next = new;
755 if (hotlist_state.running && type != HL_TYPE_COMMENT) {
756 if (type == HL_TYPE_GROUP) {
757 char *lbl = g_strconcat ("->", new->label, NULL);
759 listbox_add_item (l_hotlist, pos, 0, lbl, new);
760 g_free (lbl);
761 } else
762 listbox_add_item (l_hotlist, pos, 0, new->label, new);
763 listbox_select_entry (l_hotlist, l_hotlist->current);
765 return new;
769 #ifdef ENABLE_NLS
771 * Support routine for add_new_entry_input()/add_new_group_input()
772 * Change positions of buttons (first three widgets).
774 * This is just a quick hack. Accurate procedure must take care of
775 * internationalized label lengths and total buttonbar length...assume
776 * 64 is longer anyway.
778 static void add_widgets_i18n(QuickWidget* qw, int len)
780 int i, l[3], space, cur_x;
782 for (i = 0; i < 3; i++)
784 qw [i].text = _(qw [i].text);
785 l[i] = strlen (qw [i].text) + 3;
787 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
789 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
791 qw [i].relative_x = cur_x;
792 qw [i].x_divisions = len;
795 #endif /* ENABLE_NLS */
797 static int add_new_entry_input (char *header, char *text1, char *text2, char *help, char **r1, char **r2)
799 #define RELATIVE_Y_BUTTONS 4
800 #define RELATIVE_Y_LABEL_PTH 3
801 #define RELATIVE_Y_INPUT_PTH 4
803 QuickDialog Quick_input;
804 static QuickWidget quick_widgets [] = {
805 { quick_button, 55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), 0, B_CANCEL,
806 0, 0, "button-cancel" },
807 { quick_button, 30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), 0, B_INSERT,
808 0, 0, "button-insert" },
809 { quick_button, 10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), 0, B_APPEND,
810 0, 0, "button-append" },
811 { quick_input, 4, 80, RELATIVE_Y_INPUT_PTH, 0, "",58, 0,
812 0, 0, "input-pth" },
813 { quick_label, RELATIVE_Y_LABEL_PTH, 80, 3, 0, 0, 0, 0,
814 0, 0, "label-pth" },
815 { quick_input, 4, 80, 3, 0, "", 58, 0,
816 0, 0, "input-lbl" },
817 { quick_label, 3, 80, 2, 0, 0, 0, 0,
818 0, 0, "label-lbl" },
819 { 0 } };
821 int len;
822 int i;
823 int lines1, lines2;
824 char *my_str1, *my_str2;
826 #ifdef ENABLE_NLS
827 static int i18n_flag = 0;
828 #endif /* ENABLE_NLS */
830 len = max (strlen (header), msglen (text1, &lines1));
831 len = max (len, msglen (text2, &lines2)) + 4;
832 len = max (len, 64);
834 #ifdef ENABLE_NLS
835 if (!i18n_flag)
837 add_widgets_i18n(quick_widgets, len);
838 i18n_flag = 1;
840 #endif /* ENABLE_NLS */
842 Quick_input.xlen = len;
843 Quick_input.xpos = -1;
844 Quick_input.title = header;
845 Quick_input.help = help;
846 Quick_input.i18n = 0;
847 quick_widgets [6].text = text1;
848 quick_widgets [4].text = text2;
849 quick_widgets [5].text = *r1;
850 quick_widgets [3].text = *r2;
852 for (i = 0; i < 7; i++)
853 quick_widgets [i].y_divisions = lines1+lines2+7;
854 Quick_input.ylen = lines1 + lines2 + 7;
856 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
857 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
858 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
859 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
860 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
862 quick_widgets [5].str_result = &my_str1;
863 quick_widgets [3].str_result = &my_str2;
865 Quick_input.widgets = quick_widgets;
866 if ((i = quick_dialog (&Quick_input)) != B_CANCEL){
867 *r1 = *(quick_widgets [5].str_result);
868 *r2 = *(quick_widgets [3].str_result);
869 return i;
870 } else
871 return 0;
874 static void add_new_entry_cmd (void)
876 char *title = 0, *url = 0;
877 int ret;
879 /* Take current directory as default value for input fields */
880 title = url = cpanel->cwd;
882 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
883 "[Hotlist]", &title, &url);
885 if (!ret || !title || !*title || !url || !*url)
886 return;
888 if (ret == B_ENTER || ret == B_APPEND)
889 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 2);
890 else
891 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 1);
893 hotlist_state.modified = 1;
896 static int add_new_group_input (char *header, char *label, char **result)
898 int ret;
899 QuickDialog Quick_input;
900 static QuickWidget quick_widgets [] = {
901 { quick_button, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
902 "button-cancel" },
903 { quick_button, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT, 0, 0,
904 "button-insert" },
905 { quick_button, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND, 0, 0,
906 "button-append" },
907 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
908 { quick_label, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
909 { 0 } };
910 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
911 quick_widgets variable above */
912 int len;
913 int i;
914 int lines;
915 char *my_str;
917 #ifdef ENABLE_NLS
918 static int i18n_flag = 0;
919 #endif /* ENABLE_NLS */
921 len = max (strlen (header), msglen (label, &lines)) + 4;
922 len = max (len, 64);
924 #ifdef ENABLE_NLS
925 if (!i18n_flag)
927 add_widgets_i18n(quick_widgets, len);
928 i18n_flag = 1;
930 #endif /* ENABLE_NLS */
932 Quick_input.xlen = len;
933 Quick_input.xpos = -1;
934 Quick_input.title = header;
935 Quick_input.help = "[Hotlist]";
936 Quick_input.i18n = 0;
937 quick_widgets [4].text = label;
939 for (i = 0; i < 5; i++)
940 quick_widgets [i].y_divisions = lines+6;
941 Quick_input.ylen = lines + 6;
943 for (i = 0; i < 4; i++)
944 quick_widgets [i].relative_y = relative_y[i] + 2 + lines;
946 quick_widgets [3].str_result = &my_str;
947 quick_widgets [3].text = "";
949 Quick_input.widgets = quick_widgets;
950 if ((ret = quick_dialog (&Quick_input)) != B_CANCEL){
951 *result = *(quick_widgets [3].str_result);
952 return ret;
953 } else
954 return 0;
957 static void add_new_group_cmd (void)
959 char *label;
960 int ret;
962 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
963 if (!ret || !label || !*label)
964 return;
966 if (ret == B_ENTER || ret == B_APPEND)
967 add2hotlist (label, 0, HL_TYPE_GROUP, 2);
968 else
969 add2hotlist (label, 0, HL_TYPE_GROUP, 1);
971 hotlist_state.modified = 1;
974 void add2hotlist_cmd (void)
976 char *prompt, *label;
977 char *cp = _("Label for \"%s\":");
978 int l = strlen (cp);
980 prompt = g_strdup_printf (cp, name_trunc (cpanel->cwd, COLS-2*UX-(l+8)));
981 label = input_dialog (_(" Add to hotlist "), prompt, cpanel->cwd);
982 g_free (prompt);
983 if (!label || !*label)
984 return;
986 add2hotlist (label,g_strdup (cpanel->cwd), HL_TYPE_ENTRY, 0);
987 hotlist_state.modified = 1;
990 static void remove_group (struct hotlist *grp)
992 struct hotlist *current = grp->head;
994 while (current) {
995 struct hotlist *next = current->next;
997 if (current->type == HL_TYPE_GROUP)
998 remove_group (current);
1000 if (current->label)
1001 g_free (current->label);
1002 if (current->directory)
1003 g_free (current->directory);
1004 g_free (current);
1006 current = next;
1011 static void remove_from_hotlist (struct hotlist *entry)
1013 if (entry->type == HL_TYPE_GROUP) {
1014 if (entry->head) {
1015 char *header;
1016 int result;
1018 header = g_strconcat (_(" Remove: "),
1019 name_trunc (entry->label, 30),
1020 " ",
1021 NULL);
1022 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1023 D_ERROR, 2,
1024 _("&No"), _("&Yes"));
1025 g_free (header);
1027 if (!result)
1028 return;
1031 remove_group (entry);
1034 unlink_entry (entry);
1036 if (entry->label)
1037 g_free (entry->label);
1038 if (entry->directory)
1039 g_free (entry->directory);
1040 g_free (entry);
1041 /* now remove list entry from screen */
1042 listbox_remove_current (l_hotlist, 1);
1043 hotlist_state.modified = 1;
1046 char *hotlist_cmd (int vfs_or_hotlist)
1048 char *target = NULL;
1050 hotlist_state.type = vfs_or_hotlist;
1051 load_hotlist ();
1053 init_hotlist (vfs_or_hotlist);
1055 /* display file info */
1056 attrset (SELECTED_COLOR);
1058 hotlist_state.running = 1;
1059 run_dlg (hotlist_dlg);
1060 hotlist_state.running = 0;
1061 save_hotlist ();
1063 switch (hotlist_dlg->ret_value) {
1064 case B_CANCEL:
1065 break;
1067 case B_ENTER:
1068 if (l_hotlist->current->data) {
1069 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
1070 target = g_strdup (hlp->directory);
1071 } else
1072 target = g_strdup (l_hotlist->current->text);
1073 break;
1076 hotlist_done ();
1077 return target;
1080 static void
1081 load_group (struct hotlist *grp)
1083 void *profile_keys;
1084 char *key, *value;
1085 char *group_section;
1086 struct hotlist *current = 0;
1088 group_section = find_group_section (grp);
1090 profile_keys = profile_init_iterator (group_section, profile_name);
1092 current_group = grp;
1094 while (profile_keys){
1095 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1096 add2hotlist (g_strdup (value), g_strdup (key), HL_TYPE_GROUP, 0);
1098 g_free (group_section);
1100 profile_keys = profile_init_iterator (grp->directory, profile_name);
1102 while (profile_keys){
1103 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1104 add2hotlist (g_strdup (value),g_strdup (key), HL_TYPE_ENTRY, 0);
1107 for (current = grp->head; current; current = current->next)
1108 load_group (current);
1111 #define TKN_GROUP 0
1112 #define TKN_ENTRY 1
1113 #define TKN_STRING 2
1114 #define TKN_URL 3
1115 #define TKN_ENDGROUP 4
1116 #define TKN_COMMENT 5
1117 #define TKN_EOL 125
1118 #define TKN_EOF 126
1119 #define TKN_UNKNOWN 127
1121 static char *tkn_buf;
1122 static int tkn_buf_length;
1123 static int tkn_length;
1125 static char *hotlist_file_name;
1126 static FILE *hotlist_file;
1127 static time_t hotlist_file_mtime;
1129 static int hot_skip_blanks (void)
1131 int c;
1133 while ((c = getc (hotlist_file)) != EOF && c != '\n' && isspace (c))
1135 return c;
1139 static int hot_next_token (void)
1141 int c;
1143 #define CHECK_BUF() \
1144 do { \
1145 if (tkn_length == tkn_buf_length) \
1146 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1147 : ( g_malloc (tkn_buf_length = 1024)); \
1148 } while (0)
1150 tkn_length = 0;
1152 again:
1153 c = hot_skip_blanks ();
1154 switch (c) {
1155 case EOF:
1156 return TKN_EOF;
1157 break;
1158 case '\n':
1159 return TKN_EOL;
1160 break;
1161 case '#':
1162 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1163 if (c == EOF)
1164 return TKN_EOF;
1165 if (c != '\n') {
1166 CHECK_BUF();
1167 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1170 CHECK_BUF();
1171 tkn_buf[tkn_length] = '\0';
1172 return TKN_COMMENT;
1173 break;
1174 case '"':
1175 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1176 if (c == '\\')
1177 if ((c = getc (hotlist_file)) == EOF)
1178 return TKN_EOF;
1179 CHECK_BUF();
1180 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1182 if (c == EOF)
1183 return TKN_EOF;
1184 CHECK_BUF();
1185 tkn_buf[tkn_length] = '\0';
1186 return TKN_STRING;
1187 break;
1188 case '\\':
1189 if ((c = getc (hotlist_file)) == EOF)
1190 return TKN_EOF;
1191 if (c == '\n')
1192 goto again;
1194 /* fall through; it is taken as normal character */
1196 default:
1197 do {
1198 CHECK_BUF();
1199 tkn_buf[tkn_length++] = toupper(c);
1200 } while ((c = fgetc (hotlist_file)) != EOF && isalnum (c));
1201 if (c != EOF)
1202 ungetc (c, hotlist_file);
1203 CHECK_BUF();
1204 tkn_buf[tkn_length] = '\0';
1205 if (strncmp (tkn_buf, "GROUP", tkn_length) == 0)
1206 return TKN_GROUP;
1207 else if (strncmp (tkn_buf, "ENTRY", tkn_length) == 0)
1208 return TKN_ENTRY;
1209 else if (strncmp (tkn_buf, "ENDGROUP", tkn_length) == 0)
1210 return TKN_ENDGROUP;
1211 else if (strncmp (tkn_buf, "URL", tkn_length) == 0)
1212 return TKN_URL;
1213 else
1214 return TKN_UNKNOWN;
1215 break;
1219 #define SKIP_TO_EOL { \
1220 int _tkn; \
1221 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1224 #define CHECK_TOKEN(_TKN_) \
1225 if ((tkn = hot_next_token ()) != _TKN_) { \
1226 hotlist_state.readonly = 1; \
1227 hotlist_state.file_error = 1; \
1228 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1229 tkn = hot_next_token (); \
1230 break; \
1233 static void
1234 hot_load_group (struct hotlist * grp)
1236 int tkn;
1237 struct hotlist *new_grp;
1238 char *label, *url;
1240 current_group = grp;
1242 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1243 switch (tkn) {
1244 case TKN_GROUP:
1245 CHECK_TOKEN(TKN_STRING);
1246 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1247 SKIP_TO_EOL;
1248 hot_load_group (new_grp);
1249 current_group = grp;
1250 break;
1251 case TKN_ENTRY:
1252 CHECK_TOKEN(TKN_STRING);
1253 label = g_strdup (tkn_buf);
1254 CHECK_TOKEN(TKN_URL);
1255 CHECK_TOKEN(TKN_STRING);
1256 url = g_strdup (tkn_buf);
1257 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1258 SKIP_TO_EOL;
1259 break;
1260 case TKN_COMMENT:
1261 label = g_strdup (tkn_buf);
1262 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1263 break;
1264 case TKN_EOF:
1265 hotlist_state.readonly = 1;
1266 hotlist_state.file_error = 1;
1267 return;
1268 break;
1269 case TKN_EOL:
1270 /* skip empty lines */
1271 break;
1272 default:
1273 hotlist_state.readonly = 1;
1274 hotlist_state.file_error = 1;
1275 SKIP_TO_EOL;
1276 break;
1278 SKIP_TO_EOL;
1281 static void
1282 hot_load_file (struct hotlist * grp)
1284 int tkn;
1285 struct hotlist *new_grp;
1286 char *label, *url;
1288 current_group = grp;
1290 while ((tkn = hot_next_token())!= TKN_EOF)
1291 switch (tkn) {
1292 case TKN_GROUP:
1293 CHECK_TOKEN(TKN_STRING);
1294 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1295 SKIP_TO_EOL;
1296 hot_load_group (new_grp);
1297 current_group = grp;
1298 break;
1299 case TKN_ENTRY:
1300 CHECK_TOKEN(TKN_STRING);
1301 label = g_strdup (tkn_buf);
1302 CHECK_TOKEN(TKN_URL);
1303 CHECK_TOKEN(TKN_STRING);
1304 url = g_strdup (tkn_buf);
1305 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1306 SKIP_TO_EOL;
1307 break;
1308 case TKN_COMMENT:
1309 label = g_strdup (tkn_buf);
1310 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1311 break;
1312 case TKN_EOL:
1313 /* skip empty lines */
1314 break;
1315 default:
1316 hotlist_state.readonly = 1;
1317 hotlist_state.file_error = 1;
1318 SKIP_TO_EOL;
1319 break;
1323 static void
1324 clean_up_hotlist_groups (char *section)
1326 char *grp_section;
1327 void *profile_keys;
1328 char *key, *value;
1330 grp_section = g_strconcat (section, ".Group", NULL);
1331 if (profile_has_section (section, profile_name))
1332 profile_clean_section (section, profile_name);
1333 if (profile_has_section (grp_section, profile_name)) {
1334 profile_keys = profile_init_iterator (grp_section, profile_name);
1336 while (profile_keys) {
1337 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1338 clean_up_hotlist_groups (key);
1340 profile_clean_section (grp_section, profile_name);
1342 g_free (grp_section);
1347 void load_hotlist (void)
1349 int remove_old_list = 0;
1350 struct stat stat_buf;
1352 if (hotlist_state.loaded) {
1353 stat (hotlist_file_name, &stat_buf);
1354 if (hotlist_file_mtime < stat_buf.st_mtime)
1355 done_hotlist ();
1356 else
1357 return;
1360 if (!hotlist_file_name)
1361 hotlist_file_name = concat_dir_and_file (home_dir, HOTLIST_FILENAME);
1363 hotlist = new_hotlist ();
1364 hotlist->type = HL_TYPE_GROUP;
1365 hotlist->label = g_strdup (_(" Top level group "));
1366 hotlist->up = hotlist;
1368 * compatibility :-(
1370 hotlist->directory = g_strdup ("Hotlist");
1372 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1373 int result;
1375 load_group (hotlist);
1376 hotlist_state.loaded = 1;
1378 * just to be sure we got copy
1380 hotlist_state.modified = 1;
1381 result = save_hotlist ();
1382 hotlist_state.modified = 0;
1383 if (result) {
1384 remove_old_list = 1;
1385 } else {
1386 char *msg;
1388 msg = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME,
1389 _(" file, your old hotlist entries were not deleted"), NULL);
1391 message (D_ERROR, _(" Hotlist Load "), msg);
1392 g_free (msg);
1394 } else {
1395 hot_load_file (hotlist);
1396 fclose (hotlist_file);
1397 hotlist_state.loaded = 1;
1400 if (remove_old_list) {
1401 clean_up_hotlist_groups ("Hotlist");
1402 sync_profiles ();
1405 stat (hotlist_file_name, &stat_buf);
1406 hotlist_file_mtime = stat_buf.st_mtime;
1407 current_group = hotlist;
1410 static void
1411 save_group (struct hotlist *grp)
1413 struct hotlist *current = grp->head;
1414 char *group_section;
1416 group_section = find_group_section (grp);
1418 profile_clean_section (group_section, profile_name);
1419 for (;current && current->type == HL_TYPE_GROUP; current = current->next){
1420 WritePrivateProfileString (group_section,
1421 current->directory,
1422 current->label,
1423 profile_name);
1425 g_free (group_section);
1427 for (current = grp->head;
1428 current && current->type == HL_TYPE_GROUP;
1429 current = current->next)
1430 save_group (current);
1432 profile_clean_section (grp->directory, profile_name);
1433 for (;current; current = current->next){
1434 WritePrivateProfileString (grp->directory,
1435 current->directory,
1436 current->label,
1437 profile_name);
1441 static int list_level = 0;
1443 static void
1444 hot_save_group (struct hotlist *grp)
1446 struct hotlist *current = grp->head;
1447 int i;
1448 char *s;
1450 #define INDENT(n) \
1451 do { \
1452 for (i = 0; i < n; i++) \
1453 putc (' ', hotlist_file); \
1454 } while (0)
1456 for (;current; current = current->next)
1457 switch (current->type) {
1458 case HL_TYPE_GROUP:
1459 INDENT (list_level);
1460 fputs ("GROUP \"", hotlist_file);
1461 for (s = current->label; *s; s++) {
1462 if (*s == '"')
1463 putc ('\\', hotlist_file);
1464 else if (*s == '\\')
1465 putc ('\\', hotlist_file);
1466 putc (*s, hotlist_file);
1468 fputs ("\"\n", hotlist_file);
1469 list_level += 2;
1470 hot_save_group (current);
1471 list_level -= 2;
1472 INDENT (list_level);
1473 fputs ("ENDGROUP\n", hotlist_file);
1474 break;
1475 case HL_TYPE_ENTRY:
1476 INDENT(list_level);
1477 fputs ("ENTRY \"", hotlist_file);
1478 for (s = current->label; *s; s++) {
1479 if (*s == '"')
1480 putc ('\\', hotlist_file);
1481 else if (*s == '\\')
1482 putc ('\\', hotlist_file);
1483 putc (*s, hotlist_file);
1485 fputs ("\" URL \"", hotlist_file);
1486 for (s = current->directory; *s; s++) {
1487 if (*s == '"')
1488 putc ('\\', hotlist_file);
1489 else if (*s == '\\')
1490 putc ('\\', hotlist_file);
1491 putc (*s, hotlist_file);
1493 fputs ("\"\n", hotlist_file);
1494 break;
1495 case HL_TYPE_COMMENT:
1496 fprintf (hotlist_file, "#%s\n", current->label);
1497 break;
1502 int save_hotlist (void)
1504 int saved = 0;
1505 struct stat stat_buf;
1507 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1508 char *fbak = g_strconcat (hotlist_file_name, ".bak", NULL);
1510 rename (hotlist_file_name, fbak);
1511 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1512 if (stat (fbak, &stat_buf) == 0)
1513 chmod (hotlist_file_name, stat_buf.st_mode);
1514 else
1515 chmod (hotlist_file_name, S_IRUSR | S_IWUSR);
1516 hot_save_group (hotlist);
1517 fclose (hotlist_file);
1518 stat (hotlist_file_name, &stat_buf);
1519 hotlist_file_mtime = stat_buf.st_mtime;
1520 saved = 1;
1521 hotlist_state.modified = 0;
1522 } else
1523 rename (fbak, hotlist_file_name);
1524 g_free (fbak);
1527 return saved;
1531 * Unload list from memory.
1532 * Don't confuse with hotlist_done() for GUI.
1534 void done_hotlist (void)
1536 if (hotlist){
1537 remove_group (hotlist);
1538 if (hotlist->label)
1539 g_free (hotlist->label);
1540 if (hotlist->directory)
1541 g_free (hotlist->directory);
1542 g_free (hotlist);
1543 hotlist = 0;
1546 hotlist_state.loaded = 0;
1548 if (hotlist_file_name){
1549 g_free (hotlist_file_name);
1550 hotlist_file_name = 0;
1552 l_hotlist = 0;
1553 current_group = 0;
1554 if (tkn_buf){
1555 g_free (tkn_buf);
1556 tkn_buf_length = 0;
1557 tkn_length = 0;
1558 tkn_buf = NULL;