1 /*
2  * Copyright 2010, 2013 Stephen Fryatt <stevef@netsurf-browser.org>
3  * Copyright 2016 Vincent Sanders <vince@netsurf-browser.org>
4  *
5  * This file is part of NetSurf, http://www.netsurf-browser.org/
6  *
7  * NetSurf is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * NetSurf is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * \file
22  * Implementation of RISC OS hotlist manager.
23  */
24 
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <oslib/osmodule.h>
28 #include <oslib/wimp.h>
29 
30 #include "utils/log.h"
31 #include "utils/messages.h"
32 #include "utils/nsoption.h"
33 #include "utils/nsurl.h"
34 #include "netsurf/url_db.h"
35 #include "netsurf/window.h"
36 #include "netsurf/plotters.h"
37 #include "netsurf/keypress.h"
38 #include "desktop/hotlist.h"
39 
40 #include "riscos/gui.h"
41 #include "riscos/dialog.h"
42 #include "riscos/message.h"
43 #include "riscos/save.h"
44 #include "riscos/toolbar.h"
45 #include "riscos/wimp.h"
46 #include "riscos/wimp_event.h"
47 #include "riscos/query.h"
48 #include "riscos/menus.h"
49 #include "riscos/corewindow.h"
50 #include "riscos/hotlist.h"
51 
52 /**
53  * Hotlist window container for RISC OS.
54  */
55 struct ro_hotlist_window {
56 	struct ro_corewindow core;
57 	wimp_menu *menu;
58 };
59 
60 /** hotlist window singleton */
61 static struct ro_hotlist_window *hotlist_window = NULL;
62 
63 /** riscos template for hotlist window */
64 static wimp_window *dialog_hotlist_template;
65 
66 /** Hotlist Query Handler. */
67 static query_id	hotlist_query = QUERY_INVALID;
68 static nsurl *hotlist_delete_url = NULL;
69 
70 /**
71  * URL adding hotlist protocol message block.
72  *
73  * Message block is not currently in OSLib.
74  */
75 struct ro_hotlist_message_hotlist_addurl {
76 	wimp_MESSAGE_HEADER_MEMBERS /**< The standard message header. */
77 	char *url; /**< Pointer to the URL in RMA. */
78 	char *title; /**< Pointer to the title in RMA. */
79 	char appname[32]; /**< The application name. */
80 };
81 
82 /**
83  * change hotlist protocol message block.
84  *
85  * Message block is not currently in OSLib.
86  */
87 struct ro_hotlist_message_hotlist_changed {
88 	wimp_MESSAGE_HEADER_MEMBERS /**< The standard message header. */
89 };
90 
91 /** URL area claimed from RMA. */
92 static char *hotlist_url = NULL;
93 /** Title area claimed from RMA. */
94 static char *hotlist_title = NULL;
95 
96 
97 /**
98  * callback to draw on drawable area of ro hotlist window
99  *
100  * \param ro_cw The riscos core window structure.
101  * \param r The rectangle of the window that needs updating.
102  * \param originx The risc os plotter x origin.
103  * \param originy The risc os plotter y origin.
104  * \return NSERROR_OK on success otherwise apropriate error code
105  */
106 static nserror
hotlist_draw(struct ro_corewindow * ro_cw,int originx,int originy,struct rect * r)107 hotlist_draw(struct ro_corewindow *ro_cw,
108 	     int originx,
109 	     int originy,
110 	     struct rect *r)
111 {
112 	struct redraw_context ctx = {
113 		.interactive = true,
114 		.background_images = true,
115 		.plot = &ro_plotters
116 	};
117 
118 	ro_plot_origin_x = originx;
119 	ro_plot_origin_y = originy;
120 	no_font_blending = true;
121 	hotlist_redraw(0, 0, r, &ctx);
122 	no_font_blending = false;
123 
124 	return NSERROR_OK;
125 }
126 
127 
128 /**
129  * callback for keypress on ro hotlist window
130  *
131  * \param ro_cw The ro core window structure.
132  * \param nskey The netsurf key code.
133  * \return NSERROR_OK if key processed,
134  *         NSERROR_NOT_IMPLEMENTED if key not processed
135  *         otherwise apropriate error code
136  */
hotlist_key(struct ro_corewindow * ro_cw,uint32_t nskey)137 static nserror hotlist_key(struct ro_corewindow *ro_cw, uint32_t nskey)
138 {
139 	if (hotlist_keypress(nskey)) {
140 		return NSERROR_OK;
141 	}
142 	return NSERROR_NOT_IMPLEMENTED;
143 }
144 
145 
146 /**
147  * callback for mouse event on ro hotlist window
148  *
149  * \param ro_cw The ro core window structure.
150  * \param mouse_state mouse state
151  * \param x location of event
152  * \param y location of event
153  * \return NSERROR_OK on sucess otherwise apropriate error code.
154  */
155 static nserror
hotlist_mouse(struct ro_corewindow * ro_cw,browser_mouse_state mouse_state,int x,int y)156 hotlist_mouse(struct ro_corewindow *ro_cw,
157 	      browser_mouse_state mouse_state,
158 	      int x, int y)
159 {
160 	hotlist_mouse_action(mouse_state, x, y);
161 
162 	return NSERROR_OK;
163 }
164 
165 
166 /**
167  * handle clicks in ro hotlist window toolbar.
168  *
169  * \param ro_cw The ro core window structure.
170  * \param action The button bar action.
171  * \return NSERROR_OK if config saved, otherwise apropriate error code
172  */
173 static nserror
hotlist_toolbar_click(struct ro_corewindow * ro_cw,button_bar_action action)174 hotlist_toolbar_click(struct ro_corewindow *ro_cw, button_bar_action action)
175 {
176 	switch (action) {
177 	case TOOLBAR_BUTTON_DELETE:
178 		hotlist_keypress(NS_KEY_DELETE_LEFT);
179 		ro_toolbar_update_all_hotlists();
180 		break;
181 
182 	case TOOLBAR_BUTTON_EXPAND:
183 		hotlist_expand(false);
184 		break;
185 
186 	case TOOLBAR_BUTTON_COLLAPSE:
187 		hotlist_contract(false);
188 		break;
189 
190 	case TOOLBAR_BUTTON_OPEN:
191 		hotlist_expand(true);
192 		break;
193 
194 	case TOOLBAR_BUTTON_CLOSE:
195 		hotlist_contract(true);
196 		break;
197 
198 	case TOOLBAR_BUTTON_LAUNCH:
199 		hotlist_keypress(NS_KEY_CR);
200 		break;
201 
202 	case TOOLBAR_BUTTON_CREATE:
203 		hotlist_add_folder(NULL, false, 0);
204 		break;
205 
206 	default:
207 		break;
208 	}
209 
210 	return NSERROR_OK;
211 }
212 
213 
214 /**
215  * Handle updating state of buttons in ro core window toolbar.
216  *
217  * \param ro_cw The ro core window structure.
218  * \return NSERROR_OK if config saved, otherwise apropriate error code
219  */
hotlist_toolbar_update(struct ro_corewindow * ro_cw)220 static nserror hotlist_toolbar_update(struct ro_corewindow *ro_cw)
221 {
222 	bool has_selection;
223 
224 	has_selection = hotlist_has_selection();
225 
226 	ro_toolbar_set_button_shaded_state(ro_cw->toolbar,
227 					   TOOLBAR_BUTTON_DELETE,
228 					   !has_selection);
229 
230 	ro_toolbar_set_button_shaded_state(ro_cw->toolbar,
231 					   TOOLBAR_BUTTON_LAUNCH,
232 					   !has_selection);
233 
234 	return NSERROR_OK;
235 }
236 
237 
238 /**
239  * callback for saving of toolbar state in ro hotlist window
240  *
241  * \param ro_cw The ro core window structure.
242  * \param config The new toolbar configuration.
243  * \return NSERROR_OK if config saved, otherwise apropriate error code
244  */
hotlist_toolbar_save(struct ro_corewindow * ro_cw,char * config)245 static nserror hotlist_toolbar_save(struct ro_corewindow *ro_cw, char *config)
246 {
247 	nsoption_set_charp(toolbar_hotlist, config);
248 	ro_gui_save_options();
249 
250 	return NSERROR_OK;
251 }
252 
253 
254 /**
255  * Prepare the hotlist menu for display
256  *
257  * \param w The window owning the menu.
258  * \param i The icon owning the menu.
259  * \param menu	The menu from which the selection was made.
260  * \param pointer The pointer shape
261  * \return true if action accepted; else false.
262  */
263 static bool
hotlist_menu_prepare(wimp_w w,wimp_i i,wimp_menu * menu,wimp_pointer * pointer)264 hotlist_menu_prepare(wimp_w w, wimp_i i, wimp_menu *menu, wimp_pointer *pointer)
265 {
266 	bool selection;
267 	struct ro_hotlist_window *hotlistw;
268 
269 	hotlistw = (struct ro_hotlist_window *)ro_gui_wimp_event_get_user_data(w);
270 
271 	if ((hotlistw == NULL) ||
272 	    (menu != hotlistw->menu)) {
273 		return false;
274 	}
275 
276 	selection = hotlist_has_selection();
277 
278 	ro_gui_menu_set_entry_shaded(menu, TREE_SELECTION, !selection);
279 	ro_gui_menu_set_entry_shaded(menu, TREE_CLEAR_SELECTION, !selection);
280 
281 	ro_gui_save_prepare(GUI_SAVE_HOTLIST_EXPORT_HTML,
282 			    NULL, NULL, NULL, NULL);
283 
284 	ro_gui_menu_set_entry_shaded(menu, TOOLBAR_BUTTONS,
285 			ro_toolbar_menu_option_shade(hotlistw->core.toolbar));
286 	ro_gui_menu_set_entry_ticked(menu, TOOLBAR_BUTTONS,
287 			ro_toolbar_menu_buttons_tick(hotlistw->core.toolbar));
288 
289 	ro_gui_menu_set_entry_shaded(menu, TOOLBAR_EDIT,
290 			ro_toolbar_menu_edit_shade(hotlistw->core.toolbar));
291 	ro_gui_menu_set_entry_ticked(menu, TOOLBAR_EDIT,
292 			ro_toolbar_menu_edit_tick(hotlistw->core.toolbar));
293 
294 	return true;
295 }
296 
297 
298 /**
299  * Handle submenu warnings for the hotlist menu
300  *
301  * \param w The window owning the menu.
302  * \param i The icon owning the menu.
303  * \param menu The menu to which the warning applies.
304  * \param selection The wimp menu selection data.
305  * \param action The selected menu action.
306  */
307 static void
hotlist_menu_warning(wimp_w w,wimp_i i,wimp_menu * menu,wimp_selection * selection,menu_action action)308 hotlist_menu_warning(wimp_w w,
309 		     wimp_i i,
310 		     wimp_menu *menu,
311 		     wimp_selection *selection,
312 		     menu_action action)
313 {
314 	/* Do nothing */
315 }
316 
317 
318 /**
319  * Handle selections from the hotlist menu
320  *
321  * \param w The window owning the menu.
322  * \param i The icon owning the menu.
323  * \param menu The menu from which the selection was made.
324  * \param selection The wimp menu selection data.
325  * \param action The selected menu action.
326  * \return true if action accepted; else false.
327  */
328 static bool
hotlist_menu_select(wimp_w w,wimp_i i,wimp_menu * menu,wimp_selection * selection,menu_action action)329 hotlist_menu_select(wimp_w w,
330 		    wimp_i i,
331 		    wimp_menu *menu,
332 		    wimp_selection *selection,
333 		    menu_action action)
334 {
335 	struct ro_hotlist_window *hotlistw;
336 
337 	hotlistw = (struct ro_hotlist_window *)ro_gui_wimp_event_get_user_data(w);
338 
339 	if ((hotlistw == NULL) ||
340 	    (menu != hotlistw->menu)) {
341 		return false;
342 	}
343 
344 	switch (action) {
345 	case HOTLIST_EXPORT:
346 		ro_gui_dialog_open_persistent(w, dialog_saveas, true);
347 		return true;
348 
349 	case TREE_NEW_FOLDER:
350 		hotlist_add_folder(NULL, false, 0);
351 		return true;
352 
353 	case TREE_NEW_LINK:
354 		hotlist_add_entry(NULL, NULL, false, 0);
355 		return true;
356 
357 	case TREE_EXPAND_ALL:
358 		hotlist_expand(false);
359 		return true;
360 
361 	case TREE_EXPAND_FOLDERS:
362 		hotlist_expand(true);
363 		return true;
364 
365 	case TREE_EXPAND_LINKS:
366 		hotlist_expand(false);
367 		return true;
368 
369 	case TREE_COLLAPSE_ALL:
370 		hotlist_contract(true);
371 		return true;
372 
373 	case TREE_COLLAPSE_FOLDERS:
374 		hotlist_contract(true);
375 		return true;
376 
377 	case TREE_COLLAPSE_LINKS:
378 		hotlist_contract(false);
379 		return true;
380 
381 	case TREE_SELECTION_EDIT:
382 		hotlist_edit_selection();
383 		return true;
384 
385 	case TREE_SELECTION_LAUNCH:
386 		hotlist_keypress(NS_KEY_CR);
387 		return true;
388 
389 	case TREE_SELECTION_DELETE:
390 		hotlist_keypress(NS_KEY_DELETE_LEFT);
391 		ro_toolbar_update_all_hotlists();
392 		return true;
393 
394 	case TREE_SELECT_ALL:
395 		hotlist_keypress(NS_KEY_SELECT_ALL);
396 		return true;
397 
398 	case TREE_CLEAR_SELECTION:
399 		hotlist_keypress(NS_KEY_CLEAR_SELECTION);
400 		return true;
401 
402 	case TOOLBAR_BUTTONS:
403 		ro_toolbar_set_display_buttons(hotlistw->core.toolbar,
404 			!ro_toolbar_get_display_buttons(hotlistw->core.toolbar));
405 		return true;
406 
407 	case TOOLBAR_EDIT:
408 		ro_toolbar_toggle_edit(hotlistw->core.toolbar);
409 		return true;
410 
411 	default:
412 		return false;
413 	}
414 
415 	return false;
416 }
417 
418 
419 /**
420  * Creates the window for the hotlist tree.
421  *
422  * \return NSERROR_OK on success else appropriate error code on faliure.
423  */
ro_hotlist_init(void)424 static nserror ro_hotlist_init(void)
425 {
426 	os_error *error;
427 	struct ro_hotlist_window *ncwin;
428 	nserror res;
429 	static const struct ns_menu hotlist_menu_def = {
430 		"Hotlist", {
431 			{ "Hotlist", NO_ACTION, 0 },
432 			{ "Hotlist.New", NO_ACTION, 0 },
433 			{ "Hotlist.New.Folder", TREE_NEW_FOLDER, 0 },
434 			{ "Hotlist.New.Link", TREE_NEW_LINK, 0 },
435 			{ "_Hotlist.Export", HOTLIST_EXPORT, &dialog_saveas },
436 			{ "Hotlist.Expand", TREE_EXPAND_ALL, 0 },
437 			{ "Hotlist.Expand.All", TREE_EXPAND_ALL, 0 },
438 			{ "Hotlist.Expand.Folders", TREE_EXPAND_FOLDERS, 0 },
439 			{ "Hotlist.Expand.Links", TREE_EXPAND_LINKS, 0 },
440 			{ "Hotlist.Collapse", TREE_COLLAPSE_ALL, 0 },
441 			{ "Hotlist.Collapse.All", TREE_COLLAPSE_ALL, 0 },
442 			{ "Hotlist.Collapse.Folders", TREE_COLLAPSE_FOLDERS, 0 },
443 			{ "Hotlist.Collapse.Links", TREE_COLLAPSE_LINKS, 0 },
444 			{ "Hotlist.Toolbars", NO_ACTION, 0 },
445 			{ "_Hotlist.Toolbars.ToolButtons", TOOLBAR_BUTTONS, 0 },
446 			{ "Hotlist.Toolbars.EditToolbar", TOOLBAR_EDIT, 0 },
447 			{ "Selection", TREE_SELECTION, 0 },
448 			{ "Selection.Edit", TREE_SELECTION_EDIT, 0 },
449 			{ "Selection.Launch", TREE_SELECTION_LAUNCH, 0 },
450 			{ "Selection.Delete", TREE_SELECTION_DELETE, 0 },
451 			{ "SelectAll", TREE_SELECT_ALL, 0 },
452 			{ "Clear", TREE_CLEAR_SELECTION, 0 },
453 			{ NULL, 0, 0}
454 		}
455 	};
456 
457 	static const struct button_bar_buttons hotlist_toolbar_buttons[] = {
458 		{ "delete", TOOLBAR_BUTTON_DELETE, TOOLBAR_BUTTON_NONE, '0', "0" },
459 		{ "expand", TOOLBAR_BUTTON_EXPAND, TOOLBAR_BUTTON_COLLAPSE, '1', "1" },
460 		{ "open", TOOLBAR_BUTTON_OPEN, TOOLBAR_BUTTON_CLOSE, '2', "2" },
461 		{ "launch", TOOLBAR_BUTTON_LAUNCH, TOOLBAR_BUTTON_NONE, '3', "3" },
462 		{ "create", TOOLBAR_BUTTON_CREATE, TOOLBAR_BUTTON_NONE, '4', "4" },
463 		{ NULL, TOOLBAR_BUTTON_NONE, TOOLBAR_BUTTON_NONE, '\0', "" }
464 	};
465 
466 	if (hotlist_window != NULL) {
467 		return NSERROR_OK;
468 	}
469 
470 	ncwin = calloc(1, sizeof(*ncwin));
471 	if (ncwin == NULL) {
472 		return NSERROR_NOMEM;
473 	}
474 
475 	/* create window from template */
476 	error = xwimp_create_window(dialog_hotlist_template, &ncwin->core.wh);
477 	if (error) {
478 		NSLOG(netsurf, INFO, "xwimp_create_window: 0x%x: %s",
479 		      error->errnum, error->errmess);
480 		ro_warn_user("WimpError", error->errmess);
481 		free(ncwin);
482 		return NSERROR_NOMEM;
483 	}
484 
485 	ro_gui_set_window_title(ncwin->core.wh, messages_get("Hotlist"));
486 
487 	/* Set up callback handlers */
488 	ncwin->core.draw = hotlist_draw;
489 	ncwin->core.key = hotlist_key;
490 	ncwin->core.mouse = hotlist_mouse;
491 	ncwin->core.toolbar_click = hotlist_toolbar_click;
492 	ncwin->core.toolbar_save = hotlist_toolbar_save;
493 	/* update is not valid untill hotlist manager is initialised */
494 	ncwin->core.toolbar_update = NULL;
495 
496 	/* initialise core window */
497 	res = ro_corewindow_init(&ncwin->core,
498 				 hotlist_toolbar_buttons,
499 				 nsoption_charp(toolbar_hotlist),
500 				 THEME_STYLE_HOTLIST_TOOLBAR,
501 				 "HelpHotToolbar");
502 	if (res != NSERROR_OK) {
503 		free(ncwin);
504 		return res;
505 	}
506 
507 	res = hotlist_manager_init(ncwin->core.cb_table,
508 			   (struct core_window *)ncwin);
509 	if (res != NSERROR_OK) {
510 		free(ncwin);
511 		return res;
512 	}
513 
514 	/* setup toolbar update post hotlist manager initialisation */
515 	ncwin->core.toolbar_update = hotlist_toolbar_update;
516 	hotlist_toolbar_update(&ncwin->core);
517 
518 	/* Build the hotlist window menu. */
519 	ncwin->menu = ro_gui_menu_define_menu(&hotlist_menu_def);
520 
521 	ro_gui_wimp_event_register_menu(ncwin->core.wh,
522 					ncwin->menu, false, false);
523 	ro_gui_wimp_event_register_menu_prepare(ncwin->core.wh,
524 						hotlist_menu_prepare);
525 	ro_gui_wimp_event_register_menu_selection(ncwin->core.wh,
526 						  hotlist_menu_select);
527 	ro_gui_wimp_event_register_menu_warning(ncwin->core.wh,
528 						hotlist_menu_warning);
529 
530 	/* memoise window so it can be re-presented when necessary
531 	 * instead of recreating every time.
532 	 */
533 	hotlist_window = ncwin;
534 
535 	return NSERROR_OK;
536 }
537 
538 
539 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_present(void)540 nserror ro_gui_hotlist_present(void)
541 {
542 	nserror res;
543 
544 	/* deal with external hotlist handler */
545 	if (nsoption_bool(external_hotlists) &&
546 	    (nsoption_charp(external_hotlist_app) != NULL) &&
547 	    (*nsoption_charp(external_hotlist_app) != '\0')) {
548 		char command[2048];
549 		os_error *error;
550 
551 		snprintf(command, sizeof(command), "Filer_Run %s",
552 			 nsoption_charp(external_hotlist_app));
553 		error = xos_cli(command);
554 
555 		if (error == NULL) {
556 			return NSERROR_OK;
557 		}
558 
559 		NSLOG(netsurf, INFO, "xos_cli: 0x%x: %s", error->errnum,
560 		      error->errmess);
561 		ro_warn_user("Failed to launch external hotlist: %s",
562 			     error->errmess);
563 	}
564 
565 	res = ro_hotlist_init();
566 	if (res == NSERROR_OK) {
567 		NSLOG(netsurf, INFO, "Presenting");
568 		ro_gui_dialog_open_top(hotlist_window->core.wh,
569 				       hotlist_window->core.toolbar,
570 				       600, 800);
571 	} else {
572 		NSLOG(netsurf, INFO, "Failed presenting code %d", res);
573 	}
574 
575 	return res;
576 }
577 
578 
579 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_initialise(void)580 void ro_gui_hotlist_initialise(void)
581 {
582 	dialog_hotlist_template = ro_gui_dialog_load_template("tree");
583 }
584 
585 
586 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_finalise(void)587 nserror ro_gui_hotlist_finalise(void)
588 {
589 	nserror res;
590 
591 	if (hotlist_window == NULL) {
592 		return NSERROR_OK;
593 	}
594 
595 	res = hotlist_fini();
596 	if (res == NSERROR_OK) {
597 		res = ro_corewindow_fini(&hotlist_window->core);
598 
599 		free(hotlist_window);
600 		hotlist_window = NULL;
601 	}
602 
603 	return res;
604 }
605 
606 
607 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_check_window(wimp_w wh)608 bool ro_gui_hotlist_check_window(wimp_w wh)
609 {
610 	if ((hotlist_window != NULL) &&
611 	    (hotlist_window->core.wh == wh)) {
612 		return true;
613 	}
614 	return false;
615 }
616 
617 
618 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_check_menu(wimp_menu * menu)619 bool ro_gui_hotlist_check_menu(wimp_menu *menu)
620 {
621 	if ((hotlist_window != NULL) &&
622 	    (hotlist_window->menu == menu)) {
623 		return true;
624 	}
625 	return false;
626 }
627 
628 
629 /**
630  * Callback to schedule for the next available Null poll, by which point
631  * a hotlist client will have claimed the Message_HotlistAddURL and any
632  * details in RMA can safely be discarded.
633  *
634  * \param p Unused data pointer.
635  */
ro_gui_hotlist_scheduled_callback(void * p)636 static void ro_gui_hotlist_scheduled_callback(void *p)
637 {
638 	ro_gui_hotlist_add_cleanup();
639 }
640 
641 
642 /**
643  * Handle bounced Message_HotlistAddURL, so that RMA storage can be freed.
644  *
645  * \param message The bounced message content.
646  */
ro_gui_hotlist_addurl_bounce(wimp_message * message)647 static void ro_gui_hotlist_addurl_bounce(wimp_message *message)
648 {
649 	if (hotlist_url != NULL) {
650 		nsurl *nsurl;
651 
652 		if (nsurl_create(hotlist_url, &nsurl) != NSERROR_OK)
653 			return;
654 
655 		hotlist_add_url(nsurl);
656 		nsurl_unref(nsurl);
657 	}
658 
659 	ro_gui_hotlist_add_cleanup();
660 
661 	/* There's no longer any need to listen for the next Null poll. */
662 
663 	riscos_schedule(-1, ro_gui_hotlist_scheduled_callback, NULL);
664 }
665 
666 
667 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_add_page(nsurl * url)668 void ro_gui_hotlist_add_page(nsurl *url)
669 {
670 	const struct url_data *data;
671 	wimp_message message;
672 	struct ro_hotlist_message_hotlist_addurl *add_url =
673 		(struct ro_hotlist_message_hotlist_addurl *) &message;
674 
675 	if (url == NULL) {
676 		return;
677 	}
678 
679 	/* If we're not using external hotlists, add the page to NetSurf's
680 	 * own hotlist and return...
681 	 */
682 	if (!nsoption_bool(external_hotlists)) {
683 		hotlist_add_url(url);
684 		return;
685 	}
686 
687 	/* ...otherwise try broadcasting the details to any other
688 	 * interested parties.  If no-one answers, we'll fall back to
689 	 * NetSurf's hotlist anyway when the message bounces.
690 	 */
691 
692 	ro_gui_hotlist_add_cleanup();
693 
694 	data = urldb_get_url_data(url);
695 	if (data == NULL)
696 		return;
697 
698 	hotlist_url = osmodule_alloc(nsurl_length(url) + 1);
699 	hotlist_title = osmodule_alloc(strlen(data->title) + 1);
700 
701 	if (hotlist_url == NULL || hotlist_title == NULL) {
702 		ro_gui_hotlist_add_cleanup();
703 		return;
704 	}
705 
706 	strcpy(hotlist_url, nsurl_access(url));
707 	strcpy(hotlist_title, data->title);
708 
709 	add_url->size = 60;
710 	add_url->your_ref = 0;
711 	add_url->action = message_HOTLIST_ADD_URL;
712 	add_url->url = hotlist_url;
713 	add_url->title = hotlist_title;
714 	strcpy(add_url->appname, "NetSurf");
715 
716 	if (!ro_message_send_message(wimp_USER_MESSAGE_RECORDED,
717 				     &message,
718 				     0,
719 				     ro_gui_hotlist_addurl_bounce)) {
720 		ro_gui_hotlist_add_cleanup();
721 	}
722 
723 	/* Listen for the next Null poll, as an indication that the
724 	 * message didn't bounce.
725 	 */
726 	riscos_schedule(0, ro_gui_hotlist_scheduled_callback, NULL);
727 }
728 
729 
730 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_add_cleanup(void)731 void ro_gui_hotlist_add_cleanup(void)
732 {
733 	if (hotlist_url != NULL) {
734 		osmodule_free(hotlist_url);
735 		hotlist_url = NULL;
736 	}
737 
738 	if (hotlist_title != NULL) {
739 		osmodule_free(hotlist_title);
740 		hotlist_title = NULL;
741 	}
742 }
743 
744 
745 /**
746  * Callback confirming a URL delete query.
747  *
748  * \param id The ID of the query calling us.
749  * \param res The user's response to the query.
750  * \param p Callback data (always NULL).
751  */
752 static void
ro_gui_hotlist_remove_confirmed(query_id id,enum query_response res,void * p)753 ro_gui_hotlist_remove_confirmed(query_id id, enum query_response res, void *p)
754 {
755 	hotlist_remove_url(hotlist_delete_url);
756 	ro_toolbar_update_all_hotlists();
757 
758 	nsurl_unref(hotlist_delete_url);
759 	hotlist_delete_url = NULL;
760 	hotlist_query = QUERY_INVALID;
761 }
762 
763 
764 /**
765  * Callback cancelling a URL delete query.
766  *
767  * \param id The ID of the query calling us.
768  * \param res The user's response to the query.
769  * \param p Callback data (always NULL).
770  */
771 static void
ro_gui_hotlist_remove_cancelled(query_id id,enum query_response res,void * p)772 ro_gui_hotlist_remove_cancelled(query_id id, enum query_response res, void *p)
773 {
774 	nsurl_unref(hotlist_delete_url);
775 	hotlist_delete_url = NULL;
776 	hotlist_query = QUERY_INVALID;
777 }
778 
779 
780 /**
781  * removal query dialog callbacks.
782  */
783 static const query_callback remove_funcs = {
784 	ro_gui_hotlist_remove_confirmed,
785 	ro_gui_hotlist_remove_cancelled
786 };
787 
788 
789 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_remove_page(nsurl * url)790 void ro_gui_hotlist_remove_page(nsurl *url)
791 {
792 	if ((url == NULL) ||
793 	    nsoption_bool(external_hotlists) ||
794 	    !hotlist_has_url(url)) {
795 		return;
796 	}
797 
798 	/* Clean up any existing delete attempts before continuing. */
799 	if (hotlist_query != QUERY_INVALID) {
800 		query_close(hotlist_query);
801 		hotlist_query = QUERY_INVALID;
802 	}
803 
804 	if (hotlist_delete_url != NULL) {
805 		nsurl_unref(hotlist_delete_url);
806 		hotlist_delete_url = NULL;
807 	}
808 
809 	/* Check with the user before removing the URL, unless they don't
810 	 * want us to be careful in which case just do it.
811 	 */
812 	if (nsoption_bool(confirm_hotlist_remove)) {
813 		hotlist_query = query_user("RemoveHotlist", NULL,
814 					   &remove_funcs, NULL,
815 					   messages_get("Remove"),
816 					   messages_get("DontRemove"));
817 
818 		hotlist_delete_url = nsurl_ref(url);
819 	} else {
820 		hotlist_remove_url(url);
821 		ro_toolbar_update_all_hotlists();
822 	}
823 }
824 
825 
826 /* exported interface documented in riscos/hotlist.h */
ro_gui_hotlist_has_page(nsurl * url)827 bool ro_gui_hotlist_has_page(nsurl *url)
828 {
829 	if ((url == NULL) ||
830 	    nsoption_bool(external_hotlists)) {
831 		return false;
832 	}
833 
834 	return hotlist_has_url(url);
835 }
836