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