Just a little correction at the it.po file.
[midnight-commander.git] / src / hotlist.c
blob97a8c6facbbb59e515c034657d44927c0837db01
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" /* KEY_M_CTRL */
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
155 update_path_name (void)
157 char *text, *p;
158 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
159 Dlg_head *dlg = hotlist_state.moving ? movelist_dlg : hotlist_dlg;
161 if (list->current) {
162 if (list->current->data != 0) {
163 struct hotlist *hlp = (struct hotlist *) list->current->data;
165 if (hlp->type == HL_TYPE_ENTRY)
166 text = hlp->directory;
167 else
168 text = _("Subgroup - press ENTER to see list");
169 } else {
170 text = list->current->text;
172 } else {
173 text = "";
175 if (!hotlist_state.moving)
176 label_set_text (pname,
177 name_trunc (text, dlg->cols - (UX * 2 + 4)));
179 p = g_strconcat (" ", current_group->label, " ", NULL);
180 if (!hotlist_state.moving)
181 label_set_text (pname_group,
182 name_trunc (p, dlg->cols - (UX * 2 + 4)));
183 else
184 label_set_text (movelist_group,
185 name_trunc (p, dlg->cols - (UX * 2 + 4)));
186 g_free (p);
188 dlg_redraw (dlg);
191 #define CHECK_BUFFER \
192 do { \
193 int i; \
195 if ((i = strlen (current->label) + 3) > buflen) { \
196 g_free (buf); \
197 buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \
199 buf[0] = '\0'; \
200 } while (0)
202 static void fill_listbox (void)
204 struct hotlist *current = current_group->head;
205 static char *buf;
206 static int buflen;
208 if (!buf)
209 buf = g_malloc (buflen = 1024);
210 buf[0] = '\0';
212 while (current){
213 switch (current->type) {
214 case HL_TYPE_GROUP:
216 CHECK_BUFFER;
217 strcat (strcat (buf, "->"), current->label);
218 if (hotlist_state.moving)
219 listbox_add_item (l_movelist, 0, 0, buf, current);
220 else
221 listbox_add_item (l_hotlist, 0, 0, buf, current);
223 break;
224 case HL_TYPE_ENTRY:
225 if (hotlist_state.moving)
226 listbox_add_item (l_movelist, 0, 0, current->label, current);
227 else
228 listbox_add_item (l_hotlist, 0, 0, current->label, current);
229 break;
230 default:
231 break;
233 current = current->next;
237 #undef CHECK_BUFFER
239 static void
240 unlink_entry (struct hotlist *entry)
242 struct hotlist *current = current_group->head;
244 if (current == entry)
245 current_group->head = entry->next;
246 else
247 while (current && current->next != entry)
248 current = current->next;
249 if (current)
250 current->next = entry->next;
251 entry->next =
252 entry->up = 0;
255 static int hotlist_button_callback (int action, void *data)
257 switch (action) {
258 case B_MOVE:
260 struct hotlist *saved = current_group;
261 struct hotlist *item;
262 struct hotlist *moveto_item = 0;
263 struct hotlist *moveto_group = 0;
264 int ret;
266 if (!l_hotlist->current)
267 return 0; /* empty group - nothing to do */
268 item = l_hotlist->current->data;
269 hotlist_state.moving = 1;
270 init_movelist (LIST_MOVELIST, item);
271 run_dlg (movelist_dlg);
272 ret = movelist_dlg->ret_value;
273 hotlist_state.moving = 0;
274 if (l_movelist->current)
275 moveto_item = l_movelist->current->data;
276 moveto_group = current_group;
277 destroy_dlg (movelist_dlg);
278 current_group = saved;
279 if (ret == B_CANCEL)
280 return 0;
281 if (moveto_item == item)
282 return 0; /* If we insert/append a before/after a
283 it hardly changes anything ;) */
284 unlink_entry (item);
285 listbox_remove_current (l_hotlist, 1);
286 item->up = moveto_group;
287 if (!moveto_group->head)
288 moveto_group->head = item;
289 else if (!moveto_item) { /* we have group with just comments */
290 struct hotlist *p = moveto_group->head;
292 /* skip comments */
293 while (p->next)
294 p = p->next;
295 p->next = item;
296 } else if (ret == B_ENTER || ret == B_APPEND)
297 if (!moveto_item->next)
298 moveto_item->next = item;
299 else {
300 item->next = moveto_item->next;
301 moveto_item->next = item;
303 else if (moveto_group->head == moveto_item) {
304 moveto_group->head = item;
305 item->next = moveto_item;
306 } else {
307 struct hotlist *p = moveto_group->head;
309 while (p->next != moveto_item)
310 p = p->next;
311 item->next = p->next;
312 p->next = item;
314 listbox_remove_list (l_hotlist);
315 fill_listbox ();
316 repaint_screen ();
317 hotlist_state.modified = 1;
318 return 0;
319 break;
321 case B_REMOVE:
322 if (l_hotlist->current && l_hotlist->current->data)
323 remove_from_hotlist (l_hotlist->current->data);
324 return 0;
325 break;
327 case B_NEW_GROUP:
328 add_new_group_cmd ();
329 return 0;
330 break;
332 case B_ADD_CURRENT:
333 add2hotlist_cmd ();
334 return 0;
335 break;
337 case B_NEW_ENTRY:
338 add_new_entry_cmd ();
339 return 0;
340 break;
342 case B_ENTER:
344 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
345 if (list->current){
346 if (list->current->data) {
347 struct hotlist *hlp = (struct hotlist*) list->current->data;
348 if (hlp->type == HL_TYPE_ENTRY)
349 return 1;
350 else {
351 listbox_remove_list (list);
352 current_group = hlp;
353 fill_listbox ();
354 return 0;
356 } else
357 return 1;
360 /* Fall through if list empty - just go up */
362 case B_UP_GROUP:
364 WListbox *list = hotlist_state.moving ? l_movelist : l_hotlist;
365 listbox_remove_list (list);
366 current_group = current_group->up;
367 fill_listbox ();
368 return 0;
369 break;
372 default:
373 return 1;
374 break;
379 static int hotlist_callback (Dlg_head * h, int Par, int Msg)
381 switch (Msg) {
382 case DLG_DRAW:
383 hotlist_refresh (h);
384 break;
386 case DLG_UNHANDLED_KEY:
387 switch (Par) {
388 case KEY_M_CTRL | '\n':
389 goto l1;
390 case '\n':
391 case KEY_ENTER:
392 case KEY_RIGHT:
393 if (hotlist_button_callback (B_ENTER, 0)) {
394 h->ret_value = B_ENTER;
395 dlg_stop (h);
397 return 1;
398 break;
399 case KEY_LEFT:
400 if (hotlist_state.type != LIST_VFSLIST )
401 return !hotlist_button_callback (B_UP_GROUP, 0);
402 else
403 return 0;
404 break;
405 case KEY_DC:
406 if (!hotlist_state.moving) {
407 hotlist_button_callback (B_REMOVE, 0);
408 return 1;
410 break;
412 case ALT('\n'):
413 case ALT('\r'):
414 if (!hotlist_state.moving)
416 if (l_hotlist->current){
417 if (l_hotlist->current->data) {
418 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
419 if (hlp->type == HL_TYPE_ENTRY) {
420 char *tmp = g_strconcat ( "cd ", hlp->directory, NULL);
421 stuff (cmdline, tmp, 0);
422 g_free (tmp);
423 dlg_stop (h);
424 h->ret_value = B_CANCEL;
425 return 1;
430 return 1; /* ignore key */
431 default:
432 return 0;
434 break;
436 case DLG_POST_KEY:
437 if (hotlist_state.moving)
438 dlg_select_widget (movelist_dlg, l_movelist);
439 else
440 dlg_select_widget (hotlist_dlg, l_hotlist);
441 /* always stay on hotlist */
442 /* fall through */
444 case DLG_INIT:
445 attrset (MENU_ENTRY_COLOR);
446 update_path_name ();
447 break;
449 return 0;
452 static int l_call (void *l)
454 WListbox *list = (WListbox *) l;
455 Dlg_head *dlg = hotlist_state.moving ? movelist_dlg : hotlist_dlg;
457 if (list->current){
458 if (list->current->data) {
459 struct hotlist *hlp = (struct hotlist*) list->current->data;
460 if (hlp->type == HL_TYPE_ENTRY) {
461 dlg->ret_value = B_ENTER;
462 dlg_stop (dlg);
463 return listbox_finish;
464 } else {
465 hotlist_button_callback (B_ENTER, (void *)0);
466 hotlist_callback (dlg, '\n', DLG_POST_KEY);
467 return listbox_nothing;
469 } else {
470 dlg->ret_value = B_ENTER;
471 dlg_stop (dlg);
472 return listbox_finish;
476 hotlist_button_callback (B_UP_GROUP, (void *)0);
477 hotlist_callback (dlg, 'u', DLG_POST_KEY);
478 return listbox_nothing;
481 #ifdef USE_VFS
482 static void add_name_to_list (char *path)
484 listbox_add_item (l_hotlist, 0, 0, path, 0);
486 #endif /* !USE_VFS */
489 * Expands all button names (once) and recalculates button positions.
490 * returns number of columns in the dialog box, which is 10 chars longer
491 * then buttonbar.
493 * If common width of the window (i.e. in xterm) is less than returned
494 * width - sorry :) (anyway this did not handled in previous version too)
496 static int
497 init_i18n_stuff(int list_type, int cols)
499 register int i;
500 static char* cancel_but = N_("&Cancel");
502 #ifdef ENABLE_NLS
503 static int hotlist_i18n_flag = 0;
505 if (!hotlist_i18n_flag)
507 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
508 while (i--)
509 hotlist_but [i].text = _(hotlist_but [i].text);
511 cancel_but = _(cancel_but);
512 hotlist_i18n_flag = 1;
514 #endif /* ENABLE_NLS */
516 /* Dynamic resizing of buttonbars */
518 int len[2], count[2]; /* at most two lines of buttons */
519 int cur_x[2], row;
521 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
522 len[0] = len[1] = count[0] = count[1] = 0;
524 /* Count len of buttonbars, assuming 2 extra space between buttons */
525 while (i--)
527 if (! (hotlist_but[i].type & list_type))
528 continue;
530 row = hotlist_but [i].y;
531 ++count [row];
532 len [row] += strlen (hotlist_but [i].text) + 5;
533 if (hotlist_but [i].flags == DEFPUSH_BUTTON)
534 len [row] += 2;
536 len[0] -= 2;
537 len[1] -= 2;
539 cols = max(cols, max(len[0], len[1]));
541 /* arrange buttons */
543 cur_x[0] = cur_x[1] = 0;
544 i = sizeof (hotlist_but) / sizeof (hotlist_but [0]);
545 while (i--)
547 if (! (hotlist_but[i].type & list_type))
548 continue;
550 row = hotlist_but [i].y;
552 if (hotlist_but [i].x != 0)
554 /* not first int the row */
555 if (!strcmp (hotlist_but [i].text, cancel_but))
556 hotlist_but [i].x =
557 cols - strlen (hotlist_but [i].text) - 13;
558 else
559 hotlist_but [i].x = cur_x [row];
562 cur_x [row] += strlen (hotlist_but [i].text) + 2
563 + (hotlist_but [i].flags == DEFPUSH_BUTTON ? 5 : 3);
567 return cols;
570 static void
571 init_hotlist (int list_type)
573 int i;
574 char *title, *help_node;
575 int hotlist_cols;
577 hotlist_cols = init_i18n_stuff (list_type, COLS - 6);
579 do_refresh ();
581 hotlist_state.expanded =
582 GetPrivateProfileInt ("HotlistConfig", "expanded_view_of_groups",
583 0, profile_name);
585 if (list_type == LIST_VFSLIST) {
586 title = _("Active VFS directories");
587 help_node = "[vfshot]"; /* FIXME - no such node */
588 } else {
589 title = _("Directory hotlist");
590 help_node = "[Hotlist]";
593 hotlist_dlg =
594 create_dlg (0, 0, LINES - 2, hotlist_cols, dialog_colors,
595 hotlist_callback, help_node, title, DLG_CENTER);
597 for (i = 0; i < BUTTONS; i++) {
598 if (hotlist_but[i].type & list_type)
599 add_widget (hotlist_dlg,
600 button_new (BY + hotlist_but[i].y,
601 BX + hotlist_but[i].x,
602 hotlist_but[i].ret_cmd,
603 hotlist_but[i].flags,
604 hotlist_but[i].text,
605 hotlist_button_callback, 0,
606 hotlist_but[i].tkname));
609 /* We add the labels.
610 * pname will hold entry's pathname;
611 * pname_group will hold name of current group
613 pname = label_new (UY - 11 + LINES, UX + 2, "", "the-lab");
614 add_widget (hotlist_dlg, pname);
615 if (!hotlist_state.moving) {
616 add_widget (hotlist_dlg,
617 label_new (UY - 12 + LINES, UX + 1,
618 _(" Directory path "), NULL));
620 /* This one holds the displayed pathname */
621 pname_group = label_new (UY, UX + 1, _(" Directory label "), NULL);
622 add_widget (hotlist_dlg, pname_group);
624 /* get new listbox */
625 l_hotlist =
626 listbox_new (UY + 1, UX + 1, COLS - 2 * UX - 8, LINES - 14,
627 listbox_cback, l_call, "listbox");
629 /* Fill the hotlist with the active VFS or the hotlist */
630 #ifdef USE_VFS
631 if (list_type == LIST_VFSLIST) {
632 listbox_add_item (l_hotlist, 0, 0, home_dir, 0);
633 vfs_fill_names (add_name_to_list);
634 } else
635 #endif /* !USE_VFS */
636 fill_listbox ();
638 add_widget (hotlist_dlg, l_hotlist);
639 /* add listbox to the dialogs */
642 static void
643 init_movelist (int list_type, struct hotlist *item)
645 int i;
646 char *hdr = g_strdup_printf (_("Moving %s"), item->label);
647 int movelist_cols = init_i18n_stuff (list_type, COLS - 6);
649 do_refresh ();
651 movelist_dlg =
652 create_dlg (0, 0, LINES - 6, movelist_cols, dialog_colors,
653 hotlist_callback, "[Hotlist]", hdr, DLG_CENTER);
654 g_free (hdr);
656 for (i = 0; i < BUTTONS; i++) {
657 if (hotlist_but[i].type & list_type)
658 add_widget (movelist_dlg,
659 button_new (BY - 4 + hotlist_but[i].y,
660 BX + hotlist_but[i].x,
661 hotlist_but[i].ret_cmd,
662 hotlist_but[i].flags,
663 hotlist_but[i].text,
664 hotlist_button_callback, 0,
665 hotlist_but[i].tkname));
668 /* We add the labels. We are interested in the last one,
669 * that one will hold the path name label
671 movelist_group = label_new (UY, UX + 1, _(" Directory label "), NULL);
672 add_widget (movelist_dlg, movelist_group);
673 /* get new listbox */
674 l_movelist =
675 listbox_new (UY + 1, UX + 1, movelist_dlg->cols - 2 * UX - 2,
676 movelist_dlg->lines - 8, listbox_cback, l_call,
677 "listbox");
679 fill_listbox ();
681 add_widget (movelist_dlg, l_movelist);
682 /* add listbox to the dialogs */
686 * Destroy the list dialog.
687 * Don't confuse with done_hotlist() for the list in memory.
689 static void hotlist_done (void)
691 destroy_dlg (hotlist_dlg);
692 l_hotlist = NULL;
693 if (0)
694 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
695 repaint_screen ();
698 static char *
699 find_group_section (struct hotlist *grp)
701 return g_strconcat (grp->directory, ".Group", NULL);
706 /* 1.11.96 bor: added pos parameter to control placement of new item.
707 see widget.c, listbox_add_item()
708 now hotlist is in unsorted mode
710 static struct hotlist *
711 add2hotlist (char *label, char *directory, enum HotListType type, int pos)
713 struct hotlist *new;
714 struct hotlist *current = NULL;
717 * Hotlist is neither loaded nor loading.
718 * Must be called by "Ctrl-x a" before using hotlist.
720 if (!current_group)
721 load_hotlist ();
723 if (l_hotlist && l_hotlist->current)
724 current = l_hotlist->current->data;
726 new = new_hotlist ();
728 new->type = type;
729 new->label = label;
730 new->directory = directory;
731 new->up = current_group;
733 if (!current_group->head) { /* first element in group */
734 current_group->head = new;
735 } else if (pos == 2) { /* should be appended after current*/
736 new->next = current->next;
737 current->next = new;
738 } else if (pos == 1 &&
739 current == current_group->head) {
740 /* should be inserted before first item */
741 new->next = current;
742 current_group->head = new;
743 } else if (pos == 1) { /* befor current */
744 struct hotlist *p = current_group->head;
746 while (p->next != current)
747 p = p->next;
749 new->next = current;
750 p->next = new;
751 } else { /* append at the end */
752 struct hotlist *p = current_group->head;
754 while (p->next)
755 p = p->next;
757 p->next = new;
760 if (hotlist_state.running && type != HL_TYPE_COMMENT) {
761 if (type == HL_TYPE_GROUP) {
762 char *lbl = g_strconcat ("->", new->label, NULL);
764 listbox_add_item (l_hotlist, pos, 0, lbl, new);
765 g_free (lbl);
766 } else
767 listbox_add_item (l_hotlist, pos, 0, new->label, new);
768 listbox_select_entry (l_hotlist, l_hotlist->current);
770 return new;
774 #ifdef ENABLE_NLS
776 * Support routine for add_new_entry_input()/add_new_group_input()
777 * Change positions of buttons (first three widgets).
779 * This is just a quick hack. Accurate procedure must take care of
780 * internationalized label lengths and total buttonbar length...assume
781 * 64 is longer anyway.
783 static void add_widgets_i18n(QuickWidget* qw, int len)
785 int i, l[3], space, cur_x;
787 for (i = 0; i < 3; i++)
789 qw [i].text = _(qw [i].text);
790 l[i] = strlen (qw [i].text) + 3;
792 space = (len - 4 - l[0] - l[1] - l[2]) / 4;
794 for (cur_x = 2 + space, i = 3; i--; cur_x += l[i] + space)
796 qw [i].relative_x = cur_x;
797 qw [i].x_divisions = len;
800 #endif /* ENABLE_NLS */
802 static int add_new_entry_input (char *header, char *text1, char *text2, char *help, char **r1, char **r2)
804 #define RELATIVE_Y_BUTTONS 4
805 #define RELATIVE_Y_LABEL_PTH 3
806 #define RELATIVE_Y_INPUT_PTH 4
808 QuickDialog Quick_input;
809 static QuickWidget quick_widgets [] = {
810 { quick_button, 55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), 0, B_CANCEL,
811 0, 0, "button-cancel" },
812 { quick_button, 30, 80, RELATIVE_Y_BUTTONS, 0, N_("&Insert"), 0, B_INSERT,
813 0, 0, "button-insert" },
814 { quick_button, 10, 80, RELATIVE_Y_BUTTONS, 0, N_("&Append"), 0, B_APPEND,
815 0, 0, "button-append" },
816 { quick_input, 4, 80, RELATIVE_Y_INPUT_PTH, 0, "",58, 0,
817 0, 0, "input-pth" },
818 { quick_label, RELATIVE_Y_LABEL_PTH, 80, 3, 0, 0, 0, 0,
819 0, 0, "label-pth" },
820 { quick_input, 4, 80, 3, 0, "", 58, 0,
821 0, 0, "input-lbl" },
822 { quick_label, 3, 80, 2, 0, 0, 0, 0,
823 0, 0, "label-lbl" },
824 { 0 } };
826 int len;
827 int i;
828 int lines1, lines2;
829 char *my_str1, *my_str2;
831 #ifdef ENABLE_NLS
832 static int i18n_flag = 0;
833 #endif /* ENABLE_NLS */
835 len = max (strlen (header), msglen (text1, &lines1));
836 len = max (len, msglen (text2, &lines2)) + 4;
837 len = max (len, 64);
839 #ifdef ENABLE_NLS
840 if (!i18n_flag)
842 add_widgets_i18n(quick_widgets, len);
843 i18n_flag = 1;
845 #endif /* ENABLE_NLS */
847 Quick_input.xlen = len;
848 Quick_input.xpos = -1;
849 Quick_input.title = header;
850 Quick_input.help = help;
851 Quick_input.i18n = 0;
852 quick_widgets [6].text = text1;
853 quick_widgets [4].text = text2;
854 quick_widgets [5].text = *r1;
855 quick_widgets [3].text = *r2;
857 for (i = 0; i < 7; i++)
858 quick_widgets [i].y_divisions = lines1+lines2+7;
859 Quick_input.ylen = lines1 + lines2 + 7;
861 quick_widgets [0].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
862 quick_widgets [1].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
863 quick_widgets [2].relative_y = RELATIVE_Y_BUTTONS + (lines1 + lines2);
864 quick_widgets [3].relative_y = RELATIVE_Y_INPUT_PTH + (lines1);
865 quick_widgets [4].relative_y = RELATIVE_Y_LABEL_PTH + (lines1);
867 quick_widgets [5].str_result = &my_str1;
868 quick_widgets [3].str_result = &my_str2;
870 Quick_input.widgets = quick_widgets;
871 if ((i = quick_dialog (&Quick_input)) != B_CANCEL){
872 *r1 = *(quick_widgets [5].str_result);
873 *r2 = *(quick_widgets [3].str_result);
874 return i;
875 } else
876 return 0;
879 static void add_new_entry_cmd (void)
881 char *title = 0, *url = 0;
882 int ret;
884 /* Take current directory as default value for input fields */
885 title = url = cpanel->cwd;
887 ret = add_new_entry_input (_("New hotlist entry"), _("Directory label"), _("Directory path"),
888 "[Hotlist]", &title, &url);
890 if (!ret || !title || !*title || !url || !*url)
891 return;
893 if (ret == B_ENTER || ret == B_APPEND)
894 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 2);
895 else
896 add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 1);
898 hotlist_state.modified = 1;
901 static int add_new_group_input (char *header, char *label, char **result)
903 int ret;
904 QuickDialog Quick_input;
905 static QuickWidget quick_widgets [] = {
906 { quick_button, 55, 80, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
907 "button-cancel" },
908 { quick_button, 30, 80, 1, 0, N_("&Insert"), 0, B_INSERT, 0, 0,
909 "button-insert" },
910 { quick_button, 10, 80, 1, 0, N_("&Append"), 0, B_APPEND, 0, 0,
911 "button-append" },
912 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, "input" },
913 { quick_label, 3, 80, 2, 0, 0, 0, 0, 0, 0, "label" },
914 { 0 } };
915 int relative_y[] = {1, 1, 1, 0, 2}; /* the relative_x component from the
916 quick_widgets variable above */
917 int len;
918 int i;
919 int lines;
920 char *my_str;
922 #ifdef ENABLE_NLS
923 static int i18n_flag = 0;
924 #endif /* ENABLE_NLS */
926 len = max (strlen (header), msglen (label, &lines)) + 4;
927 len = max (len, 64);
929 #ifdef ENABLE_NLS
930 if (!i18n_flag)
932 add_widgets_i18n(quick_widgets, len);
933 i18n_flag = 1;
935 #endif /* ENABLE_NLS */
937 Quick_input.xlen = len;
938 Quick_input.xpos = -1;
939 Quick_input.title = header;
940 Quick_input.help = "[Hotlist]";
941 Quick_input.i18n = 0;
942 quick_widgets [4].text = label;
944 for (i = 0; i < 5; i++)
945 quick_widgets [i].y_divisions = lines+6;
946 Quick_input.ylen = lines + 6;
948 for (i = 0; i < 4; i++)
949 quick_widgets [i].relative_y = relative_y[i] + 2 + lines;
951 quick_widgets [3].str_result = &my_str;
952 quick_widgets [3].text = "";
954 Quick_input.widgets = quick_widgets;
955 if ((ret = quick_dialog (&Quick_input)) != B_CANCEL){
956 *result = *(quick_widgets [3].str_result);
957 return ret;
958 } else
959 return 0;
962 static void add_new_group_cmd (void)
964 char *label;
965 int ret;
967 ret = add_new_group_input (_(" New hotlist group "), _("Name of new group"), &label);
968 if (!ret || !label || !*label)
969 return;
971 if (ret == B_ENTER || ret == B_APPEND)
972 add2hotlist (label, 0, HL_TYPE_GROUP, 2);
973 else
974 add2hotlist (label, 0, HL_TYPE_GROUP, 1);
976 hotlist_state.modified = 1;
979 void add2hotlist_cmd (void)
981 char *prompt, *label;
982 char *cp = _("Label for \"%s\":");
983 int l = strlen (cp);
985 prompt = g_strdup_printf (cp, name_trunc (cpanel->cwd, COLS-2*UX-(l+8)));
986 label = input_dialog (_(" Add to hotlist "), prompt, cpanel->cwd);
987 g_free (prompt);
988 if (!label || !*label)
989 return;
991 add2hotlist (label,g_strdup (cpanel->cwd), HL_TYPE_ENTRY, 0);
992 hotlist_state.modified = 1;
995 static void remove_group (struct hotlist *grp)
997 struct hotlist *current = grp->head;
999 while (current) {
1000 struct hotlist *next = current->next;
1002 if (current->type == HL_TYPE_GROUP)
1003 remove_group (current);
1005 if (current->label)
1006 g_free (current->label);
1007 if (current->directory)
1008 g_free (current->directory);
1009 g_free (current);
1011 current = next;
1016 static void remove_from_hotlist (struct hotlist *entry)
1018 if (entry->type == HL_TYPE_GROUP) {
1019 if (entry->head) {
1020 char *header;
1021 int result;
1023 header = g_strconcat (_(" Remove: "),
1024 name_trunc (entry->label, 30),
1025 " ",
1026 NULL);
1027 result = query_dialog (header, _("\n Group not empty.\n Remove it?"),
1028 D_ERROR, 2,
1029 _("&No"), _("&Yes"));
1030 g_free (header);
1032 if (!result)
1033 return;
1036 remove_group (entry);
1039 unlink_entry (entry);
1041 if (entry->label)
1042 g_free (entry->label);
1043 if (entry->directory)
1044 g_free (entry->directory);
1045 g_free (entry);
1046 /* now remove list entry from screen */
1047 listbox_remove_current (l_hotlist, 1);
1048 hotlist_state.modified = 1;
1051 char *hotlist_cmd (int vfs_or_hotlist)
1053 char *target = NULL;
1055 hotlist_state.type = vfs_or_hotlist;
1056 load_hotlist ();
1058 init_hotlist (vfs_or_hotlist);
1060 /* display file info */
1061 attrset (SELECTED_COLOR);
1063 hotlist_state.running = 1;
1064 run_dlg (hotlist_dlg);
1065 hotlist_state.running = 0;
1066 save_hotlist ();
1068 switch (hotlist_dlg->ret_value) {
1069 case B_CANCEL:
1070 break;
1072 case B_ENTER:
1073 if (l_hotlist->current->data) {
1074 struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data;
1075 target = g_strdup (hlp->directory);
1076 } else
1077 target = g_strdup (l_hotlist->current->text);
1078 break;
1081 hotlist_done ();
1082 return target;
1085 static void
1086 load_group (struct hotlist *grp)
1088 void *profile_keys;
1089 char *key, *value;
1090 char *group_section;
1091 struct hotlist *current = 0;
1093 group_section = find_group_section (grp);
1095 profile_keys = profile_init_iterator (group_section, profile_name);
1097 current_group = grp;
1099 while (profile_keys){
1100 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1101 add2hotlist (g_strdup (value), g_strdup (key), HL_TYPE_GROUP, 0);
1103 g_free (group_section);
1105 profile_keys = profile_init_iterator (grp->directory, profile_name);
1107 while (profile_keys){
1108 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1109 add2hotlist (g_strdup (value),g_strdup (key), HL_TYPE_ENTRY, 0);
1112 for (current = grp->head; current; current = current->next)
1113 load_group (current);
1116 #define TKN_GROUP 0
1117 #define TKN_ENTRY 1
1118 #define TKN_STRING 2
1119 #define TKN_URL 3
1120 #define TKN_ENDGROUP 4
1121 #define TKN_COMMENT 5
1122 #define TKN_EOL 125
1123 #define TKN_EOF 126
1124 #define TKN_UNKNOWN 127
1126 static char *tkn_buf;
1127 static int tkn_buf_length;
1128 static int tkn_length;
1130 static char *hotlist_file_name;
1131 static FILE *hotlist_file;
1132 static time_t hotlist_file_mtime;
1134 static int hot_skip_blanks (void)
1136 int c;
1138 while ((c = getc (hotlist_file)) != EOF && c != '\n' && isspace (c))
1140 return c;
1144 static int hot_next_token (void)
1146 int c;
1148 #define CHECK_BUF() \
1149 do { \
1150 if (tkn_length == tkn_buf_length) \
1151 tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \
1152 : ( g_malloc (tkn_buf_length = 1024)); \
1153 } while (0)
1155 tkn_length = 0;
1157 again:
1158 c = hot_skip_blanks ();
1159 switch (c) {
1160 case EOF:
1161 return TKN_EOF;
1162 break;
1163 case '\n':
1164 return TKN_EOL;
1165 break;
1166 case '#':
1167 while ((c = getc (hotlist_file)) != EOF && c != '\n') {
1168 if (c == EOF)
1169 return TKN_EOF;
1170 if (c != '\n') {
1171 CHECK_BUF();
1172 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1175 CHECK_BUF();
1176 tkn_buf[tkn_length] = '\0';
1177 return TKN_COMMENT;
1178 break;
1179 case '"':
1180 while ((c = getc (hotlist_file)) != EOF && c != '"') {
1181 if (c == '\\')
1182 if ((c = getc (hotlist_file)) == EOF)
1183 return TKN_EOF;
1184 CHECK_BUF();
1185 tkn_buf[tkn_length++] = c == '\n' ? ' ' : c;
1187 if (c == EOF)
1188 return TKN_EOF;
1189 CHECK_BUF();
1190 tkn_buf[tkn_length] = '\0';
1191 return TKN_STRING;
1192 break;
1193 case '\\':
1194 if ((c = getc (hotlist_file)) == EOF)
1195 return TKN_EOF;
1196 if (c == '\n')
1197 goto again;
1199 /* fall through; it is taken as normal character */
1201 default:
1202 do {
1203 CHECK_BUF();
1204 tkn_buf[tkn_length++] = toupper(c);
1205 } while ((c = fgetc (hotlist_file)) != EOF && isalnum (c));
1206 if (c != EOF)
1207 ungetc (c, hotlist_file);
1208 CHECK_BUF();
1209 tkn_buf[tkn_length] = '\0';
1210 if (strncmp (tkn_buf, "GROUP", tkn_length) == 0)
1211 return TKN_GROUP;
1212 else if (strncmp (tkn_buf, "ENTRY", tkn_length) == 0)
1213 return TKN_ENTRY;
1214 else if (strncmp (tkn_buf, "ENDGROUP", tkn_length) == 0)
1215 return TKN_ENDGROUP;
1216 else if (strncmp (tkn_buf, "URL", tkn_length) == 0)
1217 return TKN_URL;
1218 else
1219 return TKN_UNKNOWN;
1220 break;
1224 #define SKIP_TO_EOL { \
1225 int _tkn; \
1226 while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \
1229 #define CHECK_TOKEN(_TKN_) \
1230 if ((tkn = hot_next_token ()) != _TKN_) { \
1231 hotlist_state.readonly = 1; \
1232 hotlist_state.file_error = 1; \
1233 while (tkn != TKN_EOL && tkn != TKN_EOF) \
1234 tkn = hot_next_token (); \
1235 break; \
1238 static void
1239 hot_load_group (struct hotlist * grp)
1241 int tkn;
1242 struct hotlist *new_grp;
1243 char *label, *url;
1245 current_group = grp;
1247 while ((tkn = hot_next_token()) != TKN_ENDGROUP)
1248 switch (tkn) {
1249 case TKN_GROUP:
1250 CHECK_TOKEN(TKN_STRING);
1251 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1252 SKIP_TO_EOL;
1253 hot_load_group (new_grp);
1254 current_group = grp;
1255 break;
1256 case TKN_ENTRY:
1257 CHECK_TOKEN(TKN_STRING);
1258 label = g_strdup (tkn_buf);
1259 CHECK_TOKEN(TKN_URL);
1260 CHECK_TOKEN(TKN_STRING);
1261 url = g_strdup (tkn_buf);
1262 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1263 SKIP_TO_EOL;
1264 break;
1265 case TKN_COMMENT:
1266 label = g_strdup (tkn_buf);
1267 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1268 break;
1269 case TKN_EOF:
1270 hotlist_state.readonly = 1;
1271 hotlist_state.file_error = 1;
1272 return;
1273 break;
1274 case TKN_EOL:
1275 /* skip empty lines */
1276 break;
1277 default:
1278 hotlist_state.readonly = 1;
1279 hotlist_state.file_error = 1;
1280 SKIP_TO_EOL;
1281 break;
1283 SKIP_TO_EOL;
1286 static void
1287 hot_load_file (struct hotlist * grp)
1289 int tkn;
1290 struct hotlist *new_grp;
1291 char *label, *url;
1293 current_group = grp;
1295 while ((tkn = hot_next_token())!= TKN_EOF)
1296 switch (tkn) {
1297 case TKN_GROUP:
1298 CHECK_TOKEN(TKN_STRING);
1299 new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0);
1300 SKIP_TO_EOL;
1301 hot_load_group (new_grp);
1302 current_group = grp;
1303 break;
1304 case TKN_ENTRY:
1305 CHECK_TOKEN(TKN_STRING);
1306 label = g_strdup (tkn_buf);
1307 CHECK_TOKEN(TKN_URL);
1308 CHECK_TOKEN(TKN_STRING);
1309 url = g_strdup (tkn_buf);
1310 add2hotlist (label, url, HL_TYPE_ENTRY, 0);
1311 SKIP_TO_EOL;
1312 break;
1313 case TKN_COMMENT:
1314 label = g_strdup (tkn_buf);
1315 add2hotlist (label, 0, HL_TYPE_COMMENT, 0);
1316 break;
1317 case TKN_EOL:
1318 /* skip empty lines */
1319 break;
1320 default:
1321 hotlist_state.readonly = 1;
1322 hotlist_state.file_error = 1;
1323 SKIP_TO_EOL;
1324 break;
1328 static void
1329 clean_up_hotlist_groups (char *section)
1331 char *grp_section;
1332 void *profile_keys;
1333 char *key, *value;
1335 grp_section = g_strconcat (section, ".Group", NULL);
1336 if (profile_has_section (section, profile_name))
1337 profile_clean_section (section, profile_name);
1338 if (profile_has_section (grp_section, profile_name)) {
1339 profile_keys = profile_init_iterator (grp_section, profile_name);
1341 while (profile_keys) {
1342 profile_keys = profile_iterator_next (profile_keys, &key, &value);
1343 clean_up_hotlist_groups (key);
1345 profile_clean_section (grp_section, profile_name);
1347 g_free (grp_section);
1352 void load_hotlist (void)
1354 int remove_old_list = 0;
1355 struct stat stat_buf;
1357 if (hotlist_state.loaded) {
1358 stat (hotlist_file_name, &stat_buf);
1359 if (hotlist_file_mtime < stat_buf.st_mtime)
1360 done_hotlist ();
1361 else
1362 return;
1365 if (!hotlist_file_name)
1366 hotlist_file_name = concat_dir_and_file (home_dir, HOTLIST_FILENAME);
1368 hotlist = new_hotlist ();
1369 hotlist->type = HL_TYPE_GROUP;
1370 hotlist->label = g_strdup (_(" Top level group "));
1371 hotlist->up = hotlist;
1373 * compatibility :-(
1375 hotlist->directory = g_strdup ("Hotlist");
1377 if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) {
1378 int result;
1380 load_group (hotlist);
1381 hotlist_state.loaded = 1;
1383 * just to be sure we got copy
1385 hotlist_state.modified = 1;
1386 result = save_hotlist ();
1387 hotlist_state.modified = 0;
1388 if (result) {
1389 remove_old_list = 1;
1390 } else {
1391 char *msg;
1393 msg = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME,
1394 _(" file, your old hotlist entries were not deleted"), NULL);
1396 message (D_ERROR, _(" Hotlist Load "), msg);
1397 g_free (msg);
1399 } else {
1400 hot_load_file (hotlist);
1401 fclose (hotlist_file);
1402 hotlist_state.loaded = 1;
1405 if (remove_old_list) {
1406 clean_up_hotlist_groups ("Hotlist");
1407 sync_profiles ();
1410 stat (hotlist_file_name, &stat_buf);
1411 hotlist_file_mtime = stat_buf.st_mtime;
1412 current_group = hotlist;
1415 static void
1416 save_group (struct hotlist *grp)
1418 struct hotlist *current = grp->head;
1419 char *group_section;
1421 group_section = find_group_section (grp);
1423 profile_clean_section (group_section, profile_name);
1424 for (;current && current->type == HL_TYPE_GROUP; current = current->next){
1425 WritePrivateProfileString (group_section,
1426 current->directory,
1427 current->label,
1428 profile_name);
1430 g_free (group_section);
1432 for (current = grp->head;
1433 current && current->type == HL_TYPE_GROUP;
1434 current = current->next)
1435 save_group (current);
1437 profile_clean_section (grp->directory, profile_name);
1438 for (;current; current = current->next){
1439 WritePrivateProfileString (grp->directory,
1440 current->directory,
1441 current->label,
1442 profile_name);
1446 static int list_level = 0;
1448 static void
1449 hot_save_group (struct hotlist *grp)
1451 struct hotlist *current = grp->head;
1452 int i;
1453 char *s;
1455 #define INDENT(n) \
1456 do { \
1457 for (i = 0; i < n; i++) \
1458 putc (' ', hotlist_file); \
1459 } while (0)
1461 for (;current; current = current->next)
1462 switch (current->type) {
1463 case HL_TYPE_GROUP:
1464 INDENT (list_level);
1465 fputs ("GROUP \"", hotlist_file);
1466 for (s = current->label; *s; s++) {
1467 if (*s == '"')
1468 putc ('\\', hotlist_file);
1469 else if (*s == '\\')
1470 putc ('\\', hotlist_file);
1471 putc (*s, hotlist_file);
1473 fputs ("\"\n", hotlist_file);
1474 list_level += 2;
1475 hot_save_group (current);
1476 list_level -= 2;
1477 INDENT (list_level);
1478 fputs ("ENDGROUP\n", hotlist_file);
1479 break;
1480 case HL_TYPE_ENTRY:
1481 INDENT(list_level);
1482 fputs ("ENTRY \"", hotlist_file);
1483 for (s = current->label; *s; s++) {
1484 if (*s == '"')
1485 putc ('\\', hotlist_file);
1486 else if (*s == '\\')
1487 putc ('\\', hotlist_file);
1488 putc (*s, hotlist_file);
1490 fputs ("\" URL \"", hotlist_file);
1491 for (s = current->directory; *s; s++) {
1492 if (*s == '"')
1493 putc ('\\', hotlist_file);
1494 else if (*s == '\\')
1495 putc ('\\', hotlist_file);
1496 putc (*s, hotlist_file);
1498 fputs ("\"\n", hotlist_file);
1499 break;
1500 case HL_TYPE_COMMENT:
1501 fprintf (hotlist_file, "#%s\n", current->label);
1502 break;
1507 int save_hotlist (void)
1509 int saved = 0;
1510 struct stat stat_buf;
1512 if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) {
1513 char *fbak = g_strconcat (hotlist_file_name, ".bak", NULL);
1515 rename (hotlist_file_name, fbak);
1516 if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) {
1517 if (stat (fbak, &stat_buf) == 0)
1518 chmod (hotlist_file_name, stat_buf.st_mode);
1519 else
1520 chmod (hotlist_file_name, S_IRUSR | S_IWUSR);
1521 hot_save_group (hotlist);
1522 fclose (hotlist_file);
1523 stat (hotlist_file_name, &stat_buf);
1524 hotlist_file_mtime = stat_buf.st_mtime;
1525 saved = 1;
1526 hotlist_state.modified = 0;
1527 } else
1528 rename (fbak, hotlist_file_name);
1529 g_free (fbak);
1532 return saved;
1536 * Unload list from memory.
1537 * Don't confuse with hotlist_done() for GUI.
1539 void done_hotlist (void)
1541 if (hotlist){
1542 remove_group (hotlist);
1543 if (hotlist->label)
1544 g_free (hotlist->label);
1545 if (hotlist->directory)
1546 g_free (hotlist->directory);
1547 g_free (hotlist);
1548 hotlist = 0;
1551 hotlist_state.loaded = 0;
1553 if (hotlist_file_name){
1554 g_free (hotlist_file_name);
1555 hotlist_file_name = 0;
1557 l_hotlist = 0;
1558 current_group = 0;
1559 if (tkn_buf){
1560 g_free (tkn_buf);
1561 tkn_buf_length = 0;
1562 tkn_length = 0;
1563 tkn_buf = NULL;