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 * call-seq: Notmuch::Database.new(path, [{:create => false, :mode => notmuch::MODE_READ_ONLY}]) => DB
26 * Create or open a notmuch database using the given path.
27 * If :create is +true+, create the database instead of opening.
28 * The argument :mode specifies the open mode of the database.
31 notmuch_rb_database_new(int argc, VALUE *argv, VALUE klass)
35 notmuch_rb_database_t *db;
38 #if !defined(RSTRING_PTR)
39 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
40 #endif /* !defined(RSTRING_PTR) */
43 if (argc < 1 || argc > 2)
44 rb_raise(rb_eTypeError, "Wrong number of arguments");
46 SafeStringValue(argv[0]);
47 path = RSTRING_PTR(argv[0]);
50 Check_Type(argv[1], T_HASH);
51 create = RTEST(rb_hash_aref(argv[1], ID2SYM(ID_db_create)));
52 modev = rb_hash_aref(argv[1], ID2SYM(ID_db_mode));
54 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
55 else if (!FIXNUM_P(modev))
56 rb_raise(rb_eTypeError, ":mode isn't a Fixnum");
58 mode = FIX2INT(modev);
60 case NOTMUCH_DATABASE_MODE_READ_ONLY:
61 case NOTMUCH_DATABASE_MODE_READ_WRITE:
64 rb_raise(rb_eTypeError, "Invalid mode");
70 mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
73 dbv = Data_Make_Struct(klass, notmuch_rb_database_t, NULL, notmuch_rb_database_free, db);
74 db->nm_db = create ? notmuch_database_create(path) : notmuch_database_open(path, mode);
76 rb_raise(notmuch_rb_eDatabaseError, "failed to open database");
82 * call-seq: DB.close => nil
84 * Close the notmuch database.
87 notmuch_rb_database_close(VALUE self)
89 notmuch_rb_database_t *db;
91 Data_Get_Struct(self, notmuch_rb_database_t, db);
93 notmuch_database_close(db->nm_db);
101 * call-seq: DB.path => String
103 * Return the path of the database
106 notmuch_rb_database_path(VALUE self)
108 notmuch_rb_database_t *db;
110 Data_Get_Struct(self, notmuch_rb_database_t, db);
112 rb_raise(rb_eRuntimeError, "Database closed");
114 return rb_str_new2(notmuch_database_get_path(db->nm_db));
118 * call-seq: DB.version => Fixnum
120 * Return the version of the database
123 notmuch_rb_database_version(VALUE self)
125 notmuch_rb_database_t *db;
127 Data_Get_Struct(self, notmuch_rb_database_t, db);
129 rb_raise(rb_eRuntimeError, "Database closed");
131 return INT2FIX(notmuch_database_get_version(db->nm_db));
135 * call-seq: DB.needs_upgrade? => true or false
137 * Return the +true+ if the database needs upgrading, +false+ otherwise
140 notmuch_rb_database_needs_upgrade(VALUE self)
142 notmuch_rb_database_t *db;
144 Data_Get_Struct(self, notmuch_rb_database_t, db);
146 rb_raise(rb_eRuntimeError, "Database closed");
148 return notmuch_database_needs_upgrade(db->nm_db) ? Qtrue : Qfalse;
152 notmuch_rb_upgrade_notify(void *closure, double progress)
154 VALUE *block = (VALUE *)closure;
155 rb_funcall(*block, ID_call, 1, rb_float_new(progress));
159 * call-seq: DB.upgrade! [{|progress| block }] => nil
161 * Upgrade the database.
163 * If a block is given the block is called with a progress indicator as a
164 * floating point value in the range of [0.0..1.0].
167 notmuch_rb_database_upgrade(VALUE self)
169 notmuch_status_t ret;
170 void (*pnotify) (void *closure, double progress);
171 notmuch_rb_database_t *db;
174 Data_Get_Struct(self, notmuch_rb_database_t, db);
176 rb_raise(rb_eRuntimeError, "Database closed");
178 if (rb_block_given_p()) {
179 pnotify = notmuch_rb_upgrade_notify;
180 block = rb_block_proc();
185 ret = notmuch_database_upgrade(db->nm_db, pnotify, pnotify ? &block : NULL);
186 notmuch_rb_status_raise(ret);
191 * call-seq: DB.get_directory(path) => DIR
193 * Retrieve a directory object from the database for 'path'
196 notmuch_rb_database_get_directory(VALUE self, VALUE pathv)
199 notmuch_rb_directory_t *dir;
200 notmuch_rb_database_t *db;
203 Data_Get_Struct(self, notmuch_rb_database_t, db);
205 rb_raise(rb_eRuntimeError, "Database closed");
207 #if !defined(RSTRING_PTR)
208 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
209 #endif /* !defined(RSTRING_PTR) */
211 SafeStringValue(pathv);
212 path = RSTRING_PTR(pathv);
214 dirv = Data_Make_Struct(notmuch_rb_cDirectory, notmuch_rb_directory_t,
215 notmuch_rb_directory_mark, notmuch_rb_directory_free, dir);
216 dir->nm_dir = notmuch_database_get_directory(db->nm_db, path);
219 rb_raise(notmuch_rb_eXapianError, "Xapian exception");
225 * call-seq: DB.add_message(path) => MESSAGE, isdup
227 * Add a message to the database and return it
228 * +isdup+ is a boolean that specifies whether the added message was a
232 notmuch_rb_database_add_message(VALUE self, VALUE pathv)
235 notmuch_status_t ret;
236 notmuch_rb_message_t *message;
237 notmuch_rb_database_t *db;
240 Data_Get_Struct(self, notmuch_rb_database_t, db);
242 rb_raise(rb_eRuntimeError, "Database closed");
244 #if !defined(RSTRING_PTR)
245 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
246 #endif /* !defined(RSTRING_PTR) */
248 SafeStringValue(pathv);
249 path = RSTRING_PTR(pathv);
251 messagev = Data_Make_Struct(notmuch_rb_cMessage, notmuch_rb_message_t,
252 notmuch_rb_message_mark, notmuch_rb_message_free, message);
253 ret = notmuch_database_add_message(db->nm_db, path, &message->nm_message);
254 message->parent = self;
255 notmuch_rb_status_raise(ret);
256 return rb_assoc_new(messagev, (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
260 * call-seq: DB.remove_message(path) => isdup
262 * Remove a message from the database.
263 * +isdup+ is a boolean that specifies whether the removed message was a
267 notmuch_rb_database_remove_message(VALUE self, VALUE pathv)
270 notmuch_status_t ret;
271 notmuch_rb_database_t *db;
273 Data_Get_Struct(self, notmuch_rb_database_t, db);
275 rb_raise(rb_eRuntimeError, "Database closed");
277 #if !defined(RSTRING_PTR)
278 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
279 #endif /* !defined(RSTRING_PTR) */
281 SafeStringValue(pathv);
282 path = RSTRING_PTR(pathv);
284 ret = notmuch_database_remove_message(db->nm_db, path);
285 notmuch_rb_status_raise(ret);
286 return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
290 * call-seq: DB.query(query) => QUERY
292 * Retrieve a query object for the query string 'query'
295 notmuch_rb_database_query_create(VALUE self, VALUE qstrv)
298 notmuch_rb_query_t *query;
299 notmuch_rb_database_t *db;
302 Data_Get_Struct(self, notmuch_rb_database_t, db);
304 rb_raise(rb_eRuntimeError, "Database closed");
306 #if !defined(RSTRING_PTR)
307 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
308 #endif /* !defined(RSTRING_PTR) */
310 SafeStringValue(qstrv);
311 qstr = RSTRING_PTR(qstrv);
313 queryv = Data_Make_Struct(notmuch_rb_cQuery, notmuch_rb_query_t,
314 notmuch_rb_query_mark, notmuch_rb_query_free, query);
315 query->nm_query = notmuch_query_create(db->nm_db, qstr);
317 if (!query->nm_query)
318 rb_raise(notmuch_rb_eMemoryError, "out of memory");