summaryrefslogtreecommitdiff
path: root/riscos/theme_install.c
blob: b1b733b2f7116b22a7e2d0b8b1a36b247bbd2b22 (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
/*
 * 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 2005 James Bursa <bursa@users.sourceforge.net>
 */

/** \file
 * Theme auto-installing.
 */

#include <assert.h>
#include <stdbool.h>
#include "oslib/osfile.h"
#include "netsurf/content/content.h"
#include "netsurf/desktop/browser.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/options.h"
#include "netsurf/riscos/theme.h"
#include "netsurf/riscos/wimp.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"


static bool theme_install_active;
static struct content *theme_install_content = NULL;
static struct theme_descriptor theme_install_descriptor;
wimp_w dialog_theme_install;

static void theme_install_close(void);
void theme_install_callback(content_msg msg, struct content *c,
		void *p1, void *p2, union content_msg_data data);


/**
 * Handle a CONTENT_THEME that has started loading.
 */

void theme_install_start(struct content *c)
{
	assert(c);
	assert(c->type == CONTENT_THEME);

	if (theme_install_active) {
		warn_user("ThemeInstActive", 0);
		/* raise & centre dialog */
		return;
	}

	if (!content_add_user(c, theme_install_callback, 0, 0)) {
		warn_user("NoMemory", 0);
		return;
	}

	theme_install_active = true;

	ro_gui_set_icon_string(dialog_theme_install, ICON_THEME_INSTALL_MESSAGE,
			messages_get("ThemeInstDown"));
	ro_gui_set_icon_shaded_state(dialog_theme_install,
			ICON_THEME_INSTALL_INSTALL, true);
	ro_gui_dialog_open(dialog_theme_install);
}


/**
 * Callback for fetchcache() for theme install fetches.
 */

void theme_install_callback(content_msg msg, struct content *c,
		void *p1, void *p2, union content_msg_data data)
{
	char txt_buffer[256];
	bool error = false;
	int author_indent = 0;

	switch (msg) {
		case CONTENT_MSG_READY:
			break;

		case CONTENT_MSG_DONE:
			theme_install_content = c;
			if ((c->source_size < sizeof(struct theme_file_header)) ||
					(!ro_gui_theme_read_file_header(&theme_install_descriptor,
					(struct theme_file_header *)c->source_data)))
				error = true;
			else if (c->source_size - sizeof(struct theme_file_header) !=
					theme_install_descriptor.compressed_size)
				error = true;
			
			if (error) {
				warn_user("ThemeInvalid", 0);
				theme_install_close();
				break;
			}

			/* remove ' ' from the start of the data */
			if (theme_install_descriptor.author[0] == '')
				author_indent++;
			while (theme_install_descriptor.author[author_indent] == ' ')
				author_indent++;
			snprintf(txt_buffer, 256, messages_get("ThemeInstall"),
					theme_install_descriptor.name,
					&theme_install_descriptor.author[author_indent]);
			txt_buffer[255] = '\0';
			ro_gui_set_icon_string(dialog_theme_install,
					ICON_THEME_INSTALL_MESSAGE,
					txt_buffer);
			ro_gui_set_icon_shaded_state(dialog_theme_install,
					ICON_THEME_INSTALL_INSTALL, false);
			break;

		case CONTENT_MSG_ERROR:
			theme_install_close();
			warn_user(data.error, 0);
			break;

		case CONTENT_MSG_STATUS:
			break;

		case CONTENT_MSG_LOADING:
		case CONTENT_MSG_REDIRECT:
		case CONTENT_MSG_REFORMAT:
		case CONTENT_MSG_REDRAW:
		case CONTENT_MSG_NEWPTR:
		case CONTENT_MSG_AUTH:
		default:
			assert(0);
			break;
	}
}


/**
 * Handle clicks in the theme install window
 */
void ro_gui_theme_install_click(wimp_pointer *pointer) {
	os_error *error;
  	fileswitch_object_type obj_type;
	char theme_save[256];
	char theme_leaf[256];
	char *theme_file;
	int theme_number = 1;
	bool theme_found;
	struct theme_descriptor *theme_install;

	switch (pointer->i) {
		case ICON_THEME_INSTALL_INSTALL:
			if (theme_install_content) {
				if (url_nice(theme_install_descriptor.name, &theme_file) != URL_FUNC_OK) {
					warn_user("ThemeInstallErr", 0);
					theme_install_close();
					return;
				}
				theme_found = false;
				while (!theme_found) {
				  	if (theme_number == 1)
						snprintf(theme_leaf, 256,
								"WWW.NetSurf.Themes.%s",
								theme_file);
				  	else
						snprintf(theme_leaf, 256,
								"WWW.NetSurf.Themes.%s%i",
								theme_file, theme_number);
					theme_leaf[255] = '\0';
					theme_number++;
					snprintf(theme_save, 256,
							"<Choices$Write>.%s",
							theme_leaf);
					theme_save[255] = '\0';
					error = xosfile_read_stamped(theme_save,
							&obj_type, 0, 0, 0, 0, 0);
					if (error) {
					  	warn_user("ThemeInstallErr", 0);
						theme_install_close();
						free(theme_file);
						return;
					}
					theme_found = (obj_type == osfile_NOT_FOUND);
				}
				free(theme_file);
				error = xosfile_save_stamped(theme_save, 0xffd,
						theme_install_content->source_data,
						theme_install_content->source_data +
						theme_install_content->source_size);
				if (error) {
					warn_user("ThemeInstallErr", 0);
					theme_install_close();
					return;
				}
				/* apply theme only on Select clicks */
				if (pointer->buttons == wimp_CLICK_SELECT) {
					ro_gui_theme_get_available();
					snprintf(theme_save, 256, "Choices:%s", theme_leaf);
					theme_save[255] = '\0';
					theme_install = ro_gui_theme_find(theme_save);
					if ((!theme_install) ||
							(!ro_gui_theme_apply(theme_install))) {
						warn_user("ThemeApplyErr", 0);
					} else {
					  	theme_file = strdup(theme_save);
					  	if (!theme_file) {
					  	  	warn_user("NoMemory", 0);
					  	} else {
							free(option_theme);
							option_theme = theme_file;
						}
					}
				}
				theme_install_close();
			}
			break;
		case ICON_THEME_INSTALL_CANCEL:
			if (pointer->buttons == wimp_CLICK_ADJUST)
				break;
			theme_install_close();
			break;
	}
}

static void theme_install_close(void) {
  	theme_install_active = false;
	if (theme_install_content)
		content_close(theme_install_content);
	theme_install_content = NULL;
	ro_gui_dialog_close(dialog_theme_install);
}