[gimp] app: load the mypaint brushes on startup, and do much more error checking



commit 6281d130e4568e0d812a1bab52f0bcbeac5f9f44
Author: Michael Natterer <mitch gimp org>
Date:   Mon Dec 28 18:09:37 2015 +0100

    app: load the mypaint brushes on startup, and do much more error checking
    
    Everything expects that a data object can simply be used, so make it
    behave like all other data objects and reject broken files.

 app/core/gimpmybrush-load.c    |   95 ++++++++++++++++++++++++++++++++-------
 app/core/gimpmybrush-private.h |   10 ++---
 app/core/gimpmybrush.c         |   59 -------------------------
 3 files changed, 82 insertions(+), 82 deletions(-)
---
diff --git a/app/core/gimpmybrush-load.c b/app/core/gimpmybrush-load.c
index 4b3f6c9..83ef4ce 100644
--- a/app/core/gimpmybrush-load.c
+++ b/app/core/gimpmybrush-load.c
@@ -22,6 +22,8 @@
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <gegl.h>
 
+#include <mypaint-brush.h>
+
 #include "libgimpbase/gimpbase.h"
 
 #include "core-types.h"
@@ -41,16 +43,58 @@ gimp_mybrush_load (GimpContext   *context,
                    GInputStream  *input,
                    GError       **error)
 {
-  GimpBrush *brush = NULL;
-  GdkPixbuf *pixbuf;
-  gchar     *path;
-  gchar     *basename;
-  gchar     *preview_filename;
+  GimpMybrush  *brush = NULL;
+  MyPaintBrush *mypaint_brush;
+  GdkPixbuf    *pixbuf;
+  GFileInfo    *info;
+  guint64       size;
+  guchar       *buffer;
+  gchar        *path;
+  gchar        *basename;
+  gchar        *preview_filename;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
   g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
+  info = g_file_query_info (file,
+                            G_FILE_ATTRIBUTE_STANDARD_SIZE,
+                            G_FILE_QUERY_INFO_NONE,
+                            NULL, error);
+  if (! info)
+    return NULL;
+
+  size = g_file_info_get_attribute_uint64 (info,
+                                           G_FILE_ATTRIBUTE_STANDARD_SIZE);
+  g_object_unref (info);
+
+  if (size > 32768)
+    {
+      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
+                   _("MyPaint brush file is unreasonably large, skipping."));
+      return NULL;
+    }
+
+  buffer = g_new0 (guchar, size + 1);
+
+  if (! g_input_stream_read_all (input, buffer, size, NULL, NULL, error))
+    {
+      g_free (buffer);
+      return NULL;
+    }
+
+  mypaint_brush = mypaint_brush_new ();
+  mypaint_brush_from_defaults (mypaint_brush);
+
+  if (! mypaint_brush_from_string (mypaint_brush, (const gchar *) buffer))
+    {
+      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
+                   _("Failed to deserialize MyPaint brush."));
+      mypaint_brush_unref (mypaint_brush);
+      g_free (buffer);
+      return NULL;
+    }
+
   path = g_file_get_path (file);
   basename = g_strndup (path, strlen (path) - 4);
   g_free (path);
@@ -62,22 +106,39 @@ gimp_mybrush_load (GimpContext   *context,
                                              48, 48, error);
   g_free (preview_filename);
 
-  if (pixbuf)
+  if (! pixbuf)
     {
-      basename = g_file_get_basename (file);
+      mypaint_brush_unref (mypaint_brush);
+      g_free (buffer);
+      return NULL;
+    }
 
-      brush = g_object_new (GIMP_TYPE_MYBRUSH,
-                            "name",        gimp_filename_to_utf8 (basename),
-                            "mime-type",   "image/x-gimp-myb",
-                            "icon-pixbuf", pixbuf,
-                            NULL);
+  basename = g_file_get_basename (file);
 
-      g_free (basename);
-      g_object_unref (pixbuf);
-    }
+  brush = g_object_new (GIMP_TYPE_MYBRUSH,
+                        "name",        gimp_filename_to_utf8 (basename),
+                        "mime-type",   "image/x-gimp-myb",
+                        "icon-pixbuf", pixbuf,
+                        NULL);
 
-  if (! brush)
-    return NULL;
+  g_free (basename);
+  g_object_unref (pixbuf);
+
+  brush->priv->brush_json = (gchar *) buffer;
+
+  brush->priv->radius =
+    mypaint_brush_get_base_value (mypaint_brush,
+                                  MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC);
+
+  brush->priv->opaque =
+    mypaint_brush_get_base_value (mypaint_brush,
+                                  MYPAINT_BRUSH_SETTING_OPAQUE);
+
+  brush->priv->hardness =
+    mypaint_brush_get_base_value (mypaint_brush,
+                                  MYPAINT_BRUSH_SETTING_HARDNESS);
+
+  mypaint_brush_unref (mypaint_brush);
 
   return g_list_prepend (NULL, brush);
 }
diff --git a/app/core/gimpmybrush-private.h b/app/core/gimpmybrush-private.h
index c89978d..f3e9f46 100644
--- a/app/core/gimpmybrush-private.h
+++ b/app/core/gimpmybrush-private.h
@@ -23,12 +23,10 @@
 
 struct _GimpMybrushPrivate
 {
-  gboolean  json_loaded;
-
-  gchar    *brush_json;
-  gdouble   radius;
-  gdouble   opaque;
-  gdouble   hardness;
+  gchar   *brush_json;
+  gdouble  radius;
+  gdouble  opaque;
+  gdouble  hardness;
 };
 
 
diff --git a/app/core/gimpmybrush.c b/app/core/gimpmybrush.c
index 53199fc..8bf6eb4 100644
--- a/app/core/gimpmybrush.c
+++ b/app/core/gimpmybrush.c
@@ -23,8 +23,6 @@
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <gegl.h>
 
-#include <mypaint-brush.h>
-
 #include "core-types.h"
 
 #include "gimpmybrush.h"
@@ -230,59 +228,11 @@ gimp_mybrush_get_standard (GimpContext *context)
   return standard_mybrush;
 }
 
-static void
-gimp_mybrush_load_json (GimpMybrush *brush)
-{
-  GFile        *file          = gimp_data_get_file (GIMP_DATA (brush));
-  MyPaintBrush *mypaint_brush = mypaint_brush_new ();
-
-  mypaint_brush_from_defaults (mypaint_brush);
-
-  if (file)
-    {
-      gchar *path = g_file_get_path (file);
-
-      if (g_file_get_contents (path, &brush->priv->brush_json, NULL, NULL))
-        {
-          if (! mypaint_brush_from_string (mypaint_brush,
-                                           brush->priv->brush_json))
-            {
-              g_printerr ("Failed to deserialize MyPaint brush\n");
-              g_free (brush->priv->brush_json);
-              brush->priv->brush_json = NULL;
-            }
-        }
-      else
-        {
-          g_printerr ("Failed to load MyPaint brush\n");
-        }
-    }
-
-  brush->priv->radius =
-    mypaint_brush_get_base_value (mypaint_brush,
-                                  MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC);
-
-  brush->priv->opaque =
-    mypaint_brush_get_base_value (mypaint_brush,
-                                  MYPAINT_BRUSH_SETTING_OPAQUE);
-
-  brush->priv->hardness =
-    mypaint_brush_get_base_value (mypaint_brush,
-                                  MYPAINT_BRUSH_SETTING_HARDNESS);
-
-  mypaint_brush_unref (mypaint_brush);
-
-  brush->priv->json_loaded = TRUE;
-}
-
 const gchar *
 gimp_mybrush_get_brush_json (GimpMybrush *brush)
 {
   g_return_val_if_fail (GIMP_IS_MYBRUSH (brush), NULL);
 
-  if (! brush->priv->json_loaded)
-    gimp_mybrush_load_json (brush);
-
   return brush->priv->brush_json;
 }
 
@@ -291,9 +241,6 @@ gimp_mybrush_get_radius (GimpMybrush *brush)
 {
   g_return_val_if_fail (GIMP_IS_MYBRUSH (brush), 1.0);
 
-  if (! brush->priv->json_loaded)
-    gimp_mybrush_load_json (brush);
-
   return brush->priv->radius;
 }
 
@@ -302,9 +249,6 @@ gimp_mybrush_get_opaque (GimpMybrush *brush)
 {
   g_return_val_if_fail (GIMP_IS_MYBRUSH (brush), 1.0);
 
-  if (! brush->priv->json_loaded)
-    gimp_mybrush_load_json (brush);
-
   return brush->priv->opaque;
 }
 
@@ -313,8 +257,5 @@ gimp_mybrush_get_hardness (GimpMybrush *brush)
 {
   g_return_val_if_fail (GIMP_IS_MYBRUSH (brush), 1.0);
 
-  if (! brush->priv->json_loaded)
-    gimp_mybrush_load_json (brush);
-
   return brush->priv->hardness;
 }


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