]> git.cworth.org Git - notmuch/blob - bindings/ruby/database.c
ruby: enable garbage collection using talloc
[notmuch] / bindings / ruby / database.c
1 /* The Ruby interface to the notmuch mail library
2  *
3  * Copyright © 2010, 2011 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 VALUE
24 notmuch_rb_database_alloc (VALUE klass)
25 {
26     return Data_Wrap_Notmuch_Object (klass, &notmuch_rb_database_type, NULL);
27 }
28
29 /*
30  * call-seq: Notmuch::Database.new(path [, {:create => false, :mode => Notmuch::MODE_READ_ONLY}]) => DB
31  *
32  * Create or open a notmuch database using the given path.
33  *
34  * If :create is +true+, create the database instead of opening.
35  *
36  * The argument :mode specifies the open mode of the database.
37  */
38 VALUE
39 notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
40 {
41     const char *path;
42     int create, mode;
43     VALUE pathv, hashv;
44     VALUE modev;
45     notmuch_database_t *database;
46     notmuch_status_t ret;
47
48     /* Check arguments */
49     rb_scan_args (argc, argv, "11", &pathv, &hashv);
50
51     SafeStringValue (pathv);
52     path = RSTRING_PTR (pathv);
53
54     if (!NIL_P (hashv)) {
55         Check_Type (hashv, T_HASH);
56         create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create)));
57         modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode));
58         if (NIL_P (modev))
59             mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
60         else if (!FIXNUM_P (modev))
61             rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
62         else {
63             mode = FIX2INT (modev);
64             switch (mode) {
65             case NOTMUCH_DATABASE_MODE_READ_ONLY:
66             case NOTMUCH_DATABASE_MODE_READ_WRITE:
67                 break;
68             default:
69                 rb_raise ( rb_eTypeError, "Invalid mode");
70             }
71         }
72     } else {
73         create = 0;
74         mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
75     }
76
77     rb_check_typeddata (self, &notmuch_rb_database_type);
78     if (create)
79         ret = notmuch_database_create (path, &database);
80     else
81         ret = notmuch_database_open (path, mode, &database);
82     notmuch_rb_status_raise (ret);
83
84     DATA_PTR (self) = notmuch_rb_object_create (database, "notmuch_rb_database");
85
86     return self;
87 }
88
89 /*
90  * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
91  *
92  * Identical to new, except that when it is called with a block, it yields with
93  * the new instance and closes it, and returns the result which is returned from
94  * the block.
95  */
96 VALUE
97 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
98 {
99     VALUE obj;
100
101     obj = rb_class_new_instance (argc, argv, klass);
102     if (!rb_block_given_p ())
103         return obj;
104
105     return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
106 }
107
108 /*
109  * call-seq: DB.close => nil
110  *
111  * Close the notmuch database.
112  */
113 VALUE
114 notmuch_rb_database_close (VALUE self)
115 {
116     notmuch_status_t ret;
117     ret = notmuch_rb_object_destroy (self, &notmuch_rb_database_type);
118     notmuch_rb_status_raise (ret);
119
120     return Qnil;
121 }
122
123 /*
124  * call-seq: DB.path => String
125  *
126  * Return the path of the database
127  */
128 VALUE
129 notmuch_rb_database_path (VALUE self)
130 {
131     notmuch_database_t *db;
132
133     Data_Get_Notmuch_Database (self, db);
134
135     return rb_str_new2 (notmuch_database_get_path (db));
136 }
137
138 /*
139  * call-seq: DB.version => Fixnum
140  *
141  * Return the version of the database
142  */
143 VALUE
144 notmuch_rb_database_version (VALUE self)
145 {
146     notmuch_database_t *db;
147
148     Data_Get_Notmuch_Database (self, db);
149
150     return INT2FIX (notmuch_database_get_version (db));
151 }
152
153 /*
154  * call-seq: DB.needs_upgrade? => true or false
155  *
156  * Return the +true+ if the database needs upgrading, +false+ otherwise
157  */
158 VALUE
159 notmuch_rb_database_needs_upgrade (VALUE self)
160 {
161     notmuch_database_t *db;
162
163     Data_Get_Notmuch_Database (self, db);
164
165     return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
166 }
167
168 static void
169 notmuch_rb_upgrade_notify (void *closure, double progress)
170 {
171     VALUE *block = (VALUE *) closure;
172     rb_funcall (*block, ID_call, 1, rb_float_new (progress));
173 }
174
175 /*
176  * call-seq: DB.upgrade! [{|progress| block }] => nil
177  *
178  * Upgrade the database.
179  *
180  * If a block is given the block is called with a progress indicator as a
181  * floating point value in the range of [0.0..1.0].
182  */
183 VALUE
184 notmuch_rb_database_upgrade (VALUE self)
185 {
186     notmuch_status_t ret;
187     void (*pnotify) (void *closure, double progress);
188     notmuch_database_t *db;
189     VALUE block;
190
191     Data_Get_Notmuch_Database (self, db);
192
193     if (rb_block_given_p ()) {
194         pnotify = notmuch_rb_upgrade_notify;
195         block = rb_block_proc ();
196     }
197     else
198         pnotify = NULL;
199
200     ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
201     notmuch_rb_status_raise (ret);
202
203     return Qtrue;
204 }
205
206 /*
207  * call-seq: DB.begin_atomic => nil
208  *
209  * Begin an atomic database operation.
210  */
211 VALUE
212 notmuch_rb_database_begin_atomic (VALUE self)
213 {
214     notmuch_status_t ret;
215     notmuch_database_t *db;
216
217     Data_Get_Notmuch_Database (self, db);
218
219     ret = notmuch_database_begin_atomic (db);
220     notmuch_rb_status_raise (ret);
221
222     return Qtrue;
223 }
224
225 /*
226  * call-seq: DB.end_atomic => nil
227  *
228  * Indicate the end of an atomic database operation.
229  */
230 VALUE
231 notmuch_rb_database_end_atomic (VALUE self)
232 {
233     notmuch_status_t ret;
234     notmuch_database_t *db;
235
236     Data_Get_Notmuch_Database (self, db);
237
238     ret = notmuch_database_end_atomic (db);
239     notmuch_rb_status_raise (ret);
240
241     return Qtrue;
242 }
243
244 /*
245  * call-seq: DB.get_directory(path) => DIR
246  *
247  * Retrieve a directory object from the database for 'path'
248  */
249 VALUE
250 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
251 {
252     const char *path;
253     notmuch_status_t ret;
254     notmuch_directory_t *dir;
255     notmuch_database_t *db;
256
257     Data_Get_Notmuch_Database (self, db);
258
259     SafeStringValue (pathv);
260     path = RSTRING_PTR (pathv);
261
262     ret = notmuch_database_get_directory (db, path, &dir);
263     notmuch_rb_status_raise (ret);
264     if (dir)
265         return Data_Wrap_Notmuch_Object (notmuch_rb_cDirectory, &notmuch_rb_directory_type, dir);
266     return Qnil;
267 }
268
269 /*
270  * call-seq: DB.add_message(path) => MESSAGE, isdup
271  *
272  * Add a message to the database and return it.
273  *
274  * +isdup+ is a boolean that specifies whether the added message was a
275  * duplicate.
276  */
277 VALUE
278 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
279 {
280     const char *path;
281     notmuch_status_t ret;
282     notmuch_message_t *message;
283     notmuch_database_t *db;
284
285     Data_Get_Notmuch_Database (self, db);
286
287     SafeStringValue (pathv);
288     path = RSTRING_PTR (pathv);
289
290     ret = notmuch_database_index_file (db, path, NULL, &message);
291     notmuch_rb_status_raise (ret);
292     return rb_assoc_new (Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, &notmuch_rb_message_type, message),
293         (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
294 }
295
296 /*
297  * call-seq: DB.remove_message (path) => isdup
298  *
299  * Remove a message from the database.
300  *
301  * +isdup+ is a boolean that specifies whether the removed message was a
302  * duplicate.
303  */
304 VALUE
305 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
306 {
307     const char *path;
308     notmuch_status_t ret;
309     notmuch_database_t *db;
310
311     Data_Get_Notmuch_Database (self, db);
312
313     SafeStringValue (pathv);
314     path = RSTRING_PTR (pathv);
315
316     ret = notmuch_database_remove_message (db, path);
317     notmuch_rb_status_raise (ret);
318     return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
319 }
320
321 /*
322  * call-seq: DB.find_message(id) => MESSAGE or nil
323  *
324  * Find a message by message id.
325  */
326 VALUE
327 notmuch_rb_database_find_message (VALUE self, VALUE idv)
328 {
329     const char *id;
330     notmuch_status_t ret;
331     notmuch_database_t *db;
332     notmuch_message_t *message;
333
334     Data_Get_Notmuch_Database (self, db);
335
336     SafeStringValue (idv);
337     id = RSTRING_PTR (idv);
338
339     ret = notmuch_database_find_message (db, id, &message);
340     notmuch_rb_status_raise (ret);
341
342     if (message)
343         return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, &notmuch_rb_message_type, message);
344     return Qnil;
345 }
346
347 /*
348  * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
349  *
350  * Find a message by filename.
351  */
352 VALUE
353 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
354 {
355     const char *path;
356     notmuch_status_t ret;
357     notmuch_database_t *db;
358     notmuch_message_t *message;
359
360     Data_Get_Notmuch_Database (self, db);
361
362     SafeStringValue (pathv);
363     path = RSTRING_PTR (pathv);
364
365     ret = notmuch_database_find_message_by_filename (db, path, &message);
366     notmuch_rb_status_raise (ret);
367
368     if (message)
369         return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, &notmuch_rb_message_type, message);
370     return Qnil;
371 }
372
373 /*
374  * call-seq: DB.get_all_tags() => TAGS
375  *
376  * Returns a list of all tags found in the database.
377  */
378 VALUE
379 notmuch_rb_database_get_all_tags (VALUE self)
380 {
381     notmuch_database_t *db;
382     notmuch_tags_t *tags;
383
384     Data_Get_Notmuch_Database (self, db);
385
386     tags = notmuch_database_get_all_tags (db);
387     if (!tags) {
388         const char *msg = notmuch_database_status_string (db);
389         if (!msg)
390             msg = "Unknown notmuch error";
391
392         rb_raise (notmuch_rb_eBaseError, "%s", msg);
393     }
394     return Data_Wrap_Notmuch_Object (notmuch_rb_cTags, &notmuch_rb_tags_type, tags);
395 }
396
397 /*
398  * call-seq:
399  *   DB.query(query) => QUERY
400  *   DB.query(query, sort:, excluded_tags:, omit_excluded:) => QUERY
401  *
402  * Retrieve a query object for the query string 'query'. When using keyword
403  * arguments they are passwed to the query object.
404  */
405 VALUE
406 notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self)
407 {
408     VALUE qstrv;
409     VALUE opts;
410     const char *qstr;
411     notmuch_query_t *query;
412     notmuch_database_t *db;
413
414     rb_scan_args (argc, argv, "1:", &qstrv, &opts);
415
416     Data_Get_Notmuch_Database (self, db);
417
418     SafeStringValue (qstrv);
419     qstr = RSTRING_PTR (qstrv);
420
421     query = notmuch_query_create (db, qstr);
422     if (!query)
423         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
424
425     if (!NIL_P (opts)) {
426         VALUE sort, exclude_tags, omit_excluded;
427         VALUE kwargs[3];
428         static ID keyword_ids[3];
429
430         if (!keyword_ids[0]) {
431             keyword_ids[0] = rb_intern_const ("sort");
432             keyword_ids[1] = rb_intern_const ("exclude_tags");
433             keyword_ids[2] = rb_intern_const ("omit_excluded");
434         }
435
436         rb_get_kwargs (opts, keyword_ids, 0, 3, kwargs);
437
438         sort = kwargs[0];
439         exclude_tags = kwargs[1];
440         omit_excluded = kwargs[2];
441
442         if (sort != Qundef)
443             notmuch_query_set_sort (query, FIX2UINT (sort));
444
445         if (exclude_tags != Qundef) {
446             for (int i = 0; i < RARRAY_LEN (exclude_tags); i++) {
447                 VALUE e = RARRAY_AREF (exclude_tags, i);
448                 notmuch_query_add_tag_exclude (query, RSTRING_PTR (e));
449             }
450         }
451
452         if (omit_excluded != Qundef) {
453             notmuch_exclude_t omit;
454             omit = FIXNUM_P (omit_excluded) ? FIX2UINT (omit_excluded) : RTEST(omit_excluded);
455             notmuch_query_set_omit_excluded (query, omit);
456         }
457     }
458
459     return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, &notmuch_rb_query_type, query);
460 }