diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2013-08-28 11:43:34 +0100 |
---|---|---|
committer | Michael Drake <tlsa@netsurf-browser.org> | 2013-08-28 11:43:34 +0100 |
commit | fbfe5ab17ba41187bcbba5e759dd8e2e98a1b3e7 (patch) | |
tree | f464351d771bbab3a572177350d58b2f1c1a0a00 /desktop | |
parent | 45d3b9162c82f61b80d8257523d6530f8ddba5c3 (diff) | |
download | netsurf-fbfe5ab17ba41187bcbba5e759dd8e2e98a1b3e7.tar.gz netsurf-fbfe5ab17ba41187bcbba5e759dd8e2e98a1b3e7.tar.bz2 |
Function for global history export.
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/global_history.c | 103 | ||||
-rw-r--r-- | desktop/global_history.h | 9 |
2 files changed, 112 insertions, 0 deletions
diff --git a/desktop/global_history.c b/desktop/global_history.c index 7432be284..6713fcab5 100644 --- a/desktop/global_history.c +++ b/desktop/global_history.c @@ -25,6 +25,8 @@ #include "desktop/treeview.h" #include "utils/messages.h" #include "utils/utils.h" +#include "utils/utf8.h" +#include "utils/libdom.h" #include "utils/log.h" #define N_DAYS 28 @@ -825,6 +827,107 @@ nserror global_history_add(nsurl *url) } +struct treeview_export_walk_ctx { + FILE *fp; +}; +/** Callback for treeview_walk node entering */ +static nserror global_history_export_enter_cb(void *ctx, void *node_data, + enum treeview_node_type type, bool *abort) +{ + struct treeview_export_walk_ctx *tw = ctx; + + if (type == TREE_NODE_ENTRY) { + struct global_history_entry *e = node_data; + utf8_convert_ret ret; + char *t_text; + char *u_text; + + ret = utf8_to_html(e->data[GH_TITLE].value, "iso-8859-1", + e->data[GH_TITLE].value_len, &t_text); + if (ret != UTF8_CONVERT_OK) + return NSERROR_SAVE_FAILED; + + ret = utf8_to_html(e->data[GH_URL].value, "iso-8859-1", + e->data[GH_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 global_history_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, "<li><h4>%s</h4>\n<ul>\n", f_text); + + free(f_text); + } + + return NSERROR_OK; +} +/** Callback for treeview_walk node leaving */ +static nserror global_history_export_leave_cb(void *ctx, void *node_data, + enum treeview_node_type type, bool *abort) +{ + struct treeview_export_walk_ctx *tw = ctx; + + if (type == TREE_NODE_FOLDER) { + fputs("</ul></li>\n", tw->fp); + } + + return NSERROR_OK; +} +/* Exported interface, documented in global_history.h */ +nserror global_history_export(const char *path, const char *title) +{ + struct treeview_export_walk_ctx tw; + nserror err; + FILE *fp; + + fp = fopen(path, "w"); + if (fp == NULL) + return NSERROR_SAVE_FAILED; + + if (title == NULL) + title = "NetSurf Browsing History"; + + 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(gh_ctx.tree, NULL, + global_history_export_enter_cb, + global_history_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; +} + + /* Exported interface, documented in global_history.h */ void global_history_redraw(int x, int y, struct rect *clip, const struct redraw_context *ctx) diff --git a/desktop/global_history.h b/desktop/global_history.h index 3863e8151..9d1dc4523 100644 --- a/desktop/global_history.h +++ b/desktop/global_history.h @@ -61,6 +61,15 @@ nserror global_history_fini(void); */ nserror global_history_add(nsurl *url); +/* + * Save global history to file (html) + * + * \param path The path to save history to + * \param title The title to give the document, or NULL for default + * \return NSERROR_OK on success, or appropriate error otherwise + */ +nserror global_history_export(const char *path, const char *title); + /** * Redraw the global history. * |