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 <stddef.h>
31 #include "mutt/lib.h"
32 #include "address/lib.h"
33 #include "config/lib.h"
34 #include "sort.h"
35 #include "lib.h"
36 #include "alias.h"
37 #include "gui.h"
38 
39 #define RSORT(num) ((sort_alias & SORT_REVERSE) ? -num : num)
40 
41 static short sort_alias = 0;
42 
48 static int alias_sort_name(const void *a, const void *b)
49 {
50  const struct AliasView *av_a = a;
51  const struct AliasView *av_b = b;
52 
53  if (av_a->is_visible != av_b->is_visible)
54  return av_a->is_visible ? -1 : 1;
55 
56  if (!av_a->is_visible)
57  return 0;
58 
59  int r = mutt_str_coll(av_a->alias->name, av_b->alias->name);
60 
61  return RSORT(r);
62 }
63 
69 static int alias_sort_address(const void *a, const void *b)
70 {
71  const struct AliasView *av_a = a;
72  const struct AliasView *av_b = b;
73 
74  const struct AddressList *al_a = &av_a->alias->addr;
75  const struct AddressList *al_b = &av_b->alias->addr;
76 
77  if (av_a->is_visible != av_b->is_visible)
78  return av_a->is_visible ? -1 : 1;
79 
80  if (!av_a->is_visible)
81  return 0;
82 
83  int r;
84  if (al_a == al_b)
85  r = 0;
86  else if (!al_a)
87  r = -1;
88  else if (!al_b)
89  r = 1;
90  else
91  {
92  const struct Address *addr_a = TAILQ_FIRST(al_a);
93  const struct Address *addr_b = TAILQ_FIRST(al_b);
94  if (addr_a && addr_a->personal)
95  {
96  if (addr_b && addr_b->personal)
97  r = mutt_str_coll(addr_a->personal, addr_b->personal);
98  else
99  r = 1;
100  }
101  else if (addr_b && addr_b->personal)
102  r = -1;
103  else if (addr_a && addr_b)
104  r = mutt_str_coll(addr_a->mailbox, addr_b->mailbox);
105  else
106  r = 0;
107  }
108 
109  return RSORT(r);
110 }
111 
117 static int alias_sort_unsort(const void *a, const void *b)
118 {
119  const struct AliasView *av_a = a;
120  const struct AliasView *av_b = b;
121 
122  if (av_a->is_visible != av_b->is_visible)
123  return av_a->is_visible ? -1 : 1;
124 
125  if (!av_a->is_visible)
126  return 0;
127 
128  int r = (av_a->orig_seq - av_b->orig_seq);
129 
130  return RSORT(r);
131 }
132 
137 static sort_t alias_get_sort_function(short sort)
138 {
139  switch ((sort & SORT_MASK))
140  {
141  case SORT_ALIAS:
142  return alias_sort_name;
143  case SORT_ADDRESS:
144  return alias_sort_address;
145  case SORT_ORDER:
146  return alias_sort_unsort;
147  default:
148  return alias_sort_name;
149  }
150 }
151 
157 void alias_array_sort(struct AliasViewArray *ava, const struct ConfigSubset *sub)
158 {
159  if (!ava || ARRAY_EMPTY(ava))
160  return;
161 
162  sort_alias = cs_subset_sort(sub, "sort_alias");
164 
165  struct AliasView *avp = NULL;
166  ARRAY_FOREACH(avp, ava)
167  {
168  avp->num = ARRAY_FOREACH_IDX;
169  }
170 }
char * name
Short name.
Definition: alias.h:35
static int alias_sort_unsort(const void *a, const void *b)
Compare two Aliases by their original configuration position - Implements sort_t. ...
Definition: sort.c:117
#define TAILQ_FIRST(head)
Definition: queue.h:723
void alias_array_sort(struct AliasViewArray *ava, const struct ConfigSubset *sub)
Sort and reindex an AliasViewArray.
Definition: sort.c:157
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:206
Sort by email address.
Definition: sort2.h:54
An email address.
Definition: address.h:35
char * mailbox
Mailbox and host address.
Definition: address.h:38
GUI data wrapping an Alias.
Definition: gui.h:35
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:677
Representation of a single alias to an email address.
Convenience wrapper for the config headers.
Email Address Handling.
Assorted sorting methods.
static int alias_sort_address(const void *a, const void *b)
Compare two Aliases by their Addresses - Implements sort_t.
Definition: sort.c:69
static short sort_alias
Definition: sort.c:41
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:48
int(* sort_t)(const void *a, const void *b)
Prototype for generic comparison function, compatible with qsort.
Definition: sort.h:44
static sort_t alias_get_sort_function(short sort)
Sorting function decision logic.
Definition: sort.c:137
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
bool is_visible
Is visible?
Definition: gui.h:43
#define RSORT(num)
Definition: sort.c:39
A set of inherited config items.
Definition: subset.h:46
struct Alias * alias
Alias.
Definition: gui.h:44
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition: array.h:70
static int alias_sort_name(const void *a, const void *b)
Compare two Aliases by their short names - Implements sort_t.
Definition: sort.c:48
Sort by email alias.
Definition: sort2.h:53
char * personal
Real name of address.
Definition: address.h:37
int orig_seq
Sequence in alias config file.
Definition: gui.h:38
#define ARRAY_SORT(head, fn)
Sort an array.
Definition: array.h:271
struct AddressList addr
List of Addresses the Alias expands to.
Definition: alias.h:36
Convenience wrapper for the library headers.
int num
Index number in list.
Definition: gui.h:37
Shared code for the Alias and Query Dialogs.
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78