Debian Codegraph @

lib/notmuch/query.go

raw
1 //go:build notmuch
2 // +build notmuch
3
4 package notmuch
5
6 /*
7 #cgo LDFLAGS: -lnotmuch
8
9 #include <stdlib.h>
10 #include <notmuch.h>
11
12 */
13 import "C"
14 import "unsafe"
15
16 type ExcludeMode int
17
18 const (
19 EXCLUDE_FLAG ExcludeMode = C.NOTMUCH_EXCLUDE_FLAG
20 EXCLUDE_TRUE ExcludeMode = C.NOTMUCH_EXCLUDE_TRUE
21 EXCLUDE_FALSE ExcludeMode = C.NOTMUCH_EXCLUDE_FALSE
22 EXCLUDE_ALL ExcludeMode = C.NOTMUCH_EXCLUDE_ALL
23 )
24
25 type SortMode int
26
27 const (
28 SORT_OLDEST_FIRST SortMode = C.NOTMUCH_SORT_OLDEST_FIRST
29 SORT_NEWEST_FIRST SortMode = C.NOTMUCH_SORT_NEWEST_FIRST
30 SORT_MESSAGE_ID SortMode = C.NOTMUCH_SORT_MESSAGE_ID
31 SORT_UNSORTED SortMode = C.NOTMUCH_SORT_UNSORTED
32 )
33
34 type Query struct {
35 query *C.notmuch_query_t
36 }
37
38 // Close frees resources associated with a query. Closing a query release all
39 // resources associated with any underlying search (Threads, Messages, etc)
40 func (q *Query) Close() {
41 C.notmuch_query_destroy(q.query)
42 }
43
44 // Return the string of the query
45 func (q *Query) String() string {
46 return C.GoString(C.notmuch_query_get_query_string(q.query))
47 }
48
49 // Returns the Database associated with the query. The Profile value will not
50 // be set on the returned value.
51 func (q *Query) Database() Database {
52 db := C.notmuch_query_get_database(q.query)
53 return Database{
54 db: db,
55 }
56 }
57
58 // Exclude sets the exclusion mode.
59 func (q *Query) Exclude(val ExcludeMode) {
60 cVal := C.notmuch_exclude_t(val)
61 C.notmuch_query_set_omit_excluded(q.query, cVal)
62 }
63
64 // Sort sets the sort order of the results
65 func (q *Query) Sort(sort SortMode) {
66 cVal := C.notmuch_sort_t(sort)
67 C.notmuch_query_set_sort(q.query, cVal)
68 }
69
70 // SortMode returns the current sort order of the results
71 func (q *Query) SortMode() SortMode {
72 return SortMode(C.notmuch_query_get_sort(q.query))
73 }
74
75 // ExcludeTag adds a tag to exclude from the results
76 func (q *Query) ExcludeTag(tag string) error {
77 cTag := C.CString(tag)
78 defer C.free(unsafe.Pointer(cTag))
79 return errorWrap(C.notmuch_query_add_tag_exclude(q.query, cTag))
80 }
81
82 // Threads returns an iterator over the threads that match the query
83 func (q *Query) Threads() (Threads, error) {
84 var cThreads *C.notmuch_threads_t
85 err := errorWrap(C.notmuch_query_search_threads(q.query, &cThreads)) //nolint:gocritic // see note in notmuch.go
86 if err != nil {
87 return Threads{}, err
88 }
89 threads := Threads{
90 threads: cThreads,
91 }
92 return threads, nil
93 }
94
95 // Messages returns an iterator over the messages that match the query
96 func (q *Query) Messages() (Messages, error) {
97 var cMessages *C.notmuch_messages_t
98 err := errorWrap(C.notmuch_query_search_messages(q.query, &cMessages)) //nolint:gocritic // see note in notmuch.go
99 if err != nil {
100 return Messages{}, err
101 }
102 messages := Messages{
103 messages: cMessages,
104 }
105 return messages, nil
106 }
107
108 // CountMessages returns the number of messages matching the query
109 func (q *Query) CountMessages() (int, error) {
110 var count C.uint
111 err := errorWrap(C.notmuch_query_count_messages(q.query, &count))
112 return int(count), err
113 }
114
115 // CountThreads returns the number of threads matching the query
116 func (q *Query) CountThreads() (int, error) {
117 var count C.uint
118 err := errorWrap(C.notmuch_query_count_threads(q.query, &count))
119 return int(count), err
120 }