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)
79 InternetAddressList *list;
80 InternetAddress *address;
81 const char *from, *author;
83 from = notmuch_message_get_header (message, "from");
84 list = internet_address_list_parse_string (from);
86 address = internet_address_list_get_address (list, 0);
88 author = internet_address_get_name (address);
90 InternetAddressMailbox *mailbox;
91 mailbox = INTERNET_ADDRESS_MAILBOX (address);
92 author = internet_address_mailbox_get_addr (mailbox);
94 _thread_add_author (thread, author);
96 g_object_unref (G_OBJECT (list));
99 if (! thread->subject) {
101 subject = notmuch_message_get_header (message, "subject");
102 thread->subject = talloc_strdup (thread, subject);
105 for (tags = notmuch_message_get_tags (message);
106 notmuch_tags_has_more (tags);
107 notmuch_tags_advance (tags))
109 tag = notmuch_tags_get (tags);
110 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
113 thread->total_messages++;
117 _thread_add_matched_message (notmuch_thread_t *thread,
118 notmuch_message_t *message)
122 date = notmuch_message_get_date (message);
124 if (date < thread->oldest || ! thread->matched_messages)
125 thread->oldest = date;
127 if (date > thread->newest || ! thread->matched_messages)
128 thread->newest = date;
130 thread->matched_messages++;
133 /* Create a new notmuch_thread_t object for the given thread ID,
134 * treating any messages matching 'query_string' as "matched".
136 * Creating the thread will trigger two database searches. The first
137 * is for all messages belonging to the thread, (to get the first
138 * subject line, the total count of messages, and all authors). The
139 * second search is for all messages that are in the thread and that
140 * also match the given query_string. This is to allow for a separate
141 * count of matched messages, and to allow a viewer to diplay these
142 * messages differently.
144 * Here, 'ctx' is talloc context for the resulting thread object.
146 * This function returns NULL in the case of any error.
149 _notmuch_thread_create (void *ctx,
150 notmuch_database_t *notmuch,
151 const char *thread_id,
152 const char *query_string)
154 notmuch_thread_t *thread;
155 const char *thread_id_query_string, *matched_query_string;
156 notmuch_query_t *thread_id_query, *matched_query;
157 notmuch_messages_t *messages;
159 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
160 if (unlikely (query_string == NULL))
163 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
164 thread_id_query_string,
166 if (unlikely (matched_query_string == NULL))
169 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
170 if (unlikely (thread_id_query == NULL))
173 matched_query = notmuch_query_create (notmuch, matched_query_string);
174 if (unlikely (thread_id_query == NULL))
177 thread = talloc (ctx, notmuch_thread_t);
178 if (unlikely (thread == NULL))
181 talloc_set_destructor (thread, _notmuch_thread_destructor);
183 thread->notmuch = notmuch;
184 thread->thread_id = talloc_strdup (thread, thread_id);
185 thread->subject = NULL;
186 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
188 thread->authors = NULL;
189 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
192 thread->total_messages = 0;
193 thread->matched_messages = 0;
197 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_DATE);
199 for (messages = notmuch_query_search_messages (thread_id_query, 0, -1);
200 notmuch_messages_has_more (messages);
201 notmuch_messages_advance (messages))
203 _thread_add_message (thread, notmuch_messages_get (messages));
206 notmuch_query_destroy (thread_id_query);
208 for (messages = notmuch_query_search_messages (matched_query, 0, -1);
209 notmuch_messages_has_more (messages);
210 notmuch_messages_advance (messages))
212 _thread_add_matched_message (thread, notmuch_messages_get (messages));
215 notmuch_query_destroy (matched_query);
221 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
223 return thread->thread_id;
227 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
229 return thread->total_messages;
233 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
235 return thread->matched_messages;
239 notmuch_thread_get_authors (notmuch_thread_t *thread)
241 return thread->authors;
245 notmuch_thread_get_subject (notmuch_thread_t *thread)
247 return thread->subject;
251 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
253 return thread->oldest;
257 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
259 return thread->newest;
263 notmuch_thread_get_tags (notmuch_thread_t *thread)
265 notmuch_tags_t *tags;
268 tags = _notmuch_tags_create (thread);
269 if (unlikely (tags == NULL))
272 keys = g_hash_table_get_keys (thread->tags);
274 for (l = keys; l; l = l->next)
275 _notmuch_tags_add_tag (tags, (char *) l->data);
279 _notmuch_tags_prepare_iterator (tags);
285 notmuch_thread_destroy (notmuch_thread_t *thread)
287 talloc_free (thread);