NeoMutt  2024-10-02-34-g9dd811
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
42static int sb_sort_count(const void *a, const void *b, void *sdata)
43{
44 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
45 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
46 const struct Mailbox *m1 = sbe1->mailbox;
47 const struct Mailbox *m2 = sbe2->mailbox;
48 const bool sort_reverse = *(bool *) sdata;
49
50 int rc = 0;
51 if (m1->msg_count == m2->msg_count)
53 else
55
56 return sort_reverse ? -rc : rc;
57}
58
62static int sb_sort_desc(const void *a, const void *b, void *sdata)
63{
64 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
65 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
66 const struct Mailbox *m1 = sbe1->mailbox;
67 const struct Mailbox *m2 = sbe2->mailbox;
68 const bool sort_reverse = *(bool *) sdata;
69
70 int rc = mutt_str_cmp(m1->name, m2->name);
71 return sort_reverse ? -rc : rc;
72}
73
77static int sb_sort_flagged(const void *a, const void *b, void *sdata)
78{
79 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
80 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
81 const struct Mailbox *m1 = sbe1->mailbox;
82 const struct Mailbox *m2 = sbe2->mailbox;
83 const bool sort_reverse = *(bool *) sdata;
84
85 int rc = 0;
86 if (m1->msg_flagged == m2->msg_flagged)
88 else
90
91 return sort_reverse ? -rc : rc;
92}
93
97static int sb_sort_path(const void *a, const void *b, void *sdata)
98{
99 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
100 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
101 const struct Mailbox *m1 = sbe1->mailbox;
102 const struct Mailbox *m2 = sbe2->mailbox;
103 const bool sort_reverse = *(bool *) sdata;
104
105 int rc = 0;
107 if (rc == 0)
109
110 return sort_reverse ? -rc : rc;
111}
112
116static int sb_sort_unread(const void *a, const void *b, void *sdata)
117{
118 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
119 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
120 const struct Mailbox *m1 = sbe1->mailbox;
121 const struct Mailbox *m2 = sbe2->mailbox;
122 const bool sort_reverse = *(bool *) sdata;
123
124 int rc = 0;
125 if (m1->msg_unread == m2->msg_unread)
127 else
129
130 return sort_reverse ? -rc : rc;
131}
132
136static int sb_sort_order(const void *a, const void *b, void *sdata)
137{
138 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
139 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
140 const struct Mailbox *m1 = sbe1->mailbox;
141 const struct Mailbox *m2 = sbe2->mailbox;
142 const bool sort_reverse = *(bool *) sdata;
143
144 int rc = mutt_numeric_cmp(m1->gen, m2->gen);
145 return sort_reverse ? -rc : rc;
146}
147
151static int sb_sort_unsorted(const void *a, const void *b, void *sdata)
152{
153 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
154 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
155
156 // This sort method isn't affected by the reverse flag
157 return (sbe1->mailbox->gen - sbe2->mailbox->gen);
158}
159
171void sb_sort_entries(struct SidebarWindowData *wdata, enum SortType sort)
172{
174
175 switch (sort & SORT_MASK)
176 {
177 case SORT_COUNT:
178 fn = sb_sort_count;
179 break;
180 case SORT_DESC:
181 fn = sb_sort_desc;
182 break;
183 case SORT_FLAGGED:
184 fn = sb_sort_flagged;
185 break;
186 case SORT_PATH:
187 fn = sb_sort_path;
188 break;
189 case SORT_UNREAD:
190 fn = sb_sort_unread;
191 break;
192 case SORT_ORDER:
193 fn = sb_sort_order;
195 default:
196 break;
197 }
198
199 bool sort_reverse = (sort & SORT_REVERSE);
200 ARRAY_SORT(&wdata->entries, fn, &sort_reverse);
201}
#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:62
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:116
int mutt_str_inbox_cmp(const char *a, const char *b)
Do two folders share the same path and one is an inbox -.
Definition: string.c:887
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:77
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:151
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:136
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:97
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:42
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:399
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:509
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:171
#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