NeoMutt  2024-12-12-14-g7b49f7
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 <stddef.h>
34#include "private.h"
35#include "mutt/lib.h"
36#include "config/lib.h"
37#include "email/lib.h"
38#include "core/lib.h"
39#include "sort.h"
40
44static int sb_sort_count(const void *a, const void *b, void *sdata)
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 const bool sort_reverse = *(bool *) sdata;
51
52 int rc = 0;
53 if (m1->msg_count == m2->msg_count)
55 else
57
58 return sort_reverse ? -rc : rc;
59}
60
64static int sb_sort_desc(const void *a, const void *b, void *sdata)
65{
66 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
67 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
68 const struct Mailbox *m1 = sbe1->mailbox;
69 const struct Mailbox *m2 = sbe2->mailbox;
70 const bool sort_reverse = *(bool *) sdata;
71
72 int rc = mutt_str_cmp(m1->name, m2->name);
73 return sort_reverse ? -rc : rc;
74}
75
79static int sb_sort_flagged(const void *a, const void *b, void *sdata)
80{
81 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
82 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
83 const struct Mailbox *m1 = sbe1->mailbox;
84 const struct Mailbox *m2 = sbe2->mailbox;
85 const bool sort_reverse = *(bool *) sdata;
86
87 int rc = 0;
88 if (m1->msg_flagged == m2->msg_flagged)
90 else
92
93 return sort_reverse ? -rc : rc;
94}
95
99static int sb_sort_path(const void *a, const void *b, void *sdata)
100{
101 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
102 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
103 const struct Mailbox *m1 = sbe1->mailbox;
104 const struct Mailbox *m2 = sbe2->mailbox;
105 const bool sort_reverse = *(bool *) sdata;
106
107 int rc = 0;
109 if (rc == 0)
111
112 return sort_reverse ? -rc : rc;
113}
114
118static int sb_sort_unread(const void *a, const void *b, void *sdata)
119{
120 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
121 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
122 const struct Mailbox *m1 = sbe1->mailbox;
123 const struct Mailbox *m2 = sbe2->mailbox;
124 const bool sort_reverse = *(bool *) sdata;
125
126 int rc = 0;
127 if (m1->msg_unread == m2->msg_unread)
129 else
131
132 return sort_reverse ? -rc : rc;
133}
134
138static int sb_sort_unsorted(const void *a, const void *b, void *sdata)
139{
140 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
141 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
142 const struct Mailbox *m1 = sbe1->mailbox;
143 const struct Mailbox *m2 = sbe2->mailbox;
144 const bool sort_reverse = *(bool *) sdata;
145
146 int rc = mutt_numeric_cmp(m1->gen, m2->gen);
147 return sort_reverse ? -rc : rc;
148}
149
161void sb_sort_entries(struct SidebarWindowData *wdata, enum EmailSortType sort)
162{
163 sort_t fn = NULL;
164
165 switch (sort & SORT_MASK)
166 {
167 case SB_SORT_COUNT:
168 fn = sb_sort_count;
169 break;
170 case SB_SORT_DESC:
171 fn = sb_sort_desc;
172 break;
173 case SB_SORT_FLAGGED:
174 fn = sb_sort_flagged;
175 break;
176 case SB_SORT_PATH:
177 fn = sb_sort_path;
178 break;
179 case SB_SORT_UNREAD:
180 fn = sb_sort_unread;
181 break;
182 case SB_SORT_UNSORTED:
183 default:
184 fn = sb_sort_unsorted;
185 break;
186 }
187
188 bool sort_reverse = (sort & SORT_REVERSE);
189 ARRAY_SORT(&wdata->entries, fn, &sort_reverse);
190}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition: array.h:279
Convenience wrapper for the config headers.
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:38
#define mutt_numeric_cmp(a, b)
Definition: sort.h:26
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort.h:39
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
Structs that make up an email.
EmailSortType
Methods for sorting Emails.
Definition: sort.h:53
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:64
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:118
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:79
static int sb_sort_unsorted(const void *a, const void *b, void *sdata)
Compare two Sidebar entries by order of creation - Implements sort_t -.
Definition: sort.c:138
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:99
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:44
Convenience wrapper for the library headers.
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 EmailSortType sort)
Sort the Sidebar entries.
Definition: sort.c:161
Sidebar sorting functions.
@ SB_SORT_FLAGGED
Sort by count of flagged messages.
Definition: sort.h:33
@ SB_SORT_COUNT
Sort by total message count.
Definition: sort.h:31
@ SB_SORT_DESC
Sort by mailbox description.
Definition: sort.h:32
@ SB_SORT_PATH
Sort by mailbox path (alphabetically)
Definition: sort.h:34
@ SB_SORT_UNREAD
Sort by count of unread messages.
Definition: sort.h:35
@ SB_SORT_UNSORTED
Sort into the order the mailboxes were configured.
Definition: sort.h:36
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