forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tags.c
234 lines (212 loc) · 6 KB
/
tags.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* @file
* Driver based email tags
*
* @authors
* Copyright (C) 2017 Mehdi Abaakouk <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page email_tags Email tags
*
* Driver based email tags
*/
#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "tags.h"
struct HashTable *TagTransforms = NULL; ///< Hash Table: "inbox" -> "i" - Alternative tag names
struct HashTable *TagFormats = NULL; ///< Hash Table: "inbox" -> "GI" - Tag format strings
/**
* driver_tags_getter - Get transformed tags
* @param head List of tags
* @param show_hidden Show hidden tags
* @param show_transformed Show transformed tags
* @param filter Match tags to this string
* @retval ptr String list of tags
*
* Return a new allocated string containing tags separated by space
*/
static char *driver_tags_getter(struct TagList *head, bool show_hidden,
bool show_transformed, const char *filter)
{
if (!head)
return NULL;
char *tags = NULL;
struct Tag *np = NULL;
STAILQ_FOREACH(np, head, entries)
{
if (filter && !mutt_str_equal(np->name, filter))
continue;
if (show_hidden || !np->hidden)
{
if (show_transformed && np->transformed)
mutt_str_append_item(&tags, np->transformed, ' ');
else
mutt_str_append_item(&tags, np->name, ' ');
}
}
return tags;
}
/**
* driver_tags_add - Add a tag to header
* @param[in] list List of tags
* @param[in] new_tag String representing the new tag
*
* Add a tag to the header tags
*
* @note The ownership of the string is passed to the TagList structure
*/
void driver_tags_add(struct TagList *list, char *new_tag)
{
char *new_tag_transformed = mutt_hash_find(TagTransforms, new_tag);
struct Tag *tn = mutt_mem_calloc(1, sizeof(struct Tag));
tn->name = new_tag;
tn->hidden = false;
tn->transformed = mutt_str_dup(new_tag_transformed);
/* filter out hidden tags */
const struct Slist *c_hidden_tags = cs_subset_slist(NeoMutt->sub, "hidden_tags");
if (c_hidden_tags)
if (mutt_list_find(&c_hidden_tags->head, new_tag))
tn->hidden = true;
STAILQ_INSERT_TAIL(list, tn, entries);
}
/**
* driver_tags_free - Free tags from a header
* @param[in] list List of tags
*
* Free the whole tags structure
*/
void driver_tags_free(struct TagList *list)
{
if (!list)
return;
struct Tag *np = STAILQ_FIRST(list);
struct Tag *next = NULL;
while (np)
{
next = STAILQ_NEXT(np, entries);
FREE(&np->name);
FREE(&np->transformed);
FREE(&np);
np = next;
}
STAILQ_INIT(list);
}
/**
* driver_tags_get_transformed - Get transformed tags
* @param[in] list List of tags
* @retval ptr String list of tags
*
* Return a new allocated string containing all tags separated by space with
* transformation
*/
char *driver_tags_get_transformed(struct TagList *list)
{
return driver_tags_getter(list, false, true, NULL);
}
/**
* driver_tags_get - Get tags
* @param[in] list List of tags
* @retval ptr String list of tags
*
* Return a new allocated string containing all tags separated by space
*/
char *driver_tags_get(struct TagList *list)
{
return driver_tags_getter(list, false, false, NULL);
}
/**
* driver_tags_get_with_hidden - Get tags with hiddens
* @param[in] list List of tags
* @retval ptr String list of tags
*
* Return a new allocated string containing all tags separated by space even
* the hiddens.
*/
char *driver_tags_get_with_hidden(struct TagList *list)
{
return driver_tags_getter(list, true, false, NULL);
}
/**
* driver_tags_get_transformed_for - Get transformed tag for a tag name from a header
* @param[in] head List of tags
* @param[in] name Tag to transform
* @retval ptr String tag
*
* Return a new allocated string containing all tags separated by space even
* the hiddens.
*/
char *driver_tags_get_transformed_for(struct TagList *head, const char *name)
{
return driver_tags_getter(head, true, true, name);
}
/**
* driver_tags_replace - Replace all tags
* @param[in] head List of tags
* @param[in] tags string of all tags separated by space
* @retval false No changes are made
* @retval true Tags are updated
*
* Free current tags structures and replace it by new tags
*/
bool driver_tags_replace(struct TagList *head, const char *tags)
{
if (!head)
return false;
driver_tags_free(head);
if (tags)
{
struct ListHead hsplit = STAILQ_HEAD_INITIALIZER(hsplit);
mutt_list_str_split(&hsplit, tags, ' ');
struct ListNode *np = NULL;
STAILQ_FOREACH(np, &hsplit, entries)
{
driver_tags_add(head, np->data);
}
mutt_list_clear(&hsplit);
}
return true;
}
/**
* tags_deleter - Delete a tag - Implements ::hash_hdata_free_t - @ingroup hash_hdata_free_api
*/
static void tags_deleter(int type, void *obj, intptr_t data)
{
FREE(&obj);
}
/**
* driver_tags_init - Initialize structures used for tags
*/
void driver_tags_init(void)
{
TagTransforms = mutt_hash_new(64, MUTT_HASH_STRCASECMP | MUTT_HASH_STRDUP_KEYS);
mutt_hash_set_destructor(TagTransforms, tags_deleter, 0);
TagFormats = mutt_hash_new(64, MUTT_HASH_STRDUP_KEYS);
mutt_hash_set_destructor(TagFormats, tags_deleter, 0);
}
/**
* driver_tags_cleanup - Deinitialize structures used for tags
*/
void driver_tags_cleanup(void)
{
mutt_hash_free(&TagFormats);
mutt_hash_free(&TagTransforms);
}