NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
sort.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stdbool.h>
31 #include "private.h"
32 #include "mutt/lib.h"
33 #include "config/lib.h"
34 #include "core/lib.h"
35 #include "sort.h"
36 #include "muttlib.h"
37 
39 static bool sb_sort_reverse = false;
40 
44 static int sb_sort_count(const void *a, const void *b)
45 {
46  const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
47  const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
48  const struct Mailbox *m1 = sbe1->mailbox;
49  const struct Mailbox *m2 = sbe2->mailbox;
50 
51  int rc = 0;
52  if (m1->msg_count == m2->msg_count)
54  else
55  rc = (m2->msg_count - m1->msg_count);
56 
57  if (sb_sort_reverse)
58  rc = -rc;
59  return rc;
60 }
61 
65 static int sb_sort_desc(const void *a, const void *b)
66 {
67  const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
68  const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
69  const struct Mailbox *m1 = sbe1->mailbox;
70  const struct Mailbox *m2 = sbe2->mailbox;
71 
72  int rc = mutt_str_cmp(m1->name, m2->name);
73 
74  if (sb_sort_reverse)
75  rc = -rc;
76  return rc;
77 }
78 
82 static int sb_sort_flagged(const void *a, const void *b)
83 {
84  const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
85  const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
86  const struct Mailbox *m1 = sbe1->mailbox;
87  const struct Mailbox *m2 = sbe2->mailbox;
88 
89  int rc = 0;
90  if (m1->msg_flagged == m2->msg_flagged)
92  else
93  rc = (m2->msg_flagged - m1->msg_flagged);
94 
95  if (sb_sort_reverse)
96  rc = -rc;
97  return rc;
98 }
99 
103 static int sb_sort_path(const void *a, const void *b)
104 {
105  const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
106  const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
107  const struct Mailbox *m1 = sbe1->mailbox;
108  const struct Mailbox *m2 = sbe2->mailbox;
109 
110  int rc = 0;
112  if (rc == 0)
113  rc = mutt_str_coll(mailbox_path(m1), mailbox_path(m2));
114 
115  if (sb_sort_reverse)
116  rc = -rc;
117  return rc;
118 }
119 
123 static int sb_sort_unread(const void *a, const void *b)
124 {
125  const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
126  const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
127  const struct Mailbox *m1 = sbe1->mailbox;
128  const struct Mailbox *m2 = sbe2->mailbox;
129 
130  int rc = 0;
131  if (m1->msg_unread == m2->msg_unread)
132  rc = mutt_str_coll(mailbox_path(m1), mailbox_path(m2));
133  else
134  rc = (m2->msg_unread - m1->msg_unread);
135 
136  if (sb_sort_reverse)
137  rc = -rc;
138  return rc;
139 }
140 
144 static int sb_sort_order(const void *a, const void *b)
145 {
146  const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
147  const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
148  const struct Mailbox *m1 = sbe1->mailbox;
149  const struct Mailbox *m2 = sbe2->mailbox;
150 
151  return (sb_sort_reverse ? -1 : 1) * (m1->gen - m2->gen);
152 }
153 
157 static int sb_sort_unsorted(const void *a, const void *b)
158 {
159  const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
160  const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
161 
162  // This sort method isn't affected by the reverse flag
163  return (sbe1->mailbox->gen - sbe2->mailbox->gen);
164 }
165 
177 void sb_sort_entries(struct SidebarWindowData *wdata, enum SortType sort)
178 {
180 
181  switch (sort & SORT_MASK)
182  {
183  case SORT_COUNT:
184  fn = sb_sort_count;
185  break;
186  case SORT_DESC:
187  fn = sb_sort_desc;
188  break;
189  case SORT_FLAGGED:
190  fn = sb_sort_flagged;
191  break;
192  case SORT_PATH:
193  fn = sb_sort_path;
194  break;
195  case SORT_UNREAD:
196  fn = sb_sort_unread;
197  break;
198  case SORT_ORDER:
199  fn = sb_sort_order;
200  default:
201  break;
202  }
203 
204  sb_sort_reverse = (sort & SORT_REVERSE);
205  ARRAY_SORT(&wdata->entries, fn);
206 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:567
int msg_count
Total number of messages.
Definition: mailbox.h:91
SortType
Methods for sorting.
Definition: sort2.h:41
int msg_unread
Number of unread messages.
Definition: mailbox.h:92
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
Sort by the number of flagged emails.
Definition: sort2.h:60
Sort by the folder&#39;s path.
Definition: sort2.h:61
Sidebar private Window data -.
Definition: private.h:62
Info about folders in the sidebar.
Definition: private.h:39
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:677
Convenience wrapper for the config headers.
Assorted sorting methods.
Some miscellaneous functions.
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:44
char * name
A short name for the Mailbox.
Definition: mailbox.h:85
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:48
Convenience wrapper for the core headers.
int(* sort_t)(const void *a, const void *b)
Prototype for generic comparison function, compatible with qsort.
Definition: sort.h:44
A mailbox.
Definition: mailbox.h:81
int mutt_inbox_cmp(const char *a, const char *b)
do two folders share the same path and one is an inbox
Definition: muttlib.c:1611
Sort by the number of unread emails.
Definition: sort2.h:59
Sort by the folder&#39;s description.
Definition: sort2.h:63
#define ARRAY_SORT(head, fn)
Sort an array.
Definition: array.h:271
Sort by number of emails in a folder.
Definition: sort2.h:58
Convenience wrapper for the library headers.
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:79
int gen
Generation number, for sorting.
Definition: mailbox.h:150
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78