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)
46 #if !defined(RSTRING_PTR)
47 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
48 #endif /* !defined(RSTRING_PTR) */
51 rb_scan_args (argc, argv, "11", &pathv, &hashv);
53 SafeStringValue (pathv);
54 path = RSTRING_PTR (pathv);
57 Check_Type (hashv, T_HASH);
58 create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create)));
59 modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode));
61 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
62 else if (!FIXNUM_P (modev))
63 rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
65 mode = FIX2INT (modev);
67 case NOTMUCH_DATABASE_MODE_READ_ONLY:
68 case NOTMUCH_DATABASE_MODE_READ_WRITE:
71 rb_raise ( rb_eTypeError, "Invalid mode");
76 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
79 Check_Type (self, T_DATA);
80 DATA_PTR (self) = create ? notmuch_database_create (path) : notmuch_database_open (path, mode);
82 rb_raise (notmuch_rb_eDatabaseError, "Failed to open database");
88 * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
90 * Identical to new, except that when it is called with a block, it yields with
91 * the new instance and closes it, and returns the result which is returned from
95 notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
99 obj = rb_class_new_instance (argc, argv, klass);
100 if (!rb_block_given_p ())
103 return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
107 * call-seq: DB.close => nil
109 * Close the notmuch database.
112 notmuch_rb_database_close (VALUE self)
114 notmuch_database_t *db;
116 Data_Get_Notmuch_Database (self, db);
117 notmuch_database_close (db);
118 DATA_PTR (self) = NULL;
124 * call-seq: DB.path => String
126 * Return the path of the database
129 notmuch_rb_database_path (VALUE self)
131 notmuch_database_t *db;
133 Data_Get_Notmuch_Database (self, db);
135 return rb_str_new2 (notmuch_database_get_path (db));
139 * call-seq: DB.version => Fixnum
141 * Return the version of the database
144 notmuch_rb_database_version (VALUE self)
146 notmuch_database_t *db;
148 Data_Get_Notmuch_Database (self, db);
150 return INT2FIX (notmuch_database_get_version (db));
154 * call-seq: DB.needs_upgrade? => true or false
156 * Return the +true+ if the database needs upgrading, +false+ otherwise
159 notmuch_rb_database_needs_upgrade (VALUE self)
161 notmuch_database_t *db;
163 Data_Get_Notmuch_Database (self, db);
165 return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
169 notmuch_rb_upgrade_notify (void *closure, double progress)
171 VALUE *block = (VALUE *) closure;
172 rb_funcall (*block, ID_call, 1, rb_float_new (progress));
176 * call-seq: DB.upgrade! [{|progress| block }] => nil
178 * Upgrade the database.
180 * If a block is given the block is called with a progress indicator as a
181 * floating point value in the range of [0.0..1.0].
184 notmuch_rb_database_upgrade (VALUE self)
186 notmuch_status_t ret;
187 void (*pnotify) (void *closure, double progress);
188 notmuch_database_t *db;
191 Data_Get_Notmuch_Database (self, db);
193 if (rb_block_given_p ()) {
194 pnotify = notmuch_rb_upgrade_notify;
195 block = rb_block_proc ();
200 ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
201 notmuch_rb_status_raise (ret);
207 * call-seq: DB.begin_atomic => nil
209 * Begin an atomic database operation.
212 notmuch_rb_database_begin_atomic (VALUE self)
214 notmuch_status_t ret;
215 notmuch_database_t *db;
217 Data_Get_Notmuch_Database (self, db);
219 ret = notmuch_database_begin_atomic (db);
220 notmuch_rb_status_raise (ret);
226 * call-seq: DB.end_atomic => nil
228 * Indicate the end of an atomic database operation.
231 notmuch_rb_database_end_atomic (VALUE self)
233 notmuch_status_t ret;
234 notmuch_database_t *db;
236 Data_Get_Notmuch_Database (self, db);
238 ret = notmuch_database_end_atomic (db);
239 notmuch_rb_status_raise (ret);
245 * call-seq: DB.get_directory(path) => DIR
247 * Retrieve a directory object from the database for 'path'
250 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
253 notmuch_directory_t *dir;
254 notmuch_database_t *db;
256 Data_Get_Notmuch_Database (self, db);
258 #if !defined(RSTRING_PTR)
259 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
260 #endif /* !defined(RSTRING_PTR) */
262 SafeStringValue (pathv);
263 path = RSTRING_PTR (pathv);
265 dir = notmuch_database_get_directory (db, path);
267 rb_raise (notmuch_rb_eXapianError, "Xapian exception");
269 return Data_Wrap_Struct (notmuch_rb_cDirectory, NULL, NULL, dir);
273 * call-seq: DB.add_message(path) => MESSAGE, isdup
275 * Add a message to the database and return it.
277 * +isdup+ is a boolean that specifies whether the added message was a
281 notmuch_rb_database_add_message (VALUE self, VALUE pathv)
284 notmuch_status_t ret;
285 notmuch_message_t *message;
286 notmuch_database_t *db;
288 Data_Get_Notmuch_Database (self, db);
290 #if !defined(RSTRING_PTR)
291 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
292 #endif /* !defined(RSTRING_PTR) */
294 SafeStringValue (pathv);
295 path = RSTRING_PTR (pathv);
297 ret = notmuch_database_add_message (db, path, &message);
298 notmuch_rb_status_raise (ret);
299 return rb_assoc_new (Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message),
300 (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
304 * call-seq: DB.remove_message (path) => isdup
306 * Remove a message from the database.
308 * +isdup+ is a boolean that specifies whether the removed message was a
312 notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
315 notmuch_status_t ret;
316 notmuch_database_t *db;
318 Data_Get_Notmuch_Database (self, db);
320 #if !defined(RSTRING_PTR)
321 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
322 #endif /* !defined (RSTRING_PTR) */
324 SafeStringValue (pathv);
325 path = RSTRING_PTR (pathv);
327 ret = notmuch_database_remove_message (db, path);
328 notmuch_rb_status_raise (ret);
329 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
333 * call-seq: DB.find_message(id) => MESSAGE or nil
335 * Find a message by message id.
338 notmuch_rb_database_find_message (VALUE self, VALUE idv)
341 notmuch_status_t ret;
342 notmuch_database_t *db;
343 notmuch_message_t *message;
345 Data_Get_Notmuch_Database (self, db);
347 #if !defined(RSTRING_PTR)
348 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
349 #endif /* !defined (RSTRING_PTR) */
351 SafeStringValue (idv);
352 id = RSTRING_PTR (idv);
354 ret = notmuch_database_find_message (db, id, &message);
355 notmuch_rb_status_raise (ret);
358 return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
363 * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
365 * Find a message by filename.
368 notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
371 notmuch_status_t ret;
372 notmuch_database_t *db;
373 notmuch_message_t *message;
375 Data_Get_Notmuch_Database (self, db);
377 #if !defined(RSTRING_PTR)
378 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
379 #endif /* !defined (RSTRING_PTR) */
381 SafeStringValue (pathv);
382 path = RSTRING_PTR (pathv);
384 ret = notmuch_database_find_message_by_filename (db, path, &message);
385 notmuch_rb_status_raise (ret);
388 return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
393 * call-seq: DB.query(query) => QUERY
395 * Retrieve a query object for the query string 'query'
398 notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
401 notmuch_query_t *query;
402 notmuch_database_t *db;
404 Data_Get_Notmuch_Database (self, db);
406 #if !defined(RSTRING_PTR)
407 # define RSTRING_PTR(v) (RSTRING((v))->ptr)
408 #endif /* !defined (RSTRING_PTR) */
410 SafeStringValue (qstrv);
411 qstr = RSTRING_PTR (qstrv);
413 query = notmuch_query_create (db, qstr);
415 rb_raise (notmuch_rb_eMemoryError, "Out of memory");
417 return Data_Wrap_Struct (notmuch_rb_cQuery, NULL, NULL, query);