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");
77 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
80 Check_Type(self, T_DATA);
81 DATA_PTR(self) = create ? notmuch_database_create(path) : notmuch_database_open(path, mode);
83 rb_raise(notmuch_rb_eDatabaseError, "Failed to open database");
89 * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
91 * Identical to new, except that when it is called with a block, it yields with
92 * the new instance and closes it, and returns the result which is returned from
96 notmuch_rb_database_open(int argc, VALUE *argv, VALUE klass)
100 obj = rb_class_new_instance(argc, argv, klass);
101 if (!rb_block_given_p())
104 return rb_ensure(rb_yield, obj, notmuch_rb_database_close, obj);
108 * call-seq: DB.close => nil
110 * Close the notmuch database.
113 notmuch_rb_database_close(VALUE self)
115 notmuch_database_t *db;
117 Data_Get_Notmuch_Database(self, db);
118 notmuch_database_close(db);
119 DATA_PTR(self) = NULL;
125 * call-seq: DB.path => String
127 * Return the path of the database
130 notmuch_rb_database_path(VALUE self)
132 notmuch_database_t *db;
134 Data_Get_Notmuch_Database(self, db);
136 return rb_str_new2(notmuch_database_get_path(db));
140 * call-seq: DB.version => Fixnum
142 * Return the version of the database
145 notmuch_rb_database_version(VALUE self)
147 notmuch_database_t *db;
149 Data_Get_Notmuch_Database(self, db);
151 return INT2FIX(notmuch_database_get_version(db));
155 * call-seq: DB.needs_upgrade? => true or false
157 * Return the +true+ if the database needs upgrading, +false+ otherwise
160 notmuch_rb_database_needs_upgrade(VALUE self)
162 notmuch_database_t *db;
164 Data_Get_Notmuch_Database(self, db);
166 return notmuch_database_needs_upgrade(db) ? Qtrue : Qfalse;
170 notmuch_rb_upgrade_notify(void *closure, double progress)
172 VALUE *block = (VALUE *)closure;
173 rb_funcall(*block, ID_call, 1, rb_float_new(progress));
177 * call-seq: DB.upgrade! [{|progress| block }] => nil
179 * Upgrade the database.
181 * If a block is given the block is called with a progress indicator as a
182 * floating point value in the range of [0.0..1.0].
185 notmuch_rb_database_upgrade(VALUE self)
187 notmuch_status_t ret;
188 void (*pnotify) (void *closure, double progress);
189 notmuch_database_t *db;
192 Data_Get_Notmuch_Database(self, db);
194 if (rb_block_given_p()) {
195 pnotify = notmuch_rb_upgrade_notify;
196 block = rb_block_proc();
201 ret = notmuch_database_upgrade(db, pnotify, pnotify ? &block : NULL);
202 notmuch_rb_status_raise(ret);
208 * call-seq: DB.begin_atomic => nil
210 * Begin an atomic database operation.
213 notmuch_rb_database_begin_atomic(VALUE self)
215 notmuch_status_t ret;
216 notmuch_database_t *db;
218 Data_Get_Notmuch_Database(self, db);
220 ret = notmuch_database_begin_atomic(db);
221 notmuch_rb_status_raise(ret);
227 * call-seq: DB.end_atomic => nil
229 * Indicate the end of an atomic database operation.
232 notmuch_rb_database_end_atomic(VALUE self)
234 notmuch_status_t ret;
235 notmuch_database_t *db;
237 Data_Get_Notmuch_Database(self, db);
239 ret = notmuch_database_end_atomic(db);
240 notmuch_rb_status_raise(ret);
246 * call-seq: DB.get_directory(path) => DIR
248 * Retrieve a directory object from the database for 'path'
251 notmuch_rb_database_get_directory(VALUE self, VALUE pathv)
254 notmuch_directory_t *dir;
255 notmuch_database_t *db;
257 Data_Get_Notmuch_Database(self, db);
259 #if !defined(RSTRING_PTR)
260 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
261 #endif /* !defined(RSTRING_PTR) */
263 SafeStringValue(pathv);
264 path = RSTRING_PTR(pathv);
266 dir = notmuch_database_get_directory(db, path);
268 rb_raise(notmuch_rb_eXapianError, "Xapian exception");
270 return Data_Wrap_Struct(notmuch_rb_cDirectory, NULL, NULL, dir);
274 * call-seq: DB.add_message(path) => MESSAGE, isdup
276 * Add a message to the database and return it.
278 * +isdup+ is a boolean that specifies whether the added message was a
282 notmuch_rb_database_add_message(VALUE self, VALUE pathv)
285 notmuch_status_t ret;
286 notmuch_message_t *message;
287 notmuch_database_t *db;
289 Data_Get_Notmuch_Database(self, db);
291 #if !defined(RSTRING_PTR)
292 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
293 #endif /* !defined(RSTRING_PTR) */
295 SafeStringValue(pathv);
296 path = RSTRING_PTR(pathv);
298 ret = notmuch_database_add_message(db, path, &message);
299 notmuch_rb_status_raise(ret);
300 return rb_assoc_new(Data_Wrap_Struct(notmuch_rb_cMessage, NULL, NULL, message),
301 (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
305 * call-seq: DB.remove_message(path) => isdup
307 * Remove a message from the database.
309 * +isdup+ is a boolean that specifies whether the removed message was a
313 notmuch_rb_database_remove_message(VALUE self, VALUE pathv)
316 notmuch_status_t ret;
317 notmuch_database_t *db;
319 Data_Get_Notmuch_Database(self, db);
321 #if !defined(RSTRING_PTR)
322 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
323 #endif /* !defined(RSTRING_PTR) */
325 SafeStringValue(pathv);
326 path = RSTRING_PTR(pathv);
328 ret = notmuch_database_remove_message(db, path);
329 notmuch_rb_status_raise(ret);
330 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
334 * call-seq: DB.query(query) => QUERY
336 * Retrieve a query object for the query string 'query'
339 notmuch_rb_database_query_create(VALUE self, VALUE qstrv)
342 notmuch_query_t *query;
343 notmuch_database_t *db;
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(qstrv);
352 qstr = RSTRING_PTR(qstrv);
354 query = notmuch_query_create(db, qstr);
356 rb_raise(notmuch_rb_eMemoryError, "Out of memory");
358 return Data_Wrap_Struct(notmuch_rb_cQuery, NULL, NULL, query);