1 /* The Ruby interface to the notmuch mail library
3 * Copyright © 2010, 2011 Ali Polatel
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.
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.
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/ .
18 * Author: Ali Polatel <alip@exherbo.org>
24 notmuch_rb_database_alloc (VALUE klass)
26 return Data_Wrap_Notmuch_Object (klass, ¬much_rb_database_type, NULL);
30 * call-seq: DB.destroy => nil
32 * Destroys the database, freeing all resources allocated for it.
35 notmuch_rb_database_destroy (VALUE self)
37 notmuch_rb_object_destroy (self, ¬much_rb_database_type);
43 * call-seq: Notmuch::Database.new(path [, {:create => false, :mode => Notmuch::MODE_READ_ONLY}]) => DB
45 * Create or open a notmuch database using the given path.
47 * If :create is +true+, create the database instead of opening.
49 * The argument :mode specifies the open mode of the database.
52 notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
58 notmuch_database_t *database;
63 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
66 rb_scan_args (argc, argv, "02", &pathv, &hashv);
69 SafeStringValue (pathv);
70 path = RSTRING_PTR (pathv);
76 static ID keyword_ids[2];
78 if (!keyword_ids[0]) {
79 keyword_ids[0] = rb_intern_const ("mode");
80 keyword_ids[1] = rb_intern_const ("create");
83 rb_get_kwargs (hashv, keyword_ids, 0, 2, kwargs);
88 if (rmode != Qundef) {
89 if (!FIXNUM_P (rmode))
90 rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
92 mode = FIX2INT (rmode);
94 case NOTMUCH_DATABASE_MODE_READ_ONLY:
95 case NOTMUCH_DATABASE_MODE_READ_WRITE:
98 rb_raise ( rb_eTypeError, "Invalid mode");
102 if (rcreate != Qundef)
103 create = RTEST (rcreate);
106 rb_check_typeddata (self, ¬much_rb_database_type);
108 ret = notmuch_database_create (path, &database);
110 ret = notmuch_database_open_with_config (path, mode, NULL, NULL, &database, NULL);
111 notmuch_rb_status_raise (ret);
113 DATA_PTR (self) = notmuch_rb_object_create (database, "notmuch_rb_database");
119 * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
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
126 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
130 obj = rb_class_new_instance (argc, argv, klass);
131 if (!rb_block_given_p ())
134 return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
138 * call-seq: DB.close => nil
140 * Close the notmuch database.
143 notmuch_rb_database_close (VALUE self)
145 notmuch_database_t *db;
146 notmuch_status_t ret;
148 Data_Get_Notmuch_Database (self, db);
150 ret = notmuch_database_close (db);
151 notmuch_rb_status_raise (ret);
157 * call-seq: DB.path => String
159 * Return the path of the database
162 notmuch_rb_database_path (VALUE self)
164 notmuch_database_t *db;
166 Data_Get_Notmuch_Database (self, db);
168 return rb_str_new2 (notmuch_database_get_path (db));
172 * call-seq: DB.version => Fixnum
174 * Return the version of the database
177 notmuch_rb_database_version (VALUE self)
179 notmuch_database_t *db;
181 Data_Get_Notmuch_Database (self, db);
183 return INT2FIX (notmuch_database_get_version (db));
187 * call-seq: DB.needs_upgrade? => true or false
189 * Return the +true+ if the database needs upgrading, +false+ otherwise
192 notmuch_rb_database_needs_upgrade (VALUE self)
194 notmuch_database_t *db;
196 Data_Get_Notmuch_Database (self, db);
198 return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
202 notmuch_rb_upgrade_notify (void *closure, double progress)
204 VALUE *block = (VALUE *) closure;
205 rb_funcall (*block, ID_call, 1, rb_float_new (progress));
209 * call-seq: DB.upgrade! [{|progress| block }] => nil
211 * Upgrade the database.
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].
217 notmuch_rb_database_upgrade (VALUE self)
219 notmuch_status_t ret;
220 void (*pnotify) (void *closure, double progress);
221 notmuch_database_t *db;
224 Data_Get_Notmuch_Database (self, db);
226 if (rb_block_given_p ()) {
227 pnotify = notmuch_rb_upgrade_notify;
228 block = rb_block_proc ();
233 ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
234 notmuch_rb_status_raise (ret);
240 * call-seq: DB.begin_atomic => nil
242 * Begin an atomic database operation.
245 notmuch_rb_database_begin_atomic (VALUE self)
247 notmuch_status_t ret;
248 notmuch_database_t *db;
250 Data_Get_Notmuch_Database (self, db);
252 ret = notmuch_database_begin_atomic (db);
253 notmuch_rb_status_raise (ret);
259 * call-seq: DB.end_atomic => nil
261 * Indicate the end of an atomic database operation.
264 notmuch_rb_database_end_atomic (VALUE self)
266 notmuch_status_t ret;
267 notmuch_database_t *db;
269 Data_Get_Notmuch_Database (self, db);
271 ret = notmuch_database_end_atomic (db);
272 notmuch_rb_status_raise (ret);
278 * call-seq: DB.get_directory(path) => DIR
280 * Retrieve a directory object from the database for 'path'
283 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
286 notmuch_status_t ret;
287 notmuch_directory_t *dir;
288 notmuch_database_t *db;
290 Data_Get_Notmuch_Database (self, db);
292 SafeStringValue (pathv);
293 path = RSTRING_PTR (pathv);
295 ret = notmuch_database_get_directory (db, path, &dir);
296 notmuch_rb_status_raise (ret);
298 return Data_Wrap_Notmuch_Object (notmuch_rb_cDirectory, ¬much_rb_directory_type, dir);
303 * call-seq: DB.add_message(path) => MESSAGE, isdup
305 * Add a message to the database and return it.
307 * +isdup+ is a boolean that specifies whether the added message was a
311 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
314 notmuch_status_t ret;
315 notmuch_message_t *message;
316 notmuch_database_t *db;
318 Data_Get_Notmuch_Database (self, db);
320 SafeStringValue (pathv);
321 path = RSTRING_PTR (pathv);
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, ¬much_rb_message_type, message),
326 (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
330 * call-seq: DB.remove_message (path) => isdup
332 * Remove a message from the database.
334 * +isdup+ is a boolean that specifies whether the removed message was a
338 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
341 notmuch_status_t ret;
342 notmuch_database_t *db;
344 Data_Get_Notmuch_Database (self, db);
346 SafeStringValue (pathv);
347 path = RSTRING_PTR (pathv);
349 ret = notmuch_database_remove_message (db, path);
350 notmuch_rb_status_raise (ret);
351 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
355 * call-seq: DB.find_message(id) => MESSAGE or nil
357 * Find a message by message id.
360 notmuch_rb_database_find_message (VALUE self, VALUE idv)
363 notmuch_status_t ret;
364 notmuch_database_t *db;
365 notmuch_message_t *message;
367 Data_Get_Notmuch_Database (self, db);
369 SafeStringValue (idv);
370 id = RSTRING_PTR (idv);
372 ret = notmuch_database_find_message (db, id, &message);
373 notmuch_rb_status_raise (ret);
376 return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, ¬much_rb_message_type, message);
381 * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
383 * Find a message by filename.
386 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
389 notmuch_status_t ret;
390 notmuch_database_t *db;
391 notmuch_message_t *message;
393 Data_Get_Notmuch_Database (self, db);
395 SafeStringValue (pathv);
396 path = RSTRING_PTR (pathv);
398 ret = notmuch_database_find_message_by_filename (db, path, &message);
399 notmuch_rb_status_raise (ret);
402 return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, ¬much_rb_message_type, message);
407 * call-seq: DB.get_all_tags() => TAGS
409 * Returns a list of all tags found in the database.
412 notmuch_rb_database_get_all_tags (VALUE self)
414 notmuch_database_t *db;
415 notmuch_tags_t *tags;
417 Data_Get_Notmuch_Database (self, db);
419 tags = notmuch_database_get_all_tags (db);
421 const char *msg = notmuch_database_status_string (db);
423 msg = "Unknown notmuch error";
425 rb_raise (notmuch_rb_eBaseError, "%s", msg);
427 return notmuch_rb_tags_get (tags);
432 * DB.query(query) => QUERY
433 * DB.query(query, sort:, excluded_tags:, omit_excluded:) => QUERY
435 * Retrieve a query object for the query string 'query'. When using keyword
436 * arguments they are passwed to the query object.
439 notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self)
444 notmuch_query_t *query;
445 notmuch_database_t *db;
447 rb_scan_args (argc, argv, "1:", &qstrv, &opts);
449 Data_Get_Notmuch_Database (self, db);
451 SafeStringValue (qstrv);
452 qstr = RSTRING_PTR (qstrv);
454 query = notmuch_query_create (db, qstr);
456 rb_raise (notmuch_rb_eMemoryError, "Out of memory");
459 VALUE sort, exclude_tags, omit_excluded;
461 static ID keyword_ids[3];
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");
469 rb_get_kwargs (opts, keyword_ids, 0, 3, kwargs);
472 exclude_tags = kwargs[1];
473 omit_excluded = kwargs[2];
476 notmuch_query_set_sort (query, FIX2UINT (sort));
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));
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);
492 return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, ¬much_rb_query_type, query);