NetSurf
cookies.c
Go to the documentation of this file.
1/*
2 * Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Implementation of GTK cookie manager.
22 */
23
24#include <stdint.h>
25#include <stdlib.h>
26#include <gtk/gtk.h>
27
28#include "utils/log.h"
29#include "netsurf/keypress.h"
30#include "netsurf/plotters.h"
32
33#include "gtk/cookies.h"
34#include "gtk/plotters.h"
35#include "gtk/resources.h"
36#include "gtk/corewindow.h"
37
40 GtkBuilder *builder;
41 GtkWindow *wnd;
42};
43
44static struct nsgtk_cookie_window *cookie_window = NULL;
45
46#define MENUPROTO(x) static gboolean nsgtk_on_##x##_activate( \
47 GtkMenuItem *widget, gpointer g)
48#define MENUEVENT(x) { #x, G_CALLBACK(nsgtk_on_##x##_activate) }
49#define MENUHANDLER(x) gboolean nsgtk_on_##x##_activate(GtkMenuItem *widget, \
50 gpointer g)
51
52struct menu_events {
53 const char *widget;
54 GCallback handler;
55};
56
57/* edit menu */
58MENUPROTO(delete_selected);
59MENUPROTO(delete_all);
60MENUPROTO(select_all);
61MENUPROTO(clear_selection);
62
63/* view menu*/
64MENUPROTO(expand_all);
65MENUPROTO(expand_domains);
66MENUPROTO(expand_cookies);
67MENUPROTO(collapse_all);
68MENUPROTO(collapse_domains);
69MENUPROTO(collapse_cookies);
70
71
72static struct menu_events menu_events[] = {
73
74 /* edit menu */
75 MENUEVENT(delete_selected),
76 MENUEVENT(delete_all),
77 MENUEVENT(select_all),
78 MENUEVENT(clear_selection),
79
80 /* view menu*/
81 MENUEVENT(expand_all),
82 MENUEVENT(expand_domains),
83 MENUEVENT(expand_cookies),
84 MENUEVENT(collapse_all),
85 MENUEVENT(collapse_domains),
86 MENUEVENT(collapse_cookies),
87
88 {NULL, NULL}
89};
90
91
92/* edit menu */
93MENUHANDLER(delete_selected)
94{
96 return TRUE;
97}
98
99MENUHANDLER(delete_all)
100{
105 return TRUE;
106}
107
108MENUHANDLER(select_all)
109{
113 return TRUE;
114}
115
116MENUHANDLER(clear_selection)
117{
119 return TRUE;
120}
121
122/* view menu*/
123MENUHANDLER(expand_all)
124{
126 return TRUE;
127}
128
129MENUHANDLER(expand_domains)
130{
132 return TRUE;
133}
134
135MENUHANDLER(expand_cookies)
136{
138 return TRUE;
139}
140
141MENUHANDLER(collapse_all)
142{
144 return TRUE;
145}
146
147MENUHANDLER(collapse_domains)
148{
150 return TRUE;
151}
152
153MENUHANDLER(collapse_cookies)
154{
156 return TRUE;
157}
158
159/**
160 * Connects menu events in the cookies window.
161 */
163{
164 struct menu_events *event = menu_events;
165 GtkWidget *w;
166
167 while (event->widget != NULL) {
168 w = GTK_WIDGET(gtk_builder_get_object(ncwin->builder,
169 event->widget));
170 if (w == NULL) {
171 NSLOG(netsurf, INFO,
172 "Unable to connect menu widget ""%s""",
173 event->widget);
174 } else {
175 g_signal_connect(G_OBJECT(w),
176 "activate",
177 event->handler,
178 ncwin);
179 }
180 event++;
181 }
182}
183
184/**
185 * callback for mouse action on cookie window
186 *
187 * \param nsgtk_cw The nsgtk core window structure.
188 * \param mouse_state netsurf mouse state on event
189 * \param x location of event
190 * \param y location of event
191 * \return NSERROR_OK on success otherwise appropriate error code
192 */
193static nserror
195 browser_mouse_state mouse_state,
196 int x, int y)
197{
198 cookie_manager_mouse_action(mouse_state, x, y);
199
200 return NSERROR_OK;
201}
202
203/**
204 * callback for keypress on cookie window
205 *
206 * \param nsgtk_cw The nsgtk core window structure.
207 * \param nskey The netsurf key code
208 * \return NSERROR_OK on success otherwise appropriate error code
209 */
210static nserror
211nsgtk_cookies_key(struct nsgtk_corewindow *nsgtk_cw, uint32_t nskey)
212{
213 if (cookie_manager_keypress(nskey)) {
214 return NSERROR_OK;
215 }
217}
218
219/**
220 * callback on draw event for cookie window
221 *
222 * \param nsgtk_cw The nsgtk core window structure.
223 * \param r The rectangle of the window that needs updating.
224 * \return NSERROR_OK on success otherwise appropriate error code
225 */
226static nserror
227nsgtk_cookies_draw(struct nsgtk_corewindow *nsgtk_cw, struct rect *r)
228{
229 struct redraw_context ctx = {
230 .interactive = true,
231 .background_images = true,
232 .plot = &nsgtk_plotters
233 };
234
235 cookie_manager_redraw(0, 0, r, &ctx);
236
237 return NSERROR_OK;
238}
239
240/**
241 * Creates the window for the cookies tree.
242 *
243 * \return NSERROR_OK on success else appropriate error code on failure.
244 */
246{
247 struct nsgtk_cookie_window *ncwin;
248 nserror res;
249
250 if (cookie_window != NULL) {
251 return NSERROR_OK;
252 }
253
254 ncwin = calloc(1, sizeof(*ncwin));
255 if (ncwin == NULL) {
256 return NSERROR_NOMEM;
257 }
258
259 res = nsgtk_builder_new_from_resname("cookies", &ncwin->builder);
260 if (res != NSERROR_OK) {
261 NSLOG(netsurf, INFO, "Cookie UI builder init failed");
262 free(ncwin);
263 return res;
264 }
265
266 gtk_builder_connect_signals(ncwin->builder, NULL);
267
268 ncwin->wnd = GTK_WINDOW(gtk_builder_get_object(ncwin->builder,
269 "wndCookies"));
270
271 ncwin->core.scrolled = GTK_SCROLLED_WINDOW(
272 gtk_builder_get_object(ncwin->builder, "cookiesScrolled"));
273
274 ncwin->core.drawing_area = GTK_DRAWING_AREA(
275 gtk_builder_get_object(ncwin->builder, "cookiesDrawingArea"));
276
277 /* make the delete event hide the window */
278 g_signal_connect(G_OBJECT(ncwin->wnd),
279 "delete_event",
280 G_CALLBACK(gtk_widget_hide_on_delete),
281 NULL);
282
284
286 ncwin->core.key = nsgtk_cookies_key;
288
289 res = nsgtk_corewindow_init(&ncwin->core);
290 if (res != NSERROR_OK) {
291 free(ncwin);
292 return res;
293 }
294
295 res = cookie_manager_init(ncwin->core.cb_table,
296 (struct core_window *)ncwin);
297 if (res != NSERROR_OK) {
298 free(ncwin);
299 return res;
300 }
301
302 /* memoise window so it can be represented when necessary
303 * instead of recreating every time.
304 */
305 cookie_window = ncwin;
306
307 return NSERROR_OK;
308}
309
310
311/* exported function documented gtk/cookies.h */
312nserror nsgtk_cookies_present(const char *search_term)
313{
314 nserror res;
315
316 res = nsgtk_cookies_init();
317 if (res == NSERROR_OK) {
318 gtk_window_present(cookie_window->wnd);
319 res = cookie_manager_set_search_string(search_term);
320 }
321 return res;
322}
323
324
325/* exported function documented gtk/cookies.h */
327{
328 nserror res;
329
330 if (cookie_window == NULL) {
331 return NSERROR_OK;
332 }
333
334 res = cookie_manager_fini();
335 if (res == NSERROR_OK) {
337 gtk_widget_destroy(GTK_WIDGET(cookie_window->wnd));
338 g_object_unref(G_OBJECT(cookie_window->builder));
339 free(cookie_window);
340 cookie_window = NULL;
341 }
342
343 return res;
344}
nserror cookie_manager_contract(bool all)
Contract the treeview's nodes.
nserror cookie_manager_init(struct core_window_callback_table *cw_t, void *core_window_handle)
Initialise the cookie manager.
nserror cookie_manager_expand(bool only_folders)
Expand the treeview's nodes.
void cookie_manager_redraw(int x, int y, struct rect *clip, const struct redraw_context *ctx)
Redraw the cookies manager.
void cookie_manager_mouse_action(enum browser_mouse_state mouse, int x, int y)
Handles all kinds of mouse action.
nserror cookie_manager_fini(void)
Finalise the cookie manager.
nserror cookie_manager_set_search_string(const char *string)
Set the cookie manager search string.
bool cookie_manager_keypress(uint32_t key)
Key press handling.
Cookie Manager (interface).
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOT_IMPLEMENTED
Functionality is not implemented.
Definition: errors.h:61
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
Target independent plotting GTK+ interface.
#define MENUHANDLER(x)
Definition: cookies.c:49
static struct nsgtk_cookie_window * cookie_window
Definition: cookies.c:44
static nserror nsgtk_cookies_init(void)
Creates the window for the cookies tree.
Definition: cookies.c:245
static nserror nsgtk_cookies_mouse(struct nsgtk_corewindow *nsgtk_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse action on cookie window
Definition: cookies.c:194
static nserror nsgtk_cookies_draw(struct nsgtk_corewindow *nsgtk_cw, struct rect *r)
callback on draw event for cookie window
Definition: cookies.c:227
#define MENUEVENT(x)
Definition: cookies.c:48
static struct menu_events menu_events[]
Definition: cookies.c:72
#define MENUPROTO(x)
Definition: cookies.c:46
nserror nsgtk_cookies_present(const char *search_term)
make the cookie window visible.
Definition: cookies.c:312
static void nsgtk_cookies_init_menu(struct nsgtk_cookie_window *ncwin)
Connects menu events in the cookies window.
Definition: cookies.c:162
static nserror nsgtk_cookies_key(struct nsgtk_corewindow *nsgtk_cw, uint32_t nskey)
callback for keypress on cookie window
Definition: cookies.c:211
nserror nsgtk_cookies_destroy(void)
Free any resources allocated for the cookie window.
Definition: cookies.c:326
Cookies (interface).
nserror nsgtk_corewindow_init(struct nsgtk_corewindow *nsgtk_cw)
initialise elements of gtk core window.
Definition: corewindow.c:732
nserror nsgtk_corewindow_fini(struct nsgtk_corewindow *nsgtk_cw)
finalise elements of gtk core window.
Definition: corewindow.c:786
const struct plotter_table nsgtk_plotters
GTK plotter table.
Definition: plotters.c:647
browser_mouse_state
Mouse state.
Definition: mouse.h:43
Target independent plotting interface.
Interface to key press operations.
@ NS_KEY_SELECT_ALL
Definition: keypress.h:32
@ NS_KEY_CLEAR_SELECTION
Definition: keypress.h:45
@ NS_KEY_DELETE_LEFT
Definition: keypress.h:35
@ NS_KEY_ESCAPE
Definition: keypress.h:47
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
nserror nsgtk_builder_new_from_resname(const char *resname, GtkBuilder **builder_out)
Create gtk builder object for the named ui resource.
Definition: resources.c:526
Interface to gtk builtin resource handling.
GCallback handler
Definition: cookies.c:54
const char * widget
nsgtk core window state
Definition: corewindow.h:39
nserror(* key)(struct nsgtk_corewindow *nsgtk_cw, uint32_t nskey)
callback for keypress on nsgtk core window
Definition: corewindow.h:74
nserror(* draw)(struct nsgtk_corewindow *nsgtk_cw, struct rect *r)
callback to draw on drawable area of nsgtk core window
Definition: corewindow.h:63
struct core_window_callback_table * cb_table
table of callbacks for core window operations
Definition: corewindow.h:50
GtkScrolledWindow * scrolled
scrollable area drawing area is within
Definition: corewindow.h:44
nserror(* mouse)(struct nsgtk_corewindow *nsgtk_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on nsgtk core window
Definition: corewindow.h:85
GtkDrawingArea * drawing_area
GTK drawable widget.
Definition: corewindow.h:42
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59