aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/file_send_overlay.vala
diff options
context:
space:
mode:
authorfiaxh <git@lightrise.org>2020-04-22 15:44:12 +0200
committerfiaxh <git@lightrise.org>2020-04-22 15:44:12 +0200
commit51a23728694a3f1312cc9396fc093ca178457c3c (patch)
tree321771ae3d807d19387a8656805a648d75347994 /main/src/ui/file_send_overlay.vala
parent7c4260eed718961874fc0ea665263ea2ce59338b (diff)
downloaddino-51a23728694a3f1312cc9396fc093ca178457c3c.tar.gz
dino-51a23728694a3f1312cc9396fc093ca178457c3c.zip
Add file upload preview
fixes #756
Diffstat (limited to 'main/src/ui/file_send_overlay.vala')
-rw-r--r--main/src/ui/file_send_overlay.vala88
1 files changed, 88 insertions, 0 deletions
diff --git a/main/src/ui/file_send_overlay.vala b/main/src/ui/file_send_overlay.vala
new file mode 100644
index 00000000..c19a5aef
--- /dev/null
+++ b/main/src/ui/file_send_overlay.vala
@@ -0,0 +1,88 @@
+using Gee;
+using Gdk;
+using Gtk;
+
+using Dino.Entities;
+
+namespace Dino.Ui {
+
+[GtkTemplate (ui = "/im/dino/Dino/file_send_overlay.ui")]
+public class FileSendOverlay : Gtk.EventBox {
+
+ public signal void close();
+ public signal void send_file();
+
+ [GtkChild] public Button close_button;
+ [GtkChild] public Button send_button;
+ [GtkChild] public SizingBin file_widget_insert;
+ [GtkChild] public Label info_label;
+
+ private bool can_send = true;
+
+ public FileSendOverlay(File file, FileInfo file_info) {
+ close_button.clicked.connect(() => this.destroy());
+ send_button.clicked.connect(() => {
+ send_file();
+ this.destroy();
+ });
+
+ load_file_widget.begin(file, file_info);
+
+ this.realize.connect(() => {
+ if (can_send) {
+ send_button.grab_focus();
+ } else {
+ close_button.grab_focus();
+ }
+ });
+
+ this.key_release_event.connect((event) => {
+ if (event.keyval == Gdk.Key.Escape) {
+ this.destroy();
+ }
+ return false;
+ });
+ }
+
+ public void set_file_too_large() {
+ info_label.label= "The file exceeds the server's maximum upload size.";
+ Util.force_error_color(info_label);
+ send_button.sensitive = false;
+ can_send = false;
+ }
+
+ private async void load_file_widget(File file, FileInfo file_info) {
+ string file_name = file_info.get_display_name();
+ string mime_type = file_info.get_content_type();
+
+ bool is_image = false;
+
+ foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) {
+ foreach (string supported_mime_type in pixbuf_format.get_mime_types()) {
+ if (supported_mime_type == mime_type) {
+ is_image = true;
+ }
+ }
+ }
+
+ Widget? widget = null;
+ if (is_image) {
+ FileImageWidget image_widget = new FileImageWidget() { visible=true };
+ try {
+ yield image_widget.load_from_file(file, file_name);
+ widget = image_widget;
+ } catch (Error e) { }
+ }
+
+ if (widget == null) {
+ FileDefaultWidget default_widget = new FileDefaultWidget() { visible=true };
+ default_widget.name_label.label = file_name;
+ default_widget.update_file_info(mime_type, FileTransfer.State.COMPLETE, (long)file_info.get_size());
+ widget = default_widget;
+ }
+
+ file_widget_insert.add(widget);
+ }
+}
+
+}