]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/query.c
02b7819eb6856e12aa25975dde9d506a15be8d01
[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.search_threads => THREADS
93  *
94  * Search for threads
95  */
96 VALUE
97 notmuch_rb_query_search_threads (VALUE self)
98 {
99     notmuch_query_t *query;
100     notmuch_threads_t *threads;
101
102     Data_Get_Notmuch_Query (self, query);
103
104     threads = notmuch_query_search_threads (query);
105     if (!threads)
106         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
107
108     return Data_Wrap_Struct (notmuch_rb_cThreads, NULL, NULL, threads);
109 }
110
111 /*
112  * call-seq: QUERY.search_messages => MESSAGES
113  *
114  * Search for messages
115  */
116 VALUE
117 notmuch_rb_query_search_messages (VALUE self)
118 {
119     notmuch_query_t *query;
120     notmuch_messages_t *messages;
121
122     Data_Get_Notmuch_Query (self, query);
123
124     messages = notmuch_query_search_messages (query);
125     if (!messages)
126         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
127
128     return Data_Wrap_Struct (notmuch_rb_cMessages, NULL, NULL, messages);
129 }
130
131 /*
132  * call-seq: QUERY.count_messages => Fixnum
133  *
134  * Return an estimate of the number of messages matching a search
135  */
136 VALUE
137 notmuch_rb_query_count_messages (VALUE self)
138 {
139     notmuch_query_t *query;
140
141     Data_Get_Notmuch_Query (self, query);
142
143     /* Xapian exceptions are not handled properly.
144      * (function may return 0 after printing a message)
145      * Thus there is nothing we can do here...
146      */
147     return UINT2FIX(notmuch_query_count_messages(query));
148 }