1 /* thread.cc - Results of thread-based searches from a notmuch database
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"
22 #include "database-private.h"
26 #include <gmime/gmime.h>
27 #include <glib.h> /* GHashTable */
29 struct _notmuch_thread {
30 notmuch_database_t *notmuch;
36 notmuch_bool_t has_message;
42 _notmuch_thread_destructor (notmuch_thread_t *thread)
44 g_hash_table_unref (thread->tags);
49 /* Create a new notmuch_thread_t object for an existing document in
52 * Here, 'talloc owner' is an optional talloc context to which the new
53 * thread will belong. This allows for the caller to not bother
54 * calling notmuch_thread_destroy on the thread, and know that all
55 * memory will be reclaimed with 'talloc_owner' is freed. The caller
56 * still can call notmuch_thread_destroy when finished with the
59 * The 'talloc_owner' argument can also be NULL, in which case the
60 * caller *is* responsible for calling notmuch_thread_destroy.
62 * This function returns NULL in the case of any error.
65 _notmuch_thread_create (const void *talloc_owner,
66 notmuch_database_t *notmuch,
67 const char *thread_id)
69 notmuch_thread_t *thread;
71 thread = talloc (talloc_owner, notmuch_thread_t);
72 if (unlikely (thread == NULL))
75 talloc_set_destructor (thread, _notmuch_thread_destructor);
77 thread->notmuch = notmuch;
78 thread->thread_id = talloc_strdup (thread, thread_id);
79 thread->subject = NULL;
80 thread->authors = NULL;
81 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
84 thread->has_message = 0;
92 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
94 return thread->thread_id;
98 _notmuch_thread_add_message (notmuch_thread_t *thread,
99 notmuch_message_t *message)
101 notmuch_tags_t *tags;
104 InternetAddressList *list;
105 InternetAddress *address;
106 const char *from, *author;
108 from = notmuch_message_get_header (message, "from");
109 list = internet_address_list_parse_string (from);
111 address = internet_address_list_get_address (list, 0);
113 author = internet_address_get_name (address);
114 if (author == NULL) {
115 InternetAddressMailbox *mailbox;
116 mailbox = INTERNET_ADDRESS_MAILBOX (address);
117 author = internet_address_mailbox_get_addr (mailbox);
121 thread->authors = talloc_asprintf (thread, "%s, %s",
125 thread->authors = talloc_strdup (thread, author);
128 g_object_unref (G_OBJECT (list));
131 if (! thread->subject) {
133 subject = notmuch_message_get_header (message, "subject");
134 thread->subject = talloc_strdup (thread, subject);
137 for (tags = notmuch_message_get_tags (message);
138 notmuch_tags_has_more (tags);
139 notmuch_tags_advance (tags))
141 tag = notmuch_tags_get (tags);
142 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
145 date = notmuch_message_get_date (message);
147 if (date < thread->oldest || ! thread->has_message)
148 thread->oldest = date;
150 if (date > thread->newest || ! thread->has_message)
151 thread->newest = date;
153 thread->has_message = 1;
157 notmuch_thread_get_authors (notmuch_thread_t *thread)
159 return thread->authors;
163 notmuch_thread_get_subject (notmuch_thread_t *thread)
165 return thread->subject;
169 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
171 return thread->oldest;
175 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
177 return thread->newest;
181 notmuch_thread_get_tags (notmuch_thread_t *thread)
183 notmuch_tags_t *tags;
186 tags = _notmuch_tags_create (thread);
187 if (unlikely (tags == NULL))
190 keys = g_hash_table_get_keys (thread->tags);
192 for (l = keys; l; l = l->next)
193 _notmuch_tags_add_tag (tags, (char *) l->data);
197 _notmuch_tags_prepare_iterator (tags);
203 notmuch_thread_destroy (notmuch_thread_t *thread)
205 talloc_free (thread);