]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
Revert "thread: Simplify code for assigning the subject."
[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_valid (tags);
123          notmuch_tags_move_to_next (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                              notmuch_sort_t sort)
134 {
135     time_t date;
136     notmuch_message_t *hashed_message;
137
138     date = notmuch_message_get_date (message);
139
140     if (date < thread->oldest || ! thread->matched_messages)
141         thread->oldest = date;
142
143     if (date > thread->newest || ! thread->matched_messages)
144         thread->newest = date;
145
146     const char *subject;
147     const char *cleaned_subject;
148
149     subject = notmuch_message_get_header (message, "subject");
150
151     if ((strncasecmp (subject, "Re: ", 4) == 0) ||
152         (strncasecmp (subject, "Aw: ", 4) == 0) ||
153         (strncasecmp (subject, "Vs: ", 4) == 0) ||
154         (strncasecmp (subject, "Sv: ", 4) == 0)) {
155
156         cleaned_subject = talloc_strndup (thread,
157                                           subject + 4,
158                                           strlen(subject) - 4);
159     } else {
160         cleaned_subject = talloc_strdup (thread, subject);
161     }
162
163     if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
164         (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest)) {
165         thread->subject = talloc_strdup (thread, cleaned_subject);
166     }
167
168     thread->matched_messages++;
169
170     if (g_hash_table_lookup_extended (thread->message_hash,
171                             notmuch_message_get_message_id (message), NULL,
172                             (void **) &hashed_message)) {
173         notmuch_message_set_flag (hashed_message,
174                                   NOTMUCH_MESSAGE_FLAG_MATCH, 1);
175     }
176 }
177
178 static void
179 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
180 {
181     notmuch_message_node_t **prev, *node;
182     notmuch_message_t *message, *parent;
183     const char *in_reply_to;
184
185     prev = &thread->message_list->head;
186     while ((node = *prev)) {
187         message = node->message;
188         in_reply_to = _notmuch_message_get_in_reply_to (message);
189         if (in_reply_to && strlen (in_reply_to) &&
190             g_hash_table_lookup_extended (thread->message_hash,
191                                           in_reply_to, NULL,
192                                           (void **) &parent))
193         {
194             *prev = node->next;
195             if (thread->message_list->tail == &node->next)
196                 thread->message_list->tail = prev;
197             node->next = NULL;
198             _notmuch_message_add_reply (parent, node);
199         } else {
200             prev = &((*prev)->next);
201         }
202     }
203
204     /* XXX: After scanning through the entire list looking for parents
205      * via "In-Reply-To", we should do a second pass that looks at the
206      * list of messages IDs in the "References" header instead. (And
207      * for this the parent would be the "deepest" message of all the
208      * messages found in the "References" list.)
209      *
210      * Doing this will allow messages and sub-threads to be positioned
211      * correctly in the thread even when an intermediate message is
212      * missing from the thread.
213      */
214 }
215
216 /* Create a new notmuch_thread_t object for the given thread ID,
217  * treating any messages matching 'query_string' as "matched".
218  *
219  * Creating the thread will trigger two database searches. The first
220  * is for all messages belonging to the thread, (to get the first
221  * subject line, the total count of messages, and all authors). The
222  * second search is for all messages that are in the thread and that
223  * also match the given query_string. This is to allow for a separate
224  * count of matched messages, and to allow a viewer to display these
225  * messages differently.
226  *
227  * Here, 'ctx' is talloc context for the resulting thread object.
228  *
229  * This function returns NULL in the case of any error.
230  */
231 notmuch_thread_t *
232 _notmuch_thread_create (void *ctx,
233                         notmuch_database_t *notmuch,
234                         const char *thread_id,
235                         const char *query_string,
236                         notmuch_sort_t sort)
237 {
238     notmuch_thread_t *thread;
239     const char *thread_id_query_string;
240     notmuch_query_t *thread_id_query;
241
242     notmuch_messages_t *messages;
243     notmuch_message_t *message;
244     notmuch_bool_t matched_is_subset_of_thread;
245
246     thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
247     if (unlikely (query_string == NULL))
248         return NULL;
249
250     /* Under normal circumstances we need to do two database
251      * queries. One is for the thread itself (thread_id_query_string)
252      * and the second is to determine which messages in that thread
253      * match the original query (matched_query_string).
254      *
255      * But under two circumstances, we use only the
256      * thread_id_query_string:
257      *
258      *  1. If the original query_string *is* just the thread
259      *     specification.
260      *
261      *  2. If the original query_string matches all messages ("" or
262      *     "*").
263      *
264      * In either of these cases, we can be more efficient by running
265      * just the thread_id query (since we know all messages in the
266      * thread will match the query_string).
267      *
268      * Beyond the performance advantage, in the second case, it's
269      * important to not try to create a concatenated query because our
270      * parser handles "" and "*" as special cases and will not do the
271      * right thing with a query string of "* and thread:<foo>".
272      **/
273     matched_is_subset_of_thread = 1;
274     if (strcmp (query_string, thread_id_query_string) == 0 ||
275         strcmp (query_string, "") == 0 ||
276         strcmp (query_string, "*") == 0)
277     {
278         matched_is_subset_of_thread = 0;
279     }
280
281     thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
282     if (unlikely (thread_id_query == NULL))
283         return NULL;
284
285     thread = talloc (ctx, notmuch_thread_t);
286     if (unlikely (thread == NULL))
287         return NULL;
288
289     talloc_set_destructor (thread, _notmuch_thread_destructor);
290
291     thread->notmuch = notmuch;
292     thread->thread_id = talloc_strdup (thread, thread_id);
293     thread->subject = NULL;
294     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
295                                                   free, NULL);
296     thread->authors = NULL;
297     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
298                                           free, NULL);
299
300     thread->message_list = _notmuch_message_list_create (thread);
301     if (unlikely (thread->message_list == NULL))
302         return NULL;
303
304     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
305                                                   free, NULL);
306
307     thread->total_messages = 0;
308     thread->matched_messages = 0;
309     thread->oldest = 0;
310     thread->newest = 0;
311
312     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
313
314     for (messages = notmuch_query_search_messages (thread_id_query);
315          notmuch_messages_valid (messages);
316          notmuch_messages_move_to_next (messages))
317     {
318         message = notmuch_messages_get (messages);
319
320         _thread_add_message (thread, message);
321
322         if (! matched_is_subset_of_thread)
323             _thread_add_matched_message (thread, message, sort);
324
325         _notmuch_message_close (message);
326     }
327
328     notmuch_query_destroy (thread_id_query);
329
330     if (matched_is_subset_of_thread)
331     {
332         const char *matched_query_string;
333         notmuch_query_t *matched_query;
334
335         matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
336                                                 thread_id_query_string,
337                                                 query_string);
338         if (unlikely (matched_query_string == NULL))
339             return NULL;
340
341         matched_query = notmuch_query_create (notmuch, matched_query_string);
342         if (unlikely (matched_query == NULL))
343             return NULL;
344
345         for (messages = notmuch_query_search_messages (matched_query);
346              notmuch_messages_valid (messages);
347              notmuch_messages_move_to_next (messages))
348         {
349             message = notmuch_messages_get (messages);
350             _thread_add_matched_message (thread, message, sort);
351             _notmuch_message_close (message);
352         }
353
354         notmuch_query_destroy (matched_query);
355     }
356
357     _resolve_thread_relationships (thread);
358
359     return thread;
360 }
361
362 notmuch_messages_t *
363 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
364 {
365     return _notmuch_messages_create (thread->message_list);
366 }
367
368 const char *
369 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
370 {
371     return thread->thread_id;
372 }
373
374 int
375 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
376 {
377     return thread->total_messages;
378 }
379
380 int
381 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
382 {
383     return thread->matched_messages;
384 }
385
386 const char *
387 notmuch_thread_get_authors (notmuch_thread_t *thread)
388 {
389     return thread->authors;
390 }
391
392 const char *
393 notmuch_thread_get_subject (notmuch_thread_t *thread)
394 {
395     return thread->subject;
396 }
397
398 time_t
399 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
400 {
401     return thread->oldest;
402 }
403
404 time_t
405 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
406 {
407     return thread->newest;
408 }
409
410 notmuch_tags_t *
411 notmuch_thread_get_tags (notmuch_thread_t *thread)
412 {
413     notmuch_tags_t *tags;
414     GList *keys, *l;
415
416     tags = _notmuch_tags_create (thread);
417     if (unlikely (tags == NULL))
418         return NULL;
419
420     keys = g_hash_table_get_keys (thread->tags);
421
422     for (l = keys; l; l = l->next)
423         _notmuch_tags_add_tag (tags, (char *) l->data);
424
425     g_list_free (keys);
426
427     _notmuch_tags_prepare_iterator (tags);
428
429     return tags;
430 }
431
432 void
433 notmuch_thread_destroy (notmuch_thread_t *thread)
434 {
435     talloc_free (thread);
436 }