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 #ifndef DEFS_H
22 #define DEFS_H
23 
24 #include <notmuch.h>
25 #include <ruby.h>
26 #include <talloc.h>
27 
28 extern VALUE notmuch_rb_cDatabase;
29 extern VALUE notmuch_rb_cDirectory;
30 extern VALUE notmuch_rb_cFileNames;
31 extern VALUE notmuch_rb_cQuery;
32 extern VALUE notmuch_rb_cThreads;
33 extern VALUE notmuch_rb_cThread;
34 extern VALUE notmuch_rb_cMessages;
35 extern VALUE notmuch_rb_cMessage;
36 extern VALUE notmuch_rb_cTags;
37 
38 extern VALUE notmuch_rb_eBaseError;
39 extern VALUE notmuch_rb_eDatabaseError;
40 extern VALUE notmuch_rb_eMemoryError;
41 extern VALUE notmuch_rb_eReadOnlyError;
42 extern VALUE notmuch_rb_eXapianError;
43 extern VALUE notmuch_rb_eFileError;
44 extern VALUE notmuch_rb_eFileNotEmailError;
45 extern VALUE notmuch_rb_eNullPointerError;
46 extern VALUE notmuch_rb_eTagTooLongError;
47 extern VALUE notmuch_rb_eUnbalancedFreezeThawError;
48 extern VALUE notmuch_rb_eUnbalancedAtomicError;
49 
50 extern ID ID_call;
51 extern ID ID_db_create;
52 extern ID ID_db_mode;
53 
54 /* RSTRING_PTR() is new in ruby-1.9 */
55 #if !defined(RSTRING_PTR)
56 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
57 #endif /* !defined (RSTRING_PTR) */
58 
59 extern const rb_data_type_t notmuch_rb_object_type;
60 extern const rb_data_type_t notmuch_rb_database_type;
61 extern const rb_data_type_t notmuch_rb_directory_type;
62 extern const rb_data_type_t notmuch_rb_filenames_type;
63 extern const rb_data_type_t notmuch_rb_query_type;
64 extern const rb_data_type_t notmuch_rb_threads_type;
65 extern const rb_data_type_t notmuch_rb_thread_type;
66 extern const rb_data_type_t notmuch_rb_messages_type;
67 extern const rb_data_type_t notmuch_rb_message_type;
68 extern const rb_data_type_t notmuch_rb_tags_type;
69 
70 #define Data_Get_Notmuch_Rb_Object(obj, type, ptr)		    		    \
71     do {									    \
72 	(ptr) = rb_check_typeddata ((obj), (type));				    \
73 	if (RB_UNLIKELY (!(ptr))) {						    \
74 	    VALUE cname = rb_class_name (CLASS_OF ((obj)));			    \
75 	    rb_raise (rb_eRuntimeError, "%"PRIsVALUE" object destroyed", cname);    \
76 	}									    \
77     } while (0)
78 
79 #define Data_Get_Notmuch_Object(obj, type, ptr)			\
80     do {							\
81 	notmuch_rb_object_t *rb_wrapper;			\
82 	Data_Get_Notmuch_Rb_Object ((obj), (type), rb_wrapper);	\
83 	(ptr) = rb_wrapper->nm_object;				\
84     } while (0)
85 
86 #define Data_Wrap_Notmuch_Object(klass, type, ptr) \
87     TypedData_Wrap_Struct ((klass), (type), notmuch_rb_object_create ((ptr), "notmuch_rb_object: " __location__))
88 
89 #define Data_Get_Notmuch_Database(obj, ptr) \
90     Data_Get_Notmuch_Object ((obj), &notmuch_rb_database_type, (ptr))
91 
92 #define Data_Get_Notmuch_Directory(obj, ptr) \
93     Data_Get_Notmuch_Object ((obj), &notmuch_rb_directory_type, (ptr))
94 
95 #define Data_Get_Notmuch_FileNames(obj, ptr) \
96     Data_Get_Notmuch_Object ((obj), &notmuch_rb_filenames_type, (ptr))
97 
98 #define Data_Get_Notmuch_Query(obj, ptr) \
99     Data_Get_Notmuch_Object ((obj), &notmuch_rb_query_type, (ptr))
100 
101 #define Data_Get_Notmuch_Threads(obj, ptr) \
102     Data_Get_Notmuch_Object ((obj), &notmuch_rb_threads_type, (ptr))
103 
104 #define Data_Get_Notmuch_Messages(obj, ptr) \
105     Data_Get_Notmuch_Object ((obj), &notmuch_rb_messages_type, (ptr))
106 
107 #define Data_Get_Notmuch_Thread(obj, ptr) \
108     Data_Get_Notmuch_Object ((obj), &notmuch_rb_thread_type, (ptr))
109 
110 #define Data_Get_Notmuch_Message(obj, ptr) \
111     Data_Get_Notmuch_Object ((obj), &notmuch_rb_message_type, (ptr))
112 
113 #define Data_Get_Notmuch_Tags(obj, ptr) \
114     Data_Get_Notmuch_Object ((obj), &notmuch_rb_tags_type, (ptr))
115 
116 typedef struct {
117     void *nm_object;
118 } notmuch_rb_object_t;
119 
120 static inline void *
notmuch_rb_object_create(void * nm_object,const char * name)121 notmuch_rb_object_create (void *nm_object, const char *name)
122 {
123     notmuch_rb_object_t *rb_wrapper = talloc_named_const (NULL, sizeof (*rb_wrapper), name);
124 
125     if (RB_UNLIKELY (!rb_wrapper))
126 	return NULL;
127 
128     rb_wrapper->nm_object = nm_object;
129     talloc_steal (rb_wrapper, nm_object);
130     return rb_wrapper;
131 }
132 
133 static inline void
notmuch_rb_object_free(void * rb_wrapper)134 notmuch_rb_object_free (void *rb_wrapper)
135 {
136     talloc_free (rb_wrapper);
137 }
138 
139 static inline void
notmuch_rb_object_destroy(VALUE rb_object,const rb_data_type_t * type)140 notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type)
141 {
142     notmuch_rb_object_t *rb_wrapper;
143 
144     Data_Get_Notmuch_Rb_Object (rb_object, type, rb_wrapper);
145 
146     /* Call the corresponding notmuch_*_destroy function */
147     ((void (*)(void *)) type->data) (rb_wrapper->nm_object);
148     notmuch_rb_object_free (rb_wrapper);
149     DATA_PTR (rb_object) = NULL;
150 }
151 
152 /* status.c */
153 void
154 notmuch_rb_status_raise (notmuch_status_t status);
155 
156 /* database.c */
157 VALUE
158 notmuch_rb_database_alloc (VALUE klass);
159 
160 VALUE
161 notmuch_rb_database_destroy (VALUE self);
162 
163 VALUE
164 notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE klass);
165 
166 VALUE
167 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass);
168 
169 VALUE
170 notmuch_rb_database_close (VALUE self);
171 
172 VALUE
173 notmuch_rb_database_path (VALUE self);
174 
175 VALUE
176 notmuch_rb_database_version (VALUE self);
177 
178 VALUE
179 notmuch_rb_database_needs_upgrade (VALUE self);
180 
181 VALUE
182 notmuch_rb_database_upgrade (VALUE self);
183 
184 VALUE
185 notmuch_rb_database_begin_atomic (VALUE self);
186 
187 VALUE
188 notmuch_rb_database_end_atomic (VALUE self);
189 
190 VALUE
191 notmuch_rb_database_get_directory (VALUE self, VALUE pathv);
192 
193 VALUE
194 notmuch_rb_database_add_message (VALUE self, VALUE pathv);
195 
196 VALUE
197 notmuch_rb_database_remove_message (VALUE self, VALUE pathv);
198 
199 VALUE
200 notmuch_rb_database_find_message (VALUE self, VALUE idv);
201 
202 VALUE
203 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv);
204 
205 VALUE
206 notmuch_rb_database_get_all_tags (VALUE self);
207 
208 VALUE
209 notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self);
210 
211 /* directory.c */
212 VALUE
213 notmuch_rb_directory_destroy (VALUE self);
214 
215 VALUE
216 notmuch_rb_directory_get_mtime (VALUE self);
217 
218 VALUE
219 notmuch_rb_directory_set_mtime (VALUE self, VALUE mtimev);
220 
221 VALUE
222 notmuch_rb_directory_get_child_files (VALUE self);
223 
224 VALUE
225 notmuch_rb_directory_get_child_directories (VALUE self);
226 
227 /* filenames.c */
228 VALUE
229 notmuch_rb_filenames_destroy (VALUE self);
230 
231 VALUE
232 notmuch_rb_filenames_each (VALUE self);
233 
234 /* query.c */
235 VALUE
236 notmuch_rb_query_destroy (VALUE self);
237 
238 VALUE
239 notmuch_rb_query_get_sort (VALUE self);
240 
241 VALUE
242 notmuch_rb_query_set_sort (VALUE self, VALUE sortv);
243 
244 VALUE
245 notmuch_rb_query_get_string (VALUE self);
246 
247 VALUE
248 notmuch_rb_query_add_tag_exclude (VALUE self, VALUE tagv);
249 
250 VALUE
251 notmuch_rb_query_set_omit_excluded (VALUE self, VALUE omitv);
252 
253 VALUE
254 notmuch_rb_query_search_threads (VALUE self);
255 
256 VALUE
257 notmuch_rb_query_search_messages (VALUE self);
258 
259 VALUE
260 notmuch_rb_query_count_messages (VALUE self);
261 
262 VALUE
263 notmuch_rb_query_count_threads (VALUE self);
264 
265 /* threads.c */
266 VALUE
267 notmuch_rb_threads_destroy (VALUE self);
268 
269 VALUE
270 notmuch_rb_threads_each (VALUE self);
271 
272 /* messages.c */
273 VALUE
274 notmuch_rb_messages_destroy (VALUE self);
275 
276 VALUE
277 notmuch_rb_messages_each (VALUE self);
278 
279 VALUE
280 notmuch_rb_messages_collect_tags (VALUE self);
281 
282 /* thread.c */
283 VALUE
284 notmuch_rb_thread_destroy (VALUE self);
285 
286 VALUE
287 notmuch_rb_thread_get_thread_id (VALUE self);
288 
289 VALUE
290 notmuch_rb_thread_get_total_messages (VALUE self);
291 
292 VALUE
293 notmuch_rb_thread_get_toplevel_messages (VALUE self);
294 
295 VALUE
296 notmuch_rb_thread_get_messages (VALUE self);
297 
298 VALUE
299 notmuch_rb_thread_get_matched_messages (VALUE self);
300 
301 VALUE
302 notmuch_rb_thread_get_authors (VALUE self);
303 
304 VALUE
305 notmuch_rb_thread_get_subject (VALUE self);
306 
307 VALUE
308 notmuch_rb_thread_get_oldest_date (VALUE self);
309 
310 VALUE
311 notmuch_rb_thread_get_newest_date (VALUE self);
312 
313 VALUE
314 notmuch_rb_thread_get_tags (VALUE self);
315 
316 /* message.c */
317 VALUE
318 notmuch_rb_message_destroy (VALUE self);
319 
320 VALUE
321 notmuch_rb_message_get_message_id (VALUE self);
322 
323 VALUE
324 notmuch_rb_message_get_thread_id (VALUE self);
325 
326 VALUE
327 notmuch_rb_message_get_replies (VALUE self);
328 
329 VALUE
330 notmuch_rb_message_get_filename (VALUE self);
331 
332 VALUE
333 notmuch_rb_message_get_filenames (VALUE self);
334 
335 VALUE
336 notmuch_rb_message_get_flag (VALUE self, VALUE flagv);
337 
338 VALUE
339 notmuch_rb_message_set_flag (VALUE self, VALUE flagv, VALUE valuev);
340 
341 VALUE
342 notmuch_rb_message_get_date (VALUE self);
343 
344 VALUE
345 notmuch_rb_message_get_header (VALUE self, VALUE headerv);
346 
347 VALUE
348 notmuch_rb_message_get_tags (VALUE self);
349 
350 VALUE
351 notmuch_rb_message_add_tag (VALUE self, VALUE tagv);
352 
353 VALUE
354 notmuch_rb_message_remove_tag (VALUE self, VALUE tagv);
355 
356 VALUE
357 notmuch_rb_message_remove_all_tags (VALUE self);
358 
359 VALUE
360 notmuch_rb_message_maildir_flags_to_tags (VALUE self);
361 
362 VALUE
363 notmuch_rb_message_tags_to_maildir_flags (VALUE self);
364 
365 VALUE
366 notmuch_rb_message_freeze (VALUE self);
367 
368 VALUE
369 notmuch_rb_message_thaw (VALUE self);
370 
371 /* tags.c */
372 VALUE
373 notmuch_rb_tags_destroy (VALUE self);
374 
375 VALUE
376 notmuch_rb_tags_each (VALUE self);
377 
378 /* init.c */
379 void
380 Init_notmuch (void);
381 
382 #endif
383