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