]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch.h
lib: add support for path: prefix searches
[notmuch] / lib / 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 /**
22  * @defgroup notmuch The notmuch API
23  *
24  * Not much of an email library, (just index and search)
25  *
26  * @{
27  */
28
29 #ifndef NOTMUCH_H
30 #define NOTMUCH_H
31
32 #ifndef __DOXYGEN__
33
34 #ifdef  __cplusplus
35 # define NOTMUCH_BEGIN_DECLS  extern "C" {
36 # define NOTMUCH_END_DECLS    }
37 #else
38 # define NOTMUCH_BEGIN_DECLS
39 # define NOTMUCH_END_DECLS
40 #endif
41
42 NOTMUCH_BEGIN_DECLS
43
44 #include <time.h>
45
46 #ifndef FALSE
47 #define FALSE 0
48 #endif
49
50 #ifndef TRUE
51 #define TRUE 1
52 #endif
53
54 /*
55  * The library version number.  This must agree with the soname
56  * version in Makefile.local.
57  */
58 #define LIBNOTMUCH_MAJOR_VERSION        3
59 #define LIBNOTMUCH_MINOR_VERSION        1
60 #define LIBNOTMUCH_MICRO_VERSION        0
61
62 #endif /* __DOXYGEN__ */
63
64 /**
65  * Check the version of the notmuch library being compiled against.
66  *
67  * Return true if the library being compiled against is of the
68  * specified version or above. For example:
69  *
70  * @code
71  * #if LIBNOTMUCH_CHECK_VERSION(3, 1, 0)
72  *     (code requiring libnotmuch 3.1.0 or above)
73  * #endif
74  * @endcode
75  *
76  * LIBNOTMUCH_CHECK_VERSION has been defined since version 3.1.0; to
77  * check for versions prior to that, use:
78  *
79  * @code
80  * #if !defined(NOTMUCH_CHECK_VERSION)
81  *     (code requiring libnotmuch prior to 3.1.0)
82  * #endif
83  * @endcode
84  */
85 #define LIBNOTMUCH_CHECK_VERSION (major, minor, micro)                  \
86     (LIBNOTMUCH_MAJOR_VERSION > (major) ||                                      \
87      (LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION > (minor)) || \
88      (LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION == (minor) && \
89       LIBNOTMUCH_MICRO_VERSION >= (micro)))
90
91 /**
92  * Notmuch boolean type.
93  */
94 typedef int notmuch_bool_t;
95
96 /**
97  * Status codes used for the return values of most functions.
98  *
99  * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
100  * completed without error. Any other value indicates an error.
101  */
102 typedef enum _notmuch_status {
103     /**
104      * No error occurred.
105      */
106     NOTMUCH_STATUS_SUCCESS = 0,
107     /**
108      * Out of memory.
109      */
110     NOTMUCH_STATUS_OUT_OF_MEMORY,
111     /**
112      * An attempt was made to write to a database opened in read-only
113      * mode.
114      */
115     NOTMUCH_STATUS_READ_ONLY_DATABASE,
116     /**
117      * A Xapian exception occurred.
118      */
119     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
120     /**
121      * An error occurred trying to read or write to a file (this could
122      * be file not found, permission denied, etc.)
123      *
124      * @todo We don't really want to expose this lame XAPIAN_EXCEPTION
125      * value. Instead we should map to things like DATABASE_LOCKED or
126      * whatever.
127      */
128     NOTMUCH_STATUS_FILE_ERROR,
129     /**
130      * A file was presented that doesn't appear to be an email
131      * message.
132      */
133     NOTMUCH_STATUS_FILE_NOT_EMAIL,
134     /**
135      * A file contains a message ID that is identical to a message
136      * already in the database.
137      */
138     NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID,
139     /**
140      * The user erroneously passed a NULL pointer to a notmuch
141      * function.
142      */
143     NOTMUCH_STATUS_NULL_POINTER,
144     /**
145      * A tag value is too long (exceeds NOTMUCH_TAG_MAX).
146      */
147     NOTMUCH_STATUS_TAG_TOO_LONG,
148     /**
149      * The notmuch_message_thaw function has been called more times
150      * than notmuch_message_freeze.
151      */
152     NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
153     /**
154      * notmuch_database_end_atomic has been called more times than
155      * notmuch_database_begin_atomic.
156      */
157     NOTMUCH_STATUS_UNBALANCED_ATOMIC,
158     /**
159      * The operation is not supported.
160      */
161     NOTMUCH_STATUS_UNSUPPORTED_OPERATION,
162     /**
163      * Not an actual status value. Just a way to find out how many
164      * valid status values there are.
165      */
166     NOTMUCH_STATUS_LAST_STATUS
167 } notmuch_status_t;
168
169 /**
170  * Get a string representation of a notmuch_status_t value.
171  *
172  * The result is read-only.
173  */
174 const char *
175 notmuch_status_to_string (notmuch_status_t status);
176
177 /* Various opaque data types. For each notmuch_<foo>_t see the various
178  * notmuch_<foo> functions below. */
179 #ifndef __DOXYGEN__
180 typedef struct _notmuch_database notmuch_database_t;
181 typedef struct _notmuch_query notmuch_query_t;
182 typedef struct _notmuch_threads notmuch_threads_t;
183 typedef struct _notmuch_thread notmuch_thread_t;
184 typedef struct _notmuch_messages notmuch_messages_t;
185 typedef struct _notmuch_message notmuch_message_t;
186 typedef struct _notmuch_tags notmuch_tags_t;
187 typedef struct _notmuch_directory notmuch_directory_t;
188 typedef struct _notmuch_filenames notmuch_filenames_t;
189 #endif /* __DOXYGEN__ */
190
191 /**
192  * Create a new, empty notmuch database located at 'path'.
193  *
194  * The path should be a top-level directory to a collection of
195  * plain-text email messages (one message per file). This call will
196  * create a new ".notmuch" directory within 'path' where notmuch will
197  * store its data.
198  *
199  * After a successful call to notmuch_database_create, the returned
200  * database will be open so the caller should call
201  * notmuch_database_destroy when finished with it.
202  *
203  * The database will not yet have any data in it
204  * (notmuch_database_create itself is a very cheap function). Messages
205  * contained within 'path' can be added to the database by calling
206  * notmuch_database_add_message.
207  *
208  * In case of any failure, this function returns an error status and
209  * sets *database to NULL (after printing an error message on stderr).
210  *
211  * Return value:
212  *
213  * NOTMUCH_STATUS_SUCCESS: Successfully created the database.
214  *
215  * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
216  *
217  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
218  *
219  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to create the
220  *      database file (such as permission denied, or file not found,
221  *      etc.), or the database already exists.
222  *
223  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
224  */
225 notmuch_status_t
226 notmuch_database_create (const char *path, notmuch_database_t **database);
227
228 /**
229  * Database open mode for notmuch_database_open.
230  */
231 typedef enum {
232     /**
233      * Open database for reading only.
234      */
235     NOTMUCH_DATABASE_MODE_READ_ONLY = 0,
236     /**
237      * Open database for reading and writing.
238      */
239     NOTMUCH_DATABASE_MODE_READ_WRITE
240 } notmuch_database_mode_t;
241
242 /**
243  * Open an existing notmuch database located at 'path'.
244  *
245  * The database should have been created at some time in the past,
246  * (not necessarily by this process), by calling
247  * notmuch_database_create with 'path'. By default the database should be
248  * opened for reading only. In order to write to the database you need to
249  * pass the NOTMUCH_DATABASE_MODE_READ_WRITE mode.
250  *
251  * An existing notmuch database can be identified by the presence of a
252  * directory named ".notmuch" below 'path'.
253  *
254  * The caller should call notmuch_database_destroy when finished with
255  * this database.
256  *
257  * In case of any failure, this function returns an error status and
258  * sets *database to NULL (after printing an error message on stderr).
259  *
260  * Return value:
261  *
262  * NOTMUCH_STATUS_SUCCESS: Successfully opened the database.
263  *
264  * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
265  *
266  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
267  *
268  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to open the
269  *      database file (such as permission denied, or file not found,
270  *      etc.), or the database version is unknown.
271  *
272  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
273  */
274 notmuch_status_t
275 notmuch_database_open (const char *path,
276                        notmuch_database_mode_t mode,
277                        notmuch_database_t **database);
278
279 /**
280  * Close the given notmuch database.
281  *
282  * After notmuch_database_close has been called, calls to other
283  * functions on objects derived from this database may either behave
284  * as if the database had not been closed (e.g., if the required data
285  * has been cached) or may fail with a
286  * NOTMUCH_STATUS_XAPIAN_EXCEPTION.
287  *
288  * notmuch_database_close can be called multiple times.  Later calls
289  * have no effect.
290  */
291 void
292 notmuch_database_close (notmuch_database_t *database);
293
294 /**
295  * A callback invoked by notmuch_database_compact to notify the user
296  * of the progress of the compaction process.
297  */
298 typedef void (*notmuch_compact_status_cb_t)(const char *message, void *closure);
299
300 /**
301  * Compact a notmuch database, backing up the original database to the
302  * given path.
303  *
304  * The database will be opened with NOTMUCH_DATABASE_MODE_READ_WRITE
305  * during the compaction process to ensure no writes are made.
306  *
307  * If the optional callback function 'status_cb' is non-NULL, it will
308  * be called with diagnostic and informational messages. The argument
309  * 'closure' is passed verbatim to any callback invoked.
310  */
311 notmuch_status_t
312 notmuch_database_compact (const char* path,
313                           const char* backup_path,
314                           notmuch_compact_status_cb_t status_cb,
315                           void *closure);
316
317 /**
318  * Destroy the notmuch database, closing it if necessary and freeing
319  * all associated resources.
320  */
321 void
322 notmuch_database_destroy (notmuch_database_t *database);
323
324 /**
325  * Return the database path of the given database.
326  *
327  * The return value is a string owned by notmuch so should not be
328  * modified nor freed by the caller.
329  */
330 const char *
331 notmuch_database_get_path (notmuch_database_t *database);
332
333 /**
334  * Return the database format version of the given database.
335  */
336 unsigned int
337 notmuch_database_get_version (notmuch_database_t *database);
338
339 /**
340  * Does this database need to be upgraded before writing to it?
341  *
342  * If this function returns TRUE then no functions that modify the
343  * database (notmuch_database_add_message, notmuch_message_add_tag,
344  * notmuch_directory_set_mtime, etc.) will work unless the function
345  * notmuch_database_upgrade is called successfully first.
346  */
347 notmuch_bool_t
348 notmuch_database_needs_upgrade (notmuch_database_t *database);
349
350 /**
351  * Upgrade the current database.
352  *
353  * After opening a database in read-write mode, the client should
354  * check if an upgrade is needed (notmuch_database_needs_upgrade) and
355  * if so, upgrade with this function before making any modifications.
356  *
357  * The optional progress_notify callback can be used by the caller to
358  * provide progress indication to the user. If non-NULL it will be
359  * called periodically with 'progress' as a floating-point value in
360  * the range of [0.0 .. 1.0] indicating the progress made so far in
361  * the upgrade process.  The argument 'closure' is passed verbatim to
362  * any callback invoked.
363  */
364 notmuch_status_t
365 notmuch_database_upgrade (notmuch_database_t *database,
366                           void (*progress_notify) (void *closure,
367                                                    double progress),
368                           void *closure);
369
370 /**
371  * Begin an atomic database operation.
372  *
373  * Any modifications performed between a successful begin and a
374  * notmuch_database_end_atomic will be applied to the database
375  * atomically.  Note that, unlike a typical database transaction, this
376  * only ensures atomicity, not durability; neither begin nor end
377  * necessarily flush modifications to disk.
378  *
379  * Atomic sections may be nested.  begin_atomic and end_atomic must
380  * always be called in pairs.
381  *
382  * Return value:
383  *
384  * NOTMUCH_STATUS_SUCCESS: Successfully entered atomic section.
385  *
386  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
387  *      atomic section not entered.
388  */
389 notmuch_status_t
390 notmuch_database_begin_atomic (notmuch_database_t *notmuch);
391
392 /**
393  * Indicate the end of an atomic database operation.
394  *
395  * Return value:
396  *
397  * NOTMUCH_STATUS_SUCCESS: Successfully completed atomic section.
398  *
399  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
400  *      atomic section not ended.
401  *
402  * NOTMUCH_STATUS_UNBALANCED_ATOMIC: The database is not currently in
403  *      an atomic section.
404  */
405 notmuch_status_t
406 notmuch_database_end_atomic (notmuch_database_t *notmuch);
407
408 /**
409  * Retrieve a directory object from the database for 'path'.
410  *
411  * Here, 'path' should be a path relative to the path of 'database'
412  * (see notmuch_database_get_path), or else should be an absolute path
413  * with initial components that match the path of 'database'.
414  *
415  * If this directory object does not exist in the database, this
416  * returns NOTMUCH_STATUS_SUCCESS and sets *directory to NULL.
417  *
418  * Otherwise the returned directory object is owned by the database
419  * and as such, will only be valid until notmuch_database_destroy is
420  * called.
421  *
422  * Return value:
423  *
424  * NOTMUCH_STATUS_SUCCESS: Successfully retrieved directory.
425  *
426  * NOTMUCH_STATUS_NULL_POINTER: The given 'directory' argument is NULL.
427  *
428  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
429  *      directory not retrieved.
430  */
431 notmuch_status_t
432 notmuch_database_get_directory (notmuch_database_t *database,
433                                 const char *path,
434                                 notmuch_directory_t **directory);
435
436 /**
437  * Add a new message to the given notmuch database or associate an
438  * additional filename with an existing message.
439  *
440  * Here, 'filename' should be a path relative to the path of
441  * 'database' (see notmuch_database_get_path), or else should be an
442  * absolute filename with initial components that match the path of
443  * 'database'.
444  *
445  * The file should be a single mail message (not a multi-message mbox)
446  * that is expected to remain at its current location, (since the
447  * notmuch database will reference the filename, and will not copy the
448  * entire contents of the file.
449  *
450  * If another message with the same message ID already exists in the
451  * database, rather than creating a new message, this adds 'filename'
452  * to the list of the filenames for the existing message.
453  *
454  * If 'message' is not NULL, then, on successful return
455  * (NOTMUCH_STATUS_SUCCESS or NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) '*message'
456  * will be initialized to a message object that can be used for things
457  * such as adding tags to the just-added message. The user should call
458  * notmuch_message_destroy when done with the message. On any failure
459  * '*message' will be set to NULL.
460  *
461  * Return value:
462  *
463  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
464  *
465  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
466  *      message not added.
467  *
468  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
469  *      ID as another message already in the database. The new
470  *      filename was successfully added to the message in the database
471  *      (if not already present) and the existing message is returned.
472  *
473  * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
474  *      file, (such as permission denied, or file not found,
475  *      etc.). Nothing added to the database.
476  *
477  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
478  *      like an email message. Nothing added to the database.
479  *
480  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
481  *      mode so no message can be added.
482  */
483 notmuch_status_t
484 notmuch_database_add_message (notmuch_database_t *database,
485                               const char *filename,
486                               notmuch_message_t **message);
487
488 /**
489  * Remove a message filename from the given notmuch database. If the
490  * message has no more filenames, remove the message.
491  *
492  * If the same message (as determined by the message ID) is still
493  * available via other filenames, then the message will persist in the
494  * database for those filenames. When the last filename is removed for
495  * a particular message, the database content for that message will be
496  * entirely removed.
497  *
498  * Return value:
499  *
500  * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
501  *      message was removed from the database.
502  *
503  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
504  *      message not removed.
505  *
506  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
507  *      the message persists in the database with at least one other
508  *      filename.
509  *
510  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
511  *      mode so no message can be removed.
512  */
513 notmuch_status_t
514 notmuch_database_remove_message (notmuch_database_t *database,
515                                  const char *filename);
516
517 /**
518  * Find a message with the given message_id.
519  *
520  * If a message with the given message_id is found then, on successful return
521  * (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to a message
522  * object.  The caller should call notmuch_message_destroy when done with the
523  * message.
524  *
525  * On any failure or when the message is not found, this function initializes
526  * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
527  * caller is supposed to check '*message' for NULL to find out whether the
528  * message with the given message_id was found.
529  *
530  * Return value:
531  *
532  * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'.
533  *
534  * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
535  *
536  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating message object
537  *
538  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
539  */
540 notmuch_status_t
541 notmuch_database_find_message (notmuch_database_t *database,
542                                const char *message_id,
543                                notmuch_message_t **message);
544
545 /**
546  * Find a message with the given filename.
547  *
548  * If the database contains a message with the given filename then, on
549  * successful return (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to
550  * a message object. The caller should call notmuch_message_destroy when done
551  * with the message.
552  *
553  * On any failure or when the message is not found, this function initializes
554  * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
555  * caller is supposed to check '*message' for NULL to find out whether the
556  * message with the given filename is found.
557  *
558  * Return value:
559  *
560  * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'
561  *
562  * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
563  *
564  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object
565  *
566  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
567  */
568 notmuch_status_t
569 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
570                                            const char *filename,
571                                            notmuch_message_t **message);
572
573 /**
574  * Return a list of all tags found in the database.
575  *
576  * This function creates a list of all tags found in the database. The
577  * resulting list contains all tags from all messages found in the database.
578  *
579  * On error this function returns NULL.
580  */
581 notmuch_tags_t *
582 notmuch_database_get_all_tags (notmuch_database_t *db);
583
584 /**
585  * Create a new query for 'database'.
586  *
587  * Here, 'database' should be an open database, (see
588  * notmuch_database_open and notmuch_database_create).
589  *
590  * For the query string, we'll document the syntax here more
591  * completely in the future, but it's likely to be a specialized
592  * version of the general Xapian query syntax:
593  *
594  * http://xapian.org/docs/queryparser.html
595  *
596  * As a special case, passing either a length-zero string, (that is ""),
597  * or a string consisting of a single asterisk (that is "*"), will
598  * result in a query that returns all messages in the database.
599  *
600  * See notmuch_query_set_sort for controlling the order of results.
601  * See notmuch_query_search_messages and notmuch_query_search_threads
602  * to actually execute the query.
603  *
604  * User should call notmuch_query_destroy when finished with this
605  * query.
606  *
607  * Will return NULL if insufficient memory is available.
608  */
609 notmuch_query_t *
610 notmuch_query_create (notmuch_database_t *database,
611                       const char *query_string);
612
613 /**
614  * Sort values for notmuch_query_set_sort.
615  */
616 typedef enum {
617     /**
618      * Oldest first.
619      */
620     NOTMUCH_SORT_OLDEST_FIRST,
621     /**
622      * Newest first.
623      */
624     NOTMUCH_SORT_NEWEST_FIRST,
625     /**
626      * Sort by message-id.
627      */
628     NOTMUCH_SORT_MESSAGE_ID,
629     /**
630      * Do not sort.
631      */
632     NOTMUCH_SORT_UNSORTED
633 } notmuch_sort_t;
634
635 /**
636  * Return the query_string of this query. See notmuch_query_create.
637  */
638 const char *
639 notmuch_query_get_query_string (notmuch_query_t *query);
640
641 /**
642  * Exclude values for notmuch_query_set_omit_excluded. The strange
643  * order is to maintain backward compatibility: the old FALSE/TRUE
644  * options correspond to the new
645  * NOTMUCH_EXCLUDE_FLAG/NOTMUCH_EXCLUDE_TRUE options.
646  */
647 typedef enum {
648     NOTMUCH_EXCLUDE_FLAG,
649     NOTMUCH_EXCLUDE_TRUE,
650     NOTMUCH_EXCLUDE_FALSE,
651     NOTMUCH_EXCLUDE_ALL
652 } notmuch_exclude_t;
653
654 /**
655  * Specify whether to omit excluded results or simply flag them.  By
656  * default, this is set to TRUE.
657  *
658  * If set to TRUE or ALL, notmuch_query_search_messages will omit excluded
659  * messages from the results, and notmuch_query_search_threads will omit
660  * threads that match only in excluded messages.  If set to TRUE,
661  * notmuch_query_search_threads will include all messages in threads that
662  * match in at least one non-excluded message.  Otherwise, if set to ALL,
663  * notmuch_query_search_threads will omit excluded messages from all threads.
664  *
665  * If set to FALSE or FLAG then both notmuch_query_search_messages and
666  * notmuch_query_search_threads will return all matching
667  * messages/threads regardless of exclude status. If set to FLAG then
668  * the exclude flag will be set for any excluded message that is
669  * returned by notmuch_query_search_messages, and the thread counts
670  * for threads returned by notmuch_query_search_threads will be the
671  * number of non-excluded messages/matches. Otherwise, if set to
672  * FALSE, then the exclude status is completely ignored.
673  *
674  * The performance difference when calling
675  * notmuch_query_search_messages should be relatively small (and both
676  * should be very fast).  However, in some cases,
677  * notmuch_query_search_threads is very much faster when omitting
678  * excluded messages as it does not need to construct the threads that
679  * only match in excluded messages.
680  */
681 void
682 notmuch_query_set_omit_excluded (notmuch_query_t *query,
683                                  notmuch_exclude_t omit_excluded);
684
685 /**
686  * Specify the sorting desired for this query.
687  */
688 void
689 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
690
691 /**
692  * Return the sort specified for this query. See
693  * notmuch_query_set_sort.
694  */
695 notmuch_sort_t
696 notmuch_query_get_sort (notmuch_query_t *query);
697
698 /**
699  * Add a tag that will be excluded from the query results by default.
700  * This exclusion will be overridden if this tag appears explicitly in
701  * the query.
702  */
703 void
704 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag);
705
706 /**
707  * Execute a query for threads, returning a notmuch_threads_t object
708  * which can be used to iterate over the results. The returned threads
709  * object is owned by the query and as such, will only be valid until
710  * notmuch_query_destroy.
711  *
712  * Typical usage might be:
713  *
714  *     notmuch_query_t *query;
715  *     notmuch_threads_t *threads;
716  *     notmuch_thread_t *thread;
717  *
718  *     query = notmuch_query_create (database, query_string);
719  *
720  *     for (threads = notmuch_query_search_threads (query);
721  *          notmuch_threads_valid (threads);
722  *          notmuch_threads_move_to_next (threads))
723  *     {
724  *         thread = notmuch_threads_get (threads);
725  *         ....
726  *         notmuch_thread_destroy (thread);
727  *     }
728  *
729  *     notmuch_query_destroy (query);
730  *
731  * Note: If you are finished with a thread before its containing
732  * query, you can call notmuch_thread_destroy to clean up some memory
733  * sooner (as in the above example). Otherwise, if your thread objects
734  * are long-lived, then you don't need to call notmuch_thread_destroy
735  * and all the memory will still be reclaimed when the query is
736  * destroyed.
737  *
738  * Note that there's no explicit destructor needed for the
739  * notmuch_threads_t object. (For consistency, we do provide a
740  * notmuch_threads_destroy function, but there's no good reason
741  * to call it if the query is about to be destroyed).
742  *
743  * If a Xapian exception occurs this function will return NULL.
744  */
745 notmuch_threads_t *
746 notmuch_query_search_threads (notmuch_query_t *query);
747
748 /**
749  * Execute a query for messages, returning a notmuch_messages_t object
750  * which can be used to iterate over the results. The returned
751  * messages object is owned by the query and as such, will only be
752  * valid until notmuch_query_destroy.
753  *
754  * Typical usage might be:
755  *
756  *     notmuch_query_t *query;
757  *     notmuch_messages_t *messages;
758  *     notmuch_message_t *message;
759  *
760  *     query = notmuch_query_create (database, query_string);
761  *
762  *     for (messages = notmuch_query_search_messages (query);
763  *          notmuch_messages_valid (messages);
764  *          notmuch_messages_move_to_next (messages))
765  *     {
766  *         message = notmuch_messages_get (messages);
767  *         ....
768  *         notmuch_message_destroy (message);
769  *     }
770  *
771  *     notmuch_query_destroy (query);
772  *
773  * Note: If you are finished with a message before its containing
774  * query, you can call notmuch_message_destroy to clean up some memory
775  * sooner (as in the above example). Otherwise, if your message
776  * objects are long-lived, then you don't need to call
777  * notmuch_message_destroy and all the memory will still be reclaimed
778  * when the query is destroyed.
779  *
780  * Note that there's no explicit destructor needed for the
781  * notmuch_messages_t object. (For consistency, we do provide a
782  * notmuch_messages_destroy function, but there's no good
783  * reason to call it if the query is about to be destroyed).
784  *
785  * If a Xapian exception occurs this function will return NULL.
786  */
787 notmuch_messages_t *
788 notmuch_query_search_messages (notmuch_query_t *query);
789
790 /**
791  * Destroy a notmuch_query_t along with any associated resources.
792  *
793  * This will in turn destroy any notmuch_threads_t and
794  * notmuch_messages_t objects generated by this query, (and in
795  * turn any notmuch_thread_t and notmuch_message_t objects generated
796  * from those results, etc.), if such objects haven't already been
797  * destroyed.
798  */
799 void
800 notmuch_query_destroy (notmuch_query_t *query);
801
802 /**
803  * Is the given 'threads' iterator pointing at a valid thread.
804  *
805  * When this function returns TRUE, notmuch_threads_get will return a
806  * valid object. Whereas when this function returns FALSE,
807  * notmuch_threads_get will return NULL.
808  *
809  * If passed a NULL pointer, this function returns FALSE
810  *
811  * See the documentation of notmuch_query_search_threads for example
812  * code showing how to iterate over a notmuch_threads_t object.
813  */
814 notmuch_bool_t
815 notmuch_threads_valid (notmuch_threads_t *threads);
816
817 /**
818  * Get the current thread from 'threads' as a notmuch_thread_t.
819  *
820  * Note: The returned thread belongs to 'threads' and has a lifetime
821  * identical to it (and the query to which it belongs).
822  *
823  * See the documentation of notmuch_query_search_threads for example
824  * code showing how to iterate over a notmuch_threads_t object.
825  *
826  * If an out-of-memory situation occurs, this function will return
827  * NULL.
828  */
829 notmuch_thread_t *
830 notmuch_threads_get (notmuch_threads_t *threads);
831
832 /**
833  * Move the 'threads' iterator to the next thread.
834  *
835  * If 'threads' is already pointing at the last thread then the
836  * iterator will be moved to a point just beyond that last thread,
837  * (where notmuch_threads_valid will return FALSE and
838  * notmuch_threads_get will return NULL).
839  *
840  * See the documentation of notmuch_query_search_threads for example
841  * code showing how to iterate over a notmuch_threads_t object.
842  */
843 void
844 notmuch_threads_move_to_next (notmuch_threads_t *threads);
845
846 /**
847  * Destroy a notmuch_threads_t object.
848  *
849  * It's not strictly necessary to call this function. All memory from
850  * the notmuch_threads_t object will be reclaimed when the
851  * containing query object is destroyed.
852  */
853 void
854 notmuch_threads_destroy (notmuch_threads_t *threads);
855
856 /**
857  * Return an estimate of the number of messages matching a search.
858  *
859  * This function performs a search and returns Xapian's best
860  * guess as to number of matching messages.
861  *
862  * If a Xapian exception occurs, this function may return 0 (after
863  * printing a message).
864  */
865 unsigned
866 notmuch_query_count_messages (notmuch_query_t *query);
867
868 /**
869  * Return the number of threads matching a search.
870  *
871  * This function performs a search and returns the number of unique thread IDs
872  * in the matching messages. This is the same as number of threads matching a
873  * search.
874  *
875  * Note that this is a significantly heavier operation than
876  * notmuch_query_count_messages().
877  *
878  * If an error occurs, this function may return 0.
879  */
880 unsigned
881 notmuch_query_count_threads (notmuch_query_t *query);
882
883 /**
884  * Get the thread ID of 'thread'.
885  *
886  * The returned string belongs to 'thread' and as such, should not be
887  * modified by the caller and will only be valid for as long as the
888  * thread is valid, (which is until notmuch_thread_destroy or until
889  * the query from which it derived is destroyed).
890  */
891 const char *
892 notmuch_thread_get_thread_id (notmuch_thread_t *thread);
893
894 /**
895  * Get the total number of messages in 'thread'.
896  *
897  * This count consists of all messages in the database belonging to
898  * this thread. Contrast with notmuch_thread_get_matched_messages() .
899  */
900 int
901 notmuch_thread_get_total_messages (notmuch_thread_t *thread);
902
903 /**
904  * Get a notmuch_messages_t iterator for the top-level messages in
905  * 'thread' in oldest-first order.
906  *
907  * This iterator will not necessarily iterate over all of the messages
908  * in the thread. It will only iterate over the messages in the thread
909  * which are not replies to other messages in the thread.
910  *
911  * The returned list will be destroyed when the thread is destroyed.
912  */
913 notmuch_messages_t *
914 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread);
915
916 /**
917  * Get a notmuch_thread_t iterator for all messages in 'thread' in
918  * oldest-first order.
919  *
920  * The returned list will be destroyed when the thread is destroyed.
921  */
922 notmuch_messages_t *
923 notmuch_thread_get_messages (notmuch_thread_t *thread);
924
925 /**
926  * Get the number of messages in 'thread' that matched the search.
927  *
928  * This count includes only the messages in this thread that were
929  * matched by the search from which the thread was created and were
930  * not excluded by any exclude tags passed in with the query (see
931  * notmuch_query_add_tag_exclude). Contrast with
932  * notmuch_thread_get_total_messages() .
933  */
934 int
935 notmuch_thread_get_matched_messages (notmuch_thread_t *thread);
936
937 /**
938  * Get the authors of 'thread' as a UTF-8 string.
939  *
940  * The returned string is a comma-separated list of the names of the
941  * authors of mail messages in the query results that belong to this
942  * thread.
943  *
944  * The returned string belongs to 'thread' and as such, should not be
945  * modified by the caller and will only be valid for as long as the
946  * thread is valid, (which is until notmuch_thread_destroy or until
947  * the query from which it derived is destroyed).
948  */
949 const char *
950 notmuch_thread_get_authors (notmuch_thread_t *thread);
951
952 /**
953  * Get the subject of 'thread' as a UTF-8 string.
954  *
955  * The subject is taken from the first message (according to the query
956  * order---see notmuch_query_set_sort) in the query results that
957  * belongs to this thread.
958  *
959  * The returned string belongs to 'thread' and as such, should not be
960  * modified by the caller and will only be valid for as long as the
961  * thread is valid, (which is until notmuch_thread_destroy or until
962  * the query from which it derived is destroyed).
963  */
964 const char *
965 notmuch_thread_get_subject (notmuch_thread_t *thread);
966
967 /**
968  * Get the date of the oldest message in 'thread' as a time_t value.
969  */
970 time_t
971 notmuch_thread_get_oldest_date (notmuch_thread_t *thread);
972
973 /**
974  * Get the date of the newest message in 'thread' as a time_t value.
975  */
976 time_t
977 notmuch_thread_get_newest_date (notmuch_thread_t *thread);
978
979 /**
980  * Get the tags for 'thread', returning a notmuch_tags_t object which
981  * can be used to iterate over all tags.
982  *
983  * Note: In the Notmuch database, tags are stored on individual
984  * messages, not on threads. So the tags returned here will be all
985  * tags of the messages which matched the search and which belong to
986  * this thread.
987  *
988  * The tags object is owned by the thread and as such, will only be
989  * valid for as long as the thread is valid, (for example, until
990  * notmuch_thread_destroy or until the query from which it derived is
991  * destroyed).
992  *
993  * Typical usage might be:
994  *
995  *     notmuch_thread_t *thread;
996  *     notmuch_tags_t *tags;
997  *     const char *tag;
998  *
999  *     thread = notmuch_threads_get (threads);
1000  *
1001  *     for (tags = notmuch_thread_get_tags (thread);
1002  *          notmuch_tags_valid (tags);
1003  *          notmuch_tags_move_to_next (tags))
1004  *     {
1005  *         tag = notmuch_tags_get (tags);
1006  *         ....
1007  *     }
1008  *
1009  *     notmuch_thread_destroy (thread);
1010  *
1011  * Note that there's no explicit destructor needed for the
1012  * notmuch_tags_t object. (For consistency, we do provide a
1013  * notmuch_tags_destroy function, but there's no good reason to call
1014  * it if the message is about to be destroyed).
1015  */
1016 notmuch_tags_t *
1017 notmuch_thread_get_tags (notmuch_thread_t *thread);
1018
1019 /**
1020  * Destroy a notmuch_thread_t object.
1021  */
1022 void
1023 notmuch_thread_destroy (notmuch_thread_t *thread);
1024
1025 /**
1026  * Is the given 'messages' iterator pointing at a valid message.
1027  *
1028  * When this function returns TRUE, notmuch_messages_get will return a
1029  * valid object. Whereas when this function returns FALSE,
1030  * notmuch_messages_get will return NULL.
1031  *
1032  * See the documentation of notmuch_query_search_messages for example
1033  * code showing how to iterate over a notmuch_messages_t object.
1034  */
1035 notmuch_bool_t
1036 notmuch_messages_valid (notmuch_messages_t *messages);
1037
1038 /**
1039  * Get the current message from 'messages' as a notmuch_message_t.
1040  *
1041  * Note: The returned message belongs to 'messages' and has a lifetime
1042  * identical to it (and the query to which it belongs).
1043  *
1044  * See the documentation of notmuch_query_search_messages for example
1045  * code showing how to iterate over a notmuch_messages_t object.
1046  *
1047  * If an out-of-memory situation occurs, this function will return
1048  * NULL.
1049  */
1050 notmuch_message_t *
1051 notmuch_messages_get (notmuch_messages_t *messages);
1052
1053 /**
1054  * Move the 'messages' iterator to the next message.
1055  *
1056  * If 'messages' is already pointing at the last message then the
1057  * iterator will be moved to a point just beyond that last message,
1058  * (where notmuch_messages_valid will return FALSE and
1059  * notmuch_messages_get will return NULL).
1060  *
1061  * See the documentation of notmuch_query_search_messages for example
1062  * code showing how to iterate over a notmuch_messages_t object.
1063  */
1064 void
1065 notmuch_messages_move_to_next (notmuch_messages_t *messages);
1066
1067 /**
1068  * Destroy a notmuch_messages_t object.
1069  *
1070  * It's not strictly necessary to call this function. All memory from
1071  * the notmuch_messages_t object will be reclaimed when the containing
1072  * query object is destroyed.
1073  */
1074 void
1075 notmuch_messages_destroy (notmuch_messages_t *messages);
1076
1077 /**
1078  * Return a list of tags from all messages.
1079  *
1080  * The resulting list is guaranteed not to contain duplicated tags.
1081  *
1082  * WARNING: You can no longer iterate over messages after calling this
1083  * function, because the iterator will point at the end of the list.
1084  * We do not have a function to reset the iterator yet and the only
1085  * way how you can iterate over the list again is to recreate the
1086  * message list.
1087  *
1088  * The function returns NULL on error.
1089  */
1090 notmuch_tags_t *
1091 notmuch_messages_collect_tags (notmuch_messages_t *messages);
1092
1093 /**
1094  * Get the message ID of 'message'.
1095  *
1096  * The returned string belongs to 'message' and as such, should not be
1097  * modified by the caller and will only be valid for as long as the
1098  * message is valid, (which is until the query from which it derived
1099  * is destroyed).
1100  *
1101  * This function will not return NULL since Notmuch ensures that every
1102  * message has a unique message ID, (Notmuch will generate an ID for a
1103  * message if the original file does not contain one).
1104  */
1105 const char *
1106 notmuch_message_get_message_id (notmuch_message_t *message);
1107
1108 /**
1109  * Get the thread ID of 'message'.
1110  *
1111  * The returned string belongs to 'message' and as such, should not be
1112  * modified by the caller and will only be valid for as long as the
1113  * message is valid, (for example, until the user calls
1114  * notmuch_message_destroy on 'message' or until a query from which it
1115  * derived is destroyed).
1116  *
1117  * This function will not return NULL since Notmuch ensures that every
1118  * message belongs to a single thread.
1119  */
1120 const char *
1121 notmuch_message_get_thread_id (notmuch_message_t *message);
1122
1123 /**
1124  * Get a notmuch_messages_t iterator for all of the replies to
1125  * 'message'.
1126  *
1127  * Note: This call only makes sense if 'message' was ultimately
1128  * obtained from a notmuch_thread_t object, (such as by coming
1129  * directly from the result of calling notmuch_thread_get_
1130  * toplevel_messages or by any number of subsequent
1131  * calls to notmuch_message_get_replies).
1132  *
1133  * If 'message' was obtained through some non-thread means, (such as
1134  * by a call to notmuch_query_search_messages), then this function
1135  * will return NULL.
1136  *
1137  * If there are no replies to 'message', this function will return
1138  * NULL. (Note that notmuch_messages_valid will accept that NULL
1139  * value as legitimate, and simply return FALSE for it.)
1140  */
1141 notmuch_messages_t *
1142 notmuch_message_get_replies (notmuch_message_t *message);
1143
1144 /**
1145  * Get a filename for the email corresponding to 'message'.
1146  *
1147  * The returned filename is an absolute filename, (the initial
1148  * component will match notmuch_database_get_path() ).
1149  *
1150  * The returned string belongs to the message so should not be
1151  * modified or freed by the caller (nor should it be referenced after
1152  * the message is destroyed).
1153  *
1154  * Note: If this message corresponds to multiple files in the mail
1155  * store, (that is, multiple files contain identical message IDs),
1156  * this function will arbitrarily return a single one of those
1157  * filenames. See notmuch_message_get_filenames for returning the
1158  * complete list of filenames.
1159  */
1160 const char *
1161 notmuch_message_get_filename (notmuch_message_t *message);
1162
1163 /**
1164  * Get all filenames for the email corresponding to 'message'.
1165  *
1166  * Returns a notmuch_filenames_t iterator listing all the filenames
1167  * associated with 'message'. These files may not have identical
1168  * content, but each will have the identical Message-ID.
1169  *
1170  * Each filename in the iterator is an absolute filename, (the initial
1171  * component will match notmuch_database_get_path() ).
1172  */
1173 notmuch_filenames_t *
1174 notmuch_message_get_filenames (notmuch_message_t *message);
1175
1176 /**
1177  * Message flags.
1178  */
1179 typedef enum _notmuch_message_flag {
1180     NOTMUCH_MESSAGE_FLAG_MATCH,
1181     NOTMUCH_MESSAGE_FLAG_EXCLUDED
1182 } notmuch_message_flag_t;
1183
1184 /**
1185  * Get a value of a flag for the email corresponding to 'message'.
1186  */
1187 notmuch_bool_t
1188 notmuch_message_get_flag (notmuch_message_t *message,
1189                           notmuch_message_flag_t flag);
1190
1191 /**
1192  * Set a value of a flag for the email corresponding to 'message'.
1193  */
1194 void
1195 notmuch_message_set_flag (notmuch_message_t *message,
1196                           notmuch_message_flag_t flag, notmuch_bool_t value);
1197
1198 /**
1199  * Get the date of 'message' as a time_t value.
1200  *
1201  * For the original textual representation of the Date header from the
1202  * message call notmuch_message_get_header() with a header value of
1203  * "date".
1204  */
1205 time_t
1206 notmuch_message_get_date  (notmuch_message_t *message);
1207
1208 /**
1209  * Get the value of the specified header from 'message' as a UTF-8 string.
1210  *
1211  * Common headers are stored in the database when the message is
1212  * indexed and will be returned from the database.  Other headers will
1213  * be read from the actual message file.
1214  *
1215  * The header name is case insensitive.
1216  *
1217  * The returned string belongs to the message so should not be
1218  * modified or freed by the caller (nor should it be referenced after
1219  * the message is destroyed).
1220  *
1221  * Returns an empty string ("") if the message does not contain a
1222  * header line matching 'header'. Returns NULL if any error occurs.
1223  */
1224 const char *
1225 notmuch_message_get_header (notmuch_message_t *message, const char *header);
1226
1227 /**
1228  * Get the tags for 'message', returning a notmuch_tags_t object which
1229  * can be used to iterate over all tags.
1230  *
1231  * The tags object is owned by the message and as such, will only be
1232  * valid for as long as the message is valid, (which is until the
1233  * query from which it derived is destroyed).
1234  *
1235  * Typical usage might be:
1236  *
1237  *     notmuch_message_t *message;
1238  *     notmuch_tags_t *tags;
1239  *     const char *tag;
1240  *
1241  *     message = notmuch_database_find_message (database, message_id);
1242  *
1243  *     for (tags = notmuch_message_get_tags (message);
1244  *          notmuch_tags_valid (tags);
1245  *          notmuch_tags_move_to_next (tags))
1246  *     {
1247  *         tag = notmuch_tags_get (tags);
1248  *         ....
1249  *     }
1250  *
1251  *     notmuch_message_destroy (message);
1252  *
1253  * Note that there's no explicit destructor needed for the
1254  * notmuch_tags_t object. (For consistency, we do provide a
1255  * notmuch_tags_destroy function, but there's no good reason to call
1256  * it if the message is about to be destroyed).
1257  */
1258 notmuch_tags_t *
1259 notmuch_message_get_tags (notmuch_message_t *message);
1260
1261 /**
1262  * The longest possible tag value.
1263  */
1264 #define NOTMUCH_TAG_MAX 200
1265
1266 /**
1267  * Add a tag to the given message.
1268  *
1269  * Return value:
1270  *
1271  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
1272  *
1273  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1274  *
1275  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1276  *      (exceeds NOTMUCH_TAG_MAX)
1277  *
1278  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1279  *      mode so message cannot be modified.
1280  */
1281 notmuch_status_t
1282 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
1283
1284 /**
1285  * Remove a tag from the given message.
1286  *
1287  * Return value:
1288  *
1289  * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
1290  *
1291  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1292  *
1293  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1294  *      (exceeds NOTMUCH_TAG_MAX)
1295  *
1296  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1297  *      mode so message cannot be modified.
1298  */
1299 notmuch_status_t
1300 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
1301
1302 /**
1303  * Remove all tags from the given message.
1304  *
1305  * See notmuch_message_freeze for an example showing how to safely
1306  * replace tag values.
1307  *
1308  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1309  *      mode so message cannot be modified.
1310  */
1311 notmuch_status_t
1312 notmuch_message_remove_all_tags (notmuch_message_t *message);
1313
1314 /**
1315  * Add/remove tags according to maildir flags in the message filename(s).
1316  *
1317  * This function examines the filenames of 'message' for maildir
1318  * flags, and adds or removes tags on 'message' as follows when these
1319  * flags are present:
1320  *
1321  *      Flag    Action if present
1322  *      ----    -----------------
1323  *      'D'     Adds the "draft" tag to the message
1324  *      'F'     Adds the "flagged" tag to the message
1325  *      'P'     Adds the "passed" tag to the message
1326  *      'R'     Adds the "replied" tag to the message
1327  *      'S'     Removes the "unread" tag from the message
1328  *
1329  * For each flag that is not present, the opposite action (add/remove)
1330  * is performed for the corresponding tags.
1331  *
1332  * Flags are identified as trailing components of the filename after a
1333  * sequence of ":2,".
1334  *
1335  * If there are multiple filenames associated with this message, the
1336  * flag is considered present if it appears in one or more
1337  * filenames. (That is, the flags from the multiple filenames are
1338  * combined with the logical OR operator.)
1339  *
1340  * A client can ensure that notmuch database tags remain synchronized
1341  * with maildir flags by calling this function after each call to
1342  * notmuch_database_add_message. See also
1343  * notmuch_message_tags_to_maildir_flags for synchronizing tag changes
1344  * back to maildir flags.
1345  */
1346 notmuch_status_t
1347 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);
1348
1349 /**
1350  * Rename message filename(s) to encode tags as maildir flags.
1351  *
1352  * Specifically, for each filename corresponding to this message:
1353  *
1354  * If the filename is not in a maildir directory, do nothing.  (A
1355  * maildir directory is determined as a directory named "new" or
1356  * "cur".) Similarly, if the filename has invalid maildir info,
1357  * (repeated or outof-ASCII-order flag characters after ":2,"), then
1358  * do nothing.
1359  *
1360  * If the filename is in a maildir directory, rename the file so that
1361  * its filename ends with the sequence ":2," followed by zero or more
1362  * of the following single-character flags (in ASCII order):
1363  *
1364  *   'D' iff the message has the "draft" tag
1365  *   'F' iff the message has the "flagged" tag
1366  *   'P' iff the message has the "passed" tag
1367  *   'R' iff the message has the "replied" tag
1368  *   'S' iff the message does not have the "unread" tag
1369  *
1370  * Any existing flags unmentioned in the list above will be preserved
1371  * in the renaming.
1372  *
1373  * Also, if this filename is in a directory named "new", rename it to
1374  * be within the neighboring directory named "cur".
1375  *
1376  * A client can ensure that maildir filename flags remain synchronized
1377  * with notmuch database tags by calling this function after changing
1378  * tags, (after calls to notmuch_message_add_tag,
1379  * notmuch_message_remove_tag, or notmuch_message_freeze/
1380  * notmuch_message_thaw). See also notmuch_message_maildir_flags_to_tags
1381  * for synchronizing maildir flag changes back to tags.
1382  */
1383 notmuch_status_t
1384 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message);
1385
1386 /**
1387  * Freeze the current state of 'message' within the database.
1388  *
1389  * This means that changes to the message state, (via
1390  * notmuch_message_add_tag, notmuch_message_remove_tag, and
1391  * notmuch_message_remove_all_tags), will not be committed to the
1392  * database until the message is thawed with notmuch_message_thaw.
1393  *
1394  * Multiple calls to freeze/thaw are valid and these calls will
1395  * "stack". That is there must be as many calls to thaw as to freeze
1396  * before a message is actually thawed.
1397  *
1398  * The ability to do freeze/thaw allows for safe transactions to
1399  * change tag values. For example, explicitly setting a message to
1400  * have a given set of tags might look like this:
1401  *
1402  *    notmuch_message_freeze (message);
1403  *
1404  *    notmuch_message_remove_all_tags (message);
1405  *
1406  *    for (i = 0; i < NUM_TAGS; i++)
1407  *        notmuch_message_add_tag (message, tags[i]);
1408  *
1409  *    notmuch_message_thaw (message);
1410  *
1411  * With freeze/thaw used like this, the message in the database is
1412  * guaranteed to have either the full set of original tag values, or
1413  * the full set of new tag values, but nothing in between.
1414  *
1415  * Imagine the example above without freeze/thaw and the operation
1416  * somehow getting interrupted. This could result in the message being
1417  * left with no tags if the interruption happened after
1418  * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
1419  *
1420  * Return value:
1421  *
1422  * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
1423  *
1424  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1425  *      mode so message cannot be modified.
1426  */
1427 notmuch_status_t
1428 notmuch_message_freeze (notmuch_message_t *message);
1429
1430 /**
1431  * Thaw the current 'message', synchronizing any changes that may have
1432  * occurred while 'message' was frozen into the notmuch database.
1433  *
1434  * See notmuch_message_freeze for an example of how to use this
1435  * function to safely provide tag changes.
1436  *
1437  * Multiple calls to freeze/thaw are valid and these calls with
1438  * "stack". That is there must be as many calls to thaw as to freeze
1439  * before a message is actually thawed.
1440  *
1441  * Return value:
1442  *
1443  * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
1444  *      its frozen count has successfully been reduced by 1).
1445  *
1446  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1447  *      an unfrozen message. That is, there have been an unbalanced
1448  *      number of calls to notmuch_message_freeze and
1449  *      notmuch_message_thaw.
1450  */
1451 notmuch_status_t
1452 notmuch_message_thaw (notmuch_message_t *message);
1453
1454 /**
1455  * Destroy a notmuch_message_t object.
1456  *
1457  * It can be useful to call this function in the case of a single
1458  * query object with many messages in the result, (such as iterating
1459  * over the entire database). Otherwise, it's fine to never call this
1460  * function and there will still be no memory leaks. (The memory from
1461  * the messages get reclaimed when the containing query is destroyed.)
1462  */
1463 void
1464 notmuch_message_destroy (notmuch_message_t *message);
1465
1466 /**
1467  * Is the given 'tags' iterator pointing at a valid tag.
1468  *
1469  * When this function returns TRUE, notmuch_tags_get will return a
1470  * valid string. Whereas when this function returns FALSE,
1471  * notmuch_tags_get will return NULL.
1472  *
1473  * See the documentation of notmuch_message_get_tags for example code
1474  * showing how to iterate over a notmuch_tags_t object.
1475  */
1476 notmuch_bool_t
1477 notmuch_tags_valid (notmuch_tags_t *tags);
1478
1479 /**
1480  * Get the current tag from 'tags' as a string.
1481  *
1482  * Note: The returned string belongs to 'tags' and has a lifetime
1483  * identical to it (and the query to which it ultimately belongs).
1484  *
1485  * See the documentation of notmuch_message_get_tags for example code
1486  * showing how to iterate over a notmuch_tags_t object.
1487  */
1488 const char *
1489 notmuch_tags_get (notmuch_tags_t *tags);
1490
1491 /**
1492  * Move the 'tags' iterator to the next tag.
1493  *
1494  * If 'tags' is already pointing at the last tag then the iterator
1495  * will be moved to a point just beyond that last tag, (where
1496  * notmuch_tags_valid will return FALSE and notmuch_tags_get will
1497  * return NULL).
1498  *
1499  * See the documentation of notmuch_message_get_tags for example code
1500  * showing how to iterate over a notmuch_tags_t object.
1501  */
1502 void
1503 notmuch_tags_move_to_next (notmuch_tags_t *tags);
1504
1505 /**
1506  * Destroy a notmuch_tags_t object.
1507  *
1508  * It's not strictly necessary to call this function. All memory from
1509  * the notmuch_tags_t object will be reclaimed when the containing
1510  * message or query objects are destroyed.
1511  */
1512 void
1513 notmuch_tags_destroy (notmuch_tags_t *tags);
1514
1515 /**
1516  * Store an mtime within the database for 'directory'.
1517  *
1518  * The 'directory' should be an object retrieved from the database
1519  * with notmuch_database_get_directory for a particular path.
1520  *
1521  * The intention is for the caller to use the mtime to allow efficient
1522  * identification of new messages to be added to the database. The
1523  * recommended usage is as follows:
1524  *
1525  *   o Read the mtime of a directory from the filesystem
1526  *
1527  *   o Call add_message for all mail files in the directory
1528  *
1529  *   o Call notmuch_directory_set_mtime with the mtime read from the
1530  *     filesystem.
1531  *
1532  * Then, when wanting to check for updates to the directory in the
1533  * future, the client can call notmuch_directory_get_mtime and know
1534  * that it only needs to add files if the mtime of the directory and
1535  * files are newer than the stored timestamp.
1536  *
1537  * Note: The notmuch_directory_get_mtime function does not allow the
1538  * caller to distinguish a timestamp of 0 from a non-existent
1539  * timestamp. So don't store a timestamp of 0 unless you are
1540  * comfortable with that.
1541  *
1542  * Return value:
1543  *
1544  * NOTMUCH_STATUS_SUCCESS: mtime successfully stored in database.
1545  *
1546  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
1547  *      occurred, mtime not stored.
1548  *
1549  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1550  *      mode so directory mtime cannot be modified.
1551  */
1552 notmuch_status_t
1553 notmuch_directory_set_mtime (notmuch_directory_t *directory,
1554                              time_t mtime);
1555
1556 /**
1557  * Get the mtime of a directory, (as previously stored with
1558  * notmuch_directory_set_mtime).
1559  *
1560  * Returns 0 if no mtime has previously been stored for this
1561  * directory.
1562  */
1563 time_t
1564 notmuch_directory_get_mtime (notmuch_directory_t *directory);
1565
1566 /**
1567  * Get a notmuch_filenames_t iterator listing all the filenames of
1568  * messages in the database within the given directory.
1569  *
1570  * The returned filenames will be the basename-entries only (not
1571  * complete paths).
1572  */
1573 notmuch_filenames_t *
1574 notmuch_directory_get_child_files (notmuch_directory_t *directory);
1575
1576 /**
1577  * Get a notmuch_filenams_t iterator listing all the filenames of
1578  * sub-directories in the database within the given directory.
1579  *
1580  * The returned filenames will be the basename-entries only (not
1581  * complete paths).
1582  */
1583 notmuch_filenames_t *
1584 notmuch_directory_get_child_directories (notmuch_directory_t *directory);
1585
1586 /**
1587  * Destroy a notmuch_directory_t object.
1588  */
1589 void
1590 notmuch_directory_destroy (notmuch_directory_t *directory);
1591
1592 /**
1593  * Is the given 'filenames' iterator pointing at a valid filename.
1594  *
1595  * When this function returns TRUE, notmuch_filenames_get will return
1596  * a valid string. Whereas when this function returns FALSE,
1597  * notmuch_filenames_get will return NULL.
1598  *
1599  * It is acceptable to pass NULL for 'filenames', in which case this
1600  * function will always return FALSE.
1601  */
1602 notmuch_bool_t
1603 notmuch_filenames_valid (notmuch_filenames_t *filenames);
1604
1605 /**
1606  * Get the current filename from 'filenames' as a string.
1607  *
1608  * Note: The returned string belongs to 'filenames' and has a lifetime
1609  * identical to it (and the directory to which it ultimately belongs).
1610  *
1611  * It is acceptable to pass NULL for 'filenames', in which case this
1612  * function will always return NULL.
1613  */
1614 const char *
1615 notmuch_filenames_get (notmuch_filenames_t *filenames);
1616
1617 /**
1618  * Move the 'filenames' iterator to the next filename.
1619  *
1620  * If 'filenames' is already pointing at the last filename then the
1621  * iterator will be moved to a point just beyond that last filename,
1622  * (where notmuch_filenames_valid will return FALSE and
1623  * notmuch_filenames_get will return NULL).
1624  *
1625  * It is acceptable to pass NULL for 'filenames', in which case this
1626  * function will do nothing.
1627  */
1628 void
1629 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);
1630
1631 /**
1632  * Destroy a notmuch_filenames_t object.
1633  *
1634  * It's not strictly necessary to call this function. All memory from
1635  * the notmuch_filenames_t object will be reclaimed when the
1636  * containing directory object is destroyed.
1637  *
1638  * It is acceptable to pass NULL for 'filenames', in which case this
1639  * function will do nothing.
1640  */
1641 void
1642 notmuch_filenames_destroy (notmuch_filenames_t *filenames);
1643
1644 /* @} */
1645
1646 NOTMUCH_END_DECLS
1647
1648 #endif