1 /* message.cc - Results of message-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 struct _notmuch_message {
27 notmuch_database_t *notmuch;
33 struct _notmuch_tags {
34 Xapian::TermIterator iterator;
35 Xapian::TermIterator iterator_end;
38 struct _notmuch_thread_ids {
43 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
45 /* These prefix values are specifically chosen to be compatible
46 * with sup, (http://sup.rubyforge.org), written by
47 * William Morgan <wmorgan-sup@masanjin.net>, and released
48 * under the GNU GPL v2.
56 prefix_t NORMAL_PREFIX[] = {
59 { "from_name", "FN" },
65 prefix_t BOOLEAN_PREFIX[] = {
67 { "from_email", "FE" },
74 { "attachment_extension", "O" },
81 _find_prefix (const char *name)
85 for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
86 if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
87 return NORMAL_PREFIX[i].prefix;
89 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
90 if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
91 return BOOLEAN_PREFIX[i].prefix;
96 /* We end up having to call the destructor explicitly because we had
97 * to use "placement new" in order to initialize C++ objects within a
98 * block that we allocated with talloc. So C++ is making talloc
99 * slightly less simple to use, (we wouldn't need
100 * talloc_set_destructor at all otherwise).
103 _notmuch_message_destructor (notmuch_message_t *message)
105 message->doc.~Document ();
111 _notmuch_message_create (const void *talloc_owner,
112 notmuch_database_t *notmuch,
115 notmuch_message_t *message;
117 message = talloc (talloc_owner, notmuch_message_t);
118 if (unlikely (message == NULL))
121 message->notmuch = notmuch;
122 message->doc_id = doc_id;
123 message->message_id = NULL; /* lazily created */
124 new (&message->doc) Xapian::Document;
126 talloc_set_destructor (message, _notmuch_message_destructor);
128 message->doc = notmuch->xapian_db->get_document (doc_id);
134 notmuch_message_get_message_id (notmuch_message_t *message)
136 Xapian::TermIterator i;
138 if (message->message_id)
139 return message->message_id;
141 i = message->doc.termlist_begin ();
142 i.skip_to (_find_prefix ("msgid"));
144 /* XXX: This should really be an internal error, but we'll need to
145 * fix the add_message side of things first. */
146 if (i == message->doc.termlist_end ())
149 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
150 return message->message_id;
153 /* We end up having to call the destructors explicitly because we had
154 * to use "placement new" in order to initialize C++ objects within a
155 * block that we allocated with talloc. So C++ is making talloc
156 * slightly less simple to use, (we wouldn't need
157 * talloc_set_destructor at all otherwise).
160 _notmuch_tags_destructor (notmuch_tags_t *tags)
162 tags->iterator.~TermIterator ();
163 tags->iterator_end.~TermIterator ();
169 notmuch_message_get_tags (notmuch_message_t *message)
171 notmuch_tags_t *tags;
173 tags = talloc (message, notmuch_tags_t);
174 if (unlikely (tags == NULL))
177 new (&tags->iterator) Xapian::TermIterator;
178 new (&tags->iterator_end) Xapian::TermIterator;
180 talloc_set_destructor (tags, _notmuch_tags_destructor);
182 tags->iterator = message->doc.termlist_begin ();
183 tags->iterator.skip_to (_find_prefix ("tag"));
184 tags->iterator_end = message->doc.termlist_end ();
189 notmuch_thread_ids_t *
190 notmuch_message_get_thread_ids (notmuch_message_t *message)
192 notmuch_thread_ids_t *thread_ids;
195 thread_ids = talloc (message, notmuch_thread_ids_t);
196 if (unlikely (thread_ids == NULL))
199 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
200 thread_ids->next = talloc_strdup (message, id_str.c_str ());
202 /* Initialize thread_ids->current and terminate first ID. */
203 notmuch_thread_ids_advance (thread_ids);
208 /* Synchronize changes made to message->doc into the database. */
210 _notmuch_message_sync (notmuch_message_t *message)
212 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
214 db->replace_document (message->doc_id, message->doc);
217 /* Add a name:value term to 'message', (the actual term will be
218 * encoded by prefixing the value with a short prefix). See
219 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
220 * names to prefix values.
222 * This change will not be reflected in the database until the next
223 * call to _notmuch_message_set_sync. */
224 notmuch_private_status_t
225 _notmuch_message_add_term (notmuch_message_t *message,
226 const char *prefix_name,
233 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
235 term = talloc_asprintf (message, "%s%s",
236 _find_prefix (prefix_name), value);
238 if (strlen (term) > NOTMUCH_TERM_MAX)
239 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
241 message->doc.add_term (term);
245 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
248 /* Remove a name:value term from 'message', (the actual term will be
249 * encoded by prefixing the value with a short prefix). See
250 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
251 * names to prefix values.
253 * This change will not be reflected in the database until the next
254 * call to _notmuch_message_set_sync. */
255 notmuch_private_status_t
256 _notmuch_message_remove_term (notmuch_message_t *message,
257 const char *prefix_name,
263 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
265 term = talloc_asprintf (message, "%s%s",
266 _find_prefix (prefix_name), value);
268 if (strlen (term) > NOTMUCH_TERM_MAX)
269 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
271 message->doc.remove_term (term);
275 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
279 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
281 notmuch_private_status_t status;
284 return NOTMUCH_STATUS_NULL_POINTER;
286 if (strlen (tag) > NOTMUCH_TAG_MAX)
287 return NOTMUCH_STATUS_TAG_TOO_LONG;
289 status = _notmuch_message_add_term (message, "tag", tag);
291 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
296 _notmuch_message_sync (message);
298 return NOTMUCH_STATUS_SUCCESS;
302 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
304 notmuch_private_status_t status;
307 return NOTMUCH_STATUS_NULL_POINTER;
309 if (strlen (tag) > NOTMUCH_TAG_MAX)
310 return NOTMUCH_STATUS_TAG_TOO_LONG;
312 status = _notmuch_message_remove_term (message, "tag", tag);
314 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
319 _notmuch_message_sync (message);
321 return NOTMUCH_STATUS_SUCCESS;
325 notmuch_message_destroy (notmuch_message_t *message)
327 talloc_free (message);
331 notmuch_tags_has_more (notmuch_tags_t *tags)
335 if (tags->iterator == tags->iterator_end)
339 if (s.size () && s[0] == 'L')
346 notmuch_tags_get (notmuch_tags_t *tags)
348 return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
352 notmuch_tags_advance (notmuch_tags_t *tags)
358 notmuch_tags_destroy (notmuch_tags_t *tags)
364 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
366 if (thread_ids->current == NULL || *thread_ids->current == '\0')
373 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
375 return thread_ids->current;
379 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
381 thread_ids->current = strsep (&thread_ids->next, ",");
385 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
387 talloc_free (thread_ids);