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 thread->total_messages++;
118 _thread_add_matched_message (notmuch_thread_t *thread,
119 notmuch_message_t *message)
123 date = notmuch_message_get_date (message);
125 if (date < thread->oldest || ! thread->matched_messages)
126 thread->oldest = date;
128 if (date > thread->newest || ! thread->matched_messages)
129 thread->newest = date;
131 thread->matched_messages++;
134 /* Create a new notmuch_thread_t object for the given thread ID,
135 * treating any messages matching 'query_string' as "matched".
137 * Creating the thread will trigger two database searches. The first
138 * is for all messages belonging to the thread, (to get the first
139 * subject line, the total count of messages, and all authors). The
140 * second search is for all messages that are in the thread and that
141 * also match the given query_string. This is to allow for a separate
142 * count of matched messages, and to allow a viewer to diplay these
143 * messages differently.
145 * Here, 'ctx' is talloc context for the resulting thread object.
147 * This function returns NULL in the case of any error.
150 _notmuch_thread_create (void *ctx,
151 notmuch_database_t *notmuch,
152 const char *thread_id,
153 const char *query_string)
155 notmuch_thread_t *thread;
156 const char *thread_id_query_string, *matched_query_string;
157 notmuch_query_t *thread_id_query, *matched_query;
158 notmuch_messages_t *messages;
160 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
161 if (unlikely (query_string == NULL))
164 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
165 thread_id_query_string,
167 if (unlikely (matched_query_string == NULL))
170 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
171 if (unlikely (thread_id_query == NULL))
174 matched_query = notmuch_query_create (notmuch, matched_query_string);
175 if (unlikely (thread_id_query == NULL))
178 thread = talloc (ctx, notmuch_thread_t);
179 if (unlikely (thread == NULL))
182 talloc_set_destructor (thread, _notmuch_thread_destructor);
184 thread->notmuch = notmuch;
185 thread->thread_id = talloc_strdup (thread, thread_id);
186 thread->subject = NULL;
187 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
189 thread->authors = NULL;
190 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
193 thread->total_messages = 0;
194 thread->matched_messages = 0;
198 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_DATE);
200 for (messages = notmuch_query_search_messages (thread_id_query, 0, -1);
201 notmuch_messages_has_more (messages);
202 notmuch_messages_advance (messages))
204 _thread_add_message (thread, notmuch_messages_get (messages));
207 notmuch_query_destroy (thread_id_query);
209 for (messages = notmuch_query_search_messages (matched_query, 0, -1);
210 notmuch_messages_has_more (messages);
211 notmuch_messages_advance (messages))
213 _thread_add_matched_message (thread, notmuch_messages_get (messages));
216 notmuch_query_destroy (matched_query);
222 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
224 return thread->thread_id;
228 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
230 return thread->total_messages;
234 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
236 return thread->matched_messages;
240 notmuch_thread_get_authors (notmuch_thread_t *thread)
242 return thread->authors;
246 notmuch_thread_get_subject (notmuch_thread_t *thread)
248 return thread->subject;
252 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
254 return thread->oldest;
258 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
260 return thread->newest;
264 notmuch_thread_get_tags (notmuch_thread_t *thread)
266 notmuch_tags_t *tags;
269 tags = _notmuch_tags_create (thread);
270 if (unlikely (tags == NULL))
273 keys = g_hash_table_get_keys (thread->tags);
275 for (l = keys; l; l = l->next)
276 _notmuch_tags_add_tag (tags, (char *) l->data);
280 _notmuch_tags_prepare_iterator (tags);
286 notmuch_thread_destroy (notmuch_thread_t *thread)
288 talloc_free (thread);