aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_content_view/file_preview_widget.vala
blob: e7cee93aa94d65195731fd10d096515e91dafe23 (plain) (blame)
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
82
83
84
85
86
87
88
89
90
using Gee;
using Gdk;
using Gtk;
using Xmpp;

using Dino.Entities;

namespace Dino.Ui {

    public class FilePreviewWidget : Box {

        private ScalingImage image;
        FileDefaultWidget file_default_widget;
        FileDefaultWidgetController file_default_widget_controller;

        public FilePreviewWidget() {
            this.halign = Align.START;

            this.add_css_class("file-preview-widget");
        }

        public async void load_from_thumbnail(FileTransfer file_transfer, StreamInteractor stream_interactor, int MAX_WIDTH=600, int MAX_HEIGHT=300) throws GLib.Error {
            Thread<ScalingImage?> thread = new Thread<ScalingImage?> (null, () => {
                ScalingImage image = new ScalingImage() { halign=Align.START, visible = true, max_width = MAX_WIDTH, max_height = MAX_HEIGHT };
                Gdk.Pixbuf? pixbuf = null;
                foreach (Xep.JingleContentThumbnails.Thumbnail thumbnail in file_transfer.thumbnails) {
                    pixbuf = ImageFileMetadataProvider.parse_thumbnail(thumbnail);
                    if (pixbuf != null) {
                        break;
                    }
                }
                if (pixbuf == null) {
                    warning("Can't load thumbnails of file %s", file_transfer.file_name);
                    Idle.add(load_from_thumbnail.callback);
                    throw new Error(-1, 0, "Error loading preview image");
                }
                // TODO: should this be executed? If yes, before or after scaling
                pixbuf = pixbuf.apply_embedded_orientation();

                if (file_transfer.width > 0 && file_transfer.height > 0) {
                    pixbuf = pixbuf.scale_simple(file_transfer.width, file_transfer.height, InterpType.BILINEAR);
                } else {
                    warning("Preview: Not scaling image, width: %d, height: %d\n", file_transfer.width, file_transfer.height);
                }
                if (pixbuf == null) {
                    warning("Can't scale thumbnail %s", file_transfer.file_name);
                    throw new Error(-1, 0, "Error scaling preview image");
                }

                image.load(pixbuf);
                Idle.add(load_from_thumbnail.callback);
                return image;
            });
            yield;
            this.image = thread.join();

            file_default_widget = new FileDefaultWidget() { valign=Align.END, vexpand=false, visible=false };
            file_default_widget.image_stack.visible = false;
            file_default_widget_controller = new FileDefaultWidgetController(file_default_widget);
            file_default_widget_controller.set_file_transfer(file_transfer, stream_interactor);

            Overlay overlay = new Overlay();
            overlay.set_child(image);
            overlay.add_overlay(file_default_widget);
            overlay.set_measure_overlay(image, true);
            overlay.set_clip_overlay(file_default_widget, true);

            EventControllerMotion this_motion_events = new EventControllerMotion();
            this.add_controller(this_motion_events);
            this_motion_events.enter.connect(() => {
                file_default_widget.visible = true;
            });
            this_motion_events.leave.connect(() => {
                if (file_default_widget.file_menu.popover != null && file_default_widget.file_menu.popover.visible) return;

                file_default_widget.visible = false;
            });
            GestureClick gesture_click_controller = new GestureClick();
            gesture_click_controller.set_button(1); // listen for left clicks
            this.add_controller(gesture_click_controller);
            gesture_click_controller.pressed.connect((n_press, x, y) => {
                // Check whether the click was inside the file menu. Otherwise, open the file.
                this.file_default_widget.clicked();
            });

            this.append(overlay);
        }
    }

}