Add hotlist iteration function.

This commit is contained in:
Michael Drake 2013-09-03 19:43:59 +01:00
parent 1658554437
commit 541724fb0d
2 changed files with 109 additions and 0 deletions

View File

@ -948,6 +948,67 @@ nserror hotlist_export(const char *path, const char *title)
} }
struct hotlist_iterate_ctx {
hotlist_folder_enter_cb enter_cb;
hotlist_address_cb address_cb;
hotlist_folder_leave_cb leave_cb;
void *ctx;
};
/** Callback for hotlist_iterate node entering */
static nserror hotlist_iterate_enter_cb(void *ctx, void *node_data,
enum treeview_node_type type, bool *abort)
{
struct hotlist_iterate_ctx *data = ctx;
if (type == TREE_NODE_ENTRY && data->address_cb != NULL) {
struct hotlist_entry *e = node_data;
data->address_cb(data->ctx, e->url,
e->data[HL_TITLE].value);
} else if (type == TREE_NODE_FOLDER && data->enter_cb != NULL) {
struct hotlist_folder *f = node_data;
data->enter_cb(data->ctx, f->data.value);
}
return NSERROR_OK;
}
/** Callback for hotlist_iterate node leaving */
static nserror hotlist_iterate_leave_cb(void *ctx, void *node_data,
enum treeview_node_type type, bool *abort)
{
struct hotlist_iterate_ctx *data = ctx;
if (type == TREE_NODE_FOLDER && data->leave_cb != NULL) {
data->leave_cb(data->ctx);
}
return NSERROR_OK;
}
/* Exported interface, documented in hotlist.h */
nserror hotlist_iterate(void *ctx,
hotlist_folder_enter_cb enter_cb,
hotlist_address_cb address_cb,
hotlist_folder_leave_cb leave_cb)
{
struct hotlist_iterate_ctx data;
nserror err;
data.enter_cb = enter_cb;
data.address_cb = address_cb;
data.leave_cb = leave_cb;
data.ctx = ctx;
err = treeview_walk(hl_ctx.tree, NULL,
hotlist_iterate_enter_cb,
hotlist_iterate_leave_cb,
&data, TREE_NODE_ENTRY | TREE_NODE_FOLDER);
if (err != NSERROR_OK)
return err;
return NSERROR_OK;
}
/** /**
* Initialise the treeview entry feilds * Initialise the treeview entry feilds
* *

View File

@ -117,6 +117,54 @@ nserror hotlist_add_folder(const char *title, bool at_y, int y);
*/ */
nserror hotlist_export(const char *path, const char *title); nserror hotlist_export(const char *path, const char *title);
/**
* Client callback for hotlist_iterate, reporting entry into folder
*
* \param ctx Client context
* \param title The entered folder's title
* \return NSERROR_OK on success, or appropriate error otherwise
*/
typedef nserror (*hotlist_folder_enter_cb)(void *ctx, const char *title);
/**
* Client callback for hotlist_iterate, reporting a hotlist address
*
* \param ctx Client context
* \param url The entry's address
* \param title The entry's title
* \return NSERROR_OK on success, or appropriate error otherwise
*/
typedef nserror (*hotlist_address_cb)(void *ctx, nsurl *url, const char *title);
/**
* Client callback for hotlist_iterate, reporting a hotlist folder departure
*
* \param ctx Client context
* \param title The departed folder's title
* \return NSERROR_OK on success, or appropriate error otherwise
*/
typedef nserror (*hotlist_folder_leave_cb)(void *ctx);
/**
* Walk (depth first) the hotlist, calling callbacks on entering folders,
* address nodes, and on leaving folders.
*
* \param ctx Client context, passed back to callback function
* \param enter_cb Function to call on entering nodes, or NULL
* \param address_cb Function to call on address nodes, or NULL
* \param leave_cb Function to call on leaving nodes, or NULL
* \return NSERROR_OK on success, or appropriate error otherwise
*
* Example usage: Generate a menu containing hotlist entries. For flat list
* set enter_cb and leave_cb to NULL, or for hierarchical menu
* provide the folder callbacks.
*/
nserror hotlist_iterate(void *ctx,
hotlist_folder_enter_cb enter_cb,
hotlist_address_cb address_cb,
hotlist_folder_leave_cb leave_cb);
/** /**
* Redraw the hotlist. * Redraw the hotlist.
* *