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