summaryrefslogtreecommitdiff
path: root/riscos/cookies.c
blob: 03358cf89ec683516f56805ea44780456354e721 (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
/*
 * This file is part of NetSurf, http://netsurf-browser.org/
 * Licensed under the GNU General Public License,
 *		  http://www.opensource.org/licenses/gpl-license
 * Copyright 2006 Richard Wilson <info@tinct.net>
 */

/** \file
 * Cookies (implementation).
 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "oslib/wimp.h"
#include "oslib/wimpspriteop.h"
#include "netsurf/content/urldb.h"
#include "netsurf/desktop/cookies.h"
#include "netsurf/desktop/tree.h"
#include "netsurf/riscos/cookies.h"
#include "netsurf/riscos/dialog.h"
#include "netsurf/riscos/menus.h"
#include "netsurf/riscos/options.h"
#include "netsurf/riscos/theme.h"
#include "netsurf/riscos/treeview.h"
#include "netsurf/riscos/wimp.h"
#include "netsurf/riscos/wimp_event.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"

static bool ro_gui_cookies_click(wimp_pointer *pointer);
static struct node *ro_gui_cookies_find(const char *url);

/* The history window, toolbar and plot origins */
static wimp_w cookies_window;
struct tree *cookies_tree;
static bool cookies_init;

/**
 * Initialise cookies tree
 */
void ro_gui_cookies_initialise(void)
{
	/* create our window */
	cookies_window = ro_gui_dialog_create("tree");
	ro_gui_set_window_title(cookies_window,
			messages_get("Cookies"));
	ro_gui_wimp_event_register_redraw_window(cookies_window,
			ro_gui_tree_redraw);
	ro_gui_wimp_event_register_open_window(cookies_window,
			ro_gui_tree_open);
	ro_gui_wimp_event_register_mouse_click(cookies_window,
			ro_gui_cookies_click);

	/* Create an empty tree */
	cookies_tree = calloc(sizeof(struct tree), 1);
	if (!cookies_tree) {
		warn_user("NoMemory", 0);
		return;
	}
	cookies_tree->root = tree_create_folder_node(NULL, "Root");
	if (!cookies_tree->root) {
		warn_user("NoMemory", 0);
		free(cookies_tree);
		cookies_tree = NULL;
	}
	cookies_tree->root->expanded = true;
	cookies_tree->handle = (int)cookies_window;
	cookies_tree->movable = false;
	cookies_tree->no_drag = true;
	ro_gui_wimp_event_set_user_data(cookies_window,
			cookies_tree);
	ro_gui_wimp_event_register_keypress(cookies_window,
			ro_gui_tree_keypress);

	/* Create our toolbar */
	cookies_tree->toolbar = ro_gui_theme_create_toolbar(NULL,
			THEME_COOKIES_TOOLBAR);
	if (cookies_tree->toolbar)
		ro_gui_theme_attach_toolbar(cookies_tree->toolbar,
				cookies_window);

	cookies_init = true;
	urldb_iterate_cookies(cookies_update);
	cookies_init = false;
	tree_initialise(cookies_tree);
}


/**
 * Respond to a mouse click
 *
 * \param pointer  the pointer state
 * \return true to indicate click handled
 */
bool ro_gui_cookies_click(wimp_pointer *pointer)
{
	ro_gui_tree_click(pointer, cookies_tree);
	if (pointer->buttons == wimp_CLICK_MENU)
		ro_gui_menu_create(cookies_menu, pointer->pos.x,
				pointer->pos.y, pointer->w);
	else
		ro_gui_menu_prepare_action(pointer->w, TREE_SELECTION, false);
	return true;
}


/**
 * Perform cookie addition
 *
 * \param data Cookie data for a domain, or NULL
 * \return true (for urldb_iterate_entries)
 */
bool cookies_update(const char *domain, const struct cookie_data *data)
{
	struct node *parent;
	struct node *node = NULL;
	struct node *child;
	struct node *add;
	const struct cookie_data *cookie = NULL;
	bool expanded;

	assert(domain);

	/* check if we're a domain, and add get the first cookie */
	if (data)
		for (cookie = data; cookie->prev; cookie = cookie->prev);

	if (!cookies_init) {
		node = ro_gui_cookies_find(domain);
		if (node) {
			/* mark as deleted so we don't remove the cookies */
			expanded = node->expanded;
			for (child = node->child; child; child = child->next)
				child->deleted = true;
			if (node->child)
				tree_delete_node(cookies_tree, node->child,
						true);
			/* deleting will have contracted our node */
			node->expanded = expanded;
		}
		if (!data) {
		  	if (!node)
		  		return true;
		  	tree_delete_node(cookies_tree, node, false);
			tree_handle_node_changed(cookies_tree,
					cookies_tree->root, true, false);
			return true;
		}
	}

	if (!node) {
		for (parent = cookies_tree->root->child; parent;
				parent = parent->next) {
			if (strcmp(domain, parent->data.text) < 0)
				break;
		}
		if (!parent) {
			node = tree_create_folder_node(cookies_tree->root,
					domain);
		} else {
			node = tree_create_folder_node(NULL, domain);
			if (node)
				tree_link_node(parent, node, true);
		}
	}
	if (!node)
		return true;
	node->editable = false;

	for (; cookie; cookie = cookie->next) {
		add = tree_create_cookie_node(node, cookie);
		if (add && !cookies_init)
			tree_handle_node_changed(cookies_tree, add,
					true, false);
	}
	if (!cookies_init) {
		tree_handle_node_changed(cookies_tree, node,
				true, false);
		tree_redraw_area(cookies_tree,
				node->box.x - NODE_INSTEP,
				0, NODE_INSTEP, 16384);
	}
	return true;
}

/**
 * Find an entry in the cookie tree
 *
 * \param url The URL to find
 * \return Pointer to node, or NULL if not found
 */
struct node *ro_gui_cookies_find(const char *url)
{
	struct node *node;

	for (node = cookies_tree->root->child; node; node = node->next) {
		if (!strcmp(url, node->data.text))
			return node;
	}
	return NULL;
}