]> git.notmuchmail.org Git - notmuch/blobdiff - bindings/ruby/database.c
ruby: move towards more modern RTypedData
[notmuch] / bindings / ruby / database.c
index b75dfd1897a6bb2578ca9e284ee880185997366e..bb4273e601bc7d7b134680f05f1d470225543ea6 100644 (file)
@@ -13,7 +13,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see http://www.gnu.org/licenses/ .
+ * along with this program.  If not, see https://www.gnu.org/licenses/ .
  *
  * Author: Ali Polatel <alip@exherbo.org>
  */
@@ -23,7 +23,7 @@
 VALUE
 notmuch_rb_database_alloc (VALUE klass)
 {
-    return Data_Wrap_Struct (klass, NULL, NULL, NULL);
+    return Data_Wrap_Notmuch_Object (klass, NULL);
 }
 
 /*
@@ -42,10 +42,8 @@ notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
     int create, mode;
     VALUE pathv, hashv;
     VALUE modev;
-
-#if !defined(RSTRING_PTR)
-# define RSTRING_PTR(v) (RSTRING((v))->ptr)
-#endif /* !defined(RSTRING_PTR) */
+    notmuch_database_t *database;
+    notmuch_status_t ret;
 
     /* Check arguments */
     rb_scan_args (argc, argv, "11", &pathv, &hashv);
@@ -76,10 +74,14 @@ notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
        mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
     }
 
-    Check_Type (self, T_DATA);
-    DATA_PTR (self) = create ? notmuch_database_create (path) : notmuch_database_open (path, mode);
-    if (!DATA_PTR (self))
-       rb_raise (notmuch_rb_eDatabaseError, "Failed to open database");
+    rb_check_typeddata (self, &notmuch_rb_object_type);
+    if (create)
+       ret = notmuch_database_create (path, &database);
+    else
+       ret = notmuch_database_open (path, mode, &database);
+    notmuch_rb_status_raise (ret);
+
+    DATA_PTR (self) = database;
 
     return self;
 }
@@ -111,11 +113,13 @@ notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
 VALUE
 notmuch_rb_database_close (VALUE self)
 {
+    notmuch_status_t ret;
     notmuch_database_t *db;
 
     Data_Get_Notmuch_Database (self, db);
-    notmuch_database_close (db);
+    ret = notmuch_database_destroy (db);
     DATA_PTR (self) = NULL;
+    notmuch_rb_status_raise (ret);
 
     return Qnil;
 }
@@ -250,23 +254,20 @@ VALUE
 notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
 {
     const char *path;
+    notmuch_status_t ret;
     notmuch_directory_t *dir;
     notmuch_database_t *db;
 
     Data_Get_Notmuch_Database (self, db);
 
-#if !defined(RSTRING_PTR)
-# define RSTRING_PTR(v) (RSTRING((v))->ptr)
-#endif /* !defined(RSTRING_PTR) */
-
     SafeStringValue (pathv);
     path = RSTRING_PTR (pathv);
 
-    dir = notmuch_database_get_directory (db, path);
-    if (!dir)
-        rb_raise (notmuch_rb_eXapianError, "Xapian exception");
-
-    return Data_Wrap_Struct (notmuch_rb_cDirectory, NULL, NULL, dir);
+    ret = notmuch_database_get_directory (db, path, &dir);
+    notmuch_rb_status_raise (ret);
+    if (dir)
+       return Data_Wrap_Notmuch_Object (notmuch_rb_cDirectory, dir);
+    return Qnil;
 }
 
 /*
@@ -287,16 +288,12 @@ notmuch_rb_database_add_message (VALUE self, VALUE pathv)
 
     Data_Get_Notmuch_Database (self, db);
 
-#if !defined(RSTRING_PTR)
-# define RSTRING_PTR(v) (RSTRING((v))->ptr)
-#endif /* !defined(RSTRING_PTR) */
-
     SafeStringValue (pathv);
     path = RSTRING_PTR (pathv);
 
-    ret = notmuch_database_add_message (db, path, &message);
+    ret = notmuch_database_index_file (db, path, NULL, &message);
     notmuch_rb_status_raise (ret);
-    return rb_assoc_new (Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message),
+    return rb_assoc_new (Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, message),
         (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
 }
 
@@ -317,10 +314,6 @@ notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
 
     Data_Get_Notmuch_Database (self, db);
 
-#if !defined(RSTRING_PTR)
-# define RSTRING_PTR(v) (RSTRING((v))->ptr)
-#endif /* !defined (RSTRING_PTR) */
-
     SafeStringValue (pathv);
     path = RSTRING_PTR (pathv);
 
@@ -344,10 +337,6 @@ notmuch_rb_database_find_message (VALUE self, VALUE idv)
 
     Data_Get_Notmuch_Database (self, db);
 
-#if !defined(RSTRING_PTR)
-# define RSTRING_PTR(v) (RSTRING((v))->ptr)
-#endif /* !defined (RSTRING_PTR) */
-
     SafeStringValue (idv);
     id = RSTRING_PTR (idv);
 
@@ -355,7 +344,7 @@ notmuch_rb_database_find_message (VALUE self, VALUE idv)
     notmuch_rb_status_raise (ret);
 
     if (message)
-        return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
+        return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, message);
     return Qnil;
 }
 
@@ -374,10 +363,6 @@ notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
 
     Data_Get_Notmuch_Database (self, db);
 
-#if !defined(RSTRING_PTR)
-# define RSTRING_PTR(v) (RSTRING((v))->ptr)
-#endif /* !defined (RSTRING_PTR) */
-
     SafeStringValue (pathv);
     path = RSTRING_PTR (pathv);
 
@@ -385,10 +370,34 @@ notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
     notmuch_rb_status_raise (ret);
 
     if (message)
-        return Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message);
+        return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, message);
     return Qnil;
 }
 
+/*
+ * call-seq: DB.get_all_tags() => TAGS
+ *
+ * Returns a list of all tags found in the database.
+ */
+VALUE
+notmuch_rb_database_get_all_tags (VALUE self)
+{
+    notmuch_database_t *db;
+    notmuch_tags_t *tags;
+
+    Data_Get_Notmuch_Database (self, db);
+
+    tags = notmuch_database_get_all_tags (db);
+    if (!tags) {
+       const char *msg = notmuch_database_status_string (db);
+       if (!msg)
+           msg = "Unknown notmuch error";
+
+       rb_raise (notmuch_rb_eBaseError, "%s", msg);
+    }
+    return Data_Wrap_Notmuch_Object (notmuch_rb_cTags, tags);
+}
+
 /*
  * call-seq: DB.query(query) => QUERY
  *
@@ -403,10 +412,6 @@ notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
 
     Data_Get_Notmuch_Database (self, db);
 
-#if !defined(RSTRING_PTR)
-# define RSTRING_PTR(v) (RSTRING((v))->ptr)
-#endif /* !defined (RSTRING_PTR) */
-
     SafeStringValue (qstrv);
     qstr = RSTRING_PTR (qstrv);
 
@@ -414,5 +419,5 @@ notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
     if (!query)
         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
 
-    return Data_Wrap_Struct (notmuch_rb_cQuery, NULL, NULL, query);
+    return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, query);
 }