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