]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
notmuch search: Print all authors contributing to a thread.
[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     char *authors;
34     GHashTable *tags;
35
36     notmuch_bool_t has_message;
37     time_t oldest;
38     time_t newest;
39 };
40
41 static int
42 _notmuch_thread_destructor (notmuch_thread_t *thread)
43 {
44     g_hash_table_unref (thread->tags);
45
46     return 0;
47 }
48
49 static void
50 _thread_add_message (notmuch_thread_t *thread,
51                      notmuch_message_t *message)
52 {
53     notmuch_tags_t *tags;
54     const char *tag;
55     time_t date;
56     InternetAddressList *list;
57     InternetAddress *address;
58     const char *from, *author;
59
60     from = notmuch_message_get_header (message, "from");
61     list = internet_address_list_parse_string (from);
62     if (list) {
63         address = internet_address_list_get_address (list, 0);
64         if (address) {
65             author = internet_address_get_name (address);
66             if (author == NULL) {
67                 InternetAddressMailbox *mailbox;
68                 mailbox = INTERNET_ADDRESS_MAILBOX (address);
69                 author = internet_address_mailbox_get_addr (mailbox);
70             }
71             if (author) {
72                 if (thread->authors)
73                     thread->authors = talloc_asprintf (thread, "%s, %s",
74                                                        thread->authors,
75                                                        author);
76                 else
77                     thread->authors = talloc_strdup (thread, author);
78             }
79         }
80         g_object_unref (G_OBJECT (list));
81     }
82
83     if (! thread->subject) {
84         const char *subject;
85         subject = notmuch_message_get_header (message, "subject");
86         thread->subject = talloc_strdup (thread, subject);
87     }
88
89     for (tags = notmuch_message_get_tags (message);
90          notmuch_tags_has_more (tags);
91          notmuch_tags_advance (tags))
92     {
93         tag = notmuch_tags_get (tags);
94         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
95     }
96
97     date = notmuch_message_get_date (message);
98
99     if (date < thread->oldest || ! thread->has_message)
100         thread->oldest = date;
101
102     if (date > thread->newest || ! thread->has_message)
103         thread->newest = date;
104
105     thread->has_message = 1;
106 }
107
108 /* Create a new notmuch_thread_t object for the given thread ID.
109  *
110  * Creating the thread will trigger a database search for the messages
111  * belonging to the thread so that the thread object can return some
112  * details about them, (authors, subject, etc.).
113  *
114  * Here, 'talloc owner' is an optional talloc context to which the new
115  * thread will belong. This allows for the caller to not bother
116  * calling notmuch_thread_destroy on the thread, and know that all
117  * memory will be reclaimed with 'talloc_owner' is freed. The caller
118  * still can call notmuch_thread_destroy when finished with the
119  * thread if desired.
120  *
121  * The 'talloc_owner' argument can also be NULL, in which case the
122  * caller *is* responsible for calling notmuch_thread_destroy.
123  *
124  * This function returns NULL in the case of any error.
125  */
126 notmuch_thread_t *
127 _notmuch_thread_create (const void *ctx,
128                         notmuch_database_t *notmuch,
129                         const char *thread_id)
130 {
131     notmuch_thread_t *thread;
132     const char *query_string;
133     notmuch_query_t *query;
134     notmuch_messages_t *messages;
135
136     query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
137     if (unlikely (query_string == NULL))
138         return NULL;
139
140     query = notmuch_query_create (notmuch, query_string);
141     if (unlikely (query == NULL))
142         return NULL;
143
144     thread = talloc (ctx, notmuch_thread_t);
145     if (unlikely (thread == NULL))
146         return NULL;
147
148     talloc_set_destructor (thread, _notmuch_thread_destructor);
149
150     thread->notmuch = notmuch;
151     thread->thread_id = talloc_strdup (thread, thread_id);
152     thread->subject = NULL;
153     thread->authors = NULL;
154     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
155                                           free, NULL);
156
157     thread->has_message = 0;
158     thread->oldest = 0;
159     thread->newest = 0;
160
161     notmuch_query_set_sort (query, NOTMUCH_SORT_DATE_OLDEST_FIRST);
162
163     for (messages = notmuch_query_search_messages (query, 0, -1);
164          notmuch_messages_has_more (messages);
165          notmuch_messages_advance (messages))
166     {
167         _thread_add_message (thread, notmuch_messages_get (messages));
168     }
169
170     notmuch_query_destroy (query);
171
172     return thread;
173 }
174
175 const char *
176 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
177 {
178     return thread->thread_id;
179 }
180
181 const char *
182 notmuch_thread_get_authors (notmuch_thread_t *thread)
183 {
184     return thread->authors;
185 }
186
187 const char *
188 notmuch_thread_get_subject (notmuch_thread_t *thread)
189 {
190     return thread->subject;
191 }
192
193 time_t
194 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
195 {
196     return thread->oldest;
197 }
198
199 time_t
200 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
201 {
202     return thread->newest;
203 }
204
205 notmuch_tags_t *
206 notmuch_thread_get_tags (notmuch_thread_t *thread)
207 {
208     notmuch_tags_t *tags;
209     GList *keys, *l;
210
211     tags = _notmuch_tags_create (thread);
212     if (unlikely (tags == NULL))
213         return NULL;
214
215     keys = g_hash_table_get_keys (thread->tags);
216
217     for (l = keys; l; l = l->next)
218         _notmuch_tags_add_tag (tags, (char *) l->data);
219
220     g_list_free (keys);
221
222     _notmuch_tags_prepare_iterator (tags);
223
224     return tags;
225 }
226
227 void
228 notmuch_thread_destroy (notmuch_thread_t *thread)
229 {
230     talloc_free (thread);
231 }