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