1 /*
2  * Copyright 2013 Ole Loots <ole@monochrom.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 #include <ctype.h>
20 #include <string.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 
26 #include "utils/log.h"
27 #include "utils/messages.h"
28 #include "utils/nsoption.h"
29 #include "utils/nsurl.h"
30 #include "netsurf/inttypes.h"
31 #include "netsurf/keypress.h"
32 #include "content/content.h"
33 #include "desktop/hotlist.h"
34 
35 #include "atari/gui.h"
36 #include "atari/misc.h"
37 #include "atari/treeview.h"
38 #include "atari/hotlist.h"
39 #include "atari/findfile.h"
40 #include "atari/gemtk/gemtk.h"
41 #include "atari/res/netsurf.rsh"
42 
43 extern GRECT desk_area;
44 
45 const char *tree_hotlist_path;
46 
47 struct atari_hotlist hl;
48 
49 /* Setup Atari Treeview Callbacks: */
50 static nserror atari_hotlist_init_phase2(struct core_window *cw,
51 					 struct core_window_callback_table * default_callbacks);
52 static void atari_hotlist_finish(struct core_window *cw);
53 static void atari_hotlist_keypress(struct core_window *cw,
54 				   uint32_t ucs4);
55 static void atari_hotlist_mouse_action(struct core_window *cw,
56 				       browser_mouse_state mouse,
57 				       int x, int y);
58 static void atari_hotlist_draw(struct core_window *cw, int x,
59 			       int y, struct rect *clip,
60 			       const struct redraw_context *ctx);
61 static short handle_event(GUIWIN *win, EVMULT_OUT *ev_out, short msg[8]);
62 
63 static struct atari_treeview_callbacks atari_hotlist_treeview_callbacks = {
64 	.init_phase2 = atari_hotlist_init_phase2,
65 	.finish = atari_hotlist_finish,
66 	.draw = atari_hotlist_draw,
67 	.keypress = atari_hotlist_keypress,
68 	.mouse_action = atari_hotlist_mouse_action,
69 	.gemtk_user_func = handle_event
70 };
71 
atari_hotlist_init_phase2(struct core_window * cw,struct core_window_callback_table * cb_t)72 static nserror atari_hotlist_init_phase2(struct core_window *cw,
73 					 struct core_window_callback_table *cb_t)
74 {
75 	NSLOG(netsurf, INFO, "cw:%p", cw);
76 	return hotlist_manager_init(cb_t, cw);
77 }
78 
atari_hotlist_finish(struct core_window * cw)79 static void atari_hotlist_finish(struct core_window *cw)
80 {
81 	NSLOG(netsurf, INFO, "cw:%p", cw);
82 	hotlist_fini();
83 }
84 
atari_hotlist_draw(struct core_window * cw,int x,int y,struct rect * clip,const struct redraw_context * ctx)85 static void atari_hotlist_draw(struct core_window *cw, int x,
86 			       int y, struct rect *clip,
87 			       const struct redraw_context *ctx)
88 {
89 	hotlist_redraw(x, y, clip, ctx);
90 }
91 
atari_hotlist_keypress(struct core_window * cw,uint32_t ucs4)92 static void atari_hotlist_keypress(struct core_window *cw, uint32_t ucs4)
93 {
94 	//GUIWIN *gemtk_win;
95 	//GRECT area;
96 	NSLOG(netsurf, INFO, "ucs4: %"PRIu32, ucs4);
97 	hotlist_keypress(ucs4);
98 	//gemtk_win = atari_treeview_get_gemtk_window(cw);
99 	//atari_treeview_get_grect(cw, TREEVIEW_AREA_CONTENT, &area);
100 	//gemtk_wm_exec_redraw(gemtk_win, &area);
101 }
102 
atari_hotlist_mouse_action(struct core_window * cw,browser_mouse_state mouse,int x,int y)103 static void atari_hotlist_mouse_action(struct core_window *cw,
104 				       browser_mouse_state mouse,
105 				       int x, int y)
106 {
107 	NSLOG(netsurf, INFO, "x:  %d, y: %d\n", x, y);
108 
109 	hotlist_mouse_action(mouse, x, y);
110 }
111 
112 
113 
handle_event(GUIWIN * win,EVMULT_OUT * ev_out,short msg[8])114 static short handle_event(GUIWIN *win, EVMULT_OUT *ev_out, short msg[8])
115 {
116 	char *cur_url = NULL;
117 	char *cur_title = NULL;
118 	short retval = 0;
119 	struct atari_treeview_window *tv = NULL;
120 	struct core_window *cw;
121 	struct gui_window * gw;
122 	OBJECT *toolbar;
123 	GRECT tb_area;
124 	GUIWIN * gemtk_win;
125 
126 	NSLOG(netsurf, INFO, "gw:%p", win);
127 
128 	tv = (struct atari_treeview_window*) gemtk_wm_get_user_data(win);
129 	cw = (struct core_window *)tv;
130 
131 	if (ev_out->emo_events & MU_MESAG) {
132 		switch (msg[0]) {
133 
134 		case WM_TOOLBAR:
135 			NSLOG(netsurf, INFO, "WM_TOOLBAR");
136 
137 			toolbar = gemtk_obj_get_tree(TOOLBAR_HOTLIST);
138 
139 			assert(toolbar);
140 			assert(tv);
141 
142 			switch	(msg[4]) {
143 			case TOOLBAR_HOTLIST_CREATE_FOLDER:
144 				hotlist_add_folder(NULL, 0, 0);
145 				break;
146 
147 			case TOOLBAR_HOTLIST_ADD:
148 				gw = gui_get_input_window();
149 				if(gw && gw->browser){
150 					cur_url = gui_window_get_url(gw);
151 					cur_title = gui_window_get_title(gw);
152 					// TODO: read language string.
153 					cur_title = (cur_title ? cur_title : (char*)"New bookmark");
154 				} else {
155 					cur_url = (char*)"http://www";
156 				}
157 				atari_hotlist_add_page(cur_url, cur_title);
158 				break;
159 
160 			case TOOLBAR_HOTLIST_DELETE:
161 				hotlist_keypress(NS_KEY_DELETE_LEFT);
162 				break;
163 
164 			case TOOLBAR_HOTLIST_EDIT:
165 				hotlist_edit_selection();
166 				break;
167 			}
168 
169 			gemtk_win = atari_treeview_get_gemtk_window(cw);
170 			assert(gemtk_win);
171 			toolbar[msg[4]].ob_state &= ~OS_SELECTED;
172 			atari_treeview_get_grect(cw, TREEVIEW_AREA_TOOLBAR, &tb_area);
173 			evnt_timer(150);
174 			gemtk_wm_exec_redraw(gemtk_win, &tb_area);
175 			retval = 1;
176 			break;
177 
178 		case WM_CLOSED:
179 			atari_hotlist_close();
180 			retval = 1;
181 			break;
182 
183 		default: break;
184 		}
185 	}
186 
187 	return(retval);
188 }
189 
190 
191 
atari_hotlist_init(void)192 void atari_hotlist_init(void)
193 {
194 	if (hl.init == false) {
195 		if( strcmp(nsoption_charp(hotlist_file), "") == 0 ){
196 			atari_find_resource( (char*)&hl.path, "hotlist", "hotlist" );
197 		} else {
198 			strncpy( (char*)&hl.path, nsoption_charp(hotlist_file), PATH_MAX-1 );
199 		}
200 
201 		NSLOG(netsurf, INFO, "Hotlist: %s", (char *)&hl.path);
202 		hotlist_init(hl.path, hl.path);
203 
204 		if( hl.window == NULL ){
205 			int flags = ATARI_TREEVIEW_WIDGETS;
206 			short handle = -1;
207 			OBJECT * tree = gemtk_obj_get_tree(TOOLBAR_HOTLIST);
208 			assert( tree );
209 
210 			handle = wind_create(flags, 0, 0, desk_area.g_w, desk_area.g_h);
211 			hl.window = gemtk_wm_add(handle, GEMTK_WM_FLAG_DEFAULTS, NULL);
212 			if( hl.window == NULL ) {
213 				gemtk_msg_box_show(GEMTK_MSG_BOX_ALERT,
214 						   "Failed to allocate Hotlist");
215 				return;
216 			}
217 			wind_set_str(handle, WF_NAME, (char*)messages_get("Hotlist"));
218 			gemtk_wm_set_toolbar(hl.window, tree, 0, 0);
219 			gemtk_wm_unlink(hl.window);
220 			tree_hotlist_path = (const char*)&hl.path;
221 
222 			hl.tv = atari_treeview_create(hl.window, &atari_hotlist_treeview_callbacks,
223 						      NULL, flags);
224 
225 			if (hl.tv == NULL) {
226 				/* handle it properly, clean up previous allocs */
227 				NSLOG(netsurf, INFO,
228 				      "Failed to allocate treeview");
229 				return;
230 			}
231 
232 		} else {
233 
234 		}
235 	}
236 	hl.init = true;
237 }
238 
atari_hotlist_open(void)239 void atari_hotlist_open(void)
240 {
241 	assert(hl.init);
242 	if (hl.init == false) {
243 		return;
244 	}
245 
246 	if (atari_treeview_is_open(hl.tv) == false) {
247 
248 		GRECT pos;
249 		pos.g_x = desk_area.g_w - desk_area.g_w / 4;
250 		pos.g_y = desk_area.g_y;
251 		pos.g_w = desk_area.g_w / 4;
252 		pos.g_h = desk_area.g_h;
253 
254 		atari_treeview_open(hl.tv, &pos);
255 	} else {
256 		wind_set(gemtk_wm_get_handle(hl.window), WF_TOP, 1, 0, 0, 0);
257 	}
258 }
259 
atari_hotlist_close(void)260 void atari_hotlist_close(void)
261 {
262 	atari_treeview_close(hl.tv);
263 }
264 
atari_hotlist_destroy(void)265 void atari_hotlist_destroy(void)
266 {
267 
268 	if( hl.init == false) {
269 		return;
270 	}
271 	if( hl.window != NULL ) {
272 		if (atari_treeview_is_open(hl.tv))
273 			atari_hotlist_close();
274 		wind_delete(gemtk_wm_get_handle(hl.window));
275 		gemtk_wm_remove(hl.window);
276 		hl.window = NULL;
277 		atari_treeview_delete(hl.tv);
278 		hl.init = false;
279 	}
280 	NSLOG(netsurf, INFO, "done");
281 }
282 
atari_hotlist_redraw(void)283 void atari_hotlist_redraw(void)
284 {
285 	atari_treeview_redraw(hl.tv);
286 }
287 
288 struct node;
289 
atari_hotlist_add_page(const char * url,const char * title)290 void atari_hotlist_add_page( const char * url, const char * title )
291 {
292 	nsurl *nsurl;
293 
294 	if(hl.tv == NULL)
295 		return;
296 
297 	atari_hotlist_open();
298 
299 	if (nsurl_create(url, &nsurl) != NSERROR_OK)
300 		return;
301 
302 	if (hotlist_has_url(nsurl)) {
303 		NSLOG(netsurf, INFO, "URL already added as Bookmark");
304 		nsurl_unref(nsurl);
305 		return;
306 	}
307 
308 	/* doesn't look nice:
309 	   if( hl.tv->click.x >= 0 && hl.tv->click.y >= 0 ){
310 	   hotlist_add_entry( nsurl, title, true, hl.tv->click.y );
311 	   } else {
312 
313 	   }*/
314 	//hotlist_add_url(nsurl);
315 	hotlist_add_entry(nsurl, title, 0, 0);
316 	nsurl_unref(nsurl);
317 }
318