1 /* messages.c - Iterator for a set of messages
2 *
3 * Copyright © 2009 Carl Worth
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
17 *
18 * Author: Carl Worth <cworth@cworth.org>
19 */
20
21 #include "notmuch-private.h"
22
23 #include <glib.h>
24
25 /* Create a new notmuch_message_list_t object, with 'ctx' as its
26 * talloc owner.
27 *
28 * This function can return NULL in case of out-of-memory.
29 */
30 notmuch_message_list_t *
_notmuch_message_list_create(const void * ctx)31 _notmuch_message_list_create (const void *ctx)
32 {
33 notmuch_message_list_t *list;
34
35 list = talloc (ctx, notmuch_message_list_t);
36 if (unlikely (list == NULL))
37 return NULL;
38
39 list->head = NULL;
40 list->tail = &list->head;
41
42 return list;
43 }
44
45 /* Append 'message' to the end of 'list'. */
46 void
_notmuch_message_list_add_message(notmuch_message_list_t * list,notmuch_message_t * message)47 _notmuch_message_list_add_message (notmuch_message_list_t *list,
48 notmuch_message_t *message)
49 {
50 notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
51
52 node->message = message;
53 node->next = NULL;
54
55 *(list->tail) = node;
56 list->tail = &node->next;
57 }
58
59 bool
_notmuch_message_list_empty(notmuch_message_list_t * list)60 _notmuch_message_list_empty (notmuch_message_list_t *list)
61 {
62 if (list == NULL)
63 return TRUE;
64
65 return (list->head == NULL);
66 }
67
68 notmuch_messages_t *
_notmuch_messages_create(notmuch_message_list_t * list)69 _notmuch_messages_create (notmuch_message_list_t *list)
70 {
71 notmuch_messages_t *messages;
72
73 if (list->head == NULL)
74 return NULL;
75
76 messages = talloc (list, notmuch_messages_t);
77 if (unlikely (messages == NULL))
78 return NULL;
79
80 messages->is_of_list_type = true;
81 messages->iterator = list->head;
82
83 return messages;
84 }
85
86 /* We're using the "is_of_type_list" to conditionally defer to the
87 * notmuch_mset_messages_t implementation of notmuch_messages_t in
88 * query.cc. It's ugly that that's over in query.cc, and it's ugly
89 * that we're not using a union here. Both of those uglies are due to
90 * C++:
91 *
92 * 1. I didn't want to force a C++ header file onto
93 * notmuch-private.h and suddenly subject all our code to a
94 * C++ compiler and its rules.
95 *
96 * 2. C++ won't allow me to put C++ objects, (with non-trivial
97 * constructors) into a union anyway. Even though I'd
98 * carefully control object construction with placement new
99 * anyway. *sigh*
100 */
101 notmuch_bool_t
notmuch_messages_valid(notmuch_messages_t * messages)102 notmuch_messages_valid (notmuch_messages_t *messages)
103 {
104 if (messages == NULL)
105 return false;
106
107 if (! messages->is_of_list_type)
108 return _notmuch_mset_messages_valid (messages);
109
110 return (messages->iterator != NULL);
111 }
112
113 bool
_notmuch_messages_has_next(notmuch_messages_t * messages)114 _notmuch_messages_has_next (notmuch_messages_t *messages)
115 {
116 if (! notmuch_messages_valid (messages))
117 return false;
118
119 if (! messages->is_of_list_type)
120 INTERNAL_ERROR ("_notmuch_messages_has_next not implemented for msets");
121
122 return (messages->iterator->next != NULL);
123 }
124
125 notmuch_message_t *
notmuch_messages_get(notmuch_messages_t * messages)126 notmuch_messages_get (notmuch_messages_t *messages)
127 {
128 if (! messages->is_of_list_type)
129 return _notmuch_mset_messages_get (messages);
130
131 if (messages->iterator == NULL)
132 return NULL;
133
134 return messages->iterator->message;
135 }
136
137 void
notmuch_messages_move_to_next(notmuch_messages_t * messages)138 notmuch_messages_move_to_next (notmuch_messages_t *messages)
139 {
140 if (! messages->is_of_list_type) {
141 _notmuch_mset_messages_move_to_next (messages);
142 return;
143 }
144
145 if (messages->iterator == NULL)
146 return;
147
148 messages->iterator = messages->iterator->next;
149 }
150
151 void
notmuch_messages_destroy(notmuch_messages_t * messages)152 notmuch_messages_destroy (notmuch_messages_t *messages)
153 {
154 talloc_free (messages);
155 }
156
157
158 notmuch_tags_t *
notmuch_messages_collect_tags(notmuch_messages_t * messages)159 notmuch_messages_collect_tags (notmuch_messages_t *messages)
160 {
161 notmuch_string_list_t *tags;
162 notmuch_tags_t *msg_tags;
163 notmuch_message_t *msg;
164 GHashTable *htable;
165 GList *keys, *l;
166 const char *tag;
167
168 tags = _notmuch_string_list_create (messages);
169 if (tags == NULL) return NULL;
170
171 htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);
172
173 while ((msg = notmuch_messages_get (messages))) {
174 msg_tags = notmuch_message_get_tags (msg);
175 while ((tag = notmuch_tags_get (msg_tags))) {
176 g_hash_table_insert (htable, xstrdup (tag), NULL);
177 notmuch_tags_move_to_next (msg_tags);
178 }
179 notmuch_tags_destroy (msg_tags);
180 notmuch_message_destroy (msg);
181 notmuch_messages_move_to_next (messages);
182 }
183
184 keys = g_hash_table_get_keys (htable);
185 for (l = keys; l; l = l->next) {
186 _notmuch_string_list_append (tags, (char *) l->data);
187 }
188
189 g_list_free (keys);
190 g_hash_table_destroy (htable);
191
192 _notmuch_string_list_sort (tags);
193 return _notmuch_tags_create (messages, tags);
194 }
195