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