1 // Wrapper around the notmuch library
6 #cgo LDFLAGS: -lnotmuch
16 // Status codes used for the return values of most functions
17 type Status C.notmuch_status_t
20 STATUS_SUCCESS Status = iota
22 STATUS_READ_ONLY_DATABASE
23 STATUS_XAPIAN_EXCEPTION
26 STATUS_DUPLICATE_MESSAGE_ID
29 STATUS_UNBALANCED_FREEZE_THAW
30 STATUS_UNBALANCED_ATOMIC
35 func (self Status) String() string {
39 p = C.notmuch_status_to_string(C.notmuch_status_t(self))
47 /* Various opaque data types. For each notmuch_<foo>_t see the various
48 * notmuch_<foo> functions below. */
50 type Database struct {
51 db *C.notmuch_database_t
55 query *C.notmuch_query_t
59 threads *C.notmuch_threads_t
63 thread *C.notmuch_thread_t
66 type Messages struct {
67 messages *C.notmuch_messages_t
71 message *C.notmuch_message_t
75 tags *C.notmuch_tags_t
78 type Directory struct {
79 dir *C.notmuch_directory_t
82 type Filenames struct {
83 fnames *C.notmuch_filenames_t
86 type DatabaseMode C.notmuch_database_mode_t
89 DATABASE_MODE_READ_ONLY DatabaseMode = 0
90 DATABASE_MODE_READ_WRITE
93 // Create a new, empty notmuch database located at 'path'
94 func NewDatabase(path string) (*Database, Status) {
96 var c_path *C.char = C.CString(path)
97 defer C.free(unsafe.Pointer(c_path))
100 return nil, STATUS_OUT_OF_MEMORY
103 self := &Database{db: nil}
104 st := Status(C.notmuch_database_create(c_path, &self.db))
105 if st != STATUS_SUCCESS {
111 /* Open an existing notmuch database located at 'path'.
113 * The database should have been created at some time in the past,
114 * (not necessarily by this process), by calling
115 * notmuch_database_create with 'path'. By default the database should be
116 * opened for reading only. In order to write to the database you need to
117 * pass the NOTMUCH_DATABASE_MODE_READ_WRITE mode.
119 * An existing notmuch database can be identified by the presence of a
120 * directory named ".notmuch" below 'path'.
122 * The caller should call notmuch_database_destroy when finished with
125 * In case of any failure, this function returns NULL, (after printing
126 * an error message on stderr).
128 func OpenDatabase(path string, mode DatabaseMode) (*Database, Status) {
130 var c_path *C.char = C.CString(path)
131 defer C.free(unsafe.Pointer(c_path))
134 return nil, STATUS_OUT_OF_MEMORY
137 self := &Database{db: nil}
138 st := Status(C.notmuch_database_open(c_path, C.notmuch_database_mode_t(mode), &self.db))
139 if st != STATUS_SUCCESS {
145 /* Close the given notmuch database, freeing all associated
146 * resources. See notmuch_database_open. */
147 func (self *Database) Close() {
148 C.notmuch_database_destroy(self.db)
151 /* Return the database path of the given database.
153 func (self *Database) GetPath() string {
155 /* The return value is a string owned by notmuch so should not be
156 * modified nor freed by the caller. */
157 var p *C.char = C.notmuch_database_get_path(self.db)
165 /* Return the database format version of the given database. */
166 func (self *Database) GetVersion() uint {
167 return uint(C.notmuch_database_get_version(self.db))
170 /* Does this database need to be upgraded before writing to it?
172 * If this function returns TRUE then no functions that modify the
173 * database (notmuch_database_add_message, notmuch_message_add_tag,
174 * notmuch_directory_set_mtime, etc.) will work unless the function
175 * notmuch_database_upgrade is called successfully first. */
176 func (self *Database) NeedsUpgrade() bool {
177 do_upgrade := C.notmuch_database_needs_upgrade(self.db)
184 // TODO: notmuch_database_upgrade
186 /* Retrieve a directory object from the database for 'path'.
188 * Here, 'path' should be a path relative to the path of 'database'
189 * (see notmuch_database_get_path), or else should be an absolute path
190 * with initial components that match the path of 'database'.
192 * Can return NULL if a Xapian exception occurs.
194 func (self *Database) GetDirectory(path string) (*Directory, Status) {
195 var c_path *C.char = C.CString(path)
196 defer C.free(unsafe.Pointer(c_path))
199 return nil, STATUS_OUT_OF_MEMORY
202 var c_dir *C.notmuch_directory_t
203 st := Status(C.notmuch_database_get_directory(self.db, c_path, &c_dir))
204 if st != STATUS_SUCCESS || c_dir == nil {
207 return &Directory{dir: c_dir}, st
210 /* Add a new message to the given notmuch database.
212 * Here,'filename' should be a path relative to the path of
213 * 'database' (see notmuch_database_get_path), or else should be an
214 * absolute filename with initial components that match the path of
217 * The file should be a single mail message (not a multi-message mbox)
218 * that is expected to remain at its current location, (since the
219 * notmuch database will reference the filename, and will not copy the
220 * entire contents of the file.
222 * If 'message' is not NULL, then, on successful return '*message'
223 * will be initialized to a message object that can be used for things
224 * such as adding tags to the just-added message. The user should call
225 * notmuch_message_destroy when done with the message. On any failure
226 * '*message' will be set to NULL.
230 * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
232 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
235 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
236 * ID as another message already in the database. The new
237 * filename was successfully added to the message in the database
238 * (if not already present).
240 * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
241 * file, (such as permission denied, or file not found,
242 * etc.). Nothing added to the database.
244 * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
245 * like an email message. Nothing added to the database.
247 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
248 * mode so no message can be added.
250 func (self *Database) AddMessage(fname string) (*Message, Status) {
251 var c_fname *C.char = C.CString(fname)
252 defer C.free(unsafe.Pointer(c_fname))
255 return nil, STATUS_OUT_OF_MEMORY
258 var c_msg *C.notmuch_message_t = new(C.notmuch_message_t)
259 st := Status(C.notmuch_database_add_message(self.db, c_fname, &c_msg))
261 return &Message{message: c_msg}, st
264 /* Remove a message from the given notmuch database.
266 * Note that only this particular filename association is removed from
267 * the database. If the same message (as determined by the message ID)
268 * is still available via other filenames, then the message will
269 * persist in the database for those filenames. When the last filename
270 * is removed for a particular message, the database content for that
271 * message will be entirely removed.
275 * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
276 * message was removed from the database.
278 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
279 * message not removed.
281 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
282 * the message persists in the database with at least one other
285 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
286 * mode so no message can be removed.
288 func (self *Database) RemoveMessage(fname string) Status {
290 var c_fname *C.char = C.CString(fname)
291 defer C.free(unsafe.Pointer(c_fname))
294 return STATUS_OUT_OF_MEMORY
297 st := C.notmuch_database_remove_message(self.db, c_fname)
301 /* Find a message with the given message_id.
303 * If the database contains a message with the given message_id, then
304 * a new notmuch_message_t object is returned. The caller should call
305 * notmuch_message_destroy when done with the message.
307 * This function returns NULL in the following situations:
309 * * No message is found with the given message_id
310 * * An out-of-memory situation occurs
311 * * A Xapian exception occurs
313 func (self *Database) FindMessage(message_id string) (*Message, Status) {
315 var c_msg_id *C.char = C.CString(message_id)
316 defer C.free(unsafe.Pointer(c_msg_id))
319 return nil, STATUS_OUT_OF_MEMORY
322 msg := &Message{message: nil}
323 st := Status(C.notmuch_database_find_message(self.db, c_msg_id, &msg.message))
324 if st != STATUS_SUCCESS {
330 /* Return a list of all tags found in the database.
332 * This function creates a list of all tags found in the database. The
333 * resulting list contains all tags from all messages found in the database.
335 * On error this function returns NULL.
337 func (self *Database) GetAllTags() *Tags {
338 tags := C.notmuch_database_get_all_tags(self.db)
342 return &Tags{tags: tags}
345 /* Create a new query for 'database'.
347 * Here, 'database' should be an open database, (see
348 * notmuch_database_open and notmuch_database_create).
350 * For the query string, we'll document the syntax here more
351 * completely in the future, but it's likely to be a specialized
352 * version of the general Xapian query syntax:
354 * http://xapian.org/docs/queryparser.html
356 * As a special case, passing either a length-zero string, (that is ""),
357 * or a string consisting of a single asterisk (that is "*"), will
358 * result in a query that returns all messages in the database.
360 * See notmuch_query_set_sort for controlling the order of results.
361 * See notmuch_query_search_messages and notmuch_query_search_threads
362 * to actually execute the query.
364 * User should call notmuch_query_destroy when finished with this
367 * Will return NULL if insufficient memory is available.
369 func (self *Database) CreateQuery(query string) *Query {
371 var c_query *C.char = C.CString(query)
372 defer C.free(unsafe.Pointer(c_query))
378 q := C.notmuch_query_create(self.db, c_query)
382 return &Query{query: q}
385 /* Sort values for notmuch_query_set_sort */
386 type Sort C.notmuch_sort_t
389 SORT_OLDEST_FIRST Sort = 0
395 /* Return the query_string of this query. See notmuch_query_create. */
396 func (self *Query) String() string {
397 // FIXME: do we own 'q' or not ?
398 q := C.notmuch_query_get_query_string(self.query)
399 //defer C.free(unsafe.Pointer(q))
409 /* Specify the sorting desired for this query. */
410 func (self *Query) SetSort(sort Sort) {
411 C.notmuch_query_set_sort(self.query, C.notmuch_sort_t(sort))
414 /* Return the sort specified for this query. See notmuch_query_set_sort. */
415 func (self *Query) GetSort() Sort {
416 return Sort(C.notmuch_query_get_sort(self.query))
419 /* Execute a query for threads, returning a notmuch_threads_t object
420 * which can be used to iterate over the results. The returned threads
421 * object is owned by the query and as such, will only be valid until
422 * notmuch_query_destroy.
424 * Typical usage might be:
426 * notmuch_query_t *query;
427 * notmuch_threads_t *threads;
428 * notmuch_thread_t *thread;
430 * query = notmuch_query_create (database, query_string);
432 * for (threads = notmuch_query_search_threads (query);
433 * notmuch_threads_valid (threads);
434 * notmuch_threads_move_to_next (threads))
436 * thread = notmuch_threads_get (threads);
438 * notmuch_thread_destroy (thread);
441 * notmuch_query_destroy (query);
443 * Note: If you are finished with a thread before its containing
444 * query, you can call notmuch_thread_destroy to clean up some memory
445 * sooner (as in the above example). Otherwise, if your thread objects
446 * are long-lived, then you don't need to call notmuch_thread_destroy
447 * and all the memory will still be reclaimed when the query is
450 * Note that there's no explicit destructor needed for the
451 * notmuch_threads_t object. (For consistency, we do provide a
452 * notmuch_threads_destroy function, but there's no good reason
453 * to call it if the query is about to be destroyed).
455 * If a Xapian exception occurs this function will return NULL.
457 func (self *Query) SearchThreads() *Threads {
458 threads := C.notmuch_query_search_threads(self.query)
462 return &Threads{threads: threads}
465 /* Execute a query for messages, returning a notmuch_messages_t object
466 * which can be used to iterate over the results. The returned
467 * messages object is owned by the query and as such, will only be
468 * valid until notmuch_query_destroy.
470 * Typical usage might be:
472 * notmuch_query_t *query;
473 * notmuch_messages_t *messages;
474 * notmuch_message_t *message;
476 * query = notmuch_query_create (database, query_string);
478 * for (messages = notmuch_query_search_messages (query);
479 * notmuch_messages_valid (messages);
480 * notmuch_messages_move_to_next (messages))
482 * message = notmuch_messages_get (messages);
484 * notmuch_message_destroy (message);
487 * notmuch_query_destroy (query);
489 * Note: If you are finished with a message before its containing
490 * query, you can call notmuch_message_destroy to clean up some memory
491 * sooner (as in the above example). Otherwise, if your message
492 * objects are long-lived, then you don't need to call
493 * notmuch_message_destroy and all the memory will still be reclaimed
494 * when the query is destroyed.
496 * Note that there's no explicit destructor needed for the
497 * notmuch_messages_t object. (For consistency, we do provide a
498 * notmuch_messages_destroy function, but there's no good
499 * reason to call it if the query is about to be destroyed).
501 * If a Xapian exception occurs this function will return NULL.
503 func (self *Query) SearchMessages() *Messages {
504 msgs := C.notmuch_query_search_messages(self.query)
508 return &Messages{messages: msgs}
511 /* Destroy a notmuch_query_t along with any associated resources.
513 * This will in turn destroy any notmuch_threads_t and
514 * notmuch_messages_t objects generated by this query, (and in
515 * turn any notmuch_thread_t and notmuch_message_t objects generated
516 * from those results, etc.), if such objects haven't already been
519 func (self *Query) Destroy() {
520 if self.query != nil {
521 C.notmuch_query_destroy(self.query)
525 /* Return an estimate of the number of messages matching a search
527 * This function performs a search and returns Xapian's best
528 * guess as to number of matching messages.
530 * If a Xapian exception occurs, this function may return 0 (after
531 * printing a message).
533 func (self *Query) CountMessages() uint {
534 return uint(C.notmuch_query_count_messages(self.query))
537 // TODO: wrap threads and thread
539 /* Is the given 'threads' iterator pointing at a valid thread.
541 * When this function returns TRUE, notmuch_threads_get will return a
542 * valid object. Whereas when this function returns FALSE,
543 * notmuch_threads_get will return NULL.
545 * See the documentation of notmuch_query_search_threads for example
546 * code showing how to iterate over a notmuch_threads_t object.
548 func (self *Threads) Valid() bool {
549 if self.threads == nil {
552 valid := C.notmuch_threads_valid(self.threads)
559 /* Destroy a notmuch_threads_t object.
561 * It's not strictly necessary to call this function. All memory from
562 * the notmuch_threads_t object will be reclaimed when the
563 * containg query object is destroyed.
565 func (self *Threads) Destroy() {
566 if self.threads != nil {
567 C.notmuch_threads_destroy(self.threads)
571 /* Is the given 'messages' iterator pointing at a valid message.
573 * When this function returns TRUE, notmuch_messages_get will return a
574 * valid object. Whereas when this function returns FALSE,
575 * notmuch_messages_get will return NULL.
577 * See the documentation of notmuch_query_search_messages for example
578 * code showing how to iterate over a notmuch_messages_t object.
580 func (self *Messages) Valid() bool {
581 if self.messages == nil {
584 valid := C.notmuch_messages_valid(self.messages)
591 /* Get the current message from 'messages' as a notmuch_message_t.
593 * Note: The returned message belongs to 'messages' and has a lifetime
594 * identical to it (and the query to which it belongs).
596 * See the documentation of notmuch_query_search_messages for example
597 * code showing how to iterate over a notmuch_messages_t object.
599 * If an out-of-memory situation occurs, this function will return
602 func (self *Messages) Get() *Message {
603 if self.messages == nil {
606 msg := C.notmuch_messages_get(self.messages)
610 return &Message{message: msg}
613 /* Move the 'messages' iterator to the next message.
615 * If 'messages' is already pointing at the last message then the
616 * iterator will be moved to a point just beyond that last message,
617 * (where notmuch_messages_valid will return FALSE and
618 * notmuch_messages_get will return NULL).
620 * See the documentation of notmuch_query_search_messages for example
621 * code showing how to iterate over a notmuch_messages_t object.
623 func (self *Messages) MoveToNext() {
624 if self.messages == nil {
627 C.notmuch_messages_move_to_next(self.messages)
630 /* Destroy a notmuch_messages_t object.
632 * It's not strictly necessary to call this function. All memory from
633 * the notmuch_messages_t object will be reclaimed when the containing
634 * query object is destroyed.
636 func (self *Messages) Destroy() {
637 if self.messages != nil {
638 C.notmuch_messages_destroy(self.messages)
642 /* Return a list of tags from all messages.
644 * The resulting list is guaranteed not to contain duplicated tags.
646 * WARNING: You can no longer iterate over messages after calling this
647 * function, because the iterator will point at the end of the list.
648 * We do not have a function to reset the iterator yet and the only
649 * way how you can iterate over the list again is to recreate the
652 * The function returns NULL on error.
654 func (self *Messages) CollectTags() *Tags {
655 if self.messages == nil {
658 tags := C.notmuch_messages_collect_tags(self.messages)
662 return &Tags{tags: tags}
665 /* Get the message ID of 'message'.
667 * The returned string belongs to 'message' and as such, should not be
668 * modified by the caller and will only be valid for as long as the
669 * message is valid, (which is until the query from which it derived
672 * This function will not return NULL since Notmuch ensures that every
673 * message has a unique message ID, (Notmuch will generate an ID for a
674 * message if the original file does not contain one).
676 func (self *Message) GetMessageId() string {
678 if self.message == nil {
681 id := C.notmuch_message_get_message_id(self.message)
683 // defer C.free(unsafe.Pointer(id))
687 return C.GoString(id)
690 /* Get the thread ID of 'message'.
692 * The returned string belongs to 'message' and as such, should not be
693 * modified by the caller and will only be valid for as long as the
694 * message is valid, (for example, until the user calls
695 * notmuch_message_destroy on 'message' or until a query from which it
696 * derived is destroyed).
698 * This function will not return NULL since Notmuch ensures that every
699 * message belongs to a single thread.
701 func (self *Message) GetThreadId() string {
703 if self.message == nil {
706 id := C.notmuch_message_get_thread_id(self.message)
708 // defer C.free(unsafe.Pointer(id))
714 return C.GoString(id)
717 /* Get a notmuch_messages_t iterator for all of the replies to
720 * Note: This call only makes sense if 'message' was ultimately
721 * obtained from a notmuch_thread_t object, (such as by coming
722 * directly from the result of calling notmuch_thread_get_
723 * toplevel_messages or by any number of subsequent
724 * calls to notmuch_message_get_replies).
726 * If 'message' was obtained through some non-thread means, (such as
727 * by a call to notmuch_query_search_messages), then this function
730 * If there are no replies to 'message', this function will return
731 * NULL. (Note that notmuch_messages_valid will accept that NULL
732 * value as legitimate, and simply return FALSE for it.)
734 func (self *Message) GetReplies() *Messages {
735 if self.message == nil {
738 msgs := C.notmuch_message_get_replies(self.message)
742 return &Messages{messages: msgs}
745 /* Get a filename for the email corresponding to 'message'.
747 * The returned filename is an absolute filename, (the initial
748 * component will match notmuch_database_get_path() ).
750 * The returned string belongs to the message so should not be
751 * modified or freed by the caller (nor should it be referenced after
752 * the message is destroyed).
754 * Note: If this message corresponds to multiple files in the mail
755 * store, (that is, multiple files contain identical message IDs),
756 * this function will arbitrarily return a single one of those
759 func (self *Message) GetFileName() string {
760 if self.message == nil {
763 fname := C.notmuch_message_get_filename(self.message)
765 // defer C.free(unsafe.Pointer(fname))
771 return C.GoString(fname)
774 type Flag C.notmuch_message_flag_t
777 MESSAGE_FLAG_MATCH Flag = 0
780 /* Get a value of a flag for the email corresponding to 'message'. */
781 func (self *Message) GetFlag(flag Flag) bool {
782 if self.message == nil {
785 v := C.notmuch_message_get_flag(self.message, C.notmuch_message_flag_t(flag))
792 /* Set a value of a flag for the email corresponding to 'message'. */
793 func (self *Message) SetFlag(flag Flag, value bool) {
794 if self.message == nil {
797 var v C.notmuch_bool_t = 0
801 C.notmuch_message_set_flag(self.message, C.notmuch_message_flag_t(flag), v)
804 // TODO: wrap notmuch_message_get_date
806 /* Get the value of the specified header from 'message'.
808 * The value will be read from the actual message file, not from the
809 * notmuch database. The header name is case insensitive.
811 * The returned string belongs to the message so should not be
812 * modified or freed by the caller (nor should it be referenced after
813 * the message is destroyed).
815 * Returns an empty string ("") if the message does not contain a
816 * header line matching 'header'. Returns NULL if any error occurs.
818 func (self *Message) GetHeader(header string) string {
819 if self.message == nil {
823 var c_header *C.char = C.CString(header)
824 defer C.free(unsafe.Pointer(c_header))
826 /* we dont own value */
827 value := C.notmuch_message_get_header(self.message, c_header)
832 return C.GoString(value)
835 /* Get the tags for 'message', returning a notmuch_tags_t object which
836 * can be used to iterate over all tags.
838 * The tags object is owned by the message and as such, will only be
839 * valid for as long as the message is valid, (which is until the
840 * query from which it derived is destroyed).
842 * Typical usage might be:
844 * notmuch_message_t *message;
845 * notmuch_tags_t *tags;
848 * message = notmuch_database_find_message (database, message_id);
850 * for (tags = notmuch_message_get_tags (message);
851 * notmuch_tags_valid (tags);
852 * notmuch_result_move_to_next (tags))
854 * tag = notmuch_tags_get (tags);
858 * notmuch_message_destroy (message);
860 * Note that there's no explicit destructor needed for the
861 * notmuch_tags_t object. (For consistency, we do provide a
862 * notmuch_tags_destroy function, but there's no good reason to call
863 * it if the message is about to be destroyed).
865 func (self *Message) GetTags() *Tags {
866 if self.message == nil {
869 tags := C.notmuch_message_get_tags(self.message)
873 return &Tags{tags: tags}
876 /* The longest possible tag value. */
879 /* Add a tag to the given message.
883 * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
885 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
887 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
888 * (exceeds NOTMUCH_TAG_MAX)
890 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
891 * mode so message cannot be modified.
893 func (self *Message) AddTag(tag string) Status {
894 if self.message == nil {
895 return STATUS_NULL_POINTER
897 c_tag := C.CString(tag)
898 defer C.free(unsafe.Pointer(c_tag))
900 return Status(C.notmuch_message_add_tag(self.message, c_tag))
903 /* Remove a tag from the given message.
907 * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
909 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
911 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
912 * (exceeds NOTMUCH_TAG_MAX)
914 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
915 * mode so message cannot be modified.
917 func (self *Message) RemoveTag(tag string) Status {
918 if self.message == nil {
919 return STATUS_NULL_POINTER
921 c_tag := C.CString(tag)
922 defer C.free(unsafe.Pointer(c_tag))
924 return Status(C.notmuch_message_remove_tag(self.message, c_tag))
927 /* Remove all tags from the given message.
929 * See notmuch_message_freeze for an example showing how to safely
930 * replace tag values.
932 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
933 * mode so message cannot be modified.
935 func (self *Message) RemoveAllTags() Status {
936 if self.message == nil {
937 return STATUS_NULL_POINTER
939 return Status(C.notmuch_message_remove_all_tags(self.message))
942 /* Freeze the current state of 'message' within the database.
944 * This means that changes to the message state, (via
945 * notmuch_message_add_tag, notmuch_message_remove_tag, and
946 * notmuch_message_remove_all_tags), will not be committed to the
947 * database until the message is thawed with notmuch_message_thaw.
949 * Multiple calls to freeze/thaw are valid and these calls will
950 * "stack". That is there must be as many calls to thaw as to freeze
951 * before a message is actually thawed.
953 * The ability to do freeze/thaw allows for safe transactions to
954 * change tag values. For example, explicitly setting a message to
955 * have a given set of tags might look like this:
957 * notmuch_message_freeze (message);
959 * notmuch_message_remove_all_tags (message);
961 * for (i = 0; i < NUM_TAGS; i++)
962 * notmuch_message_add_tag (message, tags[i]);
964 * notmuch_message_thaw (message);
966 * With freeze/thaw used like this, the message in the database is
967 * guaranteed to have either the full set of original tag values, or
968 * the full set of new tag values, but nothing in between.
970 * Imagine the example above without freeze/thaw and the operation
971 * somehow getting interrupted. This could result in the message being
972 * left with no tags if the interruption happened after
973 * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
977 * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
979 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
980 * mode so message cannot be modified.
982 func (self *Message) Freeze() Status {
983 if self.message == nil {
984 return STATUS_NULL_POINTER
986 return Status(C.notmuch_message_freeze(self.message))
989 /* Thaw the current 'message', synchronizing any changes that may have
990 * occurred while 'message' was frozen into the notmuch database.
992 * See notmuch_message_freeze for an example of how to use this
993 * function to safely provide tag changes.
995 * Multiple calls to freeze/thaw are valid and these calls with
996 * "stack". That is there must be as many calls to thaw as to freeze
997 * before a message is actually thawed.
1001 * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
1002 * its frozen count has successfully been reduced by 1).
1004 * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1005 * an unfrozen message. That is, there have been an unbalanced
1006 * number of calls to notmuch_message_freeze and
1007 * notmuch_message_thaw.
1009 func (self *Message) Thaw() Status {
1010 if self.message == nil {
1011 return STATUS_NULL_POINTER
1014 return Status(C.notmuch_message_thaw(self.message))
1017 /* Destroy a notmuch_message_t object.
1019 * It can be useful to call this function in the case of a single
1020 * query object with many messages in the result, (such as iterating
1021 * over the entire database). Otherwise, it's fine to never call this
1022 * function and there will still be no memory leaks. (The memory from
1023 * the messages get reclaimed when the containing query is destroyed.)
1025 func (self *Message) Destroy() {
1026 if self.message == nil {
1029 C.notmuch_message_destroy(self.message)
1032 /* Is the given 'tags' iterator pointing at a valid tag.
1034 * When this function returns TRUE, notmuch_tags_get will return a
1035 * valid string. Whereas when this function returns FALSE,
1036 * notmuch_tags_get will return NULL.
1038 * See the documentation of notmuch_message_get_tags for example code
1039 * showing how to iterate over a notmuch_tags_t object.
1041 func (self *Tags) Valid() bool {
1042 if self.tags == nil {
1045 v := C.notmuch_tags_valid(self.tags)
1052 /* Get the current tag from 'tags' as a string.
1054 * Note: The returned string belongs to 'tags' and has a lifetime
1055 * identical to it (and the query to which it ultimately belongs).
1057 * See the documentation of notmuch_message_get_tags for example code
1058 * showing how to iterate over a notmuch_tags_t object.
1060 func (self *Tags) Get() string {
1061 if self.tags == nil {
1064 s := C.notmuch_tags_get(self.tags)
1067 return C.GoString(s)
1069 func (self *Tags) String() string {
1073 /* Move the 'tags' iterator to the next tag.
1075 * If 'tags' is already pointing at the last tag then the iterator
1076 * will be moved to a point just beyond that last tag, (where
1077 * notmuch_tags_valid will return FALSE and notmuch_tags_get will
1080 * See the documentation of notmuch_message_get_tags for example code
1081 * showing how to iterate over a notmuch_tags_t object.
1083 func (self *Tags) MoveToNext() {
1084 if self.tags == nil {
1087 C.notmuch_tags_move_to_next(self.tags)
1090 /* Destroy a notmuch_tags_t object.
1092 * It's not strictly necessary to call this function. All memory from
1093 * the notmuch_tags_t object will be reclaimed when the containing
1094 * message or query objects are destroyed.
1096 func (self *Tags) Destroy() {
1097 if self.tags == nil {
1100 C.notmuch_tags_destroy(self.tags)
1103 // TODO: wrap notmuch_directory_<fct>
1105 /* Destroy a notmuch_directory_t object. */
1106 func (self *Directory) Destroy() {
1107 if self.dir == nil {
1110 C.notmuch_directory_destroy(self.dir)
1113 // TODO: wrap notmuch_filenames_<fct>
1115 /* Destroy a notmuch_filenames_t object.
1117 * It's not strictly necessary to call this function. All memory from
1118 * the notmuch_filenames_t object will be reclaimed when the
1119 * containing directory object is destroyed.
1121 * It is acceptable to pass NULL for 'filenames', in which case this
1122 * function will do nothing.
1124 func (self *Filenames) Destroy() {
1125 if self.fnames == nil {
1128 C.notmuch_filenames_destroy(self.fnames)