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 https://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 'message' to the end of 'list'. */
47 _notmuch_message_list_add_message (notmuch_message_list_t *list,
48 notmuch_message_t *message)
50 notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
52 node->message = message;
56 list->tail = &node->next;
60 _notmuch_messages_create (notmuch_message_list_t *list)
62 notmuch_messages_t *messages;
64 if (list->head == NULL)
67 messages = talloc (list, notmuch_messages_t);
68 if (unlikely (messages == NULL))
71 messages->is_of_list_type = TRUE;
72 messages->iterator = list->head;
77 /* We're using the "is_of_type_list" to conditionally defer to the
78 * notmuch_mset_messages_t implementation of notmuch_messages_t in
79 * query.cc. It's ugly that that's over in query.cc, and it's ugly
80 * that we're not using a union here. Both of those uglies are due to
83 * 1. I didn't want to force a C++ header file onto
84 * notmuch-private.h and suddenly subject all our code to a
85 * C++ compiler and its rules.
87 * 2. C++ won't allow me to put C++ objects, (with non-trivial
88 * constructors) into a union anyway. Even though I'd
89 * carefully control object construction with placement new
93 notmuch_messages_valid (notmuch_messages_t *messages)
98 if (! messages->is_of_list_type)
99 return _notmuch_mset_messages_valid (messages);
101 return (messages->iterator != NULL);
105 notmuch_messages_get (notmuch_messages_t *messages)
107 if (! messages->is_of_list_type)
108 return _notmuch_mset_messages_get (messages);
110 if (messages->iterator == NULL)
113 return messages->iterator->message;
117 notmuch_messages_move_to_next (notmuch_messages_t *messages)
119 if (! messages->is_of_list_type) {
120 _notmuch_mset_messages_move_to_next (messages);
124 if (messages->iterator == NULL)
127 messages->iterator = messages->iterator->next;
131 notmuch_messages_destroy (notmuch_messages_t *messages)
133 talloc_free (messages);
138 notmuch_messages_collect_tags (notmuch_messages_t *messages)
140 notmuch_string_list_t *tags;
141 notmuch_tags_t *msg_tags;
142 notmuch_message_t *msg;
147 tags = _notmuch_string_list_create (messages);
148 if (tags == NULL) return NULL;
150 htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);
152 while ((msg = notmuch_messages_get (messages))) {
153 msg_tags = notmuch_message_get_tags (msg);
154 while ((tag = notmuch_tags_get (msg_tags))) {
155 g_hash_table_insert (htable, xstrdup (tag), NULL);
156 notmuch_tags_move_to_next (msg_tags);
158 notmuch_tags_destroy (msg_tags);
159 notmuch_message_destroy (msg);
160 notmuch_messages_move_to_next (messages);
163 keys = g_hash_table_get_keys (htable);
164 for (l = keys; l; l = l->next) {
165 _notmuch_string_list_append (tags, (char *)l->data);
169 g_hash_table_destroy (htable);
171 _notmuch_string_list_sort (tags);
172 return _notmuch_tags_create (messages, tags);