summaryrefslogtreecommitdiff
path: root/frontends/gtk/cookies.c
blob: f252e6b3c7764878e1c1e7b9c61528e285477c66 (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
/*
 * Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#include <gtk/gtk.h>
#include <stdint.h>

#include "utils/log.h"
#include "netsurf/keypress.h"
#include "desktop/cookie_manager.h"
#include "desktop/plot_style.h"
#include "desktop/tree.h"

#include "gtk/cookies.h"
#include "gtk/plotters.h"
#include "gtk/scaffolding.h"
#include "gtk/treeview.h"
#include "gtk/resources.h"

static struct nsgtk_treeview *cookies_treeview;
static GtkBuilder *cookie_builder;
static GtkWindow *wndCookies = NULL;

#define MENUPROTO(x) static gboolean nsgtk_on_##x##_activate(	\
		GtkMenuItem *widget, gpointer g)
#define MENUEVENT(x) { #x, G_CALLBACK(nsgtk_on_##x##_activate) }
#define MENUHANDLER(x) gboolean nsgtk_on_##x##_activate(GtkMenuItem *widget, \
		gpointer g)

struct menu_events {
	const char *widget;
	GCallback handler;
};

/* edit menu */
MENUPROTO(delete_selected);
MENUPROTO(delete_all);
MENUPROTO(select_all);
MENUPROTO(clear_selection);

/* view menu*/
MENUPROTO(expand_all);
MENUPROTO(expand_domains);
MENUPROTO(expand_cookies);
MENUPROTO(collapse_all);
MENUPROTO(collapse_domains);
MENUPROTO(collapse_cookies);


static struct menu_events menu_events[] = {

	/* edit menu */
	MENUEVENT(delete_selected),
	MENUEVENT(delete_all),
	MENUEVENT(select_all),
	MENUEVENT(clear_selection),

	/* view menu*/
	MENUEVENT(expand_all),
	MENUEVENT(expand_domains),
	MENUEVENT(expand_cookies),
	MENUEVENT(collapse_all),
	MENUEVENT(collapse_domains),
	MENUEVENT(collapse_cookies),

	{NULL, NULL}
};



/**
 * Connects menu events in the cookies window.
 */
static void nsgtk_cookies_init_menu(void)
{
	struct menu_events *event = menu_events;
	GtkWidget *w;

	while (event->widget != NULL) {
		w = GTK_WIDGET(gtk_builder_get_object(cookie_builder, event->widget));
		if (w == NULL) {
			LOG("Unable to connect menu widget ""%s""", event->widget);		} else {
			g_signal_connect(G_OBJECT(w), "activate", event->handler, cookies_treeview);
		}
		event++;
	}
}

/**
 * Creates the window for the cookies tree.
 *
 * \return NSERROR_OK on success else appropriate error code on faliure.
 */
static nserror nsgtk_cookies_init(void)
{
	GtkScrolledWindow *scrolled;
	GtkDrawingArea *drawing_area;
	nserror res;

	if (wndCookies != NULL) {
		return NSERROR_OK;
	}

	res = nsgtk_builder_new_from_resname("cookies", &cookie_builder);
	if (res != NSERROR_OK) {
		LOG("Cookie UI builder init failed");
		return res;
	}

	gtk_builder_connect_signals(cookie_builder, NULL);

	wndCookies = GTK_WINDOW(gtk_builder_get_object(cookie_builder,
			"wndCookies"));

	scrolled = GTK_SCROLLED_WINDOW(gtk_builder_get_object(cookie_builder,
			"cookiesScrolled"));

	drawing_area = GTK_DRAWING_AREA(gtk_builder_get_object(cookie_builder,
			"cookiesDrawingArea"));

	cookies_treeview = nsgtk_treeview_create(TREE_COOKIES,
						 wndCookies,
						 scrolled,
						 drawing_area,
						 NULL);
	if (cookies_treeview == NULL) {
		return NSERROR_INIT_FAILED;
	}

#define CONNECT(obj, sig, callback, ptr) \
	g_signal_connect(G_OBJECT(obj), (sig), G_CALLBACK(callback), (ptr))

	CONNECT(wndCookies, "delete_event", gtk_widget_hide_on_delete, NULL);
	CONNECT(wndCookies, "hide", nsgtk_tree_window_hide, cookies_treeview);

	nsgtk_cookies_init_menu();

	return NSERROR_OK;
}




/* edit menu */
MENUHANDLER(delete_selected)
{
	cookie_manager_keypress(NS_KEY_DELETE_LEFT);
	return TRUE;
}

MENUHANDLER(delete_all)
{
	cookie_manager_keypress(NS_KEY_SELECT_ALL);
	cookie_manager_keypress(NS_KEY_DELETE_LEFT);
	return TRUE;
}

MENUHANDLER(select_all)
{
	cookie_manager_keypress(NS_KEY_SELECT_ALL);
	return TRUE;
}

MENUHANDLER(clear_selection)
{
	cookie_manager_keypress(NS_KEY_CLEAR_SELECTION);
	return TRUE;
}

/* view menu*/
MENUHANDLER(expand_all)
{
	cookie_manager_expand(false);
	return TRUE;
}

MENUHANDLER(expand_domains)
{
	cookie_manager_expand(true);
	return TRUE;
}

MENUHANDLER(expand_cookies)
{
	cookie_manager_expand(false);
	return TRUE;
}

MENUHANDLER(collapse_all)
{
	cookie_manager_contract(true);
	return TRUE;
}

MENUHANDLER(collapse_domains)
{
	cookie_manager_contract(true);
	return TRUE;
}

MENUHANDLER(collapse_cookies)
{
	cookie_manager_contract(false);
	return TRUE;
}

/* exported function documented gtk/cookies.h */
nserror nsgtk_cookies_present(void)
{
	nserror res;

	res = nsgtk_cookies_init();
	if (res == NSERROR_OK) {
		gtk_window_present(wndCookies);
	}
	return res;
}

/* exported function documented gtk/cookies.h */
void nsgtk_cookies_destroy(void)
{
	/** \todo what about cookie_builder? */
	if (wndCookies != NULL) {
		nsgtk_treeview_destroy(cookies_treeview);
	}
}