]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
4411d64d98da05964318b23c0d26cc9dba7e77af
[notmuch] / lib / thread.cc
1 /* thread.cc - Results of thread-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <xapian.h>
25
26 #include <gmime/gmime.h>
27 #include <glib.h> /* GHashTable */
28
29 struct _notmuch_thread {
30     notmuch_database_t *notmuch;
31     char *thread_id;
32     char *subject;
33     GHashTable *authors_hash;
34     char *authors;
35     GHashTable *tags;
36
37     notmuch_message_list_t *message_list;
38     GHashTable *message_hash;
39     int total_messages;
40     int matched_messages;
41     time_t oldest;
42     time_t newest;
43 };
44
45 static int
46 _notmuch_thread_destructor (notmuch_thread_t *thread)
47 {
48     g_hash_table_unref (thread->authors_hash);
49     g_hash_table_unref (thread->tags);
50     g_hash_table_unref (thread->message_hash);
51
52     return 0;
53 }
54
55 static void
56 _thread_add_author (notmuch_thread_t *thread,
57                     const char *author)
58 {
59     if (author == NULL)
60         return;
61
62     if (g_hash_table_lookup_extended (thread->authors_hash,
63                                       author, NULL, NULL))
64         return;
65
66     g_hash_table_insert (thread->authors_hash, xstrdup (author), NULL);
67
68     if (thread->authors)
69         thread->authors = talloc_asprintf (thread, "%s, %s",
70                                            thread->authors,
71                                            author);
72     else
73         thread->authors = talloc_strdup (thread, author);
74 }
75
76 /* Add 'message' as a message that belongs to 'thread'.
77  *
78  * The 'thread' will talloc_steal the 'message' and hold onto a
79  * reference to it.
80  */
81 static void
82 _thread_add_message (notmuch_thread_t *thread,
83                      notmuch_message_t *message)
84 {
85     notmuch_tags_t *tags;
86     const char *tag;
87     InternetAddressList *list;
88     InternetAddress *address;
89     const char *from, *author;
90
91     _notmuch_message_list_add_message (thread->message_list,
92                                        talloc_steal (thread, message));
93     thread->total_messages++;
94
95     g_hash_table_insert (thread->message_hash,
96                          xstrdup (notmuch_message_get_message_id (message)),
97                          message);
98
99     from = notmuch_message_get_header (message, "from");
100     list = internet_address_list_parse_string (from);
101     if (list) {
102         address = internet_address_list_get_address (list, 0);
103         if (address) {
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);
109             }
110             _thread_add_author (thread, author);
111         }
112         g_object_unref (G_OBJECT (list));
113     }
114
115     if (! thread->subject) {
116         const char *subject;
117         subject = notmuch_message_get_header (message, "subject");
118         thread->subject = talloc_strdup (thread, subject);
119     }
120
121     for (tags = notmuch_message_get_tags (message);
122          notmuch_tags_has_more (tags);
123          notmuch_tags_advance (tags))
124     {
125         tag = notmuch_tags_get (tags);
126         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
127     }
128 }
129
130 static void
131 _thread_add_matched_message (notmuch_thread_t *thread,
132                              notmuch_message_t *message)
133 {
134     time_t date;
135
136     date = notmuch_message_get_date (message);
137
138     if (date < thread->oldest || ! thread->matched_messages)
139         thread->oldest = date;
140
141     if (date > thread->newest || ! thread->matched_messages)
142         thread->newest = date;
143
144     thread->matched_messages++;
145 }
146
147 static void
148 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
149 {
150     notmuch_message_node_t **prev, *node;
151     notmuch_message_t *message, *parent;
152     const char *in_reply_to;
153
154     prev = &thread->message_list->head;
155     while ((node = *prev)) {
156         message = node->message;
157         in_reply_to = _notmuch_message_get_in_reply_to (message);
158         if (in_reply_to &&
159             g_hash_table_lookup_extended (thread->message_hash,
160                                           in_reply_to, NULL,
161                                           (void **) &parent))
162         {
163             *prev = node->next;
164             if (thread->message_list->tail == &node->next)
165                 thread->message_list->tail = prev;
166             node->next = NULL;
167             _notmuch_message_add_reply (parent, node);
168         } else {
169             prev = &((*prev)->next);
170         }
171     }
172
173     /* XXX: After scanning through the entire list looking for parents
174      * via "In-Reply-To", we should do a second pass that looks at the
175      * list of messages IDs in the "References" header instead. (And
176      * for this the parent would be the "deepest" message of all the
177      * messages found in the "References" list.)
178      *
179      * Doing this will allow messages and sub-threads to be positioned
180      * correctly in the thread even when an intermediate message is
181      * missing from the thread.
182      */
183 }
184
185 /* Create a new notmuch_thread_t object for the given thread ID,
186  * treating any messages matching 'query_string' as "matched".
187  *
188  * Creating the thread will trigger two database searches. The first
189  * is for all messages belonging to the thread, (to get the first
190  * subject line, the total count of messages, and all authors). The
191  * second search is for all messages that are in the thread and that
192  * also match the given query_string. This is to allow for a separate
193  * count of matched messages, and to allow a viewer to diplay these
194  * messages differently.
195  *
196  * Here, 'ctx' is talloc context for the resulting thread object.
197  *
198  * This function returns NULL in the case of any error.
199  */
200 notmuch_thread_t *
201 _notmuch_thread_create (void *ctx,
202                         notmuch_database_t *notmuch,
203                         const char *thread_id,
204                         const char *query_string)
205 {
206     notmuch_thread_t *thread;
207     const char *thread_id_query_string, *matched_query_string;
208     notmuch_query_t *thread_id_query, *matched_query;
209     notmuch_messages_t *messages;
210
211     thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
212     if (unlikely (query_string == NULL))
213         return NULL;
214
215     /* XXX: We could be a bit more efficient here if
216      * thread_id_query_string is identical to query_string, (then we
217      * could get by with just one database search instead of two). */
218
219     matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
220                                             thread_id_query_string,
221                                             query_string);
222     if (unlikely (matched_query_string == NULL))
223         return NULL;
224
225     thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
226     if (unlikely (thread_id_query == NULL))
227         return NULL;
228
229     matched_query = notmuch_query_create (notmuch, matched_query_string);
230     if (unlikely (thread_id_query == NULL))
231         return NULL;
232
233     thread = talloc (ctx, notmuch_thread_t);
234     if (unlikely (thread == NULL))
235         return NULL;
236
237     talloc_set_destructor (thread, _notmuch_thread_destructor);
238
239     thread->notmuch = notmuch;
240     thread->thread_id = talloc_strdup (thread, thread_id);
241     thread->subject = NULL;
242     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
243                                                   free, NULL);
244     thread->authors = NULL;
245     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
246                                           free, NULL);
247
248     thread->message_list = _notmuch_message_list_create (thread);
249     if (unlikely (thread->message_list == NULL))
250         return NULL;
251
252     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
253                                                   free, NULL);
254
255     thread->total_messages = 0;
256     thread->matched_messages = 0;
257     thread->oldest = 0;
258     thread->newest = 0;
259
260     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_DATE);
261
262     for (messages = notmuch_query_search_messages (thread_id_query, 0, -1);
263          notmuch_messages_has_more (messages);
264          notmuch_messages_advance (messages))
265     {
266         _thread_add_message (thread, notmuch_messages_get (messages));
267     }
268
269     notmuch_query_destroy (thread_id_query);
270
271     for (messages = notmuch_query_search_messages (matched_query, 0, -1);
272          notmuch_messages_has_more (messages);
273          notmuch_messages_advance (messages))
274     {
275         _thread_add_matched_message (thread, notmuch_messages_get (messages));
276     }
277
278     notmuch_query_destroy (matched_query);
279
280     _resolve_thread_relationships (thread);
281
282     return thread;
283 }
284
285 notmuch_messages_t *
286 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
287 {
288     return _notmuch_messages_create (thread->message_list);
289 }
290
291 const char *
292 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
293 {
294     return thread->thread_id;
295 }
296
297 int
298 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
299 {
300     return thread->total_messages;
301 }
302
303 int
304 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
305 {
306     return thread->matched_messages;
307 }
308
309 const char *
310 notmuch_thread_get_authors (notmuch_thread_t *thread)
311 {
312     return thread->authors;
313 }
314
315 const char *
316 notmuch_thread_get_subject (notmuch_thread_t *thread)
317 {
318     return thread->subject;
319 }
320
321 time_t
322 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
323 {
324     return thread->oldest;
325 }
326
327 time_t
328 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
329 {
330     return thread->newest;
331 }
332
333 notmuch_tags_t *
334 notmuch_thread_get_tags (notmuch_thread_t *thread)
335 {
336     notmuch_tags_t *tags;
337     GList *keys, *l;
338
339     tags = _notmuch_tags_create (thread);
340     if (unlikely (tags == NULL))
341         return NULL;
342
343     keys = g_hash_table_get_keys (thread->tags);
344
345     for (l = keys; l; l = l->next)
346         _notmuch_tags_add_tag (tags, (char *) l->data);
347
348     g_list_free (keys);
349
350     _notmuch_tags_prepare_iterator (tags);
351
352     return tags;
353 }
354
355 void
356 notmuch_thread_destroy (notmuch_thread_t *thread)
357 {
358     talloc_free (thread);
359 }