summaryrefslogtreecommitdiff
path: root/riscos/download.c
blob: 74108b136f76cc0c43935ef2d6c3231c7bb31a7f (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 * Copyright 2003 Rob Jackson <jacko@xms.ms>
 */

#include <assert.h>
#include <string.h>
#include "oslib/mimemap.h"
#include "oslib/osfile.h"
#include "oslib/wimp.h"
#include "oslib/wimpspriteop.h"
#include "netsurf/desktop/gui.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"


static wimp_window *download_template;


/**
 * Load the download window template.
 */

void ro_gui_download_init(void)
{
	download_template = ro_gui_dialog_load_template("download");
}


/**
 * Create and open a download progress window.
 */

gui_window *gui_create_download_window(struct content *content)
{
	char *nice;
	gui_window *g = xcalloc(1, sizeof(gui_window));
	os_error *e;

	assert(content->type == CONTENT_OTHER);

	g->type = GUI_DOWNLOAD_WINDOW;
	g->data.download.content = content;

	/* convert MIME type to RISC OS file type */
	e = xmimemaptranslate_mime_type_to_filetype(content->mime_type,
			&(g->data.download.file_type));
	if (e)
		g->data.download.file_type = 0xffd;

	/* fill in download window icons */
	download_template->icons[ICON_DOWNLOAD_URL].data.indirected_text.text =
		content->url;
	download_template->icons[ICON_DOWNLOAD_URL].data.indirected_text.size =
		strlen(content->url) + 1;
	strncpy(g->status, content->status_message, 256);
	download_template->icons[ICON_DOWNLOAD_STATUS].data.indirected_text.text =
		g->status;
	download_template->icons[ICON_DOWNLOAD_STATUS].data.indirected_text.size =
		256;
	sprintf(g->data.download.sprite_name, "file_%.3x",
			g->data.download.file_type);
	e = xwimpspriteop_select_sprite(g->data.download.sprite_name, 0);
	if (e)
		strcpy(g->data.download.sprite_name, "file_xxx");
	download_template->icons[ICON_DOWNLOAD_ICON].data.indirected_sprite.id =
			(osspriteop_id) g->data.download.sprite_name;
	strcpy(g->data.download.path, messages_get("SaveObject"));
	if ((nice = url_nice(content->url))) {
		strcpy(g->data.download.path, nice);
		free(nice);
	}
	download_template->icons[ICON_DOWNLOAD_PATH].data.indirected_text.text =
		g->data.download.path;
	download_template->icons[ICON_DOWNLOAD_PATH].data.indirected_text.size =
		256;

	/* create and open the download window */
	g->window = wimp_create_window(download_template);
	ro_gui_dialog_open(g->window);

	g->data.download.download_status = download_INCOMPLETE;

	g->next = window_list;
	window_list = g;
	return g;
}


/**
 * Refresh the status icon in the download window.
 */

void gui_download_window_update_status(gui_window *g)
{
	strncpy(g->status, g->data.download.content->status_message, 256);
	wimp_set_icon_state(g->window,
			ICON_DOWNLOAD_STATUS, 0, 0);
}


/**
 * Handle failed downloads.
 */

void gui_download_window_error(gui_window *g, const char *error)
{
	g->data.download.content = 0;

	/* place error message in status icon in red */
	strncpy(g->status, error, 256);
	wimp_set_icon_state(g->window,
			ICON_DOWNLOAD_STATUS,
			wimp_COLOUR_RED << wimp_ICON_FG_COLOUR_SHIFT,
			wimp_ICON_FG_COLOUR);

	/* grey out file and pathname icons */
	wimp_set_icon_state(g->window,
			ICON_DOWNLOAD_ICON, wimp_ICON_SHADED, 0);
	wimp_set_icon_state(g->window,
			ICON_DOWNLOAD_PATH, wimp_ICON_SHADED, 0);

	g->data.download.download_status = download_ERROR;
}


/**
 * Handle completed downloads.
 */

void gui_download_window_done(gui_window *g)
{
	snprintf(g->status, 256, messages_get("Downloaded"),
			human_friendly_bytesize(g->data.download.content->source_size));
	wimp_set_icon_state(g->window,
			ICON_DOWNLOAD_STATUS, 0, 0);

        // clear shaded path and icon icons
	wimp_set_icon_state(g->window,
			ICON_DOWNLOAD_ICON, 0, wimp_ICON_SHADED);
	wimp_set_icon_state(g->window,
			ICON_DOWNLOAD_PATH, 0, wimp_ICON_SHADED);

        g->data.download.download_status = download_COMPLETE;
}


/**
 * Handle clicks in a download window.
 */

void ro_download_window_click(struct gui_window *g, wimp_pointer *pointer)
{
	switch (pointer->i) {
		case ICON_DOWNLOAD_ABORT:
			if (g->data.download.download_status ==
					download_INCOMPLETE)
				fetch_abort(g->data.download.content->fetch);

			ro_download_window_close(g);
			break;

		case ICON_DOWNLOAD_ICON:
			if (g->data.download.download_status ==
					download_COMPLETE) {
				gui_current_drag_type = GUI_DRAG_DOWNLOAD_SAVE;
				current_gui = g;
				ro_gui_drag_icon(pointer);
			}
			break;
	}
}


/**
 * Handle User_Drag_Box event for a drag from a download window.
 */

void ro_download_drag_end(wimp_dragged *drag)
{
	wimp_pointer pointer;
	wimp_message message;

	wimp_get_pointer_info(&pointer);

	message.your_ref = 0;
	message.action = message_DATA_SAVE;
	message.data.data_xfer.w = pointer.w;
	message.data.data_xfer.i = pointer.i;
	message.data.data_xfer.pos.x = pointer.pos.x;
	message.data.data_xfer.pos.y = pointer.pos.y;
	message.data.data_xfer.est_size = (int)
			current_gui->data.download.content->source_size;
	message.data.data_xfer.file_type = current_gui->data.download.file_type;
	strncpy(message.data.data_xfer.file_name,
			current_gui->data.download.path, 212);
	message.size = 44 + ((strlen(message.data.data_xfer.file_name) + 4) &
			(~3u));

	wimp_send_message_to_window(wimp_USER_MESSAGE, &message,
			pointer.w, pointer.i);
}


/**
 * Handle Message_DataSaveAck for a drag from a download window.
 */

void ro_download_datasave_ack(wimp_message *message)
{
	char *data;
	char *data_end;
	os_error *error;

	assert(current_gui->data.download.download_status == download_COMPLETE);

	data = current_gui->data.download.content->source_data;
	data_end = data + current_gui->data.download.content->source_size;

	error = xosfile_save_stamped(message->data.data_xfer.file_name,
			current_gui->data.download.file_type,
			data, data_end);
	if (error) {
		LOG(("0x%x: %s\n", error->errnum, error->errmess));
		warn_user("SaveError", error->errmess);
		return;
	}

	/* Ack successful save with message_DATA_LOAD */
	message->action = message_DATA_LOAD;
	message->your_ref = message->my_ref;
	wimp_send_message_to_window(wimp_USER_MESSAGE, message, message->data.data_xfer.w, message->data.data_xfer.i);

	ro_download_window_close(current_gui);
}


struct gui_window * ro_lookup_download_window_from_w(wimp_w window)
{
  gui_window* g;
  for (g = window_list; g != NULL; g = g->next)
  {
    if (g->type == GUI_DOWNLOAD_WINDOW)
    {
      if (g->window == window)
      {
        return g;
      }
    }
  }
  return NULL;
}

void ro_download_window_close(struct gui_window *g)
{
  // free contexts etc???

  wimp_close_window(g->window);
}