summaryrefslogtreecommitdiff
path: root/desktop/history_global_core.c
blob: 3222dc7b89be317c29154b3622714f06934545f9 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright 2005 Richard Wilson <info@tinct.net>
 * Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.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 <stdlib.h>

#include "content/content.h"
#include "content/hlcache.h"
#include "content/urldb.h"
#include "desktop/browser.h"
#include "desktop/history_global_core.h"
#include "desktop/plotters.h"
#include "desktop/tree.h"
#include "desktop/tree_url_node.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/log.h"

#define MAXIMUM_BASE_NODES 16
#define GLOBAL_HISTORY_RECENT_URLS 16
#define URL_CHUNK_LENGTH 512

static struct node *global_history_base_node[MAXIMUM_BASE_NODES];
static int global_history_base_node_time[MAXIMUM_BASE_NODES];
static int global_history_base_node_count = 0;

static bool global_history_initialised;

static struct tree *global_history_tree;
static struct node *global_history_tree_root;

static hlcache_handle *folder_icon;

static const char *const weekday_msg_name [] =
{
	"Sunday",
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday"
};

/**
 * Find an entry in the global history
 *
 * \param url The URL to find
 * \return Pointer to node, or NULL if not found
 */
static struct node *history_global_find(const char *url)
{
	int i;
	struct node *node;
	const char *text;

	for (i = 0; i < global_history_base_node_count; i++) {
		if (!tree_node_is_deleted(global_history_base_node[i])) {
			node = tree_node_get_child(global_history_base_node[i]);
			for (; node != NULL; node = tree_node_get_next(node)) {
				text = tree_url_node_get_url(node);
				if ((text != NULL) && !strcmp(url, text))
					return node;
			}
		}
	}
	return NULL;
}

/**
 * Internal routine to actually perform global history addition
 *
 * \param url The URL to add
 * \param data URL data associated with URL
 * \return true (for urldb_iterate_entries)
 */
static bool global_history_add_internal(nsurl *url, const struct url_data *data)
{
	int i, j;
	struct node *parent = NULL;
	struct node *link;
	struct node *node;
	bool before = false;
	int visit_date;

	assert((url != NULL) && (data != NULL));

	visit_date = data->last_visit;

	/* find parent node */
	for (i = 0; i < global_history_base_node_count; i++) {
		if (global_history_base_node_time[i] <= visit_date) {
			parent = global_history_base_node[i];
			break;
		}
	}

	/* the entry is too old to care about */
	if (parent == NULL)
		return true;

	if (tree_node_is_deleted(parent)) {
		/* parent was deleted, so find place to insert it */
		link = global_history_tree_root;

		for (j = global_history_base_node_count - 1; j >= 0; j--) {
			if (!tree_node_is_deleted(global_history_base_node[j]) &&
			    global_history_base_node_time[j] >
			    global_history_base_node_time[i]) {
				link = global_history_base_node[j];
				before = true;
				break;
			}
		}

		tree_set_node_selected(global_history_tree,
				       parent, true, false);
		tree_set_node_expanded(global_history_tree,
				       parent, false, true, true);
		tree_link_node(global_history_tree, link, parent, before);
	}

	/* find any previous occurance */
	if (global_history_initialised == false) {
		node = history_global_find(nsurl_access(url));
		if (node != NULL) {
			tree_update_URL_node(global_history_tree,
					     node, url, data);
			tree_delink_node(global_history_tree, node);
			tree_link_node(global_history_tree, parent, node,
				       false);
			return true;
		}
	}

	/* Add the node at the bottom */
	node = tree_create_URL_node_readonly(global_history_tree,
					   parent, url, data,
					   tree_url_node_callback, NULL);

	return true;
}

static node_callback_resp
history_global_node_callback(void *user_data,
			     struct node_msg_data *msg_data)
{
	if (msg_data->msg == NODE_DELETE_ELEMENT_IMG)
		return NODE_CALLBACK_HANDLED;
	return NODE_CALLBACK_NOT_HANDLED;
}

/**
 * Initialises a single grouping node for the global history tree.
 *
 * \return false on memory exhaustion, true otherwise
 */
static bool history_global_initialise_node(const char *title,
					   time_t base, int days_back)
{
	struct tm *full_time;
	char *buffer;
	struct node *node;

	base += days_back * 60 * 60 * 24;
	if (title == NULL) {
		full_time = localtime(&base);
		buffer = strdup(messages_get(weekday_msg_name[full_time->tm_wday]));
	} else {
		buffer = strdup(title);
	}

	if (buffer == NULL) {
		LOG(("malloc failed"));
		warn_user("NoMemory", 0);
		return false;
	}

	node = tree_create_folder_node(NULL, NULL, buffer,
				       false, true, true);
	if (node == NULL) {
		LOG(("malloc failed"));
		warn_user("NoMemory", 0);
		free(buffer);
		return false;
	}
	if (folder_icon != NULL)
		tree_set_node_icon(global_history_tree, node, folder_icon);
	tree_set_node_user_callback(node, history_global_node_callback, NULL);

	global_history_base_node[global_history_base_node_count] = node;
	global_history_base_node_time[global_history_base_node_count] = base;
	global_history_base_node_count++;

	return true;
}

/**
 * Initialises the grouping nodes(Today, Yesterday etc.) for the global history
 * tree.
 *
 * \return false on memory exhaustion, true otherwise
 */
static bool history_global_initialise_nodes(void)
{
	struct tm *full_time;
	time_t t;
	int weekday;
	int i;

	/* get the current time */
	t = time(NULL);
	if (t == -1) {
		LOG(("time info unaviable"));
		return false;
	}

	/* get the time at the start of today */
	full_time = localtime(&t);
	weekday = full_time->tm_wday;
	full_time->tm_sec = 0;
	full_time->tm_min = 0;
	full_time->tm_hour = 0;
	t = mktime(full_time);
	if (t == -1) {
		LOG(("mktime failed"));
		return false;
	}

	history_global_initialise_node(messages_get("DateToday"), t, 0);
	if (weekday > 0)
		if (!history_global_initialise_node(
			    messages_get("DateYesterday"), t, -1))
			return false;
	for (i = 2; i <= weekday; i++)
		if (!history_global_initialise_node(NULL, t, -i))
			return false;

	if (!history_global_initialise_node(messages_get("Date1Week"),
					    t, -weekday - 7))
		return false;
	if (!history_global_initialise_node(messages_get("Date2Week"),
					    t, -weekday - 14))
		return false;
	if (!history_global_initialise_node(messages_get("Date3Week"),
					    t, -weekday - 21))
		return false;

	return true;
}

/**
 * Initialises the global history tree.
 *
 * \param data		user data for the callbacks
 * \param start_redraw  callback function called before every redraw
 * \param end_redraw	callback function called after every redraw
 * \return true on success, false on memory exhaustion
 */
bool history_global_initialise(struct tree *tree, const char* folder_icon_name)
{
	folder_icon = tree_load_icon(folder_icon_name);
	tree_url_node_init(folder_icon_name);

	if (tree == NULL)
		return false;

	global_history_tree = tree;
	global_history_tree_root = tree_get_root(global_history_tree);

	if (!history_global_initialise_nodes())
		return false;

	LOG(("Building history tree"));

	global_history_initialised = true;
	urldb_iterate_entries(global_history_add_internal);
	global_history_initialised = false;
	tree_set_node_expanded(global_history_tree, global_history_tree_root,
			       false, true, true);
	LOG(("History tree built"));
	return true;
}


/**
 * Get flags with which the global history tree should be created;
 *
 * \return the flags
 */
unsigned int history_global_get_tree_flags(void)
{
	return TREE_NO_FLAGS;
}


/**
 * Deletes the global history tree.
 */
void history_global_cleanup(void)
{
	hlcache_handle_release(folder_icon);
	tree_url_node_cleanup();
}


/**
 * Adds a url to the global history.
 *
 * \param url the url to be added
 */
void global_history_add(nsurl *url)
{
	const struct url_data *data;

	data = urldb_get_url_data(url);
	if (data == NULL)
		return;

	global_history_add_internal(url, data);
}


/* Actions to be connected to front end specific toolbars */

/**
 * Save the global history in a human-readable form under the given location.
 *
 * \param path the path where the history will be saved
 */
bool history_global_export(const char *path)
{
	return tree_urlfile_save(global_history_tree, path, "NetSurf history");
}

/**
 * Delete nodes which are currently selected.
 */
void history_global_delete_selected(void)
{
	tree_delete_selected_nodes(global_history_tree,
				   global_history_tree_root);
}

/**
 * Delete all nodes.
 */
void history_global_delete_all(void)
{
	bool redraw_needed = tree_get_redraw(global_history_tree);
	if (redraw_needed)
		tree_set_redraw(global_history_tree, false);

	tree_set_node_selected(global_history_tree, global_history_tree_root,
			       true, true);
	tree_delete_selected_nodes(global_history_tree,
				   global_history_tree_root);

	if (redraw_needed)
		tree_set_redraw(global_history_tree, true);
}

/**
 * Select all nodes in the tree.
 */
void history_global_select_all(void)
{
	tree_set_node_selected(global_history_tree, global_history_tree_root,
			       true, true);
}

/**
 * Unselect all nodes.
 */
void history_global_clear_selection(void)
{
	tree_set_node_selected(global_history_tree, global_history_tree_root,
			       true, false);
}

/**
 * Expand grouping folders and history entries.
 */
void history_global_expand_all(void)
{
	tree_set_node_expanded(global_history_tree, global_history_tree_root,
			       true, true, true);
}

/**
 * Expand grouping folders only.
 */
void history_global_expand_directories(void)
{
	tree_set_node_expanded(global_history_tree, global_history_tree_root,
			       true, true, false);
}

/**
 * Expand history entries only.
 */
void history_global_expand_addresses(void)
{
	tree_set_node_expanded(global_history_tree, global_history_tree_root,
			       true, false, true);
}

/**
 * Collapse grouping folders and history entries.
 */
void history_global_collapse_all(void)
{
	tree_set_node_expanded(global_history_tree, global_history_tree_root,
			       false, true, true);
}

/**
 * Collapse grouping folders only.
 */
void history_global_collapse_directories(void)
{
	tree_set_node_expanded(global_history_tree, global_history_tree_root,
			       false, true, false);
}

/**
 * Collapse history entries only.
 */
void history_global_collapse_addresses(void)
{
	tree_set_node_expanded(global_history_tree, global_history_tree_root,
			       false, false, true);
}

/**
 * Open the selected entries in seperate browser windows.
 *
 * \param  tabs  open multiple entries in tabs in the new window
 */
void history_global_launch_selected(bool tabs)
{
	tree_launch_selected(global_history_tree, tabs);
}