[evolution] I#282 - Do not preview pipe-s in GtkFileChooser



commit fa1101bc6ab6a64b0c024deed109d9a921ac29da
Author: Milan Crha <mcrha redhat com>
Date:   Wed Jan 16 15:47:24 2019 +0100

    I#282 - Do not preview pipe-s in GtkFileChooser
    
    Closes https://gitlab.gnome.org/GNOME/evolution/issues/282

 .../gui/contact-editor/e-contact-editor.c          | 11 +++++---
 src/e-util/e-attachment-store.c                    |  4 ++-
 src/e-util/e-image-chooser-dialog.c                | 12 ++++++++-
 src/e-util/e-misc-utils.c                          | 29 ++++++++++++++++++++++
 src/e-util/e-misc-utils.h                          |  1 +
 src/plugins/face/face.c                            |  3 ++-
 6 files changed, 54 insertions(+), 6 deletions(-)
---
diff --git a/src/addressbook/gui/contact-editor/e-contact-editor.c 
b/src/addressbook/gui/contact-editor/e-contact-editor.c
index f378bb0ad1..cff02830fa 100644
--- a/src/addressbook/gui/contact-editor/e-contact-editor.c
+++ b/src/addressbook/gui/contact-editor/e-contact-editor.c
@@ -4286,16 +4286,21 @@ update_preview_cb (GtkFileChooser *file_chooser,
                    gpointer data)
 {
        GtkWidget *preview;
-       gchar *filename = NULL;
+       gchar *filename;
        GdkPixbuf *pixbuf;
 
-       gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
        preview = GTK_WIDGET (data);
        filename = gtk_file_chooser_get_preview_filename (file_chooser);
-       if (filename == NULL)
+       if (!e_util_can_preview_filename (filename)) {
+               gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
+               g_free (filename);
                return;
+       }
+
+       gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
 
        pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
+
        if (!pixbuf) {
                gchar *alternate_file;
                alternate_file = e_icon_factory_get_icon_filename (
diff --git a/src/e-util/e-attachment-store.c b/src/e-util/e-attachment-store.c
index 004ed9c75d..0ba4ae2231 100644
--- a/src/e-util/e-attachment-store.c
+++ b/src/e-util/e-attachment-store.c
@@ -606,8 +606,10 @@ update_preview_cb (GtkFileChooser *file_chooser,
        gtk_image_clear (GTK_IMAGE (data));
        preview = GTK_WIDGET (data);
        filename = gtk_file_chooser_get_preview_filename (file_chooser);
-       if (filename == NULL)
+       if (!e_util_can_preview_filename (filename)) {
+               g_free (filename);
                return;
+       }
 
        pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
        g_free (filename);
diff --git a/src/e-util/e-image-chooser-dialog.c b/src/e-util/e-image-chooser-dialog.c
index 56d8f7de88..f501a473bd 100644
--- a/src/e-util/e-image-chooser-dialog.c
+++ b/src/e-util/e-image-chooser-dialog.c
@@ -22,6 +22,7 @@
 
 #include <glib/gi18n-lib.h>
 
+#include "e-misc-utils.h"
 #include "e-image-chooser-dialog.h"
 
 #define E_IMAGE_CHOOSER_DIALOG_GET_PRIVATE(obj) \
@@ -104,6 +105,7 @@ image_chooser_dialog_update_preview (GtkFileChooser *file_chooser)
        GtkWidget *preview_widget;
        GFile *preview_file;
        Context *context;
+       gchar *filename;
 
        priv = E_IMAGE_CHOOSER_DIALOG_GET_PRIVATE (file_chooser);
        preview_file = gtk_file_chooser_get_preview_file (file_chooser);
@@ -118,9 +120,17 @@ image_chooser_dialog_update_preview (GtkFileChooser *file_chooser)
        gtk_image_clear (GTK_IMAGE (preview_widget));
        gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
 
-       if (preview_file == NULL)
+       if (!preview_file)
                return;
 
+       filename = gtk_file_chooser_get_preview_filename (file_chooser);
+       if (!e_util_can_preview_filename (filename)) {
+               g_free (filename);
+               g_object_unref (preview_file);
+               return;
+       }
+       g_free (filename);
+
        priv->cancellable = g_cancellable_new ();
 
        context = g_slice_new0 (Context);
diff --git a/src/e-util/e-misc-utils.c b/src/e-util/e-misc-utils.c
index b6a280b3d7..55ab82d795 100644
--- a/src/e-util/e-misc-utils.c
+++ b/src/e-util/e-misc-utils.c
@@ -4624,3 +4624,32 @@ e_misc_util_free_global_memory (void)
        e_spell_checker_free_global_memory ();
        e_simple_async_result_free_global_memory ();
 }
+
+/**
+ * e_util_can_preview_filename:
+ * @filename: (nullable): a file name to test
+ *
+ * Returns: Whether the @filename can be used to create a preview
+ *   in GtkFileChooser and such widgets. For example directories,
+ *   pipes and sockets cannot by used.
+ *
+ * Since: 3.32
+ **/
+gboolean
+e_util_can_preview_filename (const gchar *filename)
+{
+       GStatBuf st;
+
+       if (!filename || !*filename)
+               return FALSE;
+
+       return g_stat (filename, &st) == 0
+               && !S_ISDIR (st.st_mode)
+               #ifdef S_ISFIFO
+               && !S_ISFIFO (st.st_mode)
+               #endif
+               #ifdef S_ISSOCK
+               && !S_ISSOCK (st.st_mode)
+               #endif
+               ;
+}
diff --git a/src/e-util/e-misc-utils.h b/src/e-util/e-misc-utils.h
index 472f79d795..086505c68a 100644
--- a/src/e-util/e-misc-utils.h
+++ b/src/e-util/e-misc-utils.h
@@ -353,6 +353,7 @@ gboolean    e_util_get_language_info        (const gchar *language_tag,
                                                 gchar **out_language_name,
                                                 gchar **out_country_name);
 void           e_misc_util_free_global_memory  (void);
+gboolean       e_util_can_preview_filename     (const gchar *filename);
 
 G_END_DECLS
 
diff --git a/src/plugins/face/face.c b/src/plugins/face/face.c
index 2290f23f84..8d6dfe4a6f 100644
--- a/src/plugins/face/face.c
+++ b/src/plugins/face/face.c
@@ -163,7 +163,8 @@ prepare_image (const gchar *image_filename,
        g_return_val_if_fail (file_contents != NULL, FALSE);
        g_return_val_if_fail (length != NULL, FALSE);
 
-       if (g_file_get_contents (image_filename, file_contents, length, NULL)) {
+       if (e_util_can_preview_filename (image_filename) &&
+           g_file_get_contents (image_filename, file_contents, length, NULL)) {
                GError *error = NULL;
                GdkPixbuf *pixbuf;
                GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();


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