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