aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_content_view/file_widget.vala
blob: d6d2415ad5e113dd9e0843f1bbc99439c1ee837f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using Gee;
using Gdk;
using Gtk;
using Pango;
using Xmpp;

using Dino.Entities;

namespace Dino.Ui {

public class FileMetaItem : ConversationSummary.ContentMetaItem {

    private StreamInteractor stream_interactor;
    private FileItem file_item;
    private FileTransfer file_transfer;

    public FileMetaItem(ContentItem content_item, StreamInteractor stream_interactor) {
        base(content_item);
        this.stream_interactor = stream_interactor;
        this.file_item = content_item as FileItem;
        this.file_transfer = file_item.file_transfer;
    }

    public override Object? get_widget(Plugins.ConversationItemWidgetInterface outer, Plugins.WidgetType type) {
        FileWidget widget = new FileWidget(file_transfer);
        FileWidgetController widget_controller = new FileWidgetController(widget, file_transfer, stream_interactor);
        return widget;
    }

    public override Gee.List<Plugins.MessageAction>? get_item_actions(Plugins.WidgetType type) {
        if ((file_transfer.provider != FileManager.HTTP_PROVIDER_ID && file_transfer.provider != FileManager.SFS_PROVIDER_ID) || file_transfer.info == null) return null;

        Gee.List<Plugins.MessageAction> actions = new ArrayList<Plugins.MessageAction>();

        actions.add(get_reply_action(content_item, file_item.conversation, stream_interactor));
        actions.add(get_reaction_action(content_item, file_item.conversation, stream_interactor));

        var delete_action = get_delete_action(content_item, file_item.conversation, stream_interactor);
        if (delete_action != null) actions.add(delete_action);

        return actions;
    }
}

public class FileWidget : SizeRequestBin {

    enum State {
        IMAGE,
        VIDEO,
        DEFAULT
    }

    private FileTransfer file_transfer;
    public FileTransfer.State file_transfer_state { get; set; }
    public FileContentType file_transfer_content_type { get; set; }
    private State? state = null;

    private FileDefaultWidgetController default_widget_controller;
    private Widget? content = null;

    construct {
        margin_top = 4;
        size_request_mode = SizeRequestMode.HEIGHT_FOR_WIDTH;
        check_widget_leak(this);
    }

    public FileWidget(FileTransfer file_transfer) {
        this.file_transfer = file_transfer;

        update_widget.begin();

        file_transfer.bind_property("state", this, "file-transfer-state");
        file_transfer.bind_property("content-type", this, "file-transfer-content-type");

        this.notify["file-transfer-state"].connect(update_widget);
        this.notify["file-transfer-content-type"].connect(update_widget);
    }

    private async void update_widget() {
        bool show_image = FileImageWidget.can_display(file_transfer);
        bool show_video = FileVideoWidget.can_display(file_transfer);

        if (show_image && state != State.IMAGE) {
            var content_bak = content;

            FileImageWidget file_image_widget = null;
            try {
                file_image_widget = new FileImageWidget.from_file_transfer(file_transfer);
                file_image_widget.add_css_class("file-image-widget");

                // If the widget changed in the meanwhile, stop
                if (content != content_bak) return;

                if (content != null) content.unparent();
                content = file_image_widget;
                state = State.IMAGE;
                content.insert_after(this, null);
                return;
            } catch (Error e) { }
        }

        if (show_image && state == State.IMAGE) return;

        if (show_video && state != State.VIDEO) {
            if (content != null) content.unparent();
            content = new FileVideoWidget(file_transfer);
            state = State.VIDEO;
            content.insert_after(this, null);
            return;
        }

        if (show_video && state == State.VIDEO) return;

        if (state != State.DEFAULT) {
            if (content != null) content.unparent();
            FileDefaultWidget default_file_widget = new FileDefaultWidget();
            default_widget_controller = new FileDefaultWidgetController(default_file_widget);
            default_widget_controller.set_file_transfer(file_transfer);
            content = default_file_widget;
            this.state = State.DEFAULT;
            content.insert_after(this, null);
        }
    }

    public override void dispose() {
        if (default_widget_controller != null) default_widget_controller.dispose();
        default_widget_controller = null;
        if (content != null) {
            content.unparent();
            content.dispose();
            content = null;
        }
        base.dispose();
    }
}

public class FileWidgetController : Object {

    private weak Widget widget;
    private FileTransfer file_transfer;
    private StreamInteractor? stream_interactor;

    public FileWidgetController(FileWidget widget, FileTransfer file_transfer, StreamInteractor? stream_interactor = null) {
        this.widget = widget;
        this.ref();
        this.widget.weak_ref(() => {
            this.widget = null;
            this.unref();
        });
        this.file_transfer = file_transfer;
        this.stream_interactor = stream_interactor;
    }
}

public class FileDefaultWidgetController : Object {

    private FileDefaultWidget widget;
    private FileTransfer? file_transfer;
    public string file_transfer_state { get; set; }
    public FileContentType file_transfer_content_type { get; set; }
    public int64 file_transfer_transferred_bytes { get; set; }

    private FileTransfer.State state;

    public FileDefaultWidgetController(FileDefaultWidget widget) {
        this.widget = widget;

        widget.clicked.connect(on_clicked);

        this.notify["file-transfer-state"].connect(update_file_info);
        this.notify["file-transfer-content-type"].connect(update_file_info);
        this.notify["file-transfer-transferred-bytes"].connect(update_file_info);
    }

    public void set_file_transfer(FileTransfer file_transfer) {
        this.file_transfer = file_transfer;

        widget.init_updating_file_info();
        widget.name_label.label = file_transfer.file_name;

        file_transfer.bind_property("state", this, "file-transfer-state");
        file_transfer.bind_property("content-type", this, "file-transfer-content-type");
        file_transfer.bind_property("transferred-bytes", this, "file-transfer-transferred-bytes");

        update_file_info();
    }

    private void update_file_info() {
        state = file_transfer.state;
        widget.update_file_info(file_transfer);
    }

    private void on_clicked() {
        switch (state) {
            case FileTransfer.State.COMPLETE:
                Dino.Application.get_default().activate_action("file_open_externally", new GLib.Variant.int32(file_transfer.id));
                break;
            case FileTransfer.State.NOT_STARTED:
                Dino.Application.get_default().activate_action("file_start_download", new GLib.Variant.int32(file_transfer.id));
                break;
            default:
                // Clicking doesn't do anything in FAILED and IN_PROGRESS states
                break;
        }
    }
}

}