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