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;
62 rb_scan_args (argc, argv, "11", &pathv, &hashv);
64 SafeStringValue (pathv);
65 path = RSTRING_PTR (pathv);
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));
72 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
73 else if (!FIXNUM_P (modev))
74 rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
76 mode = FIX2INT (modev);
78 case NOTMUCH_DATABASE_MODE_READ_ONLY:
79 case NOTMUCH_DATABASE_MODE_READ_WRITE:
82 rb_raise ( rb_eTypeError, "Invalid mode");
87 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
90 rb_check_typeddata (self, ¬much_rb_database_type);
92 ret = notmuch_database_create (path, &database);
94 ret = notmuch_database_open_with_config (path, mode, NULL, NULL, &database, NULL);
95 notmuch_rb_status_raise (ret);
97 DATA_PTR (self) = notmuch_rb_object_create (database, "notmuch_rb_database");
103 * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
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
110 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
114 obj = rb_class_new_instance (argc, argv, klass);
115 if (!rb_block_given_p ())
118 return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
122 * call-seq: DB.close => nil
124 * Close the notmuch database.
127 notmuch_rb_database_close (VALUE self)
129 notmuch_database_t *db;
130 notmuch_status_t ret;
132 Data_Get_Notmuch_Database (self, db);
134 ret = notmuch_database_close (db);
135 notmuch_rb_status_raise (ret);
141 * call-seq: DB.path => String
143 * Return the path of the database
146 notmuch_rb_database_path (VALUE self)
148 notmuch_database_t *db;
150 Data_Get_Notmuch_Database (self, db);
152 return rb_str_new2 (notmuch_database_get_path (db));
156 * call-seq: DB.version => Fixnum
158 * Return the version of the database
161 notmuch_rb_database_version (VALUE self)
163 notmuch_database_t *db;
165 Data_Get_Notmuch_Database (self, db);
167 return INT2FIX (notmuch_database_get_version (db));
171 * call-seq: DB.needs_upgrade? => true or false
173 * Return the +true+ if the database needs upgrading, +false+ otherwise
176 notmuch_rb_database_needs_upgrade (VALUE self)
178 notmuch_database_t *db;
180 Data_Get_Notmuch_Database (self, db);
182 return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
186 notmuch_rb_upgrade_notify (void *closure, double progress)
188 VALUE *block = (VALUE *) closure;
189 rb_funcall (*block, ID_call, 1, rb_float_new (progress));
193 * call-seq: DB.upgrade! [{|progress| block }] => nil
195 * Upgrade the database.
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].
201 notmuch_rb_database_upgrade (VALUE self)
203 notmuch_status_t ret;
204 void (*pnotify) (void *closure, double progress);
205 notmuch_database_t *db;
208 Data_Get_Notmuch_Database (self, db);
210 if (rb_block_given_p ()) {
211 pnotify = notmuch_rb_upgrade_notify;
212 block = rb_block_proc ();
217 ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
218 notmuch_rb_status_raise (ret);
224 * call-seq: DB.begin_atomic => nil
226 * Begin an atomic database operation.
229 notmuch_rb_database_begin_atomic (VALUE self)
231 notmuch_status_t ret;
232 notmuch_database_t *db;
234 Data_Get_Notmuch_Database (self, db);
236 ret = notmuch_database_begin_atomic (db);
237 notmuch_rb_status_raise (ret);
243 * call-seq: DB.end_atomic => nil
245 * Indicate the end of an atomic database operation.
248 notmuch_rb_database_end_atomic (VALUE self)
250 notmuch_status_t ret;
251 notmuch_database_t *db;
253 Data_Get_Notmuch_Database (self, db);
255 ret = notmuch_database_end_atomic (db);
256 notmuch_rb_status_raise (ret);
262 * call-seq: DB.get_directory(path) => DIR
264 * Retrieve a directory object from the database for 'path'
267 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
270 notmuch_status_t ret;
271 notmuch_directory_t *dir;
272 notmuch_database_t *db;
274 Data_Get_Notmuch_Database (self, db);
276 SafeStringValue (pathv);
277 path = RSTRING_PTR (pathv);
279 ret = notmuch_database_get_directory (db, path, &dir);
280 notmuch_rb_status_raise (ret);
282 return Data_Wrap_Notmuch_Object (notmuch_rb_cDirectory, ¬much_rb_directory_type, dir);
287 * call-seq: DB.add_message(path) => MESSAGE, isdup
289 * Add a message to the database and return it.
291 * +isdup+ is a boolean that specifies whether the added message was a
295 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
298 notmuch_status_t ret;
299 notmuch_message_t *message;
300 notmuch_database_t *db;
302 Data_Get_Notmuch_Database (self, db);
304 SafeStringValue (pathv);
305 path = RSTRING_PTR (pathv);
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);
314 * call-seq: DB.remove_message (path) => isdup
316 * Remove a message from the database.
318 * +isdup+ is a boolean that specifies whether the removed message was a
322 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
325 notmuch_status_t ret;
326 notmuch_database_t *db;
328 Data_Get_Notmuch_Database (self, db);
330 SafeStringValue (pathv);
331 path = RSTRING_PTR (pathv);
333 ret = notmuch_database_remove_message (db, path);
334 notmuch_rb_status_raise (ret);
335 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
339 * call-seq: DB.find_message(id) => MESSAGE or nil
341 * Find a message by message id.
344 notmuch_rb_database_find_message (VALUE self, VALUE idv)
347 notmuch_status_t ret;
348 notmuch_database_t *db;
349 notmuch_message_t *message;
351 Data_Get_Notmuch_Database (self, db);
353 SafeStringValue (idv);
354 id = RSTRING_PTR (idv);
356 ret = notmuch_database_find_message (db, id, &message);
357 notmuch_rb_status_raise (ret);
360 return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, ¬much_rb_message_type, message);
365 * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
367 * Find a message by filename.
370 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
373 notmuch_status_t ret;
374 notmuch_database_t *db;
375 notmuch_message_t *message;
377 Data_Get_Notmuch_Database (self, db);
379 SafeStringValue (pathv);
380 path = RSTRING_PTR (pathv);
382 ret = notmuch_database_find_message_by_filename (db, path, &message);
383 notmuch_rb_status_raise (ret);
386 return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, ¬much_rb_message_type, message);
391 * call-seq: DB.get_all_tags() => TAGS
393 * Returns a list of all tags found in the database.
396 notmuch_rb_database_get_all_tags (VALUE self)
398 notmuch_database_t *db;
399 notmuch_tags_t *tags;
401 Data_Get_Notmuch_Database (self, db);
403 tags = notmuch_database_get_all_tags (db);
405 const char *msg = notmuch_database_status_string (db);
407 msg = "Unknown notmuch error";
409 rb_raise (notmuch_rb_eBaseError, "%s", msg);
411 return Data_Wrap_Notmuch_Object (notmuch_rb_cTags, ¬much_rb_tags_type, tags);
416 * DB.query(query) => QUERY
417 * DB.query(query, sort:, excluded_tags:, omit_excluded:) => QUERY
419 * Retrieve a query object for the query string 'query'. When using keyword
420 * arguments they are passwed to the query object.
423 notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self)
428 notmuch_query_t *query;
429 notmuch_database_t *db;
431 rb_scan_args (argc, argv, "1:", &qstrv, &opts);
433 Data_Get_Notmuch_Database (self, db);
435 SafeStringValue (qstrv);
436 qstr = RSTRING_PTR (qstrv);
438 query = notmuch_query_create (db, qstr);
440 rb_raise (notmuch_rb_eMemoryError, "Out of memory");
443 VALUE sort, exclude_tags, omit_excluded;
445 static ID keyword_ids[3];
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");
453 rb_get_kwargs (opts, keyword_ids, 0, 3, kwargs);
456 exclude_tags = kwargs[1];
457 omit_excluded = kwargs[2];
460 notmuch_query_set_sort (query, FIX2UINT (sort));
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));
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);
476 return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, ¬much_rb_query_type, query);