1 /* messages.c - Iterator for a set of messages
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
23 #include <glib.h> /* GList */
26 struct _notmuch_messages {
27 notmuch_message_node_t *iterator;
30 /* Create a new notmuch_message_list_t object, with 'ctx' as its
33 * This function can return NULL in case of out-of-memory.
35 notmuch_message_list_t *
36 _notmuch_message_list_create (const void *ctx)
38 notmuch_message_list_t *list;
40 list = talloc (ctx, notmuch_message_list_t);
41 if (unlikely (list == NULL))
45 list->tail = &list->head;
50 /* Append 'node' (which can of course point to an arbitrarily long
51 * list of nodes) to the end of 'list'.
54 _notmuch_message_list_append (notmuch_message_list_t *list,
55 notmuch_message_node_t *node)
58 list->tail = &node->next;
61 /* Allocate a new node for 'message' and append it to the end of
65 _notmuch_message_list_add_message (notmuch_message_list_t *list,
66 notmuch_message_t *message)
68 notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
70 node->message = message;
73 _notmuch_message_list_append (list, node);
77 _notmuch_messages_create (notmuch_message_list_t *list)
79 notmuch_messages_t *messages;
81 if (list->head == NULL)
84 messages = talloc (list, notmuch_messages_t);
85 if (unlikely (messages == NULL))
88 messages->iterator = list->head;
94 notmuch_messages_has_more (notmuch_messages_t *messages)
96 return (messages != NULL && messages->iterator != NULL);
100 notmuch_messages_get (notmuch_messages_t *messages)
102 if (messages->iterator == NULL)
105 return messages->iterator->message;
109 notmuch_messages_advance (notmuch_messages_t *messages)
111 if (messages->iterator == NULL)
114 messages->iterator = messages->iterator->next;
118 notmuch_messages_destroy (notmuch_messages_t *messages)
120 talloc_free (messages);