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