[ghex] Minor error handling improvements; appwindow: Hide spinner on file->new



commit a72e5b0a94ed5af11bae99b87b3a54da45a06738
Author: Logan Rathbone <poprocks gmail com>
Date:   Fri Dec 17 23:18:45 2021 -0500

    Minor error handling improvements; appwindow: Hide spinner on file->new
    
    Latter: regression/bugfix from adding the document-loading spinner.

 src/ghex-application-window.c | 32 +++++++++++++++++++++-----------
 src/hex-buffer-iface.c        |  3 +++
 src/hex-buffer-malloc.c       |  6 +-----
 src/hex-document.c            |  2 +-
 4 files changed, 26 insertions(+), 17 deletions(-)
---
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index 987f71b..98a6dd9 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -694,9 +694,9 @@ document_loaded_or_saved_common (GHexApplicationWindow *self,
 }
 
 static void
-file_loaded_cb (HexDocument *doc, gpointer user_data)
+file_loaded_cb (HexDocument *doc, GHexApplicationWindow *self)
 {
-       GHexApplicationWindow *self = GHEX_APPLICATION_WINDOW(user_data);
+       g_return_if_fail (GHEX_IS_APPLICATION_WINDOW (self));
        
        gtk_spinner_stop (GTK_SPINNER(self->doc_loading_spinner));
        gtk_widget_hide (self->doc_loading_spinner);
@@ -1221,6 +1221,7 @@ new_file (GtkWidget *widget,
        refresh_dialogs (self);
        ghex_application_window_activate_tab (self, gh);
        ghex_application_window_set_insert_mode (self, TRUE);
+       file_loaded_cb (doc, self);
 }
 
 static GtkHex *
@@ -1230,8 +1231,8 @@ new_gh_from_gfile (GFile *file)
        GtkHex *gh;
 
        doc = hex_document_new_from_file (file);
-       /* FIXME - just return NULL and handle error in user-friendly manner? */
-       g_return_val_if_fail (HEX_IS_DOCUMENT (doc), NULL);
+       if (! doc)
+               return NULL;
                
        gh = GTK_HEX(gtk_hex_new (doc));
 
@@ -1253,11 +1254,7 @@ open_response_cb (GtkNativeDialog *dialog,
 
                ghex_application_window_open_file (self, file);
        }
-       else
-       {
-               g_debug ("%s: User didn't click Open. Bail out.",
-                               __func__);
-       }
+
        g_object_unref (dialog);
 }
 
@@ -2082,12 +2079,25 @@ ghex_application_window_open_file (GHexApplicationWindow *self, GFile *file)
        g_object_ref (file);
 
        gh = new_gh_from_gfile (file);
+       g_object_unref (file);
+
+       if (! gh)
+       {
+               char *error_msg = N_("There was an error loading the requested file. "
+                               "The file either no longer exists, is inaccessible, "
+                               "or you may not have permission to access the file.");
+
+               display_error_dialog (GTK_WINDOW(self), error_msg);
+
+               /* This fcn is also used to handle open operations on the cmdline. */
+               g_printerr ("%s\n", error_msg);
+
+               return;
+       }
 
        ghex_application_window_add_hex (self, gh);
        refresh_dialogs (self);
        ghex_application_window_activate_tab (self, gh);
-
-       g_object_unref (file);
 }
                        
 GtkHex *
diff --git a/src/hex-buffer-iface.c b/src/hex-buffer-iface.c
index 91d69e4..df3a635 100644
--- a/src/hex-buffer-iface.c
+++ b/src/hex-buffer-iface.c
@@ -151,5 +151,8 @@ hex_buffer_util_get_file_size (GFile *file)
        info = g_file_query_info (file,
                        G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
 
+       if (! info)
+               return 0;
+
        return g_file_info_get_size (info);
 }
diff --git a/src/hex-buffer-malloc.c b/src/hex-buffer-malloc.c
index c586b47..25dd2d1 100644
--- a/src/hex-buffer-malloc.c
+++ b/src/hex-buffer-malloc.c
@@ -33,12 +33,8 @@ update_payload_size_from_file (HexBufferMalloc *self)
 {
        self->payload_size = hex_buffer_util_get_file_size (self->file);
 
-       if (!self->payload_size)
-       {
-               g_warning ("%s: file \"%s\" is size 0 or invalid file...",
-                               __func__, g_file_get_path (self->file));
+       if (! self->payload_size)
                return FALSE;
-       }
        else
                return TRUE;
 }
diff --git a/src/hex-document.c b/src/hex-document.c
index d81b80c..83f39b9 100644
--- a/src/hex-document.c
+++ b/src/hex-document.c
@@ -352,7 +352,7 @@ hex_document_new_from_file (GFile *file)
 
        if (! hex_document_set_file (doc, file))
        {
-               g_object_unref (doc);
+               g_clear_object (&doc);
        }
 
        return doc;


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