From ed38d8d856a3295364a96a90b9358ab59769c5c5 Mon Sep 17 00:00:00 2001 From: Cem Keylan Date: Wed, 29 Sep 2021 00:46:29 +0300 Subject: download_handle(): Use GTK file chooser --- browser.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/browser.c b/browser.c index 307690e..4fbf778 100644 --- a/browser.c +++ b/browser.c @@ -537,34 +537,43 @@ download_handle_start(WebKitWebView *web_view, WebKitDownload *download, gboolean download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data) { - gchar *sug_clean, *path, *path2 = NULL, *uri; + gchar *sug_clean, *path = NULL, *uri; GtkToolItem *tb; - int suffix = 1; size_t i; + GtkWidget *dialog; + GtkFileChooser *chooser; + gint res; + + dialog = gtk_file_chooser_dialog_new("Save File", + NULL, + GTK_FILE_CHOOSER_ACTION_SAVE, + "Cancel", + GTK_RESPONSE_CANCEL, + "Save", + GTK_RESPONSE_ACCEPT, + NULL); sug_clean = g_strdup(suggested_filename); for (i = 0; i < strlen(sug_clean); i++) if (sug_clean[i] == G_DIR_SEPARATOR) sug_clean[i] = '_'; - path = g_build_filename(download_dir, sug_clean, NULL); - path2 = g_strdup(path); - while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) + chooser = GTK_FILE_CHOOSER(dialog); + gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE); + gtk_file_chooser_set_current_name(chooser, sug_clean); + gtk_file_chooser_set_current_folder(chooser, download_dir); + gtk_widget_show_all(dialog); + res = gtk_dialog_run(GTK_DIALOG(dialog)); + if (res != GTK_RESPONSE_ACCEPT) { - g_free(path2); - - path2 = g_strdup_printf("%s.%d", path, suffix); - suffix++; - } - - if (suffix == 1000) - { - fprintf(stderr, __NAME__": Suffix reached limit for download.\n"); webkit_download_cancel(download); + gtk_widget_destroy(dialog); } else { - uri = g_filename_to_uri(path2, NULL, NULL); + path = gtk_file_chooser_get_filename(chooser); + gtk_widget_destroy(dialog); + uri = g_filename_to_uri(path, NULL, NULL); webkit_download_set_destination(download, uri); g_free(uri); @@ -588,7 +597,6 @@ download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer da g_free(sug_clean); g_free(path); - g_free(path2); /* Propagate -- to whom it may concern. */ return FALSE; -- cgit v1.2.3