]> git.notmuchmail.org Git - notmuch/blob - lib/messages.c
lib: Add new implementation of notmuch_filenames_t
[notmuch] / lib / messages.c
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 http://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 *
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 a single 'node' to the end of 'list'.
46  */
47 void
48 _notmuch_message_list_append (notmuch_message_list_t *list,
49                               notmuch_message_node_t *node)
50 {
51     *(list->tail) = node;
52     list->tail = &node->next;
53 }
54
55 /* Allocate a new node for 'message' and append it to the end of
56  * 'list'.
57  */
58 void
59 _notmuch_message_list_add_message (notmuch_message_list_t *list,
60                                    notmuch_message_t *message)
61 {
62     notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
63
64     node->message = message;
65     node->next = NULL;
66
67     _notmuch_message_list_append (list, node);
68 }
69
70 notmuch_messages_t *
71 _notmuch_messages_create (notmuch_message_list_t *list)
72 {
73     notmuch_messages_t *messages;
74
75     if (list->head == NULL)
76         return NULL;
77
78     messages = talloc (list, notmuch_messages_t);
79     if (unlikely (messages == NULL))
80         return NULL;
81
82     messages->is_of_list_type = TRUE;
83     messages->iterator = list->head;
84
85     return messages;
86 }
87
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
92  * C++:
93  *
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.
97  *
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
101  *         anyway. *sigh*
102  */
103 notmuch_bool_t
104 notmuch_messages_valid (notmuch_messages_t *messages)
105 {
106     if (messages == NULL)
107         return FALSE;
108
109     if (! messages->is_of_list_type)
110         return _notmuch_mset_messages_valid (messages);
111
112     return (messages->iterator != NULL);
113 }
114
115 notmuch_message_t *
116 notmuch_messages_get (notmuch_messages_t *messages)
117 {
118     if (! messages->is_of_list_type)
119         return _notmuch_mset_messages_get (messages);
120
121     if (messages->iterator == NULL)
122         return NULL;
123
124     return messages->iterator->message;
125 }
126
127 void
128 notmuch_messages_move_to_next (notmuch_messages_t *messages)
129 {
130     if (! messages->is_of_list_type)
131         return _notmuch_mset_messages_move_to_next (messages);
132
133     if (messages->iterator == NULL)
134         return;
135
136     messages->iterator = messages->iterator->next;
137 }
138
139 void
140 notmuch_messages_destroy (notmuch_messages_t *messages)
141 {
142     talloc_free (messages);
143 }
144
145
146 notmuch_tags_t *
147 notmuch_messages_collect_tags (notmuch_messages_t *messages)
148 {
149     notmuch_tags_t *tags, *msg_tags;
150     notmuch_message_t *msg;
151     GHashTable *htable;
152     GList *keys, *l;
153     const char *tag;
154
155     tags = _notmuch_tags_create (messages);
156     if (tags == NULL) return NULL;
157
158     htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);
159
160     while ((msg = notmuch_messages_get (messages))) {
161         msg_tags = notmuch_message_get_tags (msg);
162         while ((tag = notmuch_tags_get (msg_tags))) {
163             g_hash_table_insert (htable, xstrdup (tag), NULL);
164             notmuch_tags_move_to_next (msg_tags);
165         }
166         notmuch_tags_destroy (msg_tags);
167         notmuch_message_destroy (msg);
168         notmuch_messages_move_to_next (messages);
169     }
170
171     keys = g_hash_table_get_keys (htable);
172     for (l = keys; l; l = l->next) {
173         _notmuch_tags_add_tag (tags, (char *)l->data);
174     }
175
176     g_list_free (keys);
177     g_hash_table_destroy (htable);
178
179     _notmuch_tags_prepare_iterator (tags);
180     return tags;
181 }