]> git.notmuchmail.org Git - notmuch/blob - lib/messages.c
Merge branch 'release'
[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         _notmuch_mset_messages_move_to_next (messages);
132         return;
133     }
134
135     if (messages->iterator == NULL)
136         return;
137
138     messages->iterator = messages->iterator->next;
139 }
140
141 void
142 notmuch_messages_destroy (notmuch_messages_t *messages)
143 {
144     talloc_free (messages);
145 }
146
147
148 notmuch_tags_t *
149 notmuch_messages_collect_tags (notmuch_messages_t *messages)
150 {
151     notmuch_string_list_t *tags;
152     notmuch_tags_t *msg_tags;
153     notmuch_message_t *msg;
154     GHashTable *htable;
155     GList *keys, *l;
156     const char *tag;
157
158     tags = _notmuch_string_list_create (messages);
159     if (tags == NULL) return NULL;
160
161     htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);
162
163     while ((msg = notmuch_messages_get (messages))) {
164         msg_tags = notmuch_message_get_tags (msg);
165         while ((tag = notmuch_tags_get (msg_tags))) {
166             g_hash_table_insert (htable, xstrdup (tag), NULL);
167             notmuch_tags_move_to_next (msg_tags);
168         }
169         notmuch_tags_destroy (msg_tags);
170         notmuch_message_destroy (msg);
171         notmuch_messages_move_to_next (messages);
172     }
173
174     keys = g_hash_table_get_keys (htable);
175     for (l = keys; l; l = l->next) {
176         _notmuch_string_list_append (tags, (char *)l->data);
177     }
178
179     g_list_free (keys);
180     g_hash_table_destroy (htable);
181
182     _notmuch_string_list_sort (tags);
183     return _notmuch_tags_create (messages, tags);
184 }