[gimp] all: make GimpDataLoaderFactory less static
- From: Michael Natterer <mitch src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gimp] all: make GimpDataLoaderFactory less static
- Date: Sun, 10 Jun 2018 12:17:11 +0000 (UTC)
commit f997106fb214d70ce0755ac4b68005b282bbcb0a
Author: Michael Natterer <mitch gimp org>
Date: Sat Jun 9 16:01:29 2018 +0200
all: make GimpDataLoaderFactory less static
Add functions to register loaders, remove the static array of loaders
given at construction time.
app/core/gimp-data-factories.c | 138 ++++++++++++++------------
app/core/gimpdataloaderfactory.c | 204 ++++++++++++++++++++++++++++-----------
app/core/gimpdataloaderfactory.h | 35 +++----
3 files changed, 241 insertions(+), 136 deletions(-)
---
diff --git a/app/core/gimp-data-factories.c b/app/core/gimp-data-factories.c
index f952b2fd59..709157e091 100644
--- a/app/core/gimp-data-factories.c
+++ b/app/core/gimp-data-factories.c
@@ -62,48 +62,6 @@
void
gimp_data_factories_init (Gimp *gimp)
{
- static const GimpDataLoaderEntry brush_loader_entries[] =
- {
- { gimp_brush_load, GIMP_BRUSH_FILE_EXTENSION, FALSE },
- { gimp_brush_load, GIMP_BRUSH_PIXMAP_FILE_EXTENSION, FALSE },
- { gimp_brush_load_abr, GIMP_BRUSH_PS_FILE_EXTENSION, FALSE },
- { gimp_brush_load_abr, GIMP_BRUSH_PSP_FILE_EXTENSION, FALSE },
- { gimp_brush_generated_load, GIMP_BRUSH_GENERATED_FILE_EXTENSION, TRUE },
- { gimp_brush_pipe_load, GIMP_BRUSH_PIPE_FILE_EXTENSION, FALSE }
- };
-
- static const GimpDataLoaderEntry dynamics_loader_entries[] =
- {
- { gimp_dynamics_load, GIMP_DYNAMICS_FILE_EXTENSION, TRUE }
- };
-
- static const GimpDataLoaderEntry mybrush_loader_entries[] =
- {
- { gimp_mybrush_load, GIMP_MYBRUSH_FILE_EXTENSION, FALSE }
- };
-
- static const GimpDataLoaderEntry pattern_loader_entries[] =
- {
- { gimp_pattern_load, GIMP_PATTERN_FILE_EXTENSION, FALSE },
- { gimp_pattern_load_pixbuf, NULL /* fallback loader */, FALSE }
- };
-
- static const GimpDataLoaderEntry gradient_loader_entries[] =
- {
- { gimp_gradient_load, GIMP_GRADIENT_FILE_EXTENSION, TRUE },
- { gimp_gradient_load_svg, GIMP_GRADIENT_SVG_FILE_EXTENSION, FALSE }
- };
-
- static const GimpDataLoaderEntry palette_loader_entries[] =
- {
- { gimp_palette_load, GIMP_PALETTE_FILE_EXTENSION, TRUE }
- };
-
- static const GimpDataLoaderEntry tool_preset_loader_entries[] =
- {
- { gimp_tool_preset_load, GIMP_TOOL_PRESET_FILE_EXTENSION, TRUE }
- };
-
g_return_if_fail (GIMP_IS_GIMP (gimp));
gimp->brush_factory =
@@ -112,11 +70,39 @@ gimp_data_factories_init (Gimp *gimp)
"brush-path",
"brush-path-writable",
gimp_brush_new,
- gimp_brush_get_standard,
- brush_loader_entries,
- G_N_ELEMENTS (brush_loader_entries));
+ gimp_brush_get_standard);
gimp_object_set_static_name (GIMP_OBJECT (gimp->brush_factory),
"brush factory");
+ gimp_data_loader_factory_add_loader (gimp->brush_factory,
+ "GIMP Brush",
+ gimp_brush_load,
+ GIMP_BRUSH_FILE_EXTENSION,
+ FALSE);
+ gimp_data_loader_factory_add_loader (gimp->brush_factory,
+ "GIMP Brush Pixmap",
+ gimp_brush_load,
+ GIMP_BRUSH_PIXMAP_FILE_EXTENSION,
+ FALSE);
+ gimp_data_loader_factory_add_loader (gimp->brush_factory,
+ "Photoshop ABR Brush",
+ gimp_brush_load_abr,
+ GIMP_BRUSH_PS_FILE_EXTENSION,
+ FALSE);
+ gimp_data_loader_factory_add_loader (gimp->brush_factory,
+ "Paint Shop Pro JBR Brush",
+ gimp_brush_load_abr,
+ GIMP_BRUSH_PSP_FILE_EXTENSION,
+ FALSE);
+ gimp_data_loader_factory_add_loader (gimp->brush_factory,
+ "GIMP Generated Brush",
+ gimp_brush_generated_load,
+ GIMP_BRUSH_GENERATED_FILE_EXTENSION,
+ TRUE);
+ gimp_data_loader_factory_add_loader (gimp->brush_factory,
+ "GIMP Brush Pipe",
+ gimp_brush_pipe_load,
+ GIMP_BRUSH_PIPE_FILE_EXTENSION,
+ FALSE);
gimp->dynamics_factory =
gimp_data_loader_factory_new (gimp,
@@ -124,11 +110,14 @@ gimp_data_factories_init (Gimp *gimp)
"dynamics-path",
"dynamics-path-writable",
gimp_dynamics_new,
- gimp_dynamics_get_standard,
- dynamics_loader_entries,
- G_N_ELEMENTS (dynamics_loader_entries));
+ gimp_dynamics_get_standard);
gimp_object_set_static_name (GIMP_OBJECT (gimp->dynamics_factory),
"dynamics factory");
+ gimp_data_loader_factory_add_loader (gimp->dynamics_factory,
+ "GIMP Paint Dynamics",
+ gimp_dynamics_load,
+ GIMP_DYNAMICS_FILE_EXTENSION,
+ TRUE);
gimp->mybrush_factory =
gimp_data_loader_factory_new (gimp,
@@ -136,11 +125,14 @@ gimp_data_factories_init (Gimp *gimp)
"mypaint-brush-path",
"mypaint-brush-path-writable",
NULL,
- NULL,
- mybrush_loader_entries,
- G_N_ELEMENTS (mybrush_loader_entries));
+ NULL);
gimp_object_set_static_name (GIMP_OBJECT (gimp->mybrush_factory),
"mypaint brush factory");
+ gimp_data_loader_factory_add_loader (gimp->mybrush_factory,
+ "MyPaint Brush",
+ gimp_mybrush_load,
+ GIMP_MYBRUSH_FILE_EXTENSION,
+ FALSE);
gimp->pattern_factory =
gimp_data_loader_factory_new (gimp,
@@ -148,11 +140,17 @@ gimp_data_factories_init (Gimp *gimp)
"pattern-path",
"pattern-path-writable",
NULL,
- gimp_pattern_get_standard,
- pattern_loader_entries,
- G_N_ELEMENTS (pattern_loader_entries));
+ gimp_pattern_get_standard);
gimp_object_set_static_name (GIMP_OBJECT (gimp->pattern_factory),
"pattern factory");
+ gimp_data_loader_factory_add_loader (gimp->pattern_factory,
+ "GIMP Pattern",
+ gimp_pattern_load,
+ GIMP_PATTERN_FILE_EXTENSION,
+ FALSE);
+ gimp_data_loader_factory_add_fallback (gimp->pattern_factory,
+ "Pattern from GdkPixbuf",
+ gimp_pattern_load_pixbuf);
gimp->gradient_factory =
gimp_data_loader_factory_new (gimp,
@@ -160,11 +158,19 @@ gimp_data_factories_init (Gimp *gimp)
"gradient-path",
"gradient-path-writable",
gimp_gradient_new,
- gimp_gradient_get_standard,
- gradient_loader_entries,
- G_N_ELEMENTS (gradient_loader_entries));
+ gimp_gradient_get_standard);
gimp_object_set_static_name (GIMP_OBJECT (gimp->gradient_factory),
"gradient factory");
+ gimp_data_loader_factory_add_loader (gimp->gradient_factory,
+ "GIMP Gradient",
+ gimp_gradient_load,
+ GIMP_GRADIENT_FILE_EXTENSION,
+ TRUE);
+ gimp_data_loader_factory_add_loader (gimp->gradient_factory,
+ "SVG Gradient",
+ gimp_gradient_load_svg,
+ GIMP_GRADIENT_SVG_FILE_EXTENSION,
+ FALSE);
gimp->palette_factory =
gimp_data_loader_factory_new (gimp,
@@ -172,11 +178,14 @@ gimp_data_factories_init (Gimp *gimp)
"palette-path",
"palette-path-writable",
gimp_palette_new,
- gimp_palette_get_standard,
- palette_loader_entries,
- G_N_ELEMENTS (palette_loader_entries));
+ gimp_palette_get_standard);
gimp_object_set_static_name (GIMP_OBJECT (gimp->palette_factory),
"palette factory");
+ gimp_data_loader_factory_add_loader (gimp->palette_factory,
+ "GIMP Palette",
+ gimp_palette_load,
+ GIMP_PALETTE_FILE_EXTENSION,
+ TRUE);
gimp->font_factory =
gimp_font_factory_new (gimp,
@@ -190,11 +199,14 @@ gimp_data_factories_init (Gimp *gimp)
"tool-preset-path",
"tool-preset-path-writable",
gimp_tool_preset_new,
- NULL,
- tool_preset_loader_entries,
- G_N_ELEMENTS (tool_preset_loader_entries));
+ NULL);
gimp_object_set_static_name (GIMP_OBJECT (gimp->tool_preset_factory),
"tool preset factory");
+ gimp_data_loader_factory_add_loader (gimp->tool_preset_factory,
+ "GIMP Tool Preset",
+ gimp_tool_preset_load,
+ GIMP_TOOL_PRESET_FILE_EXTENSION,
+ TRUE);
gimp->tag_cache = gimp_tag_cache_new ();
}
diff --git a/app/core/gimpdataloaderfactory.c b/app/core/gimpdataloaderfactory.c
index b2a550a1b7..6d2ac8ab81 100644
--- a/app/core/gimpdataloaderfactory.c
+++ b/app/core/gimpdataloaderfactory.c
@@ -44,20 +44,37 @@
#define GIMP_OBSOLETE_DATA_DIR_NAME "gimp-obsolete-files"
+typedef struct _GimpDataLoader GimpDataLoader;
+
+struct _GimpDataLoader
+{
+ gchar *name;
+ GimpDataLoadFunc load_func;
+ gchar *extension;
+ gboolean writable;
+};
+
+
struct _GimpDataLoaderFactoryPrivate
{
- const GimpDataLoaderEntry *loader_entries;
- gint n_loader_entries;
+ GList *loaders;
+ GimpDataLoader *fallback;
};
#define GET_PRIVATE(obj) (((GimpDataLoaderFactory *) (obj))->priv)
+static void gimp_data_loader_factory_finalize (GObject *object);
+
static void gimp_data_loader_factory_data_init (GimpDataFactory *factory,
GimpContext *context);
static void gimp_data_loader_factory_data_refresh (GimpDataFactory *factory,
GimpContext *context);
+static GimpDataLoader *
+ gimp_data_loader_factory_get_loader (GimpDataFactory *factory,
+ GFile *file);
+
static void gimp_data_loader_factory_load (GimpDataFactory *factory,
GimpContext *context,
GHashTable *cache);
@@ -75,6 +92,12 @@ static void gimp_data_loader_factory_load_data (GimpDataFactory *factory,
GFileInfo *info,
GFile *top_directory);
+static GimpDataLoader * gimp_data_loader_new (const gchar *name,
+ GimpDataLoadFunc load_func,
+ const gchar *extension,
+ gboolean writable);
+static void gimp_data_loader_free (GimpDataLoader *loader);
+
G_DEFINE_TYPE (GimpDataLoaderFactory, gimp_data_loader_factory,
GIMP_TYPE_DATA_FACTORY)
@@ -85,8 +108,11 @@ G_DEFINE_TYPE (GimpDataLoaderFactory, gimp_data_loader_factory,
static void
gimp_data_loader_factory_class_init (GimpDataLoaderFactoryClass *klass)
{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpDataFactoryClass *factory_class = GIMP_DATA_FACTORY_CLASS (klass);
+ object_class->finalize = gimp_data_loader_factory_finalize;
+
factory_class->data_init = gimp_data_loader_factory_data_init;
factory_class->data_refresh = gimp_data_loader_factory_data_refresh;
@@ -101,6 +127,19 @@ gimp_data_loader_factory_init (GimpDataLoaderFactory *factory)
GimpDataLoaderFactoryPrivate);
}
+static void
+gimp_data_loader_factory_finalize (GObject *object)
+{
+ GimpDataLoaderFactoryPrivate *priv = GET_PRIVATE (object);
+
+ g_list_free_full (priv->loaders, (GDestroyNotify) gimp_data_loader_free);
+ priv->loaders = NULL;
+
+ g_clear_pointer (&priv->fallback, gimp_data_loader_free);
+
+ G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
static void
gimp_data_loader_factory_data_init (GimpDataFactory *factory,
GimpContext *context)
@@ -189,42 +228,89 @@ gimp_data_loader_factory_data_refresh (GimpDataFactory *factory,
/* public functions */
GimpDataFactory *
-gimp_data_loader_factory_new (Gimp *gimp,
- GType data_type,
- const gchar *path_property_name,
- const gchar *writable_property_name,
- GimpDataNewFunc new_func,
- GimpDataGetStandardFunc get_standard_func,
- const GimpDataLoaderEntry *loader_entries,
- gint n_loader_entries)
+gimp_data_loader_factory_new (Gimp *gimp,
+ GType data_type,
+ const gchar *path_property_name,
+ const gchar *writable_property_name,
+ GimpDataNewFunc new_func,
+ GimpDataGetStandardFunc get_standard_func)
{
- GimpDataLoaderFactory *factory;
-
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (g_type_is_a (data_type, GIMP_TYPE_DATA), NULL);
g_return_val_if_fail (path_property_name != NULL, NULL);
g_return_val_if_fail (writable_property_name != NULL, NULL);
- g_return_val_if_fail (loader_entries != NULL, NULL);
- g_return_val_if_fail (n_loader_entries > 0, NULL);
-
- factory = g_object_new (GIMP_TYPE_DATA_LOADER_FACTORY,
- "gimp", gimp,
- "data-type", data_type,
- "path-property-name", path_property_name,
- "writable-property-name", writable_property_name,
- "new-func", new_func,
- "get-standard-func", get_standard_func,
- NULL);
-
- factory->priv->loader_entries = loader_entries;
- factory->priv->n_loader_entries = n_loader_entries;
-
- return GIMP_DATA_FACTORY (factory);
+
+ return g_object_new (GIMP_TYPE_DATA_LOADER_FACTORY,
+ "gimp", gimp,
+ "data-type", data_type,
+ "path-property-name", path_property_name,
+ "writable-property-name", writable_property_name,
+ "new-func", new_func,
+ "get-standard-func", get_standard_func,
+ NULL);
+}
+
+void
+gimp_data_loader_factory_add_loader (GimpDataFactory *factory,
+ const gchar *name,
+ GimpDataLoadFunc load_func,
+ const gchar *extension,
+ gboolean writable)
+{
+ GimpDataLoaderFactoryPrivate *priv;
+ GimpDataLoader *loader;
+
+ g_return_if_fail (GIMP_IS_DATA_LOADER_FACTORY (factory));
+ g_return_if_fail (name != NULL);
+ g_return_if_fail (load_func != NULL);
+ g_return_if_fail (extension != NULL);
+
+ priv = GET_PRIVATE (factory);
+
+ loader = gimp_data_loader_new (name, load_func, extension, writable);
+
+ priv->loaders = g_list_append (priv->loaders, loader);
+}
+
+void
+gimp_data_loader_factory_add_fallback (GimpDataFactory *factory,
+ const gchar *name,
+ GimpDataLoadFunc load_func)
+{
+ GimpDataLoaderFactoryPrivate *priv;
+
+ g_return_if_fail (GIMP_IS_DATA_LOADER_FACTORY (factory));
+ g_return_if_fail (name != NULL);
+ g_return_if_fail (load_func != NULL);
+
+ priv = GET_PRIVATE (factory);
+
+ g_clear_pointer (&priv->fallback, gimp_data_loader_free);
+
+ priv->fallback = gimp_data_loader_new (name, load_func, NULL, FALSE);
}
/* private functions */
+static GimpDataLoader *
+gimp_data_loader_factory_get_loader (GimpDataFactory *factory,
+ GFile *file)
+{
+ GimpDataLoaderFactoryPrivate *priv = GET_PRIVATE (factory);
+ GList *list;
+
+ for (list = priv->loaders; list; list = g_list_next (list))
+ {
+ GimpDataLoader *loader = list->data;
+
+ if (gimp_file_has_extension (file, loader->extension))
+ return loader;
+ }
+
+ return priv->fallback;
+}
+
static void
gimp_data_loader_factory_load (GimpDataFactory *factory,
GimpContext *context,
@@ -323,34 +409,19 @@ gimp_data_loader_factory_load_data (GimpDataFactory *factory,
GFileInfo *info,
GFile *top_directory)
{
- GimpDataLoaderFactoryPrivate *priv = GET_PRIVATE (factory);
- const GimpDataLoaderEntry *loader = NULL;
- GimpContainer *container;
- GimpContainer *container_obsolete;
- GList *data_list = NULL;
- GInputStream *input;
- guint64 mtime;
- gint i;
- GError *error = NULL;
-
- for (i = 0; i < priv->n_loader_entries; i++)
- {
- loader = &priv->loader_entries[i];
-
- /* a loder matches if its extension matches, or if it doesn't
- * have an extension, which is the case for the fallback loader,
- * which must be last in the loader array
- */
- if (! loader->extension ||
- gimp_file_has_extension (file, loader->extension))
- {
- goto insert;
- }
- }
+ GimpDataLoader *loader;
+ GimpContainer *container;
+ GimpContainer *container_obsolete;
+ GList *data_list = NULL;
+ GInputStream *input;
+ guint64 mtime;
+ GError *error = NULL;
- return;
+ loader = gimp_data_loader_factory_get_loader (factory, file);
+
+ if (! loader)
+ return;
- insert:
container = gimp_data_factory_get_container (factory);
container_obsolete = gimp_data_factory_get_container_obsolete (factory);
@@ -465,3 +536,28 @@ gimp_data_loader_factory_load_data (GimpDataFactory *factory,
g_clear_error (&error);
}
}
+
+static GimpDataLoader *
+gimp_data_loader_new (const gchar *name,
+ GimpDataLoadFunc load_func,
+ const gchar *extension,
+ gboolean writable)
+{
+ GimpDataLoader *loader = g_slice_new (GimpDataLoader);
+
+ loader->name = g_strdup (name);
+ loader->load_func = load_func;
+ loader->extension = g_strdup (extension);
+ loader->writable = writable ? TRUE : FALSE;
+
+ return loader;
+}
+
+static void
+gimp_data_loader_free (GimpDataLoader *loader)
+{
+ g_free (loader->name);
+ g_free (loader->extension);
+
+ g_slice_free (GimpDataLoader, loader);
+}
diff --git a/app/core/gimpdataloaderfactory.h b/app/core/gimpdataloaderfactory.h
index db113e3c84..43f0d9c509 100644
--- a/app/core/gimpdataloaderfactory.h
+++ b/app/core/gimpdataloaderfactory.h
@@ -31,16 +31,6 @@ typedef GList * (* GimpDataLoadFunc) (GimpContext *context,
GError **error);
-typedef struct _GimpDataLoaderEntry GimpDataLoaderEntry;
-
-struct _GimpDataLoaderEntry
-{
- GimpDataLoadFunc load_func;
- const gchar *extension;
- gboolean writable;
-};
-
-
#define GIMP_TYPE_DATA_LOADER_FACTORY (gimp_data_loader_factory_get_type ())
#define GIMP_DATA_LOADER_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
GIMP_TYPE_DATA_LOADER_FACTORY, GimpDataLoaderFactory))
#define GIMP_DATA_LOADER_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),
GIMP_TYPE_DATA_LOADER_FACTORY, GimpDataLoaderFactoryClass))
@@ -65,16 +55,23 @@ struct _GimpDataLoaderFactoryClass
};
-GType gimp_data_loader_factory_get_type (void) G_GNUC_CONST;
+GType gimp_data_loader_factory_get_type (void) G_GNUC_CONST;
+
+GimpDataFactory * gimp_data_loader_factory_new (Gimp *gimp,
+ GType data_type,
+ const gchar *path_property_name,
+ const gchar *writable_property_name,
+ GimpDataNewFunc new_func,
+ GimpDataGetStandardFunc get_standard_func);
-GimpDataFactory * gimp_data_loader_factory_new (Gimp *gimp,
- GType data_type,
- const gchar *path_property_name,
- const gchar *writable_property_name,
- GimpDataNewFunc new_func,
- GimpDataGetStandardFunc get_standard_func,
- const GimpDataLoaderEntry *loader_entries,
- gint n_loader_entries);
+void gimp_data_loader_factory_add_loader (GimpDataFactory *factory,
+ const gchar *name,
+ GimpDataLoadFunc load_func,
+ const gchar *extension,
+ gboolean writable);
+void gimp_data_loader_factory_add_fallback (GimpDataFactory *factory,
+ const gchar *name,
+ GimpDataLoadFunc load_func);
#endif /* __GIMP_DATA_LOADER_FACTORY_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]