1 /* messages.c - Iterator for a set of messages
3 * Copyright © 2009 Carl Worth
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
25 /* Create a new notmuch_message_list_t object, with 'ctx' as its
28 * This function can return NULL in case of out-of-memory.
30 notmuch_message_list_t *
31 _notmuch_message_list_create (const void *ctx)
33 notmuch_message_list_t *list;
35 list = talloc (ctx, notmuch_message_list_t);
36 if (unlikely (list == NULL))
40 list->tail = &list->head;
45 /* Append 'node' (which can of course point to an arbitrarily long
46 * list of nodes) to the end of 'list'.
49 _notmuch_message_list_append (notmuch_message_list_t *list,
50 notmuch_message_node_t *node)
53 list->tail = &node->next;
56 /* Allocate a new node for 'message' and append it to the end of
60 _notmuch_message_list_add_message (notmuch_message_list_t *list,
61 notmuch_message_t *message)
63 notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
65 node->message = message;
68 _notmuch_message_list_append (list, node);
72 _notmuch_messages_create (notmuch_message_list_t *list)
74 notmuch_messages_t *messages;
76 if (list->head == NULL)
79 messages = talloc (list, notmuch_messages_t);
80 if (unlikely (messages == NULL))
83 messages->is_of_list_type = TRUE;
84 messages->iterator = list->head;
89 /* We're using the "is_of_type_list" to conditionally defer to the
90 * notmuch_mset_messages_t implementation of notmuch_messages_t in
91 * query.cc. It's ugly that that's over in query.cc, and it's ugly
92 * that we're not using a union here. Both of those uglies are due to
95 * 1. I didn't want to force a C++ header file onto
96 * notmuch-private.h and suddenly subject all our code to a
97 * C++ compiler and its rules.
99 * 2. C++ won't allow me to put C++ objects, (with non-trivial
100 * constructors) into a union anyway. Even though I'd
101 * carefully control object construction with placement new
105 notmuch_messages_has_more (notmuch_messages_t *messages)
107 if (messages == NULL)
110 if (! messages->is_of_list_type)
111 return _notmuch_mset_messages_has_more (messages);
113 return (messages->iterator != NULL);
117 notmuch_messages_get (notmuch_messages_t *messages)
119 if (! messages->is_of_list_type)
120 return _notmuch_mset_messages_get (messages);
122 if (messages->iterator == NULL)
125 return messages->iterator->message;
129 notmuch_messages_advance (notmuch_messages_t *messages)
131 if (! messages->is_of_list_type)
132 return _notmuch_mset_messages_advance (messages);
134 if (messages->iterator == NULL)
137 messages->iterator = messages->iterator->next;
141 notmuch_messages_destroy (notmuch_messages_t *messages)
143 talloc_free (messages);
148 notmuch_messages_collect_tags (notmuch_messages_t *messages)
150 notmuch_tags_t *tags, *msg_tags;
151 notmuch_message_t *msg;
156 tags = _notmuch_tags_create (messages);
157 if (tags == NULL) return NULL;
159 htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);
161 while ((msg = notmuch_messages_get (messages))) {
162 msg_tags = notmuch_message_get_tags (msg);
163 while ((tag = notmuch_tags_get (msg_tags))) {
164 g_hash_table_insert (htable, xstrdup (tag), NULL);
165 notmuch_tags_advance (msg_tags);
167 notmuch_tags_destroy (msg_tags);
168 notmuch_message_destroy (msg);
169 notmuch_messages_advance (messages);
172 keys = g_hash_table_get_keys (htable);
173 for (l = keys; l; l = l->next) {
174 _notmuch_tags_add_tag (tags, (char *)l->data);
178 g_hash_table_destroy (htable);
180 _notmuch_tags_prepare_iterator (tags);