[gegl/meta-json: 15/18] meta-json: Lookup operation name+description properly



commit 405c13f967212bcd3bfb57b2dac16f5ff68670ea
Author: Jon Nordby <jononor gmail com>
Date:   Tue Jan 20 02:22:39 2015 +0100

    meta-json: Lookup operation name+description properly
    
    No more hardcoding :)

 operations/core/json.c                 |   61 +++++++++++++++++++++++---------
 operations/json/dropshadow2.json       |    3 ++
 operations/json/grey2.json             |    3 ++
 tests/compositions/dropshadow-json.xml |    2 +-
 tests/compositions/grey-json.xml       |    2 +-
 5 files changed, 52 insertions(+), 19 deletions(-)
---
diff --git a/operations/core/json.c b/operations/core/json.c
index 839cc2a..581a811 100644
--- a/operations/core/json.c
+++ b/operations/core/json.c
@@ -81,22 +81,32 @@ property_target_free(PropertyTarget *self)
     g_free(self);
 }
 
-// FIXME: needed?
-/*
-GType                                                                   
-json_op_get_type (void)                                             
-{                                                                     
-    return json_op_type_id;                                         
-}                                                                     
-*/
+static gchar *
+replace_char_inline(gchar *str, gchar from, gchar to) {
+    for (int i=0; i<strlen(str); i++) {
+        str[i] = (str[i] == from) ? to : str[i];
+    }
+    return str;
+}
 
 static gchar *
 component2geglop(const gchar *name) {
+    if (!name) {
+      return NULL;
+    }
     gchar *dup = g_strdup(name);
-    gchar *sep = g_strstr_len(dup, -1, "/");
-    if (sep) {
-        *sep = ':';
+    replace_char_inline(dup, '/', ':');
+    g_ascii_strdown(dup, -1);
+    return dup;
+}
+
+static gchar *
+component2gtypename(const gchar *name) {
+    if (!name) {
+      return NULL;
     }
+    gchar *dup = g_strdup(name);
+    replace_char_inline(dup, '/', '_');
     g_ascii_strdown(dup, -1);
     return dup;
 }
@@ -445,6 +455,18 @@ finalize (GObject *gobject)
 // FIXME: causes infinite loop GEGL_OPERATION_CLASS(json_op_class)->finalize(gobject);
 }
 
+/* json_op_class */
+static const gchar *
+metadata_get_property(JsonObject *root, const gchar *prop) {
+  if (json_object_has_member(root, "properties")) {
+      JsonObject *properties = json_object_get_object_member(root, "properties");
+      if (json_object_has_member(properties, prop)) {
+        return json_object_get_string_member(properties, prop);
+      }
+  }
+  return NULL;
+}
+
 static void
 json_op_class_init (gpointer klass, gpointer class_data)
 {
@@ -464,11 +486,13 @@ json_op_class_init (gpointer klass, gpointer class_data)
                                                     NULL, (GDestroyNotify)property_target_free);
   install_properties(json_op_class);
 
-  // FIXME: unharcode, look up in properties
+  const gchar *description = metadata_get_property(json_op_class->json_root, "description");
+  gchar *name = component2geglop(metadata_get_property(json_op_class->json_root, "name"));
+
   gegl_operation_class_set_keys (operation_class,
-    "name",        "gegl:greyy",
-    "categories",  "effects:light",
-    "description", "Creates a dropshadow effect on the input buffer",
+    "name",        (name) ? name : g_strdup_printf("json:%s", G_OBJECT_CLASS_NAME(object_class)),
+    "categories",  "meta:json",
+    "description",  (description) ? description : "",
     NULL);
 
 }
@@ -518,8 +542,11 @@ json_op_register_type_for_file (GTypeModule *type_module, const gchar *filepath)
         JsonObject *root = json_node_get_object (root_node);
         g_assert(root_node);
         g_print("%s: %p\n", __PRETTY_FUNCTION__, root_node);
-        // FIXME: unhardoce name, look up in json structure, fallback to basename
-        ret = json_op_register_type(type_module, "grey_json", root);
+
+        const gchar *name = metadata_get_property(root, "name");
+        gchar *type_name = (name) ? component2gtypename(name) : component2gtypename(filepath);
+        ret = json_op_register_type(type_module, type_name, root);
+        g_free(type_name);
     }
 
 //    g_object_unref(parser);
diff --git a/operations/json/dropshadow2.json b/operations/json/dropshadow2.json
index b7ad9df..223ea13 100644
--- a/operations/json/dropshadow2.json
+++ b/operations/json/dropshadow2.json
@@ -1,4 +1,7 @@
 {
+  "properties": {
+    "name": "gegl/json/dropshadow2"
+  },
   "processes": {
     "input": {
       "component": "gegl/nop"
diff --git a/operations/json/grey2.json b/operations/json/grey2.json
index a2934a4..b113208 100644
--- a/operations/json/grey2.json
+++ b/operations/json/grey2.json
@@ -1,4 +1,7 @@
 {
+  "properties": {
+    "name": "gegl/json/grey2"
+  },
   "processes": {
     "in": {
       "component": "gegl/nop"
diff --git a/tests/compositions/dropshadow-json.xml b/tests/compositions/dropshadow-json.xml
index ac5d2e1..9f3d7ab 100644
--- a/tests/compositions/dropshadow-json.xml
+++ b/tests/compositions/dropshadow-json.xml
@@ -1,6 +1,6 @@
 <?xml version='1.0' encoding='UTF-8'?>
 <gegl>
-  <node operation='gegl:dropshadow2'>
+  <node operation='gegl:json:dropshadow2'>
     <params>
       <param name='radius'>10</param>
     </params>
diff --git a/tests/compositions/grey-json.xml b/tests/compositions/grey-json.xml
index 5968587..83b26cf 100644
--- a/tests/compositions/grey-json.xml
+++ b/tests/compositions/grey-json.xml
@@ -1,6 +1,6 @@
 <?xml version='1.0' encoding='UTF-8'?>
 <gegl>
-  <node operation='gegl:greyy'>
+  <node operation='gegl:json:grey2'>
     <params>
       <param name='height'>200</param>
       <param name='width'>100</param>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]