summarylogtreecommitdiffstats
path: root/add-preview-widget.patch
blob: 7fb730286dc964085ab61deda777a8a6654f7335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
--- src/os-patches/lib/widgets/ephy-file-chooser.c~	2018-09-30 04:45:43.003990684 +0900
+++ src/os-patches/lib/widgets/ephy-file-chooser.c	2018-10-07 00:40:58.833626868 +0900
@@ -32,6 +32,12 @@
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <glib/gstdio.h>
+
+#define MAX_PREVIEW_SIZE 180
+#define MAX_PREVIEW_SOURCE_SIZE 4096
 
 static GtkFileFilter *
 ephy_file_chooser_add_pattern_filter (GtkFileChooser *dialog,
@@ -89,6 +92,53 @@
   return filth;
 }
 
+static void
+update_preview_cb (GtkFileChooser *file_chooser,
+                        gpointer data)
+{
+  GtkImage *preview = GTK_IMAGE (data);
+  g_autofree char *filename = gtk_file_chooser_get_preview_filename(file_chooser);
+
+  struct g_stat st_buf;
+  if (!filename || g_stat(filename, &st_buf) || (!S_ISREG(st_buf.st_mode))) {
+    gtk_file_chooser_set_preview_widget_active(file_chooser, FALSE);
+    return; // stat failed or file is not regular
+  }
+
+  gint preview_width = 0;
+  gint preview_height = 0;
+  GdkPixbufFormat *preview_format = gdk_pixbuf_get_file_info(filename,
+                                                             &preview_width,
+                                                             &preview_height);
+  if (!preview_format ||
+      preview_width <= 0 || preview_height <= 0 ||
+      preview_width > MAX_PREVIEW_SOURCE_SIZE ||
+      preview_height > MAX_PREVIEW_SOURCE_SIZE) {
+    gtk_file_chooser_set_preview_widget_active(file_chooser, FALSE);
+    return; // unpreviewable, 0px, or unsafely large
+  }
+
+  g_autoptr(GdkPixbuf) pixbuf = NULL;
+  if (preview_width > MAX_PREVIEW_SIZE || preview_height > MAX_PREVIEW_SIZE) {
+    pixbuf = gdk_pixbuf_new_from_file_at_size(filename,
+                                                      MAX_PREVIEW_SIZE,
+                                                      MAX_PREVIEW_SIZE,
+                                                      NULL);
+  }
+  else {
+    pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
+  }
+
+  pixbuf = gdk_pixbuf_apply_embedded_orientation(pixbuf);
+
+  gtk_widget_set_size_request(GTK_WIDGET (preview),
+                                     gdk_pixbuf_get_width(pixbuf) + 6,
+                                     gdk_pixbuf_get_height(pixbuf) + 6);
+
+  gtk_image_set_from_pixbuf(preview, pixbuf);
+  gtk_file_chooser_set_preview_widget_active(file_chooser, pixbuf != NULL);
+}
+
 GtkFileChooser *
 ephy_create_file_chooser (const char           *title,
                           GtkWidget            *parent,
@@ -121,6 +195,11 @@
     gtk_file_chooser_native_set_accept_label (GTK_FILE_CHOOSER_NATIVE (dialog), _("_Save"));
   }
 
+  GtkWidget *preview = gtk_image_new();
+  gtk_file_chooser_set_preview_widget(dialog, preview);
+  gtk_file_chooser_set_use_preview_label(dialog, FALSE);
+  g_signal_connect(dialog, "update-preview", G_CALLBACK(update_preview_cb), preview);
+
   if (default_filter != EPHY_FILE_FILTER_NONE) {
     filter[EPHY_FILE_FILTER_ALL_SUPPORTED] =
       ephy_file_chooser_add_mime_filter