[gegl] gegl-op: Add property_uri/GeglParamSpecUri



commit 2af2b6218be29d2b6882dd46dc81c0809bf40962
Author: Jon Nordby <jononor gmail com>
Date:   Mon Jan 5 13:32:44 2015 +0100

    gegl-op: Add property_uri/GeglParamSpecUri
    
    Special string property which is a URI/URL (incl dataURL).
    Annotated differently so that user interfaces can show a
    dialog particular for picking URIs.

 gegl/gegl-op.h                        |   28 +++++++++
 gegl/property-types/gegl-paramspecs.c |  107 +++++++++++++++++++++++++++++++++
 gegl/property-types/gegl-paramspecs.h |   45 ++++++++++++++
 3 files changed, 180 insertions(+), 0 deletions(-)
---
diff --git a/gegl/gegl-op.h b/gegl/gegl-op.h
index b992f1c..e1421b9 100644
--- a/gegl/gegl-op.h
+++ b/gegl/gegl-op.h
@@ -246,6 +246,7 @@ gegl_module_register (GTypeModule *module)
 #define property_int(name, label, def_val)        ITEM(name,label,def_val,int)
 #define property_string(name, label, def_val)     ITEM(name,label,def_val,string)
 #define property_file_path(name, label, def_val)  ITEM(name,label,def_val,string)
+#define property_uri(name, label, def_val)        ITEM(name,label,def_val,string)
 #define property_boolean(name, label, def_val)    ITEM(name,label,def_val,boolean)
 #define property_pointer(name, label, def_val)    ITEM(name,label,def_val,pointer)
 #define property_object(name, label, def_val)     ITEM2(name,label,def_val,object)
@@ -306,6 +307,7 @@ static GType enum_name ## _get_type (void)               \
 #undef property_string
 #undef property_boolean
 #undef property_file_path
+#undef property_uri
 #undef property_object
 #undef property_pointer
 #undef property_format
@@ -331,6 +333,7 @@ struct _GeglProperties
 #define property_string(name, label, def_val)          gchar      *name;
 #define property_boolean(name, label, def_val)         gboolean    name;
 #define property_file_path(name, label, def_val)       gchar      *name;
+#define property_uri(name, label, def_val)             gchar      *name;
 #define property_object(name, label, def_val)          GObject    *name;
 #define property_curve(name, label, def_val)           GeglCurve  *name;
 #define property_color(name, label, def_val)           GeglColor  *name;
@@ -348,6 +351,7 @@ struct _GeglProperties
 #undef property_string
 #undef property_boolean
 #undef property_file_path
+#undef property_uri
 #undef property_object
 #undef property_pointer
 #undef property_format
@@ -372,6 +376,7 @@ enum
 #define property_int(name, label, def_val)        ITEM(name,label,def_val,int)
 #define property_string(name, label, def_val)     ITEM(name,label,def_val,string)
 #define property_file_path(name, label, def_val)  ITEM(name,label,def_val,string)
+#define property_uri(name, label, def_val)        ITEM(name,label,def_val,string)
 #define property_boolean(name, label, def_val)    ITEM(name,label,def_val,boolean)
 #define property_object(name, label, def_val)     ITEM2(name,label,def_val,object)
 #define property_curve(name, label, def_val)      ITEM2(name,label,def_val,object)
@@ -414,6 +419,7 @@ get_property (GObject      *gobject,
 #undef property_string
 #undef property_boolean
 #undef property_file_path
+#undef property_uri
 #undef property_object
 #undef property_pointer
 #undef property_format
@@ -465,6 +471,12 @@ set_property (GObject      *gobject,
         g_free (properties->name);                                    \
       properties->name = g_value_dup_string (value);                  \
       break;
+#define property_uri(name, label, def_val)                            \
+    case PROP_##name:                                                 \
+      if (properties->name)                                           \
+        g_free (properties->name);                                    \
+      properties->name = g_value_dup_string (value);                  \
+      break;
 #define property_object(name, label, def_val)                         \
     case PROP_##name:                                                 \
       if (properties->name != NULL)                                   \
@@ -529,6 +541,7 @@ set_property (GObject      *gobject,
 #undef property_string
 #undef property_boolean
 #undef property_file_path
+#undef property_uri
 #undef property_object
 #undef property_pointer
 #undef property_curve
@@ -565,6 +578,12 @@ static void gegl_op_destroy_notify (gpointer data)
       g_free (properties->name);                    \
       properties->name = NULL;                      \
     }
+#define property_uri(name, label, def_val)          \
+  if (properties->name)                             \
+    {                                               \
+      g_free (properties->name);                    \
+      properties->name = NULL;                      \
+    }
 #define property_object(name, label, def_val)       \
   if (properties->name)                             \
     {                                               \
@@ -601,6 +620,7 @@ static void gegl_op_destroy_notify (gpointer data)
 #undef property_string
 #undef property_boolean
 #undef property_file_path
+#undef property_uri
 #undef property_object
 #undef property_pointer
 #undef property_format
@@ -632,6 +652,7 @@ gegl_op_constructor (GType                  type,
 #define property_string(name, label, def_val)
 #define property_boolean(name, label, def_val)
 #define property_file_path(name, label, def_val)
+#define property_uri(name, label, def_val)
 #define property_object(name, label, def_val)
 #define property_pointer(name, label, def_val)
 #define property_format(name, label, def_val)
@@ -652,6 +673,7 @@ gegl_op_constructor (GType                  type,
 #undef property_string
 #undef property_boolean
 #undef property_file_path
+#undef property_uri
 #undef property_object
 #undef property_pointer
 #undef property_format
@@ -869,6 +891,12 @@ gegl_op_class_intern_init (gpointer klass)
        gegl_param_spec_file_path (#name, label, NULL, FALSE, FALSE, def_val, flags);\
      current_prop = PROP_##name ;
 
+#define property_uri(name, label, def_val) \
+    REGISTER_IF_ANY  \
+  }{ GParamSpec *pspec = \
+       gegl_param_spec_uri (#name, label, NULL, FALSE, FALSE, def_val, flags);\
+     current_prop = PROP_##name ;
+
 #define property_object(name, label, type) \
     REGISTER_IF_ANY  \
   }{ GParamSpec *pspec = \
diff --git a/gegl/property-types/gegl-paramspecs.c b/gegl/property-types/gegl-paramspecs.c
index fcfcce2..8db15ee 100644
--- a/gegl/property-types/gegl-paramspecs.c
+++ b/gegl/property-types/gegl-paramspecs.c
@@ -781,3 +781,110 @@ gegl_param_spec_format (const gchar *name,
 
   return G_PARAM_SPEC (pspec);
 }
+
+/*
+ * GEGL_TYPE_PARAM_URI
+ */
+
+static void       gegl_param_uri_class_init (GParamSpecClass *klass);
+static void       gegl_param_uri_init       (GParamSpec *pspec);
+static gboolean   gegl_param_uri_validate   (GParamSpec *pspec,
+                                                   GValue     *value);
+
+GType
+gegl_param_uri_get_type (void)
+{
+  static GType type = 0;
+
+  if (!type)
+    {
+      const GTypeInfo info =
+      {
+        sizeof (GParamSpecClass),
+        NULL,                                        NULL,
+        (GClassInitFunc) gegl_param_uri_class_init,
+        NULL,                                        NULL,
+        sizeof (GeglParamSpecString),
+        0,
+        (GInstanceInitFunc) gegl_param_uri_init
+      };
+
+      type = g_type_register_static (G_TYPE_PARAM_STRING,
+                                     "GeglParamUri", &info, 0);
+    }
+
+  return type;
+}
+
+static void
+gegl_param_uri_class_init (GParamSpecClass *klass)
+{
+  klass->value_type     = G_TYPE_STRING;
+  klass->value_validate = gegl_param_uri_validate;
+}
+
+static void
+gegl_param_uri_init (GParamSpec *pspec)
+{
+  GeglParamSpecUri *sspec = GEGL_PARAM_SPEC_URI (pspec);
+
+  sspec->no_validate = FALSE;
+  sspec->null_ok     = FALSE;
+}
+
+static gboolean
+gegl_param_uri_validate (GParamSpec *pspec,
+                               GValue     *value)
+{
+  GeglParamSpecUri *sspec = GEGL_PARAM_SPEC_URI (pspec);
+  gchar                 *path  = value->data[0].v_pointer;
+
+  if (path)
+    {
+      gchar *s;
+
+      if (!sspec->no_validate &&
+          !g_utf8_validate (path, -1, (const gchar **) &s))
+        {
+          for (; *s; s++)
+            if (*s < ' ')
+              *s = '?';
+
+          return TRUE;
+        }
+    }
+  else if (!sspec->null_ok)
+    {
+      value->data[0].v_pointer = g_strdup ("");
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+GParamSpec *
+gegl_param_spec_uri (const gchar *name,
+                           const gchar *nick,
+                           const gchar *blurb,
+                           gboolean     no_validate,
+                           gboolean     null_ok,
+                           const gchar *default_value,
+                           GParamFlags  flags)
+{
+  GeglParamSpecUri *sspec;
+
+  sspec = g_param_spec_internal (GEGL_TYPE_PARAM_URI,
+                                 name, nick, blurb, flags);
+
+  if (sspec)
+    {
+      g_free (G_PARAM_SPEC_STRING (sspec)->default_value);
+      G_PARAM_SPEC_STRING (sspec)->default_value = g_strdup (default_value);
+
+      sspec->no_validate = no_validate ? TRUE : FALSE;
+      sspec->null_ok     = null_ok     ? TRUE : FALSE;
+    }
+
+  return G_PARAM_SPEC (sspec);
+}
+
diff --git a/gegl/property-types/gegl-paramspecs.h b/gegl/property-types/gegl-paramspecs.h
index 4b3dce0..51ad561 100644
--- a/gegl/property-types/gegl-paramspecs.h
+++ b/gegl/property-types/gegl-paramspecs.h
@@ -393,5 +393,50 @@ GParamSpec * gegl_param_spec_format (const gchar *name,
                                      GParamFlags  flags);
 
 
+
+/*
+ * GEGL_TYPE_PARAM_URI
+ */
+
+#define GEGL_TYPE_PARAM_URI           (gegl_param_uri_get_type ())
+#define GEGL_PARAM_SPEC_URI(pspec)    (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GEGL_TYPE_PARAM_URI, 
GeglParamSpecUri))
+#define GEGL_IS_PARAM_SPEC_URI(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), GEGL_TYPE_PARAM_URI))
+
+typedef struct _GeglParamSpecUri GeglParamSpecUri;
+
+struct _GeglParamSpecUri
+{
+  GParamSpecString parent_instance;
+
+  guint            no_validate : 1;
+  guint            null_ok     : 1;
+};
+
+GType        gegl_param_uri_get_type (void) G_GNUC_CONST;
+
+/**
+ * gegl_param_spec_uri:
+ * @name: canonical name of the property specified
+ * @nick: nick name for the property specified
+ * @blurb: description of the property specified
+ * @no_validate: true if the string should be validated with g_utf8_validate
+ * @null_ok: true if the string can be NULL
+ * @default_value: default value for the property specified
+ * @flags: flags for the property specified
+ *
+ * Creates a new #GeglParamSpecUri instance.
+ *
+ * Return value: (transfer full): a newly created parameter specification
+ */
+GParamSpec * gegl_param_spec_uri (const gchar *name,
+                                        const gchar *nick,
+                                        const gchar *blurb,
+                                        gboolean     no_validate,
+                                        gboolean     null_ok,
+                                        const gchar *default_value,
+                                        GParamFlags  flags);
+
+
+
 G_END_DECLS
 #endif  /*  __GEGL_PARAM_SPECS_H__  */


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