summaryrefslogtreecommitdiff
path: root/riscos/global_history.c
blob: f0940b3b5173c98c1c30ffc22b4768c9f9678790 (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
309
310
311
312
313
314
315
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *		  http://www.opensource.org/licenses/gpl-license
 * Copyright 2005 Richard Wilson <info@tinct.net>
 */

/** \file
 * Global history (implementation).
 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "oslib/wimp.h"
#include "oslib/wimpspriteop.h"
#include "netsurf/content/url_store.h"
#include "netsurf/desktop/tree.h"
#include "netsurf/riscos/global_history.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/theme.h"
#include "netsurf/riscos/treeview.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"

#define MAXIMUM_URL_LENGTH 1024

#define GLOBAL_HISTORY_RECENT_READ "Choices:WWW.NetSurf.Recent"
#define GLOBAL_HISTORY_RECENT_WRITE "<Choices$Write>.WWW.NetSurf.Recent"
#define GLOBAL_HISTORY_READ "Choices:WWW.NetSurf.History"
#define GLOBAL_HISTORY_WRITE "<Choices$Write>.WWW.NetSurf.History"


static char *global_history_recent_url[GLOBAL_HISTORY_RECENT_URLS];
static int global_history_recent_count = 0;

static void ro_gui_global_history_add(char *title, char *url, int visit_date);

/*	A basic window for the history
*/
static wimp_window history_window_definition = {
	{0, 0, 600, 800},
	0,
	0,
	wimp_TOP,
	wimp_WINDOW_NEW_FORMAT | wimp_WINDOW_MOVEABLE | wimp_WINDOW_BACK_ICON |
			wimp_WINDOW_CLOSE_ICON | wimp_WINDOW_TITLE_ICON |
			wimp_WINDOW_TOGGLE_ICON | wimp_WINDOW_SIZE_ICON |
			wimp_WINDOW_VSCROLL | wimp_WINDOW_IGNORE_XEXTENT |
			wimp_WINDOW_IGNORE_YEXTENT,
	wimp_COLOUR_BLACK,
	wimp_COLOUR_LIGHT_GREY,
	wimp_COLOUR_LIGHT_GREY,
	wimp_COLOUR_WHITE,
	wimp_COLOUR_DARK_GREY,
	wimp_COLOUR_MID_LIGHT_GREY,
	wimp_COLOUR_CREAM,
	0,
	{0, -16384, 16384, 0},
	wimp_ICON_TEXT | wimp_ICON_INDIRECTED | wimp_ICON_HCENTRED |
			wimp_ICON_VCENTRED,
	wimp_BUTTON_DOUBLE_CLICK_DRAG << wimp_ICON_BUTTON_TYPE_SHIFT,
	wimpspriteop_AREA,
	1,
	100,
	{""},
	0,
	{}
};


/*	The history window, toolbar and plot origins
*/
static wimp_w global_history_window;
struct toolbar *global_history_toolbar;
struct tree *global_history_tree;

void ro_gui_global_history_initialise(void) {
	char s[MAXIMUM_URL_LENGTH];
	FILE *fp;
  	const char *title;
	os_error *error;

	/*	Create our window
	*/
	title = messages_get("GlobalHistory");
	history_window_definition.title_data.indirected_text.text = strdup(title);
	history_window_definition.title_data.indirected_text.validation =
			(char *) -1;
	history_window_definition.title_data.indirected_text.size = strlen(title);
	error = xwimp_create_window(&history_window_definition, &global_history_window);
	if (error) {
		LOG(("xwimp_create_window: 0x%x: %s",
				error->errnum, error->errmess));
		die(error->errmess);
	}
	
	/*	Create an empty tree
	*/
	global_history_tree = calloc(sizeof(struct tree), 1);
	if (!global_history_tree) {
	 	warn_user("NoMemory", 0);
		return;
	}
	global_history_tree->root = tree_create_folder_node(NULL, "Root");
	if (!global_history_tree->root) {
	  	warn_user("NoMemory", 0);
	  	free(global_history_tree);
	  	global_history_tree = NULL;
	}
	global_history_tree->root->expanded = true;
	tree_initialise(global_history_tree);


	if (!global_history_tree) return;
	global_history_tree->handle = (int)global_history_window;

	/*	Create our toolbar
	*/
	global_history_toolbar = ro_gui_theme_create_toolbar(NULL,
			THEME_HISTORY_TOOLBAR);
	if (global_history_toolbar) {
		ro_gui_theme_attach_toolbar(global_history_toolbar,
				global_history_window);
		global_history_tree->offset_y = global_history_toolbar->height;
        }
        
        /* load recent URLs */
	fp = fopen(GLOBAL_HISTORY_RECENT_READ, "r");
	if (!fp)
		LOG(("Failed to open file '%s' for reading",
				GLOBAL_HISTORY_RECENT_READ));
	else {
		while (fgets(s, MAXIMUM_URL_LENGTH, fp)) {
		  	if (s[strlen(s) - 1] == '\n')
		  		s[strlen(s) - 1] = '\0';
			global_history_add_recent(s);
		}
		fclose(fp);
	}
}


/**
 * Saves the global history and recent URL data.
 */
void ro_gui_global_history_save(void) {
	FILE *fp;
	int i;

	/* save recent URLs */
	fp = fopen(GLOBAL_HISTORY_RECENT_WRITE, "w");
	if (!fp)
		LOG(("Failed to open file '%s' for writing",
				GLOBAL_HISTORY_RECENT_WRITE));
	else {
		for (i = global_history_recent_count - 1; i >= 0; i--)
		  	if (strlen(global_history_recent_url[i]) < MAXIMUM_URL_LENGTH)
				fprintf(fp, "%s\n", global_history_recent_url[i]);
		fclose(fp);
	}

	/* save global history tree */

}


/**
 * Shows the history window.
 */
void ro_gui_global_history_show(void) {
  	ro_gui_tree_show(global_history_tree, global_history_toolbar);
	ro_gui_menu_prepare_global_history();
}


/**
 * Respond to a mouse click
 *
 * \param pointer  the pointer state
 */
void ro_gui_global_history_click(wimp_pointer *pointer) {
	ro_gui_tree_click(pointer, global_history_tree);
	if (pointer->buttons == wimp_CLICK_MENU)
		ro_gui_create_menu(global_history_menu, pointer->pos.x,
				pointer->pos.y, NULL);
	else
		ro_gui_menu_prepare_global_history();
}


/**
 * Respond to a keypress
 *
 * \param key  the key pressed
 */
bool ro_gui_global_history_keypress(int key) {
  	bool result = ro_gui_tree_keypress(key, global_history_tree);
	ro_gui_menu_prepare_global_history();
	return result;
}


/**
 * Handles a menu closed event
 */
void ro_gui_global_history_menu_closed(void) {
	ro_gui_tree_menu_closed(global_history_tree);
	current_menu = NULL;
	ro_gui_menu_prepare_global_history();
}


/**
 * Attempts to process an interactive help message request
 *
 * \param x  the x co-ordinate to give help for
 * \param y  the x co-ordinate to give help for
 * \return the message code index
 */
int ro_gui_global_history_help(int x, int y) {
	return -1;
}


/**
 * Adds to the global history
 *
 * \param g  the gui_window to add to the global history
 */
void global_history_add(struct gui_window *g) {
	assert(g);
  	
  	if ((!g->bw->current_content) || (!global_history_tree))
  		return;
  	
  	ro_gui_global_history_add(g->title, g->bw->current_content->url, time(NULL));
}


/**
 * Adds to the global history
 *
 * \param title       the page title
 * \param url         the page URL
 * \param visit_date  the visit date
 */
void ro_gui_global_history_add(char *title, char *url, int visit_date) {
	LOG(("Added '%s' ('%s') dated %i.", title, url, visit_date));
  
}


/**
 * Adds a URL to the recently used list
 *
 * \param url  the URL to add
 */
void global_history_add_recent(const char *url) {
	struct url_content *data;
	int i;
	int j = -1;
	char *current;
	
	/* by using the url_store, we get a central char* of the string that isn't
	 * going anywhere unless we tell it to */
	data = url_store_find(url);
	if (!data)
		return;
	
	/* try to find a string already there */
	for (i = 0; i < global_history_recent_count; i++)
		if (global_history_recent_url[i] == data->url)
			j = i;
	
	/* already at head of list */
	if (j == 0)
		return;
	
	/* add to head of list */
	if (j < 0) {
		memmove(&global_history_recent_url[1],
				&global_history_recent_url[0],
				(GLOBAL_HISTORY_RECENT_URLS - 1) * sizeof(char *));
		global_history_recent_url[0] = data->url;
		global_history_recent_count++;
		if (global_history_recent_count > GLOBAL_HISTORY_RECENT_URLS)
			global_history_recent_count = GLOBAL_HISTORY_RECENT_URLS;
		if (global_history_recent_count == 1)
			ro_gui_window_prepare_navigate_all();
	} else {
		/* move to head of list */
		current = global_history_recent_url[j];
		for (i = j; i > 0; i--)
			global_history_recent_url[i] =
				global_history_recent_url[i - 1];
		global_history_recent_url[0] = current;
	}
}


/**
 * Gets details of the currently used URL list.
 *
 * \param count  set to the current number of entries in the URL array on exit
 * \return the current URL array
 */
char **global_history_get_recent(int *count) {
	*count = global_history_recent_count;
	return global_history_recent_url;
}