]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/database.c
ruby: be consistent with notmuch's coding style
[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 #if !defined(RSTRING_PTR)
47 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
48 #endif /* !defined(RSTRING_PTR) */
49
50     /* Check arguments */
51     rb_scan_args (argc, argv, "11", &pathv, &hashv);
52
53     SafeStringValue (pathv);
54     path = RSTRING_PTR (pathv);
55
56     if (!NIL_P (hashv)) {
57         Check_Type (hashv, T_HASH);
58         create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create)));
59         modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode));
60         if (NIL_P (modev))
61             mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
62         else if (!FIXNUM_P (modev))
63             rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
64         else {
65             mode = FIX2INT (modev);
66             switch (mode) {
67             case NOTMUCH_DATABASE_MODE_READ_ONLY:
68             case NOTMUCH_DATABASE_MODE_READ_WRITE:
69                 break;
70             default:
71                 rb_raise ( rb_eTypeError, "Invalid mode");
72             }
73         }
74     } else {
75         create = 0;
76         mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
77     }
78
79     Check_Type (self, T_DATA);
80     DATA_PTR (self) = create ? notmuch_database_create (path) : notmuch_database_open (path, mode);
81     if (!DATA_PTR (self))
82         rb_raise (notmuch_rb_eDatabaseError, "Failed to open database");
83
84     return self;
85 }
86
87 /*
88  * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
89  *
90  * Identical to new, except that when it is called with a block, it yields with
91  * the new instance and closes it, and returns the result which is returned from
92  * the block.
93  */
94 VALUE
95 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
96 {
97     VALUE obj;
98
99     obj = rb_class_new_instance (argc, argv, klass);
100     if (!rb_block_given_p ())
101         return obj;
102
103     return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
104 }
105
106 /*
107  * call-seq: DB.close => nil
108  *
109  * Close the notmuch database.
110  */
111 VALUE
112 notmuch_rb_database_close (VALUE self)
113 {
114     notmuch_database_t *db;
115
116     Data_Get_Notmuch_Database (self, db);
117     notmuch_database_close (db);
118     DATA_PTR (self) = NULL;
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_directory_t *dir;
254     notmuch_database_t *db;
255
256     Data_Get_Notmuch_Database (self, db);
257
258 #if !defined(RSTRING_PTR)
259 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
260 #endif /* !defined(RSTRING_PTR) */
261
262     SafeStringValue (pathv);
263     path = RSTRING_PTR (pathv);
264
265     dir = notmuch_database_get_directory (db, path);
266     if (!dir)
267         rb_raise (notmuch_rb_eXapianError, "Xapian exception");
268
269     return Data_Wrap_Struct (notmuch_rb_cDirectory, NULL, NULL, dir);
270 }
271
272 /*
273  * call-seq: DB.add_message(path) => MESSAGE, isdup
274  *
275  * Add a message to the database and return it.
276  *
277  * +isdup+ is a boolean that specifies whether the added message was a
278  * duplicate.
279  */
280 VALUE
281 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
282 {
283     const char *path;
284     notmuch_status_t ret;
285     notmuch_message_t *message;
286     notmuch_database_t *db;
287
288     Data_Get_Notmuch_Database (self, db);
289
290 #if !defined(RSTRING_PTR)
291 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
292 #endif /* !defined(RSTRING_PTR) */
293
294     SafeStringValue (pathv);
295     path = RSTRING_PTR (pathv);
296
297     ret = notmuch_database_add_message (db, path, &message);
298     notmuch_rb_status_raise (ret);
299     return rb_assoc_new (Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message),
300         (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
301 }
302
303 /*
304  * call-seq: DB.remove_message (path) => isdup
305  *
306  * Remove a message from the database.
307  *
308  * +isdup+ is a boolean that specifies whether the removed message was a
309  * duplicate.
310  */
311 VALUE
312 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
313 {
314     const char *path;
315     notmuch_status_t ret;
316     notmuch_database_t *db;
317
318     Data_Get_Notmuch_Database (self, db);
319
320 #if !defined(RSTRING_PTR)
321 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
322 #endif /* !defined (RSTRING_PTR) */
323
324     SafeStringValue (pathv);
325     path = RSTRING_PTR (pathv);
326
327     ret = notmuch_database_remove_message (db, path);
328     notmuch_rb_status_raise (ret);
329     return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
330 }
331
332 /*
333  * call-seq: DB.find_message(id) => MESSAGE or nil
334  *
335  * Find a message by message id.
336  */
337 VALUE
338 notmuch_rb_database_find_message (VALUE self, VALUE idv)
339 {
340     const char *id;
341     notmuch_status_t ret;
342     notmuch_database_t *db;
343     notmuch_message_t *message;
344
345     Data_Get_Notmuch_Database (self, db);
346
347 #if !defined(RSTRING_PTR)
348 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
349 #endif /* !defined (RSTRING_PTR) */
350
351     SafeStringValue (idv);
352     id = RSTRING_PTR (idv);
353
354     ret = notmuch_database_find_message (db, id, &message);
355     notmuch_rb_status_raise (ret);
356
357     if (message)
358         return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
359     return Qnil;
360 }
361
362 /*
363  * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
364  *
365  * Find a message by filename.
366  */
367 VALUE
368 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
369 {
370     const char *path;
371     notmuch_status_t ret;
372     notmuch_database_t *db;
373     notmuch_message_t *message;
374
375     Data_Get_Notmuch_Database (self, db);
376
377 #if !defined(RSTRING_PTR)
378 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
379 #endif /* !defined (RSTRING_PTR) */
380
381     SafeStringValue (pathv);
382     path = RSTRING_PTR (pathv);
383
384     ret = notmuch_database_find_message_by_filename (db, path, &message);
385     notmuch_rb_status_raise (ret);
386
387     if (message)
388         return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
389     return Qnil;
390 }
391
392 /*
393  * call-seq: DB.query(query) => QUERY
394  *
395  * Retrieve a query object for the query string 'query'
396  */
397 VALUE
398 notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
399 {
400     const char *qstr;
401     notmuch_query_t *query;
402     notmuch_database_t *db;
403
404     Data_Get_Notmuch_Database (self, db);
405
406 #if !defined(RSTRING_PTR)
407 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
408 #endif /* !defined (RSTRING_PTR) */
409
410     SafeStringValue (qstrv);
411     qstr = RSTRING_PTR (qstrv);
412
413     query = notmuch_query_create (db, qstr);
414     if (!query)
415         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
416
417     return Data_Wrap_Struct (notmuch_rb_cQuery, NULL, NULL, query);
418 }