]> git.notmuchmail.org Git - notmuch/blob - message.cc
5d99321d01a9c1d19b6d359b152ee46125e502f9
[notmuch] / message.cc
1 /* message.cc - Results of message-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 struct _notmuch_message {
27     Xapian::Document doc;
28 };
29
30 struct _notmuch_tags {
31     Xapian::TermIterator iterator;
32     Xapian::TermIterator iterator_end;
33 };
34
35 static int
36 _notmuch_message_destroy (notmuch_message_t *message)
37 {
38     message->doc.~Document ();
39
40     return 0;
41 }
42
43 notmuch_message_t *
44 _notmuch_message_create (notmuch_results_t *owner,
45                          notmuch_database_t *notmuch,
46                          Xapian::docid doc_id)
47 {
48     notmuch_message_t *message;
49
50     message = talloc (owner, notmuch_message_t);
51     if (unlikely (message == NULL))
52         return NULL;
53
54     new (&message->doc) Xapian::Document;
55
56     talloc_set_destructor (message, _notmuch_message_destroy);
57
58     message->doc = notmuch->xapian_db->get_document (doc_id);
59
60     return message;
61 }
62
63 const char *
64 notmuch_message_get_message_id (notmuch_message_t *message)
65 {
66     Xapian::TermIterator i;
67
68     i = message->doc.termlist_begin ();
69     i.skip_to ("Q");
70     if (i != message->doc.termlist_end ())
71         return talloc_strdup (message, (*i).c_str () + 1);
72     else
73         return NULL;
74 }
75
76 static int
77 _notmuch_tags_destroy (notmuch_tags_t *tags)
78 {
79     tags->iterator.~TermIterator ();
80     tags->iterator_end.~TermIterator ();
81
82     return 0;
83 }
84
85 notmuch_tags_t *
86 notmuch_message_get_tags (notmuch_message_t *message)
87 {
88     notmuch_tags_t *tags;
89
90     tags = talloc (message, notmuch_tags_t);
91     if (unlikely (tags == NULL))
92         return NULL;
93
94     new (&tags->iterator) Xapian::TermIterator;
95     new (&tags->iterator_end) Xapian::TermIterator;
96
97     talloc_set_destructor (tags, _notmuch_tags_destroy);
98
99     tags->iterator = message->doc.termlist_begin ();
100     tags->iterator.skip_to ("L");
101     tags->iterator_end = message->doc.termlist_end ();
102
103     return tags;
104 }
105
106 notmuch_bool_t
107 notmuch_tags_has_more (notmuch_tags_t *tags)
108 {
109     std::string s;
110
111     if (tags->iterator == tags->iterator_end)
112         return FALSE;
113
114     s = *tags->iterator;
115     if (s.size () && s[0] == 'L')
116         return TRUE;
117     else
118         return FALSE;
119 }
120
121 const char *
122 notmuch_tags_get (notmuch_tags_t *tags)
123 {
124     return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
125 }
126
127 void
128 notmuch_tags_advance (notmuch_tags_t *tags)
129 {
130     tags->iterator++;
131 }