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
notmuch_rb_query_destroy(VALUE self)29 notmuch_rb_query_destroy (VALUE self)
30 {
31 notmuch_rb_object_destroy (self, ¬much_rb_query_type);
32
33 return Qnil;
34 }
35
36 /*
37 * call-seq: QUERY.sort => fixnum
38 *
39 * Get sort type of the +QUERY+
40 */
41 VALUE
notmuch_rb_query_get_sort(VALUE self)42 notmuch_rb_query_get_sort (VALUE self)
43 {
44 notmuch_query_t *query;
45
46 Data_Get_Notmuch_Query (self, query);
47
48 return FIX2INT (notmuch_query_get_sort (query));
49 }
50
51 /*
52 * call-seq: QUERY.sort=(fixnum) => nil
53 *
54 * Set sort type of the +QUERY+
55 */
56 VALUE
notmuch_rb_query_set_sort(VALUE self,VALUE sortv)57 notmuch_rb_query_set_sort (VALUE self, VALUE sortv)
58 {
59 notmuch_query_t *query;
60
61 Data_Get_Notmuch_Query (self, query);
62
63 if (!FIXNUM_P (sortv))
64 rb_raise (rb_eTypeError, "Not a Fixnum");
65
66 notmuch_query_set_sort (query, FIX2UINT (sortv));
67
68 return Qnil;
69 }
70
71 /*
72 * call-seq: QUERY.to_s => string
73 *
74 * Get query string of the +QUERY+
75 */
76 VALUE
notmuch_rb_query_get_string(VALUE self)77 notmuch_rb_query_get_string (VALUE self)
78 {
79 notmuch_query_t *query;
80
81 Data_Get_Notmuch_Query (self, query);
82
83 return rb_str_new2 (notmuch_query_get_query_string (query));
84 }
85
86 /*
87 * call-seq: QUERY.add_tag_exclude(tag) => nil
88 *
89 * Add a tag that will be excluded from the query results by default.
90 */
91 VALUE
notmuch_rb_query_add_tag_exclude(VALUE self,VALUE tagv)92 notmuch_rb_query_add_tag_exclude (VALUE self, VALUE tagv)
93 {
94 notmuch_query_t *query;
95 const char *tag;
96
97 Data_Get_Notmuch_Query (self, query);
98 tag = RSTRING_PTR(tagv);
99
100 notmuch_query_add_tag_exclude(query, tag);
101 return Qnil;
102 }
103
104 /*
105 * call-seq: QUERY.omit_excluded=(fixnum) => nil
106 *
107 * Specify whether to omit excluded results or simply flag them.
108 * By default, this is set to +Notmuch::EXCLUDE_TRUE+.
109 */
110 VALUE
notmuch_rb_query_set_omit_excluded(VALUE self,VALUE omitv)111 notmuch_rb_query_set_omit_excluded (VALUE self, VALUE omitv)
112 {
113 notmuch_query_t *query;
114 notmuch_exclude_t value;
115
116 Data_Get_Notmuch_Query (self, query);
117
118 value = FIXNUM_P (omitv) ? FIX2UINT (omitv) : RTEST(omitv);
119 notmuch_query_set_omit_excluded (query, value);
120
121 return Qnil;
122 }
123
124 /*
125 * call-seq: QUERY.search_threads => THREADS
126 *
127 * Search for threads
128 */
129 VALUE
notmuch_rb_query_search_threads(VALUE self)130 notmuch_rb_query_search_threads (VALUE self)
131 {
132 notmuch_query_t *query;
133 notmuch_threads_t *threads;
134 notmuch_status_t status;
135
136 Data_Get_Notmuch_Query (self, query);
137
138 status = notmuch_query_search_threads (query, &threads);
139 if (status)
140 notmuch_rb_status_raise (status);
141
142 return Data_Wrap_Notmuch_Object (notmuch_rb_cThreads, ¬much_rb_threads_type, threads);
143 }
144
145 /*
146 * call-seq: QUERY.search_messages => MESSAGES
147 *
148 * Search for messages
149 */
150 VALUE
notmuch_rb_query_search_messages(VALUE self)151 notmuch_rb_query_search_messages (VALUE self)
152 {
153 notmuch_query_t *query;
154 notmuch_messages_t *messages;
155 notmuch_status_t status;
156
157 Data_Get_Notmuch_Query (self, query);
158
159 status = notmuch_query_search_messages (query, &messages);
160 if (status)
161 notmuch_rb_status_raise (status);
162
163 return Data_Wrap_Notmuch_Object (notmuch_rb_cMessages, ¬much_rb_messages_type, messages);
164 }
165
166 /*
167 * call-seq: QUERY.count_messages => Fixnum
168 *
169 * Return an estimate of the number of messages matching a search
170 */
171 VALUE
notmuch_rb_query_count_messages(VALUE self)172 notmuch_rb_query_count_messages (VALUE self)
173 {
174 notmuch_query_t *query;
175 notmuch_status_t status;
176 unsigned int count;
177
178 Data_Get_Notmuch_Query (self, query);
179
180 status = notmuch_query_count_messages (query, &count);
181 if (status)
182 notmuch_rb_status_raise (status);
183
184 return UINT2NUM(count);
185 }
186
187 /*
188 * call-seq: QUERY.count_threads => Fixnum
189 *
190 * Return an estimate of the number of threads matching a search
191 */
192 VALUE
notmuch_rb_query_count_threads(VALUE self)193 notmuch_rb_query_count_threads (VALUE self)
194 {
195 notmuch_query_t *query;
196 notmuch_status_t status;
197 unsigned int count;
198
199 Data_Get_Notmuch_Query (self, query);
200
201 status = notmuch_query_count_threads (query, &count);
202 if (status)
203 notmuch_rb_status_raise (status);
204
205 return UINT2NUM(count);
206 }
207