anotherim-desktop/main/src/ui/file_send_overlay.vala

110 lines
3.1 KiB
Vala
Raw Normal View History

2020-04-22 13:44:12 +00:00
using Gee;
using Gdk;
using Gtk;
using Dino.Entities;
namespace Dino.Ui {
2022-02-14 13:55:59 +00:00
public class FileSendOverlay {
2020-04-22 13:44:12 +00:00
public signal void close();
public signal void send_file();
2022-02-14 13:55:59 +00:00
public Box main_box;
public Button close_button;
public Button send_button;
public SizingBin file_widget_insert;
public Label info_label;
2020-04-22 13:44:12 +00:00
private bool can_send = true;
public FileSendOverlay(File file, FileInfo file_info) {
2022-02-14 13:55:59 +00:00
Builder builder = new Builder.from_resource("/im/dino/Dino/file_send_overlay.ui");
main_box = (Box) builder.get_object("main_box");
close_button = (Button) builder.get_object("close_button");
send_button = (Button) builder.get_object("send_button");
file_widget_insert = (SizingBin) builder.get_object("file_widget_insert");
info_label = (Label) builder.get_object("info_label");
close_button.clicked.connect(() => {
2022-05-14 12:45:59 +00:00
do_close();
});
2020-04-22 13:44:12 +00:00
send_button.clicked.connect(() => {
send_file();
2022-05-14 12:45:59 +00:00
do_close();
2020-04-22 13:44:12 +00:00
});
load_file_widget.begin(file, file_info);
2022-02-14 13:55:59 +00:00
main_box.realize.connect(() => {
2020-04-22 13:44:12 +00:00
if (can_send) {
send_button.grab_focus();
} else {
close_button.grab_focus();
}
});
2022-05-14 12:45:59 +00:00
var key_events = new EventControllerKey();
key_events.key_pressed.connect((keyval) => {
if (keyval == Gdk.Key.Escape) {
do_close();
}
return false;
});
this.main_box.add_controller(key_events);
2020-04-22 13:44:12 +00:00
}
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) {
2022-05-14 12:45:59 +00:00
FileImageWidget image_widget = new FileImageWidget();
2020-04-22 13:44:12 +00:00
try {
yield image_widget.load_from_file(file, file_name);
widget = image_widget;
} catch (Error e) { }
}
if (widget == null) {
2022-05-14 12:45:59 +00:00
FileDefaultWidget default_widget = new FileDefaultWidget();
2020-04-22 13:44:12 +00:00
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;
}
2022-02-14 13:55:59 +00:00
widget.set_parent(file_widget_insert);
2020-04-22 13:44:12 +00:00
}
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;
}
2022-02-14 13:55:59 +00:00
2022-05-14 12:45:59 +00:00
private void do_close() {
this.close();
main_box.unparent();
main_box.destroy();
}
2022-02-14 13:55:59 +00:00
public Widget get_widget() {
return main_box;
}
2020-04-22 13:44:12 +00:00
}
}