8be76f646fcbf9c3fb669390723d4d4114e9d9ef
[apps/madmutt.git] / addrbook.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
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.
8  * 
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.
13  * 
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "mutt_menu.h"
25 #include "mapping.h"
26 #include "sort.h"
27
28 #include "mutt_idna.h"
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
35
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},
42   {NULL}
43 };
44
45 static const char *alias_format_str (char *dest, size_t destlen, char op,
46                                      const char *src, const char *fmt,
47                                      const char *ifstring,
48                                      const char *elsestring,
49                                      unsigned long data, format_flag flags)
50 {
51   char tmp[SHORT_STRING], adr[SHORT_STRING];
52   ALIAS *alias = (ALIAS *) data;
53
54   switch (op) {
55   case 'f':
56     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
57     snprintf (dest, destlen, tmp, alias->del ? "D" : " ");
58     break;
59   case 'a':
60     mutt_format_s (dest, destlen, fmt, alias->name);
61     break;
62   case 'r':
63     adr[0] = 0;
64     rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
65     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
66     snprintf (dest, destlen, tmp, adr);
67     break;
68   case 'n':
69     snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
70     snprintf (dest, destlen, tmp, alias->num + 1);
71     break;
72   case 't':
73     dest[0] = alias->tagged ? '*' : ' ';
74     dest[1] = 0;
75     break;
76   }
77
78   return (src);
79 }
80
81 static void alias_entry (char *s, size_t slen, MUTTMENU * m, int num)
82 {
83   mutt_FormatString (s, slen, NONULL (AliasFmt), alias_format_str,
84                      (unsigned long) ((ALIAS **) m->data)[num],
85                      M_FORMAT_ARROWCURSOR);
86 }
87
88 static int alias_tag (MUTTMENU * menu, int n, int m)
89 {
90   ALIAS *cur = ((ALIAS **) menu->data)[n];
91   int ot = cur->tagged;
92
93   cur->tagged = (m >= 0 ? m : !cur->tagged);
94
95   return cur->tagged - ot;
96 }
97
98 static int alias_SortAlias (const void *a, const void *b)
99 {
100   ALIAS *pa = *(ALIAS **) a;
101   ALIAS *pb = *(ALIAS **) b;
102   int r = mutt_strcasecmp (pa->name, pb->name);
103
104   return (RSORT (r));
105 }
106
107 static int alias_SortAddress (const void *a, const void *b)
108 {
109   ADDRESS *pa = (*(ALIAS **) a)->addr;
110   ADDRESS *pb = (*(ALIAS **) b)->addr;
111   int r;
112
113   if (pa == pb)
114     r = 0;
115   else if (pa == NULL)
116     r = -1;
117   else if (pb == NULL)
118     r = 1;
119   else if (pa->personal) {
120     if (pb->personal)
121       r = mutt_strcasecmp (pa->personal, pb->personal);
122     else
123       r = 1;
124   }
125   else if (pb->personal)
126     r = -1;
127   else
128     r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
129   return (RSORT (r));
130 }
131
132 void mutt_alias_menu (char *buf, size_t buflen, ALIAS * aliases)
133 {
134   ALIAS *aliasp;
135   MUTTMENU *menu;
136   ALIAS **AliasTable = NULL;
137   int t = -1;
138   int i, done = 0;
139   int op;
140   char helpstr[SHORT_STRING];
141
142   int omax;
143
144   if (!aliases) {
145     mutt_error _("You have no aliases!");
146
147     return;
148   }
149
150   /* tell whoever called me to redraw the screen when I return */
151   set_option (OPTNEEDREDRAW);
152
153   menu = mutt_new_menu ();
154   menu->make_entry = alias_entry;
155   menu->tag = alias_tag;
156   menu->menu = MENU_ALIAS;
157   menu->title = _("Aliases");
158   menu->help =
159     mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
160
161 new_aliases:
162
163   omax = menu->max;
164
165   /* count the number of aliases */
166   for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
167     aliasp->self->del = 0;
168     aliasp->self->tagged = 0;
169     menu->max++;
170   }
171
172   safe_realloc (&AliasTable, menu->max * sizeof (ALIAS *));
173   menu->data = AliasTable;
174
175   for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
176     AliasTable[i] = aliasp->self;
177     aliases = aliasp;
178   }
179
180   if ((SortAlias & SORT_MASK) != SORT_ORDER) {
181     qsort (AliasTable, i, sizeof (ALIAS *),
182            (SortAlias & SORT_MASK) ==
183            SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
184   }
185
186   for (i = 0; i < menu->max; i++)
187     AliasTable[i]->num = i;
188
189   while (!done) {
190     if (aliases->next) {
191       menu->redraw |= REDRAW_FULL;
192       aliases = aliases->next;
193       goto new_aliases;
194     }
195
196     switch ((op = mutt_menuLoop (menu))) {
197     case OP_DELETE:
198     case OP_UNDELETE:
199       if (menu->tagprefix) {
200         for (i = 0; i < menu->max; i++)
201           if (AliasTable[i]->tagged)
202             AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
203         menu->redraw |= REDRAW_INDEX;
204       }
205       else {
206         AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
207         menu->redraw |= REDRAW_CURRENT;
208         if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
209           menu->current++;
210           menu->redraw |= REDRAW_INDEX;
211         }
212       }
213       break;
214     case OP_GENERIC_SELECT_ENTRY:
215       t = menu->current;
216     case OP_EXIT:
217       done = 1;
218       break;
219     }
220   }
221
222   for (i = 0; i < menu->max; i++) {
223     if (AliasTable[i]->tagged) {
224       mutt_addrlist_to_local (AliasTable[i]->addr);
225       rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
226       t = -1;
227     }
228   }
229
230   if (t != -1) {
231     mutt_addrlist_to_local (AliasTable[t]->addr);
232     rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
233   }
234
235   mutt_menuDestroy (&menu);
236   FREE (&AliasTable);
237
238 }