2010-10-05 23:14:46 +04:00
|
|
|
/*
|
|
|
|
* 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>
|
2012-11-10 03:22:19 +04:00
|
|
|
|
|
|
|
#include <dom/dom.h>
|
|
|
|
#include <dom/bindings/hubbub/parser.h>
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
#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"
|
2012-11-10 03:22:19 +04:00
|
|
|
#include "utils/corestrings.h"
|
2012-11-10 15:20:31 +04:00
|
|
|
#include "utils/libdom.h"
|
2010-10-05 23:14:46 +04:00
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/url.h"
|
2012-11-10 03:22:19 +04:00
|
|
|
#include "utils/utf8.h"
|
2010-10-05 23:14:46 +04:00
|
|
|
#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},
|
2011-05-07 00:40:09 +04:00
|
|
|
{CONTENT_IMAGE, NULL},
|
2011-05-11 23:29:48 +04:00
|
|
|
{CONTENT_NONE, NULL},
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
/* this serves as a sentinel */
|
|
|
|
{CONTENT_HTML, NULL}
|
|
|
|
};
|
|
|
|
|
2011-01-20 16:51:41 +03:00
|
|
|
static uint32_t tun_users = 0;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2010-12-14 03:18:24 +03:00
|
|
|
void tree_url_node_init(const char *folder_icon_name)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
|
|
|
struct icon_entry *entry;
|
|
|
|
char icon_name[MAX_ICON_NAME_LEN];
|
2011-01-20 16:51:41 +03:00
|
|
|
|
|
|
|
tun_users++;
|
|
|
|
|
2011-01-20 17:05:13 +03:00
|
|
|
if (initialised)
|
2010-10-05 23:14:46 +04:00
|
|
|
return;
|
|
|
|
initialised = true;
|
|
|
|
|
2010-12-14 03:18:24 +03:00
|
|
|
folder_icon = tree_load_icon(folder_icon_name);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-20 16:51:41 +03:00
|
|
|
void tree_url_node_cleanup()
|
|
|
|
{
|
|
|
|
struct icon_entry *entry;
|
|
|
|
|
|
|
|
tun_users--;
|
|
|
|
|
|
|
|
if (tun_users > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!initialised)
|
|
|
|
return;
|
|
|
|
initialised = false;
|
|
|
|
|
|
|
|
hlcache_handle_release(folder_icon);
|
|
|
|
|
|
|
|
entry = icon_table;
|
|
|
|
do {
|
|
|
|
hlcache_handle_release(entry->icon);
|
|
|
|
++entry;
|
|
|
|
} while (entry->type != CONTENT_HTML);
|
|
|
|
}
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
/**
|
|
|
|
* 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,
|
2012-10-11 14:20:02 +04:00
|
|
|
nsurl *url, const char *title,
|
2010-10-05 23:14:46 +04:00
|
|
|
tree_node_user_callback user_callback, void *callback_data)
|
|
|
|
{
|
|
|
|
struct node *node;
|
|
|
|
struct node_element *element;
|
|
|
|
char *text_cp, *squashed;
|
|
|
|
|
2012-10-11 14:20:02 +04:00
|
|
|
squashed = squash_whitespace(title ? title : nsurl_access(url));
|
2010-10-05 23:14:46 +04:00
|
|
|
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) {
|
2012-10-11 14:20:02 +04:00
|
|
|
text_cp = strdup(nsurl_access(url));
|
2010-10-05 23:14:46 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-07-24 15:40:08 +04:00
|
|
|
* Creates a read only tree entry for a URL, and links it into the tree.
|
2010-10-05 23:14:46 +04:00
|
|
|
*
|
|
|
|
* \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
|
|
|
|
*/
|
2011-07-24 15:40:08 +04:00
|
|
|
struct node *tree_create_URL_node_readonly(struct tree *tree,
|
2012-10-11 14:20:02 +04:00
|
|
|
struct node *parent, nsurl *url,
|
2011-07-24 15:40:08 +04:00
|
|
|
const struct url_data *data,
|
2010-10-05 23:14:46 +04:00
|
|
|
tree_node_user_callback user_callback, void *callback_data)
|
|
|
|
{
|
|
|
|
struct node *node;
|
|
|
|
struct node_element *element;
|
2011-07-24 15:40:08 +04:00
|
|
|
char *title;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
assert(url && data);
|
|
|
|
|
|
|
|
if (data->title != NULL) {
|
2011-07-24 15:40:08 +04:00
|
|
|
title = strdup(data->title);
|
2010-10-05 23:14:46 +04:00
|
|
|
} else {
|
2012-10-11 14:20:02 +04:00
|
|
|
title = strdup(nsurl_access(url));
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
|
2011-07-24 15:40:08 +04:00
|
|
|
if (title == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
node = tree_create_leaf_node(tree, parent, title, false, false, false);
|
2011-07-24 15:40:08 +04:00
|
|
|
if (node == NULL) {
|
|
|
|
free(title);
|
2010-10-05 23:14:46 +04:00
|
|
|
return NULL;
|
2011-07-24 15:40:08 +04:00
|
|
|
}
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
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) {
|
2012-10-11 14:20:02 +04:00
|
|
|
tree_update_node_element(tree, element, nsurl_access(url),
|
|
|
|
NULL);
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
|
2011-07-24 15:40:08 +04:00
|
|
|
tree_update_URL_node(tree, node, url, data);
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
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,
|
2012-10-11 14:20:02 +04:00
|
|
|
nsurl *url, const struct url_data *data)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
|
|
|
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)
|
2012-10-11 14:20:02 +04:00
|
|
|
urldb_set_url_title(url, nsurl_access(url));
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
if (data->title == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
element = tree_node_find_element(node, TREE_ELEMENT_TITLE,
|
|
|
|
NULL);
|
2011-07-24 15:40:08 +04:00
|
|
|
|
|
|
|
text_cp = strdup(data->title);
|
|
|
|
if (text_cp == NULL) {
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return;
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
2011-07-24 15:40:08 +04:00
|
|
|
tree_update_node_element(tree, element, text_cp, NULL);
|
2010-10-05 23:14:46 +04:00
|
|
|
} 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;
|
2013-02-14 22:21:11 +04:00
|
|
|
nsurl *url;
|
2011-10-06 16:31:22 +04:00
|
|
|
nserror error;
|
2010-10-05 23:14:46 +04:00
|
|
|
const char *text;
|
2010-10-28 00:15:01 +04:00
|
|
|
char *norm_text;
|
2010-10-05 23:14:46 +04:00
|
|
|
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 */
|
2013-02-14 22:21:11 +04:00
|
|
|
error = nsurl_create(msg_data->data.text, &url);
|
2012-10-11 14:20:02 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return NODE_CALLBACK_REJECT;
|
|
|
|
}
|
2013-02-14 22:21:11 +04:00
|
|
|
urldb_reset_url_visit_data(url);
|
|
|
|
nsurl_unref(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
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) {
|
2013-02-14 22:21:11 +04:00
|
|
|
nserror error;
|
|
|
|
enum browser_window_nav_flags flags;
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
text = tree_node_element_get_text(element);
|
2013-02-14 22:21:11 +04:00
|
|
|
|
|
|
|
error = nsurl_create(text, &url);
|
|
|
|
if (error == NSERROR_OK) {
|
2013-02-19 14:17:17 +04:00
|
|
|
struct browser_window *clone = NULL;
|
|
|
|
|
2013-02-18 15:51:42 +04:00
|
|
|
flags = BROWSER_WINDOW_VERIFIABLE |
|
|
|
|
BROWSER_WINDOW_HISTORY;
|
2013-02-14 22:21:11 +04:00
|
|
|
if (msg_data->flag == TREE_ELEMENT_LAUNCH_IN_TABS) {
|
2013-02-18 15:51:42 +04:00
|
|
|
flags |= BROWSER_WINDOW_TAB;
|
2013-02-19 14:17:17 +04:00
|
|
|
clone = msg_data->data.bw;
|
2013-02-14 22:21:11 +04:00
|
|
|
}
|
|
|
|
error = browser_window_create(flags,
|
|
|
|
url,
|
|
|
|
NULL,
|
2013-02-19 14:17:17 +04:00
|
|
|
clone,
|
2013-02-14 22:21:11 +04:00
|
|
|
&msg_data->data.bw);
|
|
|
|
nsurl_unref(url);
|
2011-07-02 15:41:06 +04:00
|
|
|
}
|
2013-02-14 22:21:11 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
warn_user(messages_get_errorcode(error), 0);
|
|
|
|
}
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
return NODE_CALLBACK_HANDLED;
|
|
|
|
}
|
|
|
|
break;
|
2013-02-14 22:21:11 +04:00
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
case NODE_ELEMENT_EDIT_FINISHING:
|
|
|
|
|
|
|
|
text = msg_data->data.text;
|
|
|
|
|
|
|
|
if (msg_data->flag == TREE_ELEMENT_URL) {
|
2011-10-06 16:31:22 +04:00
|
|
|
size_t len;
|
2013-02-14 22:21:11 +04:00
|
|
|
error = nsurl_create(text, &url);
|
2011-10-06 16:31:22 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return NODE_CALLBACK_REJECT;
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
2013-02-14 22:21:11 +04:00
|
|
|
error = nsurl_get(url, NSURL_WITH_FRAGMENT,
|
2011-10-06 16:31:22 +04:00
|
|
|
&norm_text, &len);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return NODE_CALLBACK_REJECT;
|
|
|
|
}
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
msg_data->data.text = norm_text;
|
|
|
|
|
2013-02-14 22:21:11 +04:00
|
|
|
data = urldb_get_url_data(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
if (data == NULL) {
|
2013-02-14 22:21:11 +04:00
|
|
|
urldb_add_url(url);
|
|
|
|
urldb_set_url_persistence(url, true);
|
|
|
|
data = urldb_get_url_data(url);
|
2012-10-11 14:20:02 +04:00
|
|
|
if (data == NULL) {
|
2013-02-14 22:21:11 +04:00
|
|
|
nsurl_unref(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
return NODE_CALLBACK_REJECT;
|
2012-10-11 14:20:02 +04:00
|
|
|
}
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
tree = user_data;
|
|
|
|
tree_update_URL_node(tree, msg_data->node,
|
2013-02-14 22:21:11 +04:00
|
|
|
url, NULL);
|
|
|
|
nsurl_unref(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
typedef struct {
|
|
|
|
struct tree *tree;
|
|
|
|
struct node *directory;
|
|
|
|
tree_node_user_callback callback;
|
|
|
|
void *callback_data;
|
|
|
|
bool last_was_h4;
|
|
|
|
dom_string *title;
|
|
|
|
} tree_url_load_ctx;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
static void tree_url_load_directory(dom_node *ul, tree_url_load_ctx *ctx);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse an entry represented as a li.
|
|
|
|
*
|
2012-11-10 03:22:19 +04:00
|
|
|
* \param li DOM node for parsed li
|
2010-10-05 23:14:46 +04:00
|
|
|
* \param directory directory to add this entry to
|
|
|
|
*/
|
2012-11-10 03:22:19 +04:00
|
|
|
static void tree_url_load_entry(dom_node *li, tree_url_load_ctx *ctx)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
2012-11-10 03:22:19 +04:00
|
|
|
dom_node *a;
|
|
|
|
dom_string *title1;
|
|
|
|
dom_string *url1;
|
|
|
|
char *title, *url2;
|
2011-10-06 16:31:22 +04:00
|
|
|
nsurl *url;
|
2012-11-10 03:22:19 +04:00
|
|
|
const struct url_data *data;
|
|
|
|
struct node *entry;
|
|
|
|
dom_exception derror;
|
2011-10-06 16:31:22 +04:00
|
|
|
nserror error;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
/* The li must contain an "a" element */
|
2012-11-10 15:20:31 +04:00
|
|
|
a = libdom_find_first_element(li, corestring_lwc_a);
|
2012-11-10 03:22:19 +04:00
|
|
|
if (a == NULL) {
|
|
|
|
warn_user("TreeLoadError", "(Missing <a> in <li>)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
derror = dom_node_get_text_content(a, &title1);
|
2012-11-11 15:23:43 +04:00
|
|
|
if (derror != DOM_NO_ERR) {
|
2012-11-10 03:22:19 +04:00
|
|
|
warn_user("TreeLoadError", "(No title)");
|
|
|
|
dom_node_unref(a);
|
|
|
|
return;
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
derror = dom_element_get_attribute(a, corestring_dom_href, &url1);
|
|
|
|
if (derror != DOM_NO_ERR || url1 == NULL) {
|
|
|
|
warn_user("TreeLoadError", "(No URL)");
|
|
|
|
dom_string_unref(title1);
|
|
|
|
dom_node_unref(a);
|
2010-10-05 23:14:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-11 15:23:43 +04:00
|
|
|
if (title1 != NULL) {
|
|
|
|
title = strndup(dom_string_data(title1),
|
|
|
|
dom_string_byte_length(title1));
|
|
|
|
dom_string_unref(title1);
|
|
|
|
} else {
|
|
|
|
title = strdup("");
|
|
|
|
}
|
2012-11-10 03:22:19 +04:00
|
|
|
if (title == NULL) {
|
|
|
|
warn_user("NoMemory", NULL);
|
|
|
|
dom_string_unref(url1);
|
|
|
|
dom_node_unref(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
/* We're loading external input.
|
2011-10-06 16:31:22 +04:00
|
|
|
* This may be garbage, so attempt to normalise via nsurl
|
2010-10-05 23:14:46 +04:00
|
|
|
*/
|
2012-11-10 03:22:19 +04:00
|
|
|
url2 = strndup(dom_string_data(url1), dom_string_byte_length(url1));
|
|
|
|
if (url2 == NULL) {
|
|
|
|
warn_user("NoMemory", NULL);
|
|
|
|
free(title);
|
|
|
|
dom_string_unref(url1);
|
|
|
|
dom_node_unref(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom_string_unref(url1);
|
|
|
|
|
|
|
|
error = nsurl_create(url2, &url);
|
|
|
|
|
|
|
|
free(url2);
|
|
|
|
|
2011-10-06 16:31:22 +04:00
|
|
|
if (error != NSERROR_OK) {
|
2012-11-10 03:22:19 +04:00
|
|
|
LOG(("Failed normalising '%s'", url2));
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2011-10-06 16:31:22 +04:00
|
|
|
warn_user("NoMemory", NULL);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
free(title);
|
|
|
|
dom_node_unref(a);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-11 14:20:02 +04:00
|
|
|
data = urldb_get_url_data(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
if (data == NULL) {
|
|
|
|
/* No entry in database, so add one */
|
2012-10-11 14:20:02 +04:00
|
|
|
urldb_add_url(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
/* now attempt to get url data */
|
2012-10-11 14:20:02 +04:00
|
|
|
data = urldb_get_url_data(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
if (data == NULL) {
|
2011-10-06 16:31:22 +04:00
|
|
|
nsurl_unref(url);
|
2012-11-10 03:22:19 +04:00
|
|
|
free(title);
|
|
|
|
dom_node_unref(a);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make this URL persistent */
|
2012-10-11 14:20:02 +04:00
|
|
|
urldb_set_url_persistence(url, true);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2010-12-05 19:09:52 +03:00
|
|
|
/* Force the title in the hotlist */
|
2012-10-11 14:20:02 +04:00
|
|
|
urldb_set_url_title(url, title);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
entry = tree_create_URL_node(ctx->tree, ctx->directory, url, title,
|
|
|
|
ctx->callback, ctx->callback_data);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
/** \todo why isn't this fatal? */
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
} else {
|
2012-11-10 03:22:19 +04:00
|
|
|
tree_update_URL_node(ctx->tree, entry, url, data);
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
|
2011-10-06 16:31:22 +04:00
|
|
|
nsurl_unref(url);
|
2012-11-10 03:22:19 +04:00
|
|
|
free(title);
|
|
|
|
dom_node_unref(a);
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
static bool tree_url_load_directory_cb(dom_node *node, void *ctx)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
2012-11-10 03:22:19 +04:00
|
|
|
tree_url_load_ctx *tctx = ctx;
|
|
|
|
dom_string *name;
|
|
|
|
dom_exception error;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
/* 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. */
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
error = dom_node_get_node_name(node, &name);
|
|
|
|
if (error != DOM_NO_ERR || name == NULL)
|
|
|
|
return false;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
if (dom_string_caseless_lwc_isequal(name, corestring_lwc_li)) {
|
|
|
|
/* entry */
|
|
|
|
tree_url_load_entry(node, tctx);
|
|
|
|
tctx->last_was_h4 = false;
|
|
|
|
} else if (dom_string_caseless_lwc_isequal(name, corestring_lwc_h4)) {
|
|
|
|
/* directory (a) */
|
|
|
|
dom_string *title;
|
|
|
|
|
|
|
|
error = dom_node_get_text_content(node, &title);
|
|
|
|
if (error != DOM_NO_ERR || title == NULL) {
|
|
|
|
warn_user("TreeLoadError", "(Empty <h4> "
|
|
|
|
"or memory exhausted.)");
|
|
|
|
dom_string_unref(name);
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
if (tctx->title != NULL)
|
|
|
|
dom_string_unref(tctx->title);
|
|
|
|
tctx->title = title;
|
|
|
|
tctx->last_was_h4 = true;
|
|
|
|
} else if (tctx->last_was_h4 && dom_string_caseless_lwc_isequal(name,
|
|
|
|
corestring_lwc_ul)) {
|
|
|
|
/* directory (b) */
|
|
|
|
dom_string *id;
|
|
|
|
bool dir_is_default;
|
|
|
|
struct node *dir;
|
|
|
|
char *title;
|
|
|
|
tree_url_load_ctx new_ctx;
|
|
|
|
|
|
|
|
error = dom_element_get_attribute(node, corestring_dom_id, &id);
|
|
|
|
if (error != DOM_NO_ERR) {
|
|
|
|
dom_string_unref(name);
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
if (id != NULL) {
|
|
|
|
dir_is_default = dom_string_caseless_lwc_isequal(id,
|
|
|
|
corestring_lwc_default);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
dom_string_unref(id);
|
|
|
|
} else {
|
|
|
|
dir_is_default = false;
|
|
|
|
}
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
title = strndup(dom_string_data(tctx->title),
|
|
|
|
dom_string_byte_length(tctx->title));
|
|
|
|
if (title == NULL) {
|
|
|
|
dom_string_unref(name);
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
dir = tree_create_folder_node(tctx->tree, tctx->directory,
|
|
|
|
title, true, false, false);
|
|
|
|
if (dir == NULL) {
|
|
|
|
dom_string_unref(name);
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-10 17:07:47 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
if (dir_is_default)
|
|
|
|
tree_set_default_folder_node(tctx->tree, dir);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
if (tctx->callback != NULL)
|
|
|
|
tree_set_node_user_callback(dir, tctx->callback,
|
|
|
|
tctx->callback_data);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
if (folder_icon != NULL)
|
|
|
|
tree_set_node_icon(tctx->tree, dir, folder_icon);
|
|
|
|
|
|
|
|
new_ctx.tree = tctx->tree;
|
|
|
|
new_ctx.directory = dir;
|
|
|
|
new_ctx.callback = tctx->callback;
|
|
|
|
new_ctx.callback_data = tctx->callback_data;
|
|
|
|
new_ctx.last_was_h4 = false;
|
|
|
|
new_ctx.title = NULL;
|
|
|
|
|
|
|
|
tree_url_load_directory(node, &new_ctx);
|
|
|
|
|
|
|
|
if (new_ctx.title != NULL) {
|
|
|
|
dom_string_unref(new_ctx.title);
|
|
|
|
new_ctx.title = NULL;
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
2012-11-10 03:22:19 +04:00
|
|
|
tctx->last_was_h4 = false;
|
|
|
|
} else {
|
|
|
|
tctx->last_was_h4 = false;
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
2012-11-10 03:22:19 +04:00
|
|
|
|
|
|
|
dom_string_unref(name);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a directory represented as a ul.
|
|
|
|
*
|
|
|
|
* \param ul DOM node for parsed ul
|
|
|
|
* \param directory directory to add this directory to
|
|
|
|
*/
|
|
|
|
static void tree_url_load_directory(dom_node *ul, tree_url_load_ctx *ctx)
|
|
|
|
{
|
|
|
|
assert(ul != NULL);
|
|
|
|
assert(ctx != NULL);
|
|
|
|
assert(ctx->directory != NULL);
|
|
|
|
|
2012-11-10 15:20:31 +04:00
|
|
|
libdom_iterate_child_elements(ul, tree_url_load_directory_cb, ctx);
|
2010-10-05 23:14:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2012-11-10 03:22:19 +04:00
|
|
|
dom_document *document;
|
|
|
|
dom_node *html, *body, *ul;
|
2010-10-05 23:14:46 +04:00
|
|
|
struct node *root;
|
2012-11-11 15:31:34 +04:00
|
|
|
nserror error;
|
2012-11-10 03:22:19 +04:00
|
|
|
tree_url_load_ctx ctx;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
if (filename == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-11 15:31:34 +04:00
|
|
|
error = libdom_parse_file(filename, "iso-8859-1", &document);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
if (error != NSERROR_NOT_FOUND) {
|
|
|
|
warn_user("TreeLoadError", messages_get("ParsingFail"));
|
|
|
|
}
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-10 15:20:31 +04:00
|
|
|
html = libdom_find_first_element((dom_node *) document,
|
2012-11-10 03:22:19 +04:00
|
|
|
corestring_lwc_html);
|
|
|
|
if (html == NULL) {
|
|
|
|
dom_node_unref(document);
|
|
|
|
warn_user("TreeLoadError", "(<html> not found)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-10 15:20:31 +04:00
|
|
|
body = libdom_find_first_element(html, corestring_lwc_body);
|
2012-11-10 03:22:19 +04:00
|
|
|
if (body == NULL) {
|
|
|
|
dom_node_unref(html);
|
|
|
|
dom_node_unref(document);
|
|
|
|
warn_user("TreeLoadError", "(<html>...<body> not found)");
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-10 15:20:31 +04:00
|
|
|
ul = libdom_find_first_element(body, corestring_lwc_ul);
|
2010-10-05 23:14:46 +04:00
|
|
|
if (ul == NULL) {
|
2012-11-10 03:22:19 +04:00
|
|
|
dom_node_unref(body);
|
|
|
|
dom_node_unref(html);
|
|
|
|
dom_node_unref(document);
|
2010-10-05 23:14:46 +04:00
|
|
|
warn_user("TreeLoadError",
|
|
|
|
"(<html>...<body>...<ul> not found.)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
root = tree_get_root(tree);
|
2012-11-10 03:22:19 +04:00
|
|
|
|
|
|
|
ctx.tree = tree;
|
|
|
|
ctx.directory = root;
|
|
|
|
ctx.callback = callback;
|
|
|
|
ctx.callback_data = callback_data;
|
|
|
|
ctx.last_was_h4 = false;
|
|
|
|
ctx.title = NULL;
|
|
|
|
|
|
|
|
tree_url_load_directory(ul, &ctx);
|
2010-10-05 23:14:46 +04:00
|
|
|
tree_set_node_expanded(tree, root, true, false, false);
|
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
if (ctx.title != NULL) {
|
|
|
|
dom_string_unref(ctx.title);
|
|
|
|
ctx.title = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom_node_unref(ul);
|
|
|
|
dom_node_unref(body);
|
|
|
|
dom_node_unref(html);
|
|
|
|
dom_node_unref(document);
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
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
|
2012-11-10 03:22:19 +04:00
|
|
|
* \param fp File to write to
|
2010-10-05 23:14:46 +04:00
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*/
|
2012-11-10 03:22:19 +04:00
|
|
|
static bool tree_url_save_entry(struct node *entry, FILE *fp)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
2012-11-10 03:22:19 +04:00
|
|
|
const char *href, *text;
|
|
|
|
char *latin1_href, *latin1_text;
|
|
|
|
utf8_convert_ret ret;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
text = tree_url_node_get_title(entry);
|
|
|
|
if (text == NULL)
|
|
|
|
return false;
|
2012-11-10 03:22:19 +04:00
|
|
|
|
|
|
|
href = tree_url_node_get_url(entry);
|
|
|
|
if (href == NULL)
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
|
|
|
|
2012-12-31 03:33:27 +04:00
|
|
|
ret = utf8_to_html(text, "iso-8859-1", strlen(text), &latin1_text);
|
2012-11-10 03:22:19 +04:00
|
|
|
if (ret != UTF8_CONVERT_OK)
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
|
|
|
|
2012-12-31 03:33:27 +04:00
|
|
|
ret = utf8_to_html(href, "iso-8859-1", strlen(href), &latin1_href);
|
2012-11-10 03:22:19 +04:00
|
|
|
if (ret != UTF8_CONVERT_OK) {
|
|
|
|
free(latin1_text);
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
2012-11-10 03:22:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "<li><a href=\"%s\">%s</a></li>",
|
|
|
|
latin1_href, latin1_text);
|
|
|
|
|
|
|
|
free(latin1_href);
|
|
|
|
free(latin1_text);
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a directory to the HTML tree for saving.
|
|
|
|
*
|
|
|
|
* \param directory hotlist directory to add
|
2012-11-10 03:22:19 +04:00
|
|
|
* \param fp File to write to
|
2010-10-05 23:14:46 +04:00
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*/
|
2012-11-10 03:22:19 +04:00
|
|
|
static bool tree_url_save_directory(struct node *directory, FILE *fp)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
|
|
|
struct node *child;
|
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
fputs("<ul", fp);
|
|
|
|
if (tree_node_is_default(directory))
|
|
|
|
fputs(" id=\"default\"", fp);
|
|
|
|
fputc('>', fp);
|
|
|
|
|
|
|
|
if (tree_node_get_child(directory) != NULL)
|
|
|
|
fputc('\n', fp);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
for (child = tree_node_get_child(directory); child != NULL;
|
2010-10-05 23:14:46 +04:00
|
|
|
child = tree_node_get_next(child)) {
|
2012-11-10 03:22:19 +04:00
|
|
|
if (tree_node_is_folder(child) == false) {
|
2010-10-05 23:14:46 +04:00
|
|
|
/* entry */
|
2012-11-10 03:22:19 +04:00
|
|
|
if (tree_url_save_entry(child, fp) == false)
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
/* directory */
|
|
|
|
/* invalid HTML */
|
2012-11-10 03:22:19 +04:00
|
|
|
const char *text;
|
|
|
|
char *latin1_text;
|
|
|
|
utf8_convert_ret ret;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
text = tree_url_node_get_title(child);
|
|
|
|
if (text == NULL)
|
|
|
|
return false;
|
|
|
|
|
2012-12-31 03:33:27 +04:00
|
|
|
ret = utf8_to_html(text, "iso-8859-1",
|
2012-11-10 03:22:19 +04:00
|
|
|
strlen(text), &latin1_text);
|
|
|
|
if (ret != UTF8_CONVERT_OK)
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
fprintf(fp, "<h4>%s</h4>\n", latin1_text);
|
|
|
|
|
|
|
|
free(latin1_text);
|
|
|
|
|
|
|
|
if (tree_url_save_directory(child, fp) == false)
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
2012-11-10 03:22:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs("</ul>", fp);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-11-10 03:22:19 +04:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(filename, "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
/* Unfortunately the Browse Hotlist format is invalid HTML,
|
|
|
|
* so this is a lie.
|
|
|
|
*/
|
2012-11-10 03:22:19 +04:00
|
|
|
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", page_title);
|
2012-12-31 03:33:27 +04:00
|
|
|
fputs("</head>\n<body>", fp);
|
2012-11-10 03:22:19 +04:00
|
|
|
|
|
|
|
if (tree_url_save_directory(tree_get_root(tree), fp) == false) {
|
|
|
|
warn_user("HotlistSaveError", 0);
|
|
|
|
fclose(fp);
|
2010-10-05 23:14:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-10 03:22:19 +04:00
|
|
|
fputs("</body>\n</html>\n", fp);
|
|
|
|
|
|
|
|
fclose(fp);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-10 03:22:19 +04:00
|
|
|
|