Blob Blame History Raw
From e327a522719e5bc2237a3e8c4064e40b41fb9d15 Mon Sep 17 00:00:00 2001
From: Jonathon Jongsma <jjongsma@redhat.com>
Date: Mon, 17 Jul 2017 16:15:12 -0500
Subject: [PATCH] Report errors when saving screenshot

Currently, the user gets no feedback if the screenshot fails (e.g. if
they don't have permission to write in the chosen directory, etc). This
patch adds a simple dialog showing the error message when a screenshot
fails.
---
 src/virt-viewer-window.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index ef40ec7..41794a6 100644
--- a/src/virt-viewer-window.c
+++ b/src/virt-viewer-window.c
@@ -936,34 +936,37 @@ static GdkPixbufFormat *get_image_format(const char *filename)
     return g_hash_table_lookup(image_formats_once.retval, ext);
 }
 
-static void
+static gboolean
 virt_viewer_window_save_screenshot(VirtViewerWindow *self,
-                                   const char *file)
+                                   const char *file,
+                                   GError **error)
 {
     VirtViewerWindowPrivate *priv = self->priv;
     GdkPixbuf *pix = virt_viewer_display_get_pixbuf(VIRT_VIEWER_DISPLAY(priv->display));
     GdkPixbufFormat *format = get_image_format(file);
+    gboolean result;
 
     if (format == NULL) {
         g_debug("unknown file extension, falling back to png");
         if (!g_str_has_suffix(file, ".png")) {
             char *png_filename;
             png_filename = g_strconcat(file, ".png", NULL);
-            gdk_pixbuf_save(pix, png_filename, "png", NULL,
-                            "tEXt::Generator App", PACKAGE, NULL);
+            result = gdk_pixbuf_save(pix, png_filename, "png", error,
+                                     "tEXt::Generator App", PACKAGE, NULL);
             g_free(png_filename);
         } else {
-            gdk_pixbuf_save(pix, file, "png", NULL,
-                            "tEXt::Generator App", PACKAGE, NULL);
+            result = gdk_pixbuf_save(pix, file, "png", error,
+                                     "tEXt::Generator App", PACKAGE, NULL);
         }
     } else {
         char *type = gdk_pixbuf_format_get_name(format);
         g_debug("saving to %s", type);
-        gdk_pixbuf_save(pix, file, type, NULL, NULL);
+        result = gdk_pixbuf_save(pix, file, type, error, NULL);
         g_free(type);
     }
 
     g_object_unref(pix);
+    return result;
 }
 
 G_MODULE_EXPORT void
@@ -992,9 +995,14 @@ virt_viewer_window_menu_file_screenshot(GtkWidget *menu G_GNUC_UNUSED,
 
     if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
         char *filename;
+        GError *error = NULL;
 
         filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog));
-        virt_viewer_window_save_screenshot(self, filename);
+        if (!virt_viewer_window_save_screenshot(self, filename, &error)) {
+            virt_viewer_app_simple_message_dialog(self->priv->app,
+                                                  error->message);
+            g_error_free(error);
+        }
         g_free(filename);
     }
 
-- 
2.13.6