summaryrefslogtreecommitdiff
path: root/atari/hotlist.c
blob: 016cbc458ab2994193e002c98e2d983cf120eaff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright 2013 Ole Loots <ole@monochrom.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "desktop/browser.h"
#include "content/content.h"
#include "content/hlcache.h"
#include "content/urldb.h"
#include "utils/nsoption.h"
#include "desktop/hotlist.h"
#include "desktop/tree.h"
#include "desktop/gui.h"
#include "desktop/core_window.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/url.h"
#include "atari/gui.h"
#include "atari/misc.h"
#include "atari/treeview.h"
#include "atari/hotlist.h"
#include "atari/findfile.h"
#include "atari/gemtk/gemtk.h"
#include "atari/res/netsurf.rsh"

extern GRECT desk_area;

struct atari_hotlist hl;

/* Setup Atari Treeview Callbacks: */
static nserror atari_hotlist_init_phase2(struct core_window *cw,
				struct core_window_callback_table * default_callbacks);
static void atari_hotlist_finish(struct core_window *cw);
static void atari_hotlist_keypress(struct core_window *cw,
												uint32_t ucs4);
static void atari_hotlist_mouse_action(struct core_window *cw,
												browser_mouse_state mouse,
												int x, int y);
static void atari_hotlist_draw(struct core_window *cw, int x,
											int y, struct rect *clip,
											const struct redraw_context *ctx);
static short handle_event(GUIWIN *win, EVMULT_OUT *ev_out, short msg[8]);

static struct atari_treeview_callbacks atari_hotlist_treeview_callbacks = {
	.init_phase2 = atari_hotlist_init_phase2,
	.finish = atari_hotlist_finish,
	.draw = atari_hotlist_draw,
	.keypress = atari_hotlist_keypress,
	.mouse_action = atari_hotlist_mouse_action,
	.gemtk_user_func = handle_event
};

static nserror atari_hotlist_init_phase2(struct core_window *cw,
								struct core_window_callback_table *cb_t)
{
	LOG((""));
	return(hotlist_init(cb_t, cw, hl.path));
}

static void atari_hotlist_finish(struct core_window *cw)
{
	LOG((""));
	hotlist_fini(hl.path);
}

static void atari_hotlist_draw(struct core_window *cw, int x,
											int y, struct rect *clip,
											const struct redraw_context *ctx)
{
	hotlist_redraw(x, y, clip, ctx);
}

static void atari_hotlist_keypress(struct core_window *cw, uint32_t ucs4)
{
	LOG(("ucs4: %lu\n", ucs4));
	hotlist_keypress(ucs4);
}

static void atari_hotlist_mouse_action(struct core_window *cw,
												browser_mouse_state mouse,
												int x, int y)
{
	LOG(("x:  %d, y: %d\n", x, y));
	if((mouse & BROWSER_MOUSE_HOVER) && hotlist_has_selection()){
		hotlist_mouse_action(mouse, x, y);
	} else {
		hotlist_mouse_action(mouse, x, y);
	}

}



static short handle_event(GUIWIN *win, EVMULT_OUT *ev_out, short msg[8])
{
	struct atari_treeview_window *tv=NULL;
	GRECT tb_area;
	GUIWIN * gemtk_win;
	struct gui_window * gw;
	char *cur_url = NULL;
	char *cur_title = NULL;

	LOG((""));

	if(ev_out->emo_events & MU_MESAG){
		switch (msg[0]) {

			case WM_TOOLBAR:
				LOG(("WM_TOOLBAR"));
				tv = (struct atari_treeview_window*) gemtk_wm_get_user_data(win);
				assert(tv);
				switch	(msg[4]) {
					case TOOLBAR_HOTLIST_CREATE_FOLDER:
						hotlist_add_folder(NULL, 0, 0);
						break;

					case TOOLBAR_HOTLIST_ADD:
						gw = gui_window_get_input_window();
						if(gw && gw->browser){
							cur_url = gui_window_get_url(gw);
							cur_title = gui_window_get_title(gw);
							// TODO: read language string.
							cur_title = (cur_title ? cur_title : "New bookmark");
						} else {
							cur_url = "http://www";
						}
						atari_hotlist_add_page(cur_url, cur_title);
						break;

					case TOOLBAR_HOTLIST_DELETE:
						hotlist_keypress(KEY_DELETE_LEFT);
						// TODO: check if redraw is really required,
						// 		  - implement treeview getter for the gemtk
						//          handle.
						break;

					case TOOLBAR_HOTLIST_EDIT:
						hotlist_edit_selection();
						break;
				}

				gemtk_win = atari_treeview_get_gemtk_window(tv);
				assert(gemtk_win);
				gemtk_obj_get_tree(TOOLBAR_HOTLIST)[msg[4]].ob_state &= ~OS_SELECTED;
				atari_treeview_get_grect(tv, TREEVIEW_AREA_TOOLBAR, &tb_area);
				evnt_timer(150);
				gemtk_wm_exec_redraw(gemtk_win, &tb_area);
			break;

			case WM_CLOSED:
				atari_hotlist_close();
			break;

			default: break;
		}
	}

	// TODO: implement selectable objects in toolbar API:
	// ObjcChange( OC_TOOLBAR, win, buff[4], ~SELECTED, OC_MSG );
}



void atari_hotlist_init(void)
{
	if (hl.init == false) {
		if( strcmp(nsoption_charp(hotlist_file), "") == 0 ){
			atari_find_resource( (char*)&hl.path, "hotlist", "hotlist" );
		} else {
			strncpy( (char*)&hl.path, nsoption_charp(hotlist_file), PATH_MAX-1 );
		}

		LOG(("Hotlist: %s",  (char*)&hl.path ));

		if( hl.window == NULL ){
			int flags = ATARI_TREEVIEW_WIDGETS;
			short handle = -1;
			GRECT desk;
			OBJECT * tree = gemtk_obj_get_tree(TOOLBAR_HOTLIST);
			assert( tree );

			handle = wind_create(flags, 0, 0, desk_area.g_w, desk_area.g_h);
			hl.window = gemtk_wm_add(handle, GEMTK_WM_FLAG_DEFAULTS, NULL);
			if( hl.window == NULL ) {
				gemtk_msg_box_show(GEMTK_MSG_BOX_ALERT,
									"Failed to allocate Hotlist");
				return;
			}
			wind_set_str(handle, WF_NAME, (char*)messages_get("Hotlist"));
			gemtk_wm_set_toolbar(hl.window, tree, 0, 0);
			gemtk_wm_unlink(hl.window);
			tree_hotlist_path = (const char*)&hl.path;

			hl.tv = atari_treeview_create(hl.window, &atari_hotlist_treeview_callbacks,
									flags);

			if (hl.tv == NULL) {
				/* handle it properly, clean up previous allocs */
				LOG(("Failed to allocate treeview"));
				return;
			}

		} else {

		}
	}
	hl.init = true;
}

void atari_hotlist_open(void)
{
	assert(hl.init);
	if (hl.init == false) {
		return;
	}

	if (atari_treeview_is_open(hl.tv) == false) {

	    GRECT pos;
	    pos.g_x = desk_area.g_w - desk_area.g_w / 4;
	    pos.g_y = desk_area.g_y;
	    pos.g_w = desk_area.g_w / 4;
	    pos.g_h = desk_area.g_h;

		atari_treeview_open(hl.tv, &pos);
	} else {
		wind_set(gemtk_wm_get_handle(hl.window), WF_TOP, 1, 0, 0, 0);
	}
}

void atari_hotlist_close(void)
{
	atari_treeview_close(hl.tv);
}

void atari_hotlist_destroy(void)
{

	if( hl.init == false) {
		return;
	}
	if( hl.window != NULL ) {
		if (atari_treeview_is_open(hl.tv))
			atari_hotlist_close();
		wind_delete(gemtk_wm_get_handle(hl.window));
		gemtk_wm_remove(hl.window);
		hl.window = NULL;
		atari_treeview_delete(hl.tv);
		hl.init = false;
	}
	LOG(("done"));
}

void atari_hotlist_redraw(void)
{
	atari_treeview_redraw(hl.tv);
}

struct node;

void atari_hotlist_add_page( const char * url, const char * title )
{
	struct node * root;
	struct node * selected = NULL;
	struct node * folder = NULL;
	nsurl *nsurl;
	ATARI_TREEVIEW_PTR tv = hl.tv;
	if(hl.tv == NULL )
		return;

	atari_hotlist_open();

	if (nsurl_create(url, &nsurl) != NSERROR_OK)
		return;

	/* doesn't look nice:
	if( hl.tv->click.x >= 0 && hl.tv->click.y >= 0 ){
		hotlist_add_entry( nsurl, title, true, hl.tv->click.y );
	} else {

	}*/
	//hotlist_add_url(nsurl);
	hotlist_add_entry(nsurl, title, 0, 0);
	nsurl_unref(nsurl);
}