]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/query.c
lib: replace deprecated n_q_search_messages with status returning version
[notmuch] / bindings / ruby / query.c
1 /* The Ruby interface to the notmuch mail library
2  *
3  * Copyright © 2010, 2011, 2012 Ali Polatel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: Ali Polatel <alip@exherbo.org>
19  */
20
21 #include "defs.h"
22
23 /*
24  * call-seq: QUERY.destroy! => nil
25  *
26  * Destroys the query, freeing all resources allocated for it.
27  */
28 VALUE
29 notmuch_rb_query_destroy (VALUE self)
30 {
31     notmuch_query_t *query;
32
33     Data_Get_Notmuch_Query (self, query);
34
35     notmuch_query_destroy (query);
36     DATA_PTR (self) = NULL;
37
38     return Qnil;
39 }
40
41 /*
42  * call-seq: QUERY.sort => fixnum
43  *
44  * Get sort type of the +QUERY+
45  */
46 VALUE
47 notmuch_rb_query_get_sort (VALUE self)
48 {
49     notmuch_query_t *query;
50
51     Data_Get_Notmuch_Query (self, query);
52
53     return FIX2INT (notmuch_query_get_sort (query));
54 }
55
56 /*
57  * call-seq: QUERY.sort=(fixnum) => nil
58  *
59  * Set sort type of the +QUERY+
60  */
61 VALUE
62 notmuch_rb_query_set_sort (VALUE self, VALUE sortv)
63 {
64     notmuch_query_t *query;
65
66     Data_Get_Notmuch_Query (self, query);
67
68     if (!FIXNUM_P (sortv))
69         rb_raise (rb_eTypeError, "Not a Fixnum");
70
71     notmuch_query_set_sort (query, FIX2UINT (sortv));
72
73     return Qnil;
74 }
75
76 /*
77  * call-seq: QUERY.to_s => string
78  *
79  * Get query string of the +QUERY+
80  */
81 VALUE
82 notmuch_rb_query_get_string (VALUE self)
83 {
84     notmuch_query_t *query;
85
86     Data_Get_Notmuch_Query (self, query);
87
88     return rb_str_new2 (notmuch_query_get_query_string (query));
89 }
90
91 /*
92  * call-seq: QUERY.add_tag_exclude(tag) => nil
93  *
94  * Add a tag that will be excluded from the query results by default.
95  */
96 VALUE
97 notmuch_rb_query_add_tag_exclude (VALUE self, VALUE tagv)
98 {
99     notmuch_query_t *query;
100     const char *tag;
101
102     Data_Get_Notmuch_Query (self, query);
103     tag = RSTRING_PTR(tagv);
104
105     notmuch_query_add_tag_exclude(query, tag);
106     return Qnil;
107 }
108
109 /*
110  * call-seq: QUERY.omit_excluded=(boolean) => nil
111  *
112  * Specify whether to omit excluded results or simply flag them.
113  * By default, this is set to +true+.
114  */
115 VALUE
116 notmuch_rb_query_set_omit_excluded (VALUE self, VALUE omitv)
117 {
118     notmuch_query_t *query;
119
120     Data_Get_Notmuch_Query (self, query);
121
122     notmuch_query_set_omit_excluded (query, RTEST (omitv));
123
124     return Qnil;
125 }
126
127 /*
128  * call-seq: QUERY.search_threads => THREADS
129  *
130  * Search for threads
131  */
132 VALUE
133 notmuch_rb_query_search_threads (VALUE self)
134 {
135     notmuch_query_t *query;
136     notmuch_threads_t *threads;
137     notmuch_status_t status;
138
139     Data_Get_Notmuch_Query (self, query);
140
141     status = notmuch_query_search_threads (query, &threads);
142     if (status)
143         notmuch_rb_status_raise (status);
144
145     return Data_Wrap_Struct (notmuch_rb_cThreads, NULL, NULL, threads);
146 }
147
148 /*
149  * call-seq: QUERY.search_messages => MESSAGES
150  *
151  * Search for messages
152  */
153 VALUE
154 notmuch_rb_query_search_messages (VALUE self)
155 {
156     notmuch_query_t *query;
157     notmuch_messages_t *messages;
158     notmuch_status_t status;
159
160     Data_Get_Notmuch_Query (self, query);
161
162     status = notmuch_query_search_messages (query, &messages);
163     if (status)
164         notmuch_rb_status_raise (status);
165
166     return Data_Wrap_Struct (notmuch_rb_cMessages, NULL, NULL, messages);
167 }
168
169 /*
170  * call-seq: QUERY.count_messages => Fixnum
171  *
172  * Return an estimate of the number of messages matching a search
173  */
174 VALUE
175 notmuch_rb_query_count_messages (VALUE self)
176 {
177     notmuch_query_t *query;
178     notmuch_status_t status;
179     unsigned int count;
180
181     Data_Get_Notmuch_Query (self, query);
182
183     status = notmuch_query_count_messages_st (query, &count);
184     if (status)
185         notmuch_rb_status_raise (status);
186
187     return UINT2NUM(count);
188 }
189
190 /*
191  * call-seq: QUERY.count_threads => Fixnum
192  *
193  * Return an estimate of the number of threads matching a search
194  */
195 VALUE
196 notmuch_rb_query_count_threads (VALUE self)
197 {
198     notmuch_query_t *query;
199     notmuch_status_t status;
200     unsigned int count;
201
202     Data_Get_Notmuch_Query (self, query);
203
204     status = notmuch_query_count_threads_st (query, &count);
205     if (status)
206         notmuch_rb_status_raise (status);
207
208     return UINT2NUM(count);
209 }