NeoMutt  2024-03-23-142-g2b2e76
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stdbool.h>
33#include "private.h"
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "sort.h"
38#include "muttlib.h"
39
43static int sb_sort_count(const void *a, const void *b, void *sdata)
44{
45 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
46 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
47 const struct Mailbox *m1 = sbe1->mailbox;
48 const struct Mailbox *m2 = sbe2->mailbox;
49 const bool sort_reverse = *(bool *) sdata;
50
51 int rc = 0;
52 if (m1->msg_count == m2->msg_count)
54 else
56
57 return sort_reverse ? -rc : rc;
58}
59
63static int sb_sort_desc(const void *a, const void *b, void *sdata)
64{
65 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
66 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
67 const struct Mailbox *m1 = sbe1->mailbox;
68 const struct Mailbox *m2 = sbe2->mailbox;
69 const bool sort_reverse = *(bool *) sdata;
70
71 int rc = mutt_str_cmp(m1->name, m2->name);
72 return sort_reverse ? -rc : rc;
73}
74
78static int sb_sort_flagged(const void *a, const void *b, void *sdata)
79{
80 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
81 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
82 const struct Mailbox *m1 = sbe1->mailbox;
83 const struct Mailbox *m2 = sbe2->mailbox;
84 const bool sort_reverse = *(bool *) sdata;
85
86 int rc = 0;
87 if (m1->msg_flagged == m2->msg_flagged)
89 else
91
92 return sort_reverse ? -rc : rc;
93}
94
98static int sb_sort_path(const void *a, const void *b, void *sdata)
99{
100 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
101 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
102 const struct Mailbox *m1 = sbe1->mailbox;
103 const struct Mailbox *m2 = sbe2->mailbox;
104 const bool sort_reverse = *(bool *) sdata;
105
106 int rc = 0;
108 if (rc == 0)
110
111 return sort_reverse ? -rc : rc;
112}
113
117static int sb_sort_unread(const void *a, const void *b, void *sdata)
118{
119 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
120 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
121 const struct Mailbox *m1 = sbe1->mailbox;
122 const struct Mailbox *m2 = sbe2->mailbox;
123 const bool sort_reverse = *(bool *) sdata;
124
125 int rc = 0;
126 if (m1->msg_unread == m2->msg_unread)
128 else
130
131 return sort_reverse ? -rc : rc;
132}
133
137static int sb_sort_order(const void *a, const void *b, void *sdata)
138{
139 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
140 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
141 const struct Mailbox *m1 = sbe1->mailbox;
142 const struct Mailbox *m2 = sbe2->mailbox;
143 const bool sort_reverse = *(bool *) sdata;
144
145 int rc = mutt_numeric_cmp(m1->gen, m2->gen);
146 return sort_reverse ? -rc : rc;
147}
148
152static int sb_sort_unsorted(const void *a, const void *b, void *sdata)
153{
154 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
155 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
156
157 // This sort method isn't affected by the reverse flag
158 return (sbe1->mailbox->gen - sbe2->mailbox->gen);
159}
160
172void sb_sort_entries(struct SidebarWindowData *wdata, enum SortType sort)
173{
175
176 switch (sort & SORT_MASK)
177 {
178 case SORT_COUNT:
179 fn = sb_sort_count;
180 break;
181 case SORT_DESC:
182 fn = sb_sort_desc;
183 break;
184 case SORT_FLAGGED:
185 fn = sb_sort_flagged;
186 break;
187 case SORT_PATH:
188 fn = sb_sort_path;
189 break;
190 case SORT_UNREAD:
191 fn = sb_sort_unread;
192 break;
193 case SORT_ORDER:
194 fn = sb_sort_order;
196 default:
197 break;
198 }
199
200 bool sort_reverse = (sort & SORT_REVERSE);
201 ARRAY_SORT(&wdata->entries, fn, &sort_reverse);
202}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition: array.h:279
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:223
static int sb_sort_desc(const void *a, const void *b, void *sdata)
Compare two Sidebar entries by description - Implements sort_t -.
Definition: sort.c:63
static int sb_sort_unread(const void *a, const void *b, void *sdata)
Compare two Sidebar entries by unread - Implements sort_t -.
Definition: sort.c:117
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:1033
static int sb_sort_flagged(const void *a, const void *b, void *sdata)
Compare two Sidebar entries by flagged - Implements sort_t -.
Definition: sort.c:78
static int sb_sort_unsorted(const void *a, const void *b, void *sdata)
Compare two Sidebar entries into their original order - Implements sort_t -.
Definition: sort.c:152
static int sb_sort_order(const void *a, const void *b, void *sdata)
Compare two Sidebar entries by order of creation - Implements sort_t -.
Definition: sort.c:137
static int sb_sort_path(const void *a, const void *b, void *sdata)
Compare two Sidebar entries by path - Implements sort_t -.
Definition: sort.c:98
static int sb_sort_count(const void *a, const void *b, void *sdata)
Compare two Sidebar entries by count - Implements sort_t -.
Definition: sort.c:43
Convenience wrapper for the library headers.
#define FALLTHROUGH
Definition: lib.h:111
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:448
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:558
Some miscellaneous functions.
int(* sort_t)(const void *a, const void *b, void *sdata)
Definition: qsort_r.h:41
GUI display the mailboxes in a side panel.
void sb_sort_entries(struct SidebarWindowData *wdata, enum SortType sort)
Sort the Sidebar entries.
Definition: sort.c:172
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:70
SortType
Methods for sorting.
Definition: sort2.h:34
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:40
@ SORT_PATH
Sort by the folder's path.
Definition: sort2.h:53
@ SORT_FLAGGED
Sort by the number of flagged emails.
Definition: sort2.h:52
@ SORT_DESC
Sort by the folder's description.
Definition: sort2.h:55
@ SORT_COUNT
Sort by number of emails in a folder.
Definition: sort2.h:50
@ SORT_UNREAD
Sort by the number of unread emails.
Definition: sort2.h:51
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:71
Assorted sorting methods.
#define mutt_numeric_cmp(a, b)
Definition: sort.h:35
A mailbox.
Definition: mailbox.h:79
int msg_count
Total number of messages.
Definition: mailbox.h:88
char * name
A short name for the Mailbox.
Definition: mailbox.h:82
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:90
int msg_unread
Number of unread messages.
Definition: mailbox.h:89
int gen
Generation number, for sorting.
Definition: mailbox.h:147
Info about folders in the sidebar.
Definition: private.h:41
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:45
Sidebar private Window data -.
Definition: private.h:88
struct SbEntryArray entries
Items to display in the sidebar.
Definition: private.h:91