refactor read function, implement write.
[apps/madmutt.git] / sort.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <lib-lib/lib-lib.h>
11
12 #include <lib-ui/lib-ui.h>
13
14 #include "mutt.h"
15 #include "alias.h"
16 #include "sort.h"
17 #include "thread.h"
18 #include "score.h"
19 #include "mutt_idna.h"
20
21 #define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x
22
23 /* function to use as discriminator when normal sort method is equal */
24 static sort_t *AuxSort = NULL;
25
26 #define AUXSORT(code,a,b) if (!code && AuxSort && !option(OPTAUXSORT)) { \
27   set_option(OPTAUXSORT); \
28   code = AuxSort(a,b); \
29   unset_option(OPTAUXSORT); \
30 } \
31 if (!code) \
32   code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
33
34 static int compare_score (const void *a, const void *b)
35 {
36   HEADER **pa = (HEADER **) a;
37   HEADER **pb = (HEADER **) b;
38   int result = (*pb)->score - (*pa)->score;     /* note that this is reverse */
39
40   AUXSORT (result, a, b);
41   return SORTCODE (result);
42 }
43
44 static int compare_size (const void *a, const void *b)
45 {
46   HEADER **pa = (HEADER **) a;
47   HEADER **pb = (HEADER **) b;
48   int result = (*pa)->content->length - (*pb)->content->length;
49
50   AUXSORT (result, a, b);
51   return SORTCODE (result);
52 }
53
54 static int compare_date_sent (const void *a, const void *b)
55 {
56   HEADER **pa = (HEADER **) a;
57   HEADER **pb = (HEADER **) b;
58   int result = (*pa)->date_sent - (*pb)->date_sent;
59
60   AUXSORT (result, a, b);
61   return SORTCODE (result);
62 }
63
64 static int compare_subject (const void *a, const void *b)
65 {
66   HEADER **pa = (HEADER **) a;
67   HEADER **pb = (HEADER **) b;
68   int rc;
69
70   if (!(*pa)->env->real_subj) {
71     if (!(*pb)->env->real_subj)
72       rc = compare_date_sent (pa, pb);
73     else
74       rc = -1;
75   }
76   else if (!(*pb)->env->real_subj)
77     rc = 1;
78   else
79     rc = m_strcasecmp((*pa)->env->real_subj, (*pb)->env->real_subj);
80   AUXSORT (rc, a, b);
81   return SORTCODE (rc);
82 }
83
84 const char *mutt_get_name (address_t * a)
85 {
86   const address_t *ali;
87   const char *name = "";
88
89   if (a) {
90     if (option (OPTREVALIAS) && (ali = alias_reverse_lookup(a))
91         && ali->personal)
92       name = ali->personal;
93     else if (a->personal)
94       name = a->personal;
95     else if (a->mailbox)
96       name = (mutt_addr_for_display (a));
97   }
98   /* don't return NULL to avoid segfault when printing/comparing */
99   return name;
100 }
101
102 static int compare_to (const void *a, const void *b)
103 {
104   HEADER **ppa = (HEADER **) a;
105   HEADER **ppb = (HEADER **) b;
106   char fa[1024];
107   char fb[1024];
108   int result;
109
110   /* mutt_get_name() will sometimes return a pointer to a static buffer.
111    * On the next call that pointer may get smashed so we copy the return value
112    * to our own memory space. */
113
114   m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->to));
115   m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->to));
116
117   result = m_strcasecmp(fa, fb);
118   AUXSORT (result, a, b);
119   return SORTCODE (result);
120 }
121
122 static int compare_from (const void *a, const void *b)
123 {
124   HEADER **ppa = (HEADER **) a;
125   HEADER **ppb = (HEADER **) b;
126   char fa[1024];
127   char fb[1024];
128   int result;
129
130   /* mutt_get_name() will sometimes return a pointer to a static buffer.
131    * On the next call that pointer may get smashed so we copy the return value
132    * to our own memory space. */
133
134   m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->from));
135   m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->from));
136
137   result = m_strcasecmp(fa, fb);
138   AUXSORT (result, a, b);
139   return SORTCODE (result);
140 }
141
142 static int compare_date_received (const void *a, const void *b)
143 {
144   HEADER **pa = (HEADER **) a;
145   HEADER **pb = (HEADER **) b;
146   int result = (*pa)->received - (*pb)->received;
147
148   AUXSORT (result, a, b);
149   return SORTCODE (result);
150 }
151
152 static int compare_order (const void *a, const void *b)
153 {
154   HEADER **ha = (HEADER **) a;
155   HEADER **hb = (HEADER **) b;
156   /* no need to auxsort because you will never have equality here */
157   return SORTCODE ((*ha)->index - (*hb)->index);
158 }
159
160 static int compare_spam (const void *a, const void *b)
161 {
162   HEADER **ppa = (HEADER **) a;
163   HEADER **ppb = (HEADER **) b;
164   char *aptr, *bptr;
165   int ahas, bhas;
166   int result = 0;
167
168   /* Firstly, require spam attributes for both msgs */
169   /* to compare. Determine which msgs have one.     */
170   ahas = (*ppa)->env && (*ppa)->env->spam;
171   bhas = (*ppb)->env && (*ppb)->env->spam;
172
173   /* If one msg has spam attr but other does not, sort the one with first. */
174   if (ahas && !bhas)
175     return SORTCODE (1);
176   if (!ahas && bhas)
177     return SORTCODE (-1);
178
179   /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
180   if (!ahas && !bhas) {
181     AUXSORT (result, a, b);
182     return SORTCODE (result);
183   }
184
185
186   /* Both have spam attrs. */
187
188   /* preliminary numeric examination */
189   result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
190             strtoul ((*ppb)->env->spam->data, &bptr, 10));
191
192   /* If either aptr or bptr is equal to data, there is no numeric    */
193   /* value for that spam attribute. In this case, compare lexically. */
194   if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
195     return SORTCODE (m_strcmp(aptr, bptr));
196
197   /* Otherwise, we have numeric value for both attrs. If these values */
198   /* are equal, then we first fall back upon string comparison, then  */
199   /* upon auxiliary sort.                                             */
200   if (result == 0) {
201     result = m_strcmp(aptr, bptr);
202     if (result == 0)
203       AUXSORT (result, a, b);
204   }
205
206   return SORTCODE (result);
207 }
208
209 sort_t *mutt_get_sort_func (int method)
210 {
211   switch (method & SORT_MASK) {
212   case SORT_RECEIVED:
213     return compare_date_received;
214   case SORT_ORDER:
215     return compare_order;
216   case SORT_DATE:
217     return compare_date_sent;
218   case SORT_SUBJECT:
219     return compare_subject;
220   case SORT_FROM:
221     return compare_from;
222   case SORT_SIZE:
223     return compare_size;
224   case SORT_TO:
225     return compare_to;
226   case SORT_SCORE:
227     return compare_score;
228   case SORT_SPAM:
229     return compare_spam;
230   default:
231     return NULL;
232   }
233   /* not reached */
234 }
235
236 void mutt_sort_headers (CONTEXT * ctx, int init)
237 {
238   int i;
239   HEADER *h;
240   THREAD *thread, *top;
241   sort_t *sortfunc;
242
243   unset_option (OPTNEEDRESORT);
244
245   if (!ctx)
246     return;
247
248   if (!ctx->msgcount) {
249     /* this function gets called by mutt_sync_mailbox(), which may have just
250      * deleted all the messages.  the virtual message numbers are not updated
251      * in that routine, so we must make sure to zero the vcount member.
252      */
253     ctx->vcount = 0;
254     mutt_clear_threads (ctx);
255     return;                     /* nothing to do! */
256   }
257
258   if (!ctx->quiet)
259     mutt_message _("Sorting mailbox...");
260
261   if (option (OPTNEEDRESCORE) && mod_score.enable) {
262     for (i = 0; i < ctx->msgcount; i++)
263       mutt_score_message (ctx, ctx->hdrs[i], 1);
264   }
265   unset_option (OPTNEEDRESCORE);
266
267   if (option (OPTRESORTINIT)) {
268     unset_option (OPTRESORTINIT);
269     init = 1;
270   }
271
272   if (init && ctx->tree)
273     mutt_clear_threads (ctx);
274
275   if ((Sort & SORT_MASK) == SORT_THREADS) {
276     AuxSort = NULL;
277     /* if $sort_aux changed after the mailbox is sorted, then all the
278        subthreads need to be resorted */
279     if (option (OPTSORTSUBTHREADS)) {
280       i = Sort;
281       Sort = SortAux;
282       if (ctx->tree)
283         ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
284       Sort = i;
285       unset_option (OPTSORTSUBTHREADS);
286     }
287     mutt_sort_threads (ctx, init);
288   }
289   else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
290            (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
291     mutt_error _("Could not find sorting function! [report this bug]");
292
293     mutt_sleep (1);
294     return;
295   }
296   else
297     qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
298
299   /* adjust the virtual message numbers */
300   ctx->vcount = 0;
301   for (i = 0; i < ctx->msgcount; i++) {
302     HEADER *cur = ctx->hdrs[i];
303
304     if (cur->virtual != -1
305         || (cur->collapsed && (!ctx->pattern || cur->limited))) {
306       cur->virtual = ctx->vcount;
307       ctx->v2r[ctx->vcount] = i;
308       ctx->vcount++;
309     }
310     cur->msgno = i;
311   }
312
313   /* re-collapse threads marked as collapsed */
314   if ((Sort & SORT_MASK) == SORT_THREADS) {
315     top = ctx->tree;
316     while ((thread = top) != NULL) {
317       while (!thread->message)
318         thread = thread->child;
319       h = thread->message;
320
321       if (h->collapsed)
322         mutt_collapse_thread (ctx, h);
323       top = top->next;
324     }
325     mutt_set_virtual (ctx);
326   }
327
328   if (!ctx->quiet)
329     mutt_clear_error ();
330 }