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