]> git.notmuchmail.org Git - notmuch/blob - thread.cc
73d17722f574b3b052bd211e2e462f8a53637720
[notmuch] / thread.cc
1 /* thread.cc - Results of thread-based searches from a notmuch database
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 #include "database-private.h"
23
24 #include <xapian.h>
25
26 #include <glib.h> /* GHashTable */
27
28 struct _notmuch_thread {
29     notmuch_database_t *notmuch;
30     char *thread_id;
31     GHashTable *tags;
32 };
33
34 static int
35 _notmuch_thread_destructor (notmuch_thread_t *thread)
36 {
37     g_hash_table_unref (thread->tags);
38
39     return 0;
40 }
41
42 /* Create a new notmuch_thread_t object for an existing document in
43  * the database.
44  *
45  * Here, 'talloc owner' is an optional talloc context to which the new
46  * thread will belong. This allows for the caller to not bother
47  * calling notmuch_thread_destroy on the thread, and know that all
48  * memory will be reclaimed with 'talloc_owner' is freed. The caller
49  * still can call notmuch_thread_destroy when finished with the
50  * thread if desired.
51  *
52  * The 'talloc_owner' argument can also be NULL, in which case the
53  * caller *is* responsible for calling notmuch_thread_destroy.
54  *
55  * This function returns NULL in the case of any error.
56  */
57 notmuch_thread_t *
58 _notmuch_thread_create (const void *talloc_owner,
59                         notmuch_database_t *notmuch,
60                         const char *thread_id)
61 {
62     notmuch_thread_t *thread;
63
64     thread = talloc (talloc_owner, notmuch_thread_t);
65     if (unlikely (thread == NULL))
66         return NULL;
67
68     talloc_set_destructor (thread, _notmuch_thread_destructor);
69
70     thread->notmuch = notmuch;
71     thread->thread_id = talloc_strdup (thread, thread_id);
72     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
73                                           free, NULL);
74
75     return thread;
76 }
77
78 const char *
79 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
80 {
81     return thread->thread_id;
82 }
83
84 void
85 _notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag)
86 {
87     g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
88 }
89
90 notmuch_tags_t *
91 notmuch_thread_get_tags (notmuch_thread_t *thread)
92 {
93     notmuch_tags_t *tags;
94     GList *keys, *l;
95
96     tags = _notmuch_tags_create (thread);
97     if (unlikely (tags == NULL))
98         return NULL;
99
100     keys = g_hash_table_get_keys (thread->tags);
101
102     for (l = keys; l; l = l->next)
103         _notmuch_tags_add_tag (tags, (char *) l->data);
104
105     g_list_free (keys);
106
107     _notmuch_tags_prepare_iterator (tags);
108
109     return tags;
110 }
111
112 void
113 notmuch_thread_destroy (notmuch_thread_t *thread)
114 {
115     talloc_free (thread);
116 }