]> git.notmuchmail.org Git - notmuch/blob - notmuch.h
Add destroy functions for results, message, and tags.
[notmuch] / notmuch.h
1 /* notmuch - Not much of an email library, (just index and search)
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 #ifndef NOTMUCH_H
22 #define NOTMUCH_H
23
24 #ifdef  __cplusplus
25 # define NOTMUCH_BEGIN_DECLS  extern "C" {
26 # define NOTMUCH_END_DECLS    }
27 #else
28 # define NOTMUCH_BEGIN_DECLS
29 # define NOTMUCH_END_DECLS
30 #endif
31
32 NOTMUCH_BEGIN_DECLS
33
34 #ifndef FALSE
35 #define FALSE 0
36 #endif
37
38 #ifndef TRUE
39 #define TRUE 1
40 #endif
41
42 typedef int notmuch_bool_t;
43
44 /* Status codes used for the return values of most functions.
45  *
46  * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
47  * completed without error. Any other value indicates an error as
48  * follows:
49  *
50  * NOTMUCH_STATUS_SUCCESS: No error occurred.
51  *
52  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
53  *
54  * NOTMUCH_STATUS_FILE_NOT_EMAIL: A file was presented that doesn't
55  *      appear to be an email message.
56  */
57 typedef enum _notmuch_status {
58     NOTMUCH_STATUS_SUCCESS = 0,
59     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
60     NOTMUCH_STATUS_FILE_NOT_EMAIL
61 } notmuch_status_t;
62
63 /* Various opaque data types. For each notmuch_<foo>_t see the various
64  * notmuch_<foo> functions below. */
65 typedef struct _notmuch_database notmuch_database_t;
66 typedef struct _notmuch_query notmuch_query_t;
67 typedef struct _notmuch_results notmuch_results_t;
68 typedef struct _notmuch_message notmuch_message_t;
69 typedef struct _notmuch_tags notmuch_tags_t;
70
71 /* Create a new, empty notmuch database located at 'path'.
72  *
73  * The path should be a top-level directory to a collection of
74  * plain-text email messages (one message per file). This call will
75  * create a new ".notmuch" directory within 'path' where notmuch will
76  * store its data.
77  *
78  * Passing a value of NULL for 'path' will cause notmuch to open the
79  * default database. The default database path can be specified by the
80  * NOTMUCH_BASE environment variable, and is equivalent to
81  * ${HOME}/mail if NOTMUCH_BASE is not set.
82  *
83  * After a successful call to notmuch_database_create, the returned
84  * database will be open so the caller should call
85  * notmuch_database_close when finished with it.
86  *
87  * The database will not yet have any data in it
88  * (notmuch_database_create itself is a very cheap function). Messages
89  * contained within 'path' can be added to the database by calling
90  * notmuch_database_add_message.
91  *
92  * In case of any failure, this function returns NULL, (after printing
93  * an error message on stderr).
94  */
95 notmuch_database_t *
96 notmuch_database_create (const char *path);
97
98 /* Open a an existing notmuch database located at 'path'.
99  *
100  * The database should have been created at some time in the past,
101  * (not necessarily by this process), by calling
102  * notmuch_database_create with 'path'.
103  *
104  * An existing notmuch database can be identified by the presence of a
105  * directory named ".notmuch" below 'path'.
106  *
107  * Passing a value of NULL for 'path' will cause notmuch to open the
108  * default database. The default database path can be specified by the
109  * NOTMUCH_BASE environment variable, and is equivalent to
110  * ${HOME}/mail if NOTMUCH_BASE is not set.
111  *
112  * The caller should call notmuch_database_close when finished with
113  * this database.
114  *
115  * In case of any failure, this function returns NULL, (after printing
116  * an error message on stderr).
117  */
118 notmuch_database_t *
119 notmuch_database_open (const char *path);
120
121 /* Close the given notmuch database, freeing all associated
122  * resources. See notmuch_database_open. */
123 void
124 notmuch_database_close (notmuch_database_t *database);
125
126 /* Lookup the default database path.
127  *
128  * This is the path that will be used by notmuch_database_create and
129  * notmuch_database_open if given a NULL path. Specifically it will be
130  * the value of the NOTMUCH_BASE environment variable if set,
131  * otherwise ${HOME}/mail
132  *
133  * Returns a newly allocated string which the caller should free()
134  * when finished with it.
135  */
136 char *
137 notmuch_database_default_path (void);
138
139 /* Return the database path of the given database.
140  *
141  * The return value is a string owned by notmuch so should not be
142  * modified nor freed by the caller. */
143 const char *
144 notmuch_database_get_path (notmuch_database_t *database);
145
146 /* Add a new message to the given notmuch database.
147  *
148  * Here,'filename' should be a path relative to the the path of
149  * 'database' (see notmuch_database_get_path). The file should be a
150  * single mail message (not a multi-message mbox) that is expected to
151  * remain at its current location, (since the notmuch database will
152  * reference the filename, and will not copy the entire contents of
153  * the file.
154  *
155  * Return value:
156  *
157  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
158  *
159  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
160  *      like an email message. Nothing added to the database.
161  */
162 notmuch_status_t
163 notmuch_database_add_message (notmuch_database_t *database,
164                               const char *filename);
165
166 /* Create a new query for 'database'.
167  *
168  * Here, 'database' should be an open database, (see
169  * notmuch_database_open and notmuch_database_create).
170  *
171  * For the query string, we'll document the syntax here more
172  * completely in the future, but it's likely to be a specialized
173  * version of the general Xapian query syntax:
174  *
175  * http://xapian.org/docs/queryparser.html
176  *
177  * As a special case, passing a value of NOTMUCH_QUERY_ALL for the
178  * query string will result in a query that returns all messages in
179  * the database.
180  *
181  * See notmuch_query_set_sort for controlling the order of results and
182  * notmuch_query_search to actually execute the query.
183  *
184  * User should call notmuch_query_destroy when finished with this
185  * query.
186  *
187  * Will return NULL if insufficient memory is available.
188  */
189 notmuch_query_t *
190 notmuch_query_create (notmuch_database_t *database,
191                       const char *query_string);
192
193 /* Special value to cause notmuch_query_create to return all
194  * messages. */
195 extern const char *NOTMUCH_QUERY_ALL;
196
197 /* Sort values for notmuch_query_set_sort */
198 typedef enum {
199     NOTMUCH_SORT_DATE_OLDEST_FIRST,
200     NOTMUCH_SORT_DATE_NEWEST_FIRST,
201     NOTMUCH_SORT_MESSAGE_ID
202 } notmuch_sort_t;
203
204 /* Specify the sorting desired for this query. */
205 void
206 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
207
208 /* Execute a query, returning a notmuch_results_t object which can be
209  * used to iterate over the results. The results object is owned by
210  * the query and as such, will only be valid until notmuch_query_destroy.
211  *
212  * Typical usage might be:
213  *
214  *     notmuch_query_t *query;
215  *     notmuch_results_t *results;
216  *
217  *     query = notmuch_query_create (database, query_string);
218  *
219  *     for (results = notmuch_query_search (query);
220  *          notmuch_results_has_more (results);
221  *          notmuch_result_advance (results))
222  *     {
223  *         message = notmuch_results_get (results);
224  *         ....
225  *     }
226  *
227  *     notmuch_query_destroy (query);
228  *
229  * Note that there's no explicit destructor needed for the
230  * notmuch_results_t object.
231  *
232  * (For consistency, we do provide a notmuch_results_destroy function,
233  * but there's no point in calling it if you're about to destroy the
234  * query object as well too---either call will free all the memory of
235  * the results).
236  */
237 notmuch_results_t *
238 notmuch_query_search (notmuch_query_t *query);
239
240 /* Destroy a notmuch_query_t along with any associated resources.
241  *
242  * This will in turn destroy any notmuch_results_t objects generated
243  * by this query, (and in turn any notmuch_message_t objects generated
244  * from those results, etc.).
245  */
246 void
247 notmuch_query_destroy (notmuch_query_t *query);
248
249 /* Does the given notmuch_results_t object contain any more results.
250  *
251  * When this function returns TRUE, notmuch_results_get will return a
252  * valid object. Whereas when this function returns FALSE,
253  * notmuch_results_get will return NULL.
254  *
255  * See the documentation of notmuch_query_search for example code
256  * showing how to iterate over a notmuch_results_t object.
257  */
258 notmuch_bool_t
259 notmuch_results_has_more (notmuch_results_t *results);
260
261 /* Get the current result from 'results' as a notmuch_message_t.
262  *
263  * Note: The returned message belongs to 'results' and has a lifetime
264  * identical to it (and the query to which it belongs).
265  *
266  * See the documentation of notmuch_query_search for example code
267  * showing how to iterate over a notmuch_results_t object.
268  */
269 notmuch_message_t *
270 notmuch_results_get (notmuch_results_t *results);
271
272 /* Advance the 'results' iterator to the next result.
273  *
274  * See the documentation of notmuch_query_search for example code
275  * showing how to iterate over a notmuch_results_t object.
276  */
277 void
278 notmuch_results_advance (notmuch_results_t *results);
279
280 /* Destroy a notmuch_results_t object.
281  *
282  * It's not strictly necessary to call this function. All memory from
283  * the notmuch_results_t object will be reclaimed when the containg
284  * query object is destroyed.
285  */
286 void
287 notmuch_results_destroy (notmuch_results_t *results);
288
289 /* Get the message ID of 'message'.
290  *
291  * The returned string belongs to 'message' and as such, should not be
292  * modified by the caller and will only be valid for as long as the
293  * message is valid, (which is until the query from which it derived
294  * is destroyed).
295  */
296 const char *
297 notmuch_message_get_message_id (notmuch_message_t *message);
298
299 /* Get the tags for 'message', returning a notmuch_tags_t object which
300  * can be used to iterate over all tags.
301  *
302  * The tags object is owned by the message and as such, will only be
303  * valid for as long as the message is valid, (which is until the
304  * query from which it derived is destroyed).
305  *
306  * Typical usage might be:
307  *
308  *     notmuch_message_t *message;
309  *     notmuch_tags_t *tags;
310  *     const char *tag;
311  *
312  *     message = notmuch_results_get (results);
313  *
314  *     for (tags = notmuch_message_get_tags (message);
315  *          notmuch_tags_has_more (tags);
316  *          notmuch_result_advance (tags))
317  *     {
318  *         tag = notmuch_tags_get_string (tags);
319  *         ....
320  *     }
321  *
322  * Note: If you are finished with a message before its containing
323  * query, you can call notmuch_message_destroy to clean up some memory
324  * sooner. If you don't call it, all the memory will still be
325  * reclaimed when the query is destroyed.
326  */
327 notmuch_tags_t *
328 notmuch_message_get_tags (notmuch_message_t *message);
329
330 /* Destroy a notmuch_message_t object.
331  *
332  * It can be useful to call this function in the case of a single
333  * query object with many messages in the result, (such as iterating
334  * over the entire database). Otherwise, it's fine to never call this
335  * function and there will still be no memory leaks. (The memory from
336  * the messages get reclaimed when the containing query is destroyed.)
337  */
338 void
339 notmuch_message_destroy (notmuch_message_t *message);
340
341 /* Does the given notmuch_tags_t object contain any more results.
342  *
343  * When this function returns TRUE, notmuch_tags_get will return a
344  * valid string. Whereas when this function returns FALSE,
345  * notmuch_tags_get will return NULL.
346  *
347  * See the documentation of notmuch_message_get_tags for example code
348  * showing how to iterate over a notmuch_tags_t object.
349  */
350 notmuch_bool_t
351 notmuch_tags_has_more (notmuch_tags_t *tags);
352
353 /* Get the current result from 'tags' as a string.
354  *
355  * Note: The returned string belongs to 'tags' and has a lifetime
356  * identical to it (and the query to which it utlimately belongs).
357  *
358  * See the documentation of notmuch_message_get_tags for example code
359  * showing how to iterate over a notmuch_tags_t object.
360  */
361 const char *
362 notmuch_tags_get (notmuch_tags_t *tags);
363
364 /* Advance the 'tags' iterator to the next tag.
365  *
366  * See the documentation of notmuch_message_get_tags for example code
367  * showing how to iterate over a notmuch_tags_t object.
368  */
369 void
370 notmuch_tags_advance (notmuch_tags_t *results);
371
372 /* Destroy a notmuch_tags_t object.
373  *
374  * It's not strictly necessary to call this function. All memory from
375  * the notmuch_tags_t object will be reclaimed when the containg
376  * message or query objects are destroyed.
377  */
378 void
379 notmuch_tags_destroy (notmuch_tags_t *tags);
380
381 NOTMUCH_END_DECLS
382
383 #endif