summaryrefslogtreecommitdiffstats
path: root/src/download.c
blob: c97a59248341f274eec6c5c1a43f5a7f2d9f1ef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * 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/config.h"
#include "i18n.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;
}