diff options
author | Cem Keylan <cem@ckyln.com> | 2021-09-29 00:46:29 +0300 |
---|---|---|
committer | Cem Keylan <cem@ckyln.com> | 2021-09-29 00:46:29 +0300 |
commit | ed38d8d856a3295364a96a90b9358ab59769c5c5 (patch) | |
tree | 20b05797988076b31a1ba174aea0f20eb619b65c | |
parent | a524bc0c30d14fb7befefa8d203ce8049307d599 (diff) | |
download | lariza-ed38d8d856a3295364a96a90b9358ab59769c5c5.tar.gz |
download_handle(): Use GTK file chooser
-rw-r--r-- | browser.c | 40 |
1 files changed, 24 insertions, 16 deletions
@@ -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; |