2 * Copyright (C) 1996-2000,2007 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_menu.h"
28 #include "mutt_idna.h"
34 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
36 static struct mapping_t AliasHelp[] = {
37 { N_("Exit"), OP_EXIT },
38 { N_("Del"), OP_DELETE },
39 { N_("Undel"), OP_UNDELETE },
40 { N_("Select"), OP_GENERIC_SELECT_ENTRY },
41 { N_("Help"), OP_HELP },
46 alias_format_str (char *dest, size_t destlen, size_t col, char op, const char *src,
47 const char *fmt, const char *ifstring, const char *elsestring,
48 unsigned long data, format_flag flags)
50 char tmp[SHORT_STRING], adr[SHORT_STRING];
51 ALIAS *alias = (ALIAS *) data;
56 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
57 snprintf (dest, destlen, tmp, alias->del ? "D" : " ");
60 mutt_format_s (dest, destlen, fmt, alias->name);
64 rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
65 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
66 snprintf (dest, destlen, tmp, adr);
69 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
70 snprintf (dest, destlen, tmp, alias->num + 1);
73 dest[0] = alias->tagged ? '*' : ' ';
81 static void alias_entry (char *s, size_t slen, MUTTMENU *m, int num)
83 mutt_FormatString (s, slen, 0, NONULL (AliasFmt), alias_format_str, (unsigned long) ((ALIAS **) m->data)[num], M_FORMAT_ARROWCURSOR);
86 static int alias_tag (MUTTMENU *menu, int n, int m)
88 ALIAS *cur = ((ALIAS **) menu->data)[n];
91 cur->tagged = (m >= 0 ? m : !cur->tagged);
93 return cur->tagged - ot;
96 static int alias_SortAlias (const void *a, const void *b)
98 ALIAS *pa = *(ALIAS **) a;
99 ALIAS *pb = *(ALIAS **) b;
100 int r = mutt_strcasecmp (pa->name, pb->name);
105 static int alias_SortAddress (const void *a, const void *b)
107 ADDRESS *pa = (*(ALIAS **) a)->addr;
108 ADDRESS *pb = (*(ALIAS **) b)->addr;
117 else if (pa->personal)
120 r = mutt_strcasecmp (pa->personal, pb->personal);
124 else if (pb->personal)
127 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
131 void mutt_alias_menu (char *buf, size_t buflen, ALIAS *aliases)
135 ALIAS **AliasTable = NULL;
139 char helpstr[LONG_STRING];
145 mutt_error _("You have no aliases!");
149 /* tell whoever called me to redraw the screen when I return */
150 set_option (OPTNEEDREDRAW);
152 menu = mutt_new_menu (MENU_ALIAS);
153 menu->make_entry = alias_entry;
154 menu->tag = alias_tag;
155 menu->title = _("Aliases");
156 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
162 /* count the number of aliases */
163 for (aliasp = aliases; aliasp; aliasp = aliasp->next)
165 aliasp->self->del = 0;
166 aliasp->self->tagged = 0;
170 safe_realloc (&AliasTable, menu->max * sizeof (ALIAS *));
171 menu->data = AliasTable;
173 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++)
175 AliasTable[i] = aliasp->self;
179 if ((SortAlias & SORT_MASK) != SORT_ORDER)
181 qsort (AliasTable, i, sizeof (ALIAS *),
182 (SortAlias & SORT_MASK) == SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
185 for (i=0; i<menu->max; i++) AliasTable[i]->num = i;
191 menu->redraw |= REDRAW_FULL;
192 aliases = aliases->next;
196 switch ((op = mutt_menuLoop (menu)))
202 for (i = 0; i < menu->max; i++)
203 if (AliasTable[i]->tagged)
204 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
205 menu->redraw |= REDRAW_INDEX;
209 AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
210 menu->redraw |= REDRAW_CURRENT;
211 if (option (OPTRESOLVE) && menu->current < menu->max - 1)
214 menu->redraw |= REDRAW_INDEX;
218 case OP_GENERIC_SELECT_ENTRY:
226 for (i = 0; i < menu->max; i++)
228 if (AliasTable[i]->tagged)
230 mutt_addrlist_to_local (AliasTable[i]->addr);
231 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
238 mutt_addrlist_to_local (AliasTable[t]->addr);
239 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
242 mutt_menuDestroy (&menu);