]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/database.c
database: add n_d_index_file (deprecates n_d_add_message)
[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_Struct (klass, NULL, NULL, 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     Check_Type (self, T_DATA);
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) = 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     notmuch_database_t *db;
118
119     Data_Get_Notmuch_Database (self, db);
120     ret = notmuch_database_destroy (db);
121     DATA_PTR (self) = NULL;
122     notmuch_rb_status_raise (ret);
123
124     return Qnil;
125 }
126
127 /*
128  * call-seq: DB.path => String
129  *
130  * Return the path of the database
131  */
132 VALUE
133 notmuch_rb_database_path (VALUE self)
134 {
135     notmuch_database_t *db;
136
137     Data_Get_Notmuch_Database (self, db);
138
139     return rb_str_new2 (notmuch_database_get_path (db));
140 }
141
142 /*
143  * call-seq: DB.version => Fixnum
144  *
145  * Return the version of the database
146  */
147 VALUE
148 notmuch_rb_database_version (VALUE self)
149 {
150     notmuch_database_t *db;
151
152     Data_Get_Notmuch_Database (self, db);
153
154     return INT2FIX (notmuch_database_get_version (db));
155 }
156
157 /*
158  * call-seq: DB.needs_upgrade? => true or false
159  *
160  * Return the +true+ if the database needs upgrading, +false+ otherwise
161  */
162 VALUE
163 notmuch_rb_database_needs_upgrade (VALUE self)
164 {
165     notmuch_database_t *db;
166
167     Data_Get_Notmuch_Database (self, db);
168
169     return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
170 }
171
172 static void
173 notmuch_rb_upgrade_notify (void *closure, double progress)
174 {
175     VALUE *block = (VALUE *) closure;
176     rb_funcall (*block, ID_call, 1, rb_float_new (progress));
177 }
178
179 /*
180  * call-seq: DB.upgrade! [{|progress| block }] => nil
181  *
182  * Upgrade the database.
183  *
184  * If a block is given the block is called with a progress indicator as a
185  * floating point value in the range of [0.0..1.0].
186  */
187 VALUE
188 notmuch_rb_database_upgrade (VALUE self)
189 {
190     notmuch_status_t ret;
191     void (*pnotify) (void *closure, double progress);
192     notmuch_database_t *db;
193     VALUE block;
194
195     Data_Get_Notmuch_Database (self, db);
196
197     if (rb_block_given_p ()) {
198         pnotify = notmuch_rb_upgrade_notify;
199         block = rb_block_proc ();
200     }
201     else
202         pnotify = NULL;
203
204     ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
205     notmuch_rb_status_raise (ret);
206
207     return Qtrue;
208 }
209
210 /*
211  * call-seq: DB.begin_atomic => nil
212  *
213  * Begin an atomic database operation.
214  */
215 VALUE
216 notmuch_rb_database_begin_atomic (VALUE self)
217 {
218     notmuch_status_t ret;
219     notmuch_database_t *db;
220
221     Data_Get_Notmuch_Database (self, db);
222
223     ret = notmuch_database_begin_atomic (db);
224     notmuch_rb_status_raise (ret);
225
226     return Qtrue;
227 }
228
229 /*
230  * call-seq: DB.end_atomic => nil
231  *
232  * Indicate the end of an atomic database operation.
233  */
234 VALUE
235 notmuch_rb_database_end_atomic (VALUE self)
236 {
237     notmuch_status_t ret;
238     notmuch_database_t *db;
239
240     Data_Get_Notmuch_Database (self, db);
241
242     ret = notmuch_database_end_atomic (db);
243     notmuch_rb_status_raise (ret);
244
245     return Qtrue;
246 }
247
248 /*
249  * call-seq: DB.get_directory(path) => DIR
250  *
251  * Retrieve a directory object from the database for 'path'
252  */
253 VALUE
254 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
255 {
256     const char *path;
257     notmuch_status_t ret;
258     notmuch_directory_t *dir;
259     notmuch_database_t *db;
260
261     Data_Get_Notmuch_Database (self, db);
262
263     SafeStringValue (pathv);
264     path = RSTRING_PTR (pathv);
265
266     ret = notmuch_database_get_directory (db, path, &dir);
267     notmuch_rb_status_raise (ret);
268     if (dir)
269         return Data_Wrap_Struct (notmuch_rb_cDirectory, NULL, NULL, dir);
270     return Qnil;
271 }
272
273 /*
274  * call-seq: DB.add_message(path) => MESSAGE, isdup
275  *
276  * Add a message to the database and return it.
277  *
278  * +isdup+ is a boolean that specifies whether the added message was a
279  * duplicate.
280  */
281 VALUE
282 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
283 {
284     const char *path;
285     notmuch_status_t ret;
286     notmuch_message_t *message;
287     notmuch_database_t *db;
288
289     Data_Get_Notmuch_Database (self, db);
290
291     SafeStringValue (pathv);
292     path = RSTRING_PTR (pathv);
293
294     ret = notmuch_database_index_file (db, path, NULL, &message);
295     notmuch_rb_status_raise (ret);
296     return rb_assoc_new (Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message),
297         (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
298 }
299
300 /*
301  * call-seq: DB.remove_message (path) => isdup
302  *
303  * Remove a message from the database.
304  *
305  * +isdup+ is a boolean that specifies whether the removed message was a
306  * duplicate.
307  */
308 VALUE
309 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
310 {
311     const char *path;
312     notmuch_status_t ret;
313     notmuch_database_t *db;
314
315     Data_Get_Notmuch_Database (self, db);
316
317     SafeStringValue (pathv);
318     path = RSTRING_PTR (pathv);
319
320     ret = notmuch_database_remove_message (db, path);
321     notmuch_rb_status_raise (ret);
322     return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
323 }
324
325 /*
326  * call-seq: DB.find_message(id) => MESSAGE or nil
327  *
328  * Find a message by message id.
329  */
330 VALUE
331 notmuch_rb_database_find_message (VALUE self, VALUE idv)
332 {
333     const char *id;
334     notmuch_status_t ret;
335     notmuch_database_t *db;
336     notmuch_message_t *message;
337
338     Data_Get_Notmuch_Database (self, db);
339
340     SafeStringValue (idv);
341     id = RSTRING_PTR (idv);
342
343     ret = notmuch_database_find_message (db, id, &message);
344     notmuch_rb_status_raise (ret);
345
346     if (message)
347         return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
348     return Qnil;
349 }
350
351 /*
352  * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
353  *
354  * Find a message by filename.
355  */
356 VALUE
357 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
358 {
359     const char *path;
360     notmuch_status_t ret;
361     notmuch_database_t *db;
362     notmuch_message_t *message;
363
364     Data_Get_Notmuch_Database (self, db);
365
366     SafeStringValue (pathv);
367     path = RSTRING_PTR (pathv);
368
369     ret = notmuch_database_find_message_by_filename (db, path, &message);
370     notmuch_rb_status_raise (ret);
371
372     if (message)
373         return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
374     return Qnil;
375 }
376
377 /*
378  * call-seq: DB.get_all_tags() => TAGS
379  *
380  * Returns a list of all tags found in the database.
381  */
382 VALUE
383 notmuch_rb_database_get_all_tags (VALUE self)
384 {
385     notmuch_database_t *db;
386     notmuch_tags_t *tags;
387
388     Data_Get_Notmuch_Database (self, db);
389
390     tags = notmuch_database_get_all_tags (db);
391     if (!tags) {
392         const char *msg = notmuch_database_status_string (db);
393         if (!msg)
394             msg = "Unknown notmuch error";
395
396         rb_raise (notmuch_rb_eBaseError, "%s", msg);
397     }
398     return Data_Wrap_Struct (notmuch_rb_cTags, NULL, NULL, tags);
399 }
400
401 /*
402  * call-seq: DB.query(query) => QUERY
403  *
404  * Retrieve a query object for the query string 'query'
405  */
406 VALUE
407 notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
408 {
409     const char *qstr;
410     notmuch_query_t *query;
411     notmuch_database_t *db;
412
413     Data_Get_Notmuch_Database (self, db);
414
415     SafeStringValue (qstrv);
416     qstr = RSTRING_PTR (qstrv);
417
418     query = notmuch_query_create (db, qstr);
419     if (!query)
420         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
421
422     return Data_Wrap_Struct (notmuch_rb_cQuery, NULL, NULL, query);
423 }