]> git.notmuchmail.org Git - notmuch/blob - bindings/go/src/notmuch/notmuch.go
go: reorganize the go bindings
[notmuch] / bindings / go / src / notmuch / notmuch.go
1 // Wrapper around the notmuch library
2
3 package notmuch
4
5 /*
6 #include <stdlib.h>
7 #include <string.h>
8 #include <time.h>
9 #include "notmuch.h"
10 */
11 import "C"
12 import "unsafe"
13
14 // Status codes used for the return values of most functions
15 type Status C.notmuch_status_t
16 const (
17         STATUS_SUCCESS Status = iota
18         STATUS_OUT_OF_MEMORY
19     STATUS_READ_ONLY_DATABASE
20     STATUS_XAPIAN_EXCEPTION
21     STATUS_FILE_ERROR
22     STATUS_FILE_NOT_EMAIL
23     STATUS_DUPLICATE_MESSAGE_ID
24     STATUS_NULL_POINTER
25     STATUS_TAG_TOO_LONG
26     STATUS_UNBALANCED_FREEZE_THAW
27     STATUS_UNBALANCED_ATOMIC
28
29     STATUS_LAST_STATUS
30 )
31
32 func (self Status) String() string {
33         var p *C.char
34         
35         // p is read-only
36         p = C.notmuch_status_to_string(C.notmuch_status_t(self))
37         if p != nil {
38                 s := C.GoString(p)
39                 return s
40         }
41         return ""
42 }
43
44 /* Various opaque data types. For each notmuch_<foo>_t see the various
45  * notmuch_<foo> functions below. */
46
47 type Database struct {
48         db *C.notmuch_database_t
49 }
50
51 type Query struct {
52         query *C.notmuch_query_t
53 }
54
55 type Threads struct {
56         threads *C.notmuch_threads_t
57 }
58
59 type Thread struct {
60         thread *C.notmuch_thread_t
61 }
62
63 type Messages struct {
64         messages *C.notmuch_messages_t
65 }
66
67 type Message struct {
68         message *C.notmuch_message_t
69 }
70
71 type Tags struct {
72         tags *C.notmuch_tags_t
73 }
74
75 type Directory struct {
76         dir *C.notmuch_directory_t
77 }
78
79 type Filenames struct {
80         fnames *C.notmuch_filenames_t
81 }
82
83 type DatabaseMode C.notmuch_database_mode_t
84 const (
85     DATABASE_MODE_READ_ONLY DatabaseMode = 0
86     DATABASE_MODE_READ_WRITE
87 )
88
89 // Create a new, empty notmuch database located at 'path'
90 func NewDatabase(path string) (*Database, Status) {
91
92         var c_path *C.char = C.CString(path)
93         defer C.free(unsafe.Pointer(c_path))
94
95         if c_path == nil {
96                 return nil, STATUS_OUT_OF_MEMORY
97         }
98
99         self := &Database{db:nil}
100         st := Status(C.notmuch_database_create(c_path, &self.db))
101         if st != STATUS_SUCCESS {
102                 return nil, st
103         }
104         return self, st
105 }
106
107 /* Open an existing notmuch database located at 'path'.
108  *
109  * The database should have been created at some time in the past,
110  * (not necessarily by this process), by calling
111  * notmuch_database_create with 'path'. By default the database should be
112  * opened for reading only. In order to write to the database you need to
113  * pass the NOTMUCH_DATABASE_MODE_READ_WRITE mode.
114  *
115  * An existing notmuch database can be identified by the presence of a
116  * directory named ".notmuch" below 'path'.
117  *
118  * The caller should call notmuch_database_destroy when finished with
119  * this database.
120  *
121  * In case of any failure, this function returns NULL, (after printing
122  * an error message on stderr).
123  */
124 func OpenDatabase(path string, mode DatabaseMode) (*Database, Status) {
125
126         var c_path *C.char = C.CString(path)
127         defer C.free(unsafe.Pointer(c_path))
128
129         if c_path == nil {
130                 return nil, STATUS_OUT_OF_MEMORY
131         }
132
133         self := &Database{db:nil}
134         st := Status(C.notmuch_database_open(c_path, C.notmuch_database_mode_t(mode), &self.db))
135         if st != STATUS_SUCCESS {
136                 return nil, st
137         }
138         return self, st
139 }
140
141 /* Close the given notmuch database, freeing all associated
142  * resources. See notmuch_database_open. */
143 func (self *Database) Close() {
144         C.notmuch_database_destroy(self.db)
145 }
146
147 /* Return the database path of the given database.
148  */
149 func (self *Database) GetPath() string {
150         
151  /* The return value is a string owned by notmuch so should not be
152   * modified nor freed by the caller. */
153         var p *C.char = C.notmuch_database_get_path(self.db)
154         if p != nil {
155                 s := C.GoString(p)
156                 return s
157         }
158         return ""
159 }
160
161 /* Return the database format version of the given database. */
162 func (self *Database) GetVersion() uint {
163         return uint(C.notmuch_database_get_version(self.db))
164 }
165
166 /* Does this database need to be upgraded before writing to it?
167  *
168  * If this function returns TRUE then no functions that modify the
169  * database (notmuch_database_add_message, notmuch_message_add_tag,
170  * notmuch_directory_set_mtime, etc.) will work unless the function
171  * notmuch_database_upgrade is called successfully first. */
172 func (self *Database) NeedsUpgrade() bool {
173         do_upgrade := C.notmuch_database_needs_upgrade(self.db)
174         if do_upgrade == 0 {
175                 return false
176         }
177         return true
178 }
179
180 // TODO: notmuch_database_upgrade
181
182
183 /* Retrieve a directory object from the database for 'path'.
184  *
185  * Here, 'path' should be a path relative to the path of 'database'
186  * (see notmuch_database_get_path), or else should be an absolute path
187  * with initial components that match the path of 'database'.
188  *
189  * Can return NULL if a Xapian exception occurs.
190  */
191 func (self *Database) GetDirectory(path string) *Directory {
192         var c_path *C.char = C.CString(path)
193         defer C.free(unsafe.Pointer(c_path))
194
195         if c_path == nil {
196                 return nil
197         }
198
199         c_dir := C.notmuch_database_get_directory(self.db, c_path)
200         if c_dir == nil {
201                 return nil
202         }
203         return &Directory{dir:c_dir}
204 }
205
206 /* Add a new message to the given notmuch database.
207  *
208  * Here,'filename' should be a path relative to the path of
209  * 'database' (see notmuch_database_get_path), or else should be an
210  * absolute filename with initial components that match the path of
211  * 'database'.
212  *
213  * The file should be a single mail message (not a multi-message mbox)
214  * that is expected to remain at its current location, (since the
215  * notmuch database will reference the filename, and will not copy the
216  * entire contents of the file.
217  *
218  * If 'message' is not NULL, then, on successful return '*message'
219  * will be initialized to a message object that can be used for things
220  * such as adding tags to the just-added message. The user should call
221  * notmuch_message_destroy when done with the message. On any failure
222  * '*message' will be set to NULL.
223  *
224  * Return value:
225  *
226  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
227  *
228  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
229  *      message not added.
230  *
231  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
232  *      ID as another message already in the database. The new
233  *      filename was successfully added to the message in the database
234  *      (if not already present).
235  *
236  * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
237  *      file, (such as permission denied, or file not found,
238  *      etc.). Nothing added to the database.
239  *
240  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
241  *      like an email message. Nothing added to the database.
242  *
243  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
244  *      mode so no message can be added.
245  */
246 func 
247 (self *Database) AddMessage(fname string) (*Message, Status) {
248         var c_fname *C.char = C.CString(fname)
249         defer C.free(unsafe.Pointer(c_fname))
250
251         if c_fname == nil {
252                 return nil, STATUS_OUT_OF_MEMORY
253         }
254
255         var c_msg *C.notmuch_message_t = new(C.notmuch_message_t)
256         st := Status(C.notmuch_database_add_message(self.db, c_fname, &c_msg))
257
258         return &Message{message:c_msg}, st
259 }
260
261 /* Remove a message from the given notmuch database.
262  *
263  * Note that only this particular filename association is removed from
264  * the database. If the same message (as determined by the message ID)
265  * is still available via other filenames, then the message will
266  * persist in the database for those filenames. When the last filename
267  * is removed for a particular message, the database content for that
268  * message will be entirely removed.
269  *
270  * Return value:
271  *
272  * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
273  *      message was removed from the database.
274  *
275  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
276  *      message not removed.
277  *
278  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
279  *      the message persists in the database with at least one other
280  *      filename.
281  *
282  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
283  *      mode so no message can be removed.
284  */
285 func (self *Database) RemoveMessage(fname string) Status {
286         
287         var c_fname *C.char = C.CString(fname)
288         defer C.free(unsafe.Pointer(c_fname))
289
290         if c_fname == nil {
291                 return STATUS_OUT_OF_MEMORY
292         }
293
294         st := C.notmuch_database_remove_message(self.db, c_fname)
295         return Status(st)
296 }
297
298 /* Find a message with the given message_id.
299  *
300  * If the database contains a message with the given message_id, then
301  * a new notmuch_message_t object is returned. The caller should call
302  * notmuch_message_destroy when done with the message.
303  *
304  * This function returns NULL in the following situations:
305  *
306  *      * No message is found with the given message_id
307  *      * An out-of-memory situation occurs
308  *      * A Xapian exception occurs
309  */
310 func (self *Database) FindMessage(message_id string) (*Message, Status) {
311         
312         var c_msg_id *C.char = C.CString(message_id)
313         defer C.free(unsafe.Pointer(c_msg_id))
314
315         if c_msg_id == nil {
316                 return nil, STATUS_OUT_OF_MEMORY
317         }
318
319         msg := &Message{message:nil}
320         st := Status(C.notmuch_database_find_message(self.db, c_msg_id, &msg.message))
321         if st != STATUS_SUCCESS {
322                 return nil, st
323         }
324         return msg, st
325 }
326
327 /* Return a list of all tags found in the database.
328  *
329  * This function creates a list of all tags found in the database. The
330  * resulting list contains all tags from all messages found in the database.
331  *
332  * On error this function returns NULL.
333  */
334 func (self *Database) GetAllTags() *Tags {
335         tags := C.notmuch_database_get_all_tags(self.db)
336         if tags == nil {
337                 return nil
338         }
339         return &Tags{tags:tags}
340 }
341
342 /* Create a new query for 'database'.
343  *
344  * Here, 'database' should be an open database, (see
345  * notmuch_database_open and notmuch_database_create).
346  *
347  * For the query string, we'll document the syntax here more
348  * completely in the future, but it's likely to be a specialized
349  * version of the general Xapian query syntax:
350  *
351  * http://xapian.org/docs/queryparser.html
352  *
353  * As a special case, passing either a length-zero string, (that is ""),
354  * or a string consisting of a single asterisk (that is "*"), will
355  * result in a query that returns all messages in the database.
356  *
357  * See notmuch_query_set_sort for controlling the order of results.
358  * See notmuch_query_search_messages and notmuch_query_search_threads
359  * to actually execute the query.
360  *
361  * User should call notmuch_query_destroy when finished with this
362  * query.
363  *
364  * Will return NULL if insufficient memory is available.
365  */
366 func (self *Database) CreateQuery(query string) *Query {
367         
368         var c_query *C.char = C.CString(query)
369         defer C.free(unsafe.Pointer(c_query))
370
371         if c_query == nil {
372                 return nil
373         }
374
375         q := C.notmuch_query_create(self.db, c_query)
376         if q == nil {
377                 return nil
378         }
379         return &Query{query:q}
380 }
381
382 /* Sort values for notmuch_query_set_sort */
383 type Sort C.notmuch_sort_t
384 const (
385         SORT_OLDEST_FIRST Sort = 0
386         SORT_NEWEST_FIRST
387         SORT_MESSAGE_ID
388         SORT_UNSORTED
389 )
390
391 /* Return the query_string of this query. See notmuch_query_create. */
392 func (self *Query) String() string {
393         // FIXME: do we own 'q' or not ?
394         q := C.notmuch_query_get_query_string(self.query)
395         //defer C.free(unsafe.Pointer(q))
396         
397         if q != nil {
398                 s := C.GoString(q)
399                 return s
400         }
401
402         return ""
403 }
404
405 /* Specify the sorting desired for this query. */
406 func (self *Query) SetSort(sort Sort) {
407         C.notmuch_query_set_sort(self.query, C.notmuch_sort_t(sort))
408 }
409
410 /* Return the sort specified for this query. See notmuch_query_set_sort. */
411 func (self *Query) GetSort() Sort {
412         return Sort(C.notmuch_query_get_sort(self.query))
413 }
414
415 /* Execute a query for threads, returning a notmuch_threads_t object
416  * which can be used to iterate over the results. The returned threads
417  * object is owned by the query and as such, will only be valid until
418  * notmuch_query_destroy.
419  *
420  * Typical usage might be:
421  *
422  *     notmuch_query_t *query;
423  *     notmuch_threads_t *threads;
424  *     notmuch_thread_t *thread;
425  *
426  *     query = notmuch_query_create (database, query_string);
427  *
428  *     for (threads = notmuch_query_search_threads (query);
429  *          notmuch_threads_valid (threads);
430  *          notmuch_threads_move_to_next (threads))
431  *     {
432  *         thread = notmuch_threads_get (threads);
433  *         ....
434  *         notmuch_thread_destroy (thread);
435  *     }
436  *
437  *     notmuch_query_destroy (query);
438  *
439  * Note: If you are finished with a thread before its containing
440  * query, you can call notmuch_thread_destroy to clean up some memory
441  * sooner (as in the above example). Otherwise, if your thread objects
442  * are long-lived, then you don't need to call notmuch_thread_destroy
443  * and all the memory will still be reclaimed when the query is
444  * destroyed.
445  *
446  * Note that there's no explicit destructor needed for the
447  * notmuch_threads_t object. (For consistency, we do provide a
448  * notmuch_threads_destroy function, but there's no good reason
449  * to call it if the query is about to be destroyed).
450  *
451  * If a Xapian exception occurs this function will return NULL.
452  */
453 func (self *Query) SearchThreads() *Threads {
454         threads := C.notmuch_query_search_threads(self.query)
455         if threads == nil {
456                 return nil
457         }
458         return &Threads{threads:threads}
459 }
460
461 /* Execute a query for messages, returning a notmuch_messages_t object
462  * which can be used to iterate over the results. The returned
463  * messages object is owned by the query and as such, will only be
464  * valid until notmuch_query_destroy.
465  *
466  * Typical usage might be:
467  *
468  *     notmuch_query_t *query;
469  *     notmuch_messages_t *messages;
470  *     notmuch_message_t *message;
471  *
472  *     query = notmuch_query_create (database, query_string);
473  *
474  *     for (messages = notmuch_query_search_messages (query);
475  *          notmuch_messages_valid (messages);
476  *          notmuch_messages_move_to_next (messages))
477  *     {
478  *         message = notmuch_messages_get (messages);
479  *         ....
480  *         notmuch_message_destroy (message);
481  *     }
482  *
483  *     notmuch_query_destroy (query);
484  *
485  * Note: If you are finished with a message before its containing
486  * query, you can call notmuch_message_destroy to clean up some memory
487  * sooner (as in the above example). Otherwise, if your message
488  * objects are long-lived, then you don't need to call
489  * notmuch_message_destroy and all the memory will still be reclaimed
490  * when the query is destroyed.
491  *
492  * Note that there's no explicit destructor needed for the
493  * notmuch_messages_t object. (For consistency, we do provide a
494  * notmuch_messages_destroy function, but there's no good
495  * reason to call it if the query is about to be destroyed).
496  *
497  * If a Xapian exception occurs this function will return NULL.
498  */
499 func (self *Query) SearchMessages() *Messages {
500         msgs := C.notmuch_query_search_messages(self.query)
501         if msgs == nil {
502                 return nil
503         }
504         return &Messages{messages:msgs}
505 }
506
507 /* Destroy a notmuch_query_t along with any associated resources.
508  *
509  * This will in turn destroy any notmuch_threads_t and
510  * notmuch_messages_t objects generated by this query, (and in
511  * turn any notmuch_thread_t and notmuch_message_t objects generated
512  * from those results, etc.), if such objects haven't already been
513  * destroyed.
514  */
515 func (self *Query) Destroy() {
516         if self.query != nil {
517                 C.notmuch_query_destroy(self.query)
518         }
519 }
520
521 /* Return an estimate of the number of messages matching a search
522  *
523  * This function performs a search and returns Xapian's best
524  * guess as to number of matching messages.
525  *
526  * If a Xapian exception occurs, this function may return 0 (after
527  * printing a message).
528  */
529 func (self *Query) CountMessages() uint {
530         return uint(C.notmuch_query_count_messages(self.query))
531 }
532
533 // TODO: wrap threads and thread
534
535 /* Is the given 'threads' iterator pointing at a valid thread.
536  *
537  * When this function returns TRUE, notmuch_threads_get will return a
538  * valid object. Whereas when this function returns FALSE,
539  * notmuch_threads_get will return NULL.
540  *
541  * See the documentation of notmuch_query_search_threads for example
542  * code showing how to iterate over a notmuch_threads_t object.
543  */
544 func (self *Threads) Valid() bool {
545         if self.threads == nil {
546                 return false
547         }
548         valid := C.notmuch_threads_valid(self.threads)
549         if valid == 0 {
550                 return false
551         }
552         return true
553 }
554
555 /* Destroy a notmuch_threads_t object.
556  *
557  * It's not strictly necessary to call this function. All memory from
558  * the notmuch_threads_t object will be reclaimed when the
559  * containg query object is destroyed.
560  */
561 func (self *Threads) Destroy() {
562         if self.threads != nil {
563                 C.notmuch_threads_destroy(self.threads)
564         }
565 }
566
567 /* Is the given 'messages' iterator pointing at a valid message.
568  *
569  * When this function returns TRUE, notmuch_messages_get will return a
570  * valid object. Whereas when this function returns FALSE,
571  * notmuch_messages_get will return NULL.
572  *
573  * See the documentation of notmuch_query_search_messages for example
574  * code showing how to iterate over a notmuch_messages_t object.
575  */
576 func (self *Messages) Valid() bool {
577         if self.messages == nil {
578                 return false
579         }
580         valid := C.notmuch_messages_valid(self.messages)
581         if valid == 0 {
582                 return false
583         }
584         return true
585 }
586
587 /* Get the current message from 'messages' as a notmuch_message_t.
588  *
589  * Note: The returned message belongs to 'messages' and has a lifetime
590  * identical to it (and the query to which it belongs).
591  *
592  * See the documentation of notmuch_query_search_messages for example
593  * code showing how to iterate over a notmuch_messages_t object.
594  *
595  * If an out-of-memory situation occurs, this function will return
596  * NULL.
597  */
598 func (self *Messages) Get() *Message {
599         if self.messages == nil {
600                 return nil
601         }
602         msg := C.notmuch_messages_get(self.messages)
603         if msg == nil {
604                 return nil
605         }
606         return &Message{message:msg}
607 }
608
609 /* Move the 'messages' iterator to the next message.
610  *
611  * If 'messages' is already pointing at the last message then the
612  * iterator will be moved to a point just beyond that last message,
613  * (where notmuch_messages_valid will return FALSE and
614  * notmuch_messages_get will return NULL).
615  *
616  * See the documentation of notmuch_query_search_messages for example
617  * code showing how to iterate over a notmuch_messages_t object.
618  */
619 func (self *Messages) MoveToNext() {
620         if self.messages == nil {
621                 return
622         }
623         C.notmuch_messages_move_to_next(self.messages)
624 }
625
626 /* Destroy a notmuch_messages_t object.
627  *
628  * It's not strictly necessary to call this function. All memory from
629  * the notmuch_messages_t object will be reclaimed when the containing
630  * query object is destroyed.
631  */
632 func (self *Messages) Destroy() {
633         if self.messages != nil {
634                 C.notmuch_messages_destroy(self.messages)
635         }
636 }
637
638 /* Return a list of tags from all messages.
639  *
640  * The resulting list is guaranteed not to contain duplicated tags.
641  *
642  * WARNING: You can no longer iterate over messages after calling this
643  * function, because the iterator will point at the end of the list.
644  * We do not have a function to reset the iterator yet and the only
645  * way how you can iterate over the list again is to recreate the
646  * message list.
647  *
648  * The function returns NULL on error.
649  */
650 func (self *Messages) CollectTags() *Tags {
651         if self.messages == nil {
652                 return nil
653         }
654         tags := C.notmuch_messages_collect_tags(self.messages)
655         if tags == nil {
656                 return nil
657         }
658         return &Tags{tags:tags}
659 }
660
661 /* Get the message ID of 'message'.
662  *
663  * The returned string belongs to 'message' and as such, should not be
664  * modified by the caller and will only be valid for as long as the
665  * message is valid, (which is until the query from which it derived
666  * is destroyed).
667  *
668  * This function will not return NULL since Notmuch ensures that every
669  * message has a unique message ID, (Notmuch will generate an ID for a
670  * message if the original file does not contain one).
671  */
672 func (self *Message) GetMessageId() string {
673
674         if self.message == nil {
675                 return ""
676         }
677         id := C.notmuch_message_get_message_id(self.message)
678         // we dont own id
679         // defer C.free(unsafe.Pointer(id))
680         if id == nil {
681                 return ""
682         }
683         return C.GoString(id)
684 }
685
686 /* Get the thread ID of 'message'.
687  *
688  * The returned string belongs to 'message' and as such, should not be
689  * modified by the caller and will only be valid for as long as the
690  * message is valid, (for example, until the user calls
691  * notmuch_message_destroy on 'message' or until a query from which it
692  * derived is destroyed).
693  *
694  * This function will not return NULL since Notmuch ensures that every
695  * message belongs to a single thread.
696  */
697 func (self *Message) GetThreadId() string {
698         
699         if self.message == nil {
700                 return ""
701         }
702         id := C.notmuch_message_get_thread_id(self.message)
703         // we dont own id
704         // defer C.free(unsafe.Pointer(id))
705         
706         if id == nil {
707                 return ""
708         }
709
710         return C.GoString(id)
711 }
712
713 /* Get a notmuch_messages_t iterator for all of the replies to
714  * 'message'.
715  *
716  * Note: This call only makes sense if 'message' was ultimately
717  * obtained from a notmuch_thread_t object, (such as by coming
718  * directly from the result of calling notmuch_thread_get_
719  * toplevel_messages or by any number of subsequent
720  * calls to notmuch_message_get_replies).
721  *
722  * If 'message' was obtained through some non-thread means, (such as
723  * by a call to notmuch_query_search_messages), then this function
724  * will return NULL.
725  *
726  * If there are no replies to 'message', this function will return
727  * NULL. (Note that notmuch_messages_valid will accept that NULL
728  * value as legitimate, and simply return FALSE for it.)
729  */
730 func (self *Message) GetReplies() *Messages {
731         if self.message == nil {
732                 return nil
733         }
734         msgs := C.notmuch_message_get_replies(self.message)
735         if msgs == nil {
736                 return nil
737         }
738         return &Messages{messages:msgs}
739 }
740
741 /* Get a filename for the email corresponding to 'message'.
742  *
743  * The returned filename is an absolute filename, (the initial
744  * component will match notmuch_database_get_path() ).
745  *
746  * The returned string belongs to the message so should not be
747  * modified or freed by the caller (nor should it be referenced after
748  * the message is destroyed).
749  *
750  * Note: If this message corresponds to multiple files in the mail
751  * store, (that is, multiple files contain identical message IDs),
752  * this function will arbitrarily return a single one of those
753  * filenames.
754  */
755 func (self *Message) GetFileName() string {
756         if self.message == nil {
757                 return ""
758         }
759         fname := C.notmuch_message_get_filename(self.message)
760         // we dont own fname
761         // defer C.free(unsafe.Pointer(fname))
762         
763         if fname == nil {
764                 return ""
765         }
766
767         return C.GoString(fname)
768 }
769
770 type Flag C.notmuch_message_flag_t
771 const (
772         MESSAGE_FLAG_MATCH Flag = 0
773 )
774
775 /* Get a value of a flag for the email corresponding to 'message'. */
776 func (self *Message) GetFlag(flag Flag) bool {
777         if self.message == nil {
778                 return false
779         }
780         v := C.notmuch_message_get_flag(self.message, C.notmuch_message_flag_t(flag))
781         if v == 0 {
782                 return false
783         }
784         return true
785 }
786
787 /* Set a value of a flag for the email corresponding to 'message'. */
788 func (self *Message) SetFlag(flag Flag, value bool) {
789         if self.message == nil {
790                 return
791         }
792         var v C.notmuch_bool_t = 0
793         if value {
794                 v = 1
795         }
796         C.notmuch_message_set_flag(self.message, C.notmuch_message_flag_t(flag), v)
797 }
798
799 // TODO: wrap notmuch_message_get_date
800
801 /* Get the value of the specified header from 'message'.
802  *
803  * The value will be read from the actual message file, not from the
804  * notmuch database. The header name is case insensitive.
805  *
806  * The returned string belongs to the message so should not be
807  * modified or freed by the caller (nor should it be referenced after
808  * the message is destroyed).
809  *
810  * Returns an empty string ("") if the message does not contain a
811  * header line matching 'header'. Returns NULL if any error occurs.
812  */
813 func (self *Message) GetHeader(header string) string {
814         if self.message == nil {
815                 return ""
816         }
817         
818         var c_header *C.char = C.CString(header)
819         defer C.free(unsafe.Pointer(c_header))
820         
821         /* we dont own value */
822         value := C.notmuch_message_get_header(self.message, c_header)
823         if value == nil {
824                 return ""
825         }
826         
827         return C.GoString(value)
828 }
829
830 /* Get the tags for 'message', returning a notmuch_tags_t object which
831  * can be used to iterate over all tags.
832  *
833  * The tags object is owned by the message and as such, will only be
834  * valid for as long as the message is valid, (which is until the
835  * query from which it derived is destroyed).
836  *
837  * Typical usage might be:
838  *
839  *     notmuch_message_t *message;
840  *     notmuch_tags_t *tags;
841  *     const char *tag;
842  *
843  *     message = notmuch_database_find_message (database, message_id);
844  *
845  *     for (tags = notmuch_message_get_tags (message);
846  *          notmuch_tags_valid (tags);
847  *          notmuch_result_move_to_next (tags))
848  *     {
849  *         tag = notmuch_tags_get (tags);
850  *         ....
851  *     }
852  *
853  *     notmuch_message_destroy (message);
854  *
855  * Note that there's no explicit destructor needed for the
856  * notmuch_tags_t object. (For consistency, we do provide a
857  * notmuch_tags_destroy function, but there's no good reason to call
858  * it if the message is about to be destroyed).
859  */
860 func (self *Message) GetTags() *Tags {
861         if self.message == nil {
862                 return nil
863         }
864         tags := C.notmuch_message_get_tags(self.message)
865         if tags == nil {
866                 return nil
867         }
868         return &Tags{tags:tags}
869 }
870
871 /* The longest possible tag value. */
872 const TAG_MAX = 200
873
874 /* Add a tag to the given message.
875  *
876  * Return value:
877  *
878  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
879  *
880  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
881  *
882  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
883  *      (exceeds NOTMUCH_TAG_MAX)
884  *
885  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
886  *      mode so message cannot be modified.
887  */
888 func (self *Message) AddTag(tag string) Status {
889         if self.message == nil {
890                 return STATUS_NULL_POINTER
891         }
892         c_tag := C.CString(tag)
893         defer C.free(unsafe.Pointer(c_tag))
894
895         return Status(C.notmuch_message_add_tag(self.message, c_tag))
896 }
897
898 /* Remove a tag from the given message.
899  *
900  * Return value:
901  *
902  * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
903  *
904  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
905  *
906  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
907  *      (exceeds NOTMUCH_TAG_MAX)
908  *
909  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
910  *      mode so message cannot be modified.
911  */
912 func (self *Message) RemoveTag(tag string) Status {
913         if self.message == nil {
914                 return STATUS_NULL_POINTER
915         }
916         c_tag := C.CString(tag)
917         defer C.free(unsafe.Pointer(c_tag))
918
919         return Status(C.notmuch_message_remove_tag(self.message, c_tag))
920 }
921
922 /* Remove all tags from the given message.
923  *
924  * See notmuch_message_freeze for an example showing how to safely
925  * replace tag values.
926  *
927  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
928  *      mode so message cannot be modified.
929  */
930 func (self *Message) RemoveAllTags() Status {
931         if self.message == nil {
932                 return STATUS_NULL_POINTER
933         }
934         return Status(C.notmuch_message_remove_all_tags(self.message))
935 }
936
937 /* Freeze the current state of 'message' within the database.
938  *
939  * This means that changes to the message state, (via
940  * notmuch_message_add_tag, notmuch_message_remove_tag, and
941  * notmuch_message_remove_all_tags), will not be committed to the
942  * database until the message is thawed with notmuch_message_thaw.
943  *
944  * Multiple calls to freeze/thaw are valid and these calls will
945  * "stack". That is there must be as many calls to thaw as to freeze
946  * before a message is actually thawed.
947  *
948  * The ability to do freeze/thaw allows for safe transactions to
949  * change tag values. For example, explicitly setting a message to
950  * have a given set of tags might look like this:
951  *
952  *    notmuch_message_freeze (message);
953  *
954  *    notmuch_message_remove_all_tags (message);
955  *
956  *    for (i = 0; i < NUM_TAGS; i++)
957  *        notmuch_message_add_tag (message, tags[i]);
958  *
959  *    notmuch_message_thaw (message);
960  *
961  * With freeze/thaw used like this, the message in the database is
962  * guaranteed to have either the full set of original tag values, or
963  * the full set of new tag values, but nothing in between.
964  *
965  * Imagine the example above without freeze/thaw and the operation
966  * somehow getting interrupted. This could result in the message being
967  * left with no tags if the interruption happened after
968  * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
969  *
970  * Return value:
971  *
972  * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
973  *
974  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
975  *      mode so message cannot be modified.
976  */
977 func (self *Message) Freeze() Status {
978         if self.message == nil {
979                 return STATUS_NULL_POINTER
980         }
981         return Status(C.notmuch_message_freeze(self.message))
982 }
983
984 /* Thaw the current 'message', synchronizing any changes that may have
985  * occurred while 'message' was frozen into the notmuch database.
986  *
987  * See notmuch_message_freeze for an example of how to use this
988  * function to safely provide tag changes.
989  *
990  * Multiple calls to freeze/thaw are valid and these calls with
991  * "stack". That is there must be as many calls to thaw as to freeze
992  * before a message is actually thawed.
993  *
994  * Return value:
995  *
996  * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
997  *      its frozen count has successfully been reduced by 1).
998  *
999  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1000  *      an unfrozen message. That is, there have been an unbalanced
1001  *      number of calls to notmuch_message_freeze and
1002  *      notmuch_message_thaw.
1003  */
1004 func (self *Message) Thaw() Status {
1005         if self.message == nil {
1006                 return STATUS_NULL_POINTER
1007         }
1008
1009         return Status(C.notmuch_message_thaw(self.message))
1010 }
1011
1012 /* Destroy a notmuch_message_t object.
1013  *
1014  * It can be useful to call this function in the case of a single
1015  * query object with many messages in the result, (such as iterating
1016  * over the entire database). Otherwise, it's fine to never call this
1017  * function and there will still be no memory leaks. (The memory from
1018  * the messages get reclaimed when the containing query is destroyed.)
1019  */
1020 func (self *Message) Destroy() {
1021         if self.message == nil {
1022                 return
1023         }
1024         C.notmuch_message_destroy(self.message)
1025 }
1026
1027 /* Is the given 'tags' iterator pointing at a valid tag.
1028  *
1029  * When this function returns TRUE, notmuch_tags_get will return a
1030  * valid string. Whereas when this function returns FALSE,
1031  * notmuch_tags_get will return NULL.
1032  *
1033  * See the documentation of notmuch_message_get_tags for example code
1034  * showing how to iterate over a notmuch_tags_t object.
1035  */
1036 func (self *Tags) Valid() bool {
1037         if self.tags == nil {
1038                 return false
1039         }
1040         v := C.notmuch_tags_valid(self.tags)
1041         if v == 0 {
1042                 return false
1043         }
1044         return true
1045 }
1046
1047 /* Get the current tag from 'tags' as a string.
1048  *
1049  * Note: The returned string belongs to 'tags' and has a lifetime
1050  * identical to it (and the query to which it ultimately belongs).
1051  *
1052  * See the documentation of notmuch_message_get_tags for example code
1053  * showing how to iterate over a notmuch_tags_t object.
1054  */
1055 func (self *Tags) Get() string {
1056         if self.tags == nil {
1057                 return ""
1058         }
1059         s := C.notmuch_tags_get(self.tags)
1060         // we dont own 's'
1061
1062         return C.GoString(s)
1063 }
1064 func (self *Tags) String() string {
1065         return self.Get()
1066 }
1067
1068 /* Move the 'tags' iterator to the next tag.
1069  *
1070  * If 'tags' is already pointing at the last tag then the iterator
1071  * will be moved to a point just beyond that last tag, (where
1072  * notmuch_tags_valid will return FALSE and notmuch_tags_get will
1073  * return NULL).
1074  *
1075  * See the documentation of notmuch_message_get_tags for example code
1076  * showing how to iterate over a notmuch_tags_t object.
1077  */
1078 func (self *Tags) MoveToNext() {
1079         if self.tags == nil {
1080                 return
1081         }
1082         C.notmuch_tags_move_to_next(self.tags)
1083 }
1084
1085 /* Destroy a notmuch_tags_t object.
1086  *
1087  * It's not strictly necessary to call this function. All memory from
1088  * the notmuch_tags_t object will be reclaimed when the containing
1089  * message or query objects are destroyed.
1090  */
1091 func (self *Tags) Destroy() {
1092         if self.tags == nil {
1093                 return
1094         }
1095         C.notmuch_tags_destroy(self.tags)
1096 }
1097
1098 // TODO: wrap notmuch_directory_<fct>
1099
1100 /* Destroy a notmuch_directory_t object. */
1101 func (self *Directory) Destroy() {
1102         if self.dir == nil {
1103                 return
1104         }
1105         C.notmuch_directory_destroy(self.dir)
1106 }
1107
1108 // TODO: wrap notmuch_filenames_<fct>
1109
1110 /* Destroy a notmuch_filenames_t object.
1111  *
1112  * It's not strictly necessary to call this function. All memory from
1113  * the notmuch_filenames_t object will be reclaimed when the
1114  * containing directory object is destroyed.
1115  *
1116  * It is acceptable to pass NULL for 'filenames', in which case this
1117  * function will do nothing.
1118  */
1119 func (self *Filenames) Destroy() {
1120         if self.fnames == nil {
1121                 return
1122         }
1123         C.notmuch_filenames_destroy(self.fnames)
1124 }
1125 /* EOF */