summaryrefslogtreecommitdiff
path: root/riscos/options.c
blob: c570ddbfb060cea3a17cf4277c9636092892919c (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
/*
 * 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 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
 */

#include "netsurf/desktop/options.h"
#include <stdio.h>
#include <string.h>
#include "oslib/messagetrans.h"
#include "oslib/osfile.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"

struct options OPTIONS;
static char* lookup(messagetrans_control_block* cb, const char* token, const char* deflt);
static int lookup_yesno(messagetrans_control_block* cb, const char* token, const char* deflt);
static int lookup_i(messagetrans_control_block* cb, const char* token, const char* deflt);
static const char* yesno(int q);

char* lookup(messagetrans_control_block* cb, const char* token, const char* deflt)
{
	int used;
	char buffer[256];

	LOG(("Looking up token '%s'",token));

  	messagetrans_lookup(cb, token, buffer, 256, 0,0,0,0, &used);
	if (used > 0)
	{
		return strdup(buffer);
	}
	else
		return strdup(deflt);
}

int lookup_yesno(messagetrans_control_block* cb, const char* token, const char* deflt)
{
	char* find;
	LOG(("yesno token '%s' (default '%s')", token, deflt));
	find = lookup(cb, token, deflt);
	if (strcmp(find, "Y") == 0)
	{
		xfree(find);
		return -1;
	}
		xfree(find);
		return 0;
}

int lookup_i(messagetrans_control_block* cb, const char* token, const char* deflt)
{
	char* find = lookup(cb, token, deflt);
	int ret = atoi(find);
		xfree(find);
		return ret;
}

const char* yesno(int q)
{
	if (q)
		return "Y";
	else
		return "N";
}

static const char * const WRITE_DIR = "<Choices$Write>.WWW.NetSurf";

void options_write(struct options* opt, char* filename)
{
	char* fn;
	FILE* f;

	fn = xcalloc(strlen(WRITE_DIR) + (filename == 0 ? 7 : strlen(filename)) + 10,
			sizeof(char));
	sprintf(fn, "%s.%s", WRITE_DIR, filename == 0 ? "Choices" : filename);

        xosfile_create_dir("<Choices$Write>.WWW", 0);
	xosfile_create_dir(WRITE_DIR, 0);

	LOG(("filename: %s", fn));

	f = fopen(fn, "w");
	if (f != NULL)
	{
		fprintf(f, "# General options - for any platform\n# Proxy\n");
		fprintf(f, "USE_HTTP:%s\n", yesno(opt->http));
		fprintf(f, "HTTP_PROXY:%s\n", opt->http_proxy);
		fprintf(f, "HTTP_PORT:%d\n", opt->http_port);

		fprintf(f, "\n# RISC OS specific options\n# Browser\n");
		fprintf(f, "RO_MOUSE_GESTURES:%s\n", yesno(opt->use_mouse_gestures));
		fprintf(f, "RO_TEXT_SELECTION:%s\n", yesno(opt->allow_text_selection));
		fprintf(f, "RO_FORM_ELEMENTS:%s\n", yesno(opt->use_riscos_elements));
		fprintf(f, "RO_SHOW_TOOLBAR:%s\n", yesno(opt->show_toolbar));
		fprintf(f, "RO_SHOW_PRINT:%s\n", yesno(opt->show_print_preview));
		fprintf(f, "\n# Theme\n");
		fprintf(f, "RO_THEME:%s\n", opt->theme);
	}
	else
	    LOG(("Couldn't open Choices file"));

	fclose(f);
	xfree(fn);
}

void options_init(struct options* opt)
{
	opt->http = 0;
	opt->http_proxy = strdup("");
	opt->http_port = 8080;
	opt->use_mouse_gestures = 0;
	opt->allow_text_selection = 1;
	opt->use_riscos_elements = 1;
	opt->show_toolbar = 1;
	opt->show_print_preview = 0;
	opt->theme = strdup("Default");
}

void options_read(struct options* opt, char* filename)
{
	messagetrans_control_block cb;
  	messagetrans_file_flags flags;
    	char* data;
	char* fn;
	int size;

        fn = xcalloc(20 + (filename == 0 ? 7 : strlen(filename)), sizeof(char));
	sprintf(fn, "Choices:WWW.NetSurf.%s", filename == 0 ? "Choices" : filename);

	LOG(("Getting file info"));
  	if (xmessagetrans_file_info(fn, &flags, &size) != NULL)
		return;

        /* catch empty choices file - this is a kludge but should work */
        if (size <= 10) {

                LOG(("Empty Choices file - using defaults"));
                options_init(opt);
                return;
        }

	LOG(("Allocating %d bytes", size));
  	data = xcalloc(size, sizeof(char));
  	messagetrans_open_file(&cb, fn, data);

	opt->http = lookup_yesno(&cb, "USE_HTTP", "N");
	xfree(opt->http_proxy);
	opt->http_proxy = lookup(&cb, "HTTP_PROXY", "");
	opt->http_port = lookup_i(&cb, "HTTP_PORT", "8080");

	opt->use_mouse_gestures = lookup_yesno(&cb, "RO_MOUSE_GESTURES", "N");
	opt->allow_text_selection = lookup_yesno(&cb, "RO_TEXT_SELECTION", "Y");
	opt->use_riscos_elements = lookup_yesno(&cb, "RO_FORM_ELEMENTS", "Y");
	opt->show_toolbar = lookup_yesno(&cb, "RO_SHOW_TOOLBAR", "Y");
	opt->show_print_preview = lookup_yesno(&cb, "RO_SHOW_PRINT", "N");

	xfree(opt->theme);
	opt->theme = lookup(&cb, "RO_THEME", "Default");
	messagetrans_close_file(&cb);
	xfree(data);
	xfree(fn);
}

void options_to_ro(struct options* opt, struct ro_choices* ro)
{
	ro->browser.use_mouse_gestures = opt->use_mouse_gestures;
	ro->browser.allow_text_selection = opt->allow_text_selection;
	ro->browser.use_riscos_elements = opt->use_riscos_elements;
	ro->browser.show_toolbar = opt->show_toolbar;
	ro->browser.show_print_preview = opt->show_print_preview;

	ro->proxy.http = opt->http;
	if (opt->http_proxy != NULL)
		strcpy(ro->proxy.http_proxy, opt->http_proxy);
	else
		strcpy(ro->proxy.http_proxy, "");
	ro->proxy.http_port = opt->http_port;

	if (opt->theme != NULL)
		strcpy(ro->theme.name, opt->theme);
	else
		strcpy(ro->theme.name, "Default");
}

void ro_to_options(struct ro_choices* ro, struct options* opt)
{
	opt->use_mouse_gestures = ro->browser.use_mouse_gestures;
	opt->allow_text_selection = ro->browser.allow_text_selection;
	opt->use_riscos_elements = ro->browser.use_riscos_elements;
	opt->show_toolbar = ro->browser.show_toolbar;
	opt->show_print_preview = ro->browser.show_print_preview;

	opt->http = ro->proxy.http;
	xfree(opt->http_proxy);
	opt->http_proxy = strdup(ro->proxy.http_proxy);
	opt->http_port = ro->proxy.http_port;

	xfree(opt->theme);
	opt->theme = strdup(ro->theme.name);
}