summaryrefslogtreecommitdiff
path: root/desktop/tree_url_node.c
blob: 182c9332d27737cc7b624fa400db0fbb24c41db5 (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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
 * 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/>.
 */

/** \file
 * Creation of URL nodes with use of trees (implementation)
 */


#include <assert.h>
#include <ctype.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>

#include "content/content.h"
#include "content/hlcache.h"
#include "content/urldb.h"
#include "desktop/browser.h"
#include "desktop/options.h"
#include "desktop/tree_url_node.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/url.h"
#include "utils/utils.h"

/** Flags for each type of url tree node. */
enum tree_element_url {
	TREE_ELEMENT_URL = 0x01,
	TREE_ELEMENT_LAST_VISIT = 0x02,
	TREE_ELEMENT_VISITS = 0x03,
	TREE_ELEMENT_THUMBNAIL = 0x04,
};

#define MAX_ICON_NAME_LEN 256

static bool initialised = false;

static hlcache_handle *folder_icon;

struct icon_entry {
	content_type type;
	hlcache_handle *icon;
};

struct icon_entry icon_table[] = {
	{CONTENT_HTML, NULL},
	{CONTENT_TEXTPLAIN, NULL},
	{CONTENT_CSS, NULL},
#if defined(WITH_MNG) || defined(WITH_PNG)
	{CONTENT_PNG, NULL},
#endif
#ifdef WITH_MNG
	{CONTENT_JNG, NULL},
	{CONTENT_MNG, NULL},
#endif
#ifdef WITH_JPEG
	{CONTENT_JPEG, NULL},
#endif
#ifdef WITH_GIF
	{CONTENT_GIF, NULL},
#endif
#ifdef WITH_BMP
	{CONTENT_BMP, NULL},
	{CONTENT_ICO, NULL},
#endif
#ifdef WITH_SPRITE
	{CONTENT_SPRITE, NULL},
#endif
#ifdef WITH_DRAW
	{CONTENT_DRAW, NULL},
#endif
#ifdef WITH_ARTWORKS
	{CONTENT_ARTWORKS, NULL},
#endif
#ifdef WITH_NS_SVG
	{CONTENT_SVG, NULL},
#endif
#ifdef WITH_WEBP
	{CONTENT_WEBP, NULL},
#endif
#ifdef WITH_AMIGA_ICON
	{CONTENT_AMIGA_ICON, NULL},
#endif
 	{CONTENT_UNKNOWN, NULL},

	/* this serves as a sentinel */
	{CONTENT_HTML, NULL}
};


void tree_url_node_init(const char *folder_icon_name)
{
	struct icon_entry *entry;
	char icon_name[MAX_ICON_NAME_LEN];

	if (initialised || option_tree_icons_dir == NULL)
		return;
	initialised = true;

	folder_icon = tree_load_icon(folder_icon_name);

	entry = icon_table;
	do {

		tree_icon_name_from_content_type(icon_name, entry->type);
		entry->icon = tree_load_icon(icon_name);

		++entry;
	} while (entry->type != CONTENT_HTML);

}


/**
 * Creates a tree entry for a URL, and links it into the tree
 *
 * \param parent     the node to link to
 * \param url        the URL (copied)
 * \param data	     the URL data to use
 * \param title	     the custom title to use
 * \return the node created, or NULL for failure
 */
struct node *tree_create_URL_node(struct tree *tree, struct node *parent,
		const char *url, const char *title,
		tree_node_user_callback user_callback, void *callback_data)
{
	struct node *node;
	struct node_element *element;
	char *text_cp, *squashed;

	squashed = squash_whitespace(title ? title : url);
	text_cp = strdup(squashed);
	if (text_cp == NULL) {
		LOG(("malloc failed"));
		warn_user("NoMemory", 0);
		return NULL;
	}
	free(squashed);
	node = tree_create_leaf_node(tree, parent, text_cp, true, false,
				     false);
	if (node == NULL) {
		free(text_cp);
		return NULL;
	}

	if (user_callback != NULL)
		tree_set_node_user_callback(node, user_callback,
					    callback_data);

	tree_create_node_element(node, NODE_ELEMENT_BITMAP,
				 TREE_ELEMENT_THUMBNAIL, false);
	tree_create_node_element(node, NODE_ELEMENT_TEXT, TREE_ELEMENT_VISITS,
				 false);
	tree_create_node_element(node, NODE_ELEMENT_TEXT,
				 TREE_ELEMENT_LAST_VISIT, false);
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_URL, true);
	if (element != NULL) {
		text_cp = strdup(url);
		if (text_cp == NULL) {
			tree_delete_node(tree, node, false);
			LOG(("malloc failed"));
			warn_user("NoMemory", 0);
			return NULL;
		}
		tree_update_node_element(tree, element, text_cp, NULL);
	}

	return node;
}


/**
 * Creates a tree entry for a URL, and links it into the tree.
 *
 * All information is used directly from the url_data, and as such cannot be
 * edited and should never be freed.
 *
 * \param parent      the node to link to
 * \param url         the URL
 * \param data	      the URL data to use
 * \return the node created, or NULL for failure
 */
struct node *tree_create_URL_node_shared(struct tree *tree, struct node *parent,
		const char *url, const struct url_data *data,
		tree_node_user_callback user_callback, void *callback_data)
{
	struct node *node;
	struct node_element *element;
	const char *title;

	assert(url && data);

	if (data->title != NULL) {
		title = data->title;
	} else {
		title = url;
	}

	node = tree_create_leaf_node(tree, parent, title, false, false, false);
	if (node == NULL)
		return NULL;

	if (user_callback != NULL) {
		tree_set_node_user_callback(node, user_callback,
					    callback_data);
	}

	tree_create_node_element(node, NODE_ELEMENT_BITMAP,
				 TREE_ELEMENT_THUMBNAIL, false);
	tree_create_node_element(node, NODE_ELEMENT_TEXT, TREE_ELEMENT_VISITS,
				 false);
	tree_create_node_element(node, NODE_ELEMENT_TEXT,
				 TREE_ELEMENT_LAST_VISIT, false);
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_URL, false);
	if (element != NULL) {
		tree_update_node_element(tree, element, url, NULL);
	}

	tree_update_URL_node(tree, node, url, data, true);
	return node;
}


/**
 * Updates the node details for a URL node.
 *
 * \param node  the node to update
 */
void tree_update_URL_node(struct tree *tree, struct node *node,
		const char *url, const struct url_data *data, bool shared)
{
	struct node_element *element;
	struct bitmap *bitmap = NULL;
	struct icon_entry *entry;
	char *text_cp;

	assert(node != NULL);

	element = tree_node_find_element(node, TREE_ELEMENT_URL, NULL);
	if (element == NULL)
		return;

	if (data != NULL) {
		if (data->title == NULL)
			urldb_set_url_title(url, url);

		if (data->title == NULL)
			return;

		element = tree_node_find_element(node, TREE_ELEMENT_TITLE,
						 NULL);
		if (shared)
			tree_update_node_element(tree, element, data->title,
						 NULL);
		else {
			text_cp = strdup(data->title);
			if (text_cp == NULL) {
				LOG(("malloc failed"));
				warn_user("NoMemory", 0);
				return;
			}
			tree_update_node_element(tree, element,	text_cp, NULL);
		}
	} else {
		data = urldb_get_url_data(url);
		if (data == NULL)
			return;
	}

	entry = icon_table;
	do {
		if (entry->type == data->type) {
			if (entry->icon != NULL)
				tree_set_node_icon(tree, node, entry->icon);
			break;
		}
		++entry;
	} while (entry->type != CONTENT_HTML);

	/* update last visit text */
	element = tree_node_find_element(node, TREE_ELEMENT_LAST_VISIT, element);
	tree_update_element_text(tree,
		element, 
		messages_get_buff("TreeLast",
			(data->last_visit > 0) ?
			ctime((time_t *)&data->last_visit) :
			messages_get("TreeUnknown")));


	/* update number of visits text */
	element = tree_node_find_element(node, TREE_ELEMENT_VISITS, element);
	tree_update_element_text(tree,
		element, 
		messages_get_buff("TreeVisits", data->visits));


	/* update thumbnail */
	element = tree_node_find_element(node, TREE_ELEMENT_THUMBNAIL, element);
	if (element != NULL) {
		bitmap = urldb_get_thumbnail(url);

		if (bitmap != NULL) {
			tree_update_node_element(tree, element, NULL, bitmap);
		}
	}
}


const char *tree_url_node_get_title(struct node *node)
{
	struct node_element *element;
	element = tree_node_find_element(node, TREE_ELEMENT_TITLE, NULL);
	if (element == NULL)
		return NULL;
	return tree_node_element_get_text(element);
}


const char *tree_url_node_get_url(struct node *node)
{
	struct node_element *element;
	element = tree_node_find_element(node, TREE_ELEMENT_URL, NULL);
	if (element == NULL)
		return NULL;
	return tree_node_element_get_text(element);
}

void tree_url_node_edit_title(struct tree *tree, struct node *node)
{
	struct node_element *element;
	element = tree_node_find_element(node, TREE_ELEMENT_TITLE, NULL);
	tree_start_edit(tree, element);
}

void tree_url_node_edit_url(struct tree *tree, struct node *node)
{
	struct node_element *element;
	element = tree_node_find_element(node, TREE_ELEMENT_URL, NULL);
	tree_start_edit(tree, element);
}

node_callback_resp tree_url_node_callback(void *user_data,
					  struct node_msg_data *msg_data)
{
	struct tree *tree;
	struct node_element *element;
	url_func_result res;
	const char *text;
	char *norm_text;
	const struct url_data *data;

	/** @todo memory leaks on non-shared folder deletion. */
	switch (msg_data->msg) {
	case NODE_DELETE_ELEMENT_TXT:
		switch (msg_data->flag) {
			/* only history is using non-editable url
			 * elements so only history deletion will run
			 * this code
			 */
		case TREE_ELEMENT_URL:
			/* reset URL characteristics */
			urldb_reset_url_visit_data(
				msg_data->data.text);
			return NODE_CALLBACK_HANDLED;
		case TREE_ELEMENT_TITLE:
			return NODE_CALLBACK_HANDLED;
		}
		break;
	case NODE_DELETE_ELEMENT_IMG:
		if (msg_data->flag == TREE_ELEMENT_THUMBNAIL ||
		    msg_data->flag == TREE_ELEMENT_TITLE)
			return NODE_CALLBACK_HANDLED;
		break;
	case NODE_LAUNCH:
		element = tree_node_find_element(msg_data->node,
						 TREE_ELEMENT_URL, NULL);
		if (element != NULL) {
			text = tree_node_element_get_text(element);
			browser_window_create(text, NULL, 0,
					      true, false);
			return NODE_CALLBACK_HANDLED;
		}
		break;
	case NODE_ELEMENT_EDIT_FINISHING:

		text = msg_data->data.text;

		if (msg_data->flag == TREE_ELEMENT_URL) {
			res = url_normalize(text, &norm_text);
			if (res != URL_FUNC_OK) {
				if (res == URL_FUNC_FAILED) {
					warn_user("NoURLError", 0);
					return NODE_CALLBACK_CONTINUE;
				}
				else {
					warn_user("NoMemory", 0);
					return NODE_CALLBACK_REJECT;
				}

			}
			msg_data->data.text = norm_text;

			data = urldb_get_url_data(norm_text);
			if (data == NULL) {
				urldb_add_url(norm_text);
				urldb_set_url_persistence(norm_text,
							  true);
				data = urldb_get_url_data(norm_text);
				if (data == NULL)
					return NODE_CALLBACK_REJECT;
			}
			tree = user_data;
			tree_update_URL_node(tree, msg_data->node,
					     norm_text, NULL, false);
		}
		else if (msg_data->flag == TREE_ELEMENT_TITLE) {
			while (isspace(*text))
				text++;
			norm_text = strdup(text);
			if (norm_text == NULL) {
				LOG(("malloc failed"));
				warn_user("NoMemory", 0);
				return NODE_CALLBACK_REJECT;
			}
			/* don't allow zero length entry text, return
			   false */
			if (norm_text[0] == '\0') {
				warn_user("NoNameError", 0);
				msg_data->data.text = NULL;
				return NODE_CALLBACK_CONTINUE;
			}
			msg_data->data.text = norm_text;
		}

		return NODE_CALLBACK_HANDLED;
	default:
		break;
	}
	return NODE_CALLBACK_NOT_HANDLED;
}

/**
 * Search the children of an xmlNode for an element.
 *
 * \param  node  xmlNode to search children of, or 0
 * \param  name  name of element to find
 * \return  first child of node which is an element and matches name, or
 *          0 if not found or parameter node is 0
 */
static xmlNode *tree_url_find_xml_element(xmlNode *node, const char *name)
{
	xmlNode *xmlnode;
	if (node == NULL)
		return NULL;

	for (xmlnode = node->children;
	     xmlnode && !(xmlnode->type == XML_ELEMENT_NODE &&
		    strcmp((const char *) xmlnode->name, name) == 0);
	     xmlnode = xmlnode->next)
		;

	return xmlnode;
}

/**
 * Parse an entry represented as a li.
 *
 * \param  li         xmlNode for parsed li
 * \param  directory  directory to add this entry to
 */
static void tree_url_load_entry(xmlNode *li, struct tree *tree,
		struct node *directory, tree_node_user_callback callback,
		void *callback_data)
{
	char *url = NULL, *url1 = NULL;
	char *title = NULL;
	struct node *entry;
	xmlNode *xmlnode;
	const struct url_data *data;
	url_func_result res;

	for (xmlnode = li->children; xmlnode; xmlnode = xmlnode->next) {
		/* The li must contain an "a" element */
		if (xmlnode->type == XML_ELEMENT_NODE &&
		    strcmp((const char *)xmlnode->name, "a") == 0) {
			url1 = (char *)xmlGetProp(xmlnode, (const xmlChar *) "href");
			title = (char *)xmlNodeGetContent(xmlnode);
		}
	}

	if ((url1 == NULL) || (title == NULL)) {
		warn_user("TreeLoadError", "(Missing <a> in <li> or "
			  "memory exhausted.)");
		return;
	}

	/* We're loading external input.
	 * This may be garbage, so attempt to normalise
	 */
	res = url_normalize(url1, &url);
	if (res != URL_FUNC_OK) {
		LOG(("Failed normalising '%s'", url1));

		if (res == URL_FUNC_NOMEM)
			warn_user("NoMemory", NULL);

		xmlFree(url1);
		xmlFree(title);

		return;
	}

	/* No longer need this */
	xmlFree(url1);

	data = urldb_get_url_data(url);
	if (data == NULL) {
		/* No entry in database, so add one */
		urldb_add_url(url);
		/* now attempt to get url data */
		data = urldb_get_url_data(url);
	}
	if (data == NULL) {
		xmlFree(title);
		free(url);

		return;
	}

	/* Make this URL persistent */
	urldb_set_url_persistence(url, true);

	/* Force the title in the hotlist */
	urldb_set_url_title(url, title);

	entry = tree_create_URL_node(tree, directory, url, title,
				     callback, callback_data);

 	if (entry == NULL) {
 		/** \todo why isn't this fatal? */
 		warn_user("NoMemory", 0);
 	} else {
		tree_update_URL_node(tree, entry, url, data, false);
	}


	xmlFree(title);
	free(url);
}

/**
 * Parse a directory represented as a ul.
 *
 * \param  ul         xmlNode for parsed ul
 * \param  directory  directory to add this directory to
 */
static void tree_url_load_directory(xmlNode *ul, struct tree *tree,
		struct node *directory, tree_node_user_callback callback,
		void *callback_data)
{
	char *title;
	struct node *dir;
	xmlNode *xmlnode;

	assert(ul != NULL);
	assert(directory != NULL);

	for (xmlnode = ul->children; xmlnode; xmlnode = xmlnode->next) {
		/* The ul may contain entries as a li, or directories as
		 * an h4 followed by a ul. Non-element nodes may be present
		 * (eg. text, comments), and are ignored. */

		if (xmlnode->type != XML_ELEMENT_NODE)
			continue;

		if (strcmp((const char *)xmlnode->name, "li") == 0) {
			/* entry */
			tree_url_load_entry(xmlnode, tree, directory, callback,
					    callback_data);

		} else if (strcmp((const char *)xmlnode->name, "h4") == 0) {
			/* directory */
			title = (char *) xmlNodeGetContent(xmlnode );
			if (!title) {
				warn_user("TreeLoadError", "(Empty <h4> "
					  "or memory exhausted.)");
				return;
			}

			for (xmlnode = xmlnode->next;
			     xmlnode && xmlnode->type != XML_ELEMENT_NODE;
			     xmlnode = xmlnode->next)	
				;
			if ((xmlnode == NULL) || 
			    strcmp((const char *)xmlnode->name, "ul") != 0) {
				/* next element isn't expected ul */
				free(title);
				warn_user("TreeLoadError", "(Expected "
					  "<ul> not present.)");
				return;
			}

			dir = tree_create_folder_node(tree, directory, title,
						      true, false, false);
			if (dir == NULL) {
				free(title);
				return;
			}

			if (callback != NULL)
				tree_set_node_user_callback(dir, callback,
							    callback_data);

 			if (folder_icon != NULL)
 				tree_set_node_icon(tree, dir, folder_icon);

			tree_url_load_directory(xmlnode, tree, dir, callback,
						callback_data);
		}
	}
}

/**
 * Loads an url tree from a specified file.
 *
 * \param  filename  	name of file to read
 * \param  tree		empty tree which data will be read into
 * \return the file represented as a tree, or NULL on failure
 */
bool tree_urlfile_load(const char *filename, struct tree *tree,
		       tree_node_user_callback callback, void *callback_data)
{
	xmlDoc *doc;
	xmlNode *html, *body, *ul;
	struct node *root;
	FILE *fp = NULL;

	if (filename == NULL) {
		return false;
	}

	fp = fopen(filename, "r");
	if (fp == NULL) {
		return false;
	}
	fclose(fp);

	doc = htmlParseFile(filename, "iso-8859-1");
	if (doc == NULL) {
		warn_user("TreeLoadError", messages_get("ParsingFail"));
		return false;
	}

	html = tree_url_find_xml_element((xmlNode *) doc, "html");
	body = tree_url_find_xml_element(html, "body");
	ul = tree_url_find_xml_element(body, "ul");
	if (ul == NULL) {
		xmlFreeDoc(doc);
		warn_user("TreeLoadError",
			  "(<html>...<body>...<ul> not found.)");
		return false;
	}

	root = tree_get_root(tree);
	tree_url_load_directory(ul, tree, root, callback, callback_data);
	tree_set_node_expanded(tree, root, true, false, false);

	xmlFreeDoc(doc);
	return true;
}

/**
 * Add an entry to the HTML tree for saving.
 *
 * The node must contain a sequence of node_elements in the following order:
 *
 * \param  entry  hotlist entry to add
 * \param  node   node to add li to
 * \return  true on success, false on memory exhaustion
 */
static bool tree_url_save_entry(struct node *entry, xmlNode *node)
{
	xmlNode *li, *a;
	xmlAttr *href;
	const char *text;

	li = xmlNewChild(node, NULL, (const xmlChar *) "li", NULL);
	if (li == NULL)
		return false;


	text = tree_url_node_get_title(entry);
	if (text == NULL)
		return false;
	a = xmlNewTextChild(li, NULL, (const xmlChar *) "a",
			    (const xmlChar *) text);
	if (a == NULL)
		return false;

	text = tree_url_node_get_url(entry);
	if (text == NULL)
		return false;

	href = xmlNewProp(a, (const xmlChar *) "href", (const xmlChar *) text);
	if (href == NULL)
		return false;
	return true;
}

/**
 * Add a directory to the HTML tree for saving.
 *
 * \param  directory  hotlist directory to add
 * \param  node       node to add ul to
 * \return  true on success, false on memory exhaustion
 */
static bool tree_url_save_directory(struct node *directory, xmlNode *node)
{
	struct node *child;
	xmlNode *ul, *h4;
	const char *text;

	ul = xmlNewChild(node, NULL, (const xmlChar *)"ul", NULL);
	if (ul == NULL)
		return false;

	for (child = tree_node_get_child(directory); child;
	     child = tree_node_get_next(child)) {
		if (!tree_node_is_folder(child)) {
			/* entry */
			if (!tree_url_save_entry(child, ul))
				return false;
		} else {
			/* directory */
			/* invalid HTML */

			text = tree_url_node_get_title(child);
			if (text == NULL)
				return false;

			h4 = xmlNewTextChild(ul, NULL,
					     (const xmlChar *) "h4",
					     (const xmlChar *) text);
			if (h4 == NULL)
				return false;

			if (!tree_url_save_directory(child, ul))
				return false;
		}	}

	return true;
}








/**
 * Perform a save to a specified file in the form of a html page
 *
 * \param filename	the file to save to
 * \param page_title 	title of the page
 */
bool tree_urlfile_save(struct tree *tree, const char *filename,
		       const char *page_title)
{
	int res;
	xmlDoc *doc;
	xmlNode *html, *head, *title, *body;

	/* Unfortunately the Browse Hotlist format is invalid HTML,
	 * so this is a lie. 
	 */
	doc = htmlNewDoc(
		(const xmlChar *) "http://www.w3.org/TR/html4/strict.dtd",
		(const xmlChar *) "-//W3C//DTD HTML 4.01//EN");
	if (doc == NULL) {
		warn_user("NoMemory", 0);
		return false;
	}

	html = xmlNewNode(NULL, (const xmlChar *) "html");
	if (html == NULL) {
		warn_user("NoMemory", 0);
		xmlFreeDoc(doc);
		return false;
	}
	xmlDocSetRootElement(doc, html);

	head = xmlNewChild(html, NULL, (const xmlChar *) "head", NULL);
	if (head == NULL) {
		warn_user("NoMemory", 0);
		xmlFreeDoc(doc);
		return false;
	}

	title  = xmlNewTextChild(head, NULL, (const xmlChar *) "title",
				 (const xmlChar *) page_title);
	if (title == NULL) {
		warn_user("NoMemory", 0);
		xmlFreeDoc(doc);
		return false;
	}

	body = xmlNewChild(html, NULL, (const xmlChar *) "body", NULL);
	if (body == NULL) {
		warn_user("NoMemory", 0);
		xmlFreeDoc(doc);
		return false;
	}

	if (!tree_url_save_directory(tree_get_root(tree), body)) {
 		warn_user("NoMemory", 0);
 		xmlFreeDoc(doc);
 		return false;
 	}

	doc->charset = XML_CHAR_ENCODING_UTF8;
	res = htmlSaveFileEnc(filename, doc, "iso-8859-1");
	if (res == -1) {
		warn_user("HotlistSaveError", 0);
		xmlFreeDoc(doc);
		return false;
	}

	xmlFreeDoc(doc);
	return true;
}