[gegl] handlers: Allow registration of operations by MIME type



commit 274eaef5b0c6b015f779bc08b3d303b04c9a27c5
Author: Martin Blanchard <tchaik gmx com>
Date:   Mon Jan 11 21:21:56 2016 +0100

    handlers: Allow registration of operations by MIME type
    
    Allow registration of MIME types in handlers db, along with
    filename extensions. Move code to gegl-operation-handlers.c as
    db and calls are not dedicated to filename extensions anymore.
    Old gegl_extension_handler_* functions are kept around in order
    to avoid API breakage, marked as deprecated.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760493

 gegl/gegl-init.c                                   |    4 +-
 gegl/gegl-plugin.h                                 |    1 +
 gegl/operation/Makefile.am                         |    4 +-
 gegl/operation/gegl-extension-handler.c            |   79 +-----------
 gegl/operation/gegl-extension-handler.h            |   26 +++-
 ...private.h => gegl-operation-handlers-private.h} |    4 +-
 gegl/operation/gegl-operation-handlers.c           |  133 ++++++++++++++++++++
 ...handler-private.h => gegl-operation-handlers.h} |   16 ++-
 8 files changed, 180 insertions(+), 87 deletions(-)
---
diff --git a/gegl/gegl-init.c b/gegl/gegl-init.c
index 0fcbf02..cc1195b 100644
--- a/gegl/gegl-init.c
+++ b/gegl/gegl-init.c
@@ -96,7 +96,7 @@ guint gegl_debug_flags = 0;
 #include "buffer/gegl-buffer.h"
 #include "operation/gegl-operation.h"
 #include "operation/gegl-operations.h"
-#include "operation/gegl-extension-handler-private.h"
+#include "operation/gegl-operation-handlers-private.h"
 #include "buffer/gegl-buffer-private.h"
 #include "buffer/gegl-buffer-iterator-private.h"
 #include "buffer/gegl-tile-backend-ram.h"
@@ -462,7 +462,7 @@ gegl_exit (void)
   gegl_tile_backend_swap_cleanup ();
   gegl_tile_cache_destroy ();
   gegl_operation_gtype_cleanup ();
-  gegl_extension_handler_cleanup ();
+  gegl_operation_handlers_cleanup ();
   gegl_random_cleanup ();
   gegl_cl_cleanup ();
 
diff --git a/gegl/gegl-plugin.h b/gegl/gegl-plugin.h
index a1f82e8..c9a7b29 100644
--- a/gegl/gegl-plugin.h
+++ b/gegl/gegl-plugin.h
@@ -79,6 +79,7 @@ GType gegl_module_register_type (GTypeModule     *module,
 #include <operation/gegl-operation-sink.h>
 #include <operation/gegl-operation-meta.h>
 #include <operation/gegl-extension-handler.h>
+#include <operation/gegl-operation-handlers.h>
 #include <operation/gegl-operation-property-keys.h>
 
 G_END_DECLS
diff --git a/gegl/operation/Makefile.am b/gegl/operation/Makefile.am
index cae1e71..5eb9f89 100644
--- a/gegl/operation/Makefile.am
+++ b/gegl/operation/Makefile.am
@@ -27,6 +27,7 @@ liboperation_public_HEADERS = \
        gegl-operation-composer3.h       \
        gegl-operation-context.h         \
        gegl-operation-filter.h          \
+       gegl-operation-handlers.h        \
        gegl-operation-meta.h            \
        gegl-operation-meta-json.h       \
        gegl-operation-point-composer.h  \
@@ -40,12 +41,13 @@ liboperation_public_HEADERS = \
 
 liboperation_sources = \
        gegl-extension-handler.c                \
-       gegl-extension-handler-private.h \
        gegl-operation.c                        \
        gegl-operation-area-filter.c            \
        gegl-operation-composer.c               \
        gegl-operation-composer3.c              \
        gegl-operation-filter.c                 \
+       gegl-operation-handlers.c               \
+       gegl-operation-handlers-private.h       \
        gegl-operation-meta.c                   \
        gegl-operation-meta-json.c                      \
        gegl-operation-point-composer.c         \
diff --git a/gegl/operation/gegl-extension-handler.c b/gegl/operation/gegl-extension-handler.c
index 6c080ce..1b366e4 100644
--- a/gegl/operation/gegl-extension-handler.c
+++ b/gegl/operation/gegl-extension-handler.c
@@ -19,108 +19,43 @@
 #include "config.h"
 #include <glib.h>
 #include "gegl-extension-handler.h"
-#include "gegl-extension-handler-private.h"
-
-static GHashTable *load_handlers = NULL;
-static GHashTable *save_handlers = NULL;
-
-static void
-gegl_extension_handler_register_util (GHashTable  **handlers,
-                                      const gchar  *extension,
-                                      const gchar  *handler)
-{
-  /* Case fold so we get case insensitive extension comparisions */
-  gchar *ext = g_utf8_casefold (extension, -1);
-
-  if (!*handlers)
-    *handlers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-
-  g_hash_table_insert (*handlers, ext, g_strdup (handler));
-}
+#include "gegl-operation-handlers.h"
 
 void
 gegl_extension_handler_register (const gchar *extension,
                                  const gchar *handler)
 {
-  gegl_extension_handler_register_loader (extension, handler);
+  gegl_operation_handlers_register_loader (extension, handler);
 }
 
 void
 gegl_extension_handler_register_loader (const gchar *extension,
                                         const gchar *handler)
 {
-  gegl_extension_handler_register_util (&load_handlers, extension, handler);
+  gegl_operation_handlers_register_loader (extension, handler);
 }
 
 void
 gegl_extension_handler_register_saver (const gchar *extension,
                                        const gchar *handler)
 {
-  gegl_extension_handler_register_util (&save_handlers, extension, handler);
-}
-
-static const gchar *
-gegl_extension_handler_get_util (GHashTable *handlers,
-                                 const gchar *extension,
-                                 const gchar *handler_name,
-                                 const gchar *def)
-{
-  const gchar *handler = NULL;
-  gchar *ext = NULL;
-
-  if (!handlers)
-    return NULL;
-
-  /* Case fold so we get case insensitive extension comparisions */
-  ext = g_utf8_casefold (extension, -1);
-  handler = g_hash_table_lookup (handlers, ext);
-  g_free (ext);
-
-  if (handler)
-    return handler;
-
-  g_warning ("No %s for extension \"%s\", falling back to \"%s\"",
-             handler_name, extension, def);
-
-  return def;
+  gegl_operation_handlers_register_saver (extension, handler);
 }
 
 const gchar *
 gegl_extension_handler_get (const gchar *extension)
 {
-  return gegl_extension_handler_get_loader (extension);
+  return gegl_operation_handlers_get_loader (extension);
 }
 
 const gchar *
 gegl_extension_handler_get_loader (const gchar *extension)
 {
-  return gegl_extension_handler_get_util (load_handlers,
-                                          extension,
-                                          "loader",
-                                          "gegl:magick-load");
+  return gegl_operation_handlers_get_loader (extension);
 }
 
 const gchar *
 gegl_extension_handler_get_saver (const gchar *extension)
 {
-  return gegl_extension_handler_get_util (save_handlers,
-                                          extension,
-                                          "saver",
-                                          "gegl:png-save");
-}
-
-void
-gegl_extension_handler_cleanup (void)
-{
-  if (load_handlers)
-    {
-      g_hash_table_destroy (load_handlers);
-      load_handlers = NULL;
-    }
-
-  if (save_handlers)
-    {
-      g_hash_table_destroy (save_handlers);
-      save_handlers = NULL;
-    }
+  return gegl_operation_handlers_get_saver (extension);
 }
diff --git a/gegl/operation/gegl-extension-handler.h b/gegl/operation/gegl-extension-handler.h
index a32c2df..cf24c18 100644
--- a/gegl/operation/gegl-extension-handler.h
+++ b/gegl/operation/gegl-extension-handler.h
@@ -24,20 +24,34 @@
  * TODO: remove this function in future versions!
  */
 void          gegl_extension_handler_register        (const gchar *extension,
-                                                      const gchar *handler);
+                                                      const gchar *handler) G_GNUC_DEPRECATED;
 
+/* deprecated: use gegl_operation_handlers_register_loader() instead.
+ * TODO: remove this function in future versions!
+ */
 void          gegl_extension_handler_register_loader (const gchar *extension,
-                                                      const gchar *handler);
+                                                      const gchar *handler) G_GNUC_DEPRECATED;
+
+/* deprecated: use gegl_operation_handlers_register_saver() instead.
+ * TODO: remove this function in future versions!
+ */
 void          gegl_extension_handler_register_saver  (const gchar *extension,
-                                                      const gchar *handler);
+                                                      const gchar *handler) G_GNUC_DEPRECATED;
 
 /* similar to gegl_extension_handler_get_loader(), kept for
  * compatibility reasons, do NOT use in newly written code.
  * TODO: remove this function in future versions!
  */
-const gchar * gegl_extension_handler_get             (const gchar *extension);
+const gchar * gegl_extension_handler_get             (const gchar *extension) G_GNUC_DEPRECATED;
 
-const gchar * gegl_extension_handler_get_loader      (const gchar *extension);
-const gchar * gegl_extension_handler_get_saver       (const gchar *extension);
+/* deprecated: use gegl_operation_handlers_get_loader() instead.
+ * TODO: remove this function in future versions!
+ */
+const gchar * gegl_extension_handler_get_loader      (const gchar *extension) G_GNUC_DEPRECATED;
+
+/* deprecated: use gegl_operation_handlers_get_saver() instead.
+ * TODO: remove this function in future versions!
+ */
+const gchar * gegl_extension_handler_get_saver       (const gchar *extension) G_GNUC_DEPRECATED;
 
 #endif
diff --git a/gegl/operation/gegl-extension-handler-private.h 
b/gegl/operation/gegl-operation-handlers-private.h
similarity index 88%
copy from gegl/operation/gegl-extension-handler-private.h
copy to gegl/operation/gegl-operation-handlers-private.h
index 7c54454..0f045a0 100644
--- a/gegl/operation/gegl-extension-handler-private.h
+++ b/gegl/operation/gegl-operation-handlers-private.h
@@ -19,6 +19,6 @@
 #ifndef __GEGL_EXTENSION_HANDLER_PRIVATE_H__
 #define __GEGL_EXTENSION_HANDLER_PRIVATE_H__
 
-void          gegl_extension_handler_cleanup        (void);
+void          gegl_operation_handlers_cleanup         (void);
 
-#endif /* __GEGL_EXTENSION_HANDLER_PRIVATE_H__ */
\ No newline at end of file
+#endif
diff --git a/gegl/operation/gegl-operation-handlers.c b/gegl/operation/gegl-operation-handlers.c
new file mode 100644
index 0000000..acd8770
--- /dev/null
+++ b/gegl/operation/gegl-operation-handlers.c
@@ -0,0 +1,133 @@
+/* This file is part of GEGL
+ *
+ * GEGL is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * GEGL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2006 Øyvind Kolås <pippin gimp org>
+ * Copyright 2016 Martin Blanchard <tchaik gmx com>
+ *  */
+
+#include "config.h"
+#include <glib.h>
+
+#include "gegl-operation-handlers.h"
+#include "gegl-operation-handlers-private.h"
+
+static GHashTable *load_handlers = NULL;
+static GHashTable *save_handlers = NULL;
+
+static gboolean
+gegl_operation_handlers_register_util (GHashTable **handlers,
+                                       const gchar *content_type,
+                                       const gchar *handler)
+{
+  gchar *type = NULL;
+
+  if (g_str_has_prefix (content_type, "."))
+    type = g_utf8_casefold (content_type, -1);
+  else if (g_str_has_prefix (content_type, "image/"))
+    type = g_strdup (content_type);
+  else
+    return FALSE;
+
+  if (*handlers == NULL)
+    *handlers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+  g_hash_table_insert (*handlers, type, g_strdup (handler));
+
+  return TRUE;
+}
+
+gboolean
+gegl_operation_handlers_register_loader (const gchar *content_type,
+                                         const gchar *handler)
+{
+  return gegl_operation_handlers_register_util (&load_handlers,
+                                                content_type,
+                                                handler);
+}
+
+gboolean
+gegl_operation_handlers_register_saver (const gchar *content_type,
+                                        const gchar *handler)
+{
+  return gegl_operation_handlers_register_util (&save_handlers,
+                                                content_type,
+                                                handler);
+}
+
+static const gchar *
+gegl_operation_handlers_get_util (GHashTable *handlers,
+                                  const gchar *content_type,
+                                  const gchar *handler_type,
+                                  const gchar *fallback)
+{
+  const gchar *handler = NULL;
+  gchar *type = NULL;
+
+  if (handlers == NULL)
+    return NULL;
+
+  if (g_str_has_prefix (content_type, "."))
+    type = g_utf8_casefold (content_type, -1);
+  else if (g_str_has_prefix (content_type, "image/"))
+    type = g_strdup (content_type);
+  else
+    return NULL;
+
+  handler = g_hash_table_lookup (handlers, type);
+
+  g_free (type);
+
+  if (handler != NULL)
+    return handler;
+
+  g_warning ("No %s for content type \"%s\", falling back to \"%s\"",
+             handler_type, content_type, fallback);
+
+  return fallback;
+}
+
+const gchar *
+gegl_operation_handlers_get_loader (const gchar *content_type)
+{
+  return gegl_operation_handlers_get_util (load_handlers,
+                                           content_type,
+                                           "loader",
+                                           "gegl:magick-load");
+}
+
+const gchar *
+gegl_operation_handlers_get_saver (const gchar *content_type)
+{
+  return gegl_operation_handlers_get_util (save_handlers,
+                                           content_type,
+                                           "saver",
+                                           "gegl:png-save");
+}
+
+void
+gegl_operation_handlers_cleanup (void)
+{
+  if (load_handlers != NULL)
+    {
+      g_hash_table_destroy (load_handlers);
+      load_handlers = NULL;
+    }
+
+  if (save_handlers != NULL)
+    {
+      g_hash_table_destroy (save_handlers);
+      save_handlers = NULL;
+    }
+}
diff --git a/gegl/operation/gegl-extension-handler-private.h b/gegl/operation/gegl-operation-handlers.h
similarity index 56%
rename from gegl/operation/gegl-extension-handler-private.h
rename to gegl/operation/gegl-operation-handlers.h
index 7c54454..9db38ff 100644
--- a/gegl/operation/gegl-extension-handler-private.h
+++ b/gegl/operation/gegl-operation-handlers.h
@@ -16,9 +16,17 @@
  * Copyright 2006 Øyvind Kolås <pippin gimp org>
  */
 
-#ifndef __GEGL_EXTENSION_HANDLER_PRIVATE_H__
-#define __GEGL_EXTENSION_HANDLER_PRIVATE_H__
+#ifndef __GEGL_OPERATION_HANDLERS_H__
+#define __GEGL_OPERATION_HANDLERS_H__
 
-void          gegl_extension_handler_cleanup        (void);
+gboolean      gegl_operation_handlers_register_loader (const gchar *content_type,
+                                                       const gchar *handler);
 
-#endif /* __GEGL_EXTENSION_HANDLER_PRIVATE_H__ */
\ No newline at end of file
+gboolean      gegl_operation_handlers_register_saver  (const gchar *content_type,
+                                                       const gchar *handler);
+
+const gchar * gegl_operation_handlers_get_loader      (const gchar *content_type);
+
+const gchar * gegl_operation_handlers_get_saver       (const gchar *content_type);
+
+#endif


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