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;
33 GHashTable *authors_hash;
44 _notmuch_thread_destructor (notmuch_thread_t *thread)
46 g_hash_table_unref (thread->authors_hash);
47 g_hash_table_unref (thread->tags);
53 _thread_add_author (notmuch_thread_t *thread,
59 if (g_hash_table_lookup_extended (thread->authors_hash,
63 g_hash_table_insert (thread->authors_hash, xstrdup (author), NULL);
66 thread->authors = talloc_asprintf (thread, "%s, %s",
70 thread->authors = talloc_strdup (thread, author);
74 _thread_add_message (notmuch_thread_t *thread,
75 notmuch_message_t *message)
80 InternetAddressList *list;
81 InternetAddress *address;
82 const char *from, *author;
84 from = notmuch_message_get_header (message, "from");
85 list = internet_address_list_parse_string (from);
87 address = internet_address_list_get_address (list, 0);
89 author = internet_address_get_name (address);
91 InternetAddressMailbox *mailbox;
92 mailbox = INTERNET_ADDRESS_MAILBOX (address);
93 author = internet_address_mailbox_get_addr (mailbox);
95 _thread_add_author (thread, author);
97 g_object_unref (G_OBJECT (list));
100 if (! thread->subject) {
102 subject = notmuch_message_get_header (message, "subject");
103 thread->subject = talloc_strdup (thread, subject);
106 for (tags = notmuch_message_get_tags (message);
107 notmuch_tags_has_more (tags);
108 notmuch_tags_advance (tags))
110 tag = notmuch_tags_get (tags);
111 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
114 date = notmuch_message_get_date (message);
116 if (date < thread->oldest || ! thread->total_messages)
117 thread->oldest = date;
119 if (date > thread->newest || ! thread->total_messages)
120 thread->newest = date;
122 thread->total_messages++;
125 /* Create a new notmuch_thread_t object for the given thread ID,
126 * treating any messages matching 'query_string' as "matched".
128 * Creating the thread will trigger two database searches. The first
129 * is for all messages belonging to the thread, (to get the first
130 * subject line, the total count of messages, and all authors). The
131 * second search is for all messages that are in the thread and that
132 * also match the given query_string. This is to allow for a separate
133 * count of matched messages, and to allow a viewer to diplay these
134 * messages differently.
136 * Here, 'ctx' is talloc context for the resulting thread object.
138 * This function returns NULL in the case of any error.
141 _notmuch_thread_create (void *ctx,
142 notmuch_database_t *notmuch,
143 const char *thread_id,
144 const char *query_string)
146 notmuch_thread_t *thread;
147 const char *thread_id_query_string, *matched_query_string;
148 notmuch_query_t *thread_id_query, *matched_query;
149 notmuch_messages_t *messages;
151 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
152 if (unlikely (query_string == NULL))
155 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
156 thread_id_query_string,
158 if (unlikely (matched_query_string == NULL))
161 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
162 if (unlikely (thread_id_query == NULL))
165 matched_query = notmuch_query_create (notmuch, matched_query_string);
166 if (unlikely (thread_id_query == NULL))
169 thread = talloc (ctx, notmuch_thread_t);
170 if (unlikely (thread == NULL))
173 talloc_set_destructor (thread, _notmuch_thread_destructor);
175 thread->notmuch = notmuch;
176 thread->thread_id = talloc_strdup (thread, thread_id);
177 thread->subject = NULL;
178 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
180 thread->authors = NULL;
181 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
184 thread->total_messages = 0;
185 thread->matched_messages = 0;
189 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_DATE_OLDEST_FIRST);
191 for (messages = notmuch_query_search_messages (thread_id_query, 0, -1);
192 notmuch_messages_has_more (messages);
193 notmuch_messages_advance (messages))
195 _thread_add_message (thread, notmuch_messages_get (messages));
198 notmuch_query_destroy (thread_id_query);
200 for (messages = notmuch_query_search_messages (matched_query, 0, -1);
201 notmuch_messages_has_more (messages);
202 notmuch_messages_advance (messages))
204 thread->matched_messages++;
207 notmuch_query_destroy (matched_query);
213 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
215 return thread->thread_id;
219 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
221 return thread->total_messages;
225 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
227 return thread->matched_messages;
231 notmuch_thread_get_authors (notmuch_thread_t *thread)
233 return thread->authors;
237 notmuch_thread_get_subject (notmuch_thread_t *thread)
239 return thread->subject;
243 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
245 return thread->oldest;
249 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
251 return thread->newest;
255 notmuch_thread_get_tags (notmuch_thread_t *thread)
257 notmuch_tags_t *tags;
260 tags = _notmuch_tags_create (thread);
261 if (unlikely (tags == NULL))
264 keys = g_hash_table_get_keys (thread->tags);
266 for (l = keys; l; l = l->next)
267 _notmuch_tags_add_tag (tags, (char *) l->data);
271 _notmuch_tags_prepare_iterator (tags);
277 notmuch_thread_destroy (notmuch_thread_t *thread)
279 talloc_free (thread);