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, "02", &pathv, &hashv);
65 SafeStringValue (pathv);
66 path = RSTRING_PTR (pathv);
72 Check_Type (hashv, T_HASH);
73 create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create)));
74 modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode));
76 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
77 else if (!FIXNUM_P (modev))
78 rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
80 mode = FIX2INT (modev);
82 case NOTMUCH_DATABASE_MODE_READ_ONLY:
83 case NOTMUCH_DATABASE_MODE_READ_WRITE:
86 rb_raise ( rb_eTypeError, "Invalid mode");
91 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
94 rb_check_typeddata (self, ¬much_rb_database_type);
96 ret = notmuch_database_create (path, &database);
98 ret = notmuch_database_open_with_config (path, mode, NULL, NULL, &database, NULL);
99 notmuch_rb_status_raise (ret);
101 DATA_PTR (self) = notmuch_rb_object_create (database, "notmuch_rb_database");
107 * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
109 * Identical to new, except that when it is called with a block, it yields with
110 * the new instance and closes it, and returns the result which is returned from
114 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
118 obj = rb_class_new_instance (argc, argv, klass);
119 if (!rb_block_given_p ())
122 return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
126 * call-seq: DB.close => nil
128 * Close the notmuch database.
131 notmuch_rb_database_close (VALUE self)
133 notmuch_database_t *db;
134 notmuch_status_t ret;
136 Data_Get_Notmuch_Database (self, db);
138 ret = notmuch_database_close (db);
139 notmuch_rb_status_raise (ret);
145 * call-seq: DB.path => String
147 * Return the path of the database
150 notmuch_rb_database_path (VALUE self)
152 notmuch_database_t *db;
154 Data_Get_Notmuch_Database (self, db);
156 return rb_str_new2 (notmuch_database_get_path (db));
160 * call-seq: DB.version => Fixnum
162 * Return the version of the database
165 notmuch_rb_database_version (VALUE self)
167 notmuch_database_t *db;
169 Data_Get_Notmuch_Database (self, db);
171 return INT2FIX (notmuch_database_get_version (db));
175 * call-seq: DB.needs_upgrade? => true or false
177 * Return the +true+ if the database needs upgrading, +false+ otherwise
180 notmuch_rb_database_needs_upgrade (VALUE self)
182 notmuch_database_t *db;
184 Data_Get_Notmuch_Database (self, db);
186 return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
190 notmuch_rb_upgrade_notify (void *closure, double progress)
192 VALUE *block = (VALUE *) closure;
193 rb_funcall (*block, ID_call, 1, rb_float_new (progress));
197 * call-seq: DB.upgrade! [{|progress| block }] => nil
199 * Upgrade the database.
201 * If a block is given the block is called with a progress indicator as a
202 * floating point value in the range of [0.0..1.0].
205 notmuch_rb_database_upgrade (VALUE self)
207 notmuch_status_t ret;
208 void (*pnotify) (void *closure, double progress);
209 notmuch_database_t *db;
212 Data_Get_Notmuch_Database (self, db);
214 if (rb_block_given_p ()) {
215 pnotify = notmuch_rb_upgrade_notify;
216 block = rb_block_proc ();
221 ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
222 notmuch_rb_status_raise (ret);
228 * call-seq: DB.begin_atomic => nil
230 * Begin an atomic database operation.
233 notmuch_rb_database_begin_atomic (VALUE self)
235 notmuch_status_t ret;
236 notmuch_database_t *db;
238 Data_Get_Notmuch_Database (self, db);
240 ret = notmuch_database_begin_atomic (db);
241 notmuch_rb_status_raise (ret);
247 * call-seq: DB.end_atomic => nil
249 * Indicate the end of an atomic database operation.
252 notmuch_rb_database_end_atomic (VALUE self)
254 notmuch_status_t ret;
255 notmuch_database_t *db;
257 Data_Get_Notmuch_Database (self, db);
259 ret = notmuch_database_end_atomic (db);
260 notmuch_rb_status_raise (ret);
266 * call-seq: DB.get_directory(path) => DIR
268 * Retrieve a directory object from the database for 'path'
271 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
274 notmuch_status_t ret;
275 notmuch_directory_t *dir;
276 notmuch_database_t *db;
278 Data_Get_Notmuch_Database (self, db);
280 SafeStringValue (pathv);
281 path = RSTRING_PTR (pathv);
283 ret = notmuch_database_get_directory (db, path, &dir);
284 notmuch_rb_status_raise (ret);
286 return Data_Wrap_Notmuch_Object (notmuch_rb_cDirectory, ¬much_rb_directory_type, dir);
291 * call-seq: DB.add_message(path) => MESSAGE, isdup
293 * Add a message to the database and return it.
295 * +isdup+ is a boolean that specifies whether the added message was a
299 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
302 notmuch_status_t ret;
303 notmuch_message_t *message;
304 notmuch_database_t *db;
306 Data_Get_Notmuch_Database (self, db);
308 SafeStringValue (pathv);
309 path = RSTRING_PTR (pathv);
311 ret = notmuch_database_index_file (db, path, NULL, &message);
312 notmuch_rb_status_raise (ret);
313 return rb_assoc_new (Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, ¬much_rb_message_type, message),
314 (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
318 * call-seq: DB.remove_message (path) => isdup
320 * Remove a message from the database.
322 * +isdup+ is a boolean that specifies whether the removed message was a
326 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
329 notmuch_status_t ret;
330 notmuch_database_t *db;
332 Data_Get_Notmuch_Database (self, db);
334 SafeStringValue (pathv);
335 path = RSTRING_PTR (pathv);
337 ret = notmuch_database_remove_message (db, path);
338 notmuch_rb_status_raise (ret);
339 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
343 * call-seq: DB.find_message(id) => MESSAGE or nil
345 * Find a message by message id.
348 notmuch_rb_database_find_message (VALUE self, VALUE idv)
351 notmuch_status_t ret;
352 notmuch_database_t *db;
353 notmuch_message_t *message;
355 Data_Get_Notmuch_Database (self, db);
357 SafeStringValue (idv);
358 id = RSTRING_PTR (idv);
360 ret = notmuch_database_find_message (db, id, &message);
361 notmuch_rb_status_raise (ret);
364 return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, ¬much_rb_message_type, message);
369 * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
371 * Find a message by filename.
374 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
377 notmuch_status_t ret;
378 notmuch_database_t *db;
379 notmuch_message_t *message;
381 Data_Get_Notmuch_Database (self, db);
383 SafeStringValue (pathv);
384 path = RSTRING_PTR (pathv);
386 ret = notmuch_database_find_message_by_filename (db, path, &message);
387 notmuch_rb_status_raise (ret);
390 return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, ¬much_rb_message_type, message);
395 * call-seq: DB.get_all_tags() => TAGS
397 * Returns a list of all tags found in the database.
400 notmuch_rb_database_get_all_tags (VALUE self)
402 notmuch_database_t *db;
403 notmuch_tags_t *tags;
405 Data_Get_Notmuch_Database (self, db);
407 tags = notmuch_database_get_all_tags (db);
409 const char *msg = notmuch_database_status_string (db);
411 msg = "Unknown notmuch error";
413 rb_raise (notmuch_rb_eBaseError, "%s", msg);
415 return notmuch_rb_tags_get (tags);
420 * DB.query(query) => QUERY
421 * DB.query(query, sort:, excluded_tags:, omit_excluded:) => QUERY
423 * Retrieve a query object for the query string 'query'. When using keyword
424 * arguments they are passwed to the query object.
427 notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self)
432 notmuch_query_t *query;
433 notmuch_database_t *db;
435 rb_scan_args (argc, argv, "1:", &qstrv, &opts);
437 Data_Get_Notmuch_Database (self, db);
439 SafeStringValue (qstrv);
440 qstr = RSTRING_PTR (qstrv);
442 query = notmuch_query_create (db, qstr);
444 rb_raise (notmuch_rb_eMemoryError, "Out of memory");
447 VALUE sort, exclude_tags, omit_excluded;
449 static ID keyword_ids[3];
451 if (!keyword_ids[0]) {
452 keyword_ids[0] = rb_intern_const ("sort");
453 keyword_ids[1] = rb_intern_const ("exclude_tags");
454 keyword_ids[2] = rb_intern_const ("omit_excluded");
457 rb_get_kwargs (opts, keyword_ids, 0, 3, kwargs);
460 exclude_tags = kwargs[1];
461 omit_excluded = kwargs[2];
464 notmuch_query_set_sort (query, FIX2UINT (sort));
466 if (exclude_tags != Qundef) {
467 for (int i = 0; i < RARRAY_LEN (exclude_tags); i++) {
468 VALUE e = RARRAY_AREF (exclude_tags, i);
469 notmuch_query_add_tag_exclude (query, RSTRING_PTR (e));
473 if (omit_excluded != Qundef) {
474 notmuch_exclude_t omit;
475 omit = FIXNUM_P (omit_excluded) ? FIX2UINT (omit_excluded) : RTEST(omit_excluded);
476 notmuch_query_set_omit_excluded (query, omit);
480 return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, ¬much_rb_query_type, query);