summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--desktop/cookies.c13
-rw-r--r--desktop/sslcert.c15
-rw-r--r--desktop/tree.c68
-rw-r--r--desktop/tree.h14
-rw-r--r--desktop/tree_url_node.c46
-rw-r--r--utils/utils.c38
6 files changed, 106 insertions, 88 deletions
diff --git a/desktop/cookies.c b/desktop/cookies.c
index 539be3e2f..197b3fbd1 100644
--- a/desktop/cookies.c
+++ b/desktop/cookies.c
@@ -264,19 +264,12 @@ static struct node *cookies_create_cookie_node(struct node *parent,
const struct cookie_data *data)
{
struct node *node;
- char *name;
- name = strdup(data->name);
- if (name == NULL) {
- LOG(("malloc failed"));
- warn_user("NoMemory", 0);
- return NULL;
- }
-
- node = tree_create_leaf_node(cookies_tree, NULL, name,
+ node = tree_create_leaf_node(cookies_tree,
+ NULL,
+ data->name,
false, false, false);
if (node == NULL) {
- free(name);
return NULL;
}
diff --git a/desktop/sslcert.c b/desktop/sslcert.c
index b7a424465..2b4d726e0 100644
--- a/desktop/sslcert.c
+++ b/desktop/sslcert.c
@@ -119,19 +119,22 @@ static node_callback_resp sslcert_node_callback(void *user_data,
static struct node *sslcert_create_node(const struct ssl_cert_info *cert)
{
- struct node *node;
+ struct node *node = NULL;
struct node_element *element;
char *text;
text = messages_get_buff("SSL_Certificate_Subject", cert->subject);
- if (text == NULL)
- return NULL;
-
- node = tree_create_leaf_node(NULL, NULL, text, false, false, false);
- if (node == NULL) {
+ if (text != NULL) {
+ node = tree_create_leaf_node(NULL,
+ NULL,
+ text,
+ false, false, false);
free(text);
+ }
+ if (node == NULL) {
return NULL;
}
+
tree_set_node_user_callback(node, sslcert_node_callback, NULL);
/* add issuer node */
diff --git a/desktop/tree.c b/desktop/tree.c
index e72ef6001..c62793e02 100644
--- a/desktop/tree.c
+++ b/desktop/tree.c
@@ -117,7 +117,7 @@ struct node_element {
struct node *parent; /**< Parent node */
node_element_type type; /**< Element type */
struct node_element_box box; /**< Element bounding box */
- const char *text; /**< Text for the element */
+ char *text; /**< Text for the element */
void *bitmap; /**< Bitmap for the element */
struct node_element *next; /**< Next node element */
unsigned int flag; /**< Client specified flag for data
@@ -578,25 +578,14 @@ struct node *tree_create_folder_node(struct tree *tree, struct node *parent,
node->previous = NULL;
tree_recalculate_node_sizes(tree, node, true);
- if (parent != NULL)
+ if (parent != NULL) {
tree_link_node(tree, parent, node, false);
+ }
return node;
}
-
-/**
- * Creates a leaf node with the specified title, and optionally links it into
- * the tree.
- *
- * \param tree the owner tree of 'parent', may be NULL
- * \param parent the parent node, or NULL not to link
- * \param title the node title (not copied, used directly)
- * \param editable if true, the node title will be editable
- * \param retain_in_memory if true, the node will stay in memory after deletion
- * \param deleted if true, the node is created with the deleted flag
- * \return the newly created node.
- */
+/* exported interface documented in desktop/tree.h */
struct node *tree_create_leaf_node(struct tree *tree, struct node *parent,
const char *title, bool editable, bool retain_in_memory,
bool deleted)
@@ -607,8 +596,12 @@ struct node *tree_create_leaf_node(struct tree *tree, struct node *parent,
node = calloc(sizeof(struct node), 1);
if (node == NULL) {
- LOG(("calloc failed"));
- warn_user("NoMemory", 0);
+ return NULL;
+ }
+
+ node->data.text = strdup(title);
+ if (node->data.text == NULL) {
+ free(node);
return NULL;
}
@@ -617,7 +610,6 @@ struct node *tree_create_leaf_node(struct tree *tree, struct node *parent,
node->deleted = deleted;
node->data.parent = node;
node->data.type = NODE_ELEMENT_TEXT;
- node->data.text = title;
node->data.flag = TREE_ELEMENT_TITLE;
node->data.editable = editable;
node->sort = NULL;
@@ -625,8 +617,9 @@ struct node *tree_create_leaf_node(struct tree *tree, struct node *parent,
node->previous = NULL;
tree_recalculate_node_sizes(tree, node, true);
- if (parent != NULL)
+ if (parent != NULL) {
tree_link_node(tree, parent, node, false);
+ }
return node;
}
@@ -1488,18 +1481,20 @@ void tree_update_node_element(struct tree *tree, struct node_element *element,
assert(element != NULL);
- if (tree != NULL && element == tree->editing)
+ if ((tree != NULL) && (element == tree->editing)) {
tree_stop_edit(tree, false);
+ }
- if (text != NULL && (element->type == NODE_ELEMENT_TEXT ||
- element->type == NODE_ELEMENT_TEXT_PLUS_ICON)) {
+ if ((text != NULL) &&
+ (element->type == NODE_ELEMENT_TEXT ||
+ element->type == NODE_ELEMENT_TEXT_PLUS_ICON)) {
if (element->text != NULL) {
- if(strcmp(element->text, text) == 0) text_changed = true;
-
+ if (strcmp(element->text, text) == 0) {
+ text_changed = true;
+ }
response = NODE_CALLBACK_NOT_HANDLED;
- if (!element->editable &&
- element->parent->user_callback !=
- NULL) {
+ if ((!element->editable) &&
+ (element->parent->user_callback != NULL)) {
msg_data.msg = NODE_DELETE_ELEMENT_TXT;
msg_data.flag = element->flag;
msg_data.node = element->parent;
@@ -1508,14 +1503,16 @@ void tree_update_node_element(struct tree *tree, struct node_element *element,
element->parent->callback_data,
&msg_data);
}
- if (response != NODE_CALLBACK_HANDLED)
- free((void *)element->text);
+ if (response != NODE_CALLBACK_HANDLED) {
+ free(element->text);
+ }
}
- element->text = text;
+ element->text = (char *)text;
}
- if (bitmap != NULL && (element->type == NODE_ELEMENT_BITMAP ||
- element->type == NODE_ELEMENT_TEXT_PLUS_ICON)) {
+ if ((bitmap != NULL) &&
+ ((element->type == NODE_ELEMENT_BITMAP) ||
+ (element->type == NODE_ELEMENT_TEXT_PLUS_ICON))) {
if (element->bitmap != NULL) {
response = NODE_CALLBACK_NOT_HANDLED;
if (element->parent->user_callback != NULL) {
@@ -1527,10 +1524,11 @@ void tree_update_node_element(struct tree *tree, struct node_element *element,
element->parent->callback_data,
&msg_data);
}
- if (response != NODE_CALLBACK_HANDLED)
+
+ if (response != NODE_CALLBACK_HANDLED) {
free(element->bitmap);
- }
- else {
+ }
+ } else {
/* Increase the box width to accomodate the new icon */
element->box.width += NODE_INSTEP;
}
diff --git a/desktop/tree.h b/desktop/tree.h
index b88108776..00ac99984 100644
--- a/desktop/tree.h
+++ b/desktop/tree.h
@@ -153,9 +153,23 @@ struct tree *tree_create(unsigned int flags,
struct node *tree_create_folder_node(struct tree *tree, struct node *parent,
const char *title, bool editable, bool retain_in_memory,
bool deleted);
+
+/**
+ * Creates a leaf node with the specified title, and optionally links it into
+ * the tree.
+ *
+ * \param tree the owner tree of 'parent', may be NULL
+ * \param parent the parent node, or NULL not to link
+ * \param title the node title.
+ * \param editable if true, the node title will be editable
+ * \param retain_in_memory if true, the node will stay in memory after deletion
+ * \param deleted if true, the node is created with the deleted flag
+ * \return the newly created node or NULL on error.
+ */
struct node *tree_create_leaf_node(struct tree *tree, struct node *parent,
const char *title, bool editable, bool retain_in_memory,
bool deleted);
+
struct node_element *tree_create_node_element(struct node *parent,
node_element_type type, unsigned int flag, bool editable);
void tree_link_node(struct tree *tree, struct node *link, struct node *node,
diff --git a/desktop/tree_url_node.c b/desktop/tree_url_node.c
index 7646048bb..938cd1d69 100644
--- a/desktop/tree_url_node.c
+++ b/desktop/tree_url_node.c
@@ -126,35 +126,41 @@ void tree_url_node_cleanup()
* \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
+ * \param title custom title to use or NULL to use url
* \return the node created, or NULL for failure
*/
struct node *tree_create_URL_node(struct tree *tree, struct node *parent,
nsurl *url, const char *title,
tree_node_user_callback user_callback, void *callback_data)
{
- struct node *node;
+ struct node *node = NULL;
struct node_element *element;
- char *text_cp, *squashed;
- squashed = squash_whitespace(title ? title : nsurl_access(url));
- text_cp = strdup(squashed);
- if (text_cp == NULL) {
- LOG(("malloc failed"));
- warn_user("NoMemory", 0);
- return NULL;
+ if (title == NULL) {
+ node = tree_create_leaf_node(tree,
+ parent,
+ nsurl_access(url),
+ true, false, false);
+ } else {
+ char *squashed;
+
+ squashed = squash_whitespace(title);
+ if (squashed != NULL) {
+ node = tree_create_leaf_node(tree,
+ parent,
+ squashed,
+ true, false, false);
+ free(squashed);
+ }
}
- 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)
+ 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);
@@ -165,7 +171,7 @@ struct node *tree_create_URL_node(struct tree *tree, struct node *parent,
element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_URL, true);
if (element != NULL) {
- text_cp = strdup(nsurl_access(url));
+ char *text_cp = strdup(nsurl_access(url));
if (text_cp == NULL) {
tree_delete_node(tree, node, false);
LOG(("malloc failed"));
@@ -194,22 +200,18 @@ struct node *tree_create_URL_node_readonly(struct tree *tree,
{
struct node *node;
struct node_element *element;
- char *title;
+ const char *title;
assert(url && data);
if (data->title != NULL) {
- title = strdup(data->title);
+ title = data->title;
} else {
- title = strdup(nsurl_access(url));
+ title = nsurl_access(url);
}
- if (title == NULL)
- return NULL;
-
node = tree_create_leaf_node(tree, parent, title, false, false, false);
if (node == NULL) {
- free(title);
return NULL;
}
diff --git a/utils/utils.c b/utils/utils.c
index 3398a7df8..8155f4af1 100644
--- a/utils/utils.c
+++ b/utils/utils.c
@@ -98,26 +98,34 @@ char *remove_underscores(const char *s, bool replacespace)
/**
* Replace consecutive whitespace with a single space.
*
+ * @todo determine if squash_whitespace utf-8 safe and that it needs to be
+ *
* \param s source string
- * \return heap allocated result, or 0 on memory exhaustion
+ * \return heap allocated result, or NULL on memory exhaustion
*/
-char * squash_whitespace(const char *s)
+char *squash_whitespace(const char *s)
{
- char *c = malloc(strlen(s) + 1);
+ char *c;
int i = 0, j = 0;
- if (!c)
- return 0;
- do {
- if (s[i] == ' ' || s[i] == '\n' || s[i] == '\r' ||
- s[i] == '\t') {
- c[j++] = ' ';
- while (s[i] == ' ' || s[i] == '\n' || s[i] == '\r' ||
- s[i] == '\t')
- i++;
- }
- c[j++] = s[i++];
- } while (s[i - 1] != 0);
+
+ c = malloc(strlen(s) + 1);
+ if (c != NULL) {
+ do {
+ if (s[i] == ' ' ||
+ s[i] == '\n' ||
+ s[i] == '\r' ||
+ s[i] == '\t') {
+ c[j++] = ' ';
+ while (s[i] == ' ' ||
+ s[i] == '\n' ||
+ s[i] == '\r' ||
+ s[i] == '\t')
+ i++;
+ }
+ c[j++] = s[i++];
+ } while (s[i - 1] != 0);
+ }
return c;
}