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