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