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