]> git.notmuchmail.org Git - notmuch/blob - lib/messages.c
8b627750b5838a90abc31fe26b055b77df1f6c68
[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 typedef struct _message_list {
26     notmuch_message_t *message;
27     struct _message_list *next;
28 } message_list_t;
29
30 struct _notmuch_messages {
31     message_list_t *head;
32     message_list_t **tail;
33 };
34
35 /* Create a new notmuch_messages_t object, with 'ctx' as its talloc owner.
36  *
37  * This function can return NULL in case of out-of-memory.
38  */
39 notmuch_messages_t *
40 _notmuch_messages_create (void *ctx)
41 {
42     notmuch_messages_t *messages;
43
44     messages = talloc (ctx, notmuch_messages_t);
45     if (unlikely (messages == NULL))
46         return NULL;
47
48     messages->head = NULL;
49     messages->tail = &messages->head;
50
51     return messages;
52 }
53
54 /* Add a new message to 'messages'.
55  */
56 void
57 _notmuch_messages_add_message (notmuch_messages_t *messages,
58                                notmuch_message_t *message)
59 {
60     message_list_t *new = talloc (messages, message_list_t);
61
62     new->message = message;
63     new->next = NULL;
64
65     *(messages->tail) = new;
66     messages->tail = &new->next;
67 }
68
69 notmuch_bool_t
70 notmuch_messages_has_more (notmuch_messages_t *messages)
71 {
72     return messages->head != NULL;
73 }
74
75 notmuch_message_t *
76 notmuch_messages_get (notmuch_messages_t *messages)
77 {
78     if (messages->head == NULL)
79         return NULL;
80
81     return messages->head->message;
82 }
83
84 void
85 notmuch_messages_advance (notmuch_messages_t *messages)
86 {
87     if (messages->head == NULL)
88         return;
89
90     messages->head = messages->head->next;
91 }
92
93 void
94 notmuch_messages_destroy (notmuch_messages_t *messages)
95 {
96     talloc_free (messages);
97 }