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 
36 #include "desktop/cookie_manager.h"
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 
49 enum {
50 	/* Project menu */
51 	AMI_COOKIE_M_PROJECT = 0,
52 	 AMI_COOKIE_M_EXPAND,
53 	  AMI_COOKIE_M_EXPAND_ALL,
54 	  AMI_COOKIE_M_EXPAND_DOMAINS,
55 	  AMI_COOKIE_M_EXPAND_COOKIES,
56 	 AMI_COOKIE_M_COLLAPSE,
57 	  AMI_COOKIE_M_COLLAPSE_ALL,
58 	  AMI_COOKIE_M_COLLAPSE_DOMAINS,
59 	  AMI_COOKIE_M_COLLAPSE_COOKIES,
60 	 AMI_COOKIE_M_BAR_P1,
61 	 AMI_COOKIE_M_SNAPSHOT,
62 	 AMI_COOKIE_M_BAR_P2,
63 	 AMI_COOKIE_M_CLOSE,
64 	/* Edit menu */
65 	AMI_COOKIE_M_EDIT,
66 	 AMI_COOKIE_M_SELECTALL,
67 	 AMI_COOKIE_M_CLEAR,
68 	 AMI_COOKIE_M_BAR_E1,
69 	 AMI_COOKIE_M_DELETE,
70 	AMI_COOKIE_M_LAST
71 };
72 
73 /**
74  * Amiga cookie viewer window context
75  */
76 struct ami_cookie_window {
77 	/** Amiga core window context */
78 	struct ami_corewindow core;
79 
80 	struct ami_menu_data *menu_data[AMI_COOKIE_M_LAST + 1];
81 	struct Menu *imenu; /* Intuition menu */
82 };
83 
84 static struct ami_cookie_window *cookie_window = NULL;
85 
86 
87 static void
ami_cookies_menu_free(struct ami_cookie_window * cookie_win)88 ami_cookies_menu_free(struct ami_cookie_window *cookie_win)
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  */
100 static void
ami_cookies_destroy(struct ami_corewindow * ami_cw)101 ami_cookies_destroy(struct ami_corewindow *ami_cw)
102 {
103 	nserror res;
104 
105 	if(cookie_window == NULL)
106 		return;
107 
108 	res = cookie_manager_fini();
109 	if (res == NSERROR_OK) {
110 		ami_cookies_menu_free(cookie_window);
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  */
126 static nserror
ami_cookies_mouse(struct ami_corewindow * ami_cw,browser_mouse_state mouse_state,int x,int y)127 ami_cookies_mouse(struct ami_corewindow *ami_cw,
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  */
143 static nserror
ami_cookies_key(struct ami_corewindow * ami_cw,uint32_t nskey)144 ami_cookies_key(struct ami_corewindow *ami_cw, uint32_t nskey)
145 {
146 	if (cookie_manager_keypress(nskey)) {
147 			return NSERROR_OK;
148 	}
149 	return NSERROR_NOT_IMPLEMENTED;
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  */
162 static nserror
ami_cookies_draw(struct ami_corewindow * ami_cw,int x,int y,struct rect * r,struct redraw_context * ctx)163 ami_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 
HOOKF(void,ami_cookies_menu_item_project_expand_all,APTR,window,struct IntuiMessage *)176 HOOKF(void, ami_cookies_menu_item_project_expand_all, APTR, window, struct IntuiMessage *)
177 {
178 	cookie_manager_expand(false);
179 }
180 
HOOKF(void,ami_cookies_menu_item_project_expand_domains,APTR,window,struct IntuiMessage *)181 HOOKF(void, ami_cookies_menu_item_project_expand_domains, APTR, window, struct IntuiMessage *)
182 {
183 	cookie_manager_expand(true);
184 }
185 
HOOKF(void,ami_cookies_menu_item_project_expand_cookies,APTR,window,struct IntuiMessage *)186 HOOKF(void, ami_cookies_menu_item_project_expand_cookies, APTR, window, struct IntuiMessage *)
187 {
188 	cookie_manager_expand(false);
189 }
190 
HOOKF(void,ami_cookies_menu_item_project_collapse_all,APTR,window,struct IntuiMessage *)191 HOOKF(void, ami_cookies_menu_item_project_collapse_all, APTR, window, struct IntuiMessage *)
192 {
193 	cookie_manager_contract(true);
194 }
195 
HOOKF(void,ami_cookies_menu_item_project_collapse_domains,APTR,window,struct IntuiMessage *)196 HOOKF(void, ami_cookies_menu_item_project_collapse_domains, APTR, window, struct IntuiMessage *)
197 {
198 	cookie_manager_contract(true);
199 }
200 
HOOKF(void,ami_cookies_menu_item_project_collapse_cookies,APTR,window,struct IntuiMessage *)201 HOOKF(void, ami_cookies_menu_item_project_collapse_cookies, APTR, window, struct IntuiMessage *)
202 {
203 	cookie_manager_contract(false);
204 }
205 
HOOKF(void,ami_cookies_menu_item_project_snapshot,APTR,window,struct IntuiMessage *)206 HOOKF(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 
HOOKF(void,ami_cookies_menu_item_project_close,APTR,window,struct IntuiMessage *)217 HOOKF(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 
HOOKF(void,ami_cookies_menu_item_edit_select_all,APTR,window,struct IntuiMessage *)225 HOOKF(void, ami_cookies_menu_item_edit_select_all, APTR, window, struct IntuiMessage *)
226 {
227 	cookie_manager_keypress(NS_KEY_SELECT_ALL);
228 }
229 
HOOKF(void,ami_cookies_menu_item_edit_clear,APTR,window,struct IntuiMessage *)230 HOOKF(void, ami_cookies_menu_item_edit_clear, APTR, window, struct IntuiMessage *)
231 {
232 	cookie_manager_keypress(NS_KEY_CLEAR_SELECTION);
233 }
234 
HOOKF(void,ami_cookies_menu_item_edit_delete,APTR,window,struct IntuiMessage *)235 HOOKF(void, ami_cookies_menu_item_edit_delete, APTR, window, struct IntuiMessage *)
236 {
237 	cookie_manager_keypress(NS_KEY_DELETE_LEFT);
238 }
239 
240 
241 /* menu setup */
242 
ami_cookies_menulabs(struct ami_menu_data ** md)243 static 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 
279 static struct Menu *
ami_cookies_menu_create(struct ami_cookie_window * cookie_win)280 ami_cookies_menu_create(struct ami_cookie_window *cookie_win)
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 
290 static nserror
ami_cookies_create_window(struct ami_cookie_window * cookie_win)291 ami_cookies_create_window(struct ami_cookie_window *cookie_win)
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 */
ami_cookies_present(const char * search_term)348 nserror 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");
368 		ami_utf8_free(ncwin->core.wintitle);
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;
376 	ncwin->core.mouse = ami_cookies_mouse;
377 	ncwin->core.close = ami_cookies_destroy;
378 	ncwin->core.event = NULL;
379 
380 	res = ami_corewindow_init(&ncwin->core);
381 	if (res != NSERROR_OK) {
382 		ami_utf8_free(ncwin->core.wintitle);
383 		DisposeObject(ncwin->core.objects[GID_CW_WIN]);
384 		free(ncwin);
385 		return res;
386 	}
387 
388 	res = cookie_manager_init(ncwin->core.cb_table, (struct core_window *)ncwin);
389 	if (res != NSERROR_OK) {
390 		ami_utf8_free(ncwin->core.wintitle);
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 
405