Reduce old cookie manager to skeleton.

This commit is contained in:
Michael Drake 2013-09-02 13:06:40 +01:00
parent 452c89aa83
commit 81b2a859c0
2 changed files with 1 additions and 400 deletions

View File

@ -39,318 +39,6 @@
#include "utils/url.h"
#include "utils/utils.h"
/** Flags for each type of cookie tree node. */
enum tree_element_cookie {
TREE_ELEMENT_PERSISTENT = 0x01,
TREE_ELEMENT_VERSION = 0x02,
TREE_ELEMENT_SECURE = 0x03,
TREE_ELEMENT_LAST_USED = 0x04,
TREE_ELEMENT_EXPIRES = 0x05,
TREE_ELEMENT_PATH = 0x06,
TREE_ELEMENT_DOMAIN = 0x07,
TREE_ELEMENT_COMMENT = 0x08,
TREE_ELEMENT_VALUE = 0x09,
};
static struct tree *cookies_tree;
static struct node *cookies_tree_root;
static bool user_delete;
static hlcache_handle *folder_icon;
static hlcache_handle *cookie_icon;
/**
* Find an entry in the cookie tree
*
* \param node the node to check the children of
* \param title The title to find
* \return Pointer to node, or NULL if not found
*/
static struct node *cookies_find(struct node *node, const char *title)
{
struct node *search;
struct node_element *element;
assert(node !=NULL);
for (search = tree_node_get_child(node); search;
search = tree_node_get_next(search)) {
element = tree_node_find_element(search, TREE_ELEMENT_TITLE,
NULL);
if (strcmp(title, tree_node_element_get_text(element)) == 0)
return search;
}
return NULL;
}
/**
* Callback for all cookie tree nodes.
*/
static node_callback_resp cookies_node_callback(void *user_data, struct node_msg_data *msg_data)
{
struct node *node = msg_data->node;
struct node_element *domain, *path;
const char *domain_t, *path_t, *name_t;
char *space;
bool is_folder = tree_node_is_folder(node);
/* we don't remove any icons here */
if (msg_data->msg == NODE_DELETE_ELEMENT_IMG)
return NODE_CALLBACK_HANDLED;
/* let the tree handle events other than text data removal */
if (msg_data->msg != NODE_DELETE_ELEMENT_TXT)
return NODE_CALLBACK_NOT_HANDLED;
/* check if it's a domain folder */
if (is_folder)
return NODE_CALLBACK_NOT_HANDLED;
switch (msg_data->flag) {
case TREE_ELEMENT_TITLE:
if (!user_delete)
break;
/* get the rest of the cookie data */
domain = tree_node_find_element(node,
TREE_ELEMENT_DOMAIN, NULL);
path = tree_node_find_element(node, TREE_ELEMENT_PATH,
NULL);
if ((domain != NULL) &&
(path != NULL)) {
domain_t = tree_node_element_get_text(domain) +
strlen(messages_get(
"TreeDomain")) - 4;
space = strchr(domain_t, ' ');
if (space != NULL)
*space = '\0';
path_t = tree_node_element_get_text(path) +
strlen(messages_get("TreePath"))
- 4;
space = strchr(path_t, ' ');
if (space != NULL)
*space = '\0';
name_t = msg_data->data.text;
urldb_delete_cookie(domain_t, path_t, name_t);
}
break;
default:
break;
}
free(msg_data->data.text);
return NODE_CALLBACK_HANDLED;
}
/**
* Updates a tree entry for a cookie.
*
* All information is copied from the cookie_data, and as such can
* be edited and should be freed.
*
* \param node The node to update
* \param data The cookie data to use
* \return true if node updated, or false for failure
*/
static bool cookies_update_cookie_node(struct node *node,
const struct cookie_data *data)
{
struct node_element *element;
char buffer[32];
assert(data != NULL);
/* update the value text */
element = tree_node_find_element(node, TREE_ELEMENT_VALUE, NULL);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreeValue",
data->value != NULL ?
data->value :
messages_get("TreeUnused")));
/* update the comment text */
if ((data->comment != NULL) &&
(strcmp(data->comment, "") != 0)) {
element = tree_node_find_element(node, TREE_ELEMENT_COMMENT, NULL);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreeComment",
data->comment));
}
/* update domain text */
element = tree_node_find_element(node, TREE_ELEMENT_DOMAIN, element);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreeDomain",
data->domain,
data->domain_from_set ?
messages_get("TreeHeaders") :
""));
/* update path text */
element = tree_node_find_element(node, TREE_ELEMENT_PATH, element);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreePath", data->path,
data->path_from_set ?
messages_get("TreeHeaders") :
""));
/* update expiry text */
element = tree_node_find_element(node, TREE_ELEMENT_EXPIRES, element);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreeExpires",
(data->expires > 0)
? (data->expires == 1)
? messages_get("TreeSession")
: ctime(&data->expires)
: messages_get("TreeUnknown")));
/* update last used text */
element = tree_node_find_element(node, TREE_ELEMENT_LAST_USED, element);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreeLastUsed",
(data->last_used > 0) ?
ctime(&data->last_used) :
messages_get("TreeUnknown")));
/* update secure text */
element = tree_node_find_element(node, TREE_ELEMENT_SECURE, element);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreeSecure",
data->secure ?
messages_get("Yes") :
messages_get("No")));
/* update version text */
element = tree_node_find_element(node, TREE_ELEMENT_VERSION, element);
snprintf(buffer, sizeof(buffer), "TreeVersion%i", data->version);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreeVersion",
messages_get(buffer)));
/* update persistant text */
element = tree_node_find_element(node, TREE_ELEMENT_PERSISTENT, element);
tree_update_element_text(cookies_tree,
element,
messages_get_buff("TreePersistent",
data->no_destroy ?
messages_get("Yes") :
messages_get("No")));
return true;
}
/**
* Creates an empty tree entry for a cookie, and links it into the tree.
*
* All information is copied from the cookie_data, and as such can
* be edited and should be freed.
*
* \param parent the node to link to
* \param data the cookie data to use
* \return the node created, or NULL for failure
*/
static struct node *cookies_create_cookie_node(struct node *parent,
const struct cookie_data *data)
{
struct node *node;
node = tree_create_leaf_node(cookies_tree,
NULL,
data->name,
false, false, false);
if (node == NULL) {
return NULL;
}
tree_set_node_user_callback(node, cookies_node_callback, NULL);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_PERSISTENT, false);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_VERSION, false);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_SECURE, false);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_LAST_USED, false);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_EXPIRES, false);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_PATH, false);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_DOMAIN, false);
if ((data->comment) && (strcmp(data->comment, "")))
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_COMMENT, false);
tree_create_node_element(node, NODE_ELEMENT_TEXT,
TREE_ELEMENT_VALUE, false);
tree_set_node_icon(cookies_tree, node, cookie_icon);
if (!cookies_update_cookie_node(node, data))
{
tree_delete_node(NULL, node, false);
return NULL;
}
tree_link_node(cookies_tree, parent, node, false);
return node;
}
/**
* Called when scheduled event gets fired. Actually performs the update.
*/
static void cookies_schedule_callback(const void *scheduled_data)
{
const struct cookie_data *data = scheduled_data;
struct node *node = NULL;
struct node *cookie_node = NULL;
assert(data != NULL);
node = cookies_find(cookies_tree_root, data->domain);
if (node == NULL) {
node = tree_create_folder_node(cookies_tree,
cookies_tree_root,
data->domain,
false, false, false);
if (node != NULL) {
tree_set_node_user_callback(node,
cookies_node_callback,
NULL);
tree_set_node_icon(cookies_tree, node, folder_icon);
}
}
if (node != NULL) {
cookie_node = cookies_find(node, data->name);
if (cookie_node == NULL) {
cookies_create_cookie_node(node, data);
} else {
cookies_update_cookie_node(cookie_node, data);
}
}
}
/**
* Initialises cookies tree.
*
@ -365,22 +53,6 @@ bool cookies_initialise(struct tree *tree, const char* folder_icon_name, const c
if (tree == NULL)
return false;
LOG(("Making cookie tree"));
folder_icon = tree_load_icon(folder_icon_name);
cookie_icon = tree_load_icon(cookie_icon_name);
/* Create an empty tree */
cookies_tree = tree;
cookies_tree_root = tree_get_root(cookies_tree);
user_delete = false;
urldb_iterate_cookies(cookies_schedule_update);
tree_set_node_expanded(cookies_tree, cookies_tree_root,
false, true, true);
LOG(("Made cookie tree"));
return true;
}
@ -392,30 +64,7 @@ bool cookies_initialise(struct tree *tree, const char* folder_icon_name, const c
*/
unsigned int cookies_get_tree_flags(void)
{
return TREE_DELETE_EMPTY_DIRS | TREE_COOKIES;
}
/* exported interface documented in cookies_old.h */
bool cookies_schedule_update(const struct cookie_data *data)
{
assert(data != NULL);
assert(user_delete == false);
if (cookies_tree_root != NULL)
cookies_schedule_callback(data);
return true;
}
/* exported interface documented in cookies_old.h */
void cookies_remove(const struct cookie_data *data)
{
assert(data != NULL);
if (cookies_tree_root != NULL)
cookies_schedule_callback(data);
return TREE_COOKIES;
}
@ -424,8 +73,6 @@ void cookies_remove(const struct cookie_data *data)
*/
void cookies_cleanup(void)
{
hlcache_handle_release(folder_icon);
hlcache_handle_release(cookie_icon);
}
/* Actions to be connected to front end specific toolbars */
@ -435,9 +82,6 @@ void cookies_cleanup(void)
*/
void cookies_delete_selected(void)
{
user_delete = true;
tree_delete_selected_nodes(cookies_tree, cookies_tree_root);
user_delete = false;
}
/**
@ -445,17 +89,6 @@ void cookies_delete_selected(void)
*/
void cookies_delete_all(void)
{
bool needs_redraw = tree_get_redraw(cookies_tree);
if (needs_redraw)
tree_set_redraw(cookies_tree, false);
user_delete = true;
tree_set_node_selected(cookies_tree, cookies_tree_root, true, true);
tree_delete_selected_nodes(cookies_tree, cookies_tree_root);
user_delete = false;
if (needs_redraw)
tree_set_redraw(cookies_tree, true);
}
/**
@ -463,7 +96,6 @@ void cookies_delete_all(void)
*/
void cookies_select_all(void)
{
tree_set_node_selected(cookies_tree, cookies_tree_root, true, true);
}
/**
@ -471,7 +103,6 @@ void cookies_select_all(void)
*/
void cookies_clear_selection(void)
{
tree_set_node_selected(cookies_tree, cookies_tree_root, true, false);
}
/**
@ -479,8 +110,6 @@ void cookies_clear_selection(void)
*/
void cookies_expand_all(void)
{
tree_set_node_expanded(cookies_tree, cookies_tree_root,
true, true, true);
}
/**
@ -488,8 +117,6 @@ void cookies_expand_all(void)
*/
void cookies_expand_domains(void)
{
tree_set_node_expanded(cookies_tree, cookies_tree_root,
true, true, false);
}
/**
@ -497,8 +124,6 @@ void cookies_expand_domains(void)
*/
void cookies_expand_cookies(void)
{
tree_set_node_expanded(cookies_tree, cookies_tree_root,
true, false, true);
}
/**
@ -506,8 +131,6 @@ void cookies_expand_cookies(void)
*/
void cookies_collapse_all(void)
{
tree_set_node_expanded(cookies_tree, cookies_tree_root,
false, true, true);
}
/**
@ -515,8 +138,6 @@ void cookies_collapse_all(void)
*/
void cookies_collapse_domains(void)
{
tree_set_node_expanded(cookies_tree, cookies_tree_root,
false, true, false);
}
/**
@ -524,6 +145,4 @@ void cookies_collapse_domains(void)
*/
void cookies_collapse_cookies(void)
{
tree_set_node_expanded(cookies_tree, cookies_tree_root,
false, false, true);
}

View File

@ -32,24 +32,6 @@ struct cookie_data;
bool cookies_initialise(struct tree *tree, const char* folder_icon_name, const char* cookie_icon_name);
unsigned int cookies_get_tree_flags(void);
/**
* Perform cookie updates and addition. The update is only scheduled here.
* The actual update is performed in the callback function.
*
* \param data Data of cookie being updated.
* \return true (for urldb_iterate_entries)
*/
bool cookies_schedule_update(const struct cookie_data *data);
/**
* Remove a cookie from the active set.
* The cookie is to be removed from the active set and no futher
* references made to the cookie data.
*
* \param data Data of cookie being removed.
*/
void cookies_remove(const struct cookie_data *data);
void cookies_cleanup(void);
void cookies_delete_selected(void);