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;
37 notmuch_message_list_t *message_list;
38 GHashTable *message_hash;
46 _notmuch_thread_destructor (notmuch_thread_t *thread)
48 g_hash_table_unref (thread->authors_hash);
49 g_hash_table_unref (thread->tags);
50 g_hash_table_unref (thread->message_hash);
56 _thread_add_author (notmuch_thread_t *thread,
62 if (g_hash_table_lookup_extended (thread->authors_hash,
66 g_hash_table_insert (thread->authors_hash, xstrdup (author), NULL);
69 thread->authors = talloc_asprintf (thread, "%s, %s",
73 thread->authors = talloc_strdup (thread, author);
76 /* Add 'message' as a message that belongs to 'thread'.
78 * The 'thread' will talloc_steal the 'message' and hold onto a
82 _thread_add_message (notmuch_thread_t *thread,
83 notmuch_message_t *message)
87 InternetAddressList *list;
88 InternetAddress *address;
89 const char *from, *author;
91 _notmuch_message_list_add_message (thread->message_list,
92 talloc_steal (thread, message));
93 thread->total_messages++;
95 g_hash_table_insert (thread->message_hash,
96 xstrdup (notmuch_message_get_message_id (message)),
99 from = notmuch_message_get_header (message, "from");
100 list = internet_address_list_parse_string (from);
102 address = internet_address_list_get_address (list, 0);
104 author = internet_address_get_name (address);
105 if (author == NULL) {
106 InternetAddressMailbox *mailbox;
107 mailbox = INTERNET_ADDRESS_MAILBOX (address);
108 author = internet_address_mailbox_get_addr (mailbox);
110 _thread_add_author (thread, author);
112 g_object_unref (G_OBJECT (list));
115 if (! thread->subject) {
117 subject = notmuch_message_get_header (message, "subject");
118 thread->subject = talloc_strdup (thread, subject);
121 for (tags = notmuch_message_get_tags (message);
122 notmuch_tags_valid (tags);
123 notmuch_tags_move_to_next (tags))
125 tag = notmuch_tags_get (tags);
126 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
131 _thread_set_subject_from_message (notmuch_thread_t *thread,
132 notmuch_message_t *message)
135 const char *cleaned_subject;
137 subject = notmuch_message_get_header (message, "subject");
139 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
140 (strncasecmp (subject, "Aw: ", 4) == 0) ||
141 (strncasecmp (subject, "Vs: ", 4) == 0) ||
142 (strncasecmp (subject, "Sv: ", 4) == 0)) {
144 cleaned_subject = talloc_strndup (thread,
146 strlen(subject) - 4);
148 cleaned_subject = talloc_strdup (thread, subject);
152 talloc_free (thread->subject);
154 thread->subject = talloc_strdup (thread, cleaned_subject);
158 _thread_add_matched_message (notmuch_thread_t *thread,
159 notmuch_message_t *message,
163 notmuch_message_t *hashed_message;
165 date = notmuch_message_get_date (message);
167 if (date < thread->oldest || ! thread->matched_messages)
168 thread->oldest = date;
170 if (date > thread->newest || ! thread->matched_messages)
171 thread->newest = date;
173 thread->matched_messages++;
175 if (g_hash_table_lookup_extended (thread->message_hash,
176 notmuch_message_get_message_id (message), NULL,
177 (void **) &hashed_message)) {
178 notmuch_message_set_flag (hashed_message,
179 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
182 if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
183 (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
185 _thread_set_subject_from_message (thread, message);
190 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
192 notmuch_message_node_t **prev, *node;
193 notmuch_message_t *message, *parent;
194 const char *in_reply_to;
196 prev = &thread->message_list->head;
197 while ((node = *prev)) {
198 message = node->message;
199 in_reply_to = _notmuch_message_get_in_reply_to (message);
200 if (in_reply_to && strlen (in_reply_to) &&
201 g_hash_table_lookup_extended (thread->message_hash,
206 if (thread->message_list->tail == &node->next)
207 thread->message_list->tail = prev;
209 _notmuch_message_add_reply (parent, node);
211 prev = &((*prev)->next);
215 /* XXX: After scanning through the entire list looking for parents
216 * via "In-Reply-To", we should do a second pass that looks at the
217 * list of messages IDs in the "References" header instead. (And
218 * for this the parent would be the "deepest" message of all the
219 * messages found in the "References" list.)
221 * Doing this will allow messages and sub-threads to be positioned
222 * correctly in the thread even when an intermediate message is
223 * missing from the thread.
227 /* Create a new notmuch_thread_t object for the given thread ID,
228 * treating any messages matching 'query_string' as "matched".
230 * Creating the thread will trigger two database searches. The first
231 * is for all messages belonging to the thread, (to get the first
232 * subject line, the total count of messages, and all authors). The
233 * second search is for all messages that are in the thread and that
234 * also match the given query_string. This is to allow for a separate
235 * count of matched messages, and to allow a viewer to display these
236 * messages differently.
238 * Here, 'ctx' is talloc context for the resulting thread object.
240 * This function returns NULL in the case of any error.
243 _notmuch_thread_create (void *ctx,
244 notmuch_database_t *notmuch,
245 const char *thread_id,
246 const char *query_string,
249 notmuch_thread_t *thread;
250 const char *thread_id_query_string;
251 notmuch_query_t *thread_id_query;
253 notmuch_messages_t *messages;
254 notmuch_message_t *message;
255 notmuch_bool_t matched_is_subset_of_thread;
257 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
258 if (unlikely (query_string == NULL))
261 /* Under normal circumstances we need to do two database
262 * queries. One is for the thread itself (thread_id_query_string)
263 * and the second is to determine which messages in that thread
264 * match the original query (matched_query_string).
266 * But under two circumstances, we use only the
267 * thread_id_query_string:
269 * 1. If the original query_string *is* just the thread
272 * 2. If the original query_string matches all messages ("" or
275 * In either of these cases, we can be more efficient by running
276 * just the thread_id query (since we know all messages in the
277 * thread will match the query_string).
279 * Beyond the performance advantage, in the second case, it's
280 * important to not try to create a concatenated query because our
281 * parser handles "" and "*" as special cases and will not do the
282 * right thing with a query string of "* and thread:<foo>".
284 matched_is_subset_of_thread = 1;
285 if (strcmp (query_string, thread_id_query_string) == 0 ||
286 strcmp (query_string, "") == 0 ||
287 strcmp (query_string, "*") == 0)
289 matched_is_subset_of_thread = 0;
292 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
293 if (unlikely (thread_id_query == NULL))
296 thread = talloc (ctx, notmuch_thread_t);
297 if (unlikely (thread == NULL))
300 talloc_set_destructor (thread, _notmuch_thread_destructor);
302 thread->notmuch = notmuch;
303 thread->thread_id = talloc_strdup (thread, thread_id);
304 thread->subject = NULL;
305 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
307 thread->authors = NULL;
308 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
311 thread->message_list = _notmuch_message_list_create (thread);
312 if (unlikely (thread->message_list == NULL))
315 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
318 thread->total_messages = 0;
319 thread->matched_messages = 0;
323 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
325 for (messages = notmuch_query_search_messages (thread_id_query);
326 notmuch_messages_valid (messages);
327 notmuch_messages_move_to_next (messages))
329 message = notmuch_messages_get (messages);
331 _thread_add_message (thread, message);
333 if (! matched_is_subset_of_thread)
334 _thread_add_matched_message (thread, message, sort);
336 _notmuch_message_close (message);
339 notmuch_query_destroy (thread_id_query);
341 if (matched_is_subset_of_thread)
343 const char *matched_query_string;
344 notmuch_query_t *matched_query;
346 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
347 thread_id_query_string,
349 if (unlikely (matched_query_string == NULL))
352 matched_query = notmuch_query_create (notmuch, matched_query_string);
353 if (unlikely (matched_query == NULL))
356 for (messages = notmuch_query_search_messages (matched_query);
357 notmuch_messages_valid (messages);
358 notmuch_messages_move_to_next (messages))
360 message = notmuch_messages_get (messages);
361 _thread_add_matched_message (thread, message, sort);
362 _notmuch_message_close (message);
365 notmuch_query_destroy (matched_query);
368 _resolve_thread_relationships (thread);
374 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
376 return _notmuch_messages_create (thread->message_list);
380 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
382 return thread->thread_id;
386 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
388 return thread->total_messages;
392 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
394 return thread->matched_messages;
398 notmuch_thread_get_authors (notmuch_thread_t *thread)
400 return thread->authors;
404 notmuch_thread_get_subject (notmuch_thread_t *thread)
406 return thread->subject;
410 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
412 return thread->oldest;
416 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
418 return thread->newest;
422 notmuch_thread_get_tags (notmuch_thread_t *thread)
424 notmuch_tags_t *tags;
427 tags = _notmuch_tags_create (thread);
428 if (unlikely (tags == NULL))
431 keys = g_hash_table_get_keys (thread->tags);
433 for (l = keys; l; l = l->next)
434 _notmuch_tags_add_tag (tags, (char *) l->data);
438 _notmuch_tags_prepare_iterator (tags);
444 notmuch_thread_destroy (notmuch_thread_t *thread)
446 talloc_free (thread);