1 /* The Ruby interface to the notmuch mail library
3 * Copyright © 2010 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.get_directory(path) => DIR
210 * Retrieve a directory object from the database for 'path'
213 notmuch_rb_database_get_directory(VALUE self, VALUE pathv)
216 notmuch_directory_t *dir;
217 notmuch_database_t *db;
219 Data_Get_Notmuch_Database(self, db);
221 #if !defined(RSTRING_PTR)
222 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
223 #endif /* !defined(RSTRING_PTR) */
225 SafeStringValue(pathv);
226 path = RSTRING_PTR(pathv);
228 dir = notmuch_database_get_directory(db, path);
230 rb_raise(notmuch_rb_eXapianError, "Xapian exception");
232 return Data_Wrap_Struct(notmuch_rb_cDirectory, NULL, NULL, dir);
236 * call-seq: DB.add_message(path) => MESSAGE, isdup
238 * Add a message to the database and return it.
240 * +isdup+ is a boolean that specifies whether the added message was a
244 notmuch_rb_database_add_message(VALUE self, VALUE pathv)
247 notmuch_status_t ret;
248 notmuch_message_t *message;
249 notmuch_database_t *db;
251 Data_Get_Notmuch_Database(self, db);
253 #if !defined(RSTRING_PTR)
254 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
255 #endif /* !defined(RSTRING_PTR) */
257 SafeStringValue(pathv);
258 path = RSTRING_PTR(pathv);
260 ret = notmuch_database_add_message(db, path, &message);
261 notmuch_rb_status_raise(ret);
262 return rb_assoc_new(Data_Wrap_Struct(notmuch_rb_cMessage, NULL, NULL, message),
263 (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
267 * call-seq: DB.remove_message(path) => isdup
269 * Remove a message from the database.
271 * +isdup+ is a boolean that specifies whether the removed message was a
275 notmuch_rb_database_remove_message(VALUE self, VALUE pathv)
278 notmuch_status_t ret;
279 notmuch_database_t *db;
281 Data_Get_Notmuch_Database(self, db);
283 #if !defined(RSTRING_PTR)
284 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
285 #endif /* !defined(RSTRING_PTR) */
287 SafeStringValue(pathv);
288 path = RSTRING_PTR(pathv);
290 ret = notmuch_database_remove_message(db, path);
291 notmuch_rb_status_raise(ret);
292 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
296 * call-seq: DB.query(query) => QUERY
298 * Retrieve a query object for the query string 'query'
301 notmuch_rb_database_query_create(VALUE self, VALUE qstrv)
304 notmuch_query_t *query;
305 notmuch_database_t *db;
307 Data_Get_Notmuch_Database(self, db);
309 #if !defined(RSTRING_PTR)
310 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
311 #endif /* !defined(RSTRING_PTR) */
313 SafeStringValue(qstrv);
314 qstr = RSTRING_PTR(qstrv);
316 query = notmuch_query_create(db, qstr);
318 rb_raise(notmuch_rb_eMemoryError, "Out of memory");
320 return Data_Wrap_Struct(notmuch_rb_cQuery, NULL, NULL, query);