logo

badwolf

Minimalist and privacy-oriented WebKitGTK+ browser
commit: a8763f013f6b2395a2ee8dd52acf93bbabbfcffd
parent: e3110230168447885451cc2673b1aa91a59aa7ae
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 17 May 2019 13:42:48 +0200

Spawn a GtkFileDialog before downloading to disk

Diffstat:

Mbadwolf.c60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+), 0 deletions(-)

diff --git a/badwolf.c b/badwolf.c @@ -74,6 +74,12 @@ static gboolean WebViewCb_decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision, WebKitPolicyDecisionType decision_type, gpointer user_data); +static void web_contentCb_download_started(WebKitWebContext *context, + WebKitDownload *download, + gpointer user_data); +static gboolean downloadCb_decide_destination(WebKitDownload *download, + gchar *suggested_filename, + gpointer user_data); static gboolean locationCb_activate(GtkEntry *location, gpointer user_data); static gboolean javascriptCb_toggled(GtkButton *javascript, gpointer user_data); static gboolean SearchEntryCb_next__match(GtkSearchEntry *search, gpointer user_data); @@ -497,6 +503,54 @@ WebViewCb_decide_policy(WebKitWebView *web_view, return TRUE; } +static void +web_contentCb_download_started(WebKitWebContext *context, + WebKitDownload *download, + gpointer user_data) +{ + (void)context; + + g_signal_connect(G_OBJECT(download), + "decide-destination", + G_CALLBACK(downloadCb_decide_destination), + user_data); +} + +static gboolean +downloadCb_decide_destination(WebKitDownload *download, + gchar *suggested_filename, + gpointer user_data) +{ + struct Client *browser = (struct Client *)user_data; + gint chooser_response; + GtkWindow *parent_window = GTK_WINDOW(browser->window->main_window); + + GtkWidget *file_dialog = gtk_file_chooser_dialog_new(NULL, + parent_window, + GTK_FILE_CHOOSER_ACTION_SAVE, + _("_Cancel"), + GTK_RESPONSE_CANCEL, + _("_Open"), + GTK_RESPONSE_ACCEPT, + NULL); + GtkFileChooser *file_chooser = GTK_FILE_CHOOSER(file_dialog); + + gtk_file_chooser_set_current_name(file_chooser, suggested_filename); + gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, TRUE); + webkit_download_set_allow_overwrite(download, TRUE); + + chooser_response = gtk_dialog_run(GTK_DIALOG(file_dialog)); + + if(chooser_response == GTK_RESPONSE_ACCEPT) + webkit_download_set_destination(download, gtk_file_chooser_get_uri(file_chooser)); + else + webkit_download_cancel(download); + + gtk_widget_destroy(file_dialog); + + return TRUE; /* Stop other handlers */ +} + static gboolean locationCb_activate(GtkEntry *location, gpointer user_data) { @@ -681,6 +735,12 @@ new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web browser->webView, "permission-request", G_CALLBACK(WebViewCb_permission_request), NULL); g_signal_connect(browser->webView, "decide-policy", G_CALLBACK(WebViewCb_decide_policy), NULL); + /* signals for WebView's WebContext */ + g_signal_connect(G_OBJECT(web_context), + "download-started", + G_CALLBACK(web_contentCb_download_started), + browser); + /* signals for search widget */ g_signal_connect(browser->search, "next-match", G_CALLBACK(SearchEntryCb_next__match), browser); g_signal_connect(