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 a single 'node' to the end of 'list'.
48 _notmuch_message_list_append (notmuch_message_list_t *list,
49 notmuch_message_node_t *node)
52 list->tail = &node->next;
55 /* Allocate a new node for 'message' and append it to the end of
59 _notmuch_message_list_add_message (notmuch_message_list_t *list,
60 notmuch_message_t *message)
62 notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
64 node->message = message;
67 _notmuch_message_list_append (list, node);
71 _notmuch_messages_create (notmuch_message_list_t *list)
73 notmuch_messages_t *messages;
75 if (list->head == NULL)
78 messages = talloc (list, notmuch_messages_t);
79 if (unlikely (messages == NULL))
82 messages->is_of_list_type = TRUE;
83 messages->iterator = list->head;
88 /* We're using the "is_of_type_list" to conditionally defer to the
89 * notmuch_mset_messages_t implementation of notmuch_messages_t in
90 * query.cc. It's ugly that that's over in query.cc, and it's ugly
91 * that we're not using a union here. Both of those uglies are due to
94 * 1. I didn't want to force a C++ header file onto
95 * notmuch-private.h and suddenly subject all our code to a
96 * C++ compiler and its rules.
98 * 2. C++ won't allow me to put C++ objects, (with non-trivial
99 * constructors) into a union anyway. Even though I'd
100 * carefully control object construction with placement new
104 notmuch_messages_valid (notmuch_messages_t *messages)
106 if (messages == NULL)
109 if (! messages->is_of_list_type)
110 return _notmuch_mset_messages_valid (messages);
112 return (messages->iterator != NULL);
116 notmuch_messages_get (notmuch_messages_t *messages)
118 if (! messages->is_of_list_type)
119 return _notmuch_mset_messages_get (messages);
121 if (messages->iterator == NULL)
124 return messages->iterator->message;
128 notmuch_messages_move_to_next (notmuch_messages_t *messages)
130 if (! messages->is_of_list_type)
131 return _notmuch_mset_messages_move_to_next (messages);
133 if (messages->iterator == NULL)
136 messages->iterator = messages->iterator->next;
140 notmuch_messages_destroy (notmuch_messages_t *messages)
142 talloc_free (messages);
147 notmuch_messages_collect_tags (notmuch_messages_t *messages)
149 notmuch_string_list_t *tags;
150 notmuch_tags_t *msg_tags;
151 notmuch_message_t *msg;
156 tags = _notmuch_string_list_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_move_to_next (msg_tags);
167 notmuch_tags_destroy (msg_tags);
168 notmuch_message_destroy (msg);
169 notmuch_messages_move_to_next (messages);
172 keys = g_hash_table_get_keys (htable);
173 for (l = keys; l; l = l->next) {
174 _notmuch_string_list_append (tags, (char *)l->data);
178 g_hash_table_destroy (htable);
180 _notmuch_string_list_sort (tags);
181 return _notmuch_tags_create (messages, tags);