diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/download.c | 123 | ||||
-rw-r--r-- | src/download.h | 38 | ||||
-rw-r--r-- | src/local.mk | 1 |
3 files changed, 162 insertions, 0 deletions
diff --git a/src/download.c b/src/download.c new file mode 100644 index 0000000..ad8f9c3 --- /dev/null +++ b/src/download.c @@ -0,0 +1,123 @@ +/* + * Download + * + * Copyright (C) 2017 Patrick McDermott + * + * This file is part of Marquee. + * + * Marquee is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Marquee is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Marquee. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "download.h" + +#include <glib.h> +#include <webkit2/webkit2.h> + +#include "config.h" + +struct _MqDownload { + MqConfig *config; + WebKitDownload *wk_download; +}; + +static void +chooser_response_cb(GtkWidget *dialog, gint response_id, MqDownload *download) +{ + gchar *dir; + gchar *uri; + + if (response_id == GTK_RESPONSE_ACCEPT) { + dir = gtk_file_chooser_get_current_folder( + GTK_FILE_CHOOSER(dialog)); + if (dir) { + mq_config_set_string(download->config, + "directories.downloads", dir); + g_free(dir); + mq_config_save(download->config); + } + + uri = gtk_file_chooser_get_uri( + GTK_FILE_CHOOSER(dialog)); + webkit_download_set_destination(download->wk_download, + uri); + g_free(uri); + } else { + webkit_download_cancel(download->wk_download); + } + + gtk_widget_destroy(dialog); +} + +static gboolean +decide_destination_cb(WebKitDownload *wk_download, + const gchar *suggested_filename, MqDownload *download) +{ + GtkWidget *dialog; + GtkFileChooser *chooser; + gchar *dir; + + dialog = gtk_file_chooser_dialog_new("Save File", + GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET( + webkit_download_get_web_view(wk_download)))), + GTK_FILE_CHOOSER_ACTION_SAVE, + "_Cancel", GTK_RESPONSE_CANCEL, + "_Save", GTK_RESPONSE_ACCEPT, + NULL); + chooser = GTK_FILE_CHOOSER(dialog); + + gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE); + + dir = mq_config_get_string(download->config, "directories.downloads"); + gtk_file_chooser_set_current_folder(chooser, dir); + g_free(dir); + + gtk_file_chooser_set_current_name(chooser, suggested_filename); + /* NB: Don't free suggested_filename! WebKit will free it. */ + + g_signal_connect(dialog, "response", + G_CALLBACK(chooser_response_cb), download); + /* g_object_ref(wk_download); */ + /* WebKit requires this to be synchronous. */ + gtk_dialog_run(GTK_DIALOG(dialog)); + + return TRUE; +} + +static void +finished_cb(WebKitDownload G_GNUC_UNUSED *wk_download, MqDownload *download) +{ + g_free(download); +} + +MqDownload * +mq_download_new(MqConfig *config, WebKitDownload *wk_download) +/* TODO: Take MqDownloadManager argument when implemented. */ +{ + MqDownload *download; + + download = g_new0(MqDownload, 1); + download->config = config; + download->wk_download = wk_download; + + webkit_download_set_allow_overwrite(wk_download, TRUE); + + g_signal_connect(wk_download, "decide-destination", + G_CALLBACK(decide_destination_cb), download); + /* TODO: Add callbacks for "created-destination", "received-data", and + * "failed" signals. */ + g_signal_connect(wk_download, "finished", + G_CALLBACK(finished_cb), download); + + return download; +} diff --git a/src/download.h b/src/download.h new file mode 100644 index 0000000..92681fc --- /dev/null +++ b/src/download.h @@ -0,0 +1,38 @@ +/* + * Download + * + * Copyright (C) 2017 Patrick McDermott + * + * This file is part of Marquee. + * + * Marquee is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Marquee is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Marquee. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MQ_DOWNLOAD_H +#define MQ_DOWNLOAD_H + +typedef struct _MqDownload MqDownload; + +#include <webkit2/webkit2.h> + +#include "config.h" + +G_BEGIN_DECLS + +MqDownload * +mq_download_new(MqConfig *config, WebKitDownload *wk_download); + +G_END_DECLS + +#endif /* MQ_DOWNLOAD_H */ diff --git a/src/local.mk b/src/local.mk index d1c66b1..b6b5a9d 100644 --- a/src/local.mk +++ b/src/local.mk @@ -2,6 +2,7 @@ marquee_SOURCES += \ %reldir%/about.c \ %reldir%/application.c \ %reldir%/config.c \ + %reldir%/download.c \ %reldir%/html.c \ %reldir%/main.c \ %reldir%/notebook.c \ |