From 3d980fd6f5f5fa5a9a4e7d31ac883b8e65570ce8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <philipp@hoerist.com>
Date: Wed, 21 Mar 2018 20:24:20 +0100
Subject: [PATCH] Show image preview in filechooser dialog

---
 gajim/dialogs.py              | 23 ++++++++++++++++++++++-
 gajim/filetransfers_window.py |  1 +
 gajim/gui_interface.py        |  1 +
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/gajim/dialogs.py b/gajim/dialogs.py
index 3a3af2b631..1ac2bf7974 100644
--- a/gajim/dialogs.py
+++ b/gajim/dialogs.py
@@ -1441,7 +1441,7 @@ class FileChooserDialog(Gtk.FileChooserDialog):
     """
     def __init__(self, title_text, action, buttons, default_response,
     select_multiple=False, current_folder=None, on_response_ok=None,
-    on_response_cancel=None, transient_for=None):
+    on_response_cancel=None, preview=False, transient_for=None):
 
         Gtk.FileChooserDialog.__init__(self, title=title_text,
             parent=transient_for, action=action)
@@ -1459,6 +1459,12 @@ class FileChooserDialog(Gtk.FileChooserDialog):
         # in gtk+-2.10 clicked signal on some of the buttons in a dialog
         # is emitted twice, so we cannot rely on 'clicked' signal
         self.connect('response', self.on_dialog_response)
+
+        if preview:
+            self.set_use_preview_label(False)
+            self.set_preview_widget(Gtk.Image())
+            self.connect('selection-changed', self.update_preview)
+
         self.show_all()
 
     def on_dialog_response(self, dialog, response):
@@ -1479,6 +1485,21 @@ class FileChooserDialog(Gtk.FileChooserDialog):
             else:
                 self.just_destroy(dialog)
 
+    def update_preview(self, widget):
+        path_to_file = widget.get_preview_filename()
+        preview = widget.get_preview_widget()
+        if path_to_file is None or os.path.isdir(path_to_file):
+            # nothing to preview or directory
+            # make sure you clean image do show nothing
+            preview.clear()
+            return
+        try:
+            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path_to_file, 200, 200)
+        except GObject.GError:
+            preview.clear()
+            return
+        widget.get_preview_widget().set_from_pixbuf(pixbuf)
+
     def just_destroy(self, widget):
         self.destroy()
 
diff --git a/gajim/filetransfers_window.py b/gajim/filetransfers_window.py
index 2935eaac24..ef1a0b75a0 100644
--- a/gajim/filetransfers_window.py
+++ b/gajim/filetransfers_window.py
@@ -349,6 +349,7 @@ class FileTransfersWindow:
                 app.config.get('last_send_dir'),
                 on_response_ok=on_ok,
                 on_response_cancel=lambda e:dialog.destroy(),
+                preview=True,
                 transient_for=app.interface.roster.window
                 )
 
diff --git a/gajim/gui_interface.py b/gajim/gui_interface.py
index 70d7acfd22..686963e63f 100644
--- a/gajim/gui_interface.py
+++ b/gajim/gui_interface.py
@@ -1156,6 +1156,7 @@ class Interface:
                      Gtk.STOCK_OPEN, Gtk.ResponseType.OK),
             select_multiple=True,
             default_response=Gtk.ResponseType.OK,
+            preview=True,
             transient_for=chat_control.parent_win.window)
 
     @staticmethod
-- 
GitLab