Add Hotlist export/save support.

This commit is contained in:
Michael Drake 2013-08-22 16:06:06 +01:00
parent 1212f1d321
commit f0749394d1

View File

@ -30,6 +30,7 @@
#include "utils/corestrings.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/utf8.h"
#include "utils/libdom.h"
#include "utils/log.h"
@ -785,14 +786,107 @@ static nserror hotlist_generate(void)
}
struct treeview_walk_ctx {
FILE *fp;
};
/** Callback for treeview_walk node entering */
static nserror hotlist_export_enter_cb(void *ctx, void *node_data,
enum treeview_node_type type, bool *abort)
{
struct treeview_walk_ctx *tw = ctx;
if (type == TREE_NODE_ENTRY) {
struct hotlist_entry *e = node_data;
utf8_convert_ret ret;
char *t_text;
char *u_text;
ret = utf8_to_html(e->data[HL_TITLE].value, "iso-8859-1",
e->data[HL_TITLE].value_len, &t_text);
if (ret != UTF8_CONVERT_OK)
return NSERROR_SAVE_FAILED;
ret = utf8_to_html(e->data[HL_URL].value, "iso-8859-1",
e->data[HL_URL].value_len, &u_text);
if (ret != UTF8_CONVERT_OK) {
free(t_text);
return NSERROR_SAVE_FAILED;
}
fprintf(tw->fp, "<li><a href=\"%s\">%s</a></li>\n",
u_text, t_text);
free(t_text);
free(u_text);
} else if (type == TREE_NODE_FOLDER) {
struct hotlist_folder *f = node_data;
utf8_convert_ret ret;
char *f_text;
ret = utf8_to_html(f->data.value, "iso-8859-1",
f->data.value_len, &f_text);
if (ret != UTF8_CONVERT_OK)
return NSERROR_SAVE_FAILED;
fprintf(tw->fp, "<h4>%s</h4>\n<ul>\n", f_text);
free(f_text);
}
return NSERROR_OK;
}
/** Callback for treeview_walk node leaving */
static nserror hotlist_export_leave_cb(void *ctx, void *node_data,
enum treeview_node_type type, bool *abort)
{
struct treeview_walk_ctx *tw = ctx;
if (type == TREE_NODE_FOLDER) {
fputs("</ul>\n", tw->fp);
}
return NSERROR_OK;
}
/*
* Save hotlist to file
*
* \return NSERROR_OK on success, or appropriate error otherwise
*/
static nserror hotlist_export(const char *path)
static nserror hotlist_export(const char *path, const char *title)
{
/* TODO */
struct treeview_walk_ctx tw;
nserror err;
FILE *fp;
fp = fopen(path, "w");
if (fp == NULL)
return NSERROR_SAVE_FAILED;
/* The Acorn Browse Hotlist format, which we mimic[*], is invalid HTML
* claming to be valid.
* [*] Why? */
fputs("<!DOCTYPE html "
"PUBLIC \"//W3C/DTD HTML 4.01//EN\" "
"\"http://www.w3.org/TR/html4/strict.dtd\">\n", fp);
fputs("<html>\n<head>\n", fp);
fputs("<meta http-equiv=\"Content-Type\" "
"content=\"text/html; charset=iso-8859-1\">\n", fp);
fprintf(fp, "<title>%s</title>\n", title);
fputs("</head>\n<body>\n<ul>\n", fp);
tw.fp = fp;
err = treeview_walk(hl_ctx.tree, NULL,
hotlist_export_enter_cb,
hotlist_export_leave_cb,
&tw, TREE_NODE_ENTRY | TREE_NODE_FOLDER);
if (err != NSERROR_OK)
return err;
fputs("</ul>\n</body>\n</html>\n", fp);
fclose(fp);
return NSERROR_OK;
}
@ -949,7 +1043,7 @@ nserror hotlist_fini(const char *path)
LOG(("Finalising hotlist"));
/* Save the hotlist */
err = hotlist_export(path);
err = hotlist_export(path, "NetSurf hotlist");
if (err != NSERROR_OK) {
warn_user("Couldn't save the hotlist.", 0);
}