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 http://www.gnu.org/licenses/ .
18 * Author: Ali Polatel <alip@exherbo.org>
24 notmuch_rb_database_alloc (VALUE klass)
26 return Data_Wrap_Struct (klass, NULL, NULL, NULL);
30 * call-seq: Notmuch::Database.new(path [, {:create => false, :mode => Notmuch::MODE_READ_ONLY}]) => DB
32 * Create or open a notmuch database using the given path.
34 * If :create is +true+, create the database instead of opening.
36 * The argument :mode specifies the open mode of the database.
39 notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
45 notmuch_database_t *database;
49 rb_scan_args (argc, argv, "11", &pathv, &hashv);
51 SafeStringValue (pathv);
52 path = RSTRING_PTR (pathv);
55 Check_Type (hashv, T_HASH);
56 create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create)));
57 modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode));
59 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
60 else if (!FIXNUM_P (modev))
61 rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
63 mode = FIX2INT (modev);
65 case NOTMUCH_DATABASE_MODE_READ_ONLY:
66 case NOTMUCH_DATABASE_MODE_READ_WRITE:
69 rb_raise ( rb_eTypeError, "Invalid mode");
74 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
77 Check_Type (self, T_DATA);
79 ret = notmuch_database_create (path, &database);
81 ret = notmuch_database_open (path, mode, &database);
82 notmuch_rb_status_raise (ret);
84 DATA_PTR (self) = database;
90 * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
92 * Identical to new, except that when it is called with a block, it yields with
93 * the new instance and closes it, and returns the result which is returned from
97 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
101 obj = rb_class_new_instance (argc, argv, klass);
102 if (!rb_block_given_p ())
105 return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
109 * call-seq: DB.close => nil
111 * Close the notmuch database.
114 notmuch_rb_database_close (VALUE self)
116 notmuch_database_t *db;
118 Data_Get_Notmuch_Database (self, db);
119 notmuch_database_destroy (db);
120 DATA_PTR (self) = NULL;
126 * call-seq: DB.path => String
128 * Return the path of the database
131 notmuch_rb_database_path (VALUE self)
133 notmuch_database_t *db;
135 Data_Get_Notmuch_Database (self, db);
137 return rb_str_new2 (notmuch_database_get_path (db));
141 * call-seq: DB.version => Fixnum
143 * Return the version of the database
146 notmuch_rb_database_version (VALUE self)
148 notmuch_database_t *db;
150 Data_Get_Notmuch_Database (self, db);
152 return INT2FIX (notmuch_database_get_version (db));
156 * call-seq: DB.needs_upgrade? => true or false
158 * Return the +true+ if the database needs upgrading, +false+ otherwise
161 notmuch_rb_database_needs_upgrade (VALUE self)
163 notmuch_database_t *db;
165 Data_Get_Notmuch_Database (self, db);
167 return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
171 notmuch_rb_upgrade_notify (void *closure, double progress)
173 VALUE *block = (VALUE *) closure;
174 rb_funcall (*block, ID_call, 1, rb_float_new (progress));
178 * call-seq: DB.upgrade! [{|progress| block }] => nil
180 * Upgrade the database.
182 * If a block is given the block is called with a progress indicator as a
183 * floating point value in the range of [0.0..1.0].
186 notmuch_rb_database_upgrade (VALUE self)
188 notmuch_status_t ret;
189 void (*pnotify) (void *closure, double progress);
190 notmuch_database_t *db;
193 Data_Get_Notmuch_Database (self, db);
195 if (rb_block_given_p ()) {
196 pnotify = notmuch_rb_upgrade_notify;
197 block = rb_block_proc ();
202 ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
203 notmuch_rb_status_raise (ret);
209 * call-seq: DB.begin_atomic => nil
211 * Begin an atomic database operation.
214 notmuch_rb_database_begin_atomic (VALUE self)
216 notmuch_status_t ret;
217 notmuch_database_t *db;
219 Data_Get_Notmuch_Database (self, db);
221 ret = notmuch_database_begin_atomic (db);
222 notmuch_rb_status_raise (ret);
228 * call-seq: DB.end_atomic => nil
230 * Indicate the end of an atomic database operation.
233 notmuch_rb_database_end_atomic (VALUE self)
235 notmuch_status_t ret;
236 notmuch_database_t *db;
238 Data_Get_Notmuch_Database (self, db);
240 ret = notmuch_database_end_atomic (db);
241 notmuch_rb_status_raise (ret);
247 * call-seq: DB.get_directory(path) => DIR
249 * Retrieve a directory object from the database for 'path'
252 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
255 notmuch_status_t ret;
256 notmuch_directory_t *dir;
257 notmuch_database_t *db;
259 Data_Get_Notmuch_Database (self, db);
261 SafeStringValue (pathv);
262 path = RSTRING_PTR (pathv);
264 ret = notmuch_database_get_directory (db, path, &dir);
265 notmuch_rb_status_raise (ret);
267 return Data_Wrap_Struct (notmuch_rb_cDirectory, NULL, NULL, dir);
272 * call-seq: DB.add_message(path) => MESSAGE, isdup
274 * Add a message to the database and return it.
276 * +isdup+ is a boolean that specifies whether the added message was a
280 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
283 notmuch_status_t ret;
284 notmuch_message_t *message;
285 notmuch_database_t *db;
287 Data_Get_Notmuch_Database (self, db);
289 SafeStringValue (pathv);
290 path = RSTRING_PTR (pathv);
292 ret = notmuch_database_add_message (db, path, &message);
293 notmuch_rb_status_raise (ret);
294 return rb_assoc_new (Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message),
295 (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
299 * call-seq: DB.remove_message (path) => isdup
301 * Remove a message from the database.
303 * +isdup+ is a boolean that specifies whether the removed message was a
307 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
310 notmuch_status_t ret;
311 notmuch_database_t *db;
313 Data_Get_Notmuch_Database (self, db);
315 SafeStringValue (pathv);
316 path = RSTRING_PTR (pathv);
318 ret = notmuch_database_remove_message (db, path);
319 notmuch_rb_status_raise (ret);
320 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
324 * call-seq: DB.find_message(id) => MESSAGE or nil
326 * Find a message by message id.
329 notmuch_rb_database_find_message (VALUE self, VALUE idv)
332 notmuch_status_t ret;
333 notmuch_database_t *db;
334 notmuch_message_t *message;
336 Data_Get_Notmuch_Database (self, db);
338 SafeStringValue (idv);
339 id = RSTRING_PTR (idv);
341 ret = notmuch_database_find_message (db, id, &message);
342 notmuch_rb_status_raise (ret);
345 return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
350 * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
352 * Find a message by filename.
355 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
358 notmuch_status_t ret;
359 notmuch_database_t *db;
360 notmuch_message_t *message;
362 Data_Get_Notmuch_Database (self, db);
364 SafeStringValue (pathv);
365 path = RSTRING_PTR (pathv);
367 ret = notmuch_database_find_message_by_filename (db, path, &message);
368 notmuch_rb_status_raise (ret);
371 return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
376 * call-seq: DB.query(query) => QUERY
378 * Retrieve a query object for the query string 'query'
381 notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
384 notmuch_query_t *query;
385 notmuch_database_t *db;
387 Data_Get_Notmuch_Database (self, db);
389 SafeStringValue (qstrv);
390 qstr = RSTRING_PTR (qstrv);
392 query = notmuch_query_create (db, qstr);
394 rb_raise (notmuch_rb_eMemoryError, "Out of memory");
396 return Data_Wrap_Struct (notmuch_rb_cQuery, NULL, NULL, query);