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);
50 _thread_add_message (notmuch_thread_t *thread,
51 notmuch_message_t *message)
56 InternetAddressList *list;
57 InternetAddress *address;
58 const char *from, *author;
60 from = notmuch_message_get_header (message, "from");
61 list = internet_address_list_parse_string (from);
63 address = internet_address_list_get_address (list, 0);
65 author = internet_address_get_name (address);
67 InternetAddressMailbox *mailbox;
68 mailbox = INTERNET_ADDRESS_MAILBOX (address);
69 author = internet_address_mailbox_get_addr (mailbox);
73 thread->authors = talloc_asprintf (thread, "%s, %s",
77 thread->authors = talloc_strdup (thread, author);
80 g_object_unref (G_OBJECT (list));
83 if (! thread->subject) {
85 subject = notmuch_message_get_header (message, "subject");
86 thread->subject = talloc_strdup (thread, subject);
89 for (tags = notmuch_message_get_tags (message);
90 notmuch_tags_has_more (tags);
91 notmuch_tags_advance (tags))
93 tag = notmuch_tags_get (tags);
94 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
97 date = notmuch_message_get_date (message);
99 if (date < thread->oldest || ! thread->has_message)
100 thread->oldest = date;
102 if (date > thread->newest || ! thread->has_message)
103 thread->newest = date;
105 thread->has_message = 1;
108 /* Create a new notmuch_thread_t object for the given thread ID.
110 * Creating the thread will trigger a database search for the messages
111 * belonging to the thread so that the thread object can return some
112 * details about them, (authors, subject, etc.).
114 * Here, 'talloc owner' is an optional talloc context to which the new
115 * thread will belong. This allows for the caller to not bother
116 * calling notmuch_thread_destroy on the thread, and know that all
117 * memory will be reclaimed with 'talloc_owner' is freed. The caller
118 * still can call notmuch_thread_destroy when finished with the
121 * The 'talloc_owner' argument can also be NULL, in which case the
122 * caller *is* responsible for calling notmuch_thread_destroy.
124 * This function returns NULL in the case of any error.
127 _notmuch_thread_create (const void *ctx,
128 notmuch_database_t *notmuch,
129 const char *thread_id)
131 notmuch_thread_t *thread;
132 const char *query_string;
133 notmuch_query_t *query;
134 notmuch_messages_t *messages;
136 query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
137 if (unlikely (query_string == NULL))
140 query = notmuch_query_create (notmuch, query_string);
141 if (unlikely (query == NULL))
144 thread = talloc (ctx, notmuch_thread_t);
145 if (unlikely (thread == NULL))
148 talloc_set_destructor (thread, _notmuch_thread_destructor);
150 thread->notmuch = notmuch;
151 thread->thread_id = talloc_strdup (thread, thread_id);
152 thread->subject = NULL;
153 thread->authors = NULL;
154 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
157 thread->has_message = 0;
161 notmuch_query_set_sort (query, NOTMUCH_SORT_DATE_OLDEST_FIRST);
163 for (messages = notmuch_query_search_messages (query, 0, -1);
164 notmuch_messages_has_more (messages);
165 notmuch_messages_advance (messages))
167 _thread_add_message (thread, notmuch_messages_get (messages));
170 notmuch_query_destroy (query);
176 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
178 return thread->thread_id;
182 notmuch_thread_get_authors (notmuch_thread_t *thread)
184 return thread->authors;
188 notmuch_thread_get_subject (notmuch_thread_t *thread)
190 return thread->subject;
194 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
196 return thread->oldest;
200 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
202 return thread->newest;
206 notmuch_thread_get_tags (notmuch_thread_t *thread)
208 notmuch_tags_t *tags;
211 tags = _notmuch_tags_create (thread);
212 if (unlikely (tags == NULL))
215 keys = g_hash_table_get_keys (thread->tags);
217 for (l = keys; l; l = l->next)
218 _notmuch_tags_add_tag (tags, (char *) l->data);
222 _notmuch_tags_prepare_iterator (tags);
228 notmuch_thread_destroy (notmuch_thread_t *thread)
230 talloc_free (thread);