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