summaryrefslogtreecommitdiffstats
path: root/src/download.c
diff options
context:
space:
mode:
authorPatrick McDermott <pj@pehjota.net>2017-10-29 14:51:07 (EDT)
committer Patrick McDermott <pj@pehjota.net>2017-10-29 14:51:07 (EDT)
commit4c3e1927b48a981e7481c27514393fc1e32f408d (patch)
treef2eb021b4ad18eecc4e7eb63dece324edb4a9aa8 /src/download.c
parent6b25c8a994a84da1b629e14cc4ce149d43da9b3a (diff)
downloadmarquee-4c3e1927b48a981e7481c27514393fc1e32f408d.zip
marquee-4c3e1927b48a981e7481c27514393fc1e32f408d.tar.gz
marquee-4c3e1927b48a981e7481c27514393fc1e32f408d.tar.bz2
MqDownload: New type
Diffstat (limited to 'src/download.c')
-rw-r--r--src/download.c123
1 files changed, 123 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;
+}