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