summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmaan Bhojwani <me@armaanb.net>2021-06-10 15:12:09 -0400
committerArmaan Bhojwani <me@armaanb.net>2021-06-10 17:41:59 -0400
commite46f96f17208fd8905a1a2c2f2d448eaf9acc84f (patch)
treeb2d76cd118e95ecbaa30d8ea33d3992ef511a173
parentd03513da57afb7151aafba198d0a95f10e097c26 (diff)
downloadchorizo-e46f96f17208fd8905a1a2c2f2d448eaf9acc84f.tar.gz
Use GTK file chooser for saving files
-rw-r--r--src/downloads.c114
1 files changed, 52 insertions, 62 deletions
diff --git a/src/downloads.c b/src/downloads.c
index 52a8eb0..db7aaf8 100644
--- a/src/downloads.c
+++ b/src/downloads.c
@@ -54,18 +54,10 @@ changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data) {
uri = webkit_download_get_destination(download);
filename = g_filename_from_uri(uri, NULL, NULL);
- if (filename == NULL) {
- /* This really should not happen because WebKit uses that URI
- * to write to a file... */
- fprintf(stderr, __NAME__ ": Could not construct file name from URI!\n");
- t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
- webkit_uri_response_get_uri(resp), p, size_mb);
- } else {
- base = g_path_get_basename(filename);
- t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
- g_free(filename);
- g_free(base);
- }
+ base = g_path_get_basename(filename);
+ t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
+ g_free(filename);
+ g_free(base);
gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
g_free(t);
}
@@ -89,62 +81,60 @@ download_start(WebKitWebView *web_view, WebKitDownload *download,
gboolean
download_handle(WebKitDownload *download, gchar *suggested_filename,
gpointer data) {
- gchar *sug_clean, *path, *path2 = NULL, *uri;
+ gchar *uri;
GtkToolItem *tb;
- int suffix = 1;
- size_t i;
- sug_clean = g_strdup(suggested_filename);
- for (i = 0; i < strlen(sug_clean); i++)
- if (sug_clean[i] == G_DIR_SEPARATOR)
- sug_clean[i] = '_';
+ GtkWidget *chooser = gtk_file_chooser_dialog_new(
+ "Choose download location", GTK_WINDOW(dm.win),
+ GTK_FILE_CHOOSER_ACTION_SAVE, "Save file", GTK_RESPONSE_ACCEPT,
+ "Cancel", GTK_RESPONSE_CANCEL, NULL);
+ gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser),
+ (char *)data);
+ gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser),
+ suggested_filename);
+ gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(chooser),
+ TRUE);
+ gint res = gtk_dialog_run(GTK_DIALOG(chooser));
+
+ switch (res) {
+ case GTK_RESPONSE_ACCEPT:
+ uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(chooser));
+ webkit_download_set_destination(
+ download, gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(chooser)));
+ break;
+ case GTK_RESPONSE_CANCEL:
+ return FALSE;
+ default:
+ return FALSE;
+ }
- path = g_build_filename((char *)data, sug_clean, NULL);
- path2 = g_strdup(path);
- while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) {
- g_free(path2);
+ gtk_widget_destroy(chooser);
- path2 = g_strdup_printf("%s.%d", path, suffix);
- suffix++;
- }
+ remove(uri + 7);
+ tb = gtk_tool_button_new(NULL, NULL);
+ gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "network-receive");
+ gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), uri);
+ gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
+ gtk_widget_show_all(dm.win);
- if (suffix == 1000) {
- fprintf(stderr, __NAME__ ": Suffix reached limit for download.\n");
- webkit_download_cancel(download);
- } else {
- uri = g_filename_to_uri(path2, NULL, NULL);
- webkit_download_set_destination(download, uri);
- g_free(uri);
-
- tb = gtk_tool_button_new(NULL, NULL);
- gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "network-receive");
- gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
- gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
- gtk_widget_show_all(dm.win);
-
- g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
- G_CALLBACK(changed_download_progress), tb);
-
- downloads++;
- g_signal_connect(G_OBJECT(download), "finished",
- G_CALLBACK(download_finished), tb);
-
- g_object_ref(download);
-
- struct DownloadItem *payload = malloc(sizeof(*payload));
- payload->tb = (GtkToolButton *)tb;
- payload->download = download;
- g_signal_connect(G_OBJECT(tb), "clicked", G_CALLBACK(download_click),
- payload);
- g_signal_connect(G_OBJECT(tb), "failed", G_CALLBACK(download_cancel),
- payload);
- g_signal_connect(G_OBJECT(tb), "destroy_event", G_CALLBACK(g_free),
- payload);
- }
+ g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
+ G_CALLBACK(changed_download_progress), tb);
+
+ downloads++;
+ g_signal_connect(G_OBJECT(download), "finished",
+ G_CALLBACK(download_finished), tb);
+
+ g_object_ref(download);
- g_free(sug_clean);
- g_free(path);
- g_free(path2);
+ struct DownloadItem *payload = malloc(sizeof(*payload));
+ payload->tb = (GtkToolButton *)tb;
+ payload->download = download;
+ g_signal_connect(G_OBJECT(tb), "clicked", G_CALLBACK(download_click),
+ payload);
+ g_signal_connect(G_OBJECT(tb), "failed", G_CALLBACK(download_cancel),
+ payload);
+ g_signal_connect(G_OBJECT(tb), "destroy_event", G_CALLBACK(g_free),
+ payload);
// Propagate -- to whom it may concern.
return FALSE;