NetSurf
cookies.c
Go to the documentation of this file.
1/*
2 * Copyright 2017 Chris Young <chris@unsatisfactorysoftware.co.uk>
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 Amiga cookie viewer using core windows.
22 */
23
24#include <stdint.h>
25#include <stdlib.h>
26#include <stdbool.h>
27
28#include <proto/intuition.h>
29
30#include <classes/window.h>
31#include <gadgets/layout.h>
32#include <gadgets/space.h>
33
34#include <reaction/reaction_macros.h>
35
37#include "netsurf/keypress.h"
38#include "netsurf/plotters.h"
39#include "utils/log.h"
40#include "utils/messages.h"
41#include "utils/nsoption.h"
42
43#include "amiga/cookies.h"
44#include "amiga/corewindow.h"
45#include "amiga/libs.h"
46#include "amiga/menu.h"
47#include "amiga/utf8.h"
48
49enum {
50 /* Project menu */
64 /* Edit menu */
71};
72
73/**
74 * Amiga cookie viewer window context
75 */
77 /** Amiga core window context */
79
81 struct Menu *imenu; /* Intuition menu */
82};
83
84static struct ami_cookie_window *cookie_window = NULL;
85
86
87static void
89{
90 SetAttrs(cookie_win->core.objects[GID_CW_WIN],
91 WINDOW_MenuStrip, NULL,
92 TAG_DONE);
93
94 ami_menu_free_menu(cookie_win->menu_data, AMI_COOKIE_M_LAST, cookie_win->imenu);
95}
96
97/**
98 * destroy a previously created cookie view
99 */
100static void
102{
103 nserror res;
104
105 if(cookie_window == NULL)
106 return;
107
108 res = cookie_manager_fini();
109 if (res == NSERROR_OK) {
111 res = ami_corewindow_fini(&cookie_window->core); /* closes the window for us, frees cookie_win */
112 cookie_window = NULL;
113 }
114}
115
116
117/**
118 * callback for mouse action for cookie viewer on core window
119 *
120 * \param ami_cw The Amiga core window structure.
121 * \param mouse_state netsurf mouse state on event
122 * \param x location of event
123 * \param y location of event
124 * \return NSERROR_OK on success otherwise apropriate error code
125 */
126static nserror
128 browser_mouse_state mouse_state,
129 int x, int y)
130{
131 cookie_manager_mouse_action(mouse_state, x, y);
132
133 return NSERROR_OK;
134}
135
136/**
137 * callback for keypress for cookies viewer on core window
138 *
139 * \param ami_cw The Amiga core window structure.
140 * \param nskey The netsurf key code
141 * \return NSERROR_OK on success otherwise apropriate error code
142 */
143static nserror
144ami_cookies_key(struct ami_corewindow *ami_cw, uint32_t nskey)
145{
146 if (cookie_manager_keypress(nskey)) {
147 return NSERROR_OK;
148 }
150}
151
152/**
153 * callback on draw event for cookies viewer on core window
154 *
155 * \param ami_cw The Amiga core window structure.
156 * \param x The x cordinate to plot at
157 * \param y The y cordinate to plot at
158 * \param r The rectangle of the window that needs updating.
159 * \param ctx The drawing context
160 * \return NSERROR_OK on success otherwise apropriate error code
161 */
162static nserror
163ami_cookies_draw(struct ami_corewindow *ami_cw, int x, int y, struct rect *r, struct redraw_context *ctx)
164{
165 cookie_manager_redraw(x, y, r, ctx);
166
167 return NSERROR_OK;
168}
169
170/**
171 * menu stuff
172 */
173
174 /* menu hook functions */
175
176HOOKF(void, ami_cookies_menu_item_project_expand_all, APTR, window, struct IntuiMessage *)
177{
179}
180
181HOOKF(void, ami_cookies_menu_item_project_expand_domains, APTR, window, struct IntuiMessage *)
182{
184}
185
186HOOKF(void, ami_cookies_menu_item_project_expand_cookies, APTR, window, struct IntuiMessage *)
187{
189}
190
191HOOKF(void, ami_cookies_menu_item_project_collapse_all, APTR, window, struct IntuiMessage *)
192{
194}
195
196HOOKF(void, ami_cookies_menu_item_project_collapse_domains, APTR, window, struct IntuiMessage *)
197{
199}
200
201HOOKF(void, ami_cookies_menu_item_project_collapse_cookies, APTR, window, struct IntuiMessage *)
202{
204}
205
206HOOKF(void, ami_cookies_menu_item_project_snapshot, APTR, window, struct IntuiMessage *)
207{
208 struct ami_corewindow *ami_cw;
209 GetAttr(WINDOW_UserData, (Object *)window, (ULONG *)&ami_cw);
210
211 nsoption_set_int(cookies_window_ypos, ami_cw->win->TopEdge);
212 nsoption_set_int(cookies_window_xpos, ami_cw->win->LeftEdge);
213 nsoption_set_int(cookies_window_xsize, ami_cw->win->Width);
214 nsoption_set_int(cookies_window_ysize, ami_cw->win->Height);
215}
216
217HOOKF(void, ami_cookies_menu_item_project_close, APTR, window, struct IntuiMessage *)
218{
219 struct ami_corewindow *ami_cw;
220 GetAttr(WINDOW_UserData, (Object *)window, (ULONG *)&ami_cw);
221
222 ami_cw->close_window = true;
223}
224
225HOOKF(void, ami_cookies_menu_item_edit_select_all, APTR, window, struct IntuiMessage *)
226{
228}
229
230HOOKF(void, ami_cookies_menu_item_edit_clear, APTR, window, struct IntuiMessage *)
231{
233}
234
235HOOKF(void, ami_cookies_menu_item_edit_delete, APTR, window, struct IntuiMessage *)
236{
238}
239
240
241/* menu setup */
242
243static void ami_cookies_menulabs(struct ami_menu_data **md)
244{
245 ami_menu_alloc_item(md, AMI_COOKIE_M_PROJECT, NM_TITLE, "Tree", NULL, NULL, NULL, NULL, 0);
246 ami_menu_alloc_item(md, AMI_COOKIE_M_EXPAND, NM_ITEM, "Expand", NULL, "TBImages:list_folderunfold", NULL, NULL, 0);
247 ami_menu_alloc_item(md, AMI_COOKIE_M_EXPAND_ALL, NM_SUB, "All", "+", NULL,
248 ami_cookies_menu_item_project_expand_all, NULL, 0);
249 ami_menu_alloc_item(md, AMI_COOKIE_M_EXPAND_DOMAINS, NM_SUB, "Domains", NULL, NULL,
250 ami_cookies_menu_item_project_expand_domains, NULL, 0);
251 ami_menu_alloc_item(md, AMI_COOKIE_M_EXPAND_COOKIES, NM_SUB, "Cookies", NULL, NULL,
252 ami_cookies_menu_item_project_expand_cookies, NULL, 0);
253 ami_menu_alloc_item(md, AMI_COOKIE_M_COLLAPSE, NM_ITEM, "Collapse", NULL, "TBImages:list_folderfold", NULL, NULL, 0);
254 ami_menu_alloc_item(md, AMI_COOKIE_M_COLLAPSE_ALL, NM_SUB, "All", "-", NULL,
255 ami_cookies_menu_item_project_collapse_all, NULL, 0);
256 ami_menu_alloc_item(md, AMI_COOKIE_M_COLLAPSE_DOMAINS, NM_SUB, "Domains", NULL, NULL,
257 ami_cookies_menu_item_project_collapse_domains, NULL, 0);
258 ami_menu_alloc_item(md, AMI_COOKIE_M_COLLAPSE_COOKIES, NM_SUB, "Cookies", NULL, NULL,
259 ami_cookies_menu_item_project_collapse_cookies, NULL, 0);
260 ami_menu_alloc_item(md, AMI_COOKIE_M_BAR_P1, NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL, 0);
261 ami_menu_alloc_item(md, AMI_COOKIE_M_SNAPSHOT, NM_ITEM, "SnapshotWindow", NULL, "TBImages:list_hold",
262 ami_cookies_menu_item_project_snapshot, NULL, 0);
263 ami_menu_alloc_item(md, AMI_COOKIE_M_BAR_P2, NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL, 0);
264 ami_menu_alloc_item(md, AMI_COOKIE_M_CLOSE, NM_ITEM, "CloseWindow", "K", "TBImages:list_cancel",
265 ami_cookies_menu_item_project_close, NULL, 0);
266
267 ami_menu_alloc_item(md, AMI_COOKIE_M_EDIT, NM_TITLE, "Edit", NULL, NULL, NULL, NULL, 0);
268 ami_menu_alloc_item(md, AMI_COOKIE_M_SELECTALL, NM_ITEM, "SelectAllNS", "A", NSA_SPACE,
269 ami_cookies_menu_item_edit_select_all, NULL, 0);
270 ami_menu_alloc_item(md, AMI_COOKIE_M_CLEAR, NM_ITEM, "ClearNS", NULL, NSA_SPACE,
271 ami_cookies_menu_item_edit_clear, NULL, 0);
272 ami_menu_alloc_item(md, AMI_COOKIE_M_BAR_E1, NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL, 0);
273 ami_menu_alloc_item(md, AMI_COOKIE_M_DELETE, NM_ITEM, "TreeDelete", "Del", "TBImages:list_delete",
274 ami_cookies_menu_item_edit_delete, NULL, 0);
275
276 ami_menu_alloc_item(md, AMI_COOKIE_M_LAST, NM_END, NULL, NULL, NULL, NULL, NULL, 0);
277}
278
279static struct Menu *
281{
282 ami_cookies_menulabs(cookie_win->menu_data);
283 cookie_win->imenu = ami_menu_layout(cookie_win->menu_data, AMI_COOKIE_M_LAST);
284 if(cookie_win->imenu == NULL) return NULL;
285
286 return cookie_win->imenu;
287}
288
289
290static nserror
292{
293 struct ami_corewindow *ami_cw = (struct ami_corewindow *)&cookie_win->core;
294 ULONG refresh_mode = WA_SmartRefresh;
295
296 if(nsoption_bool(window_simple_refresh) == true) {
297 refresh_mode = WA_SimpleRefresh;
298 }
299
300 ami_cw->objects[GID_CW_WIN] = WindowObj,
301 WA_ScreenTitle, ami_gui_get_screen_title(),
302 WA_Title, ami_cw->wintitle,
303 WA_Activate, TRUE,
304 WA_DepthGadget, TRUE,
305 WA_DragBar, TRUE,
306 WA_CloseGadget, TRUE,
307 WA_SizeGadget, TRUE,
308 WA_SizeBRight, TRUE,
309 WA_Top, nsoption_int(cookies_window_ypos),
310 WA_Left, nsoption_int(cookies_window_xpos),
311 WA_Width, nsoption_int(cookies_window_xsize),
312 WA_Height, nsoption_int(cookies_window_ysize),
313 WA_PubScreen, ami_gui_get_screen(),
314 WA_ReportMouse, TRUE,
315 refresh_mode, TRUE,
316 WA_IDCMP, IDCMP_MOUSEMOVE | IDCMP_MOUSEBUTTONS | IDCMP_NEWSIZE |
317 IDCMP_RAWKEY | IDCMP_GADGETUP | IDCMP_IDCMPUPDATE |
318 IDCMP_EXTENDEDMOUSE | IDCMP_SIZEVERIFY | IDCMP_REFRESHWINDOW,
319 WINDOW_IDCMPHook, &ami_cw->idcmp_hook,
320 WINDOW_IDCMPHookBits, IDCMP_IDCMPUPDATE | IDCMP_EXTENDEDMOUSE |
321 IDCMP_SIZEVERIFY | IDCMP_REFRESHWINDOW,
322 WINDOW_SharedPort, ami_gui_get_shared_msgport(),
323 WINDOW_HorizProp, 1,
324 WINDOW_VertProp, 1,
325 WINDOW_UserData, cookie_win,
326 WINDOW_MenuStrip, ami_cookies_menu_create(cookie_win),
327 WINDOW_MenuUserData, WGUD_HOOK,
328 WINDOW_IconifyGadget, FALSE,
329 WINDOW_Position, WPOS_CENTERSCREEN,
330 WINDOW_ParentGroup, ami_cw->objects[GID_CW_MAIN] = LayoutVObj,
331 LAYOUT_AddChild, ami_cw->objects[GID_CW_DRAW] = SpaceObj,
332 GA_ID, GID_CW_DRAW,
333 SPACE_Transparent, TRUE,
334 SPACE_BevelStyle, BVS_DISPLAY,
335 GA_RelVerify, TRUE,
336 SpaceEnd,
337 EndGroup,
338 EndWindow;
339
340 if(ami_cw->objects[GID_CW_WIN] == NULL) {
341 return NSERROR_NOMEM;
342 }
343
344 return NSERROR_OK;
345}
346
347/* exported interface documented in amiga/cookies.h */
348nserror ami_cookies_present(const char *search_term)
349{
350 struct ami_cookie_window *ncwin;
351 nserror res;
352
353 if(cookie_window != NULL) {
354 //windowtofront()
355 return NSERROR_OK;
356 }
357
358 ncwin = calloc(1, sizeof(struct ami_cookie_window));
359 if (ncwin == NULL) {
360 return NSERROR_NOMEM;
361 }
362
363 ncwin->core.wintitle = ami_utf8_easy((char *)messages_get("Cookies"));
364
365 res = ami_cookies_create_window(ncwin);
366 if (res != NSERROR_OK) {
367 NSLOG(netsurf, INFO, "SSL UI builder init failed");
369 free(ncwin);
370 return res;
371 }
372
373 /* initialise Amiga core window */
374 ncwin->core.draw = ami_cookies_draw;
375 ncwin->core.key = ami_cookies_key;
378 ncwin->core.event = NULL;
379
380 res = ami_corewindow_init(&ncwin->core);
381 if (res != NSERROR_OK) {
383 DisposeObject(ncwin->core.objects[GID_CW_WIN]);
384 free(ncwin);
385 return res;
386 }
387
388 res = cookie_manager_init((struct core_window *)ncwin);
389 if (res != NSERROR_OK) {
391 DisposeObject(ncwin->core.objects[GID_CW_WIN]);
392 free(ncwin);
393 return res;
394 }
395
396 cookie_window = ncwin;
397
398 if (search_term != NULL) {
399 res = cookie_manager_set_search_string(search_term);
400 }
401
402 return res;
403}
404
static struct ami_cookie_window * cookie_window
Definition: cookies.c:84
static nserror ami_cookies_draw(struct ami_corewindow *ami_cw, int x, int y, struct rect *r, struct redraw_context *ctx)
callback on draw event for cookies viewer on core window
Definition: cookies.c:163
static void ami_cookies_destroy(struct ami_corewindow *ami_cw)
destroy a previously created cookie view
Definition: cookies.c:101
nserror ami_cookies_present(const char *search_term)
Open the cookie viewer.
Definition: cookies.c:348
static nserror ami_cookies_mouse(struct ami_corewindow *ami_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse action for cookie viewer on core window
Definition: cookies.c:127
HOOKF(void, ami_cookies_menu_item_project_expand_all, APTR, window, struct IntuiMessage *)
menu stuff
Definition: cookies.c:176
static struct Menu * ami_cookies_menu_create(struct ami_cookie_window *cookie_win)
Definition: cookies.c:280
@ AMI_COOKIE_M_EXPAND_COOKIES
Definition: cookies.c:55
@ AMI_COOKIE_M_BAR_P1
Definition: cookies.c:60
@ AMI_COOKIE_M_SNAPSHOT
Definition: cookies.c:61
@ AMI_COOKIE_M_SELECTALL
Definition: cookies.c:66
@ AMI_COOKIE_M_EDIT
Definition: cookies.c:65
@ AMI_COOKIE_M_LAST
Definition: cookies.c:70
@ AMI_COOKIE_M_CLOSE
Definition: cookies.c:63
@ AMI_COOKIE_M_DELETE
Definition: cookies.c:69
@ AMI_COOKIE_M_EXPAND
Definition: cookies.c:52
@ AMI_COOKIE_M_PROJECT
Definition: cookies.c:51
@ AMI_COOKIE_M_COLLAPSE
Definition: cookies.c:56
@ AMI_COOKIE_M_EXPAND_ALL
Definition: cookies.c:53
@ AMI_COOKIE_M_COLLAPSE_ALL
Definition: cookies.c:57
@ AMI_COOKIE_M_BAR_E1
Definition: cookies.c:68
@ AMI_COOKIE_M_CLEAR
Definition: cookies.c:67
@ AMI_COOKIE_M_EXPAND_DOMAINS
Definition: cookies.c:54
@ AMI_COOKIE_M_COLLAPSE_DOMAINS
Definition: cookies.c:58
@ AMI_COOKIE_M_COLLAPSE_COOKIES
Definition: cookies.c:59
@ AMI_COOKIE_M_BAR_P2
Definition: cookies.c:62
static void ami_cookies_menu_free(struct ami_cookie_window *cookie_win)
Definition: cookies.c:88
static nserror ami_cookies_create_window(struct ami_cookie_window *cookie_win)
Definition: cookies.c:291
static nserror ami_cookies_key(struct ami_corewindow *ami_cw, uint32_t nskey)
callback for keypress for cookies viewer on core window
Definition: cookies.c:144
static void ami_cookies_menulabs(struct ami_menu_data **md)
Definition: cookies.c:243
nserror ami_corewindow_fini(struct ami_corewindow *ami_cw)
finalise elements of Amiga core window.
Definition: corewindow.c:987
nserror ami_corewindow_init(struct ami_corewindow *ami_cw)
initialise elements of Amiga core window.
Definition: corewindow.c:928
@ GID_CW_MAIN
Definition: corewindow.h:35
@ GID_CW_WIN
Definition: corewindow.h:34
@ GID_CW_DRAW
Definition: corewindow.h:36
struct Screen * ami_gui_get_screen(void)
Get a pointer to the screen NetSurf is running on.
Definition: gui.c:404
STRPTR ami_gui_get_screen_title(void)
Get the string for NetSurf's screen titlebar.
Definition: gui.c:975
struct MsgPort * ami_gui_get_shared_msgport(void)
Get shared message port.
struct Menu * ami_menu_layout(struct ami_menu_data **md, int max)
Definition: menu.c:419
void ami_menu_free_menu(struct ami_menu_data **md, int max, struct Menu *imenu)
Definition: menu.c:430
void ami_menu_alloc_item(struct ami_menu_data **md, int num, UBYTE type, const char *restrict label, const char *restrict key, const char *restrict icon, void *restrict func, void *restrict hookdata, UWORD flags)
Definition: menu.c:115
#define NSA_SPACE
empty space
Definition: menu.h:37
nserror cookie_manager_contract(bool all)
Contract the treeview's nodes.
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_init(void *core_window_handle)
Initialise the cookie manager.
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
void ami_utf8_free(char *ptr)
Definition: utf8.c:104
char * ami_utf8_easy(const char *string)
Definition: utf8.c:109
browser_mouse_state
Mouse state: 1 is primary mouse button.
Definition: mouse.h:52
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
#define WindowObj
Definition: libs.h:77
#define LayoutVObj
Definition: libs.h:65
#define SpaceObj
Definition: libs.h:74
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
#define IDCMP_EXTENDEDMOUSE
Definition: os3support.h:121
#define BVS_DISPLAY
Definition: os3support.h:120
Amiga core window state.
Definition: corewindow.h:47
void(* close)(struct ami_corewindow *ami_cw)
callback to close an Amiga core window
Definition: corewindow.h:164
Object * objects[GID_CW_LAST]
Definition: corewindow.h:54
bool close_window
Definition: corewindow.h:67
nserror(* mouse)(struct ami_corewindow *ami_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on Amiga core window
Definition: corewindow.h:120
nserror(* draw)(struct ami_corewindow *ami_cw, int x, int y, struct rect *r, struct redraw_context *ctx)
callback to draw on drawable area of Amiga core window
Definition: corewindow.h:97
nserror(* key)(struct ami_corewindow *ami_cw, uint32_t nskey)
callback for keypress on Amiga core window
Definition: corewindow.h:109
struct Window * win
Definition: corewindow.h:53
BOOL(* event)(struct ami_corewindow *ami_cw, ULONG result)
callback for unknown events on Amiga core window eg.
Definition: corewindow.h:132
struct Hook idcmp_hook
Definition: corewindow.h:56
char * wintitle
window title, must be allocated wth ami_utf8 function
Definition: corewindow.h:78
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
Option reading and saving interface.
#define nsoption_int(OPTION)
Get the value of an integer option.
Definition: nsoption.h:313
#define nsoption_set_int(OPTION, VALUE)
set an integer option in the default table
Definition: nsoption.h:348
#define nsoption_bool(OPTION)
Get the value of a boolean option.
Definition: nsoption.h:304