From 30297a04d9ca7ab900723373a1746fd54e1d3d20 Mon Sep 17 00:00:00 2001 From: Dovi Cowan Date: Sun, 19 Jun 2022 18:42:53 +0100 Subject: [PATCH] try to implement download via Portals --- src/Widgets/Browser.vala | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/Widgets/Browser.vala b/src/Widgets/Browser.vala index 72d321c..01710c2 100644 --- a/src/Widgets/Browser.vala +++ b/src/Widgets/Browser.vala @@ -35,6 +35,8 @@ namespace Webpin.Widgets { Gtk.Box container; Granite.Widgets.Toast app_notification; GLib.Icon icon_for_notification; + // For saving downloads + Gtk.FileChooserNative file_chooser; public signal void external_request (WebKit.NavigationAction action); public signal void request_begin (); @@ -164,6 +166,54 @@ namespace Webpin.Widgets { } return base.key_press_event (event); }); + + web_view.web_context.download_started.connect ((event) => { + file_chooser = new Gtk.FileChooserNative (_("Choose download location"), + WebpinApp.instance.mainwindow, + Gtk.FileChooserAction.SAVE, + _("Save"), + _("Cancel")); + + var download_dir = GLib.Environment.get_user_special_dir (GLib.UserDirectory.DOWNLOAD); + + file_chooser.set_current_folder (download_dir); + + event.decide_destination.connect ((download_event) => { + file_chooser.set_current_name (download_event); + + if (file_chooser.run () == Gtk.ResponseType.ACCEPT) { + var filename = file_chooser.get_filename (); + + event.set_destination (filename); + warning (filename); + file_chooser.destroy (); + + app_notification.title = _ ("Download successful"); + app_notification.send_notification (); + + return false; + } else { + app_notification.title = _ ("Download failed or was cancelled"); + app_notification.send_notification (); + + event.cancel (); + + return false; + } + }); + + event.failed.connect((failed_event) => { + error ("Download failed!"); + }); + + event.created_destination.connect ((created_event) => { + message ("Destination created successfully"); + }); + + event.finished.connect ((finished_event) => { + message ("Download finished successfully"); + }); + }); } public void go_home () {