Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete favourites #91

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Factored out generic functionality from history handling.
msdemlei committed Apr 18, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 6356012c08028cbf1436c8fa4197b7f2e95dc49a
22 changes: 5 additions & 17 deletions about.c
Original file line number Diff line number Diff line change
@@ -562,27 +562,15 @@ xtp_handle_dl(struct tab *t, uint8_t cmd, int id, const char *query)
void
xtp_handle_hl(struct tab *t, uint8_t cmd, int id, const char *query)
{
struct pagelist_entry *h, *next, *ht;
int i = 1;
struct pagelist_entry *h, *ht;

switch (cmd) {
case XT_XTP_HL_REMOVE:
/* walk backwards, as listed in reverse */
for (h = RB_MAX(history_list, &hl); h != NULL; h = next) {
next = RB_PREV(history_list, &hl, h);
if (id == i) {
RB_REMOVE(history_list, &hl, h);
g_free((gpointer) h->title);
g_free((gpointer) h->uri);
g_free(h);
break;
}
i++;
}
remove_pagelist_entry_by_count(&hl, 1);
break;
case XT_XTP_HL_REMOVE_ALL:
RB_FOREACH_SAFE(h, history_list, &hl, ht)
RB_REMOVE(history_list, &hl, h);
RB_FOREACH_SAFE(h, pagelist, &hl, ht)
remove_pagelist_entry(&hl, h);
break;
case XT_XTP_HL_LIST:
/* Nothing - just xtp_page_hl() below */
@@ -1672,7 +1660,7 @@ xtp_page_hl(struct tab *t, struct karg *args)
"<th style='width: 40px'>Rm</th></tr>\n",
XT_XTP_STR, XT_XTP_HL, t->session_key, XT_XTP_HL_REMOVE_ALL);

RB_FOREACH_REVERSE(h, history_list, &hl) {
RB_FOREACH_REVERSE(h, pagelist, &hl) {
tmp = body;
body = g_strdup_printf(
"%s\n<tr>"
106 changes: 86 additions & 20 deletions history.c
Original file line number Diff line number Diff line change
@@ -20,7 +20,10 @@
*/

/* This module contains generic code for lists of web pages, as well
as special handling for history and favorites. */
as special handling for history and favorites.

The page lists are actually managed as red-black trees, but that's
an implementation detail. */


#include <xombrero.h>
@@ -32,6 +35,39 @@ as special handling for history and favorites. */
* than MAX_HISTORY_AGE. */
#define XT_MAX_HISTORY_AGE (60.0 * 60.0 * 24 * 14) /* 14 days */

/* remove what's pointed to by item from list.
*/
void
remove_pagelist_entry(struct pagelist *list, struct pagelist_entry *item)
{
RB_REMOVE(pagelist, list, item);
g_free((gpointer) item->title);
g_free((gpointer) item->uri);
g_free(item);
}

/* remove the n-th item from a pagelist.

Returns 0 on success, 1 if the list is too short.
*/
int remove_pagelist_entry_by_count(struct pagelist *list,
int count)
{
int i;
struct pagelist_entry *item, *next;

/* walk backwards, as listed in reverse */
for (item = RB_MAX(pagelist, list); item != NULL; item = next) {
next = RB_PREV(pagelist, list, item);
if (count == i) {
remove_pagelist_entry(list, item);
return 0;
}
i++;
}
return 1;
}

int
purge_history(void)
{
@@ -44,17 +80,17 @@ purge_history(void)
if (hl_purge_count == XT_MAX_HL_PURGE_COUNT) {
hl_purge_count = 0;

for (h = RB_MIN(history_list, &hl); h != NULL; h = next) {
for (h = RB_MIN(pagelist, &hl); h != NULL; h = next) {

next = RB_NEXT(history_list, &hl, h);
next = RB_NEXT(pagelist, &hl, h);

age = difftime(time(NULL), h->time);

if (age > XT_MAX_HISTORY_AGE) {
DNPRINTF(XT_D_HISTORY, "%s: removing %s (age %.1f)\n",
__func__, h->uri, age);

RB_REMOVE(history_list, &hl, h);
RB_REMOVE(pagelist, &hl, h);
g_free(h->uri);
g_free(h->title);
g_free(h);
@@ -69,7 +105,8 @@ purge_history(void)
}

int
insert_history_item(const gchar *uri, const gchar *title, time_t time)
insert_pagelist_entry(struct pagelist *list,
const gchar *uri, const gchar *title, time_t time)
{
struct pagelist_entry *h;

@@ -83,18 +120,13 @@ insert_history_item(const gchar *uri, const gchar *title, time_t time)

DNPRINTF(XT_D_HISTORY, "%s: adding %s\n", __func__, h->uri);

RB_INSERT(history_list, &hl, h);
completion_add_uri(h->uri);
hl_purge_count++;

purge_history();
update_history_tabs(NULL);

RB_INSERT(pagelist, list, h);

return (0);
}

int
restore_global_history(void)
load_pagelist_from_disk(struct pagelist *list, char *file_name)
{
char file[PATH_MAX];
FILE *f;
@@ -103,7 +135,7 @@ restore_global_history(void)
struct tm tm;
const char delim[3] = {'\\', '\\', '\0'};

snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_HISTORY_FILE);
snprintf(file, sizeof file, "%s" PS "%s", work_dir, file_name);

if ((f = fopen(file, "r")) == NULL) {
warnx("%s: fopen", __func__);
@@ -134,7 +166,7 @@ restore_global_history(void)

time = mktime(&tm);

if (insert_history_item(uri, title, time)) {
if (insert_pagelist_entry(list, uri, title, time)) {
err = "failed to insert item";
goto done;
}
@@ -161,21 +193,21 @@ restore_global_history(void)
}

int
save_global_history_to_disk(struct tab *t)
save_pagelist_to_disk(struct pagelist *list, char *file_name)
{
char file[PATH_MAX];
FILE *f;
struct pagelist_entry *h;

snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_HISTORY_FILE);
snprintf(file, sizeof file, "%s" PS "%s", work_dir, file_name);

if ((f = fopen(file, "w")) == NULL) {
show_oops(t, "%s: global history file: %s",
warnx("%s: global history file: %s",
__func__, strerror(errno));
return (1);
}

RB_FOREACH_REVERSE(h, history_list, &hl) {
RB_FOREACH_REVERSE(h, pagelist, list) {
if (h->uri && h->title && h->time)
fprintf(f, "%s\n%s\n%s", h->uri, h->title,
ctime(&h->time));
@@ -196,7 +228,7 @@ color_visited_helper(void)
char *d, *s = NULL, *t;
struct pagelist_entry *h;

RB_FOREACH_REVERSE(h, history_list, &hl) {
RB_FOREACH_REVERSE(h, pagelist, &hl) {
if (s == NULL)
s = g_strdup_printf("'%s':'dummy'", h->uri);
else {
@@ -249,3 +281,37 @@ color_visited(struct tab *t, char *visited)

return (0);
}

int
insert_history_item(const gchar *uri, const gchar *title, time_t time)
{
int retval;

retval = (insert_pagelist_entry(&hl, uri, title, time));

if (retval==0) {
completion_add_uri(uri);
hl_purge_count++;
}

purge_history();
update_history_tabs(NULL);

return (retval);
}

int
restore_global_history(void)
{
return (load_pagelist_from_disk(
&hl, XT_HISTORY_FILE));
}

int
save_global_history_to_disk(struct tab *t)
{
/* tab was passed for error messaging; we're now using warnx
in save_pagelist and hence don't need that any more. Do we? */
return (save_pagelist_to_disk(
&hl, XT_HISTORY_FILE));
}
10 changes: 5 additions & 5 deletions xombrero.c
Original file line number Diff line number Diff line change
@@ -208,7 +208,7 @@ GtkWidget *tab_bar_box;
GtkWidget *arrow, *abtn;
GdkEvent *fevent = NULL;
struct tab_list tabs;
struct history_list hl;
struct pagelist hl;
int hl_purge_count = 0;
struct session_list sessions;
struct wl_list c_wl;
@@ -689,11 +689,11 @@ int download_rb_cmp(struct download *, struct download *);
gboolean cmd_execute(struct tab *t, char *str);

int
history_rb_cmp(struct pagelist_entry *h1, struct pagelist_entry *h2)
pagelist_rb_cmp(struct pagelist_entry *h1, struct pagelist_entry *h2)
{
return (strcmp(h1->uri, h2->uri));
}
RB_GENERATE(history_list, pagelist_entry, entry, history_rb_cmp);
RB_GENERATE(pagelist, pagelist_entry, entry, pagelist_rb_cmp);

int
download_rb_cmp(struct download *e1, struct download *e2)
@@ -4285,7 +4285,7 @@ notify_load_status_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
!strncmp(uri, "https://", strlen("https://")) ||
!strncmp(uri, "file://", strlen("file://"))) {
find.uri = (gchar *)uri;
h = RB_FIND(history_list, &hl, &find);
h = RB_FIND(pagelist, &hl, &find);
if (!h)
insert_history_item(uri,
get_title(t, FALSE), time(NULL));
@@ -6176,7 +6176,7 @@ cmd_getlist(int id, char *key)

if (id >= 0) {
if (cmds[id].type & XT_URLARG) {
RB_FOREACH_REVERSE(h, history_list, &hl)
RB_FOREACH_REVERSE(h, pagelist, &hl)
if (match_uri(h->uri, key)) {
cmd_status.list[c] = (char *)h->uri;
if (++c > 255)
10 changes: 6 additions & 4 deletions xombrero.h
Original file line number Diff line number Diff line change
@@ -331,8 +331,8 @@ struct pagelist_entry {
gchar *title;
time_t time; /* When the item was added. */
};
RB_HEAD(history_list, pagelist_entry);
RB_PROTOTYPE(history_list, pagelist_entry, entry, history_rb_cmp);
RB_HEAD(pagelist, pagelist_entry);
RB_PROTOTYPE(pagelist, pagelist_entry, entry, pagelist_rb_cmp);

#define XT_STS_FLAGS_INCLUDE_SUBDOMAINS (1)
#define XT_STS_FLAGS_EXPAND (2)
@@ -425,12 +425,14 @@ void setup_cookies(void);
void soup_cookie_jar_add_cookie(SoupCookieJar *, SoupCookie *);
void soup_cookie_jar_delete_cookie(SoupCookieJar *, SoupCookie *);

/* history */
/* page lists */
int insert_history_item(const gchar *uri, const gchar *title, time_t time);
int save_global_history_to_disk(struct tab *t);
int restore_global_history(void);
char *color_visited_helper(void);
int color_visited(struct tab *t, char *visited);
int remove_pagelist_entry_by_count(struct pagelist *list, int count);
void remove_pagelist_entry(struct pagelist *list, struct pagelist_entry *item);

/* completion */
void completion_add(struct tab *);
@@ -988,7 +990,7 @@ extern void (*_soup_cookie_jar_add_cookie)(SoupCookieJar *, SoupCookie *);
extern void (*_soup_cookie_jar_delete_cookie)(SoupCookieJar *,
SoupCookie *);

extern struct history_list hl;
extern struct pagelist hl;
extern int hl_purge_count;
extern struct download_list downloads;
extern struct tab_list tabs;