[gimp/metadata-improvements: 10/11] plug-ins: improve error handling and use GError in metadata-editor/viewer.




commit 0ffee001bf1dfbd97ad866bc5c157c59d3433a9e
Author: Jacob Boerema <jgboerema gmail com>
Date:   Mon Nov 30 21:38:44 2020 -0500

    plug-ins: improve error handling and use GError in metadata-editor/viewer.
    
    metadata-editor and metadata-viewer did return GIMP_PDB_SUCCESS when
    loading the dialog ui failed, we changed this to GIMP_PDB_EXECUTION_ERROR
    and now use a GError to report it back to GIMP.
    
    In metadata-editor also improve the error handling when loading the calendar
    dialog fails.
    
    The error also only showed in the console so change that to show a dialog
    or the error console.

 plug-ins/metadata/metadata-editor.c | 42 ++++++++++++++++++++-----------------
 plug-ins/metadata/metadata-viewer.c | 28 +++++++++++++++----------
 2 files changed, 40 insertions(+), 30 deletions(-)
---
diff --git a/plug-ins/metadata/metadata-editor.c b/plug-ins/metadata/metadata-editor.c
index 8ae5f7001b..dce69da56e 100644
--- a/plug-ins/metadata/metadata-editor.c
+++ b/plug-ins/metadata/metadata-editor.c
@@ -75,7 +75,8 @@ static GimpValueArray * metadata_run              (GimpProcedure        *procedu
                                                    gpointer              run_data);
 
 static gboolean metadata_editor_dialog           (GimpImage           *image,
-                                                  GimpMetadata        *metadata);
+                                                  GimpMetadata        *metadata,
+                                                  GError             **error);
 
 static void metadata_dialog_editor_set_metadata  (GExiv2Metadata      *metadata,
                                                   GtkBuilder          *builder);
@@ -473,6 +474,7 @@ metadata_run (GimpProcedure        *procedure,
 {
   GimpImage    *image;
   GimpMetadata *metadata;
+  GError       *error  = NULL;
 
   INIT_I18N ();
 
@@ -493,9 +495,10 @@ metadata_run (GimpProcedure        *procedure,
       gimp_image_set_metadata (image, metadata);
     }
 
-  metadata_editor_dialog (image, metadata);
-
-  return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
+  if (metadata_editor_dialog (image, metadata, &error))
+    return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
+  else
+    return gimp_procedure_new_return_values (procedure, GIMP_PDB_EXECUTION_ERROR, error);
 }
 
 
@@ -514,8 +517,9 @@ builder_get_widget (GtkBuilder  *builder,
 }
 
 static gboolean
-metadata_editor_dialog (GimpImage    *image,
-                        GimpMetadata *g_metadata)
+metadata_editor_dialog (GimpImage     *image,
+                        GimpMetadata  *g_metadata,
+                        GError       **error)
 {
   GtkBuilder     *builder;
   GtkWidget      *dialog;
@@ -526,7 +530,7 @@ metadata_editor_dialog (GimpImage    *image,
   gchar          *ui_file;
   gchar          *title;
   gchar          *name;
-  GError         *error = NULL;
+  GError         *local_error = NULL;
   gboolean        run;
 
   metadata = GEXIV2_METADATA (g_metadata);
@@ -542,13 +546,14 @@ metadata_editor_dialog (GimpImage    *image,
   ui_file = g_build_filename (gimp_data_directory (),
                               "ui", "plug-ins", "plug-in-metadata-editor.ui", NULL);
 
-  if (! gtk_builder_add_from_file (builder, ui_file, &error))
+  if (! gtk_builder_add_from_file (builder, ui_file, &local_error))
     {
-      g_printerr ("Error occurred while loading UI file!\n");
-      g_printerr ("Message: %s\n", error->message);
-      g_clear_error (&error);
-      if (ui_file)
-        g_free (ui_file);
+      if (! local_error)
+        local_error = g_error_new_literal (G_FILE_ERROR, 0,
+                                           _("Error loading metadata-editor dialog."));
+      g_propagate_error (error, local_error);
+
+      g_free (ui_file);
       g_object_unref (builder);
       return FALSE;
     }
@@ -798,12 +803,11 @@ on_date_button_clicked (GtkButton *widget,
 
   if (! gtk_builder_add_from_file (builder, ui_file, &error))
     {
-      g_printerr ("Error occurred while loading UI file!\n");
-      if (error != NULL)
-        {
-          g_printerr ("Message: %s\n", error->message);
-          g_clear_error (&error);
-        }
+      g_log ("", G_LOG_LEVEL_MESSAGE,
+             _("Error loading calendar. %s"),
+             error ? error->message : "");
+      g_clear_error (&error);
+
       if (ui_file)
         g_free (ui_file);
       g_object_unref (builder);
diff --git a/plug-ins/metadata/metadata-viewer.c b/plug-ins/metadata/metadata-viewer.c
index 01ddc03625..640e4af931 100644
--- a/plug-ins/metadata/metadata-viewer.c
+++ b/plug-ins/metadata/metadata-viewer.c
@@ -99,7 +99,8 @@ static GimpValueArray * metadata_run              (GimpProcedure        *procedu
                                                    gpointer              run_data);
 
 static gboolean  metadata_viewer_dialog           (GimpImage      *image,
-                                                   GimpMetadata   *g_metadata);
+                                                   GimpMetadata   *g_metadata,
+                                                   GError        **error);
 static void      metadata_dialog_set_metadata     (GExiv2Metadata *metadata,
                                                    GtkBuilder     *builder);
 static void      metadata_dialog_append_tags      (GExiv2Metadata  *metadata,
@@ -208,6 +209,7 @@ metadata_run (GimpProcedure        *procedure,
 {
   GimpImage    *image;
   GimpMetadata *metadata;
+  GError       *error  = NULL;
 
   INIT_I18N ();
 
@@ -228,14 +230,16 @@ metadata_run (GimpProcedure        *procedure,
       gimp_image_set_metadata (image, metadata);
     }
 
-  metadata_viewer_dialog (image, metadata);
-
-  return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
+  if (metadata_viewer_dialog (image, metadata, &error))
+    return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
+  else
+    return gimp_procedure_new_return_values (procedure, GIMP_PDB_EXECUTION_ERROR, error);
 }
 
 static gboolean
-metadata_viewer_dialog (GimpImage    *image,
-                        GimpMetadata *g_metadata)
+metadata_viewer_dialog (GimpImage     *image,
+                        GimpMetadata  *g_metadata,
+                        GError       **error)
 {
   GtkBuilder     *builder;
   GtkWidget      *dialog;
@@ -244,7 +248,7 @@ metadata_viewer_dialog (GimpImage    *image,
   gchar          *ui_file;
   gchar          *title;
   gchar          *name;
-  GError         *error = NULL;
+  GError         *local_error = NULL;
   GExiv2Metadata *metadata;
 
   metadata = GEXIV2_METADATA(g_metadata);
@@ -254,11 +258,13 @@ metadata_viewer_dialog (GimpImage    *image,
   ui_file = g_build_filename (gimp_data_directory (),
                               "ui", "plug-ins", "plug-in-metadata-viewer.ui", NULL);
 
-  if (! gtk_builder_add_from_file (builder, ui_file, &error))
+  if (! gtk_builder_add_from_file (builder, ui_file, &local_error))
     {
-      g_printerr ("Error occurred while loading UI file!\n");
-      g_printerr ("Message: %s\n", error->message);
-      g_clear_error (&error);
+      if (! local_error)
+        local_error = g_error_new_literal (G_FILE_ERROR, 0,
+                                           _("Error loading metadata-viewer dialog."));
+      g_propagate_error (error, local_error);
+
       g_free (ui_file);
       g_object_unref (builder);
       return FALSE;


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