-
Notifications
You must be signed in to change notification settings - Fork 37
/
history.c
247 lines (200 loc) · 5.64 KB
/
history.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright (c) 2010, 2011 Marco Peereboom <[email protected]>
* Copyright (c) 2011 Stevan Andjelkovic <[email protected]>
* Copyright (c) 2010, 2011 Edd Barrett <[email protected]>
* Copyright (c) 2011 Todd T. Fries <[email protected]>
* Copyright (c) 2011 Raphael Graf <[email protected]>
* Copyright (c) 2011 Michal Mazurek <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <xombrero.h>
#define XT_HISTORY_FILE ("history")
#define XT_MAX_HL_PURGE_COUNT (1000) /* Purge the history for every
* MAX_HL_PURGE_COUNT items inserted into
* history and delete all items older
* than MAX_HISTORY_AGE. */
#define XT_MAX_HISTORY_AGE (60.0 * 60.0 * 24 * 14) /* 14 days */
int
purge_history(void)
{
struct history *h, *next;
double age = 0.0;
DNPRINTF(XT_D_HISTORY, "%s: hl_purge_count = %d (%d is max)\n",
__func__, hl_purge_count, XT_MAX_HL_PURGE_COUNT);
if (hl_purge_count == XT_MAX_HL_PURGE_COUNT) {
hl_purge_count = 0;
for (h = RB_MIN(history_list, &hl); h != NULL; h = next) {
next = RB_NEXT(history_list, &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);
g_free(h->uri);
g_free(h->title);
g_free(h);
} else {
DNPRINTF(XT_D_HISTORY, "%s: keeping %s (age %.1f)\n",
__func__, h->uri, age);
}
}
}
return (0);
}
int
insert_history_item(const gchar *uri, const gchar *title, time_t time)
{
struct history *h;
if (!(uri && strlen(uri) && title && strlen(title)))
return (1);
h = g_malloc(sizeof(struct history));
h->uri = g_strdup(uri);
h->title = g_strdup(title);
h->time = 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);
return (0);
}
int
restore_global_history(void)
{
char file[PATH_MAX];
FILE *f;
gchar *uri, *title = NULL, *stime = NULL, *err = NULL;
time_t time;
struct tm tm;
const char delim[3] = {'\\', '\\', '\0'};
snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_HISTORY_FILE);
if ((f = fopen(file, "r")) == NULL) {
warnx("%s: fopen", __func__);
return (1);
}
for (;;) {
if ((uri = fparseln(f, NULL, NULL, delim, 0)) == NULL)
if (feof(f) || ferror(f))
break;
if ((title = fparseln(f, NULL, NULL, delim, 0)) == NULL)
if (feof(f) || ferror(f)) {
err = "broken history file (title)";
goto done;
}
if ((stime = fparseln(f, NULL, NULL, delim, 0)) == NULL)
if (feof(f) || ferror(f)) {
err = "broken history file (time)";
goto done;
}
if (strptime(stime, "%a %b %d %H:%M:%S %Y", &tm) == NULL) {
err = "strptime failed to parse time";
goto done;
}
time = mktime(&tm);
if (insert_history_item(uri, title, time)) {
err = "failed to insert item";
goto done;
}
free(uri);
free(title);
free(stime);
uri = NULL;
title = NULL;
stime = NULL;
}
done:
if (err && strlen(err)) {
warnx("%s: %s\n", __func__, err);
free(uri);
free(title);
free(stime);
return (1);
}
return (0);
}
int
save_global_history_to_disk(struct tab *t)
{
char file[PATH_MAX];
FILE *f;
struct history *h;
snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_HISTORY_FILE);
if ((f = fopen(file, "w")) == NULL) {
show_oops(t, "%s: global history file: %s",
__func__, strerror(errno));
return (1);
}
RB_FOREACH_REVERSE(h, history_list, &hl) {
if (h->uri && h->title && h->time)
fprintf(f, "%s\n%s\n%s", h->uri, h->title,
ctime(&h->time));
}
fclose(f);
return (0);
}
/*
* Marshall the internal record of visited URIs into a Javascript hash table in
* string form.
*/
char *
color_visited_helper(void)
{
char *d, *s = NULL, *t;
struct history *h;
RB_FOREACH_REVERSE(h, history_list, &hl) {
if (s == NULL)
s = g_strdup_printf("'%s':'dummy'", h->uri);
else {
d = g_strdup_printf("'%s':'dummy'", h->uri);
t = g_strjoin(",", s, d, NULL);
g_free(d);
g_free(s);
s = t;
}
}
t = g_strdup_printf("{%s}", s);
g_free(s);
s = t;
DNPRINTF(XT_D_VISITED, "%s: s = %s\n", __func__, s);
return (s);
}
int
color_visited(struct tab *t, char *visited)
{
char *s, *v;
if (t == NULL || visited == NULL) {
show_oops(NULL, "%s: invalid parameters", __func__);
return (1);
}
/*
* Create a string representing an annonymous Javascript function, which
* takes a hash table of visited URIs as an argument, goes through the
* links at the current web page and colors them if they indeed been
* visited.
*/
v = g_strdup_printf("(%s);", visited),
s = g_strconcat(
"(function(visitedUris) {",
" for (var i = 0; i < document.links.length; i++)",
" if (visitedUris[document.links[i].href])",
" document.links[i].style.color = 'purple';",
"})",
v, NULL);
run_script(t, s);
g_free(s);
g_free(v);
g_free(visited);
return (0);
}