]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/query.c
Merge tag 'debian/0.18.1-2'
[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 http://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
138     Data_Get_Notmuch_Query (self, query);
139
140     threads = notmuch_query_search_threads (query);
141     if (!threads)
142         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
143
144     return Data_Wrap_Struct (notmuch_rb_cThreads, NULL, NULL, threads);
145 }
146
147 /*
148  * call-seq: QUERY.search_messages => MESSAGES
149  *
150  * Search for messages
151  */
152 VALUE
153 notmuch_rb_query_search_messages (VALUE self)
154 {
155     notmuch_query_t *query;
156     notmuch_messages_t *messages;
157
158     Data_Get_Notmuch_Query (self, query);
159
160     messages = notmuch_query_search_messages (query);
161     if (!messages)
162         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
163
164     return Data_Wrap_Struct (notmuch_rb_cMessages, NULL, NULL, messages);
165 }
166
167 /*
168  * call-seq: QUERY.count_messages => Fixnum
169  *
170  * Return an estimate of the number of messages matching a search
171  */
172 VALUE
173 notmuch_rb_query_count_messages (VALUE self)
174 {
175     notmuch_query_t *query;
176
177     Data_Get_Notmuch_Query (self, query);
178
179     /* Xapian exceptions are not handled properly.
180      * (function may return 0 after printing a message)
181      * Thus there is nothing we can do here...
182      */
183     return UINT2NUM(notmuch_query_count_messages(query));
184 }
185
186 /*
187  * call-seq: QUERY.count_threads => Fixnum
188  *
189  * Return an estimate of the number of threads matching a search
190  */
191 VALUE
192 notmuch_rb_query_count_threads (VALUE self)
193 {
194     notmuch_query_t *query;
195
196     Data_Get_Notmuch_Query (self, query);
197
198     /* Xapian exceptions are not handled properly.
199      * (function may return 0 after printing a message)
200      * Thus there is nothing we can do here...
201      */
202     return UINT2NUM(notmuch_query_count_threads(query));
203 }