]> git.notmuchmail.org Git - notmuch/blob - lib/messages.c
lib: Bump library version from 3.0.0 to 3.1.0
[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 'message' to the end of 'list'. */
46 void
47 _notmuch_message_list_add_message (notmuch_message_list_t *list,
48                                    notmuch_message_t *message)
49 {
50     notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
51
52     node->message = message;
53     node->next = NULL;
54
55     *(list->tail) = node;
56     list->tail = &node->next;
57 }
58
59 notmuch_messages_t *
60 _notmuch_messages_create (notmuch_message_list_t *list)
61 {
62     notmuch_messages_t *messages;
63
64     if (list->head == NULL)
65         return NULL;
66
67     messages = talloc (list, notmuch_messages_t);
68     if (unlikely (messages == NULL))
69         return NULL;
70
71     messages->is_of_list_type = TRUE;
72     messages->iterator = list->head;
73
74     return messages;
75 }
76
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
81  * C++:
82  *
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.
86  *
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
90  *         anyway. *sigh*
91  */
92 notmuch_bool_t
93 notmuch_messages_valid (notmuch_messages_t *messages)
94 {
95     if (messages == NULL)
96         return FALSE;
97
98     if (! messages->is_of_list_type)
99         return _notmuch_mset_messages_valid (messages);
100
101     return (messages->iterator != NULL);
102 }
103
104 notmuch_message_t *
105 notmuch_messages_get (notmuch_messages_t *messages)
106 {
107     if (! messages->is_of_list_type)
108         return _notmuch_mset_messages_get (messages);
109
110     if (messages->iterator == NULL)
111         return NULL;
112
113     return messages->iterator->message;
114 }
115
116 void
117 notmuch_messages_move_to_next (notmuch_messages_t *messages)
118 {
119     if (! messages->is_of_list_type) {
120         _notmuch_mset_messages_move_to_next (messages);
121         return;
122     }
123
124     if (messages->iterator == NULL)
125         return;
126
127     messages->iterator = messages->iterator->next;
128 }
129
130 void
131 notmuch_messages_destroy (notmuch_messages_t *messages)
132 {
133     talloc_free (messages);
134 }
135
136
137 notmuch_tags_t *
138 notmuch_messages_collect_tags (notmuch_messages_t *messages)
139 {
140     notmuch_string_list_t *tags;
141     notmuch_tags_t *msg_tags;
142     notmuch_message_t *msg;
143     GHashTable *htable;
144     GList *keys, *l;
145     const char *tag;
146
147     tags = _notmuch_string_list_create (messages);
148     if (tags == NULL) return NULL;
149
150     htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);
151
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);
157         }
158         notmuch_tags_destroy (msg_tags);
159         notmuch_message_destroy (msg);
160         notmuch_messages_move_to_next (messages);
161     }
162
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);
166     }
167
168     g_list_free (keys);
169     g_hash_table_destroy (htable);
170
171     _notmuch_string_list_sort (tags);
172     return _notmuch_tags_create (messages, tags);
173 }