]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/database.c
ba9a1391a50918f25ca2d1bc5921c4907666e11b
[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 http://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
46     /* Check arguments */
47     rb_scan_args (argc, argv, "11", &pathv, &hashv);
48
49     SafeStringValue (pathv);
50     path = RSTRING_PTR (pathv);
51
52     if (!NIL_P (hashv)) {
53         Check_Type (hashv, T_HASH);
54         create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create)));
55         modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode));
56         if (NIL_P (modev))
57             mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
58         else if (!FIXNUM_P (modev))
59             rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
60         else {
61             mode = FIX2INT (modev);
62             switch (mode) {
63             case NOTMUCH_DATABASE_MODE_READ_ONLY:
64             case NOTMUCH_DATABASE_MODE_READ_WRITE:
65                 break;
66             default:
67                 rb_raise ( rb_eTypeError, "Invalid mode");
68             }
69         }
70     } else {
71         create = 0;
72         mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
73     }
74
75     Check_Type (self, T_DATA);
76     DATA_PTR (self) = create ? notmuch_database_create (path) : notmuch_database_open (path, mode);
77     if (!DATA_PTR (self))
78         rb_raise (notmuch_rb_eDatabaseError, "Failed to open database");
79
80     return self;
81 }
82
83 /*
84  * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
85  *
86  * Identical to new, except that when it is called with a block, it yields with
87  * the new instance and closes it, and returns the result which is returned from
88  * the block.
89  */
90 VALUE
91 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
92 {
93     VALUE obj;
94
95     obj = rb_class_new_instance (argc, argv, klass);
96     if (!rb_block_given_p ())
97         return obj;
98
99     return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
100 }
101
102 /*
103  * call-seq: DB.close => nil
104  *
105  * Close the notmuch database.
106  */
107 VALUE
108 notmuch_rb_database_close (VALUE self)
109 {
110     notmuch_database_t *db;
111
112     Data_Get_Notmuch_Database (self, db);
113     notmuch_database_destroy (db);
114     DATA_PTR (self) = NULL;
115
116     return Qnil;
117 }
118
119 /*
120  * call-seq: DB.path => String
121  *
122  * Return the path of the database
123  */
124 VALUE
125 notmuch_rb_database_path (VALUE self)
126 {
127     notmuch_database_t *db;
128
129     Data_Get_Notmuch_Database (self, db);
130
131     return rb_str_new2 (notmuch_database_get_path (db));
132 }
133
134 /*
135  * call-seq: DB.version => Fixnum
136  *
137  * Return the version of the database
138  */
139 VALUE
140 notmuch_rb_database_version (VALUE self)
141 {
142     notmuch_database_t *db;
143
144     Data_Get_Notmuch_Database (self, db);
145
146     return INT2FIX (notmuch_database_get_version (db));
147 }
148
149 /*
150  * call-seq: DB.needs_upgrade? => true or false
151  *
152  * Return the +true+ if the database needs upgrading, +false+ otherwise
153  */
154 VALUE
155 notmuch_rb_database_needs_upgrade (VALUE self)
156 {
157     notmuch_database_t *db;
158
159     Data_Get_Notmuch_Database (self, db);
160
161     return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
162 }
163
164 static void
165 notmuch_rb_upgrade_notify (void *closure, double progress)
166 {
167     VALUE *block = (VALUE *) closure;
168     rb_funcall (*block, ID_call, 1, rb_float_new (progress));
169 }
170
171 /*
172  * call-seq: DB.upgrade! [{|progress| block }] => nil
173  *
174  * Upgrade the database.
175  *
176  * If a block is given the block is called with a progress indicator as a
177  * floating point value in the range of [0.0..1.0].
178  */
179 VALUE
180 notmuch_rb_database_upgrade (VALUE self)
181 {
182     notmuch_status_t ret;
183     void (*pnotify) (void *closure, double progress);
184     notmuch_database_t *db;
185     VALUE block;
186
187     Data_Get_Notmuch_Database (self, db);
188
189     if (rb_block_given_p ()) {
190         pnotify = notmuch_rb_upgrade_notify;
191         block = rb_block_proc ();
192     }
193     else
194         pnotify = NULL;
195
196     ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
197     notmuch_rb_status_raise (ret);
198
199     return Qtrue;
200 }
201
202 /*
203  * call-seq: DB.begin_atomic => nil
204  *
205  * Begin an atomic database operation.
206  */
207 VALUE
208 notmuch_rb_database_begin_atomic (VALUE self)
209 {
210     notmuch_status_t ret;
211     notmuch_database_t *db;
212
213     Data_Get_Notmuch_Database (self, db);
214
215     ret = notmuch_database_begin_atomic (db);
216     notmuch_rb_status_raise (ret);
217
218     return Qtrue;
219 }
220
221 /*
222  * call-seq: DB.end_atomic => nil
223  *
224  * Indicate the end of an atomic database operation.
225  */
226 VALUE
227 notmuch_rb_database_end_atomic (VALUE self)
228 {
229     notmuch_status_t ret;
230     notmuch_database_t *db;
231
232     Data_Get_Notmuch_Database (self, db);
233
234     ret = notmuch_database_end_atomic (db);
235     notmuch_rb_status_raise (ret);
236
237     return Qtrue;
238 }
239
240 /*
241  * call-seq: DB.get_directory(path) => DIR
242  *
243  * Retrieve a directory object from the database for 'path'
244  */
245 VALUE
246 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
247 {
248     const char *path;
249     notmuch_directory_t *dir;
250     notmuch_database_t *db;
251
252     Data_Get_Notmuch_Database (self, db);
253
254     SafeStringValue (pathv);
255     path = RSTRING_PTR (pathv);
256
257     dir = notmuch_database_get_directory (db, path);
258     if (!dir)
259         rb_raise (notmuch_rb_eXapianError, "Xapian exception");
260
261     return Data_Wrap_Struct (notmuch_rb_cDirectory, NULL, NULL, dir);
262 }
263
264 /*
265  * call-seq: DB.add_message(path) => MESSAGE, isdup
266  *
267  * Add a message to the database and return it.
268  *
269  * +isdup+ is a boolean that specifies whether the added message was a
270  * duplicate.
271  */
272 VALUE
273 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
274 {
275     const char *path;
276     notmuch_status_t ret;
277     notmuch_message_t *message;
278     notmuch_database_t *db;
279
280     Data_Get_Notmuch_Database (self, db);
281
282     SafeStringValue (pathv);
283     path = RSTRING_PTR (pathv);
284
285     ret = notmuch_database_add_message (db, path, &message);
286     notmuch_rb_status_raise (ret);
287     return rb_assoc_new (Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message),
288         (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
289 }
290
291 /*
292  * call-seq: DB.remove_message (path) => isdup
293  *
294  * Remove a message from the database.
295  *
296  * +isdup+ is a boolean that specifies whether the removed message was a
297  * duplicate.
298  */
299 VALUE
300 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
301 {
302     const char *path;
303     notmuch_status_t ret;
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_remove_message (db, path);
312     notmuch_rb_status_raise (ret);
313     return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
314 }
315
316 /*
317  * call-seq: DB.find_message(id) => MESSAGE or nil
318  *
319  * Find a message by message id.
320  */
321 VALUE
322 notmuch_rb_database_find_message (VALUE self, VALUE idv)
323 {
324     const char *id;
325     notmuch_status_t ret;
326     notmuch_database_t *db;
327     notmuch_message_t *message;
328
329     Data_Get_Notmuch_Database (self, db);
330
331     SafeStringValue (idv);
332     id = RSTRING_PTR (idv);
333
334     ret = notmuch_database_find_message (db, id, &message);
335     notmuch_rb_status_raise (ret);
336
337     if (message)
338         return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
339     return Qnil;
340 }
341
342 /*
343  * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
344  *
345  * Find a message by filename.
346  */
347 VALUE
348 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
349 {
350     const char *path;
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 (pathv);
358     path = RSTRING_PTR (pathv);
359
360     ret = notmuch_database_find_message_by_filename (db, path, &message);
361     notmuch_rb_status_raise (ret);
362
363     if (message)
364         return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
365     return Qnil;
366 }
367
368 /*
369  * call-seq: DB.query(query) => QUERY
370  *
371  * Retrieve a query object for the query string 'query'
372  */
373 VALUE
374 notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
375 {
376     const char *qstr;
377     notmuch_query_t *query;
378     notmuch_database_t *db;
379
380     Data_Get_Notmuch_Database (self, db);
381
382     SafeStringValue (qstrv);
383     qstr = RSTRING_PTR (qstrv);
384
385     query = notmuch_query_create (db, qstr);
386     if (!query)
387         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
388
389     return Data_Wrap_Struct (notmuch_rb_cQuery, NULL, NULL, query);
390 }