]> git.notmuchmail.org Git - notmuch/blob - lib/messages.c
database: Add "replyto" to the database schema documentation.
[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> /* GList */
24
25
26 struct _notmuch_messages {
27     notmuch_message_node_t *iterator;
28 };
29
30 /* Create a new notmuch_message_list_t object, with 'ctx' as its
31  * talloc owner.
32  *
33  * This function can return NULL in case of out-of-memory.
34  */
35 notmuch_message_list_t *
36 _notmuch_message_list_create (const void *ctx)
37 {
38     notmuch_message_list_t *list;
39
40     list = talloc (ctx, notmuch_message_list_t);
41     if (unlikely (list == NULL))
42         return NULL;
43
44     list->head = NULL;
45     list->tail = &list->head;
46
47     return list;
48 }
49
50 /* Append 'node' (which can of course point to an arbitrarily long
51  * list of nodes) to the end of 'list'.
52  */
53 void
54 _notmuch_message_list_append (notmuch_message_list_t *list,
55                               notmuch_message_node_t *node)
56 {
57     *(list->tail) = node;
58     list->tail = &node->next;
59 }
60
61 /* Allocate a new node for 'message' and append it to the end of
62  * 'list'.
63  */
64 void
65 _notmuch_message_list_add_message (notmuch_message_list_t *list,
66                                    notmuch_message_t *message)
67 {
68     notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
69
70     node->message = message;
71     node->next = NULL;
72
73     _notmuch_message_list_append (list, node);
74 }
75
76 notmuch_messages_t *
77 _notmuch_messages_create (notmuch_message_list_t *list)
78 {
79     notmuch_messages_t *messages;
80
81     if (list->head == NULL)
82         return NULL;
83
84     messages = talloc (list, notmuch_messages_t);
85     if (unlikely (messages == NULL))
86         return NULL;
87
88     messages->iterator = list->head;
89
90     return messages;
91 }
92
93 notmuch_bool_t
94 notmuch_messages_has_more (notmuch_messages_t *messages)
95 {
96     return (messages != NULL && messages->iterator != NULL);
97 }
98
99 notmuch_message_t *
100 notmuch_messages_get (notmuch_messages_t *messages)
101 {
102     if (messages->iterator == NULL)
103         return NULL;
104
105     return messages->iterator->message;
106 }
107
108 void
109 notmuch_messages_advance (notmuch_messages_t *messages)
110 {
111     if (messages->iterator == NULL)
112         return;
113
114     messages->iterator = messages->iterator->next;
115 }
116
117 void
118 notmuch_messages_destroy (notmuch_messages_t *messages)
119 {
120     talloc_free (messages);
121 }