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