[gimp] libgimp: add gimp_pdb_get_last_error() and _get_last_status()



commit 1c9b3c7055255fc5ee27ca866c891aa7e3b9fc98
Author: Michael Natterer <mitch gimp org>
Date:   Sun Aug 11 16:41:58 2019 +0200

    libgimp: add gimp_pdb_get_last_error() and _get_last_status()
    
    and move the old API to gimplegacy.[ch].

 libgimp/gimp-private.h     |   1 -
 libgimp/gimp.c             |  98 ------------------------------------
 libgimp/gimp.def           |   2 +
 libgimp/gimp.h             |   6 ---
 libgimp/gimplegacy.c       | 118 ++++++++++++++++++++++++++++++++++++++++---
 libgimp/gimplegacy.h       |   5 ++
 libgimp/gimppdb.c          | 121 +++++++++++++++++++++++++++++++++++++++++++--
 libgimp/gimppdb.h          |   3 ++
 libgimp/gimpplugin.c       |   6 +--
 plug-ins/common/file-pat.c |   2 +-
 10 files changed, 243 insertions(+), 119 deletions(-)
---
diff --git a/libgimp/gimp-private.h b/libgimp/gimp-private.h
index 49faa889b3..107f5a2471 100644
--- a/libgimp/gimp-private.h
+++ b/libgimp/gimp-private.h
@@ -29,7 +29,6 @@ gint   _gimp_main_internal (GType                 plug_in_type,
                             gint                  argc,
                             gchar                *argv[]);
 void   _gimp_config        (GPConfig             *config);
-void   _gimp_set_pdb_error (GimpValueArray       *return_vals);
 
 
 G_END_DECLS
diff --git a/libgimp/gimp.c b/libgimp/gimp.c
index 37121dec99..9e1c762f0e 100644
--- a/libgimp/gimp.c
+++ b/libgimp/gimp.c
@@ -161,10 +161,6 @@ static const gchar        *progname          = NULL;
 static GimpStackTraceMode  stack_trace_mode  = GIMP_STACK_TRACE_NEVER;
 
 
-static GimpPDBStatusType   pdb_error_status  = GIMP_PDB_SUCCESS;
-static gchar              *pdb_error_message = NULL;
-
-
 /**
  * gimp_main:
  * @plug_in_type: the type of the #GimpPlugIn subclass of the plug-in
@@ -684,68 +680,6 @@ gimp_quit (void)
   exit (EXIT_SUCCESS);
 }
 
-/**
- * gimp_get_pdb_error:
- *
- * Retrieves the error message from the last procedure call.
- *
- * If a procedure call fails, then it might pass an error message with
- * the return values. Plug-ins that are using the libgimp C wrappers
- * don't access the procedure return values directly. Thus libgimp
- * stores the error message and makes it available with this
- * function. The next procedure call unsets the error message again.
- *
- * The returned string is owned by libgimp and must not be freed or
- * modified.
- *
- * Returns: the error message
- *
- * Since: 2.6
- **/
-const gchar *
-gimp_get_pdb_error (void)
-{
-  if (pdb_error_message && strlen (pdb_error_message))
-    return pdb_error_message;
-
-  switch (pdb_error_status)
-    {
-    case GIMP_PDB_SUCCESS:
-      /*  procedure executed successfully  */
-      return _("success");
-
-    case GIMP_PDB_EXECUTION_ERROR:
-      /*  procedure execution failed       */
-      return _("execution error");
-
-    case GIMP_PDB_CALLING_ERROR:
-      /*  procedure called incorrectly     */
-      return _("calling error");
-
-    case GIMP_PDB_CANCEL:
-      /*  procedure execution cancelled    */
-      return _("cancelled");
-
-    default:
-      return "invalid return status";
-    }
-}
-
-/**
- * gimp_get_pdb_status:
- *
- * Retrieves the status from the last procedure call.
- *
- * Returns: the #GimpPDBStatusType.
- *
- * Since: 2.10
- **/
-GimpPDBStatusType
-gimp_get_pdb_status (void)
-{
-  return pdb_error_status;
-}
-
 /**
  * gimp_tile_width:
  *
@@ -1233,35 +1167,3 @@ _gimp_config (GPConfig *config)
 
   _gimp_shm_open (config->shm_ID);
 }
-
-void
-_gimp_set_pdb_error (GimpValueArray *return_values)
-{
-  g_clear_pointer (&pdb_error_message, g_free);
-  pdb_error_status = GIMP_PDB_SUCCESS;
-
-  if (gimp_value_array_length (return_values) > 0)
-    {
-      pdb_error_status =
-        g_value_get_enum (gimp_value_array_index (return_values, 0));
-
-      switch (pdb_error_status)
-        {
-        case GIMP_PDB_SUCCESS:
-        case GIMP_PDB_PASS_THROUGH:
-          break;
-
-        case GIMP_PDB_EXECUTION_ERROR:
-        case GIMP_PDB_CALLING_ERROR:
-        case GIMP_PDB_CANCEL:
-          if (gimp_value_array_length (return_values) > 1)
-            {
-              GValue *value = gimp_value_array_index (return_values, 1);
-
-              if (G_VALUE_HOLDS_STRING (value))
-                pdb_error_message = g_value_dup_string (value);
-            }
-          break;
-        }
-    }
-}
diff --git a/libgimp/gimp.def b/libgimp/gimp.def
index 657eb5862b..ee97894867 100644
--- a/libgimp/gimp.def
+++ b/libgimp/gimp.def
@@ -635,6 +635,8 @@ EXPORTS
        gimp_pdb_dump_to_file
        gimp_pdb_get_data
        gimp_pdb_get_data_size
+       gimp_pdb_get_last_error
+       gimp_pdb_get_last_status
        gimp_pdb_get_type
        gimp_pdb_lookup_procedure
        gimp_pdb_proc_arg
diff --git a/libgimp/gimp.h b/libgimp/gimp.h
index bdfa69481e..846d8758cd 100644
--- a/libgimp/gimp.h
+++ b/libgimp/gimp.h
@@ -157,12 +157,6 @@ GimpPDB           * gimp_get_pdb              (void);
  */
 void                gimp_quit                 (void) G_GNUC_NORETURN;
 
-/* Retrieve the error message and return status for the last procedure
- * call.
- */
-const gchar       * gimp_get_pdb_error        (void);
-GimpPDBStatusType   gimp_get_pdb_status       (void);
-
 /* Return various constants given by the GIMP core at plug-in config time.
  */
 guint               gimp_tile_width           (void) G_GNUC_CONST;
diff --git a/libgimp/gimplegacy.c b/libgimp/gimplegacy.c
index efcee94b4c..d26e0988a3 100644
--- a/libgimp/gimplegacy.c
+++ b/libgimp/gimplegacy.c
@@ -35,6 +35,8 @@
 #include "gimpplugin_pdb.h"
 #include "gimplegacy-private.h"
 
+#include "libgimp-intl.h"
+
 
 /**
  * SECTION: gimplegacy
@@ -81,17 +83,21 @@ static gboolean   gimp_write                   (GIOChannel      *channel,
                                                 gpointer         user_data);
 static gboolean   gimp_flush                   (GIOChannel      *channel,
                                                 gpointer         user_data);
+static void       gimp_set_pdb_error           (GimpValueArray  *return_vals);
+
 
+GIOChannel                *_gimp_readchannel  = NULL;
+GIOChannel                *_gimp_writechannel = NULL;
 
-GIOChannel            *_gimp_readchannel  = NULL;
-GIOChannel            *_gimp_writechannel = NULL;
+static gchar               write_buffer[WRITE_BUFFER_SIZE];
+static gulong              write_buffer_index = 0;
 
-static gchar           write_buffer[WRITE_BUFFER_SIZE];
-static gulong          write_buffer_index = 0;
+static GimpPlugInInfo      PLUG_IN_INFO       = { 0, };
 
-static GimpPlugInInfo  PLUG_IN_INFO       = { 0, };
+static GHashTable         *gimp_temp_proc_ht  = NULL;
 
-static GHashTable     *gimp_temp_proc_ht  = NULL;
+static GimpPDBStatusType   pdb_error_status  = GIMP_PDB_SUCCESS;
+static gchar              *pdb_error_message = NULL;
 
 
 /**
@@ -822,7 +828,7 @@ gimp_run_procedure_array (const gchar          *name,
 
   gimp_wire_destroy (&msg);
 
-  _gimp_set_pdb_error (return_values);
+  gimp_set_pdb_error (return_values);
 
   return return_values;
 }
@@ -938,6 +944,72 @@ gimp_destroy_paramdefs (GimpParamDef *paramdefs,
   g_free (paramdefs);
 }
 
+/**
+ * gimp_get_pdb_error:
+ *
+ * Retrieves the error message from the last procedure call.
+ *
+ * If a procedure call fails, then it might pass an error message with
+ * the return values. Plug-ins that are using the libgimp C wrappers
+ * don't access the procedure return values directly. Thus libgimp
+ * stores the error message and makes it available with this
+ * function. The next procedure call unsets the error message again.
+ *
+ * The returned string is owned by libgimp and must not be freed or
+ * modified.
+ *
+ * Returns: the error message
+ *
+ * Since: 2.6
+ **/
+const gchar *
+gimp_get_pdb_error (void)
+{
+  ASSERT_NO_PLUG_IN_EXISTS (G_STRFUNC);
+
+  if (pdb_error_message && strlen (pdb_error_message))
+    return pdb_error_message;
+
+  switch (pdb_error_status)
+    {
+    case GIMP_PDB_SUCCESS:
+      /*  procedure executed successfully  */
+      return _("success");
+
+    case GIMP_PDB_EXECUTION_ERROR:
+      /*  procedure execution failed       */
+      return _("execution error");
+
+    case GIMP_PDB_CALLING_ERROR:
+      /*  procedure called incorrectly     */
+      return _("calling error");
+
+    case GIMP_PDB_CANCEL:
+      /*  procedure execution cancelled    */
+      return _("cancelled");
+
+    default:
+      return "invalid return status";
+    }
+}
+
+/**
+ * gimp_get_pdb_status:
+ *
+ * Retrieves the status from the last procedure call.
+ *
+ * Returns: the #GimpPDBStatusType.
+ *
+ * Since: 2.10
+ **/
+GimpPDBStatusType
+gimp_get_pdb_status (void)
+{
+  ASSERT_NO_PLUG_IN_EXISTS (G_STRFUNC);
+
+  return pdb_error_status;
+}
+
 void
 _gimp_legacy_initialize (const GimpPlugInInfo *info,
                          GIOChannel           *read_channel,
@@ -1966,3 +2038,35 @@ gimp_flush (GIOChannel *channel,
 
   return TRUE;
 }
+
+static void
+gimp_set_pdb_error (GimpValueArray *return_values)
+{
+  g_clear_pointer (&pdb_error_message, g_free);
+  pdb_error_status = GIMP_PDB_SUCCESS;
+
+  if (gimp_value_array_length (return_values) > 0)
+    {
+      pdb_error_status =
+        g_value_get_enum (gimp_value_array_index (return_values, 0));
+
+      switch (pdb_error_status)
+        {
+        case GIMP_PDB_SUCCESS:
+        case GIMP_PDB_PASS_THROUGH:
+          break;
+
+        case GIMP_PDB_EXECUTION_ERROR:
+        case GIMP_PDB_CALLING_ERROR:
+        case GIMP_PDB_CANCEL:
+          if (gimp_value_array_length (return_values) > 1)
+            {
+              GValue *value = gimp_value_array_index (return_values, 1);
+
+              if (G_VALUE_HOLDS_STRING (value))
+                pdb_error_message = g_value_dup_string (value);
+            }
+          break;
+        }
+    }
+}
diff --git a/libgimp/gimplegacy.h b/libgimp/gimplegacy.h
index d880185c2e..80e0e1e34b 100644
--- a/libgimp/gimplegacy.h
+++ b/libgimp/gimplegacy.h
@@ -323,6 +323,11 @@ void           gimp_destroy_params      (GimpParam       *params,
 void           gimp_destroy_paramdefs   (GimpParamDef    *paramdefs,
                                          gint             n_params);
 
+/* Retrieve the error message and return status for the last procedure
+ * call.
+ */
+const gchar       * gimp_get_pdb_error  (void);
+GimpPDBStatusType   gimp_get_pdb_status (void);
 
 /* gimp_plugin API that should now be done by using GimpPlugIn
  */
diff --git a/libgimp/gimppdb.c b/libgimp/gimppdb.c
index 25c6727fc1..f155e6535f 100644
--- a/libgimp/gimppdb.c
+++ b/libgimp/gimppdb.c
@@ -34,6 +34,8 @@
 #include "gimppdbprocedure.h"
 #include "gimpplugin-private.h"
 
+#include "libgimp-intl.h"
+
 
 /**
  * SECTION: gimppdb
@@ -51,10 +53,16 @@ struct _GimpPDBPrivate
   GimpPlugIn *plug_in;
 
   GHashTable *procedures;
+
+  GimpPDBStatusType   error_status;
+  gchar              *error_message;
 };
 
 
-static void   gimp_pdb_finalize (GObject *object);
+static void   gimp_pdb_finalize  (GObject        *object);
+
+static void   gimp_pdb_set_error (GimpPDB        *pdb,
+                                  GimpValueArray *return_values);
 
 
 G_DEFINE_TYPE_WITH_PRIVATE (GimpPDB, gimp_pdb, G_TYPE_OBJECT)
@@ -77,6 +85,8 @@ gimp_pdb_init (GimpPDB *pdb)
 
   pdb->priv->procedures = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  g_free, g_object_unref);
+
+  pdb->priv->error_status = GIMP_PDB_SUCCESS;
 }
 
 static void
@@ -85,7 +95,8 @@ gimp_pdb_finalize (GObject *object)
   GimpPDB *pdb = GIMP_PDB (object);
 
   g_clear_object (&pdb->priv->plug_in);
-  g_clear_pointer (&pdb->priv->procedures, g_hash_table_unref);
+  g_clear_pointer (&pdb->priv->procedures,    g_hash_table_unref);
+  g_clear_pointer (&pdb->priv->error_message, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -305,7 +316,7 @@ gimp_pdb_run_procedure_array (GimpPDB              *pdb,
 
   gimp_wire_destroy (&msg);
 
-  _gimp_set_pdb_error (return_values);
+  gimp_pdb_set_error (pdb, return_values);
 
   return return_values;
 }
@@ -481,6 +492,74 @@ gimp_pdb_proc_return_value (const gchar *procedure_name,
   return _gimp_pdb_proc_return_value (procedure_name, val_num);
 }
 
+/**
+ * gimp_pdb_get_last_error:
+ * @pdb: a #GimpPDB.
+ *
+ * Retrieves the error message from the last procedure call.
+ *
+ * If a procedure call fails, then it might pass an error message with
+ * the return values. Plug-ins that are using the libgimp C wrappers
+ * don't access the procedure return values directly. Thus #GimpPDB
+ * stores the error message and makes it available with this
+ * function. The next procedure call unsets the error message again.
+ *
+ * The returned string is owned by @pdb and must not be freed or
+ * modified.
+ *
+ * Returns: the error message
+ *
+ * Since: 3.0
+ **/
+const gchar *
+gimp_pdb_get_last_error (GimpPDB *pdb)
+{
+  g_return_val_if_fail (GIMP_IS_PDB (pdb), NULL);
+
+  if (pdb->priv->error_message && strlen (pdb->priv->error_message))
+    return pdb->priv->error_message;
+
+  switch (pdb->priv->error_status)
+    {
+    case GIMP_PDB_SUCCESS:
+      /*  procedure executed successfully  */
+      return _("success");
+
+    case GIMP_PDB_EXECUTION_ERROR:
+      /*  procedure execution failed       */
+      return _("execution error");
+
+    case GIMP_PDB_CALLING_ERROR:
+      /*  procedure called incorrectly     */
+      return _("calling error");
+
+    case GIMP_PDB_CANCEL:
+      /*  procedure execution cancelled    */
+      return _("cancelled");
+
+    default:
+      return "invalid return status";
+    }
+}
+
+/**
+ * gimp_pdb_get_last_status:
+ * @pdb: a #GimpPDB.
+ *
+ * Retrieves the status from the last procedure call.
+ *
+ * Returns: the #GimpPDBStatusType.
+ *
+ * Since: 3.0
+ **/
+GimpPDBStatusType
+gimp_pdb_get_last_status (GimpPDB *pdb)
+{
+  g_return_val_if_fail (GIMP_IS_PDB (pdb), GIMP_PDB_SUCCESS);
+
+  return pdb->priv->error_status;
+}
+
 /*  Cruft API  */
 
 /**
@@ -555,3 +634,39 @@ gimp_pdb_set_data (const gchar   *identifier,
 {
   return _gimp_pdb_set_data (identifier, bytes, data);
 }
+
+
+/*  private functions  */
+
+static void
+gimp_pdb_set_error (GimpPDB        *pdb,
+                    GimpValueArray *return_values)
+{
+  g_clear_pointer (&pdb->priv->error_message, g_free);
+  pdb->priv->error_status = GIMP_PDB_SUCCESS;
+
+  if (gimp_value_array_length (return_values) > 0)
+    {
+      pdb->priv->error_status =
+        g_value_get_enum (gimp_value_array_index (return_values, 0));
+
+      switch (pdb->priv->error_status)
+        {
+        case GIMP_PDB_SUCCESS:
+        case GIMP_PDB_PASS_THROUGH:
+          break;
+
+        case GIMP_PDB_EXECUTION_ERROR:
+        case GIMP_PDB_CALLING_ERROR:
+        case GIMP_PDB_CANCEL:
+          if (gimp_value_array_length (return_values) > 1)
+            {
+              GValue *value = gimp_value_array_index (return_values, 1);
+
+              if (G_VALUE_HOLDS_STRING (value))
+                pdb->priv->error_message = g_value_dup_string (value);
+            }
+          break;
+        }
+    }
+}
diff --git a/libgimp/gimppdb.h b/libgimp/gimppdb.h
index cfb5eebf50..b834e0df6e 100644
--- a/libgimp/gimppdb.h
+++ b/libgimp/gimppdb.h
@@ -100,6 +100,9 @@ gchar         ** gimp_pdb_query_procedures     (GimpPDB              *pdb,
                                                 const gchar          *proc_type,
                                                 gint                 *num_matches);
 
+const gchar       * gimp_pdb_get_last_error    (GimpPDB              *pdb);
+GimpPDBStatusType   gimp_pdb_get_last_status   (GimpPDB              *pdb);
+
 /*  Temporary API, to go away before 3.0  */
 
 GParamSpec * gimp_pdb_proc_argument     (const gchar *procedure_name,
diff --git a/libgimp/gimpplugin.c b/libgimp/gimpplugin.c
index 8e45d0b482..5f0e4d84f2 100644
--- a/libgimp/gimpplugin.c
+++ b/libgimp/gimpplugin.c
@@ -618,9 +618,9 @@ gimp_plug_in_extension_process (GimpPlugIn *plug_in,
  * call made by a plug-in fails. Using this procedure the plug-in can
  * change this behavior. If the error handler is set to
  * %GIMP_PDB_ERROR_HANDLER_PLUGIN, then the plug-in is responsible for
- * calling gimp_get_pdb_error() and handling the error whenever one if
- * its procedure calls fails. It can do this by displaying the error
- * message or by forwarding it in its own return values.
+ * calling gimp_pdb_get_last_error() and handling the error whenever
+ * one if its procedure calls fails. It can do this by displaying the
+ * error message or by forwarding it in its own return values.
  *
  * Since: 3.0
  **/
diff --git a/plug-ins/common/file-pat.c b/plug-ins/common/file-pat.c
index d8da79e50f..98090925dd 100644
--- a/plug-ins/common/file-pat.c
+++ b/plug-ins/common/file-pat.c
@@ -262,7 +262,7 @@ pat_save (GimpProcedure        *procedure,
           g_set_error (&error, 0, 0,
                        "Running procedure 'file-pat-save-internal' "
                        "failed: %s",
-                       gimp_get_pdb_error ());
+                       gimp_pdb_get_last_error (gimp_get_pdb ()));
 
           status = GIMP_PDB_EXECUTION_ERROR;
         }


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