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