1 /* tags.c - Iterator for tags returned from message or thread
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 */
25 struct _notmuch_tags {
31 /* XXX: Should write some talloc-friendly list to avoid the need for
34 _notmuch_tags_destructor (notmuch_tags_t *tags)
36 g_list_free (tags->tags);
41 /* Create a new notmuch_tags_t object, with 'ctx' as its talloc owner.
43 * This function can return NULL in case of out-of-memory.
46 _notmuch_tags_create (void *ctx)
50 tags = talloc (ctx, notmuch_tags_t);
51 if (unlikely (tags == NULL))
54 talloc_set_destructor (tags, _notmuch_tags_destructor);
58 tags->iterator = NULL;
63 /* Add a new tag to 'tags'. The tags object will create its own copy
66 * Note: The tags object will not do anything to prevent duplicate
67 * tags being stored, so the caller really shouldn't pass
70 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag)
72 tags->tags = g_list_prepend (tags->tags, talloc_strdup (tags, tag));
76 /* Prepare 'tag' for iteration.
78 * The internal creator of 'tags' should call this function before
79 * returning 'tags' to the user to call the public functions such as
80 * notmuch_tags_has_more, notmuch_tags_get, and notmuch_tags_advance. */
82 _notmuch_tags_prepare_iterator (notmuch_tags_t *tags)
85 tags->tags = g_list_sort (tags->tags, (GCompareFunc) strcmp);
88 tags->iterator = tags->tags;
92 notmuch_tags_has_more (notmuch_tags_t *tags)
94 return tags->iterator != NULL;
98 notmuch_tags_get (notmuch_tags_t *tags)
101 return (char *) tags->iterator->data;
107 notmuch_tags_advance (notmuch_tags_t *tags)
109 tags->iterator = tags->iterator->next;
113 notmuch_tags_destroy (notmuch_tags_t *tags)