Don't color bar when warning of cached cert mismatch
[xombrero.git] / xombrero.c
blob1795ff707bbba765a6140916a3baee07de49b8c9
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, 2012 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 * Copyright (c) 2012, 2013 Josh Rickmar <jrick@devio.us>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <xombrero.h>
24 #include "version.h"
26 char *version = XOMBRERO_VERSION;
28 #ifdef XT_DEBUG
29 uint32_t swm_debug = 0
30 | XT_D_MOVE
31 | XT_D_KEY
32 | XT_D_TAB
33 | XT_D_URL
34 | XT_D_CMD
35 | XT_D_NAV
36 | XT_D_DOWNLOAD
37 | XT_D_CONFIG
38 | XT_D_JS
39 | XT_D_FAVORITE
40 | XT_D_PRINTING
41 | XT_D_COOKIE
42 | XT_D_KEYBINDING
43 | XT_D_CLIP
44 | XT_D_BUFFERCMD
45 | XT_D_INSPECTOR
46 | XT_D_VISITED
47 | XT_D_HISTORY
49 #endif
51 char *icons[] = {
52 "xombreroicon16.png",
53 "xombreroicon32.png",
54 "xombreroicon48.png",
55 "xombreroicon64.png",
56 "xombreroicon128.png"
59 struct session {
60 TAILQ_ENTRY(session) entry;
61 const gchar *name;
63 TAILQ_HEAD(session_list, session);
65 struct undo {
66 TAILQ_ENTRY(undo) entry;
67 gchar *uri;
68 GList *history;
69 int back; /* Keeps track of how many back
70 * history items there are. */
72 TAILQ_HEAD(undo_tailq, undo);
74 struct command_entry {
75 char *line;
76 TAILQ_ENTRY(command_entry) entry;
78 TAILQ_HEAD(command_list, command_entry);
80 /* defines */
81 #define XT_CACHE_DIR ("cache")
82 #define XT_CERT_DIR ("certs")
83 #define XT_CERT_CACHE_DIR ("certs_cache")
84 #define XT_JS_DIR ("js")
85 #define XT_SESSIONS_DIR ("sessions")
86 #define XT_TEMP_DIR ("tmp")
87 #define XT_QMARKS_FILE ("quickmarks")
88 #define XT_SAVED_TABS_FILE ("main_session")
89 #define XT_RESTART_TABS_FILE ("restart_tabs")
90 #define XT_SOCKET_FILE ("socket")
91 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
92 #define XT_SEARCH_FILE ("search_history")
93 #define XT_COMMAND_FILE ("command_history")
94 #define XT_DLMAN_REFRESH "10"
95 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
96 #define XT_MAX_UNDO_CLOSE_TAB (32)
97 #define XT_PRINT_EXTRA_MARGIN 10
98 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
99 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
100 #define XT_MAX_CERTS (32)
102 /* colors */
103 #define XT_COLOR_RED "#cc0000"
104 #define XT_COLOR_YELLOW "#ffff66"
105 #define XT_COLOR_BLUE "lightblue"
106 #define XT_COLOR_GREEN "#99ff66"
107 #define XT_COLOR_WHITE "white"
108 #define XT_COLOR_BLACK "black"
110 #define XT_COLOR_CT_BACKGROUND "#000000"
111 #define XT_COLOR_CT_INACTIVE "#dddddd"
112 #define XT_COLOR_CT_ACTIVE "#bbbb00"
113 #define XT_COLOR_CT_SEPARATOR "#555555"
115 #define XT_COLOR_SB_SEPARATOR "#555555"
117 /* CSS element names */
118 #define XT_CSS_NORMAL ""
119 #define XT_CSS_RED "red"
120 #define XT_CSS_YELLOW "yellow"
121 #define XT_CSS_GREEN "green"
122 #define XT_CSS_BLUE "blue"
123 #define XT_CSS_HIDDEN "hidden"
124 #define XT_CSS_ACTIVE "active"
126 #define XT_PROTO_DELIM "://"
128 /* actions */
129 #define XT_MOVE_INVALID (0)
130 #define XT_MOVE_DOWN (1)
131 #define XT_MOVE_UP (2)
132 #define XT_MOVE_BOTTOM (3)
133 #define XT_MOVE_TOP (4)
134 #define XT_MOVE_PAGEDOWN (5)
135 #define XT_MOVE_PAGEUP (6)
136 #define XT_MOVE_HALFDOWN (7)
137 #define XT_MOVE_HALFUP (8)
138 #define XT_MOVE_LEFT (9)
139 #define XT_MOVE_FARLEFT (10)
140 #define XT_MOVE_RIGHT (11)
141 #define XT_MOVE_FARRIGHT (12)
142 #define XT_MOVE_PERCENT (13)
143 #define XT_MOVE_CENTER (14)
145 #define XT_QMARK_SET (0)
146 #define XT_QMARK_OPEN (1)
147 #define XT_QMARK_TAB (2)
149 #define XT_MARK_SET (0)
150 #define XT_MARK_GOTO (1)
152 #define XT_GO_UP_ROOT (999)
154 #define XT_NAV_INVALID (0)
155 #define XT_NAV_BACK (1)
156 #define XT_NAV_FORWARD (2)
157 #define XT_NAV_RELOAD (3)
158 #define XT_NAV_STOP (4)
160 #define XT_FOCUS_INVALID (0)
161 #define XT_FOCUS_URI (1)
162 #define XT_FOCUS_SEARCH (2)
164 #define XT_SEARCH_INVALID (0)
165 #define XT_SEARCH_NEXT (1)
166 #define XT_SEARCH_PREV (2)
168 #define XT_PASTE_CURRENT_TAB (0)
169 #define XT_PASTE_NEW_TAB (1)
171 #define XT_ZOOM_IN (-1)
172 #define XT_ZOOM_OUT (-2)
173 #define XT_ZOOM_NORMAL (100)
175 #define XT_SES_DONOTHING (0)
176 #define XT_SES_CLOSETABS (1)
178 #define XT_PREFIX (1<<0)
179 #define XT_USERARG (1<<1)
180 #define XT_URLARG (1<<2)
181 #define XT_INTARG (1<<3)
182 #define XT_SESSARG (1<<4)
183 #define XT_SETARG (1<<5)
185 #define XT_HINT_NEWTAB (1<<0)
187 #define XT_BUFCMD_SZ (8)
189 #define XT_EJS_SHOW (1<<0)
191 GtkWidget * create_button(const char *, const char *, int);
193 void recalc_tabs(void);
194 void recolor_compact_tabs(void);
195 void set_current_tab(int page_num);
196 gboolean update_statusbar_position(GtkAdjustment*, gpointer);
197 void marks_clear(struct tab *t);
199 /* globals */
200 extern char *__progname;
201 char * const *start_argv;
202 struct passwd *pwd;
203 GtkWidget *main_window;
204 GtkNotebook *notebook;
205 GtkWidget *tab_bar;
206 GtkWidget *tab_bar_box;
207 GtkWidget *arrow, *abtn;
208 GdkEvent *fevent = NULL;
209 struct tab_list tabs;
210 struct history_list hl;
211 int hl_purge_count = 0;
212 struct session_list sessions;
213 struct wl_list c_wl;
214 struct wl_list js_wl;
215 struct wl_list pl_wl;
216 struct wl_list force_https;
217 struct wl_list svil;
218 struct strict_transport_tree st_tree;
219 struct undo_tailq undos;
220 struct keybinding_list kbl;
221 struct sp_list spl;
222 struct user_agent_list ua_list;
223 struct http_accept_list ha_list;
224 struct domain_id_list di_list;
225 struct cmd_alias_list cal;
226 struct custom_uri_list cul;
227 struct command_list chl;
228 struct command_list shl;
229 struct command_entry *history_at;
230 struct command_entry *search_at;
231 struct secviolation_list svl;
232 struct set_reject_list srl;
233 int undo_count;
234 int cmd_history_count = 0;
235 int search_history_count = 0;
236 char *global_search;
237 uint64_t blocked_cookies = 0;
238 char named_session[PATH_MAX];
239 GtkListStore *completion_model;
240 GtkListStore *buffers_store;
241 char *stylesheet;
243 char *qmarks[XT_NOQMARKS];
244 int btn_down; /* M1 down in any wv */
245 regex_t url_re; /* guess_search regex */
247 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
248 int next_download_id = 1;
250 void xxx_dir(char *);
251 int icon_size_map(int);
252 void activate_uri_entry_cb(GtkWidget*, struct tab *);
254 void
255 history_delete(struct command_list *l, int *counter)
257 struct command_entry *c;
259 if (l == NULL || counter == NULL)
260 return;
262 c = TAILQ_LAST(l, command_list);
263 if (c == NULL)
264 return;
266 TAILQ_REMOVE(l, c, entry);
267 *counter -= 1;
268 g_free(c->line);
269 g_free(c);
272 void
273 history_add(struct command_list *list, char *file, char *l, int *counter)
275 struct command_entry *c;
276 FILE *f;
278 if (list == NULL || l == NULL || counter == NULL)
279 return;
281 /* don't add the same line */
282 c = TAILQ_FIRST(list);
283 if (c)
284 if (!strcmp(c->line + 1 /* skip space */, l))
285 return;
287 c = g_malloc0(sizeof *c);
288 c->line = g_strdup_printf(" %s", l);
290 *counter += 1;
291 TAILQ_INSERT_HEAD(list, c, entry);
293 if (*counter > 1000)
294 history_delete(list, counter);
296 if (history_autosave && file) {
297 f = fopen(file, "w");
298 if (f == NULL) {
299 show_oops(NULL, "couldn't write history %s", file);
300 return;
303 TAILQ_FOREACH_REVERSE(c, list, command_list, entry) {
304 c->line[0] = ' ';
305 fprintf(f, "%s\n", c->line);
308 fclose(f);
313 history_read(struct command_list *list, char *file, int *counter)
315 FILE *f;
316 char *s, line[65536];
318 if (list == NULL || file == NULL)
319 return (1);
321 f = fopen(file, "r");
322 if (f == NULL) {
323 startpage_add("couldn't open history file %s", file);
324 return (1);
327 for (;;) {
328 s = fgets(line, sizeof line, f);
329 if (s == NULL || feof(f) || ferror(f))
330 break;
331 if ((s = strchr(line, '\n')) == NULL) {
332 startpage_add("invalid history file %s", file);
333 fclose(f);
334 return (1);
336 *s = '\0';
338 history_add(list, NULL, line + 1, counter);
341 fclose(f);
343 return (0);
346 /* marks array storage. */
347 char
348 indextomark(int i)
350 if (i < 0 || i >= XT_NOMARKS)
351 return (0);
353 return XT_MARKS[i];
357 marktoindex(char m)
359 char *ret;
361 if ((ret = strchr(XT_MARKS, m)) != NULL)
362 return ret - XT_MARKS;
364 return (-1);
367 /* quickmarks array storage. */
368 char
369 indextoqmark(int i)
371 if (i < 0 || i >= XT_NOQMARKS)
372 return (0);
374 return XT_QMARKS[i];
378 qmarktoindex(char m)
380 char *ret;
382 if ((ret = strchr(XT_QMARKS, m)) != NULL)
383 return ret - XT_QMARKS;
385 return (-1);
389 is_g_object_setting(GObject *o, char *str)
391 guint n_props = 0, i;
392 GParamSpec **proplist;
393 int rv = 0;
395 if (!G_IS_OBJECT(o))
396 return (0);
398 proplist = g_object_class_list_properties(G_OBJECT_GET_CLASS(o),
399 &n_props);
401 for (i = 0; i < n_props; i++) {
402 if (! strcmp(proplist[i]->name, str)) {
403 rv = 1;
404 break;
408 g_free(proplist);
409 return (rv);
412 struct tab *
413 get_current_tab(void)
415 struct tab *t;
417 TAILQ_FOREACH(t, &tabs, entry) {
418 if (t->tab_id == gtk_notebook_get_current_page(notebook))
419 return (t);
422 warnx("%s: no current tab", __func__);
424 return (NULL);
428 set_ssl_ca_file(struct settings *s, char *file)
430 struct stat sb;
432 if (file == NULL || strlen(file) == 0)
433 return (-1);
434 if (stat(file, &sb)) {
435 warnx("no CA file: %s", file);
436 return (-1);
438 expand_tilde(ssl_ca_file, sizeof ssl_ca_file, file);
439 g_object_set(session,
440 SOUP_SESSION_SSL_CA_FILE, ssl_ca_file,
441 SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
442 (void *)NULL);
443 return (0);
446 void
447 set_status(struct tab *t, gchar *fmt, ...)
449 va_list ap;
450 gchar *status;
452 va_start(ap, fmt);
454 status = g_strdup_vprintf(fmt, ap);
456 gtk_entry_set_text(GTK_ENTRY(t->sbe.uri), status);
458 if (!t->status)
459 t->status = status;
460 else if (strcmp(t->status, status)) {
461 /* set new status */
462 g_free(t->status);
463 t->status = status;
464 } else
465 g_free(status);
467 va_end(ap);
470 void
471 hide_cmd(struct tab *t)
473 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
475 history_at = NULL; /* just in case */
476 search_at = NULL; /* just in case */
477 gtk_widget_set_can_focus(t->cmd, FALSE);
478 gtk_widget_hide(t->cmd);
481 void
482 show_cmd(struct tab *t, const char *s)
484 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
486 /* without this you can't middle click in t->cmd to paste */
487 gtk_entry_set_text(GTK_ENTRY(t->cmd), "");
489 history_at = NULL;
490 search_at = NULL;
491 gtk_widget_hide(t->oops);
492 gtk_widget_set_can_focus(t->cmd, TRUE);
493 gtk_widget_show(t->cmd);
495 gtk_widget_grab_focus(GTK_WIDGET(t->cmd));
496 #if GTK_CHECK_VERSION(3, 0, 0)
497 gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
498 #else
499 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
500 &t->default_style->base[GTK_STATE_NORMAL]);
501 #endif
502 gtk_entry_set_text(GTK_ENTRY(t->cmd), s);
503 gtk_editable_set_position(GTK_EDITABLE(t->cmd), -1);
506 void
507 hide_buffers(struct tab *t)
509 gtk_widget_hide(t->buffers);
510 gtk_widget_set_can_focus(t->buffers, FALSE);
511 gtk_list_store_clear(buffers_store);
514 enum {
515 COL_ID = 0,
516 COL_FAVICON,
517 COL_TITLE,
518 NUM_COLS
522 sort_tabs_by_page_num(struct tab ***stabs)
524 int num_tabs = 0;
525 struct tab *t;
527 num_tabs = gtk_notebook_get_n_pages(notebook);
529 *stabs = g_malloc0(num_tabs * sizeof(struct tab *));
531 TAILQ_FOREACH(t, &tabs, entry)
532 (*stabs)[gtk_notebook_page_num(notebook, t->vbox)] = t;
534 return (num_tabs);
537 void
538 buffers_make_list(void)
540 GtkTreeIter iter;
541 const gchar *title = NULL;
542 struct tab **stabs = NULL;
543 int i, num_tabs;
545 num_tabs = sort_tabs_by_page_num(&stabs);
547 for (i = 0; i < num_tabs; i++)
548 if (stabs[i]) {
549 gtk_list_store_append(buffers_store, &iter);
550 title = get_title(stabs[i], FALSE);
551 gtk_list_store_set(buffers_store, &iter,
552 COL_ID, i + 1, /* Enumerate the tabs starting from 1
553 * rather than 0. */
554 COL_FAVICON, gtk_image_get_pixbuf
555 (GTK_IMAGE(stabs[i]->tab_elems.favicon)),
556 COL_TITLE, title,
557 -1);
560 g_free(stabs);
563 void
564 show_buffers(struct tab *t)
566 GtkTreeIter iter;
567 GtkTreeSelection *sel;
568 GtkTreePath *path;
569 int index;
571 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)))
572 return;
574 buffers_make_list();
576 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(t->buffers));
577 index = gtk_notebook_get_current_page(notebook);
578 path = gtk_tree_path_new_from_indices(index, -1);
579 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter, path))
580 gtk_tree_selection_select_iter(sel, &iter);
581 gtk_tree_path_free(path);
583 gtk_widget_show(t->buffers);
584 gtk_widget_set_can_focus(t->buffers, TRUE);
585 gtk_widget_grab_focus(GTK_WIDGET(t->buffers));
588 void
589 toggle_buffers(struct tab *t)
591 if (gtk_widget_get_visible(t->buffers))
592 hide_buffers(t);
593 else
594 show_buffers(t);
598 buffers(struct tab *t, struct karg *args)
600 show_buffers(t);
602 return (0);
606 set_scrollbar_visibility(struct tab *t, int visible)
608 #if GTK_CHECK_VERSION(3, 0, 0)
609 GtkWidget *h_scrollbar, *v_scrollbar;
611 h_scrollbar = gtk_scrolled_window_get_hscrollbar(
612 GTK_SCROLLED_WINDOW(t->browser_win));
613 v_scrollbar = gtk_scrolled_window_get_vscrollbar(
614 GTK_SCROLLED_WINDOW(t->browser_win));
616 if (visible == 0) {
617 gtk_widget_set_name(h_scrollbar, XT_CSS_HIDDEN);
618 gtk_widget_set_name(v_scrollbar, XT_CSS_HIDDEN);
619 } else {
620 gtk_widget_set_name(h_scrollbar, "");
621 gtk_widget_set_name(v_scrollbar, "");
624 return (0);
625 #else
626 return (visible == 0);
627 #endif
630 void
631 hide_oops(struct tab *t)
633 gtk_widget_hide(t->oops);
636 void
637 show_oops(struct tab *at, const char *fmt, ...)
639 va_list ap;
640 char *msg = NULL;
641 struct tab *t = NULL;
643 if (fmt == NULL)
644 return;
646 if (at == NULL) {
647 if ((t = get_current_tab()) == NULL)
648 return;
649 } else
650 t = at;
652 va_start(ap, fmt);
653 if ((msg = g_strdup_vprintf(fmt, ap)) == NULL)
654 errx(1, "show_oops failed");
655 va_end(ap);
657 gtk_entry_set_text(GTK_ENTRY(t->oops), msg);
658 gtk_widget_hide(t->cmd);
659 gtk_widget_show(t->oops);
661 if (msg)
662 g_free(msg);
665 char work_dir[PATH_MAX];
666 char certs_dir[PATH_MAX];
667 char certs_cache_dir[PATH_MAX];
668 char js_dir[PATH_MAX];
669 char cache_dir[PATH_MAX];
670 char sessions_dir[PATH_MAX];
671 char temp_dir[PATH_MAX];
672 char cookie_file[PATH_MAX];
673 char *strict_transport_file = NULL;
674 SoupSession *session;
675 SoupCookieJar *s_cookiejar;
676 SoupCookieJar *p_cookiejar;
677 char rc_fname[PATH_MAX];
679 struct mime_type_list mtl;
680 struct alias_list aliases;
682 /* protos */
683 struct tab *create_new_tab(const char *, struct undo *, int, int);
684 void delete_tab(struct tab *);
685 void setzoom_webkit(struct tab *, int);
686 int download_rb_cmp(struct download *, struct download *);
687 gboolean cmd_execute(struct tab *t, char *str);
690 history_rb_cmp(struct history *h1, struct history *h2)
692 return (strcmp(h1->uri, h2->uri));
694 RB_GENERATE(history_list, history, entry, history_rb_cmp);
697 download_rb_cmp(struct download *e1, struct download *e2)
699 return (e1->id < e2->id ? -1 : e1->id > e2->id);
701 RB_GENERATE(download_list, download, entry, download_rb_cmp);
704 secviolation_rb_cmp(struct secviolation *s1, struct secviolation *s2)
706 return (s1->xtp_arg < s2->xtp_arg ? -1 : s1->xtp_arg > s2->xtp_arg);
708 RB_GENERATE(secviolation_list, secviolation, entry, secviolation_rb_cmp);
711 user_agent_rb_cmp(struct user_agent *ua1, struct user_agent *ua2)
713 return (ua1->id < ua2->id ? -1 : ua1->id > ua2->id);
715 RB_GENERATE(user_agent_list, user_agent, entry, user_agent_rb_cmp);
718 http_accept_rb_cmp(struct http_accept *ha1, struct http_accept *ha2)
720 return (ha1->id < ha2->id ? -1 : ha1->id > ha2->id);
722 RB_GENERATE(http_accept_list, http_accept, entry, http_accept_rb_cmp);
725 domain_id_rb_cmp(struct domain_id *d1, struct domain_id *d2)
727 return (strcmp(d1->domain, d2->domain));
729 RB_GENERATE(domain_id_list, domain_id, entry, domain_id_rb_cmp);
731 struct valid_url_types {
732 char *type;
733 } vut[] = {
734 { "http://" },
735 { "https://" },
736 { "ftp://" },
737 { "file://" },
738 { XT_URI_ABOUT },
739 { XT_XTP_STR },
743 valid_url_type(const char *url)
745 int i;
747 for (i = 0; i < LENGTH(vut); i++)
748 if (!strncasecmp(vut[i].type, url, strlen(vut[i].type)))
749 return (0);
751 return (1);
754 char *
755 match_alias(const char *url_in)
757 struct alias *a;
758 char *arg;
759 char *url_out = NULL, *search, *enc_arg;
760 char **sv;
762 search = g_strdup(url_in);
763 arg = search;
764 if (strsep(&arg, " \t") == NULL) {
765 show_oops(NULL, "match_alias: NULL URL");
766 goto done;
769 TAILQ_FOREACH(a, &aliases, entry) {
770 if (!strcmp(search, a->a_name))
771 break;
774 if (a != NULL) {
775 DNPRINTF(XT_D_URL, "match_alias: matched alias %s\n",
776 a->a_name);
777 if (arg == NULL)
778 arg = "";
779 enc_arg = soup_uri_encode(arg, XT_RESERVED_CHARS);
780 sv = g_strsplit(a->a_uri, "%s", 2);
781 if (arg != NULL)
782 url_out = g_strjoinv(enc_arg, sv);
783 else
784 url_out = g_strjoinv("", sv);
785 g_free(enc_arg);
786 g_strfreev(sv);
788 done:
789 g_free(search);
790 return (url_out);
793 char *
794 guess_url_type(const char *url_in)
796 struct stat sb;
797 char cwd[PATH_MAX] = {0};
798 char *url_out = NULL, *enc_search = NULL;
799 char *path = NULL;
800 char **sv = NULL;
801 int i;
804 /* substitute aliases */
805 url_out = match_alias(url_in);
806 if (url_out != NULL)
807 return (url_out);
809 /* see if we are an about page */
810 if (!strncmp(url_in, XT_URI_ABOUT, XT_URI_ABOUT_LEN))
811 for (i = 0; i < about_list_size(); i++)
812 if (!strcmp(&url_in[XT_URI_ABOUT_LEN],
813 about_list[i].name)) {
814 url_out = g_strdup(url_in);
815 goto done;
818 if (guess_search && url_regex &&
819 !(g_str_has_prefix(url_in, "http://") ||
820 g_str_has_prefix(url_in, "https://"))) {
821 if (regexec(&url_re, url_in, 0, NULL, 0)) {
822 /* invalid URI so search instead */
823 enc_search = soup_uri_encode(url_in, XT_RESERVED_CHARS);
824 sv = g_strsplit(search_string, "%s", 2);
825 url_out = g_strjoinv(enc_search, sv);
826 g_free(enc_search);
827 g_strfreev(sv);
828 goto done;
832 /* XXX not sure about this heuristic */
833 if (stat(url_in, &sb) == 0) {
834 if (url_in[0] == '/')
835 url_out = g_filename_to_uri(url_in, NULL, NULL);
836 else {
837 if (getcwd(cwd, PATH_MAX) != NULL) {
838 path = g_strdup_printf("%s" PS "%s", cwd,
839 url_in);
840 url_out = g_filename_to_uri(path, NULL, NULL);
841 g_free(path);
844 } else
845 url_out = g_strdup_printf("http://%s", url_in); /* guess http */
846 done:
847 DNPRINTF(XT_D_URL, "guess_url_type: guessed %s\n", url_out);
849 return (url_out);
852 void
853 set_normal_tab_meaning(struct tab *t)
855 if (t == NULL)
856 return;
858 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
859 if (t->session_key != NULL) {
860 g_free(t->session_key);
861 t->session_key = NULL;
865 void
866 load_uri(struct tab *t, const gchar *uri)
868 struct karg args;
869 gchar *newuri = NULL;
870 int i;
872 if (uri == NULL)
873 return;
875 /* Strip leading spaces. */
876 while (*uri && isspace(*uri))
877 uri++;
879 if (strlen(uri) == 0) {
880 blank(t, NULL);
881 return;
884 set_normal_tab_meaning(t);
886 if (valid_url_type(uri)) {
887 newuri = guess_url_type(uri);
888 uri = newuri;
891 /* clear :cert show host */
892 if (t->about_cert_host) {
893 g_free(t->about_cert_host);
894 t->about_cert_host = NULL;
897 if (!strncmp(uri, XT_URI_ABOUT, XT_URI_ABOUT_LEN)) {
898 for (i = 0; i < about_list_size(); i++)
899 if (!strcmp(&uri[XT_URI_ABOUT_LEN], about_list[i].name) &&
900 about_list[i].func != NULL) {
901 bzero(&args, sizeof args);
902 about_list[i].func(t, &args);
903 gtk_widget_set_sensitive(GTK_WIDGET(t->stop),
904 FALSE);
905 goto done;
907 show_oops(t, "invalid about page");
908 goto done;
911 /* remove old HTTPS cert chain (if any) */
912 if (t->pem) {
913 g_free(t->pem);
914 t->pem = NULL;
917 set_status(t, "Loading: %s", (char *)uri);
918 marks_clear(t);
919 webkit_web_view_load_uri(t->wv, uri);
920 done:
921 if (newuri)
922 g_free(newuri);
925 const gchar *
926 get_uri(struct tab *t)
928 const gchar *uri = NULL;
930 if (webkit_web_view_get_load_status(t->wv) == WEBKIT_LOAD_FAILED &&
931 !t->download_requested)
932 return (NULL);
933 if (t->xtp_meaning == XT_XTP_TAB_MEANING_NORMAL)
934 uri = webkit_web_view_get_uri(t->wv);
935 else {
936 /* use tmp_uri to make sure it is g_freed */
937 if (t->tmp_uri)
938 g_free(t->tmp_uri);
939 t->tmp_uri = g_strdup_printf("%s%s", XT_URI_ABOUT,
940 about_list[t->xtp_meaning].name);
941 uri = t->tmp_uri;
943 return (uri);
946 const gchar *
947 get_title(struct tab *t, bool window)
949 const gchar *set = NULL, *title = NULL;
950 WebKitLoadStatus status;
952 status = webkit_web_view_get_load_status(t->wv);
953 if (status == WEBKIT_LOAD_PROVISIONAL ||
954 (status == WEBKIT_LOAD_FAILED && !t->download_requested) ||
955 t->xtp_meaning == XT_XTP_TAB_MEANING_BL)
956 goto notitle;
958 title = webkit_web_view_get_title(t->wv);
959 if ((set = title ? title : get_uri(t)))
960 return set;
962 notitle:
963 set = window ? XT_NAME : "(untitled)";
965 return set;
968 struct mime_type *
969 find_mime_type(char *mime_type)
971 struct mime_type *m, *def = NULL, *rv = NULL;
973 TAILQ_FOREACH(m, &mtl, entry) {
974 if (m->mt_default &&
975 !strncmp(mime_type, m->mt_type, strlen(m->mt_type)))
976 def = m;
978 if (m->mt_default == 0 && !strcmp(mime_type, m->mt_type)) {
979 rv = m;
980 break;
984 if (rv == NULL)
985 rv = def;
987 return (rv);
991 * This only escapes the & and < characters, as per the discussion found here:
992 * http://lists.apple.com/archives/Webkitsdk-dev/2007/May/msg00056.html
994 char *
995 html_escape(const char *val)
997 char *s, *sp;
998 char **sv;
1000 if (val == NULL)
1001 return NULL;
1003 sv = g_strsplit(val, "&", -1);
1004 s = g_strjoinv("&amp;", sv);
1005 g_strfreev(sv);
1006 sp = s;
1007 sv = g_strsplit(val, "<", -1);
1008 s = g_strjoinv("&lt;", sv);
1009 g_strfreev(sv);
1010 g_free(sp);
1011 return (s);
1014 struct wl_entry *
1015 wl_find_uri(const gchar *s, struct wl_list *wl)
1017 int i;
1018 char *ss;
1019 struct wl_entry *w;
1021 if (s == NULL || wl == NULL)
1022 return (NULL);
1024 if (!strncmp(s, "http://", strlen("http://")))
1025 s = &s[strlen("http://")];
1026 else if (!strncmp(s, "https://", strlen("https://")))
1027 s = &s[strlen("https://")];
1029 if (strlen(s) < 2)
1030 return (NULL);
1032 for (i = 0; i < strlen(s) + 1 /* yes er need this */; i++)
1033 /* chop string at first slash */
1034 if (s[i] == '/' || s[i] == ':' || s[i] == '\0') {
1035 ss = g_strdup(s);
1036 ss[i] = '\0';
1037 w = wl_find(ss, wl);
1038 g_free(ss);
1039 return (w);
1042 return (NULL);
1045 char *
1046 js_ref_to_string(JSContextRef context, JSValueRef ref)
1048 char *s = NULL;
1049 size_t l;
1050 JSStringRef jsref;
1052 jsref = JSValueToStringCopy(context, ref, NULL);
1053 if (jsref == NULL)
1054 return (NULL);
1056 l = JSStringGetMaximumUTF8CStringSize(jsref);
1057 s = g_malloc(l);
1058 if (s)
1059 JSStringGetUTF8CString(jsref, s, l);
1060 JSStringRelease(jsref);
1062 return (s);
1065 #define XT_JS_DONE ("done;")
1066 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
1067 #define XT_JS_INSERT ("insert;")
1068 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
1071 run_script(struct tab *t, char *s)
1073 JSGlobalContextRef ctx;
1074 WebKitWebFrame *frame;
1075 JSStringRef str;
1076 JSValueRef val, exception;
1077 char *es;
1079 DNPRINTF(XT_D_JS, "%s: tab %d %s\n", __func__,
1080 t->tab_id, s == (char *)JS_HINTING ? "JS_HINTING" : s);
1082 frame = webkit_web_view_get_main_frame(t->wv);
1083 ctx = webkit_web_frame_get_global_context(frame);
1085 str = JSStringCreateWithUTF8CString(s);
1086 val = JSEvaluateScript(ctx, str, JSContextGetGlobalObject(ctx),
1087 NULL, 0, &exception);
1088 JSStringRelease(str);
1090 DNPRINTF(XT_D_JS, "%s: val %p\n", __func__, val);
1091 if (val == NULL) {
1092 es = js_ref_to_string(ctx, exception);
1093 if (es) {
1094 DNPRINTF(XT_D_JS, "%s: exception %s\n", __func__, es);
1095 g_free(es);
1097 return (1);
1098 } else {
1099 es = js_ref_to_string(ctx, val);
1100 #if 0
1101 /* return values */
1102 if (!strncmp(es, XT_JS_DONE, XT_JS_DONE_LEN))
1103 ; /* do nothing */
1104 if (!strncmp(es, XT_JS_INSERT, XT_JS_INSERT_LEN))
1105 ; /* do nothing */
1106 #endif
1107 if (es) {
1108 DNPRINTF(XT_D_JS, "%s: val %s\n", __func__, es);
1109 g_free(es);
1113 return (0);
1116 void
1117 enable_hints(struct tab *t)
1119 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1121 if (t->new_tab)
1122 run_script(t, "hints.createHints('', 'F');");
1123 else
1124 run_script(t, "hints.createHints('', 'f');");
1125 t->mode = XT_MODE_HINT;
1128 void
1129 disable_hints(struct tab *t)
1131 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1133 run_script(t, "hints.clearHints();");
1134 t->mode = XT_MODE_COMMAND;
1135 t->new_tab = 0;
1139 passthrough(struct tab *t, struct karg *args)
1141 t->mode = XT_MODE_PASSTHROUGH;
1142 return (0);
1146 modurl(struct tab *t, struct karg *args)
1148 const gchar *uri = NULL;
1149 char *u = NULL;
1151 /* XXX kind of a bad hack, but oh well */
1152 if (gtk_widget_has_focus(t->uri_entry)) {
1153 if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) &&
1154 (strlen(uri))) {
1155 u = g_strdup_printf("www.%s.com", uri);
1156 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), u);
1157 g_free(u);
1158 activate_uri_entry_cb(t->uri_entry, t);
1161 return (0);
1165 hint(struct tab *t, struct karg *args)
1168 DNPRINTF(XT_D_JS, "hint: tab %d args %d\n", t->tab_id, args->i);
1170 if (t->mode == XT_MODE_HINT) {
1171 if (args->i == XT_HINT_NEWTAB)
1172 t->new_tab = 1;
1173 enable_hints(t);
1174 } else
1175 disable_hints(t);
1177 return (0);
1180 void
1181 apply_style(struct tab *t)
1183 t->styled = 1;
1184 g_object_set(G_OBJECT(t->settings),
1185 "user-stylesheet-uri", t->stylesheet, (char *)NULL);
1188 void
1189 remove_style(struct tab *t)
1191 t->styled = 0;
1192 g_object_set(G_OBJECT(t->settings),
1193 "user-stylesheet-uri", NULL, (char *)NULL);
1197 userstyle_cmd(struct tab *t, struct karg *args)
1199 char script[PATH_MAX] = {'\0'};
1200 char *script_uri;
1201 struct tab *tt;
1203 DNPRINTF(XT_D_JS, "userstyle_cmd: tab %d\n", t->tab_id);
1205 if (args->s != NULL && strlen(args->s)) {
1206 expand_tilde(script, sizeof script, args->s);
1207 script_uri = g_filename_to_uri(script, NULL, NULL);
1208 } else
1209 script_uri = g_strdup(userstyle);
1211 if (script_uri == NULL)
1212 return (1);
1214 switch (args->i) {
1215 case XT_STYLE_CURRENT_TAB:
1216 if (t->styled && !strcmp(script_uri, t->stylesheet))
1217 remove_style(t);
1218 else {
1219 if (t->stylesheet)
1220 g_free(t->stylesheet);
1221 t->stylesheet = g_strdup(script_uri);
1222 apply_style(t);
1224 break;
1225 case XT_STYLE_GLOBAL:
1226 if (userstyle_global && !strcmp(script_uri, t->stylesheet)) {
1227 userstyle_global = 0;
1228 TAILQ_FOREACH(tt, &tabs, entry)
1229 remove_style(tt);
1230 } else {
1231 userstyle_global = 1;
1233 /* need to save this stylesheet for new tabs */
1234 if (stylesheet)
1235 g_free(stylesheet);
1236 stylesheet = g_strdup(script_uri);
1238 TAILQ_FOREACH(tt, &tabs, entry) {
1239 if (tt->stylesheet)
1240 g_free(tt->stylesheet);
1241 tt->stylesheet = g_strdup(script_uri);
1242 apply_style(tt);
1245 break;
1248 g_free(script_uri);
1250 return (0);
1254 quit(struct tab *t, struct karg *args)
1256 if (save_global_history)
1257 save_global_history_to_disk(t);
1259 gtk_main_quit();
1261 return (1);
1264 void
1265 restore_sessions_list(void)
1267 DIR *sdir = NULL;
1268 struct dirent *dp = NULL;
1269 struct session *s;
1270 int reg;
1272 sdir = opendir(sessions_dir);
1273 if (sdir) {
1274 while ((dp = readdir(sdir)) != NULL) {
1275 #if defined __MINGW32__
1276 reg = 1; /* windows only has regular files */
1277 #else
1278 reg = dp->d_type == DT_REG;
1279 #endif
1280 if (dp && reg) {
1281 s = g_malloc(sizeof(struct session));
1282 s->name = g_strdup(dp->d_name);
1283 TAILQ_INSERT_TAIL(&sessions, s, entry);
1286 closedir(sdir);
1291 open_tabs(struct tab *t, struct karg *a)
1293 char file[PATH_MAX];
1294 FILE *f = NULL;
1295 char *uri = NULL;
1296 int rv = 1;
1297 struct tab *ti, *tt;
1299 if (a == NULL)
1300 goto done;
1302 ti = TAILQ_LAST(&tabs, tab_list);
1304 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, a->s);
1305 if ((f = fopen(file, "r")) == NULL)
1306 goto done;
1308 for (;;) {
1309 if ((uri = fparseln(f, NULL, NULL, "\0\0\0", 0)) == NULL) {
1310 if (feof(f) || ferror(f))
1311 break;
1312 } else {
1313 /* retrieve session name */
1314 if (g_str_has_prefix(uri, XT_SAVE_SESSION_ID)) {
1315 strlcpy(named_session,
1316 &uri[strlen(XT_SAVE_SESSION_ID)],
1317 sizeof named_session);
1318 continue;
1321 if (strlen(uri))
1322 create_new_tab(uri, NULL, 1, -1);
1324 free(uri);
1328 /* close open tabs */
1329 if (a->i == XT_SES_CLOSETABS && ti != NULL) {
1330 for (;;) {
1331 tt = TAILQ_FIRST(&tabs);
1332 if (tt == NULL)
1333 break;
1334 if (tt != ti) {
1335 delete_tab(tt);
1336 continue;
1338 delete_tab(tt);
1339 break;
1343 rv = 0;
1344 done:
1345 if (f)
1346 fclose(f);
1348 return (rv);
1352 restore_saved_tabs(void)
1354 char file[PATH_MAX];
1355 int unlink_file = 0;
1356 struct stat sb;
1357 struct karg a;
1358 int rv = 0;
1360 snprintf(file, sizeof file, "%s" PS "%s",
1361 sessions_dir, XT_RESTART_TABS_FILE);
1362 if (stat(file, &sb) == -1)
1363 a.s = XT_SAVED_TABS_FILE;
1364 else {
1365 unlink_file = 1;
1366 a.s = XT_RESTART_TABS_FILE;
1369 a.i = XT_SES_DONOTHING;
1370 rv = open_tabs(NULL, &a);
1372 if (unlink_file)
1373 unlink(file);
1375 return (rv);
1379 save_tabs(struct tab *t, struct karg *a)
1381 char file[PATH_MAX];
1382 FILE *f;
1383 int num_tabs = 0, i;
1384 struct tab **stabs = NULL;
1386 /* tab may be null here */
1388 if (a == NULL)
1389 return (1);
1390 if (a->s == NULL)
1391 snprintf(file, sizeof file, "%s" PS "%s",
1392 sessions_dir, named_session);
1393 else
1394 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, a->s);
1396 if ((f = fopen(file, "w")) == NULL) {
1397 show_oops(t, "Can't open save_tabs file: %s", strerror(errno));
1398 return (1);
1401 /* save session name */
1402 fprintf(f, "%s%s\n", XT_SAVE_SESSION_ID, named_session);
1404 /* Save tabs, in the order they are arranged in the notebook. */
1405 num_tabs = sort_tabs_by_page_num(&stabs);
1407 for (i = 0; i < num_tabs; i++)
1408 if (stabs[i]) {
1409 if (get_uri(stabs[i]) != NULL)
1410 fprintf(f, "%s\n", get_uri(stabs[i]));
1411 else if (gtk_entry_get_text(GTK_ENTRY(
1412 stabs[i]->uri_entry)))
1413 fprintf(f, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1414 stabs[i]->uri_entry)));
1417 g_free(stabs);
1419 /* try and make sure this gets to disk NOW. XXX Backup first? */
1420 if (fflush(f) != 0 || fsync(fileno(f)) != 0) {
1421 show_oops(t, "May not have managed to save session: %s",
1422 strerror(errno));
1425 fclose(f);
1427 return (0);
1431 save_tabs_and_quit(struct tab *t, struct karg *args)
1433 struct karg a;
1435 a.s = NULL;
1436 save_tabs(t, &a);
1437 quit(t, NULL);
1439 return (1);
1442 void
1443 expand_tilde(char *path, size_t len, const char *s)
1445 struct passwd *pwd;
1446 int i;
1447 char user[LOGIN_NAME_MAX];
1448 const char *sc = s;
1450 if (path == NULL || s == NULL)
1451 errx(1, "expand_tilde");
1453 if (s[0] != '~') {
1454 strlcpy(path, sc, len);
1455 return;
1458 ++s;
1459 for (i = 0; s[i] != PSC && s[i] != '\0'; ++i)
1460 user[i] = s[i];
1461 user[i] = '\0';
1462 s = &s[i];
1464 pwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1465 if (pwd == NULL)
1466 strlcpy(path, sc, len);
1467 else
1468 snprintf(path, len, "%s%s", pwd->pw_dir, s);
1472 run_page_script(struct tab *t, struct karg *args)
1474 const gchar *uri;
1475 char *tmp, script[PATH_MAX];
1476 char *sv[3];
1478 tmp = args->s != NULL && strlen(args->s) > 0 ? args->s : default_script;
1479 if (tmp[0] == '\0') {
1480 show_oops(t, "no script specified");
1481 return (1);
1484 if ((uri = get_uri(t)) == NULL) {
1485 show_oops(t, "tab is empty, not running script");
1486 return (1);
1489 expand_tilde(script, sizeof script, tmp);
1491 sv[0] = script;
1492 sv[1] = (char *)uri;
1493 sv[2] = NULL;
1494 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
1495 NULL, NULL)) {
1496 show_oops(t, "%s: could not spawn process: %s %s", __func__,
1497 sv[0], sv[1]);
1498 return (1);
1499 } else
1500 show_oops(t, "running: %s %s", sv[0], sv[1]);
1502 return (0);
1506 yank_uri(struct tab *t, struct karg *args)
1508 const gchar *uri;
1509 GtkClipboard *clipboard, *primary;
1511 if ((uri = get_uri(t)) == NULL)
1512 return (1);
1514 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1515 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1516 gtk_clipboard_set_text(primary, uri, -1);
1517 gtk_clipboard_set_text(clipboard, uri, -1);
1519 return (0);
1523 paste_uri(struct tab *t, struct karg *args)
1525 GtkClipboard *clipboard, *primary;
1526 gchar *c = NULL, *p = NULL, *uri;
1527 int i;
1529 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1530 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1531 c = gtk_clipboard_wait_for_text(clipboard);
1532 p = gtk_clipboard_wait_for_text(primary);
1534 if (c || p) {
1535 #ifdef __MINGW32__
1536 /* Windows try clipboard first */
1537 uri = c ? c : p;
1538 #else
1539 /* UNIX try primary first */
1540 uri = p ? p : c;
1541 #endif
1542 /* replace all newlines with spaces */
1543 for (i = 0; uri[i] != '\0'; ++i)
1544 if (uri[i] == '\n')
1545 uri[i] = ' ';
1547 while (*uri && isspace(*uri))
1548 uri++;
1549 if (strlen(uri) == 0) {
1550 show_oops(t, "empty paste buffer");
1551 goto done;
1553 if (guess_search == 0 && valid_url_type(uri)) {
1554 /* we can be clever and paste this in search box */
1555 show_oops(t, "not a valid URL");
1556 goto done;
1559 if (args->i == XT_PASTE_CURRENT_TAB)
1560 load_uri(t, uri);
1561 else if (args->i == XT_PASTE_NEW_TAB)
1562 create_new_tab(uri, NULL, 1, -1);
1565 done:
1566 if (c)
1567 g_free(c);
1568 if (p)
1569 g_free(p);
1571 return (0);
1574 void
1575 js_toggle_cb(GtkWidget *w, struct tab *t)
1577 struct karg a;
1578 int es, set;
1580 g_object_get(G_OBJECT(t->settings),
1581 "enable-scripts", &es, (char *)NULL);
1582 es = !es;
1583 if (es)
1584 set = XT_WL_ENABLE;
1585 else
1586 set = XT_WL_DISABLE;
1588 a.i = set | XT_WL_TOPLEVEL;
1589 toggle_pl(t, &a);
1591 a.i = set | XT_WL_TOPLEVEL;
1592 toggle_cwl(t, &a);
1594 a.i = XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD;
1595 toggle_js(t, &a);
1598 void
1599 proxy_toggle_cb(GtkWidget *w, struct tab *t)
1601 struct karg args = {0};
1603 args.i = XT_PRXY_TOGGLE;
1604 proxy_cmd(t, &args);
1608 toggle_src(struct tab *t, struct karg *args)
1610 gboolean mode;
1612 if (t == NULL)
1613 return (0);
1615 mode = webkit_web_view_get_view_source_mode(t->wv);
1616 webkit_web_view_set_view_source_mode(t->wv, !mode);
1617 webkit_web_view_reload(t->wv);
1619 return (0);
1622 void
1623 focus_webview(struct tab *t)
1625 if (t == NULL)
1626 return;
1628 /* only grab focus if we are visible */
1629 if (gtk_notebook_get_current_page(notebook) == t->tab_id)
1630 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
1634 focus(struct tab *t, struct karg *args)
1636 if (t == NULL || args == NULL)
1637 return (1);
1639 if (show_url == 0)
1640 return (0);
1642 if (args->i == XT_FOCUS_URI)
1643 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
1644 else if (args->i == XT_FOCUS_SEARCH)
1645 gtk_widget_grab_focus(GTK_WIDGET(t->search_entry));
1647 return (0);
1650 void
1651 free_connection_certs(gnutls_x509_crt_t *certs, size_t cert_count)
1653 int i;
1655 for (i = 0; i < cert_count; i++)
1656 gnutls_x509_crt_deinit(certs[i]);
1657 gnutls_free(certs);
1660 #if GTK_CHECK_VERSION(3, 0, 0)
1661 void
1662 statusbar_modify_attr(struct tab *t, const char *css_name)
1664 gtk_widget_set_name(t->sbe.ebox, css_name);
1666 #else
1667 void
1668 statusbar_modify_attr(struct tab *t, const char *text, const char *base)
1670 GdkColor c_text, c_base;
1672 gdk_color_parse(text, &c_text);
1673 gdk_color_parse(base, &c_base);
1675 gtk_widget_modify_bg(t->sbe.ebox, GTK_STATE_NORMAL, &c_base);
1676 gtk_widget_modify_base(t->sbe.uri, GTK_STATE_NORMAL, &c_base);
1677 gtk_widget_modify_text(t->sbe.uri, GTK_STATE_NORMAL, &c_text);
1679 #endif
1681 void
1682 save_certs(struct tab *t, gnutls_x509_crt_t *certs,
1683 size_t cert_count, const char *domain, const char *dir)
1685 size_t cert_buf_sz;
1686 char file[PATH_MAX];
1687 char *cert_buf = NULL;
1688 int rv;
1689 int i;
1690 FILE *f;
1692 if (t == NULL || certs == NULL || cert_count <= 0 || domain == NULL)
1693 return;
1695 snprintf(file, sizeof file, "%s" PS "%s", dir, domain);
1696 if ((f = fopen(file, "w")) == NULL) {
1697 show_oops(t, "Can't create cert file %s %s",
1698 file, strerror(errno));
1699 return;
1702 for (i = 0; i < cert_count; i++) {
1704 * Because we support old crap and can't use
1705 * gnutls_x509_crt_export2(), we intentionally use an empty
1706 * buffer and then make a second call with the known size
1707 * needed.
1709 cert_buf_sz = 0;
1710 rv = gnutls_x509_crt_export(certs[i], GNUTLS_X509_FMT_PEM,
1711 cert_buf, &cert_buf_sz);
1712 if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER) {
1713 cert_buf = gnutls_malloc(cert_buf_sz * sizeof(char));
1714 if (cert_buf == NULL) {
1715 show_oops(t, "gnutls_x509_crt_export failed");
1716 goto done;
1718 rv = gnutls_x509_crt_export(certs[i],
1719 GNUTLS_X509_FMT_PEM, cert_buf, &cert_buf_sz);
1721 if (rv != 0) {
1722 show_oops(t, "gnutls_x509_crt_export failure: %s",
1723 gnutls_strerror(rv));
1724 goto done;
1726 cert_buf[cert_buf_sz] = '\0';
1727 if (fwrite(cert_buf, cert_buf_sz, 1, f) != 1) {
1728 show_oops(t, "Can't write certs: %s", strerror(errno));
1729 goto done;
1731 gnutls_free(cert_buf);
1732 cert_buf = NULL;
1735 done:
1736 if (cert_buf)
1737 gnutls_free(cert_buf);
1738 fclose(f);
1741 enum cert_trust {
1742 CERT_LOCAL,
1743 CERT_TRUSTED,
1744 CERT_UNTRUSTED,
1745 CERT_BAD
1748 gnutls_x509_crt_t *
1749 get_local_cert_chain(const char *uri, size_t *ncerts, const char **error_str,
1750 const char *dir)
1752 SoupURI *su;
1753 unsigned char cert_buf[64 * 1024] = {0};
1754 gnutls_datum_t data;
1755 unsigned int max_certs = XT_MAX_CERTS;
1756 int bytes_read;
1757 char file[PATH_MAX];
1758 FILE *f;
1759 gnutls_x509_crt_t *certs;
1761 if ((su = soup_uri_new(uri)) == NULL) {
1762 *error_str = "Invalid URI";
1763 return (NULL);
1766 snprintf(file, sizeof file, "%s" PS "%s", dir, su->host);
1767 soup_uri_free(su);
1768 if ((f = fopen(file, "r")) == NULL) {
1769 *error_str = "Could not read local cert";
1770 return (NULL);
1773 bytes_read = fread(cert_buf, sizeof *cert_buf, sizeof cert_buf, f);
1774 if (bytes_read == 0) {
1775 *error_str = "Could not read local cert";
1776 return (NULL);
1779 certs = gnutls_malloc(sizeof(*certs) * max_certs);
1780 if (certs == NULL) {
1781 *error_str = "Error allocating memory";
1782 return (NULL);
1784 data.data = cert_buf;
1785 data.size = bytes_read;
1786 if (gnutls_x509_crt_list_import(certs, &max_certs, &data,
1787 GNUTLS_X509_FMT_PEM, 0) < 0) {
1788 gnutls_free(certs);
1789 *error_str = "Error reading local cert chain";
1790 return (NULL);
1793 *ncerts = max_certs;
1794 return (certs);
1798 * This uses the pem-encoded cert chain saved in t->pem instead of
1799 * grabbing the remote cert. We save it beforehand and read it here
1800 * so as to not open a side channel that ignores proxy settings.
1802 gnutls_x509_crt_t *
1803 get_chain_for_pem(char *pem, size_t *ncerts, const char **error_str)
1805 gnutls_datum_t data;
1806 unsigned int max_certs = XT_MAX_CERTS;
1807 gnutls_x509_crt_t *certs;
1809 if (pem == NULL) {
1810 *error_str = "Error reading remote cert chain";
1811 return (NULL);
1814 certs = gnutls_malloc(sizeof(*certs) * max_certs);
1815 if (certs == NULL) {
1816 *error_str = "Error allocating memory";
1817 return (NULL);
1819 data.data = (unsigned char *)pem;
1820 data.size = strlen(pem);
1821 if (gnutls_x509_crt_list_import(certs, &max_certs, &data,
1822 GNUTLS_X509_FMT_PEM, 0) < 0) {
1823 gnutls_free(certs);
1824 *error_str = "Error reading remote cert chain";
1825 return (NULL);
1828 *ncerts = max_certs;
1829 return (certs);
1834 cert_cmd(struct tab *t, struct karg *args)
1836 const gchar *uri, *error_str = NULL;
1837 size_t cert_count;
1838 gnutls_x509_crt_t *certs;
1839 SoupURI *su;
1840 char *host;
1841 #if !GTK_CHECK_VERSION(3, 0, 0)
1842 GdkColor color;
1843 #endif
1845 if (t == NULL)
1846 return (1);
1848 if (args->s != NULL) {
1849 uri = args->s;
1850 } else if ((uri = get_uri(t)) == NULL) {
1851 show_oops(t, "Invalid URI");
1852 return (1);
1854 if ((su = soup_uri_new(uri)) == NULL) {
1855 show_oops(t, "Invalid URI");
1856 return (1);
1860 * if we're only showing the local certs, don't open a socket and get
1861 * the remote certs
1863 if (args->i & XT_SHOW && args->i & XT_CACHE) {
1864 certs = get_local_cert_chain(uri, &cert_count, &error_str,
1865 certs_cache_dir);
1866 if (error_str == NULL) {
1867 t->about_cert_host = g_strdup(su->host);
1868 show_certs(t, certs, cert_count, "Certificate Chain");
1869 free_connection_certs(certs, cert_count);
1870 } else {
1871 show_oops(t, "%s", error_str);
1872 soup_uri_free(su);
1873 return (1);
1875 soup_uri_free(su);
1876 return (0);
1879 certs = get_chain_for_pem(t->pem, &cert_count, &error_str);
1880 if (error_str)
1881 goto done;
1883 if (args->i & XT_SHOW) {
1884 t->about_cert_host = g_strdup(su->host);
1885 show_certs(t, certs, cert_count, "Certificate Chain");
1886 } else if (args->i & XT_SAVE) {
1887 host = t->about_cert_host ? t->about_cert_host : su->host;
1888 save_certs(t, certs, cert_count, host, certs_dir);
1889 #if GTK_CHECK_VERSION(3, 0, 0)
1890 gtk_widget_set_name(t->uri_entry, XT_CSS_BLUE);
1891 statusbar_modify_attr(t, XT_CSS_BLUE);
1892 #else
1893 gdk_color_parse(XT_COLOR_BLUE, &color);
1894 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
1895 statusbar_modify_attr(t, XT_COLOR_BLACK, XT_COLOR_BLUE);
1896 #endif
1897 } else if (args->i & XT_CACHE)
1898 save_certs(t, certs, cert_count, su->host, certs_cache_dir);
1900 free_connection_certs(certs, cert_count);
1902 done:
1903 soup_uri_free(su);
1905 if (error_str && strlen(error_str))
1906 show_oops(t, "%s", error_str);
1907 return (0);
1911 * Checks whether the remote cert is identical to the local saved
1912 * cert. Returns CERT_LOCAL if unchanged, CERT_UNTRUSTED if local
1913 * cert does not exist, and CERT_BAD if different.
1915 * Saves entire cert chain in pem encoding to chain for it to be
1916 * cached later, if needed.
1918 enum cert_trust
1919 check_local_certs(const char *file, GTlsCertificate *cert, char **chain)
1921 char r_cert_buf[64 * 1024];
1922 FILE *f;
1923 GTlsCertificate *tmpcert = NULL;
1924 GTlsCertificate *issuer = NULL;
1925 char *pem = NULL;
1926 char *tmp = NULL;
1927 enum cert_trust rv = CERT_LOCAL;
1928 int i;
1930 if ((f = fopen(file, "r")) == NULL) {
1931 /* no local cert to check */
1932 rv = CERT_UNTRUSTED;
1935 for (i = 0;;) {
1936 if (i == 0) {
1937 g_object_get(G_OBJECT(cert), "certificate-pem", &pem,
1938 NULL);
1939 g_object_get(G_OBJECT(cert), "issuer", &issuer, NULL);
1940 } else {
1941 if (issuer == NULL)
1942 break;
1943 g_object_get(G_OBJECT(tmpcert), "issuer", &issuer,
1944 NULL);
1945 if (issuer == NULL)
1946 break;
1948 g_object_get(G_OBJECT(tmpcert), "certificate-pem", &pem,
1949 NULL);
1951 i++;
1953 if (tmpcert != NULL)
1954 g_object_unref(G_OBJECT(tmpcert));
1955 tmpcert = issuer;
1956 if (f) {
1957 if (fread(r_cert_buf, strlen(pem), 1, f) != 1 && !feof(f))
1958 rv = CERT_BAD;
1959 if (bcmp(r_cert_buf, pem, strlen(pem)))
1960 rv = CERT_BAD;
1962 tmp = g_strdup_printf("%s%s", *chain, pem);
1963 g_free(pem);
1964 g_free(*chain);
1965 *chain = tmp;
1968 if (issuer != NULL)
1969 g_object_unref(G_OBJECT(issuer));
1970 if (f != NULL)
1971 fclose(f);
1972 return (rv);
1976 check_cert_changes(struct tab *t, GTlsCertificate *cert, const char *file, const char *uri)
1978 SoupURI *soupuri = NULL;
1979 struct karg args = {0};
1980 struct wl_entry *w = NULL;
1981 char *chain = NULL;
1982 int ret = 0;
1984 chain = g_strdup("");
1985 switch(check_local_certs(file, cert, &chain)) {
1986 case CERT_LOCAL:
1987 /* The cached certificate is identical */
1988 break;
1989 case CERT_TRUSTED: /* FALLTHROUGH */
1990 case CERT_UNTRUSTED:
1991 /* cache new certificate */
1992 args.i = XT_CACHE;
1993 cert_cmd(t, &args);
1994 break;
1995 case CERT_BAD:
1996 if ((soupuri = soup_uri_new(uri)) == NULL ||
1997 soupuri->host == NULL)
1998 break;
1999 if ((w = wl_find(soupuri->host, &svil)) != NULL)
2000 break;
2001 t->xtp_meaning = XT_XTP_TAB_MEANING_SV;
2002 args.s = g_strdup((char *)uri);
2003 xtp_page_sv(t, &args);
2004 ret = 1;
2005 break;
2008 if (soupuri)
2009 soup_uri_free(soupuri);
2010 if (chain)
2011 g_free(chain);
2012 return (ret);
2016 remove_cookie(int index)
2018 int i, rv = 1;
2019 GSList *cf;
2020 SoupCookie *c;
2022 DNPRINTF(XT_D_COOKIE, "remove_cookie: %d\n", index);
2024 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2026 for (i = 1; cf; cf = cf->next, i++) {
2027 if (i != index)
2028 continue;
2029 c = cf->data;
2030 print_cookie("remove cookie", c);
2031 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2032 rv = 0;
2033 break;
2036 soup_cookies_free(cf);
2038 return (rv);
2042 remove_cookie_domain(int domain_id)
2044 int domain_count, rv = 1;
2045 GSList *cf;
2046 SoupCookie *c;
2047 char *last_domain;
2049 DNPRINTF(XT_D_COOKIE, "remove_cookie_domain: %d\n", domain_id);
2051 last_domain = "";
2052 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2054 for (domain_count = 0; cf; cf = cf->next) {
2055 c = cf->data;
2057 if (strcmp(last_domain, c->domain) != 0) {
2058 domain_count++;
2059 last_domain = c->domain;
2062 if (domain_count < domain_id)
2063 continue;
2064 else if (domain_count > domain_id)
2065 break;
2067 print_cookie("remove cookie", c);
2068 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2069 rv = 0;
2072 soup_cookies_free(cf);
2074 return (rv);
2078 remove_cookie_all()
2080 int rv = 1;
2081 GSList *cf;
2082 SoupCookie *c;
2084 DNPRINTF(XT_D_COOKIE, "remove_cookie_all\n");
2086 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2088 for (; cf; cf = cf->next) {
2089 c = cf->data;
2091 print_cookie("remove cookie", c);
2092 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2093 rv = 0;
2096 soup_cookies_free(cf);
2098 return (rv);
2102 toplevel_cmd(struct tab *t, struct karg *args)
2104 js_toggle_cb(t->js_toggle, t);
2106 return (0);
2110 can_go_back_for_real(struct tab *t)
2112 int i;
2113 WebKitWebHistoryItem *item;
2114 const gchar *uri;
2116 if (t == NULL)
2117 return (FALSE);
2119 if (t->item != NULL)
2120 return (TRUE);
2122 /* rely on webkit to make sure we can go backward when on an about page */
2123 uri = get_uri(t);
2124 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2125 g_str_has_prefix(uri, "xxxt://"))
2126 return (webkit_web_view_can_go_back(t->wv));
2128 /* the back/forward list is stupid so help determine if we can go back */
2129 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2130 item != NULL;
2131 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2132 if (strcmp(webkit_web_history_item_get_uri(item), uri))
2133 return (TRUE);
2136 return (FALSE);
2140 can_go_forward_for_real(struct tab *t)
2142 int i;
2143 WebKitWebHistoryItem *item;
2144 const gchar *uri;
2146 if (t == NULL)
2147 return (FALSE);
2149 /* rely on webkit to make sure we can go forward when on an about page */
2150 uri = get_uri(t);
2151 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2152 g_str_has_prefix(uri, "xxxt://"))
2153 return (webkit_web_view_can_go_forward(t->wv));
2155 /* the back/forwars list is stupid so help selecting a different item */
2156 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2157 item != NULL;
2158 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2159 if (strcmp(webkit_web_history_item_get_uri(item), uri))
2160 return (TRUE);
2163 return (FALSE);
2166 void
2167 go_back_for_real(struct tab *t)
2169 int i;
2170 WebKitWebHistoryItem *item;
2171 const gchar *uri;
2173 if (t == NULL)
2174 return;
2176 uri = get_uri(t);
2177 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2178 g_str_has_prefix(uri, "xxxt://")) {
2179 webkit_web_view_go_back(t->wv);
2180 return;
2182 /* the back/forwars list is stupid so help selecting a different item */
2183 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2184 item != NULL;
2185 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2186 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2187 webkit_web_view_go_to_back_forward_item(t->wv, item);
2188 break;
2193 void
2194 go_forward_for_real(struct tab *t)
2196 int i;
2197 WebKitWebHistoryItem *item;
2198 const gchar *uri;
2200 if (t == NULL)
2201 return;
2203 uri = get_uri(t);
2204 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2205 g_str_has_prefix(uri, "xxxt://")) {
2206 webkit_web_view_go_forward(t->wv);
2207 return;
2209 /* the back/forwars list is stupid so help selecting a different item */
2210 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2211 item != NULL;
2212 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2213 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2214 webkit_web_view_go_to_back_forward_item(t->wv, item);
2215 break;
2221 navaction(struct tab *t, struct karg *args)
2223 WebKitWebHistoryItem *item = NULL;
2224 WebKitWebFrame *frame;
2226 DNPRINTF(XT_D_NAV, "navaction: tab %d opcode %d\n",
2227 t->tab_id, args->i);
2229 hide_oops(t);
2230 set_normal_tab_meaning(t);
2231 if (t->item) {
2232 if (args->i == XT_NAV_BACK)
2233 item = webkit_web_back_forward_list_get_current_item(t->bfl);
2234 else
2235 item = webkit_web_back_forward_list_get_forward_item(t->bfl);
2238 switch (args->i) {
2239 case XT_NAV_BACK:
2240 if (t->item) {
2241 if (item == NULL)
2242 return (XT_CB_PASSTHROUGH);
2243 webkit_web_view_go_to_back_forward_item(t->wv, item);
2244 t->item = NULL;
2245 return (XT_CB_PASSTHROUGH);
2247 marks_clear(t);
2248 go_back_for_real(t);
2249 break;
2250 case XT_NAV_FORWARD:
2251 if (t->item) {
2252 if (item == NULL)
2253 return (XT_CB_PASSTHROUGH);
2254 webkit_web_view_go_to_back_forward_item(t->wv, item);
2255 t->item = NULL;
2256 return (XT_CB_PASSTHROUGH);
2258 marks_clear(t);
2259 go_forward_for_real(t);
2260 break;
2261 case XT_NAV_RELOAD:
2262 frame = webkit_web_view_get_main_frame(t->wv);
2263 webkit_web_frame_reload(frame);
2264 break;
2265 case XT_NAV_STOP:
2266 frame = webkit_web_view_get_main_frame(t->wv);
2267 webkit_web_frame_stop_loading(frame);
2268 break;
2270 return (XT_CB_PASSTHROUGH);
2274 move(struct tab *t, struct karg *args)
2276 GtkAdjustment *adjust;
2277 double pi, si, pos, ps, upper, lower, max;
2278 double percent;
2280 switch (args->i) {
2281 case XT_MOVE_DOWN:
2282 case XT_MOVE_UP:
2283 case XT_MOVE_BOTTOM:
2284 case XT_MOVE_TOP:
2285 case XT_MOVE_PAGEDOWN:
2286 case XT_MOVE_PAGEUP:
2287 case XT_MOVE_HALFDOWN:
2288 case XT_MOVE_HALFUP:
2289 case XT_MOVE_PERCENT:
2290 case XT_MOVE_CENTER:
2291 adjust = t->adjust_v;
2292 break;
2293 default:
2294 adjust = t->adjust_h;
2295 break;
2298 pos = gtk_adjustment_get_value(adjust);
2299 ps = gtk_adjustment_get_page_size(adjust);
2300 upper = gtk_adjustment_get_upper(adjust);
2301 lower = gtk_adjustment_get_lower(adjust);
2302 si = gtk_adjustment_get_step_increment(adjust);
2303 pi = gtk_adjustment_get_page_increment(adjust);
2304 max = upper - ps;
2306 DNPRINTF(XT_D_MOVE, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2307 "max %f si %f pi %f\n",
2308 args->i, adjust == t->adjust_h ? "horizontal" : "vertical",
2309 pos, ps, upper, lower, max, si, pi);
2311 switch (args->i) {
2312 case XT_MOVE_DOWN:
2313 case XT_MOVE_RIGHT:
2314 pos += si;
2315 gtk_adjustment_set_value(adjust, MIN(pos, max));
2316 break;
2317 case XT_MOVE_UP:
2318 case XT_MOVE_LEFT:
2319 pos -= si;
2320 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2321 break;
2322 case XT_MOVE_BOTTOM:
2323 case XT_MOVE_FARRIGHT:
2324 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2325 gtk_adjustment_set_value(adjust, max);
2326 break;
2327 case XT_MOVE_TOP:
2328 case XT_MOVE_FARLEFT:
2329 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2330 gtk_adjustment_set_value(adjust, lower);
2331 break;
2332 case XT_MOVE_PAGEDOWN:
2333 pos += pi;
2334 gtk_adjustment_set_value(adjust, MIN(pos, max));
2335 break;
2336 case XT_MOVE_PAGEUP:
2337 pos -= pi;
2338 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2339 break;
2340 case XT_MOVE_HALFDOWN:
2341 pos += pi / 2;
2342 gtk_adjustment_set_value(adjust, MIN(pos, max));
2343 break;
2344 case XT_MOVE_HALFUP:
2345 pos -= pi / 2;
2346 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2347 break;
2348 case XT_MOVE_CENTER:
2349 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2350 args->s = g_strdup("50.0");
2351 /* FALLTHROUGH */
2352 case XT_MOVE_PERCENT:
2353 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2354 percent = atoi(args->s) / 100.0;
2355 pos = max * percent;
2356 if (pos < 0.0 || pos > max)
2357 break;
2358 gtk_adjustment_set_value(adjust, pos);
2359 break;
2360 default:
2361 return (XT_CB_PASSTHROUGH);
2364 DNPRINTF(XT_D_MOVE, "move: new pos %f %f\n", pos, MIN(pos, max));
2366 return (XT_CB_HANDLED);
2369 void
2370 url_set_visibility(void)
2372 struct tab *t;
2374 TAILQ_FOREACH(t, &tabs, entry)
2375 if (show_url == 0) {
2376 gtk_widget_hide(t->toolbar);
2377 focus_webview(t);
2378 } else
2379 gtk_widget_show(t->toolbar);
2382 void
2383 notebook_tab_set_visibility(void)
2385 if (show_tabs == 0) {
2386 gtk_widget_hide(tab_bar);
2387 gtk_notebook_set_show_tabs(notebook, FALSE);
2388 } else {
2389 if (tab_style == XT_TABS_NORMAL) {
2390 gtk_widget_hide(tab_bar);
2391 gtk_notebook_set_show_tabs(notebook, TRUE);
2392 } else if (tab_style == XT_TABS_COMPACT) {
2393 gtk_widget_show(tab_bar);
2394 gtk_notebook_set_show_tabs(notebook, FALSE);
2399 void
2400 statusbar_set_visibility(void)
2402 struct tab *t;
2404 TAILQ_FOREACH(t, &tabs, entry){
2405 if (show_statusbar == 0)
2406 gtk_widget_hide(t->statusbar);
2407 else
2408 gtk_widget_show(t->statusbar);
2410 focus_webview(t);
2414 void
2415 url_set(struct tab *t, int enable_url_entry)
2417 GdkPixbuf *pixbuf;
2418 int progress;
2420 show_url = enable_url_entry;
2422 if (enable_url_entry) {
2423 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
2424 GTK_ENTRY_ICON_PRIMARY, NULL);
2425 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri), 0);
2426 } else {
2427 pixbuf = gtk_entry_get_icon_pixbuf(GTK_ENTRY(t->uri_entry),
2428 GTK_ENTRY_ICON_PRIMARY);
2429 progress =
2430 gtk_entry_get_progress_fraction(GTK_ENTRY(t->uri_entry));
2431 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
2432 GTK_ENTRY_ICON_PRIMARY, pixbuf);
2433 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
2434 progress);
2439 fullscreen(struct tab *t, struct karg *args)
2441 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2443 if (t == NULL)
2444 return (XT_CB_PASSTHROUGH);
2446 if (show_url == 0) {
2447 url_set(t, 1);
2448 show_tabs = 1;
2449 } else {
2450 url_set(t, 0);
2451 show_tabs = 0;
2454 url_set_visibility();
2455 notebook_tab_set_visibility();
2457 return (XT_CB_HANDLED);
2461 statustoggle(struct tab *t, struct karg *args)
2463 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2465 if (show_statusbar == 1) {
2466 show_statusbar = 0;
2467 statusbar_set_visibility();
2468 } else if (show_statusbar == 0) {
2469 show_statusbar = 1;
2470 statusbar_set_visibility();
2472 return (XT_CB_HANDLED);
2476 urlaction(struct tab *t, struct karg *args)
2478 int rv = XT_CB_HANDLED;
2480 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2482 if (t == NULL)
2483 return (XT_CB_PASSTHROUGH);
2485 switch (args->i) {
2486 case XT_URL_SHOW:
2487 if (show_url == 0) {
2488 url_set(t, 1);
2489 url_set_visibility();
2491 break;
2492 case XT_URL_HIDE:
2493 if (show_url == 1) {
2494 url_set(t, 0);
2495 url_set_visibility();
2497 break;
2499 return (rv);
2503 tabaction(struct tab *t, struct karg *args)
2505 int rv = XT_CB_HANDLED;
2506 char *url = args->s;
2507 struct undo *u;
2508 struct tab *tt, *tv;
2510 DNPRINTF(XT_D_TAB, "tabaction: %p %d\n", t, args->i);
2512 if (t == NULL)
2513 return (XT_CB_PASSTHROUGH);
2515 switch (args->i) {
2516 case XT_TAB_NEW:
2517 if (strlen(url) > 0)
2518 create_new_tab(url, NULL, 1, args->precount);
2519 else
2520 create_new_tab(NULL, NULL, 1, args->precount);
2521 break;
2522 case XT_TAB_DELETE:
2523 if (args->precount < 0)
2524 delete_tab(t);
2525 else
2526 TAILQ_FOREACH(tt, &tabs, entry)
2527 if (tt->tab_id == args->precount - 1) {
2528 delete_tab(tt);
2529 break;
2531 break;
2532 case XT_TAB_DELQUIT:
2533 if (gtk_notebook_get_n_pages(notebook) > 1)
2534 delete_tab(t);
2535 else
2536 quit(t, args);
2537 break;
2538 case XT_TAB_ONLY:
2539 TAILQ_FOREACH_SAFE(tt, &tabs, entry, tv)
2540 if (t != tt)
2541 delete_tab(tt);
2542 break;
2543 case XT_TAB_OPEN:
2544 if (strlen(url) > 0)
2546 else {
2547 rv = XT_CB_PASSTHROUGH;
2548 goto done;
2550 load_uri(t, url);
2551 break;
2552 case XT_TAB_SHOW:
2553 if (show_tabs == 0) {
2554 show_tabs = 1;
2555 notebook_tab_set_visibility();
2557 break;
2558 case XT_TAB_HIDE:
2559 if (show_tabs == 1) {
2560 show_tabs = 0;
2561 notebook_tab_set_visibility();
2563 break;
2564 case XT_TAB_NEXTSTYLE:
2565 if (tab_style == XT_TABS_NORMAL) {
2566 tab_style = XT_TABS_COMPACT;
2567 recolor_compact_tabs();
2569 else
2570 tab_style = XT_TABS_NORMAL;
2571 notebook_tab_set_visibility();
2572 break;
2573 case XT_TAB_UNDO_CLOSE:
2574 if (undo_count == 0) {
2575 DNPRINTF(XT_D_TAB, "%s: no tabs to undo close",
2576 __func__);
2577 goto done;
2578 } else {
2579 undo_count--;
2580 u = TAILQ_FIRST(&undos);
2581 create_new_tab(u->uri, u, 1, -1);
2583 TAILQ_REMOVE(&undos, u, entry);
2584 g_free(u->uri);
2585 /* u->history is freed in create_new_tab() */
2586 g_free(u);
2588 break;
2589 case XT_TAB_LOAD_IMAGES:
2591 if (!auto_load_images) {
2593 /* Enable auto-load images (this will load all
2594 * previously unloaded images). */
2595 g_object_set(G_OBJECT(t->settings),
2596 "auto-load-images", TRUE, (char *)NULL);
2597 webkit_web_view_set_settings(t->wv, t->settings);
2599 webkit_web_view_reload(t->wv);
2601 /* Webkit triggers an event when we change the setting,
2602 * so we can't disable the auto-loading at once.
2604 * Unfortunately, webkit does not tell us when it's done.
2605 * Instead, we wait until the next request, and then
2606 * disable autoloading again.
2608 t->load_images = TRUE;
2610 break;
2611 default:
2612 rv = XT_CB_PASSTHROUGH;
2613 goto done;
2616 done:
2617 if (args->s) {
2618 g_free(args->s);
2619 args->s = NULL;
2622 return (rv);
2626 resizetab(struct tab *t, struct karg *args)
2628 if (t == NULL || args == NULL) {
2629 show_oops(NULL, "resizetab invalid parameters");
2630 return (XT_CB_PASSTHROUGH);
2633 DNPRINTF(XT_D_TAB, "resizetab: tab %d %d\n",
2634 t->tab_id, args->i);
2636 setzoom_webkit(t, args->i);
2638 return (XT_CB_HANDLED);
2642 movetab(struct tab *t, struct karg *args)
2644 int n, dest;
2646 if (t == NULL || args == NULL) {
2647 show_oops(NULL, "movetab invalid parameters");
2648 return (XT_CB_PASSTHROUGH);
2651 DNPRINTF(XT_D_TAB, "movetab: tab %d opcode %d\n",
2652 t->tab_id, args->i);
2654 if (args->i >= XT_TAB_INVALID)
2655 return (XT_CB_PASSTHROUGH);
2657 if (TAILQ_EMPTY(&tabs))
2658 return (XT_CB_PASSTHROUGH);
2660 n = gtk_notebook_get_n_pages(notebook);
2661 dest = gtk_notebook_get_current_page(notebook);
2663 switch (args->i) {
2664 case XT_TAB_NEXT:
2665 if (args->precount < 0)
2666 dest = dest == n - 1 ? 0 : dest + 1;
2667 else
2668 dest = args->precount - 1;
2670 break;
2671 case XT_TAB_PREV:
2672 if (args->precount < 0)
2673 dest -= 1;
2674 else
2675 dest -= args->precount % n;
2677 if (dest < 0)
2678 dest += n;
2680 break;
2681 case XT_TAB_FIRST:
2682 dest = 0;
2683 break;
2684 case XT_TAB_LAST:
2685 dest = n - 1;
2686 break;
2687 default:
2688 return (XT_CB_PASSTHROUGH);
2691 if (dest < 0 || dest >= n)
2692 return (XT_CB_PASSTHROUGH);
2693 if (t->tab_id == dest) {
2694 DNPRINTF(XT_D_TAB, "movetab: do nothing\n");
2695 return (XT_CB_HANDLED);
2698 set_current_tab(dest);
2700 return (XT_CB_HANDLED);
2703 int cmd_prefix = 0;
2705 struct prompt_sub {
2706 const char *s;
2707 const char *(*f)(struct tab *);
2708 } subs[] = {
2709 { "<uri>", get_uri },
2713 command(struct tab *t, struct karg *args)
2715 struct karg a = {0};
2716 int i, cmd_setup = 0;
2717 char *s = NULL, *sp = NULL, *sl = NULL;
2718 gchar **sv;
2720 if (t == NULL || args == NULL) {
2721 show_oops(NULL, "command invalid parameters");
2722 return (XT_CB_PASSTHROUGH);
2725 switch (args->i) {
2726 case '/':
2727 s = "/";
2728 break;
2729 case '?':
2730 s = "?";
2731 break;
2732 case ':':
2733 if (cmd_prefix == 0) {
2734 if (args->s != NULL && strlen(args->s) != 0) {
2735 sp = g_strdup_printf(":%s", args->s);
2736 s = sp;
2737 } else
2738 s = ":";
2739 } else {
2740 sp = g_strdup_printf(":%d", cmd_prefix);
2741 s = sp;
2742 cmd_prefix = 0;
2744 sl = g_strdup(s);
2745 if (sp) {
2746 g_free(sp);
2747 sp = NULL;
2749 s = sl;
2750 for (i = 0; i < LENGTH(subs); ++i) {
2751 sv = g_strsplit(sl, subs[i].s, -1);
2752 if (sl)
2753 g_free(sl);
2754 sl = g_strjoinv(subs[i].f(t), sv);
2755 g_strfreev(sv);
2756 s = sl;
2758 break;
2759 case '.':
2760 t->mode = XT_MODE_HINT;
2761 a.i = 0;
2762 s = ".";
2764 * js code will auto fire() if a single link is visible,
2765 * causing the focus-out-event cb function to be called. Setup
2766 * the cmd _before_ triggering hinting code so the cmd can get
2767 * killed by the cb in this case.
2769 show_cmd(t, s);
2770 cmd_setup = 1;
2771 hint(t, &a);
2772 break;
2773 case ',':
2774 t->mode = XT_MODE_HINT;
2775 a.i = XT_HINT_NEWTAB;
2776 s = ",";
2777 show_cmd(t, s);
2778 cmd_setup = 1;
2779 hint(t, &a);
2780 break;
2781 default:
2782 show_oops(t, "command: invalid opcode %d", args->i);
2783 return (XT_CB_PASSTHROUGH);
2786 DNPRINTF(XT_D_CMD, "%s: tab %d type %s\n", __func__, t->tab_id, s);
2788 if (!cmd_setup)
2789 show_cmd(t, s);
2791 if (sp)
2792 g_free(sp);
2793 if (sl)
2794 g_free(sl);
2796 return (XT_CB_HANDLED);
2800 search(struct tab *t, struct karg *args)
2802 gboolean d;
2804 if (t == NULL || args == NULL) {
2805 show_oops(NULL, "search invalid parameters");
2806 return (1);
2809 switch (args->i) {
2810 case XT_SEARCH_NEXT:
2811 d = t->search_forward;
2812 break;
2813 case XT_SEARCH_PREV:
2814 d = !t->search_forward;
2815 break;
2816 default:
2817 return (XT_CB_PASSTHROUGH);
2820 if (t->search_text == NULL) {
2821 if (global_search == NULL)
2822 return (XT_CB_PASSTHROUGH);
2823 else {
2824 d = t->search_forward = TRUE;
2825 t->search_text = g_strdup(global_search);
2826 webkit_web_view_mark_text_matches(t->wv, global_search, FALSE, 0);
2827 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
2831 DNPRINTF(XT_D_CMD, "search: tab %d opc %d forw %d text %s\n",
2832 t->tab_id, args->i, t->search_forward, t->search_text);
2834 webkit_web_view_search_text(t->wv, t->search_text, FALSE, d, TRUE);
2836 return (XT_CB_HANDLED);
2840 session_save(struct tab *t, char *filename)
2842 struct karg a;
2843 int rv = 1;
2844 struct session *s;
2846 if (strlen(filename) == 0)
2847 goto done;
2849 if (filename[0] == '.' || filename[0] == '/')
2850 goto done;
2852 a.s = filename;
2853 if (save_tabs(t, &a))
2854 goto done;
2855 strlcpy(named_session, filename, sizeof named_session);
2857 /* add the new session to the list of sessions */
2858 s = g_malloc(sizeof(struct session));
2859 s->name = g_strdup(filename);
2860 TAILQ_INSERT_TAIL(&sessions, s, entry);
2862 rv = 0;
2863 done:
2864 return (rv);
2868 session_open(struct tab *t, char *filename)
2870 struct karg a;
2871 int rv = 1;
2873 if (strlen(filename) == 0)
2874 goto done;
2876 if (filename[0] == '.' || filename[0] == '/')
2877 goto done;
2879 a.s = filename;
2880 a.i = XT_SES_CLOSETABS;
2881 if (open_tabs(t, &a))
2882 goto done;
2884 strlcpy(named_session, filename, sizeof named_session);
2886 rv = 0;
2887 done:
2888 return (rv);
2892 session_delete(struct tab *t, char *filename)
2894 char file[PATH_MAX];
2895 int rv = 1;
2896 struct session *s;
2898 if (strlen(filename) == 0)
2899 goto done;
2901 if (filename[0] == '.' || filename[0] == '/')
2902 goto done;
2904 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, filename);
2905 if (unlink(file))
2906 goto done;
2908 if (!strcmp(filename, named_session))
2909 strlcpy(named_session, XT_SAVED_TABS_FILE,
2910 sizeof named_session);
2912 /* remove session from sessions list */
2913 TAILQ_FOREACH(s, &sessions, entry) {
2914 if (!strcmp(s->name, filename))
2915 break;
2917 if (s == NULL)
2918 goto done;
2919 TAILQ_REMOVE(&sessions, s, entry);
2920 g_free((gpointer) s->name);
2921 g_free(s);
2923 rv = 0;
2924 done:
2925 return (rv);
2929 session_cmd(struct tab *t, struct karg *args)
2931 char *filename = args->s;
2933 if (t == NULL)
2934 return (1);
2936 if (args->i & XT_SHOW)
2937 show_oops(t, "Current session: %s", named_session[0] == '\0' ?
2938 XT_SAVED_TABS_FILE : named_session);
2939 else if (args->i & XT_SAVE) {
2940 if (session_save(t, filename)) {
2941 show_oops(t, "Can't save session: %s",
2942 filename ? filename : "INVALID");
2943 goto done;
2945 } else if (args->i & XT_OPEN) {
2946 if (session_open(t, filename)) {
2947 show_oops(t, "Can't open session: %s",
2948 filename ? filename : "INVALID");
2949 goto done;
2951 } else if (args->i & XT_DELETE) {
2952 if (session_delete(t, filename)) {
2953 show_oops(t, "Can't delete session: %s",
2954 filename ? filename : "INVALID");
2955 goto done;
2958 done:
2959 return (XT_CB_PASSTHROUGH);
2963 script_cmd(struct tab *t, struct karg *args)
2965 struct stat sb;
2966 FILE *f = NULL;
2967 char *buf = NULL;
2969 if (t == NULL)
2970 goto done;
2972 if ((f = fopen(args->s, "r")) == NULL) {
2973 show_oops(t, "Can't open script file: %s", args->s);
2974 goto done;
2977 if (fstat(fileno(f), &sb) == -1) {
2978 show_oops(t, "Can't stat script file: %s", args->s);
2979 goto done;
2982 buf = g_malloc0(sb.st_size + 1);
2983 if (fread(buf, 1, sb.st_size, f) != sb.st_size) {
2984 show_oops(t, "Can't read script file: %s", args->s);
2985 goto done;
2988 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
2989 run_script(t, buf);
2991 done:
2992 if (f)
2993 fclose(f);
2994 if (buf)
2995 g_free(buf);
2997 return (XT_CB_PASSTHROUGH);
3001 * Make a hardcopy of the page
3004 print_page(struct tab *t, struct karg *args)
3006 WebKitWebFrame *frame;
3007 GtkPageSetup *ps;
3008 GtkPrintOperation *op;
3009 GtkPrintOperationAction action;
3010 GtkPrintOperationResult print_res;
3011 GError *g_err = NULL;
3012 int marg_l, marg_r, marg_t, marg_b;
3013 int ret = 0;
3015 DNPRINTF(XT_D_PRINTING, "%s:", __func__);
3017 ps = gtk_page_setup_new();
3018 op = gtk_print_operation_new();
3019 action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG;
3020 frame = webkit_web_view_get_main_frame(t->wv);
3022 /* the default margins are too small, so we will bump them */
3023 marg_l = gtk_page_setup_get_left_margin(ps, GTK_UNIT_MM) +
3024 XT_PRINT_EXTRA_MARGIN;
3025 marg_r = gtk_page_setup_get_right_margin(ps, GTK_UNIT_MM) +
3026 XT_PRINT_EXTRA_MARGIN;
3027 marg_t = gtk_page_setup_get_top_margin(ps, GTK_UNIT_MM) +
3028 XT_PRINT_EXTRA_MARGIN;
3029 marg_b = gtk_page_setup_get_bottom_margin(ps, GTK_UNIT_MM) +
3030 XT_PRINT_EXTRA_MARGIN;
3032 /* set margins */
3033 gtk_page_setup_set_left_margin(ps, marg_l, GTK_UNIT_MM);
3034 gtk_page_setup_set_right_margin(ps, marg_r, GTK_UNIT_MM);
3035 gtk_page_setup_set_top_margin(ps, marg_t, GTK_UNIT_MM);
3036 gtk_page_setup_set_bottom_margin(ps, marg_b, GTK_UNIT_MM);
3038 gtk_print_operation_set_default_page_setup(op, ps);
3040 print_res = webkit_web_frame_print_full(frame, op, action, &g_err);
3042 /* check it worked */
3043 if (print_res == GTK_PRINT_OPERATION_RESULT_ERROR) {
3044 show_oops(NULL, "can't print: %s", g_err->message);
3045 g_error_free (g_err);
3046 ret = 1;
3049 g_object_unref(G_OBJECT(ps));
3050 g_object_unref(G_OBJECT(op));
3051 return (ret);
3055 go_home(struct tab *t, struct karg *args)
3057 load_uri(t, home);
3058 return (0);
3062 set_encoding(struct tab *t, struct karg *args)
3064 const gchar *e;
3066 if (args->s && strlen(g_strstrip(args->s)) == 0) {
3067 e = webkit_web_view_get_custom_encoding(t->wv);
3068 if (e == NULL)
3069 e = webkit_web_view_get_encoding(t->wv);
3070 show_oops(t, "encoding: %s", e ? e : "N/A");
3071 } else
3072 webkit_web_view_set_custom_encoding(t->wv, args->s);
3074 return (0);
3078 restart(struct tab *t, struct karg *args)
3080 struct karg a;
3082 a.s = XT_RESTART_TABS_FILE;
3083 save_tabs(t, &a);
3084 execvp(start_argv[0], start_argv);
3085 /* NOTREACHED */
3087 return (0);
3090 char *http_proxy_save; /* not a setting, used to toggle */
3093 proxy_cmd(struct tab *t, struct karg *args)
3095 struct tab *tt;
3097 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
3099 if (t == NULL)
3100 return (1);
3102 /* setup */
3103 if (http_proxy) {
3104 TAILQ_FOREACH(tt, &tabs, entry)
3105 gtk_widget_show(t->proxy_toggle);
3106 if (http_proxy_save)
3107 g_free(http_proxy_save);
3108 http_proxy_save = g_strdup(http_proxy);
3111 if (args->i & XT_PRXY_SHOW) {
3112 if (http_proxy)
3113 show_oops(t, "http_proxy = %s", http_proxy);
3114 else
3115 show_oops(t, "proxy is currently disabled");
3116 } else if (args->i & XT_PRXY_TOGGLE) {
3117 if (http_proxy_save == NULL && http_proxy == NULL) {
3118 show_oops(t, "can't toggle proxy");
3119 goto done;
3121 TAILQ_FOREACH(tt, &tabs, entry)
3122 gtk_widget_show(t->proxy_toggle);
3123 if (http_proxy) {
3124 setup_proxy(NULL);
3125 button_set_file(t->proxy_toggle, "tordisabled.ico");
3126 show_oops(t, "http proxy disabled");
3127 } else {
3128 setup_proxy(http_proxy_save);
3129 button_set_file(t->proxy_toggle, "torenabled.ico");
3130 show_oops(t, "http_proxy = %s", http_proxy);
3133 done:
3134 return (XT_CB_PASSTHROUGH);
3138 * If you can read this functionthen you are a sick and twisted individual.
3139 * I hope we never meet, it'll be violent.
3141 gboolean
3142 eval_cb(const GMatchInfo *info, GString *res, gpointer data)
3144 gchar *match, *num;
3145 gint start = -1, end = -1, i;
3146 struct karg *args = data;
3149 * match contains the string UP TO the match.
3151 * res is what is returned, note that whatever remains in the sent in
3152 * string is appended on the way out.
3154 * for example /123/456/789/moo came in
3155 * match contains /123/456/789/
3156 * we assign that to res and replace /789/ with the replacement text
3157 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
3160 match = g_match_info_fetch(info, 0);
3161 if (match == NULL)
3162 goto done;
3164 if (g_match_info_fetch_pos(info, 1, &start, &end) == FALSE)
3165 goto freeit;
3167 g_string_assign(res, match);
3169 i = atoi(&match[start + 1]);
3170 if (args->i == XT_URL_PLUS)
3171 i++;
3172 else
3173 i--;
3175 /* preserve whitespace when likely */
3176 num = g_strdup_printf("%0*d", end - start - 2, i);
3177 g_string_overwrite_len(res, start + 1, num, end - start - 2);
3178 g_free(num);
3180 freeit:
3181 g_free(match);
3182 done:
3183 return (FALSE); /* doesn't matter */
3187 urlmod_cmd(struct tab *t, struct karg *args)
3189 const gchar *uri;
3190 GRegex *reg;
3191 gchar *res;
3193 if (t == NULL)
3194 goto done;
3195 if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) == NULL)
3196 goto done;
3197 if (strlen(uri) == 0)
3198 goto done;
3200 reg = g_regex_new(".*(/[0-9]+/)", 0, 0, NULL);
3201 if (reg == NULL)
3202 goto done;
3203 res = g_regex_replace_eval(reg, uri, -1, 0, 0, eval_cb, args, NULL);
3204 if (res == NULL)
3205 goto free_reg;
3207 if (!strcmp(res, uri))
3208 goto free_res;
3210 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), res);
3211 activate_uri_entry_cb(t->uri_entry, t);
3213 free_res:
3214 g_free(res);
3215 free_reg:
3216 g_regex_unref(reg);
3217 done:
3218 return (XT_CB_PASSTHROUGH);
3221 struct cmd {
3222 char *cmd;
3223 int level;
3224 int (*func)(struct tab *, struct karg *);
3225 int arg;
3226 int type;
3227 } cmds[] = {
3228 { "command_mode", 0, command_mode, XT_MODE_COMMAND, 0 },
3229 { "insert_mode", 0, command_mode, XT_MODE_INSERT, 0 },
3230 { "command", 0, command, ':', 0 },
3231 { "search", 0, command, '/', 0 },
3232 { "searchb", 0, command, '?', 0 },
3233 { "hinting", 0, command, '.', 0 },
3234 { "hinting_newtab", 0, command, ',', 0 },
3235 { "togglesrc", 0, toggle_src, 0, 0 },
3236 { "editsrc", 0, edit_src, 0, 0 },
3237 { "editelement", 0, edit_element, 0, 0 },
3238 { "passthrough", 0, passthrough, 0, 0 },
3239 { "modurl", 0, modurl, 0, 0 },
3241 /* yanking and pasting */
3242 { "yankuri", 0, yank_uri, 0, 0 },
3243 { "pasteuricur", 0, paste_uri, XT_PASTE_CURRENT_TAB, 0 },
3244 { "pasteurinew", 0, paste_uri, XT_PASTE_NEW_TAB, 0 },
3246 /* search */
3247 { "searchnext", 0, search, XT_SEARCH_NEXT, 0 },
3248 { "searchprevious", 0, search, XT_SEARCH_PREV, 0 },
3250 /* focus */
3251 { "focusaddress", 0, focus, XT_FOCUS_URI, 0 },
3252 { "focussearch", 0, focus, XT_FOCUS_SEARCH, 0 },
3254 /* hinting */
3255 { "hinting", 0, hint, 0, 0 },
3256 { "hinting_newtab", 0, hint, XT_HINT_NEWTAB, 0 },
3258 /* custom stylesheet */
3259 { "userstyle", 0, userstyle_cmd, XT_STYLE_CURRENT_TAB, XT_USERARG },
3260 { "userstyle_global", 0, userstyle_cmd, XT_STYLE_GLOBAL, XT_USERARG },
3262 /* navigation */
3263 { "goback", 0, navaction, XT_NAV_BACK, 0 },
3264 { "goforward", 0, navaction, XT_NAV_FORWARD, 0 },
3265 { "reload", 0, navaction, XT_NAV_RELOAD, 0 },
3266 { "stop", 0, navaction, XT_NAV_STOP, 0 },
3268 /* vertical movement */
3269 { "scrolldown", 0, move, XT_MOVE_DOWN, 0 },
3270 { "scrollup", 0, move, XT_MOVE_UP, 0 },
3271 { "scrollbottom", 0, move, XT_MOVE_BOTTOM, 0 },
3272 { "scrolltop", 0, move, XT_MOVE_TOP, 0 },
3273 { "1", 0, move, XT_MOVE_TOP, 0 },
3274 { "scrollhalfdown", 0, move, XT_MOVE_HALFDOWN, 0 },
3275 { "scrollhalfup", 0, move, XT_MOVE_HALFUP, 0 },
3276 { "scrollpagedown", 0, move, XT_MOVE_PAGEDOWN, 0 },
3277 { "scrollpageup", 0, move, XT_MOVE_PAGEUP, 0 },
3278 /* horizontal movement */
3279 { "scrollright", 0, move, XT_MOVE_RIGHT, 0 },
3280 { "scrollleft", 0, move, XT_MOVE_LEFT, 0 },
3281 { "scrollfarright", 0, move, XT_MOVE_FARRIGHT, 0 },
3282 { "scrollfarleft", 0, move, XT_MOVE_FARLEFT, 0 },
3284 { "favorites", 0, xtp_page_fl, XT_SHOW, 0 },
3285 { "fav", 0, xtp_page_fl, XT_SHOW, 0 },
3286 { "favedit", 0, xtp_page_fl, XT_SHOW|XT_DELETE, 0 },
3287 { "favadd", 0, add_favorite, 0, 0 },
3289 { "qall", 0, quit, 0, 0 },
3290 { "quitall", 0, quit, 0, 0 },
3291 { "w", 0, save_tabs, 0, 0 },
3292 { "wq", 0, save_tabs_and_quit, 0, 0 },
3293 { "help", 0, help, 0, 0 },
3294 { "about", 0, xtp_page_ab, 0, 0 },
3295 { "stats", 0, stats, 0, 0 },
3296 { "version", 0, xtp_page_ab, 0, 0 },
3298 /* js command */
3299 { "js", 0, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3300 { "save", 1, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3301 { "domain", 2, js_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3302 { "fqdn", 2, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3303 { "show", 1, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3304 { "all", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3305 { "persistent", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3306 { "session", 2, js_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3307 { "toggle", 1, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3308 { "domain", 2, js_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3309 { "fqdn", 2, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3311 /* cookie command */
3312 { "cookie", 0, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3313 { "save", 1, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3314 { "domain", 2, cookie_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3315 { "fqdn", 2, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3316 { "show", 1, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3317 { "all", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3318 { "persistent", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3319 { "session", 2, cookie_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3320 { "toggle", 1, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3321 { "domain", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3322 { "fqdn", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3323 { "purge", 1, cookie_cmd, XT_DELETE, 0 },
3325 /* plugin command */
3326 { "plugin", 0, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3327 { "save", 1, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3328 { "domain", 2, pl_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3329 { "fqdn", 2, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3330 { "show", 1, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3331 { "all", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3332 { "persistent", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3333 { "session", 2, pl_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3334 { "toggle", 1, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3335 { "domain", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3336 { "fqdn", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3338 /* https command */
3339 { "https", 0, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3340 { "save", 1, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3341 { "domain", 2, https_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3342 { "fqdn", 2, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3343 { "show", 1, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3344 { "all", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3345 { "persistent", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3346 { "session", 2, https_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3347 { "toggle", 1, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3348 { "domain", 2, https_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3349 { "fqdn", 2, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3351 /* toplevel (domain) command */
3352 { "toplevel", 0, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3353 { "toggle", 1, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3355 /* cookie jar */
3356 { "cookiejar", 0, xtp_page_cl, 0, 0 },
3358 /* cert command */
3359 { "cert", 0, cert_cmd, XT_SHOW, 0 },
3360 { "save", 1, cert_cmd, XT_SAVE, 0 },
3361 { "show", 1, cert_cmd, XT_SHOW, 0 },
3363 { "ca", 0, ca_cmd, 0, 0 },
3364 { "downloadmgr", 0, xtp_page_dl, 0, 0 },
3365 { "dl", 0, xtp_page_dl, 0, 0 },
3366 { "h", 0, xtp_page_hl, 0, 0 },
3367 { "history", 0, xtp_page_hl, 0, 0 },
3368 { "home", 0, go_home, 0, 0 },
3369 { "restart", 0, restart, 0, 0 },
3370 { "urlhide", 0, urlaction, XT_URL_HIDE, 0 },
3371 { "urlshow", 0, urlaction, XT_URL_SHOW, 0 },
3372 { "statustoggle", 0, statustoggle, 0, 0 },
3373 { "run_script", 0, run_page_script, 0, XT_USERARG },
3375 { "print", 0, print_page, 0, 0 },
3377 /* tabs */
3378 { "focusin", 0, resizetab, XT_ZOOM_IN, 0 },
3379 { "focusout", 0, resizetab, XT_ZOOM_OUT, 0 },
3380 { "focusreset", 0, resizetab, XT_ZOOM_NORMAL, 0 },
3381 { "q", 0, tabaction, XT_TAB_DELQUIT, 0 },
3382 { "quit", 0, tabaction, XT_TAB_DELQUIT, 0 },
3383 { "open", 0, tabaction, XT_TAB_OPEN, XT_URLARG },
3384 { "tabclose", 0, tabaction, XT_TAB_DELETE, XT_PREFIX | XT_INTARG},
3385 { "tabedit", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3386 { "tabfirst", 0, movetab, XT_TAB_FIRST, 0 },
3387 { "tabhide", 0, tabaction, XT_TAB_HIDE, 0 },
3388 { "tablast", 0, movetab, XT_TAB_LAST, 0 },
3389 { "tabnew", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3390 { "tabnext", 0, movetab, XT_TAB_NEXT, XT_PREFIX | XT_INTARG},
3391 { "tabnextstyle", 0, tabaction, XT_TAB_NEXTSTYLE, 0 },
3392 { "tabonly", 0, tabaction, XT_TAB_ONLY, 0 },
3393 { "tabprevious", 0, movetab, XT_TAB_PREV, XT_PREFIX | XT_INTARG},
3394 { "tabrewind", 0, movetab, XT_TAB_FIRST, 0 },
3395 { "tabshow", 0, tabaction, XT_TAB_SHOW, 0 },
3396 { "tabs", 0, buffers, 0, 0 },
3397 { "tabundoclose", 0, tabaction, XT_TAB_UNDO_CLOSE, 0 },
3398 { "buffers", 0, buffers, 0, 0 },
3399 { "ls", 0, buffers, 0, 0 },
3400 { "encoding", 0, set_encoding, 0, XT_USERARG },
3401 { "loadimages", 0, tabaction, XT_TAB_LOAD_IMAGES, 0 },
3403 /* settings */
3404 { "set", 0, set, 0, XT_SETARG },
3405 { "runtime", 0, xtp_page_rt, 0, 0 },
3407 { "fullscreen", 0, fullscreen, 0, 0 },
3408 { "f", 0, fullscreen, 0, 0 },
3410 /* sessions */
3411 { "session", 0, session_cmd, XT_SHOW, 0 },
3412 { "delete", 1, session_cmd, XT_DELETE, XT_SESSARG },
3413 { "open", 1, session_cmd, XT_OPEN, XT_SESSARG },
3414 { "save", 1, session_cmd, XT_SAVE, XT_USERARG },
3415 { "show", 1, session_cmd, XT_SHOW, 0 },
3417 /* external javascript */
3418 { "script", 0, script_cmd, XT_EJS_SHOW, XT_USERARG },
3420 /* inspector */
3421 { "inspector", 0, inspector_cmd, XT_INS_SHOW, 0 },
3422 { "show", 1, inspector_cmd, XT_INS_SHOW, 0 },
3423 { "hide", 1, inspector_cmd, XT_INS_HIDE, 0 },
3425 /* proxy */
3426 { "proxy", 0, proxy_cmd, XT_PRXY_SHOW, 0 },
3427 { "show", 1, proxy_cmd, XT_PRXY_SHOW, 0 },
3428 { "toggle", 1, proxy_cmd, XT_PRXY_TOGGLE, 0 },
3430 /* url mod */
3431 { "urlmod", 0, urlmod_cmd, XT_URL, 0 },
3432 { "plus", 1, urlmod_cmd, XT_URL_PLUS, 0 },
3433 { "min", 1, urlmod_cmd, XT_URL_MIN, 0 },
3436 struct {
3437 int index;
3438 int len;
3439 gchar *list[256];
3440 } cmd_status = {-1, 0};
3442 gboolean
3443 wv_release_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3446 if (e->type == GDK_BUTTON_RELEASE && e->button == 1)
3447 btn_down = 0;
3449 return (FALSE);
3452 gboolean
3453 wv_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3455 struct karg a;
3456 WebKitHitTestResult *hit_test_result;
3457 guint context;
3459 hit_test_result = webkit_web_view_get_hit_test_result(t->wv, e);
3460 g_object_get(hit_test_result, "context", &context, NULL);
3461 g_object_unref(G_OBJECT(hit_test_result));
3463 hide_oops(t);
3464 hide_buffers(t);
3466 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE)
3467 t->mode = XT_MODE_INSERT;
3468 else
3469 t->mode = XT_MODE_COMMAND;
3471 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3472 btn_down = 1;
3473 else if (e->type == GDK_BUTTON_PRESS && e->button == 8 /* btn 4 */) {
3474 /* go backward */
3475 a.i = XT_NAV_BACK;
3476 navaction(t, &a);
3478 return (TRUE);
3479 } else if (e->type == GDK_BUTTON_PRESS && e->button == 9 /* btn 5 */) {
3480 /* go forward */
3481 a.i = XT_NAV_FORWARD;
3482 navaction(t, &a);
3484 return (TRUE);
3487 return (FALSE);
3490 gboolean
3491 tab_close_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3493 DNPRINTF(XT_D_TAB, "tab_close_cb: tab %d\n", t->tab_id);
3495 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3496 delete_tab(t);
3498 return (FALSE);
3502 parse_custom_uri(struct tab *t, const char *uri)
3504 struct custom_uri *u;
3505 int handled = 0;
3506 char *sv[3];
3508 TAILQ_FOREACH(u, &cul, entry) {
3509 if (strncmp(uri, u->uri, strlen(u->uri)))
3510 continue;
3512 handled = 1;
3513 sv[0] = u->cmd;
3514 sv[1] = (char *)uri;
3515 sv[2] = NULL;
3516 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL,
3517 NULL, NULL, NULL))
3518 show_oops(t, "%s: could not spawn process", __func__);
3521 return (handled);
3524 void
3525 activate_uri_entry_cb(GtkWidget* entry, struct tab *t)
3527 const gchar *uri = gtk_entry_get_text(GTK_ENTRY(entry));
3529 DNPRINTF(XT_D_URL, "activate_uri_entry_cb: %s\n", uri);
3531 if (t == NULL) {
3532 show_oops(NULL, "activate_uri_entry_cb invalid parameters");
3533 return;
3536 if (uri == NULL) {
3537 show_oops(t, "activate_uri_entry_cb no uri");
3538 return;
3541 uri += strspn(uri, "\t ");
3543 if (parse_custom_uri(t, uri))
3544 return;
3546 /* otherwise continue to load page normally */
3547 load_uri(t, (gchar *)uri);
3548 focus_webview(t);
3551 void
3552 activate_search_entry_cb(GtkWidget* entry, struct tab *t)
3554 const gchar *search = gtk_entry_get_text(GTK_ENTRY(entry));
3555 char *newuri = NULL;
3556 gchar *enc_search;
3557 char **sv;
3559 DNPRINTF(XT_D_URL, "activate_search_entry_cb: %s\n", search);
3561 if (t == NULL) {
3562 show_oops(NULL, "activate_search_entry_cb invalid parameters");
3563 return;
3566 if (search_string == NULL || strlen(search_string) == 0) {
3567 show_oops(t, "no search_string");
3568 return;
3571 set_normal_tab_meaning(t);
3573 enc_search = soup_uri_encode(search, XT_RESERVED_CHARS);
3574 sv = g_strsplit(search_string, "%s", 2);
3575 newuri = g_strjoinv(enc_search, sv);
3576 g_free(enc_search);
3577 g_strfreev(sv);
3579 marks_clear(t);
3580 load_uri(t, newuri);
3581 focus_webview(t);
3583 if (newuri)
3584 g_free(newuri);
3587 void
3588 check_and_set_cookie(const gchar *uri, struct tab *t)
3590 struct wl_entry *w = NULL;
3591 int es = 0;
3593 if (uri == NULL || t == NULL)
3594 return;
3596 if ((w = wl_find_uri(uri, &c_wl)) == NULL)
3597 es = 0;
3598 else
3599 es = 1;
3601 DNPRINTF(XT_D_COOKIE, "check_and_set_cookie: %s %s\n",
3602 es ? "enable" : "disable", uri);
3604 g_object_set(G_OBJECT(t->settings),
3605 "enable-html5-local-storage", es, (char *)NULL);
3606 webkit_web_view_set_settings(t->wv, t->settings);
3609 void
3610 check_and_set_js(const gchar *uri, struct tab *t)
3612 struct wl_entry *w = NULL;
3613 int es = 0;
3615 if (uri == NULL || t == NULL)
3616 return;
3618 if ((w = wl_find_uri(uri, &js_wl)) == NULL)
3619 es = 0;
3620 else
3621 es = 1;
3623 DNPRINTF(XT_D_JS, "check_and_set_js: %s %s\n",
3624 es ? "enable" : "disable", uri);
3626 g_object_set(G_OBJECT(t->settings),
3627 "enable-scripts", es, (char *)NULL);
3628 g_object_set(G_OBJECT(t->settings),
3629 "javascript-can-open-windows-automatically", es, (char *)NULL);
3630 webkit_web_view_set_settings(t->wv, t->settings);
3632 button_set_stockid(t->js_toggle,
3633 es ? GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE);
3636 void
3637 check_and_set_pl(const gchar *uri, struct tab *t)
3639 struct wl_entry *w = NULL;
3640 int es = 0;
3642 if (uri == NULL || t == NULL)
3643 return;
3645 if ((w = wl_find_uri(uri, &pl_wl)) == NULL)
3646 es = 0;
3647 else
3648 es = 1;
3650 DNPRINTF(XT_D_JS, "check_and_set_pl: %s %s\n",
3651 es ? "enable" : "disable", uri);
3653 g_object_set(G_OBJECT(t->settings),
3654 "enable-plugins", es, (char *)NULL);
3655 webkit_web_view_set_settings(t->wv, t->settings);
3658 #if GTK_CHECK_VERSION(3, 0, 0)
3659 /* A lot of this can be removed when gtk2 is dropped on the floor */
3660 char *
3661 get_css_name(const char *col_str)
3663 char *name = NULL;
3665 if (!strcmp(col_str, XT_COLOR_WHITE))
3666 name = g_strdup(XT_CSS_NORMAL);
3667 else if (!strcmp(col_str, XT_COLOR_RED))
3668 name = g_strdup(XT_CSS_RED);
3669 else if (!strcmp(col_str, XT_COLOR_YELLOW))
3670 name = g_strdup(XT_CSS_YELLOW);
3671 else if (!strcmp(col_str, XT_COLOR_GREEN))
3672 name = g_strdup(XT_CSS_GREEN);
3673 else if (!strcmp(col_str, XT_COLOR_BLUE))
3674 name = g_strdup(XT_CSS_BLUE);
3675 return (name);
3677 #endif
3679 void
3680 show_ca_status(struct tab *t, const char *uri)
3682 char domain[8182], file[PATH_MAX];
3683 SoupMessage *msg = NULL;
3684 GTlsCertificate *cert = NULL;
3685 GTlsCertificateFlags flags = 0;
3686 gchar *col_str = XT_COLOR_RED;
3687 char *cut_uri;
3688 char *chain;
3689 char *s;
3690 #if GTK_CHECK_VERSION(3, 0, 0)
3691 char *name;
3692 #else
3693 GdkColor color;
3694 gchar *text, *base;
3695 #endif
3696 enum cert_trust trust;
3697 int nocolor = 0;
3698 int i;
3700 DNPRINTF(XT_D_URL, "show_ca_status: %d %s %s\n",
3701 ssl_strict_certs, ssl_ca_file, uri);
3703 if (t == NULL)
3704 return;
3706 if (uri == NULL || g_str_has_prefix(uri, "http://") ||
3707 !g_str_has_prefix(uri, "https://"))
3708 return;
3711 * Cut the uri to get the certs off the homepage. We can't use the
3712 * full URI here since it may include arguments and we don't want to make
3713 * these requests multiple times.
3715 cut_uri = g_strdup(uri);
3716 s = cut_uri;
3717 for (i = 0; i < 3; ++i)
3718 s = strchr(&(s[1]), '/');
3719 s[1] = '\0';
3721 msg = soup_message_new("HEAD", cut_uri);
3722 g_free(cut_uri);
3723 if (msg == NULL)
3724 return;
3725 soup_message_set_flags(msg, SOUP_MESSAGE_NO_REDIRECT);
3726 soup_session_send_message(session, msg);
3727 if (msg->status_code == SOUP_STATUS_SSL_FAILED ||
3728 msg->status_code == SOUP_STATUS_TLS_FAILED) {
3729 DNPRINTF(XT_D_URL, "%s: status not ok: %d\n", uri,
3730 msg->status_code);
3731 goto done;
3733 if (!soup_message_get_https_status(msg, &cert, &flags)) {
3734 DNPRINTF(XT_D_URL, "%s: invalid response\n", uri);
3735 goto done;
3737 if (!G_IS_TLS_CERTIFICATE(cert)) {
3738 DNPRINTF(XT_D_URL, "%s: no cert\n", uri);
3739 goto done;
3742 if (flags == 0)
3743 col_str = XT_COLOR_GREEN;
3744 else
3745 col_str = XT_COLOR_YELLOW;
3747 strlcpy(domain, uri + strlen("https://"), sizeof domain);
3748 for (i = 0; i < strlen(domain); i++)
3749 if (domain[i] == '/') {
3750 domain[i] = '\0';
3751 break;
3754 snprintf(file, sizeof file, "%s" PS "%s", certs_cache_dir, domain);
3755 if (warn_cert_changes) {
3756 if (check_cert_changes(t, cert, file, uri))
3757 nocolor = 1;
3760 snprintf(file, sizeof file, "%s" PS "%s", certs_dir, domain);
3761 chain = g_strdup("");
3762 if ((trust = check_local_certs(file, cert, &chain)) == CERT_LOCAL)
3763 col_str = XT_COLOR_BLUE;
3764 if (t->pem)
3765 g_free(t->pem);
3766 t->pem = chain;
3768 done:
3769 g_object_unref(msg);
3770 if (!strcmp(col_str, XT_COLOR_WHITE) || nocolor) {
3771 #if GTK_CHECK_VERSION(3, 0, 0)
3772 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
3773 statusbar_modify_attr(t, XT_CSS_NORMAL);
3774 #else
3775 text = gdk_color_to_string(
3776 &t->default_style->text[GTK_STATE_NORMAL]);
3777 base = gdk_color_to_string(
3778 &t->default_style->base[GTK_STATE_NORMAL]);
3779 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
3780 &t->default_style->base[GTK_STATE_NORMAL]);
3781 statusbar_modify_attr(t, text, base);
3782 g_free(text);
3783 g_free(base);
3784 #endif
3785 } else {
3786 #if GTK_CHECK_VERSION(3, 0, 0)
3787 name = get_css_name(col_str);
3788 gtk_widget_set_name(t->uri_entry, name);
3789 statusbar_modify_attr(t, name);
3790 g_free(name);
3791 #else
3792 gdk_color_parse(col_str, &color);
3793 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
3794 statusbar_modify_attr(t, XT_COLOR_BLACK, col_str);
3795 #endif
3799 void
3800 free_favicon(struct tab *t)
3802 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p req %p\n",
3803 __func__, t->icon_download, t->icon_request);
3805 if (t->icon_request)
3806 g_object_unref(t->icon_request);
3807 if (t->icon_dest_uri)
3808 g_free(t->icon_dest_uri);
3810 t->icon_request = NULL;
3811 t->icon_dest_uri = NULL;
3814 void
3815 xt_icon_from_name(struct tab *t, gchar *name)
3817 if (!enable_favicon_entry)
3818 return;
3820 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->uri_entry),
3821 GTK_ENTRY_ICON_PRIMARY, "text-html");
3822 if (show_url == 0)
3823 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3824 GTK_ENTRY_ICON_PRIMARY, "text-html");
3825 else
3826 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3827 GTK_ENTRY_ICON_PRIMARY, NULL);
3830 void
3831 xt_icon_from_pixbuf(struct tab *t, GdkPixbuf *pb)
3833 GdkPixbuf *pb_scaled;
3835 if (gdk_pixbuf_get_width(pb) > 16 || gdk_pixbuf_get_height(pb) > 16)
3836 pb_scaled = gdk_pixbuf_scale_simple(pb, 16, 16,
3837 GDK_INTERP_BILINEAR);
3838 else
3839 pb_scaled = pb;
3841 if (enable_favicon_entry) {
3843 /* Classic tabs. */
3844 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->uri_entry),
3845 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3847 /* Minimal tabs. */
3848 if (show_url == 0) {
3849 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
3850 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3851 } else
3852 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3853 GTK_ENTRY_ICON_PRIMARY, NULL);
3856 /* XXX: Only supports the minimal tabs atm. */
3857 if (enable_favicon_tabs)
3858 gtk_image_set_from_pixbuf(GTK_IMAGE(t->tab_elems.favicon),
3859 pb_scaled);
3861 if (pb_scaled != pb)
3862 g_object_unref(pb_scaled);
3865 void
3866 xt_icon_from_file(struct tab *t, char *uri)
3868 GdkPixbuf *pb;
3869 char *file;
3871 if (g_str_has_prefix(uri, "file://"))
3872 file = g_filename_from_uri(uri, NULL, NULL);
3873 else
3874 file = g_strdup(uri);
3876 if (file == NULL)
3877 return;
3879 pb = gdk_pixbuf_new_from_file(file, NULL);
3880 if (pb) {
3881 xt_icon_from_pixbuf(t, pb);
3882 g_object_unref(pb);
3883 } else
3884 xt_icon_from_name(t, "text-html");
3886 g_free(file);
3889 gboolean
3890 is_valid_icon(char *file)
3892 gboolean valid = 0;
3893 const char *mime_type;
3894 GFileInfo *fi;
3895 GFile *gf;
3897 gf = g_file_new_for_path(file);
3898 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
3899 NULL, NULL);
3900 mime_type = g_file_info_get_content_type(fi);
3901 valid = g_strcmp0(mime_type, "image/x-ico") == 0 ||
3902 g_strcmp0(mime_type, "image/vnd.microsoft.icon") == 0 ||
3903 g_strcmp0(mime_type, "image/png") == 0 ||
3904 g_strcmp0(mime_type, "image/gif") == 0 ||
3905 g_strcmp0(mime_type, "application/octet-stream") == 0;
3906 g_object_unref(fi);
3907 g_object_unref(gf);
3909 return (valid);
3912 void
3913 set_favicon_from_file(struct tab *t, char *uri)
3915 struct stat sb;
3916 char *file;
3918 if (t == NULL || uri == NULL)
3919 return;
3921 if (g_str_has_prefix(uri, "file://"))
3922 file = g_filename_from_uri(uri, NULL, NULL);
3923 else
3924 file = g_strdup(uri);
3926 if (file == NULL)
3927 return;
3929 DNPRINTF(XT_D_DOWNLOAD, "%s: loading %s\n", __func__, file);
3931 if (!stat(file, &sb)) {
3932 if (sb.st_size == 0 || !is_valid_icon(file)) {
3933 /* corrupt icon so trash it */
3934 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
3935 __func__, file);
3936 unlink(file);
3937 /* no need to set icon to default here */
3938 goto done;
3941 xt_icon_from_file(t, file);
3942 done:
3943 g_free(file);
3946 void
3947 favicon_download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
3948 WebKitWebView *wv)
3950 WebKitDownloadStatus status = webkit_download_get_status(download);
3951 struct tab *tt = NULL, *t = NULL;
3954 * find the webview instead of passing in the tab as it could have been
3955 * deleted from underneath us.
3957 TAILQ_FOREACH(tt, &tabs, entry) {
3958 if (tt->wv == wv) {
3959 t = tt;
3960 break;
3963 if (t == NULL)
3964 return;
3966 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d status %d\n",
3967 __func__, t->tab_id, status);
3969 switch (status) {
3970 case WEBKIT_DOWNLOAD_STATUS_ERROR:
3971 /* -1 */
3972 t->icon_download = NULL;
3973 free_favicon(t);
3974 break;
3975 case WEBKIT_DOWNLOAD_STATUS_CREATED:
3976 /* 0 */
3977 break;
3978 case WEBKIT_DOWNLOAD_STATUS_STARTED:
3979 /* 1 */
3980 break;
3981 case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
3982 /* 2 */
3983 DNPRINTF(XT_D_DOWNLOAD, "%s: freeing favicon %d\n",
3984 __func__, t->tab_id);
3985 t->icon_download = NULL;
3986 free_favicon(t);
3987 break;
3988 case WEBKIT_DOWNLOAD_STATUS_FINISHED:
3989 /* 3 */
3991 DNPRINTF(XT_D_DOWNLOAD, "%s: setting icon to %s\n",
3992 __func__, t->icon_dest_uri);
3993 set_favicon_from_file(t, t->icon_dest_uri);
3994 /* these will be freed post callback */
3995 t->icon_request = NULL;
3996 t->icon_download = NULL;
3997 break;
3998 default:
3999 break;
4003 void
4004 abort_favicon_download(struct tab *t)
4006 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p\n", __func__, t->icon_download);
4008 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
4009 if (t->icon_download) {
4010 g_signal_handlers_disconnect_by_func(G_OBJECT(t->icon_download),
4011 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
4012 webkit_download_cancel(t->icon_download);
4013 t->icon_download = NULL;
4014 } else
4015 free_favicon(t);
4016 #endif
4018 xt_icon_from_name(t, "text-html");
4021 void
4022 notify_icon_loaded_cb(WebKitWebView *wv, gchar *uri, struct tab *t)
4024 DNPRINTF(XT_D_DOWNLOAD, "%s %s\n", __func__, uri);
4026 if (uri == NULL || t == NULL)
4027 return;
4029 #if WEBKIT_CHECK_VERSION(1, 4, 0)
4030 /* take icon from WebKitIconDatabase */
4031 GdkPixbuf *pb = NULL;
4033 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
4034 #if WEBKIT_CHECK_VERSION(1, 8, 0)
4036 * If the page was not loaded (for example, via ssl_strict_certs), do
4037 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
4038 * glib warning.
4040 if (wv && webkit_web_view_get_uri(wv))
4041 pb = webkit_web_view_try_get_favicon_pixbuf(wv, 0, 0);
4042 #else
4043 if (wv && webkit_web_view_get_uri(wv))
4044 pb = webkit_web_view_get_icon_pixbuf(wv);
4045 #endif
4046 if (pb) {
4047 xt_icon_from_pixbuf(t, pb);
4048 g_object_unref(pb);
4049 } else
4050 xt_icon_from_name(t, "text-html");
4051 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
4052 /* download icon to cache dir */
4053 gchar *name_hash, file[PATH_MAX];
4054 struct stat sb;
4056 if (t->icon_request) {
4057 DNPRINTF(XT_D_DOWNLOAD, "%s: download in progress\n", __func__);
4058 return;
4061 /* check to see if we got the icon in cache */
4062 name_hash = g_compute_checksum_for_string(G_CHECKSUM_SHA256, uri, -1);
4063 snprintf(file, sizeof file, "%s" PS "%s.ico", cache_dir, name_hash);
4064 g_free(name_hash);
4066 if (!stat(file, &sb)) {
4067 if (sb.st_size > 0) {
4068 DNPRINTF(XT_D_DOWNLOAD, "%s: loading from cache %s\n",
4069 __func__, file);
4070 set_favicon_from_file(t, file);
4071 return;
4074 /* corrupt icon so trash it */
4075 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
4076 __func__, file);
4077 unlink(file);
4080 /* create download for icon */
4081 t->icon_request = webkit_network_request_new(uri);
4082 if (t->icon_request == NULL) {
4083 DNPRINTF(XT_D_DOWNLOAD, "%s: invalid uri %s\n",
4084 __func__, uri);
4085 return;
4088 t->icon_download = webkit_download_new(t->icon_request);
4089 if (t->icon_download == NULL)
4090 return;
4092 /* we have to free icon_dest_uri later */
4093 if ((t->icon_dest_uri = g_filename_to_uri(file, NULL, NULL)) == NULL)
4094 return;
4095 webkit_download_set_destination_uri(t->icon_download,
4096 t->icon_dest_uri);
4098 if (webkit_download_get_status(t->icon_download) ==
4099 WEBKIT_DOWNLOAD_STATUS_ERROR) {
4100 g_object_unref(t->icon_request);
4101 g_free(t->icon_dest_uri);
4102 t->icon_request = NULL;
4103 t->icon_dest_uri = NULL;
4104 return;
4107 g_signal_connect(G_OBJECT(t->icon_download), "notify::status",
4108 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
4110 webkit_download_start(t->icon_download);
4111 #endif
4114 void
4115 notify_load_status_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4117 const gchar *uri = NULL;
4118 struct history *h, find;
4119 struct karg a;
4120 gchar *tmp_uri = NULL;
4121 #if !GTK_CHECK_VERSION(3, 0, 0)
4122 gchar *text, *base;
4123 #endif
4125 DNPRINTF(XT_D_URL, "notify_load_status_cb: %d %s\n",
4126 webkit_web_view_get_load_status(wview),
4127 get_uri(t) ? get_uri(t) : "NOTHING");
4129 if (t == NULL) {
4130 show_oops(NULL, "notify_load_status_cb invalid parameters");
4131 return;
4134 switch (webkit_web_view_get_load_status(wview)) {
4135 case WEBKIT_LOAD_PROVISIONAL:
4136 /* 0 */
4137 abort_favicon_download(t);
4138 #if GTK_CHECK_VERSION(2, 20, 0)
4139 gtk_widget_show(t->spinner);
4140 gtk_spinner_start(GTK_SPINNER(t->spinner));
4141 #endif
4142 t->download_requested = 0;
4144 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), TRUE);
4146 /* assume we are a new address */
4147 #if GTK_CHECK_VERSION(3, 0, 0)
4148 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
4149 statusbar_modify_attr(t, XT_CSS_NORMAL);
4150 #else
4151 text = gdk_color_to_string(
4152 &t->default_style->text[GTK_STATE_NORMAL]);
4153 base = gdk_color_to_string(
4154 &t->default_style->base[GTK_STATE_NORMAL]);
4155 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
4156 &t->default_style->base[GTK_STATE_NORMAL]);
4157 statusbar_modify_attr(t, text, base);
4158 g_free(text);
4159 g_free(base);
4160 #endif
4162 /* DOM is changing, unreference the previous focused element */
4163 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4164 if (t->active)
4165 g_object_unref(t->active);
4166 t->active = NULL;
4167 if (t->active_text) {
4168 g_free(t->active_text);
4169 t->active_text = NULL;
4171 #endif
4173 /* take focus if we are visible */
4174 focus_webview(t);
4175 t->focus_wv = 1;
4177 marks_clear(t);
4178 break;
4180 case WEBKIT_LOAD_COMMITTED:
4181 /* 1 */
4182 uri = get_uri(t);
4183 if (uri == NULL)
4184 return;
4185 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), uri);
4187 if (t->status) {
4188 g_free(t->status);
4189 t->status = NULL;
4191 set_status(t, "Loading: %s", (char *)uri);
4193 /* clear t->item, except if we're switching to an about: page */
4194 if (t->item && !g_str_has_prefix(uri, "xxxt://") &&
4195 !g_str_has_prefix(uri, "about:")) {
4196 g_object_unref(t->item);
4197 t->item = NULL;
4200 /* check if js white listing is enabled */
4201 if (enable_plugin_whitelist)
4202 check_and_set_pl(uri, t);
4203 if (enable_cookie_whitelist)
4204 check_and_set_cookie(uri, t);
4205 if (enable_js_whitelist)
4206 check_and_set_js(uri, t);
4208 if (t->styled)
4209 apply_style(t);
4212 /* we know enough to autosave the session */
4213 if (session_autosave) {
4214 a.s = NULL;
4215 save_tabs(t, &a);
4218 show_ca_status(t, uri);
4219 run_script(t, JS_HINTING);
4220 if (enable_autoscroll)
4221 run_script(t, JS_AUTOSCROLL);
4222 break;
4224 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
4225 /* 3 */
4226 if (color_visited_uris) {
4227 color_visited(t, color_visited_helper());
4230 * This colors the links you middle-click (open in new
4231 * tab) in the current tab.
4233 if (t->tab_id != gtk_notebook_get_current_page(notebook) &&
4234 (uri = get_uri(t)) != NULL)
4235 color_visited(get_current_tab(),
4236 g_strdup_printf("{'%s' : 'dummy'}", uri));
4238 break;
4240 case WEBKIT_LOAD_FINISHED:
4241 /* 2 */
4242 if ((uri = get_uri(t)) == NULL)
4243 return;
4245 * js_autorun calls get_uri which frees t->tmp_uri if on an
4246 * "about:" page. On "about:" pages, uri points to t->tmp_uri.
4247 * I.e. we will use freed memory. Prevent that.
4249 tmp_uri = g_strdup(uri);
4251 /* autorun some js if enabled */
4252 js_autorun(t);
4254 input_autofocus(t);
4256 if (!strncmp(tmp_uri, "http://", strlen("http://")) ||
4257 !strncmp(tmp_uri, "https://", strlen("https://")) ||
4258 !strncmp(tmp_uri, "file://", strlen("file://"))) {
4259 find.uri = (gchar *)tmp_uri;
4260 h = RB_FIND(history_list, &hl, &find);
4261 if (!h)
4262 insert_history_item(tmp_uri,
4263 get_title(t, FALSE), time(NULL));
4264 else
4265 h->time = time(NULL);
4268 if (statusbar_style == XT_STATUSBAR_URL)
4269 set_status(t, "%s", (char *)tmp_uri);
4270 else
4271 set_status(t, "%s", get_title(t, FALSE));
4272 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4273 #if GTK_CHECK_VERSION(2, 20, 0)
4274 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4275 gtk_widget_hide(t->spinner);
4276 #endif
4277 g_free(tmp_uri);
4278 break;
4280 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4281 case WEBKIT_LOAD_FAILED:
4282 /* 4 */
4283 if (!t->download_requested) {
4284 gtk_label_set_text(GTK_LABEL(t->label),
4285 get_title(t, FALSE));
4286 gtk_label_set_text(GTK_LABEL(t->tab_elems.label),
4287 get_title(t, FALSE));
4288 set_status(t, "%s", (char *)get_title(t, FALSE));
4289 gtk_window_set_title(GTK_WINDOW(main_window),
4290 get_title(t, TRUE));
4291 } else {
4294 #endif
4295 default:
4296 #if GTK_CHECK_VERSION(2, 20, 0)
4297 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4298 gtk_widget_hide(t->spinner);
4299 #endif
4300 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4301 break;
4304 gtk_widget_set_sensitive(GTK_WIDGET(t->backward),
4305 can_go_back_for_real(t));
4307 gtk_widget_set_sensitive(GTK_WIDGET(t->forward),
4308 can_go_forward_for_real(t));
4311 void
4312 notify_title_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4314 const gchar *title = NULL, *win_title = NULL;
4316 title = get_title(t, FALSE);
4317 win_title = get_title(t, TRUE);
4318 if (title) {
4319 gtk_label_set_text(GTK_LABEL(t->label), title);
4320 gtk_label_set_text(GTK_LABEL(t->tab_elems.label), title);
4323 if (win_title && t->tab_id == gtk_notebook_get_current_page(notebook))
4324 gtk_window_set_title(GTK_WINDOW(main_window), win_title);
4327 char *
4328 get_domain(const gchar *host)
4330 size_t x;
4331 char *p;
4333 /* handle silly domains like .co.uk */
4335 if ((x = strlen(host)) <= 6)
4336 return (g_strdup(host));
4338 if (host[x - 3] == '.' && host[x - 6] == '.') {
4339 x = x - 7;
4340 while (x > 0)
4341 if (host[x] != '.')
4342 x--;
4343 else
4344 return (g_strdup(&host[x + 1]));
4347 /* find first . */
4348 p = g_strrstr(host, ".");
4349 if (p == NULL)
4350 return (g_strdup(""));
4351 p--;
4352 while (p > host)
4353 if (*p != '.')
4354 p--;
4355 else
4356 return (g_strdup(p + 1));
4358 return (g_strdup(host));
4361 void
4362 js_autorun(struct tab *t)
4364 SoupURI *su = NULL;
4365 const gchar *uri;
4366 size_t got_default = 0, got_host = 0;
4367 struct stat sb;
4368 char deff[PATH_MAX], hostf[PATH_MAX];
4369 char *js = NULL, *jsat, *domain = NULL;
4370 FILE *deffile = NULL, *hostfile = NULL;
4372 if (enable_js_autorun == 0)
4373 return;
4375 uri = get_uri(t);
4376 if (uri &&
4377 !(g_str_has_prefix(uri, "http://") ||
4378 g_str_has_prefix(uri, "https://")))
4379 goto done;
4381 su = soup_uri_new(uri);
4382 if (su == NULL)
4383 goto done;
4384 if (!SOUP_URI_VALID_FOR_HTTP(su))
4385 goto done;
4387 DNPRINTF(XT_D_JS, "%s: host: %s domain: %s\n", __func__,
4388 su->host, domain);
4389 domain = get_domain(su->host);
4391 snprintf(deff, sizeof deff, "%s" PS "default.js", js_dir);
4392 if ((deffile = fopen(deff, "r")) != NULL) {
4393 if (fstat(fileno(deffile), &sb) == -1) {
4394 show_oops(t, "can't stat default JS file");
4395 goto done;
4397 got_default = sb.st_size;
4400 /* try host first followed by domain */
4401 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, su->host);
4402 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4403 if ((hostfile = fopen(hostf, "r")) == NULL) {
4404 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, domain);
4405 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4406 if ((hostfile = fopen(hostf, "r")) == NULL)
4407 goto nofile;
4409 DNPRINTF(XT_D_JS, "file: %s\n", hostf);
4410 if (fstat(fileno(hostfile), &sb) == -1) {
4411 show_oops(t, "can't stat %s JS file", hostf);
4412 goto done;
4414 got_host = sb.st_size;
4416 nofile:
4417 if (got_default + got_host == 0)
4418 goto done;
4420 js = g_malloc0(got_default + got_host + 1);
4421 jsat = js;
4423 if (got_default) {
4424 if (fread(js, got_default, 1, deffile) != 1) {
4425 show_oops(t, "default file read error");
4426 goto done;
4428 jsat = js + got_default;
4431 if (got_host) {
4432 if (fread(jsat, got_host, 1, hostfile) != 1) {
4433 show_oops(t, "host file read error");
4434 goto done;
4438 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
4439 run_script(t, js);
4441 done:
4442 if (su)
4443 soup_uri_free(su);
4444 if (js)
4445 g_free(js);
4446 if (deffile)
4447 fclose(deffile);
4448 if (hostfile)
4449 fclose(hostfile);
4450 if (domain)
4451 g_free(domain);
4454 void
4455 webview_progress_changed_cb(WebKitWebView *wv, GParamSpec *pspec, struct tab *t)
4457 gdouble progress;
4459 progress = webkit_web_view_get_progress(wv);
4460 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
4461 progress == 1.0 ? 0 : progress);
4462 gtk_entry_set_progress_fraction(GTK_ENTRY(t->uri_entry),
4463 progress == 1.0 ? 0 : progress);
4465 update_statusbar_position(NULL, NULL);
4469 strict_transport_rb_cmp(struct strict_transport *a, struct strict_transport *b)
4471 char *p1, *p2;
4472 int l1, l2;
4474 /* compare strings from the end */
4475 l1 = strlen(a->host);
4476 l2 = strlen(b->host);
4478 p1 = a->host + l1;
4479 p2 = b->host + l2;
4480 for (; *p1 == *p2 && p1 > a->host && p2 > b->host;
4481 p1--, p2--)
4485 * Check if we need to do pattern expansion,
4486 * or if we're just keeping the tree in order
4488 if (a->flags & XT_STS_FLAGS_EXPAND &&
4489 b->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) {
4490 /* Check if we're matching the
4491 * 'host.xyz' part in '*.host.xyz'
4493 if (p2 == b->host && (p1 == a->host || *(p1-1) == '.')) {
4494 return (0);
4498 if (p1 == a->host && p2 == b->host)
4499 return (0);
4500 if (p1 == a->host)
4501 return (1);
4502 if (p2 == b->host)
4503 return (-1);
4505 if (*p1 < *p2)
4506 return (-1);
4507 if (*p1 > *p2)
4508 return (1);
4510 return (0);
4512 RB_GENERATE(strict_transport_tree, strict_transport, entry,
4513 strict_transport_rb_cmp);
4516 strict_transport_add(const char *domain, time_t timeout, int subdomains)
4518 struct strict_transport *d, find;
4519 time_t now;
4520 FILE *f;
4522 if (enable_strict_transport == FALSE)
4523 return (0);
4525 DPRINTF("strict_transport_add(%s,%" PRIi64 ",%d)\n", domain,
4526 (uint64_t)timeout, subdomains);
4528 now = time(NULL);
4529 if (timeout < now)
4530 return (0);
4532 find.host = (char *)domain;
4533 find.flags = 0;
4534 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4536 /* update flags */
4537 if (d) {
4538 /* check if update is needed */
4539 if (d->timeout == timeout &&
4540 (d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) == subdomains)
4541 return (0);
4543 d->timeout = timeout;
4544 if (subdomains)
4545 d->flags |= XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4547 /* We're still initializing */
4548 if (strict_transport_file == NULL)
4549 return (0);
4551 if ((f = fopen(strict_transport_file, "w")) == NULL) {
4552 show_oops(NULL,
4553 "can't open strict-transport rules file");
4554 return (1);
4557 fprintf(f, "# Generated file - do not update unless you know "
4558 "what you're doing\n");
4559 RB_FOREACH(d, strict_transport_tree, &st_tree) {
4560 if (d->timeout < now)
4561 continue;
4562 fprintf(f, "%s\t%" PRIi64 "\t%d\n", d->host,
4563 (uint64_t)d->timeout,
4564 d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS);
4566 fclose(f);
4567 } else {
4568 d = g_malloc(sizeof *d);
4569 d->host= g_strdup(domain);
4570 d->timeout = timeout;
4571 if (subdomains)
4572 d->flags = XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4573 else
4574 d->flags = 0;
4575 RB_INSERT(strict_transport_tree, &st_tree, d);
4577 /* We're still initializing */
4578 if (strict_transport_file == NULL)
4579 return (0);
4581 if ((f = fopen(strict_transport_file, "a+")) == NULL) {
4582 show_oops(NULL,
4583 "can't open strict-transport rules file");
4584 return (1);
4587 fseek(f, 0, SEEK_END);
4588 fprintf(f,"%s\t%" PRIi64 "\t%d\n", d->host, (uint64_t)timeout,
4589 subdomains);
4590 fclose(f);
4592 return (0);
4596 strict_transport_check(const char *host)
4598 static struct strict_transport *d = NULL;
4599 struct strict_transport find;
4601 if (enable_strict_transport == FALSE)
4602 return (0);
4604 find.host = (char *)host;
4606 /* match for domains that include subdomains */
4607 find.flags = XT_STS_FLAGS_EXPAND;
4609 /* First, check if we're already at the right node */
4610 if (d != NULL && strict_transport_rb_cmp(&find, d) == 0) {
4611 return (1);
4614 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4615 if (d != NULL)
4616 return (1);
4618 return (0);
4622 strict_transport_init()
4624 char file[PATH_MAX];
4625 char delim[3];
4626 char *rule;
4627 char *ptr;
4628 FILE *f;
4629 size_t len;
4630 time_t timeout, now;
4631 int subdomains;
4633 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_STS_FILE);
4634 if ((f = fopen(file, "r")) == NULL) {
4635 strict_transport_file = g_strdup(file);
4636 return (0);
4639 delim[0] = '\\';
4640 delim[1] = '\\';
4641 delim[2] = '#';
4642 rule = NULL;
4643 now = time(NULL);
4645 for (;;) {
4646 if ((rule = fparseln(f, &len, NULL, delim, 0)) == NULL) {
4647 if (!feof(f) || ferror(f))
4648 goto corrupt_file;
4649 else
4650 break;
4653 /* get second entry */
4654 if ((ptr = strpbrk(rule, " \t")) == NULL)
4655 goto corrupt_file;
4657 *ptr++ = '\0';
4658 timeout = atoi(ptr);
4660 /* get third entry */
4661 if ((ptr = strpbrk(ptr, " \t")) == NULL)
4662 goto corrupt_file;
4664 ptr ++;
4665 subdomains = atoi(ptr);
4667 if (timeout > now)
4668 strict_transport_add(rule, timeout, subdomains);
4669 free(rule);
4672 fclose(f);
4673 strict_transport_file = g_strdup(file);
4674 return (0);
4676 corrupt_file:
4677 startpage_add("strict-transport rules file ('%s') is corrupt", file);
4678 if (rule)
4679 free(rule);
4680 fclose(f);
4681 return (1);
4685 force_https_check(const char *uri)
4687 struct wl_entry *w = NULL;
4689 if (uri == NULL)
4690 return (0);
4692 if ((w = wl_find_uri(uri, &force_https)) == NULL)
4693 return (0);
4694 else
4695 return (1);
4698 void
4699 strict_transport_security_cb(SoupMessage *msg, gpointer data)
4701 SoupURI *uri;
4702 const char *sts;
4703 char *ptr;
4704 time_t timeout = 0;
4705 int subdomains = FALSE;
4707 if (msg == NULL)
4708 return;
4710 sts = soup_message_headers_get_one(msg->response_headers,
4711 "Strict-Transport-Security");
4712 uri = soup_message_get_uri(msg);
4714 if (sts == NULL || uri == NULL)
4715 return;
4717 if ((ptr = strcasestr(sts, "max-age="))) {
4718 ptr += strlen("max-age=");
4719 timeout = atoll(ptr);
4720 } else
4721 return; /* malformed header - max-age must be included */
4723 if ((ptr = strcasestr(sts, "includeSubDomains")))
4724 subdomains = TRUE;
4726 strict_transport_add(uri->host, timeout + time(NULL), subdomains);
4729 void
4730 session_rq_cb(SoupSession *s, SoupMessage *msg, SoupSocket *socket,
4731 gpointer data)
4733 SoupURI *dest;
4734 SoupURI *ref_uri;
4735 const char *ref;
4737 char *ref_suffix;
4738 char *dest_suffix;
4740 if (s == NULL || msg == NULL)
4741 return;
4743 if (enable_strict_transport) {
4744 soup_message_add_header_handler(msg, "finished",
4745 "Strict-Transport-Security",
4746 G_CALLBACK(strict_transport_security_cb), NULL);
4749 if (referer_mode == XT_REFERER_ALWAYS)
4750 return;
4752 /* Check if referer is set - and what the user requested for referers */
4753 ref = soup_message_headers_get_one(msg->request_headers, "Referer");
4754 if (ref) {
4755 DNPRINTF(XT_D_NAV, "session_rq_cb: Referer: %s\n", ref);
4756 switch (referer_mode) {
4757 case XT_REFERER_NEVER:
4758 DNPRINTF(XT_D_NAV, "session_rq_cb: removing referer\n");
4759 soup_message_headers_remove(msg->request_headers,
4760 "Referer");
4761 break;
4762 case XT_REFERER_SAME_DOMAIN:
4763 ref_uri = soup_uri_new(ref);
4764 dest = soup_message_get_uri(msg);
4766 ref_suffix = tld_get_suffix(ref_uri->host);
4767 dest_suffix = tld_get_suffix(dest->host);
4769 if (dest && ref_suffix && dest_suffix &&
4770 strcmp(ref_suffix, dest_suffix) != 0) {
4771 soup_message_headers_remove(msg->request_headers,
4772 "Referer");
4773 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4774 "referer (not same domain) (suffixes: %s - %s)\n",
4775 ref_suffix, dest_suffix);
4777 soup_uri_free(ref_uri);
4778 break;
4779 case XT_REFERER_SAME_FQDN:
4780 ref_uri = soup_uri_new(ref);
4781 dest = soup_message_get_uri(msg);
4782 if (dest && strcmp(ref_uri->host, dest->host) != 0) {
4783 soup_message_headers_remove(msg->request_headers,
4784 "Referer");
4785 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4786 "referer (not same fqdn) (should be %s)\n",
4787 dest->host);
4789 soup_uri_free(ref_uri);
4790 break;
4791 case XT_REFERER_CUSTOM:
4792 DNPRINTF(XT_D_NAV, "session_rq_cb: setting referer "
4793 "to %s\n", referer_custom);
4794 soup_message_headers_replace(msg->request_headers,
4795 "Referer", referer_custom);
4796 break;
4802 webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf,
4803 WebKitNetworkRequest *request, WebKitWebNavigationAction *na,
4804 WebKitWebPolicyDecision *pd, struct tab *t)
4806 WebKitWebNavigationReason reason;
4807 char *uri;
4809 if (t == NULL) {
4810 show_oops(NULL, "webview_npd_cb invalid parameters");
4811 return (FALSE);
4814 DNPRINTF(XT_D_NAV, "webview_npd_cb: ctrl_click %d %s\n",
4815 t->ctrl_click,
4816 webkit_network_request_get_uri(request));
4818 uri = (char *)webkit_network_request_get_uri(request);
4820 if (!auto_load_images && t->load_images) {
4822 /* Disable autoloading of images, now that we're done loading
4823 * them. */
4824 g_object_set(G_OBJECT(t->settings),
4825 "auto-load-images", FALSE, (char *)NULL);
4826 webkit_web_view_set_settings(t->wv, t->settings);
4828 t->load_images = FALSE;
4831 /* If this is an xtp url, we don't load anything else. */
4832 if (parse_xtp_url(t, uri)) {
4833 webkit_web_policy_decision_ignore(pd);
4834 return (TRUE);
4837 if (parse_custom_uri(t, uri)) {
4838 webkit_web_policy_decision_ignore(pd);
4839 return (TRUE);
4842 if (valid_url_type(uri)) {
4843 show_oops(t, "Stopping attempt to load an invalid URI (possible"
4844 " bait and switch attack)");
4845 webkit_web_policy_decision_ignore(pd);
4846 return (TRUE);
4849 if ((t->mode == XT_MODE_HINT && t->new_tab) || t->ctrl_click) {
4850 t->ctrl_click = 0;
4851 create_new_tab(uri, NULL, ctrl_click_focus, -1);
4852 webkit_web_policy_decision_ignore(pd);
4853 return (TRUE); /* we made the decission */
4857 * This is a little hairy but it comes down to this:
4858 * when we run in whitelist mode we have to assist the browser in
4859 * opening the URL that it would have opened in a new tab.
4861 reason = webkit_web_navigation_action_get_reason(na);
4862 if (reason == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
4863 set_normal_tab_meaning(t);
4864 if (enable_scripts == 0 && enable_cookie_whitelist == 1)
4865 load_uri(t, uri);
4866 webkit_web_policy_decision_use(pd);
4867 return (TRUE); /* we made the decision */
4870 return (FALSE);
4873 void
4874 webview_rrs_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitWebResource *res,
4875 WebKitNetworkRequest *request, WebKitNetworkResponse *response,
4876 struct tab *t)
4878 SoupMessage *msg = NULL;
4879 SoupURI *uri = NULL;
4880 struct http_accept ha_find, *ha = NULL;
4881 struct user_agent ua_find, *ua = NULL;
4882 struct domain_id di_find, *di = NULL;
4883 char *uri_s = NULL;
4885 msg = webkit_network_request_get_message(request);
4886 if (!msg)
4887 return;
4889 uri = soup_message_get_uri(msg);
4890 if (!uri)
4891 return;
4892 uri_s = soup_uri_to_string(uri, FALSE);
4894 if (strcmp(uri->scheme, SOUP_URI_SCHEME_HTTP) == 0) {
4895 if (strict_transport_check(uri->host) ||
4896 force_https_check(uri_s)) {
4897 DNPRINTF(XT_D_NAV, "webview_rrs_cb: force https for %s\n",
4898 uri->host);
4899 soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTPS);
4903 if (do_not_track)
4904 soup_message_headers_append(msg->request_headers, "DNT", "1");
4907 * Check if resources on this domain have been loaded before. If
4908 * not, add the current tab's http-accept and user-agent id's to a
4909 * new domain_id and insert into the RB tree. Use these http headers
4910 * for all resources loaded from this domain for the lifetime of the
4911 * browser.
4913 if ((di_find.domain = uri->host) == NULL)
4914 goto done;
4915 if ((di = RB_FIND(domain_id_list, &di_list, &di_find)) == NULL) {
4916 di = g_malloc(sizeof *di);
4917 di->domain = g_strdup(uri->host);
4918 di->ua_id = t->user_agent_id++;
4919 di->ha_id = t->http_accept_id++;
4920 RB_INSERT(domain_id_list, &di_list, di);
4922 ua_find.id = t->user_agent_id;
4923 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4924 if (ua == NULL)
4925 t->user_agent_id = 0;
4927 ha_find.id = t->http_accept_id;
4928 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4929 if (ha == NULL)
4930 t->http_accept_id = 0;
4933 ua_find.id = di->ua_id;
4934 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4935 ha_find.id = di->ha_id;
4936 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4938 if (ua != NULL)
4939 soup_message_headers_replace(msg->request_headers,
4940 "User-Agent", ua->value);
4941 if (ha != NULL)
4942 soup_message_headers_replace(msg->request_headers,
4943 "Accept", ha->value);
4945 done:
4946 if (uri_s)
4947 g_free(uri_s);
4950 WebKitWebView *
4951 webview_cwv_cb(WebKitWebView *wv, WebKitWebFrame *wf, struct tab *t)
4953 struct tab *tt;
4954 struct wl_entry *w = NULL;
4955 const gchar *uri;
4956 WebKitWebView *webview = NULL;
4957 int x = 1;
4959 DNPRINTF(XT_D_NAV, "webview_cwv_cb: %s\n",
4960 webkit_web_view_get_uri(wv));
4962 if (tabless) {
4963 /* open in current tab */
4964 webview = t->wv;
4965 } else if (enable_scripts == 0 && enable_js_whitelist == 1) {
4966 uri = webkit_web_view_get_uri(wv);
4967 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
4968 return (NULL);
4970 if (t->ctrl_click) {
4971 x = ctrl_click_focus;
4972 t->ctrl_click = 0;
4974 tt = create_new_tab(NULL, NULL, x, -1);
4975 webview = tt->wv;
4976 } else if (enable_scripts == 1) {
4977 if (t->ctrl_click) {
4978 x = ctrl_click_focus;
4979 t->ctrl_click = 0;
4981 tt = create_new_tab(NULL, NULL, x, -1);
4982 webview = tt->wv;
4985 return (webview);
4988 gboolean
4989 webview_closewv_cb(WebKitWebView *wv, struct tab *t)
4991 const gchar *uri;
4992 struct wl_entry *w = NULL;
4994 DNPRINTF(XT_D_NAV, "webview_close_cb: %d\n", t->tab_id);
4996 if (enable_scripts == 0 && enable_cookie_whitelist == 1) {
4997 uri = webkit_web_view_get_uri(wv);
4998 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
4999 return (FALSE);
5001 delete_tab(t);
5002 } else if (enable_scripts == 1)
5003 delete_tab(t);
5005 return (TRUE);
5009 webview_event_cb(GtkWidget *w, GdkEventButton *e, struct tab *t)
5011 /* we can not eat the event without throwing gtk off so defer it */
5013 /* catch middle click */
5014 if (e->type == GDK_BUTTON_RELEASE && e->button == 2) {
5015 t->ctrl_click = 1;
5016 goto done;
5019 /* catch ctrl click */
5020 if (e->type == GDK_BUTTON_RELEASE &&
5021 CLEAN(e->state) == GDK_CONTROL_MASK)
5022 t->ctrl_click = 1;
5023 else
5024 t->ctrl_click = 0;
5025 done:
5026 return (XT_CB_PASSTHROUGH);
5030 run_mimehandler(struct tab *t, char *mime_type, WebKitNetworkRequest *request)
5032 struct mime_type *m;
5033 char *sv[3];
5034 GError *gerr = NULL;
5036 m = find_mime_type(mime_type);
5037 if (m == NULL)
5038 return (1);
5039 if (m->mt_download)
5040 return (1);
5042 sv[0] = m->mt_action;
5043 sv[1] = (char *)webkit_network_request_get_uri(request);
5044 sv[2] = NULL;
5046 /* ignore donothing from example config */
5047 if (m->mt_action && !strcmp(m->mt_action, "donothing"))
5048 return (0);
5050 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
5051 NULL, &gerr))
5052 show_oops(t, "%s: could not spawn process (%s)", __func__,
5053 gerr ? gerr->message : "N/A");
5054 return (0);
5057 char *
5058 get_mime_type(const char *uri)
5060 GFileInfo *fi;
5061 GFile *gf;
5062 const gchar *m;
5063 char *mime_type = NULL;
5064 char *file;
5066 if (uri == NULL) {
5067 show_oops(NULL, "%s: invalid parameters", __func__);
5068 return (NULL);
5071 if (g_str_has_prefix(uri, "file://"))
5072 file = g_filename_from_uri(uri, NULL, NULL);
5073 else
5074 file = g_strdup(uri);
5076 if (file == NULL)
5077 return (NULL);
5079 gf = g_file_new_for_path(file);
5080 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
5081 NULL, NULL);
5082 if ((m = g_file_info_get_content_type(fi)) != NULL)
5083 mime_type = g_strdup(m);
5084 g_object_unref(fi);
5085 g_object_unref(gf);
5086 g_free(file);
5088 return (mime_type);
5092 run_download_mimehandler(char *mime_type, char *file)
5094 struct mime_type *m;
5095 char *sv[3];
5097 m = find_mime_type(mime_type);
5098 if (m == NULL)
5099 return (1);
5101 sv[0] = m->mt_action;
5102 sv[1] = file;
5103 sv[2] = NULL;
5104 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
5105 NULL, NULL)) {
5106 show_oops(NULL, "%s: could not spawn process: %s %s", __func__,
5107 sv[0], sv[1]);
5108 return (1);
5110 return (0);
5113 void
5114 download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
5115 WebKitWebView *wv)
5117 WebKitDownloadStatus status;
5118 const char *uri;
5119 char *file = NULL;
5120 char *mime = NULL;
5122 if (download == NULL)
5123 return;
5124 status = webkit_download_get_status(download);
5125 if (status != WEBKIT_DOWNLOAD_STATUS_FINISHED)
5126 return;
5128 if (download_notifications)
5129 show_oops(NULL, "Download of '%s' finished",
5130 basename(webkit_download_get_destination_uri(download)));
5131 uri = webkit_download_get_destination_uri(download);
5132 if (uri == NULL)
5133 return;
5134 mime = get_mime_type(uri);
5135 if (mime == NULL)
5136 return;
5138 if (g_str_has_prefix(uri, "file://"))
5139 file = g_filename_from_uri(uri, NULL, NULL);
5140 else
5141 file = g_strdup(uri);
5143 if (file == NULL)
5144 goto done;
5146 run_download_mimehandler((char *)mime, file);
5148 done:
5149 g_free(mime);
5153 webview_mimetype_cb(WebKitWebView *wv, WebKitWebFrame *frame,
5154 WebKitNetworkRequest *request, char *mime_type,
5155 WebKitWebPolicyDecision *decision, struct tab *t)
5157 if (t == NULL) {
5158 show_oops(NULL, "webview_mimetype_cb invalid parameters");
5159 return (FALSE);
5162 DNPRINTF(XT_D_DOWNLOAD, "webview_mimetype_cb: tab %d mime %s\n",
5163 t->tab_id, mime_type);
5165 if (run_mimehandler(t, mime_type, request) == 0) {
5166 webkit_web_policy_decision_ignore(decision);
5167 focus_webview(t);
5168 return (TRUE);
5171 if (webkit_web_view_can_show_mime_type(wv, mime_type) == FALSE) {
5172 webkit_web_policy_decision_download(decision);
5173 return (TRUE);
5176 return (FALSE);
5180 download_start(struct tab *t, struct download *d, int flag)
5182 WebKitNetworkRequest *req;
5183 struct stat sb;
5184 const gchar *suggested_name;
5185 gchar *filename = NULL;
5186 char *uri = NULL;
5187 char *path = NULL;
5188 int ret = TRUE;
5189 int i;
5191 if (d == NULL || t == NULL) {
5192 show_oops(NULL, "%s invalid parameters", __func__);
5193 return (FALSE);
5196 suggested_name = webkit_download_get_suggested_filename(d->download);
5197 if (suggested_name == NULL)
5198 return (FALSE); /* abort download */
5200 i = 0;
5201 do {
5202 if (filename) {
5203 g_free(filename);
5204 filename = NULL;
5206 if (i) {
5207 g_free(uri);
5208 uri = NULL;
5209 filename = g_strdup_printf("%d%s", i, suggested_name);
5211 #ifdef __MINGW32__
5212 /* XXX using urls doesn't work properly in windows? */
5213 uri = g_strdup_printf("%s\\%s", download_dir, i ?
5214 filename : suggested_name);
5215 #else
5216 path = g_strdup_printf("%s" PS "%s", download_dir, i ?
5217 filename : suggested_name);
5218 if ((uri = g_filename_to_uri(path, NULL, NULL)) == NULL)
5219 break; /* XXX */
5220 #endif
5221 i++;
5222 #ifdef __MINGW32__
5223 } while (!stat(uri, &sb));
5224 #else
5225 } while (!stat(path, &sb));
5226 #endif
5228 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d filename %s "
5229 "local %s\n", __func__, t->tab_id, filename, uri);
5231 /* if we're restarting the download, or starting
5232 * it after doing something else, we need to recreate
5233 * the download request.
5235 if (flag == XT_DL_RESTART) {
5236 req = webkit_network_request_new(webkit_download_get_uri(d->download));
5237 webkit_download_cancel(d->download);
5238 g_object_unref(d->download);
5239 d->download = webkit_download_new(req);
5240 g_object_unref(req);
5243 webkit_download_set_destination_uri(d->download, uri);
5245 if (webkit_download_get_status(d->download) ==
5246 WEBKIT_DOWNLOAD_STATUS_ERROR) {
5247 show_oops(t, "%s: download failed to start", __func__);
5248 ret = FALSE;
5249 show_oops(t, "Download Failed");
5250 } else {
5251 /* connect "download first" mime handler */
5252 g_signal_connect(G_OBJECT(d->download), "notify::status",
5253 G_CALLBACK(download_status_changed_cb), NULL);
5255 /* get from history */
5256 g_object_ref(d->download);
5257 show_oops(t, "Download of '%s' started...",
5258 basename(webkit_download_get_destination_uri(d->download)));
5261 if (flag != XT_DL_START)
5262 webkit_download_start(d->download);
5264 DNPRINTF(XT_D_DOWNLOAD, "download status : %d",
5265 webkit_download_get_status(d->download));
5267 /* sync other download manager tabs */
5268 update_download_tabs(NULL);
5270 if (uri)
5271 g_free(uri);
5272 if (path)
5273 g_free(path);
5274 if (filename)
5275 g_free(filename);
5277 return (ret);
5281 download_ask_cb(struct tab *t, GdkEventKey *e, gpointer data)
5283 struct download *d = data;
5285 /* unset mode */
5286 t->mode_cb = NULL;
5287 t->mode_cb_data = NULL;
5289 if (!e || !d) {
5290 e->keyval = GDK_Escape;
5291 return (XT_CB_PASSTHROUGH);
5294 DPRINTF("download_ask_cb: User pressed %c\n", e->keyval);
5295 if (e->keyval == 'y' || e->keyval == 'Y' || e->keyval == GDK_Return)
5296 /* We need to do a RESTART, because we're not calling from
5297 * webview_download_cb
5299 download_start(t, d, XT_DL_RESTART);
5301 /* for all other keyvals, we just let the download be */
5302 e->keyval = GDK_Escape;
5303 return (XT_CB_HANDLED);
5307 download_ask(struct tab *t, struct download *d)
5309 const gchar *suggested_name;
5311 suggested_name = webkit_download_get_suggested_filename(d->download);
5312 if (suggested_name == NULL)
5313 return (FALSE); /* abort download */
5315 show_oops(t, "download file %s [y/n] ?", suggested_name);
5316 t->mode_cb = download_ask_cb;
5317 t->mode_cb_data = d;
5319 return (TRUE);
5323 webview_download_cb(WebKitWebView *wv, WebKitDownload *wk_download,
5324 struct tab *t)
5326 const gchar *suggested_name;
5327 struct download *download_entry;
5328 int ret = TRUE;
5330 if (wk_download == NULL || t == NULL) {
5331 show_oops(NULL, "%s invalid parameters", __func__);
5332 return (FALSE);
5335 suggested_name = webkit_download_get_suggested_filename(wk_download);
5336 if (suggested_name == NULL)
5337 return (FALSE); /* abort download */
5339 download_entry = g_malloc(sizeof(struct download));
5340 download_entry->download = wk_download;
5341 download_entry->tab = t;
5342 download_entry->id = next_download_id++;
5343 RB_INSERT(download_list, &downloads, download_entry);
5344 t->download_requested = 1;
5346 if (download_mode == XT_DM_START)
5347 ret = download_start(t, download_entry, XT_DL_START);
5348 else if (download_mode == XT_DM_ASK)
5349 ret = download_ask(t, download_entry);
5350 else if (download_mode == XT_DM_ADD)
5351 show_oops(t, "added %s to download manager",
5352 suggested_name);
5354 /* sync other download manager tabs */
5355 update_download_tabs(NULL);
5358 * NOTE: never redirect/render the current tab before this
5359 * function returns. This will cause the download to never start.
5361 return (ret); /* start download */
5364 void
5365 webview_hover_cb(WebKitWebView *wv, gchar *title, gchar *uri, struct tab *t)
5367 DNPRINTF(XT_D_KEY, "webview_hover_cb: %s %s\n", title, uri);
5369 if (t == NULL) {
5370 show_oops(NULL, "webview_hover_cb");
5371 return;
5374 if (uri)
5375 set_status(t, "Link: %s", uri);
5376 else {
5377 if (statusbar_style == XT_STATUSBAR_URL) {
5378 const gchar *page_uri;
5380 if ((page_uri = get_uri(t)) != NULL)
5381 set_status(t, "%s", page_uri);
5382 } else
5383 set_status(t, "%s", (char *)get_title(t, FALSE));
5388 mark(struct tab *t, struct karg *arg)
5390 char mark;
5391 int index;
5392 double pos;
5394 mark = arg->s[1];
5395 if ((index = marktoindex(mark)) == -1)
5396 return (-1);
5398 if (arg->i == XT_MARK_SET)
5399 t->mark[index] = gtk_adjustment_get_value(t->adjust_v);
5400 else if (arg->i == XT_MARK_GOTO) {
5401 if (t->mark[index] == XT_INVALID_MARK) {
5402 show_oops(t, "mark '%c' does not exist", mark);
5403 return (-1);
5405 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5406 something changes the document size */
5407 pos = gtk_adjustment_get_value(t->adjust_v);
5408 gtk_adjustment_set_value(t->adjust_v, t->mark[index]);
5409 t->mark[marktoindex('\'')] = pos;
5412 return (0);
5415 void
5416 marks_clear(struct tab *t)
5418 int i;
5420 for (i = 0; i < LENGTH(t->mark); i++)
5421 t->mark[i] = XT_INVALID_MARK;
5425 qmarks_load(void)
5427 char file[PATH_MAX];
5428 char *line = NULL, *p;
5429 int index, i;
5430 FILE *f;
5431 size_t linelen;
5433 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5434 if ((f = fopen(file, "r+")) == NULL) {
5435 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5436 return (1);
5439 for (i = 1; ; i++) {
5440 if ((line = fparseln(f, &linelen, NULL, NULL, 0)) == NULL)
5441 break;
5442 if (strlen(line) == 0 || line[0] == '#') {
5443 free(line);
5444 line = NULL;
5445 continue;
5448 p = strtok(line, " \t");
5450 if (p == NULL || strlen(p) != 1 ||
5451 (index = qmarktoindex(*p)) == -1) {
5452 warnx("corrupt quickmarks file, line %d", i);
5453 break;
5456 p = strtok(NULL, " \t");
5457 if (qmarks[index] != NULL)
5458 g_free(qmarks[index]);
5459 qmarks[index] = g_strdup(p);
5462 fclose(f);
5464 return (0);
5468 qmarks_save(void)
5470 char file[PATH_MAX];
5471 int i;
5472 FILE *f;
5474 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5475 if ((f = fopen(file, "r+")) == NULL) {
5476 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5477 return (1);
5480 for (i = 0; i < XT_NOQMARKS; i++)
5481 if (qmarks[i] != NULL)
5482 fprintf(f, "%c %s\n", indextoqmark(i), qmarks[i]);
5484 fclose(f);
5486 return (0);
5490 qmark(struct tab *t, struct karg *arg)
5492 char mark;
5493 int index;
5495 mark = arg->s[strlen(arg->s)-1];
5496 index = qmarktoindex(mark);
5497 if (index == -1)
5498 return (-1);
5500 switch (arg->i) {
5501 case XT_QMARK_SET:
5502 if (qmarks[index] != NULL) {
5503 g_free(qmarks[index]);
5504 qmarks[index] = NULL;
5507 qmarks_load(); /* sync if multiple instances */
5508 qmarks[index] = g_strdup(get_uri(t));
5509 qmarks_save();
5510 break;
5511 case XT_QMARK_OPEN:
5512 if (qmarks[index] != NULL)
5513 load_uri(t, qmarks[index]);
5514 else {
5515 show_oops(t, "quickmark \"%c\" does not exist",
5516 mark);
5517 return (-1);
5519 break;
5520 case XT_QMARK_TAB:
5521 if (qmarks[index] != NULL)
5522 create_new_tab(qmarks[index], NULL, 1, -1);
5523 else {
5524 show_oops(t, "quickmark \"%c\" does not exist",
5525 mark);
5526 return (-1);
5528 break;
5531 return (0);
5535 go_up(struct tab *t, struct karg *args)
5537 int levels;
5538 int lastidx;
5539 char *uri;
5540 char *tmp;
5541 char *p;
5543 if (args->i == XT_GO_UP_ROOT)
5544 levels = XT_GO_UP_ROOT;
5545 else if ((levels = atoi(args->s)) == 0)
5546 levels = 1;
5548 uri = g_strdup(get_uri(t));
5549 if (uri == NULL)
5550 return (1);
5552 if ((tmp = strstr(uri, XT_PROTO_DELIM)) == NULL)
5553 return (1);
5555 tmp += strlen(XT_PROTO_DELIM);
5557 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5558 lastidx = strlen(tmp) - 1;
5559 if (lastidx > 0) {
5560 if (tmp[lastidx] == '/')
5561 tmp[lastidx] = '\0';
5564 while (levels--) {
5565 p = strrchr(tmp, '/');
5566 if (p == tmp) { /* Are we at the root of a file://-path? */
5567 p[1] = '\0';
5568 break;
5569 } else if (p != NULL)
5570 *p = '\0';
5571 else
5572 break;
5575 load_uri(t, uri);
5576 g_free(uri);
5578 return (0);
5582 gototab(struct tab *t, struct karg *args)
5584 int tab;
5585 struct karg arg = {0, NULL, -1};
5587 tab = atoi(args->s);
5589 if (args->i == 0)
5590 arg.i = XT_TAB_NEXT;
5591 else
5592 arg.i = args->i;
5594 arg.precount = tab;
5596 movetab(t, &arg);
5598 return (0);
5602 zoom_amount(struct tab *t, struct karg *arg)
5604 struct karg narg = {0, NULL, -1};
5606 narg.i = atoi(arg->s);
5607 resizetab(t, &narg);
5609 return (0);
5613 flip_colon(struct tab *t, struct karg *arg)
5615 struct karg narg = {0, NULL, -1};
5616 char *p;
5618 if (t == NULL || arg == NULL)
5619 return (1);
5621 p = strstr(arg->s, ":");
5622 if (p == NULL)
5623 return (1);
5624 *p = '\0';
5626 narg.i = ':';
5627 narg.s = arg->s;
5628 command(t, &narg);
5630 return (0);
5633 /* buffer commands receive the regex that triggered them in arg.s */
5634 char bcmd[XT_BUFCMD_SZ];
5635 struct buffercmd {
5636 char *regex;
5637 int precount;
5638 #define XT_PRE_NO (0)
5639 #define XT_PRE_YES (1)
5640 #define XT_PRE_MAYBE (2)
5641 char *cmd;
5642 int (*func)(struct tab *, struct karg *);
5643 int arg;
5644 regex_t cregex;
5645 } buffercmds[] = {
5646 { "^[0-9]*gu$", XT_PRE_MAYBE, "gu", go_up, 0 },
5647 { "^gU$", XT_PRE_NO, "gU", go_up, XT_GO_UP_ROOT },
5648 { "^gg$", XT_PRE_NO, "gg", move, XT_MOVE_TOP },
5649 { "^gG$", XT_PRE_NO, "gG", move, XT_MOVE_BOTTOM },
5650 { "^[0-9]+%$", XT_PRE_YES, "%", move, XT_MOVE_PERCENT },
5651 { "^zz$", XT_PRE_NO, "zz", move, XT_MOVE_CENTER },
5652 { "^gh$", XT_PRE_NO, "gh", go_home, 0 },
5653 { "^m[a-zA-Z0-9]$", XT_PRE_NO, "m", mark, XT_MARK_SET },
5654 { "^['][a-zA-Z0-9']$", XT_PRE_NO, "'", mark, XT_MARK_GOTO },
5655 { "^[0-9]+t$", XT_PRE_YES, "t", gototab, 0 },
5656 { "^g0$", XT_PRE_YES, "g0", movetab, XT_TAB_FIRST },
5657 { "^g[$]$", XT_PRE_YES, "g$", movetab, XT_TAB_LAST },
5658 { "^[0-9]*gt$", XT_PRE_YES, "t", movetab, XT_TAB_NEXT },
5659 { "^[0-9]*gT$", XT_PRE_YES, "T", movetab, XT_TAB_PREV },
5660 { "^M[a-zA-Z0-9]$", XT_PRE_NO, "M", qmark, XT_QMARK_SET },
5661 { "^go[a-zA-Z0-9]$", XT_PRE_NO, "go", qmark, XT_QMARK_OPEN },
5662 { "^gn[a-zA-Z0-9]$", XT_PRE_NO, "gn", qmark, XT_QMARK_TAB },
5663 { "^ZR$", XT_PRE_NO, "ZR", restart, 0 },
5664 { "^ZZ$", XT_PRE_NO, "ZZ", quit, 0 },
5665 { "^zi$", XT_PRE_NO, "zi", resizetab, XT_ZOOM_IN },
5666 { "^zo$", XT_PRE_NO, "zo", resizetab, XT_ZOOM_OUT },
5667 { "^z0$", XT_PRE_NO, "z0", resizetab, XT_ZOOM_NORMAL },
5668 { "^[0-9]+Z$", XT_PRE_YES, "Z", zoom_amount, 0 },
5669 { "^[0-9]+:$", XT_PRE_YES, ":", flip_colon, 0 },
5672 void
5673 buffercmd_init(void)
5675 int i;
5677 for (i = 0; i < LENGTH(buffercmds); i++)
5678 if (regcomp(&buffercmds[i].cregex, buffercmds[i].regex,
5679 REG_EXTENDED | REG_NOSUB))
5680 startpage_add("invalid buffercmd regex %s",
5681 buffercmds[i].regex);
5684 void
5685 buffercmd_abort(struct tab *t)
5687 int i;
5689 if (t == NULL)
5690 return;
5692 DNPRINTF(XT_D_BUFFERCMD, "%s: clearing buffer\n", __func__);
5694 for (i = 0; i < LENGTH(bcmd); i++)
5695 bcmd[i] = '\0';
5697 cmd_prefix = 0; /* clear prefix for non-buffer commands */
5698 if (t->sbe.buffercmd)
5699 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5702 void
5703 buffercmd_execute(struct tab *t, struct buffercmd *cmd)
5705 struct karg arg = {0, NULL, -1};
5707 arg.i = cmd->arg;
5708 arg.s = g_strdup(bcmd);
5710 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_execute: buffer \"%s\" "
5711 "matches regex \"%s\", executing\n", bcmd, cmd->regex);
5712 cmd->func(t, &arg);
5714 if (arg.s)
5715 g_free(arg.s);
5717 buffercmd_abort(t);
5720 gboolean
5721 buffercmd_addkey(struct tab *t, guint keyval)
5723 int i, c, match ;
5724 char s[XT_BUFCMD_SZ];
5726 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers))) {
5727 buffercmd_abort(t);
5728 return (XT_CB_PASSTHROUGH);
5731 if (keyval == GDK_Escape) {
5732 buffercmd_abort(t);
5733 return (XT_CB_HANDLED);
5736 /* key with modifier or non-ascii character */
5737 if (!isascii(keyval)) {
5739 * XXX this looks wrong but fixes some sites like
5740 * http://www.seslisozluk.com/
5741 * that eat a shift or ctrl and end putting default focus in js
5742 * instead of ignoring the keystroke
5743 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5745 return (XT_CB_HANDLED);
5748 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: adding key \"%c\" "
5749 "to buffer \"%s\"\n", keyval, bcmd);
5751 for (i = 0; i < LENGTH(bcmd); i++)
5752 if (bcmd[i] == '\0') {
5753 bcmd[i] = keyval;
5754 break;
5757 /* buffer full, ignore input */
5758 if (i >= LENGTH(bcmd) -1) {
5759 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: buffer full\n");
5760 buffercmd_abort(t);
5761 return (XT_CB_HANDLED);
5764 if (t->sbe.buffercmd != NULL)
5765 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5767 /* find exact match */
5768 for (i = 0; i < LENGTH(buffercmds); i++)
5769 if (regexec(&buffercmds[i].cregex, bcmd,
5770 (size_t) 0, NULL, 0) == 0) {
5771 buffercmd_execute(t, &buffercmds[i]);
5772 goto done;
5775 /* find non exact matches to see if we need to abort ot not */
5776 for (i = 0, match = 0; i < LENGTH(buffercmds); i++) {
5777 DNPRINTF(XT_D_BUFFERCMD, "trying: %s\n", bcmd);
5778 c = -1;
5779 s[0] = '\0';
5780 if (buffercmds[i].precount == XT_PRE_MAYBE) {
5781 if (isdigit(bcmd[0])) {
5782 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5783 continue;
5784 } else {
5785 c = 0;
5786 if (sscanf(bcmd, "%s", s) == 0)
5787 continue;
5789 } else if (buffercmds[i].precount == XT_PRE_YES) {
5790 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5791 continue;
5792 } else {
5793 if (sscanf(bcmd, "%s", s) == 0)
5794 continue;
5796 if (c == -1 && buffercmds[i].precount)
5797 continue;
5798 if (!strncmp(s, buffercmds[i].cmd, strlen(s)))
5799 match++;
5801 DNPRINTF(XT_D_BUFFERCMD, "got[%d] %d <%s>: %d %s\n",
5802 i, match, buffercmds[i].cmd, c, s);
5804 if (match == 0) {
5805 DNPRINTF(XT_D_BUFFERCMD, "aborting: %s\n", bcmd);
5806 buffercmd_abort(t);
5809 done:
5810 return (XT_CB_HANDLED);
5814 * XXX we were seeing a bunch of focus issues with the toplevel
5815 * main_window losing its is-active and has-toplevel-focus properties.
5816 * This is the most correct and portable solution we could come up with
5817 * without relying on calling internal GTK functions (which we
5818 * couldn't link to in Linux).
5820 #if GTK_CHECK_VERSION(3, 0, 0)
5821 void
5822 fake_focus_in(GtkWidget *w)
5824 if (fevent == NULL) {
5825 fevent = gdk_event_new(GDK_FOCUS_CHANGE);
5826 fevent->focus_change.window =
5827 gtk_widget_get_window(main_window);
5828 fevent->focus_change.type = GDK_FOCUS_CHANGE;
5829 fevent->focus_change.in = TRUE;
5831 gtk_widget_send_focus_change(main_window, fevent);
5833 #endif
5835 gboolean
5836 handle_keypress(struct tab *t, GdkEventKey *e, int entry)
5838 struct karg args;
5839 struct key_binding *k;
5842 * This sometimes gets randomly unset for whatever reason in GTK3.
5843 * If we're handling a keypress, the main window's is-active propery
5844 * *must* be true, or else many things will break.
5846 #if GTK_CHECK_VERSION(3, 0, 0)
5847 fake_focus_in(main_window);
5848 #endif
5850 /* handle keybindings if buffercmd is empty.
5851 if not empty, allow commands like C-n */
5852 if (bcmd[0] == '\0' || ((e->state & (CTRL | MOD1)) != 0))
5853 TAILQ_FOREACH(k, &kbl, entry)
5854 if (e->keyval == k->key
5855 && (entry ? k->use_in_entry : 1)) {
5856 /* when we are edditing eat ctrl/mod keys */
5857 if (edit_mode == XT_EM_VI &&
5858 t->mode == XT_MODE_INSERT &&
5859 (e->state & CTRL || e->state & MOD1))
5860 return (XT_CB_PASSTHROUGH);
5862 if (k->mask == 0) {
5863 if ((e->state & (CTRL | MOD1)) == 0)
5864 goto runcmd;
5865 } else if ((e->state & k->mask) == k->mask) {
5866 goto runcmd;
5870 if (!entry && ((e->state & (CTRL | MOD1)) == 0))
5871 return (buffercmd_addkey(t, e->keyval));
5873 return (XT_CB_PASSTHROUGH);
5875 runcmd:
5876 if (k->cmd[0] == ':') {
5877 args.i = ':';
5878 args.s = &k->cmd[1];
5879 return (command(t, &args));
5880 } else
5881 return (cmd_execute(t, k->cmd));
5885 wv_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5887 char s[2];
5889 /* don't use w directly; use t->whatever instead */
5891 if (t == NULL) {
5892 show_oops(NULL, "wv_keypress_cb");
5893 return (XT_CB_PASSTHROUGH);
5896 hide_oops(t);
5898 if (t->mode_cb)
5899 return (t->mode_cb(t, e, t->mode_cb_data));
5901 DNPRINTF(XT_D_KEY, "wv_keypress_cb: mode %d keyval 0x%x mask "
5902 "0x%x tab %d\n", t->mode, e->keyval, e->state, t->tab_id);
5904 /* Hide buffers, if they are visible, with escape. */
5905 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)) &&
5906 CLEAN(e->state) == 0 && e->keyval == GDK_Escape) {
5907 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
5908 hide_buffers(t);
5909 return (XT_CB_HANDLED);
5912 if ((CLEAN(e->state) == 0 && e->keyval == GDK_Tab) ||
5913 (CLEAN(e->state) == SHFT && e->keyval == GDK_Tab))
5914 /* something focussy is about to happen */
5915 return (XT_CB_PASSTHROUGH);
5917 /* check if we are some sort of text input thing in the dom */
5918 input_check_mode(t);
5920 if (t->mode == XT_MODE_PASSTHROUGH) {
5921 if (CLEAN(e->state) == 0 && e->keyval == GDK_Escape)
5922 t->mode = XT_MODE_COMMAND;
5923 return (XT_CB_PASSTHROUGH);
5924 } else if (t->mode == XT_MODE_COMMAND || t->mode == XT_MODE_HINT) {
5925 /* prefix input */
5926 snprintf(s, sizeof s, "%c", e->keyval);
5927 if (CLEAN(e->state) == 0 && isdigit(s[0]))
5928 cmd_prefix = 10 * cmd_prefix + atoi(s);
5929 return (handle_keypress(t, e, 0));
5930 } else {
5931 /* insert mode */
5932 return (handle_keypress(t, e, 1));
5935 /* NOTREACHED */
5936 return (XT_CB_PASSTHROUGH);
5939 gboolean
5940 hint_continue(struct tab *t)
5942 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5943 char *s;
5944 const gchar *errstr = NULL;
5945 gboolean rv = TRUE;
5946 int i;
5948 if (!(c[0] == '.' || c[0] == ','))
5949 goto done;
5950 if (strlen(c) == 1) {
5951 /* XXX should not happen */
5952 rv = TRUE;
5953 goto done;
5956 if (isdigit(c[1])) {
5957 /* numeric input */
5958 i = strtonum(&c[1], 1, 4096, &errstr);
5959 if (errstr) {
5960 show_oops(t, "invalid numerical hint %s", &c[1]);
5961 goto done;
5963 s = g_strdup_printf("hints.updateHints(%d);", i);
5964 run_script(t, s);
5965 g_free(s);
5966 } else {
5967 /* alphanumeric input */
5968 s = g_strdup_printf("hints.createHints('%s', '%c');",
5969 &c[1], c[0] == '.' ? 'f' : 'F');
5970 run_script(t, s);
5971 g_free(s);
5974 rv = FALSE;
5975 done:
5976 return (rv);
5979 gboolean
5980 search_continue(struct tab *t)
5982 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5983 gboolean rv = FALSE;
5985 if (c[0] == ':' || c[0] == '.' || c[0] == ',')
5986 goto done;
5987 if (strlen(c) == 1) {
5988 webkit_web_view_unmark_text_matches(t->wv);
5989 goto done;
5992 if (c[0] == '/')
5993 t->search_forward = TRUE;
5994 else if (c[0] == '?')
5995 t->search_forward = FALSE;
5996 else
5997 goto done;
5999 rv = TRUE;
6000 done:
6001 return (rv);
6004 gboolean
6005 search_cb(struct tab *t)
6007 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
6008 #if !GTK_CHECK_VERSION(3, 0, 0)
6009 GdkColor color;
6010 #endif
6012 if (search_continue(t) == FALSE)
6013 goto done;
6015 /* search */
6016 if (webkit_web_view_search_text(t->wv, &c[1], FALSE, t->search_forward,
6017 TRUE) == FALSE) {
6018 /* not found, mark red */
6019 #if GTK_CHECK_VERSION(3, 0, 0)
6020 gtk_widget_set_name(t->cmd, XT_CSS_RED);
6021 #else
6022 gdk_color_parse(XT_COLOR_RED, &color);
6023 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL, &color);
6024 #endif
6025 /* unmark and remove selection */
6026 webkit_web_view_unmark_text_matches(t->wv);
6027 /* my kingdom for a way to unselect text in webview */
6028 } else {
6029 /* found, highlight all */
6030 webkit_web_view_unmark_text_matches(t->wv);
6031 webkit_web_view_mark_text_matches(t->wv, &c[1], FALSE, 0);
6032 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
6033 #if GTK_CHECK_VERSION(3, 0, 0)
6034 gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
6035 #else
6036 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
6037 &t->default_style->base[GTK_STATE_NORMAL]);
6038 #endif
6040 done:
6041 t->search_id = 0;
6042 return (FALSE);
6046 cmd_keyrelease_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6048 const gchar *c = gtk_entry_get_text(w);
6050 if (t == NULL) {
6051 show_oops(NULL, "cmd_keyrelease_cb invalid parameters");
6052 return (XT_CB_PASSTHROUGH);
6055 DNPRINTF(XT_D_CMD, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
6056 e->keyval, e->state, t->tab_id);
6058 /* hinting */
6059 if (!(e->keyval == GDK_Tab || e->keyval == GDK_ISO_Left_Tab)) {
6060 if (hint_continue(t) == FALSE)
6061 goto done;
6064 /* search */
6065 if (search_continue(t) == FALSE)
6066 goto done;
6068 /* if search length is > 4 then no longer play timeout games */
6069 if (strlen(c) > 4) {
6070 if (t->search_id) {
6071 g_source_remove(t->search_id);
6072 t->search_id = 0;
6074 search_cb(t);
6075 goto done;
6078 /* reestablish a new timer if the user types fast */
6079 if (t->search_id)
6080 g_source_remove(t->search_id);
6081 t->search_id = g_timeout_add(250, (GSourceFunc)search_cb, (gpointer)t);
6083 done:
6084 return (XT_CB_PASSTHROUGH);
6087 gboolean
6088 match_uri(const gchar *uri, const gchar *key) {
6089 gchar *voffset;
6090 size_t len;
6091 gboolean match = FALSE;
6093 len = strlen(key);
6095 if (!strncmp(key, uri, len))
6096 match = TRUE;
6097 else {
6098 voffset = strstr(uri, "/") + 2;
6099 if (!strncmp(key, voffset, len))
6100 match = TRUE;
6101 else if (g_str_has_prefix(voffset, "www.")) {
6102 voffset = voffset + strlen("www.");
6103 if (!strncmp(key, voffset, len))
6104 match = TRUE;
6108 return (match);
6111 gboolean
6112 match_session(const gchar *name, const gchar *key) {
6113 char *sub;
6115 sub = strcasestr(name, key);
6117 return sub == name;
6120 void
6121 cmd_getlist(int id, char *key)
6123 int i, dep, c = 0;
6124 struct history *h;
6125 struct session *s;
6127 if (id >= 0) {
6128 if (cmds[id].type & XT_URLARG) {
6129 RB_FOREACH_REVERSE(h, history_list, &hl)
6130 if (match_uri(h->uri, key)) {
6131 cmd_status.list[c] = (char *)h->uri;
6132 if (++c > 255)
6133 break;
6135 cmd_status.len = c;
6136 return;
6137 } else if (cmds[id].type & XT_SESSARG) {
6138 TAILQ_FOREACH(s, &sessions, entry)
6139 if (match_session(s->name, key)) {
6140 cmd_status.list[c] = (char *)s->name;
6141 if (++c > 255)
6142 break;
6144 cmd_status.len = c;
6145 return;
6146 } else if (cmds[id].type & XT_SETARG) {
6147 for (i = 0; i < get_settings_size(); i++)
6148 if (!strncmp(key, get_setting_name(i),
6149 strlen(key)))
6150 cmd_status.list[c++] =
6151 get_setting_name(i);
6152 cmd_status.len = c;
6153 return;
6157 dep = (id == -1) ? 0 : cmds[id].level + 1;
6159 for (i = id + 1; i < LENGTH(cmds); i++) {
6160 if (cmds[i].level < dep)
6161 break;
6162 if (cmds[i].level == dep && !strncmp(key, cmds[i].cmd,
6163 strlen(key)) && !isdigit(cmds[i].cmd[0]))
6164 cmd_status.list[c++] = cmds[i].cmd;
6168 cmd_status.len = c;
6171 char *
6172 cmd_getnext(int dir)
6174 cmd_status.index += dir;
6176 if (cmd_status.index < 0)
6177 cmd_status.index = cmd_status.len - 1;
6178 else if (cmd_status.index >= cmd_status.len)
6179 cmd_status.index = 0;
6181 return cmd_status.list[cmd_status.index];
6185 cmd_tokenize(char *s, char *tokens[])
6187 int i = 0;
6188 char *tok, *last = NULL;
6189 size_t len = strlen(s);
6190 bool blank;
6192 blank = len == 0 || (len > 0 && s[len - 1] == ' ');
6193 for (tok = strtok_r(s, " ", &last); tok && i < 3;
6194 tok = strtok_r(NULL, " ", &last), i++)
6195 tokens[i] = tok;
6197 if (blank && i < 3)
6198 tokens[i++] = "";
6200 return (i);
6203 void
6204 cmd_complete(struct tab *t, char *str, int dir)
6206 GtkEntry *w = GTK_ENTRY(t->cmd);
6207 int i, j, levels, c = 0, dep = 0, parent = -1;
6208 int matchcount = 0;
6209 char *tok, *match, *s = g_strdup(str);
6210 char *tokens[3];
6211 char res[XT_MAX_URL_LENGTH + 32] = ":";
6212 char *sc = s;
6214 DNPRINTF(XT_D_CMD, "%s: complete %s\n", __func__, str);
6216 /* copy prefix*/
6217 for (i = 0; isdigit(s[i]); i++)
6218 res[i + 1] = s[i];
6220 for (; isspace(s[i]); i++)
6221 res[i + 1] = s[i];
6223 s += i;
6225 levels = cmd_tokenize(s, tokens);
6227 for (i = 0; i < levels - 1; i++) {
6228 tok = tokens[i];
6229 matchcount = 0;
6230 for (j = c; j < LENGTH(cmds); j++) {
6231 if (cmds[j].level < dep)
6232 break;
6233 if (cmds[j].level == dep && !strncmp(tok, cmds[j].cmd,
6234 strlen(tok))) {
6235 matchcount++;
6236 c = j + 1;
6237 if (strlen(tok) == strlen(cmds[j].cmd)) {
6238 matchcount = 1;
6239 break;
6244 if (matchcount == 1) {
6245 strlcat(res, tok, sizeof res);
6246 strlcat(res, " ", sizeof res);
6247 dep++;
6248 } else {
6249 g_free(sc);
6250 return;
6253 parent = c - 1;
6256 if (cmd_status.index == -1)
6257 cmd_getlist(parent, tokens[i]);
6259 if (cmd_status.len > 0) {
6260 match = cmd_getnext(dir);
6261 strlcat(res, match, sizeof res);
6262 gtk_entry_set_text(w, res);
6263 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6266 g_free(sc);
6269 char *
6270 parse_prefix_and_alias(const char *str, int *prefix)
6272 struct cmd_alias *c;
6273 char *s = g_strdup(str), *sc;
6275 g_strstrip(s);
6276 sc = s;
6278 if (isdigit(s[0])) {
6279 sscanf(s, "%d", prefix);
6280 while (isdigit(s[0]) || isspace(s[0]))
6281 ++s;
6284 TAILQ_FOREACH(c, &cal, entry) {
6285 if (strncmp(s, c->alias, strlen(c->alias)))
6286 continue;
6288 if (strlen(s) == strlen(c->alias)) {
6289 g_free(sc);
6290 return (g_strdup(c->cmd));
6293 if (!isspace(s[strlen(c->alias)]))
6294 continue;
6296 s = g_strdup_printf("%s %s", c->cmd, &s[strlen(c->alias) + 1]);
6297 g_free(sc);
6298 return (s);
6300 s = g_strdup(s);
6301 g_free(sc);
6302 return (s);
6305 gboolean
6306 cmd_execute(struct tab *t, char *str)
6308 struct cmd *cmd = NULL;
6309 char *tok, *last = NULL, *s = str;
6310 int j = 0, len, c = 0, dep = 0, matchcount = 0;
6311 int prefix = -1, rv = XT_CB_PASSTHROUGH;
6312 struct karg arg = {0, NULL, -1};
6314 s = parse_prefix_and_alias(s, &prefix);
6316 for (tok = strtok_r(s, " ", &last); tok;
6317 tok = strtok_r(NULL, " ", &last)) {
6318 matchcount = 0;
6319 for (j = c; j < LENGTH(cmds); j++) {
6320 if (cmds[j].level < dep)
6321 break;
6322 len = (tok[strlen(tok) - 1] == '!') ? strlen(tok) - 1 :
6323 strlen(tok);
6324 if (cmds[j].level == dep &&
6325 !strncmp(tok, cmds[j].cmd, len)) {
6326 matchcount++;
6327 c = j + 1;
6328 cmd = &cmds[j];
6329 if (len == strlen(cmds[j].cmd)) {
6330 matchcount = 1;
6331 break;
6335 if (matchcount == 1) {
6336 if (cmd->type > 0)
6337 goto execute_cmd;
6338 dep++;
6339 } else {
6340 show_oops(t, "Invalid command: %s", str);
6341 goto done;
6344 execute_cmd:
6345 if (cmd == NULL) {
6346 show_oops(t, "Empty command");
6347 goto done;
6349 arg.i = cmd->arg;
6351 if (prefix != -1)
6352 arg.precount = prefix;
6353 else if (cmd_prefix > 0)
6354 arg.precount = cmd_prefix;
6356 if (j > 0 && !(cmd->type & XT_PREFIX) && arg.precount > -1) {
6357 show_oops(t, "No prefix allowed: %s", str);
6358 goto done;
6360 if (cmd->type > 1)
6361 arg.s = last ? g_strdup(last) : g_strdup("");
6362 if (cmd->type & XT_INTARG && last && strlen(last) > 0) {
6363 if (arg.s == NULL) {
6364 show_oops(t, "Invalid command");
6365 goto done;
6367 arg.precount = atoi(arg.s);
6368 if (arg.precount <= 0) {
6369 if (arg.s[0] == '0')
6370 show_oops(t, "Zero count");
6371 else
6372 show_oops(t, "Trailing characters");
6373 goto done;
6377 DNPRINTF(XT_D_CMD, "%s: prefix %d arg %s\n",
6378 __func__, arg.precount, arg.s);
6380 cmd->func(t, &arg);
6382 rv = XT_CB_HANDLED;
6383 done:
6384 if (j > 0)
6385 cmd_prefix = 0;
6386 g_free(s);
6387 if (arg.s)
6388 g_free(arg.s);
6390 return (rv);
6394 save_runtime_setting(const char *name, const char *val)
6396 struct stat sb;
6397 FILE *f;
6398 size_t linelen;
6399 int found = 0;
6400 char file[PATH_MAX];
6401 char delim[3] = { '\0', '\0', '\0' };
6402 char *line, *lt, *start;
6403 char *contents, *tmp;
6405 if (runtime_settings == NULL || strlen(runtime_settings) == 0)
6406 return (-1);
6408 snprintf(file, sizeof file, "%s" PS "%s", work_dir, runtime_settings);
6409 if (stat(file, &sb) || (f = fopen(file, "r+")) == NULL)
6410 return (-1);
6411 lt = g_strdup_printf("%s=%s", name, val);
6412 contents = g_strdup("");
6413 while (!feof(f)) {
6414 line = fparseln(f, &linelen, NULL, delim, 0);
6415 if (line == NULL || linelen == 0)
6416 continue;
6417 tmp = contents;
6418 start = g_strdup_printf("%s=", name);
6419 if (strstr(line, start) == NULL)
6420 contents = g_strdup_printf("%s%s\n", contents, line);
6421 else {
6422 found = 1;
6423 contents = g_strdup_printf("%s%s\n", contents, lt);
6425 g_free(tmp);
6426 g_free(start);
6427 free(line);
6428 line = NULL;
6430 if (found == 0) {
6431 tmp = contents;
6432 contents = g_strdup_printf("%s%s\n", contents, lt);
6433 g_free(tmp);
6435 if ((f = freopen(file, "w", f)) == NULL)
6436 return (-1);
6437 else {
6438 fputs(contents, f);
6439 fclose(f);
6441 g_free(lt);
6442 g_free(contents);
6444 return (0);
6448 entry_focus_cb(GtkWidget *w, GdkEvent e, struct tab *t)
6451 * This sometimes gets randomly unset for whatever reason in GTK3,
6452 * causing a GtkEntry's text cursor becomes invisible. When we focus
6453 * a GtkEntry, be sure to manually reset the main window's is-active
6454 * property so the cursor is shown correctly.
6456 #if GTK_CHECK_VERSION(3, 0, 0)
6457 fake_focus_in(main_window);
6458 #endif
6459 return (XT_CB_PASSTHROUGH);
6463 entry_key_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6465 if (t == NULL) {
6466 show_oops(NULL, "entry_key_cb invalid parameters");
6467 return (XT_CB_PASSTHROUGH);
6470 DNPRINTF(XT_D_CMD, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6471 e->keyval, e->state, t->tab_id);
6473 hide_oops(t);
6475 if (e->keyval == GDK_Escape) {
6476 /* don't use focus_webview(t) because we want to type :cmds */
6477 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
6480 return (handle_keypress(t, e, 1));
6483 struct command_entry *
6484 history_prev(struct command_list *l, struct command_entry *at)
6486 if (at == NULL)
6487 at = TAILQ_LAST(l, command_list);
6488 else {
6489 at = TAILQ_PREV(at, command_list, entry);
6490 if (at == NULL)
6491 at = TAILQ_LAST(l, command_list);
6494 return (at);
6497 struct command_entry *
6498 history_next(struct command_list *l, struct command_entry *at)
6500 if (at == NULL)
6501 at = TAILQ_FIRST(l);
6502 else {
6503 at = TAILQ_NEXT(at, entry);
6504 if (at == NULL)
6505 at = TAILQ_FIRST(l);
6508 return (at);
6512 cmd_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6514 int rv = XT_CB_HANDLED;
6515 const gchar *c = gtk_entry_get_text(w);
6516 char *s;
6518 if (t == NULL) {
6519 show_oops(NULL, "cmd_keypress_cb parameters");
6520 return (XT_CB_PASSTHROUGH);
6523 DNPRINTF(XT_D_CMD, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6524 e->keyval, e->state, t->tab_id);
6526 /* sanity */
6527 if (c == NULL)
6528 e->keyval = GDK_Escape;
6529 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6530 c[0] == '.' || c[0] == ','))
6531 e->keyval = GDK_Escape;
6533 if (e->keyval != GDK_Tab && e->keyval != GDK_Shift_L &&
6534 e->keyval != GDK_ISO_Left_Tab)
6535 cmd_status.index = -1;
6537 switch (e->keyval) {
6538 case GDK_Tab:
6539 if (c[0] == ':')
6540 cmd_complete(t, (char *)&c[1], 1);
6541 else if (c[0] == '.' || c[0] == ',')
6542 run_script(t, "hints.focusNextHint();");
6543 goto done;
6544 case GDK_ISO_Left_Tab:
6545 if (c[0] == ':')
6546 cmd_complete(t, (char *)&c[1], -1);
6547 else if (c[0] == '.' || c[0] == ',')
6548 run_script(t, "hints.focusPreviousHint();");
6549 goto done;
6550 case GDK_Down:
6551 if (c[0] == '?') {
6552 if ((search_at = history_next(&shl, search_at))) {
6553 search_at->line[0] = c[0];
6554 gtk_entry_set_text(w, search_at->line);
6555 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6557 } else if (c[0] == '/') {
6558 if ((search_at = history_prev(&shl, search_at))) {
6559 search_at->line[0] = c[0];
6560 gtk_entry_set_text(w, search_at->line);
6561 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6563 } else if (c[0] == ':') {
6564 if ((history_at = history_prev(&chl, history_at))) {
6565 history_at->line[0] = c[0];
6566 gtk_entry_set_text(w, history_at->line);
6567 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6570 goto done;
6571 case GDK_Up:
6572 if (c[0] == '/') {
6573 if ((search_at = history_next(&shl, search_at))) {
6574 search_at->line[0] = c[0];
6575 gtk_entry_set_text(w, search_at->line);
6576 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6578 } else if (c[0] == '?') {
6579 if ((search_at = history_prev(&shl, search_at))) {
6580 search_at->line[0] = c[0];
6581 gtk_entry_set_text(w, search_at->line);
6582 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6584 } if (c[0] == ':') {
6585 if ((history_at = history_next(&chl, history_at))) {
6586 history_at->line[0] = c[0];
6587 gtk_entry_set_text(w, history_at->line);
6588 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6591 goto done;
6592 case GDK_BackSpace:
6593 if (!(!strcmp(c, ":") || !strcmp(c, "/") || !strcmp(c, "?") ||
6594 !strcmp(c, ".") || !strcmp(c, ","))) {
6595 /* see if we are doing hinting and reset it */
6596 if (c[0] == '.' || c[0] == ',') {
6597 /* recreate hints */
6598 s = g_strdup_printf("hints.createHints('', "
6599 "'%c');", c[0] == '.' ? 'f' : 'F');
6600 run_script(t, s);
6601 g_free(s);
6603 break;
6606 /* FALLTHROUGH */
6607 case GDK_Escape:
6608 hide_cmd(t);
6610 /* cancel search */
6611 if (c != NULL && (c[0] == '/' || c[0] == '?'))
6612 webkit_web_view_unmark_text_matches(t->wv);
6614 /* no need to cancel hints */
6615 goto done;
6618 rv = XT_CB_PASSTHROUGH;
6619 done:
6620 return (rv);
6623 void
6624 wv_popup_activ_cb(GtkMenuItem *menu, struct tab *t)
6626 GtkAction *a = NULL;
6627 GtkClipboard *clipboard, *primary;
6628 const gchar *name, *uri;
6630 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
6631 if (a == NULL)
6632 return;
6633 name = gtk_action_get_name(a);
6635 DNPRINTF(XT_D_CMD, "wv_popup_activ_cb: tab %d action %s\n",
6636 t->tab_id, name);
6639 * context-menu-action-3 copy link location
6640 * context-menu-action-7 copy image address
6641 * context-menu-action-2030 copy video link location
6644 /* copy actions */
6645 if ((g_strcmp0(name, "context-menu-action-3") == 0) ||
6646 (g_strcmp0(name, "context-menu-action-7") == 0) ||
6647 (g_strcmp0(name, "context-menu-action-2030") == 0)) {
6648 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
6649 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
6650 uri = gtk_clipboard_wait_for_text(clipboard);
6651 if (uri == NULL)
6652 return;
6653 gtk_clipboard_set_text(primary, uri, -1);
6657 void
6658 wv_popup_cb(WebKitWebView *wview, GtkMenu *menu, struct tab *t)
6660 GList *items, *l;
6662 DNPRINTF(XT_D_CMD, "wv_popup_cb: tab %d\n", t->tab_id);
6664 items = gtk_container_get_children(GTK_CONTAINER(menu));
6665 for (l = items; l; l = l->next)
6666 g_signal_connect(l->data, "activate",
6667 G_CALLBACK(wv_popup_activ_cb), t);
6668 g_list_free(items);
6671 void
6672 cmd_popup_cb(GtkEntry *entry, GtkMenu *menu, struct tab *t)
6674 /* popup menu enabled */
6675 t->popup = 1;
6679 cmd_focusout_cb(GtkWidget *w, GdkEventFocus *e, struct tab *t)
6681 if (t == NULL) {
6682 show_oops(NULL, "cmd_focusout_cb invalid parameters");
6683 return (XT_CB_PASSTHROUGH);
6686 DNPRINTF(XT_D_CMD, "cmd_focusout_cb: tab %d popup %d\n",
6687 t->tab_id, t->popup);
6689 /* if popup is enabled don't lose focus */
6690 if (t->popup) {
6691 t->popup = 0;
6692 return (XT_CB_PASSTHROUGH);
6695 hide_cmd(t);
6696 hide_oops(t);
6697 disable_hints(t);
6699 return (XT_CB_PASSTHROUGH);
6702 void
6703 cmd_hide_cb(GtkWidget *w, struct tab *t)
6705 if (t == NULL) {
6706 show_oops(NULL, "%s: invalid parameters", __func__);
6707 return;
6710 if (show_url == 0 || t->focus_wv)
6711 focus_webview(t);
6712 else
6713 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
6716 void
6717 cmd_activate_cb(GtkEntry *entry, struct tab *t)
6719 char *s;
6720 const gchar *c = gtk_entry_get_text(entry);
6722 if (t == NULL) {
6723 show_oops(NULL, "cmd_activate_cb invalid parameters");
6724 return;
6727 DNPRINTF(XT_D_CMD, "cmd_activate_cb: tab %d %s\n", t->tab_id, c);
6729 /* sanity */
6730 if (c == NULL)
6731 goto done;
6732 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6733 c[0] == '.' || c[0] == ','))
6734 goto done;
6735 if (strlen(c) < 1)
6736 goto done;
6737 s = (char *)&c[1];
6739 if (c[0] == '/' || c[0] == '?') {
6740 /* see if there is a timer pending */
6741 if (t->search_id) {
6742 g_source_remove(t->search_id);
6743 t->search_id = 0;
6744 search_cb(t);
6747 if (t->search_text) {
6748 g_free(t->search_text);
6749 t->search_text = NULL;
6752 t->search_text = g_strdup(s);
6753 if (global_search)
6754 g_free(global_search);
6755 global_search = g_strdup(s);
6756 t->search_forward = c[0] == '/';
6758 history_add(&shl, search_file, s, &search_history_count);
6759 } else if (c[0] == '.' || c[0] == ',') {
6760 run_script(t, "hints.fire();");
6761 /* XXX history for link following? */
6762 } else if (c[0] == ':') {
6763 history_add(&chl, command_file, s, &cmd_history_count);
6764 /* can't call hide_cmd after cmd_execute */
6765 hide_cmd(t);
6766 cmd_execute(t, s);
6767 return;
6770 done:
6771 hide_cmd(t);
6774 void
6775 backward_cb(GtkWidget *w, struct tab *t)
6777 struct karg a;
6779 if (t == NULL) {
6780 show_oops(NULL, "backward_cb invalid parameters");
6781 return;
6784 DNPRINTF(XT_D_NAV, "backward_cb: tab %d\n", t->tab_id);
6786 a.i = XT_NAV_BACK;
6787 navaction(t, &a);
6790 void
6791 forward_cb(GtkWidget *w, struct tab *t)
6793 struct karg a;
6795 if (t == NULL) {
6796 show_oops(NULL, "forward_cb invalid parameters");
6797 return;
6800 DNPRINTF(XT_D_NAV, "forward_cb: tab %d\n", t->tab_id);
6802 a.i = XT_NAV_FORWARD;
6803 navaction(t, &a);
6806 void
6807 home_cb(GtkWidget *w, struct tab *t)
6809 if (t == NULL) {
6810 show_oops(NULL, "home_cb invalid parameters");
6811 return;
6814 DNPRINTF(XT_D_NAV, "home_cb: tab %d\n", t->tab_id);
6816 load_uri(t, home);
6819 void
6820 stop_cb(GtkWidget *w, struct tab *t)
6822 WebKitWebFrame *frame;
6824 if (t == NULL) {
6825 show_oops(NULL, "stop_cb invalid parameters");
6826 return;
6829 DNPRINTF(XT_D_NAV, "stop_cb: tab %d\n", t->tab_id);
6831 frame = webkit_web_view_get_main_frame(t->wv);
6832 if (frame == NULL) {
6833 show_oops(t, "stop_cb: no frame");
6834 return;
6837 webkit_web_frame_stop_loading(frame);
6838 abort_favicon_download(t);
6841 void
6842 setup_webkit(struct tab *t)
6844 if (is_g_object_setting(G_OBJECT(t->settings), "enable-dns-prefetching"))
6845 g_object_set(G_OBJECT(t->settings), "enable-dns-prefetching",
6846 FALSE, (char *)NULL);
6847 else
6848 warnx("webkit does not have \"enable-dns-prefetching\" property");
6849 g_object_set(G_OBJECT(t->settings), "default-encoding", encoding,
6850 (char *)NULL);
6851 g_object_set(G_OBJECT(t->settings),
6852 "enable-scripts", enable_scripts, (char *)NULL);
6853 g_object_set(G_OBJECT(t->settings),
6854 "enable-plugins", enable_plugins, (char *)NULL);
6855 g_object_set(G_OBJECT(t->settings),
6856 "javascript-can-open-windows-automatically", enable_scripts,
6857 (char *)NULL);
6858 g_object_set(G_OBJECT(t->settings),
6859 "enable-html5-database", FALSE, (char *)NULL);
6860 g_object_set(G_OBJECT(t->settings),
6861 "enable-html5-local-storage", enable_localstorage, (char *)NULL);
6862 g_object_set(G_OBJECT(t->settings),
6863 "enable_spell_checking", enable_spell_checking, (char *)NULL);
6864 g_object_set(G_OBJECT(t->settings),
6865 "spell_checking_languages", spell_check_languages, (char *)NULL);
6866 g_object_set(G_OBJECT(t->settings),
6867 "enable-developer-extras", TRUE, (char *)NULL);
6868 g_object_set(G_OBJECT(t->wv),
6869 "full-content-zoom", TRUE, (char *)NULL);
6870 g_object_set(G_OBJECT(t->settings),
6871 "auto-load-images", auto_load_images, (char *)NULL);
6872 if (is_g_object_setting(G_OBJECT(t->settings),
6873 "enable-display-of-insecure-content"))
6874 g_object_set(G_OBJECT(t->settings),
6875 "enable-display-of-insecure-content",
6876 allow_insecure_content, (char *)NULL);
6877 if (is_g_object_setting(G_OBJECT(t->settings),
6878 "enable-running-of-insecure-content"))
6879 g_object_set(G_OBJECT(t->settings),
6880 "enable-running-of-insecure-content",
6881 allow_insecure_scripts, (char *)NULL);
6883 webkit_web_view_set_settings(t->wv, t->settings);
6886 gboolean
6887 update_statusbar_position(GtkAdjustment* adjustment, gpointer data)
6889 struct tab *ti, *t = NULL;
6890 gdouble view_size, value, max;
6891 gchar *position;
6893 TAILQ_FOREACH(ti, &tabs, entry)
6894 if (ti->tab_id == gtk_notebook_get_current_page(notebook)) {
6895 t = ti;
6896 break;
6899 if (t == NULL)
6900 return FALSE;
6902 if (adjustment == NULL)
6903 adjustment = gtk_scrolled_window_get_vadjustment(
6904 GTK_SCROLLED_WINDOW(t->browser_win));
6906 view_size = gtk_adjustment_get_page_size(adjustment);
6907 value = gtk_adjustment_get_value(adjustment);
6908 max = gtk_adjustment_get_upper(adjustment) - view_size;
6910 if (max == 0)
6911 position = g_strdup("All");
6912 else if (value == max)
6913 position = g_strdup("Bot");
6914 else if (value == 0)
6915 position = g_strdup("Top");
6916 else
6917 position = g_strdup_printf("%d%%", (int) ((value / max) * 100));
6919 if (t->sbe.position != NULL)
6920 gtk_label_set_text(GTK_LABEL(t->sbe.position), position);
6921 g_free(position);
6923 return (TRUE);
6926 GtkWidget *
6927 create_window(const gchar *name)
6929 GtkWidget *w;
6931 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
6932 if (window_maximize)
6933 gtk_window_maximize(GTK_WINDOW(w));
6934 else
6935 gtk_window_set_default_size(GTK_WINDOW(w), window_width, window_height);
6936 gtk_widget_set_name(w, name);
6937 gtk_window_set_wmclass(GTK_WINDOW(w), name, "Xombrero");
6939 return (w);
6942 GtkWidget *
6943 create_browser(struct tab *t)
6945 GtkWidget *w;
6946 GtkAdjustment *adjustment;
6948 if (t == NULL) {
6949 show_oops(NULL, "create_browser invalid parameters");
6950 return (NULL);
6953 w = gtk_scrolled_window_new(NULL, NULL);
6954 gtk_widget_set_can_focus(w, FALSE);
6955 t->adjust_h = gtk_scrolled_window_get_hadjustment(
6956 GTK_SCROLLED_WINDOW(w));
6957 t->adjust_v = gtk_scrolled_window_get_vadjustment(
6958 GTK_SCROLLED_WINDOW(w));
6959 #if !GTK_CHECK_VERSION(3, 0, 0)
6960 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
6961 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
6962 #endif
6965 t->wv = WEBKIT_WEB_VIEW(webkit_web_view_new());
6966 gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(t->wv));
6968 /* set defaults */
6969 t->settings = webkit_web_view_get_settings(t->wv);
6970 t->stylesheet = g_strdup(stylesheet);
6971 t->load_images = auto_load_images;
6973 adjustment =
6974 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w));
6975 g_signal_connect(G_OBJECT(adjustment), "value-changed",
6976 G_CALLBACK(update_statusbar_position), NULL);
6978 setup_webkit(t);
6979 setup_inspector(t);
6981 return (w);
6984 GtkWidget *
6985 create_kiosk_toolbar(struct tab *t)
6987 GtkWidget *toolbar = NULL;
6988 GtkBox *b;
6990 #if GTK_CHECK_VERSION(3, 0, 0)
6991 toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
6992 gtk_widget_set_name(toolbar, "toolbar");
6993 #else
6994 toolbar = gtk_hbox_new(FALSE, 1);
6995 #endif
6997 b = GTK_BOX(toolbar);
6998 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
7000 /* backward button */
7001 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
7002 gtk_widget_set_sensitive(t->backward, FALSE);
7003 g_signal_connect(G_OBJECT(t->backward), "clicked",
7004 G_CALLBACK(backward_cb), t);
7005 gtk_box_pack_start(b, t->backward, TRUE, TRUE, 0);
7007 /* forward button */
7008 t->forward = create_button("Forward", GTK_STOCK_GO_FORWARD, 0);
7009 gtk_widget_set_sensitive(t->forward, FALSE);
7010 g_signal_connect(G_OBJECT(t->forward), "clicked",
7011 G_CALLBACK(forward_cb), t);
7012 gtk_box_pack_start(b, t->forward, TRUE, TRUE, 0);
7014 /* home button */
7015 t->gohome = create_button("Home", GTK_STOCK_HOME, 0);
7016 gtk_widget_set_sensitive(t->gohome, true);
7017 g_signal_connect(G_OBJECT(t->gohome), "clicked",
7018 G_CALLBACK(home_cb), t);
7019 gtk_box_pack_start(b, t->gohome, TRUE, TRUE, 0);
7022 * Create widgets but don't use them. This is just so we don't get
7023 * loads of errors when trying to do stuff with them. Sink the floating
7024 * reference here, and do a manual unreference when the tab is deleted.
7026 t->uri_entry = gtk_entry_new();
7027 g_object_ref_sink(G_OBJECT(t->uri_entry));
7028 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
7029 G_CALLBACK(entry_focus_cb), t);
7030 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
7031 g_object_ref_sink(G_OBJECT(t->stop));
7032 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
7033 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
7034 g_object_ref_sink(G_OBJECT(t->stop));
7036 #if !GTK_CHECK_VERSION(3, 0, 0)
7037 t->default_style = gtk_rc_get_style(t->uri_entry);
7038 #endif
7040 return (toolbar);
7043 GtkWidget *
7044 create_toolbar(struct tab *t)
7046 GtkWidget *toolbar = NULL;
7047 GtkBox *b;
7050 * t->stop, t->js_toggle, and t->uri_entry are shared by the kiosk
7051 * toolbar, but not shown or used. We still create them there so we can
7052 * avoid loads of warnings when trying to use them. Instead, we sink
7053 * the floating reference here, and do the final unreference when
7054 * deleting the tab.
7057 #if GTK_CHECK_VERSION(3, 0, 0)
7058 toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
7059 gtk_widget_set_name(toolbar, "toolbar");
7060 #else
7061 b = gtk_hbox_new(FALSE, 1);
7062 #endif
7064 b = GTK_BOX(toolbar);
7065 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
7067 /* backward button */
7068 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
7069 gtk_widget_set_sensitive(t->backward, FALSE);
7070 g_signal_connect(G_OBJECT(t->backward), "clicked",
7071 G_CALLBACK(backward_cb), t);
7072 gtk_box_pack_start(b, t->backward, FALSE, FALSE, 0);
7074 /* forward button */
7075 t->forward = create_button("Forward",GTK_STOCK_GO_FORWARD, 0);
7076 gtk_widget_set_sensitive(t->forward, FALSE);
7077 g_signal_connect(G_OBJECT(t->forward), "clicked",
7078 G_CALLBACK(forward_cb), t);
7079 gtk_box_pack_start(b, t->forward, FALSE, FALSE, 0);
7081 /* stop button */
7082 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
7083 g_object_ref_sink(G_OBJECT(t->stop));
7084 gtk_widget_set_sensitive(t->stop, FALSE);
7085 g_signal_connect(G_OBJECT(t->stop), "clicked", G_CALLBACK(stop_cb), t);
7086 gtk_box_pack_start(b, t->stop, FALSE, FALSE, 0);
7088 /* JS button */
7089 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
7090 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
7091 g_object_ref_sink(G_OBJECT(t->js_toggle));
7092 gtk_widget_set_sensitive(t->js_toggle, TRUE);
7093 g_signal_connect(G_OBJECT(t->js_toggle), "clicked",
7094 G_CALLBACK(js_toggle_cb), t);
7095 gtk_box_pack_start(b, t->js_toggle, FALSE, FALSE, 0);
7097 /* toggle proxy button */
7098 t->proxy_toggle = create_button("Proxy-Toggle", proxy_uri ?
7099 GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT, 0);
7100 /* override icons */
7101 if (proxy_uri)
7102 button_set_file(t->proxy_toggle, "torenabled.ico");
7103 else
7104 button_set_file(t->proxy_toggle, "tordisabled.ico");
7105 gtk_widget_set_sensitive(t->proxy_toggle, TRUE);
7106 g_signal_connect(G_OBJECT(t->proxy_toggle), "clicked",
7107 G_CALLBACK(proxy_toggle_cb), t);
7108 gtk_box_pack_start(b, t->proxy_toggle, FALSE, FALSE, 0);
7110 t->uri_entry = gtk_entry_new();
7111 g_object_ref_sink(G_OBJECT(t->uri_entry));
7112 g_signal_connect(G_OBJECT(t->uri_entry), "activate",
7113 G_CALLBACK(activate_uri_entry_cb), t);
7114 g_signal_connect(G_OBJECT(t->uri_entry), "key-press-event",
7115 G_CALLBACK(entry_key_cb), t);
7116 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
7117 G_CALLBACK(entry_focus_cb), t);
7118 completion_add(t);
7119 gtk_box_pack_start(b, t->uri_entry, TRUE, TRUE, 0);
7121 /* search entry */
7122 t->search_entry = gtk_entry_new();
7123 gtk_entry_set_width_chars(GTK_ENTRY(t->search_entry), 30);
7124 g_signal_connect(G_OBJECT(t->search_entry), "activate",
7125 G_CALLBACK(activate_search_entry_cb), t);
7126 g_signal_connect(G_OBJECT(t->search_entry), "key-press-event",
7127 G_CALLBACK(entry_key_cb), t);
7128 g_signal_connect(G_OBJECT(t->search_entry), "focus-in-event",
7129 G_CALLBACK(entry_focus_cb), t);
7130 gtk_box_pack_start(b, t->search_entry, FALSE, FALSE, 0);
7132 #if !GTK_CHECK_VERSION(3, 0, 0)
7133 t->default_style = gtk_rc_get_style(t->uri_entry);
7134 #endif
7136 return (toolbar);
7139 GtkWidget *
7140 create_buffers(struct tab *t)
7142 GtkCellRenderer *renderer;
7143 GtkWidget *view;
7145 view = gtk_tree_view_new();
7147 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
7149 renderer = gtk_cell_renderer_text_new();
7150 gtk_tree_view_insert_column_with_attributes
7151 (GTK_TREE_VIEW(view), -1, "Id", renderer, "text", COL_ID, (char *)NULL);
7153 renderer = gtk_cell_renderer_pixbuf_new();
7154 gtk_tree_view_insert_column_with_attributes
7155 (GTK_TREE_VIEW(view), -1, "Favicon", renderer, "pixbuf", COL_FAVICON,
7156 (char *)NULL);
7158 renderer = gtk_cell_renderer_text_new();
7159 gtk_tree_view_insert_column_with_attributes
7160 (GTK_TREE_VIEW(view), -1, "Title", renderer, "text", COL_TITLE,
7161 (char *)NULL);
7163 gtk_tree_view_set_model
7164 (GTK_TREE_VIEW(view), GTK_TREE_MODEL(buffers_store));
7166 return (view);
7169 void
7170 row_activated_cb(GtkTreeView *view, GtkTreePath *path,
7171 GtkTreeViewColumn *col, struct tab *t)
7173 GtkTreeIter iter;
7174 guint id;
7176 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7178 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter,
7179 path)) {
7180 gtk_tree_model_get
7181 (GTK_TREE_MODEL(buffers_store), &iter, COL_ID, &id, -1);
7182 set_current_tab(id - 1);
7185 hide_buffers(t);
7188 /* after tab reordering/creation/removal */
7189 void
7190 recalc_tabs(void)
7192 struct tab *t;
7194 TAILQ_FOREACH(t, &tabs, entry)
7195 t->tab_id = gtk_notebook_page_num(notebook, t->vbox);
7198 void
7199 update_statusbar_tabs(struct tab *t)
7201 int tab_id, max_tab_id;
7202 char s[16];
7204 if (t != NULL)
7205 tab_id = t->tab_id;
7206 else
7207 tab_id = gtk_notebook_get_current_page(notebook);
7209 max_tab_id = gtk_notebook_get_n_pages(notebook);
7210 snprintf(s, sizeof s, "%d/%d", tab_id + 1, max_tab_id);
7212 if (t == NULL)
7213 t = get_current_tab();
7215 if (t != NULL && t->sbe.tabs != NULL)
7216 gtk_label_set_text(GTK_LABEL(t->sbe.tabs), s);
7219 /* after active tab change */
7220 void
7221 recolor_compact_tabs(void)
7223 struct tab *t;
7224 int curid = 0;
7225 #if !GTK_CHECK_VERSION(3, 0, 0)
7226 GdkColor color_active, color_inactive;
7228 gdk_color_parse(XT_COLOR_CT_ACTIVE, &color_active);
7229 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color_inactive);
7230 #endif
7231 curid = gtk_notebook_get_current_page(notebook);
7233 TAILQ_FOREACH(t, &tabs, entry) {
7234 #if GTK_CHECK_VERSION(3, 0, 0)
7235 if (t->tab_id == curid)
7236 gtk_widget_set_name(t->tab_elems.label, XT_CSS_ACTIVE);
7237 else
7238 gtk_widget_set_name(t->tab_elems.label, "");
7239 #else
7240 if (t->tab_id == curid)
7241 gtk_widget_modify_fg(t->tab_elems.label,
7242 GTK_STATE_NORMAL, &color_active);
7243 else
7244 gtk_widget_modify_fg(t->tab_elems.label,
7245 GTK_STATE_NORMAL, &color_inactive);
7246 #endif
7250 void
7251 set_current_tab(int page_num)
7253 buffercmd_abort(get_current_tab());
7254 gtk_notebook_set_current_page(notebook, page_num);
7255 recolor_compact_tabs();
7256 update_statusbar_tabs(NULL);
7260 undo_close_tab_save(struct tab *t)
7262 int m, n;
7263 const gchar *uri;
7264 struct undo *u1, *u2;
7265 GList *items;
7266 GList *item;
7267 WebKitWebHistoryItem *el;
7269 if ((uri = get_uri(t)) == NULL)
7270 return (1);
7272 u1 = g_malloc0(sizeof(struct undo));
7273 u1->uri = g_strdup(uri);
7275 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7277 m = webkit_web_back_forward_list_get_forward_length(t->bfl);
7278 n = webkit_web_back_forward_list_get_back_length(t->bfl);
7279 u1->back = n;
7281 /* forward history */
7282 items = webkit_web_back_forward_list_get_forward_list_with_limit(t->bfl, m);
7283 for (item = g_list_first(items); item; item = item->next) {
7284 u1->history = g_list_prepend(u1->history,
7285 webkit_web_history_item_copy(item->data));
7287 g_list_free(items);
7289 /* current item */
7290 if (m) {
7291 el = webkit_web_back_forward_list_get_current_item(t->bfl);
7292 u1->history = g_list_prepend(u1->history,
7293 webkit_web_history_item_copy(el));
7296 /* back history */
7297 items = webkit_web_back_forward_list_get_back_list_with_limit(t->bfl, n);
7298 for (item = g_list_first(items); item; item = item->next) {
7299 u1->history = g_list_prepend(u1->history,
7300 webkit_web_history_item_copy(item->data));
7302 g_list_free(items);
7304 TAILQ_INSERT_HEAD(&undos, u1, entry);
7306 if (undo_count > XT_MAX_UNDO_CLOSE_TAB) {
7307 u2 = TAILQ_LAST(&undos, undo_tailq);
7308 TAILQ_REMOVE(&undos, u2, entry);
7309 g_free(u2->uri);
7310 g_list_free(u2->history);
7311 g_free(u2);
7312 } else
7313 undo_count++;
7315 return (0);
7318 void
7319 delete_tab(struct tab *t)
7321 struct karg a;
7323 DNPRINTF(XT_D_TAB, "delete_tab: %p\n", t);
7325 if (t == NULL)
7326 return;
7328 TAILQ_REMOVE(&tabs, t, entry);
7329 buffercmd_abort(t);
7331 /* Halt all webkit activity. */
7332 abort_favicon_download(t);
7333 webkit_web_view_stop_loading(t->wv);
7335 /* Save the tab, so we can undo the close. */
7336 undo_close_tab_save(t);
7338 /* just in case */
7339 if (t->search_id)
7340 g_source_remove(t->search_id);
7342 /* session key */
7343 if (t->session_key)
7344 g_free(t->session_key);
7346 /* inspector */
7347 bzero(&a, sizeof a);
7348 a.i = XT_INS_CLOSE;
7349 inspector_cmd(t, &a);
7351 if (browser_mode == XT_BM_KIOSK) {
7352 gtk_widget_destroy(t->uri_entry);
7353 gtk_widget_destroy(t->stop);
7354 gtk_widget_destroy(t->js_toggle);
7357 /* widgets not shown in the kiosk toolbar */
7358 gtk_widget_destroy(t->stop);
7359 g_object_unref(G_OBJECT(t->stop));
7360 gtk_widget_destroy(t->js_toggle);
7361 g_object_unref(G_OBJECT(t->js_toggle));
7362 gtk_widget_destroy(t->uri_entry);
7363 g_object_unref(G_OBJECT(t->uri_entry));
7365 g_object_unref(G_OBJECT(t->completion));
7366 if (t->item)
7367 g_object_unref(t->item);
7369 gtk_widget_destroy(t->tab_elems.eventbox);
7370 gtk_widget_destroy(t->vbox);
7372 g_free(t->stylesheet);
7373 g_free(t->tmp_uri);
7374 g_free(t->status);
7375 g_free(t);
7376 t = NULL;
7378 if (TAILQ_EMPTY(&tabs)) {
7379 if (browser_mode == XT_BM_KIOSK)
7380 create_new_tab(home, NULL, 1, -1);
7381 else
7382 create_new_tab(NULL, NULL, 1, -1);
7385 /* recreate session */
7386 if (session_autosave) {
7387 bzero(&a, sizeof a);
7388 a.s = NULL;
7389 save_tabs(NULL, &a);
7392 recalc_tabs();
7393 recolor_compact_tabs();
7396 void
7397 update_statusbar_zoom(struct tab *t)
7399 gfloat zoom;
7400 char s[16] = { '\0' };
7402 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7403 if ((zoom <= 0.99 || zoom >= 1.01))
7404 snprintf(s, sizeof s, "%d%%", (int)(zoom * 100));
7405 if (t->sbe.zoom != NULL)
7406 gtk_label_set_text(GTK_LABEL(t->sbe.zoom), s);
7409 void
7410 setzoom_webkit(struct tab *t, int adjust)
7412 #define XT_ZOOMPERCENT 0.04
7414 gfloat zoom;
7416 if (t == NULL) {
7417 show_oops(NULL, "setzoom_webkit invalid parameters");
7418 return;
7421 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7422 if (adjust == XT_ZOOM_IN)
7423 zoom += XT_ZOOMPERCENT;
7424 else if (adjust == XT_ZOOM_OUT)
7425 zoom -= XT_ZOOMPERCENT;
7426 else if (adjust > 0)
7427 zoom = default_zoom_level + adjust / 100.0 - 1.0;
7428 else {
7429 show_oops(t, "setzoom_webkit invalid zoom value");
7430 return;
7433 if (zoom < XT_ZOOMPERCENT)
7434 zoom = XT_ZOOMPERCENT;
7435 g_object_set(G_OBJECT(t->wv), "zoom-level", zoom, (char *)NULL);
7436 update_statusbar_zoom(t);
7439 gboolean
7440 tab_clicked_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
7442 struct tab *t = (struct tab *) data;
7444 DNPRINTF(XT_D_TAB, "tab_clicked_cb: tab: %d\n", t->tab_id);
7446 switch (event->button) {
7447 case 1:
7448 set_current_tab(t->tab_id);
7449 break;
7450 case 2:
7451 delete_tab(t);
7452 break;
7455 return TRUE;
7458 void
7459 append_tab(struct tab *t)
7461 if (t == NULL)
7462 return;
7464 TAILQ_INSERT_TAIL(&tabs, t, entry);
7465 t->tab_id = gtk_notebook_append_page(notebook, t->vbox, t->tab_content);
7468 GtkWidget *
7469 create_sbe(void)
7471 GtkWidget *sbe;
7473 sbe = gtk_label_new(NULL);
7474 gtk_widget_set_can_focus(GTK_WIDGET(sbe), FALSE);
7475 modify_font(GTK_WIDGET(sbe), statusbar_font);
7476 return (sbe);
7480 statusbar_create(struct tab *t)
7482 GtkWidget *box; /* container for statusbar elems */
7483 GtkWidget *sep;
7484 GtkBox *b;
7485 char *p;
7486 #if !GTK_CHECK_VERSION(3, 0, 0)
7487 GdkColor color;
7488 #endif
7490 if (t == NULL) {
7491 DPRINTF("%s: invalid parameters", __func__);
7492 return (1);
7495 #if GTK_CHECK_VERSION(3, 0, 0)
7496 t->statusbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7497 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7498 gtk_widget_set_name(GTK_WIDGET(t->statusbar), "statusbar");
7499 #else
7500 t->statusbar = gtk_hbox_new(FALSE, 0);
7501 box = gtk_hbox_new(FALSE, 0);
7502 #endif
7503 t->sbe.ebox = gtk_event_box_new();
7505 gtk_widget_set_can_focus(GTK_WIDGET(t->statusbar), FALSE);
7506 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.ebox), FALSE);
7507 gtk_widget_set_can_focus(GTK_WIDGET(box), FALSE);
7509 gtk_box_set_spacing(GTK_BOX(box), 10);
7510 gtk_box_pack_start(GTK_BOX(t->statusbar), t->sbe.ebox, TRUE, TRUE, 0);
7511 gtk_container_add(GTK_CONTAINER(t->sbe.ebox), box);
7513 t->sbe.uri = gtk_entry_new();
7514 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.uri), FALSE);
7515 modify_font(GTK_WIDGET(t->sbe.uri), statusbar_font);
7516 #if !GTK_CHECK_VERSION(3, 0, 0)
7517 gtk_entry_set_inner_border(GTK_ENTRY(t->sbe.uri), NULL);
7518 gtk_entry_set_has_frame(GTK_ENTRY(t->sbe.uri), FALSE);
7519 #endif
7521 #if GTK_CHECK_VERSION(3, 0, 0)
7522 statusbar_modify_attr(t, XT_CSS_NORMAL);
7523 #else
7524 statusbar_modify_attr(t, XT_COLOR_WHITE, XT_COLOR_BLACK);
7525 #endif
7527 b = GTK_BOX(box);
7528 gtk_box_pack_start(b, t->sbe.uri, TRUE, TRUE, 0);
7530 for (p = statusbar_elems; *p != '\0'; p++) {
7531 switch (*p) {
7532 case '|':
7533 #if GTK_CHECK_VERSION(3, 0, 0)
7534 sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
7535 #else
7536 sep = gtk_vseparator_new();
7537 gdk_color_parse(XT_COLOR_SB_SEPARATOR, &color);
7538 gtk_widget_modify_bg(sep, GTK_STATE_NORMAL, &color);
7539 #endif
7540 gtk_box_pack_start(b, sep, FALSE, FALSE, 0);
7541 break;
7542 case 'P':
7543 if (t->sbe.position == NULL) {
7544 t->sbe.position = create_sbe();
7545 gtk_box_pack_start(b, t->sbe.position, FALSE,
7546 FALSE, 0);
7548 break;
7549 case 'B':
7550 if (t->sbe.buffercmd == NULL) {
7551 t->sbe.buffercmd = create_sbe();
7552 gtk_box_pack_start(b, t->sbe.buffercmd, FALSE,
7553 FALSE, 0);
7555 break;
7556 case 'Z':
7557 if (t->sbe.zoom == NULL) {
7558 t->sbe.zoom = create_sbe();
7559 gtk_box_pack_start(b, t->sbe.zoom, FALSE, FALSE,
7562 break;
7563 case 'T':
7564 if (t->sbe.tabs == NULL) {
7565 t->sbe.tabs = create_sbe();
7566 gtk_box_pack_start(b, t->sbe.tabs, FALSE, FALSE,
7569 break;
7570 case 'p':
7571 if (t->sbe.proxy == NULL) {
7572 t->sbe.proxy = create_sbe();
7573 gtk_box_pack_start(b, t->sbe.proxy, FALSE,
7574 FALSE, 0);
7575 if (proxy_uri)
7576 gtk_entry_set_text(
7577 GTK_ENTRY(t->sbe.proxy), "proxy");
7579 break;
7580 default:
7581 warnx("illegal flag \"%c\" in statusbar_elems\n", *p);
7582 break;
7586 gtk_box_pack_end(GTK_BOX(t->vbox), t->statusbar, FALSE, FALSE, 0);
7588 return (0);
7591 struct tab *
7592 create_new_tab(const char *title, struct undo *u, int focus, int position)
7594 struct tab *t;
7595 int load = 1, id;
7596 GtkWidget *b, *bb;
7597 WebKitWebHistoryItem *item;
7598 GList *items;
7599 char *sv[3];
7600 #if !GTK_CHECK_VERSION(3, 0, 0)
7601 GdkColor color;
7602 #endif
7604 DNPRINTF(XT_D_TAB, "create_new_tab: title %s focus %d\n", title, focus);
7606 if (tabless && !TAILQ_EMPTY(&tabs)) {
7607 if (single_instance) {
7608 DNPRINTF(XT_D_TAB,
7609 "create_new_tab: new tab rejected\n");
7610 return (NULL);
7612 sv[0] = start_argv[0];
7613 sv[1] = (char *)title;
7614 sv[2] = (char *)NULL;
7615 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH,
7616 NULL, NULL, NULL, NULL))
7617 show_oops(NULL, "%s: could not spawn process",
7618 __func__);
7619 return (NULL);
7622 t = g_malloc0(sizeof *t);
7624 if (title == NULL) {
7625 title = "(untitled)";
7626 load = 0;
7629 #if GTK_CHECK_VERSION(3, 0, 0)
7630 t->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
7631 b = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7632 gtk_widget_set_name(t->vbox, "vbox");
7633 #else
7634 t->vbox = gtk_vbox_new(FALSE, 0);
7635 b = gtk_hbox_new(FALSE, 0);
7636 #endif
7637 gtk_widget_set_can_focus(t->vbox, FALSE);
7639 /* label + button for tab */
7640 t->tab_content = b;
7641 gtk_widget_set_can_focus(t->tab_content, FALSE);
7643 t->user_agent_id = 0;
7644 t->http_accept_id = 0;
7646 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7647 t->active = NULL;
7648 #endif
7650 #if GTK_CHECK_VERSION(2, 20, 0)
7651 t->spinner = gtk_spinner_new();
7652 #endif
7653 t->label = gtk_label_new(title);
7654 bb = create_button("Close", GTK_STOCK_CLOSE, 1);
7655 gtk_label_set_max_width_chars(GTK_LABEL(t->label), 20);
7656 gtk_label_set_ellipsize(GTK_LABEL(t->label), PANGO_ELLIPSIZE_END);
7657 gtk_label_set_line_wrap(GTK_LABEL(t->label), FALSE);
7658 gtk_widget_set_size_request(t->tab_content, 130, 0);
7661 * this is a total hack and most likely breaks with other styles but
7662 * is necessary so the text doesn't bounce around when the spinner is
7663 * shown/hidden
7665 #if GTK_CHECK_VERSION(3, 0, 0)
7666 gtk_widget_set_size_request(t->label, 95, 0);
7667 #else
7668 gtk_widget_set_size_request(t->label, 100, 0);
7669 #endif
7671 gtk_box_pack_start(GTK_BOX(t->tab_content), bb, FALSE, FALSE, 0);
7672 gtk_box_pack_start(GTK_BOX(t->tab_content), t->label, FALSE, FALSE, 0);
7673 #if GTK_CHECK_VERSION(2, 20, 0)
7674 gtk_box_pack_end(GTK_BOX(b), t->spinner, FALSE, FALSE, 0);
7675 #endif
7677 /* toolbar */
7678 if (browser_mode == XT_BM_KIOSK) {
7679 t->toolbar = create_kiosk_toolbar(t);
7680 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7682 } else {
7683 t->toolbar = create_toolbar(t);
7684 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7688 /* marks */
7689 marks_clear(t);
7691 /* browser */
7692 t->browser_win = create_browser(t);
7693 set_scrollbar_visibility(t, show_scrollbars);
7694 gtk_box_pack_start(GTK_BOX(t->vbox), t->browser_win, TRUE, TRUE, 0);
7696 /* oops message for user feedback */
7697 t->oops = gtk_entry_new();
7698 gtk_entry_set_inner_border(GTK_ENTRY(t->oops), NULL);
7699 gtk_entry_set_has_frame(GTK_ENTRY(t->oops), FALSE);
7700 gtk_widget_set_can_focus(GTK_WIDGET(t->oops), FALSE);
7701 #if GTK_CHECK_VERSION(3, 0, 0)
7702 gtk_widget_set_name(t->oops, XT_CSS_RED);
7703 #else
7704 gdk_color_parse(XT_COLOR_RED, &color);
7705 gtk_widget_modify_base(t->oops, GTK_STATE_NORMAL, &color);
7706 #endif
7707 gtk_box_pack_end(GTK_BOX(t->vbox), t->oops, FALSE, FALSE, 0);
7708 modify_font(GTK_WIDGET(t->oops), oops_font);
7710 /* command entry */
7711 t->cmd = gtk_entry_new();
7712 g_signal_connect(G_OBJECT(t->cmd), "focus-in-event",
7713 G_CALLBACK(entry_focus_cb), t);
7714 gtk_entry_set_inner_border(GTK_ENTRY(t->cmd), NULL);
7715 gtk_entry_set_has_frame(GTK_ENTRY(t->cmd), FALSE);
7716 gtk_box_pack_end(GTK_BOX(t->vbox), t->cmd, FALSE, FALSE, 0);
7717 modify_font(GTK_WIDGET(t->cmd), cmd_font);
7719 /* status bar */
7720 statusbar_create(t);
7722 /* buffer list */
7723 t->buffers = create_buffers(t);
7724 gtk_box_pack_end(GTK_BOX(t->vbox), t->buffers, FALSE, FALSE, 0);
7726 /* xtp meaning is normal by default */
7727 set_normal_tab_meaning(t);
7729 /* set empty favicon */
7730 xt_icon_from_name(t, "text-html");
7732 /* and show it all */
7733 gtk_widget_show_all(b);
7734 gtk_widget_show_all(t->vbox);
7736 if (!fancy_bar) {
7737 gtk_widget_hide(t->backward);
7738 gtk_widget_hide(t->forward);
7739 gtk_widget_hide(t->stop);
7740 gtk_widget_hide(t->js_toggle);
7742 if (!fancy_bar || (search_string == NULL || strlen(search_string) == 0))
7743 gtk_widget_hide(t->search_entry);
7744 if (http_proxy == NULL && http_proxy_save == NULL)
7745 gtk_widget_hide(t->proxy_toggle);
7747 /* compact tab bar */
7748 t->tab_elems.label = gtk_label_new(title);
7749 t->tab_elems.favicon = gtk_image_new();
7751 t->tab_elems.eventbox = gtk_event_box_new();
7752 gtk_widget_set_name(t->tab_elems.eventbox, "compact_tab");
7753 #if GTK_CHECK_VERSION(3, 0, 0)
7754 t->tab_elems.box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7756 gtk_label_set_ellipsize(GTK_LABEL(t->tab_elems.label),
7757 PANGO_ELLIPSIZE_END);
7758 gtk_widget_override_font(t->tab_elems.label, tabbar_font);
7759 gtk_widget_set_halign(t->tab_elems.label, GTK_ALIGN_START);
7760 gtk_widget_set_valign(t->tab_elems.label, GTK_ALIGN_START);
7761 #else
7762 t->tab_elems.box = gtk_hbox_new(FALSE, 0);
7764 gtk_label_set_width_chars(GTK_LABEL(t->tab_elems.label), 1);
7765 gtk_misc_set_alignment(GTK_MISC(t->tab_elems.label), 0.0, 0.0);
7766 gtk_misc_set_padding(GTK_MISC(t->tab_elems.label), 4.0, 4.0);
7767 modify_font(GTK_WIDGET(t->tab_elems.label), tabbar_font);
7769 gdk_color_parse(XT_COLOR_CT_BACKGROUND, &color);
7770 gtk_widget_modify_bg(t->tab_elems.eventbox, GTK_STATE_NORMAL, &color);
7771 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color);
7772 gtk_widget_modify_fg(t->tab_elems.label, GTK_STATE_NORMAL, &color);
7773 #endif
7775 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.favicon, FALSE,
7776 FALSE, 0);
7777 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.label, TRUE,
7778 TRUE, 0);
7779 gtk_container_add(GTK_CONTAINER(t->tab_elems.eventbox),
7780 t->tab_elems.box);
7782 gtk_box_pack_start(GTK_BOX(tab_bar_box), t->tab_elems.eventbox, TRUE,
7783 TRUE, 0);
7784 gtk_widget_show_all(t->tab_elems.eventbox);
7786 if (append_next == 0 || gtk_notebook_get_n_pages(notebook) == 0)
7787 append_tab(t);
7788 else {
7789 id = position >= 0 ? position :
7790 gtk_notebook_get_current_page(notebook) + 1;
7791 if (id > gtk_notebook_get_n_pages(notebook))
7792 append_tab(t);
7793 else {
7794 TAILQ_INSERT_TAIL(&tabs, t, entry);
7795 gtk_notebook_insert_page(notebook, t->vbox, t->tab_content,
7796 id);
7797 gtk_box_reorder_child(GTK_BOX(tab_bar_box),
7798 t->tab_elems.eventbox, id);
7799 recalc_tabs();
7803 #if GTK_CHECK_VERSION(2, 20, 0)
7804 /* turn spinner off if we are a new tab without uri */
7805 if (!load) {
7806 gtk_spinner_stop(GTK_SPINNER(t->spinner));
7807 gtk_widget_hide(t->spinner);
7809 #endif
7810 /* make notebook tabs reorderable */
7811 gtk_notebook_set_tab_reorderable(notebook, t->vbox, TRUE);
7813 /* compact tabs clickable */
7814 g_signal_connect(G_OBJECT(t->tab_elems.eventbox),
7815 "button_press_event", G_CALLBACK(tab_clicked_cb), t);
7817 g_object_connect(G_OBJECT(t->cmd),
7818 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7819 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb), t,
7820 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7821 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb), t,
7822 "signal::activate", G_CALLBACK(cmd_activate_cb), t,
7823 "signal::populate-popup", G_CALLBACK(cmd_popup_cb), t,
7824 "signal::hide", G_CALLBACK(cmd_hide_cb), t,
7825 (char *)NULL);
7827 /* reuse wv_button_cb to hide oops */
7828 g_object_connect(G_OBJECT(t->oops),
7829 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7830 (char *)NULL);
7832 g_signal_connect(t->buffers,
7833 "row-activated", G_CALLBACK(row_activated_cb), t);
7834 g_object_connect(G_OBJECT(t->buffers),
7835 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t, (char *)NULL);
7837 g_object_connect(G_OBJECT(t->wv),
7838 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t,
7839 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb), t,
7840 "signal::download-requested", G_CALLBACK(webview_download_cb), t,
7841 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb), t,
7842 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7843 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7844 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb), t,
7845 "signal::create-web-view", G_CALLBACK(webview_cwv_cb), t,
7846 "signal::close-web-view", G_CALLBACK(webview_closewv_cb), t,
7847 "signal::event", G_CALLBACK(webview_event_cb), t,
7848 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb), t,
7849 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7850 "signal::button_release_event", G_CALLBACK(wv_release_button_cb), t,
7851 "signal::populate-popup", G_CALLBACK(wv_popup_cb), t,
7852 (char *)NULL);
7853 g_signal_connect(t->wv,
7854 "notify::load-status", G_CALLBACK(notify_load_status_cb), t);
7855 g_signal_connect(t->wv,
7856 "notify::title", G_CALLBACK(notify_title_cb), t);
7857 t->progress_handle = g_signal_connect(t->wv,
7858 "notify::progress", G_CALLBACK(webview_progress_changed_cb), t);
7860 /* hijack the unused keys as if we were the browser */
7861 //g_object_connect(G_OBJECT(t->toolbar),
7862 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7863 // (char *)NULL);
7865 g_signal_connect(G_OBJECT(bb), "button_press_event",
7866 G_CALLBACK(tab_close_cb), t);
7868 /* setup history */
7869 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7870 /* restore the tab's history */
7871 if (u && u->history) {
7872 items = u->history;
7873 while (items) {
7874 item = items->data;
7875 webkit_web_back_forward_list_add_item(t->bfl, item);
7876 items = g_list_next(items);
7879 item = g_list_nth_data(u->history, u->back);
7880 if (item)
7881 webkit_web_view_go_to_back_forward_item(t->wv, item);
7883 g_list_free(items);
7884 g_list_free(u->history);
7885 } else
7886 webkit_web_back_forward_list_clear(t->bfl);
7888 /* check and show url and statusbar */
7889 url_set_visibility();
7890 statusbar_set_visibility();
7892 if (focus) {
7893 set_current_tab(t->tab_id);
7894 DNPRINTF(XT_D_TAB, "create_new_tab: going to tab: %d\n",
7895 t->tab_id);
7897 if (load) {
7898 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), title);
7899 load_uri(t, title);
7900 } else {
7901 if (show_url == 1)
7902 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
7903 else
7904 focus_webview(t);
7906 } else if (load)
7907 load_uri(t, title);
7909 if (userstyle_global)
7910 apply_style(t);
7912 recolor_compact_tabs();
7913 setzoom_webkit(t, XT_ZOOM_NORMAL);
7914 return (t);
7917 void
7918 notebook_switchpage_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7919 gpointer *udata)
7921 struct tab *t;
7922 const gchar *uri;
7924 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: tab: %d\n", pn);
7926 if (gtk_notebook_get_current_page(notebook) == -1)
7927 recalc_tabs();
7929 TAILQ_FOREACH(t, &tabs, entry) {
7930 if (t->tab_id == pn) {
7931 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: going to "
7932 "%d\n", pn);
7934 uri = get_title(t, TRUE);
7935 gtk_window_set_title(GTK_WINDOW(main_window), uri);
7937 hide_cmd(t);
7938 hide_oops(t);
7939 hide_buffers(t);
7941 if (t->focus_wv) {
7942 /* can't use focus_webview here */
7943 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7945 update_statusbar_tabs(t);
7946 break;
7951 void
7952 notebook_pagereordered_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7953 gpointer *udata)
7955 struct tab *t = NULL, *tt;
7957 recalc_tabs();
7959 TAILQ_FOREACH(tt, &tabs, entry)
7960 if (tt->tab_id == pn) {
7961 t = tt;
7962 break;
7964 if (t == NULL)
7965 return;
7966 DNPRINTF(XT_D_TAB, "page_reordered_cb: tab: %d\n", t->tab_id);
7968 gtk_box_reorder_child(GTK_BOX(tab_bar_box), t->tab_elems.eventbox,
7969 t->tab_id);
7971 update_statusbar_tabs(t);
7974 void
7975 menuitem_response(struct tab *t)
7977 gtk_notebook_set_current_page(notebook, t->tab_id);
7981 destroy_menu(GtkMenuShell *m, void *notused)
7983 gtk_widget_destroy(GTK_WIDGET(m));
7984 g_object_unref(G_OBJECT(m));
7985 return (XT_CB_PASSTHROUGH);
7988 gboolean
7989 arrow_cb(GtkWidget *w, GdkEventButton *event, gpointer user_data)
7991 GtkWidget *menu = NULL, *menu_items;
7992 GdkEventButton *bevent;
7993 struct tab **stabs = NULL;
7994 int i, num_tabs;
7995 const gchar *uri;
7997 if (event->type == GDK_BUTTON_PRESS) {
7998 bevent = (GdkEventButton *) event;
7999 menu = gtk_menu_new();
8000 g_object_ref_sink(G_OBJECT(menu));
8002 num_tabs = sort_tabs_by_page_num(&stabs);
8003 for (i = 0; i < num_tabs; ++i) {
8004 if (stabs[i] == NULL)
8005 continue;
8006 if ((uri = get_uri(stabs[i])) == NULL)
8007 /* XXX make sure there is something to print */
8008 /* XXX add gui pages in here to look purdy */
8009 uri = "(untitled)";
8010 menu_items = gtk_menu_item_new_with_label(uri);
8011 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_items);
8012 gtk_widget_show(menu_items);
8014 g_signal_connect_swapped(menu_items,
8015 "activate", G_CALLBACK(menuitem_response),
8016 (gpointer)stabs[i]);
8018 g_free(stabs);
8020 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
8021 bevent->button, bevent->time);
8023 g_object_connect(G_OBJECT(menu),
8024 "signal::selection-done", G_CALLBACK(destroy_menu), NULL,
8025 (char *)NULL);
8027 return (TRUE /* eat event */);
8030 return (FALSE /* propagate */);
8034 icon_size_map(int iconsz)
8036 if (iconsz <= GTK_ICON_SIZE_INVALID ||
8037 iconsz > GTK_ICON_SIZE_DIALOG)
8038 return (GTK_ICON_SIZE_SMALL_TOOLBAR);
8040 return (iconsz);
8043 GtkWidget *
8044 create_button(const char *name, const char *stockid, int size)
8046 GtkWidget *button, *image;
8047 int gtk_icon_size;
8048 #if !GTK_CHECK_VERSION(3, 0, 0)
8049 gchar *newstyle;
8050 #endif
8052 #if !GTK_CHECK_VERSION(3, 0, 0)
8053 newstyle = g_strdup_printf(
8054 "style \"%s-style\"\n"
8055 "{\n"
8056 " GtkWidget::focus-padding = 0\n"
8057 " GtkWidget::focus-line-width = 0\n"
8058 " xthickness = 0\n"
8059 " ythickness = 0\n"
8060 "}\n"
8061 "widget \"*.%s\" style \"%s-style\"", name, name, name);
8062 gtk_rc_parse_string(newstyle);
8063 g_free(newstyle);
8064 #endif
8065 button = gtk_button_new();
8066 gtk_widget_set_can_focus(button, FALSE);
8067 gtk_button_set_focus_on_click(GTK_BUTTON(button), FALSE);
8068 gtk_icon_size = icon_size_map(size ? size : icon_size);
8070 image = gtk_image_new_from_stock(stockid, gtk_icon_size);
8071 gtk_container_set_border_width(GTK_CONTAINER(button), 0);
8072 gtk_button_set_image(GTK_BUTTON(button), GTK_WIDGET(image));
8073 gtk_widget_set_name(button, name);
8074 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
8076 return (button);
8079 void
8080 button_set_file(GtkWidget *button, char *filename)
8082 GtkWidget *image;
8083 char file[PATH_MAX];
8085 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, filename);
8086 image = gtk_image_new_from_file(file);
8087 gtk_button_set_image(GTK_BUTTON(button), image);
8090 void
8091 button_set_stockid(GtkWidget *button, char *stockid)
8093 GtkWidget *image;
8095 image = gtk_image_new_from_stock(stockid, icon_size_map(icon_size));
8096 gtk_button_set_image(GTK_BUTTON(button), image);
8099 void
8100 create_canvas(void)
8102 GtkWidget *vbox;
8103 GList *l = NULL;
8104 GdkPixbuf *pb;
8105 char file[PATH_MAX];
8106 int i;
8107 #if !GTK_CHECK_VERSION(3, 0, 0)
8108 GdkColor color;
8109 #endif
8111 #if GTK_CHECK_VERSION(3, 0, 0)
8112 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
8113 #else
8114 vbox = gtk_vbox_new(FALSE, 0);
8115 #endif
8116 gtk_box_set_spacing(GTK_BOX(vbox), 0);
8117 gtk_widget_set_can_focus(vbox, FALSE);
8118 notebook = GTK_NOTEBOOK(gtk_notebook_new());
8119 #if !GTK_CHECK_VERSION(3, 0, 0)
8120 /* XXX seems to be needed with gtk+2 */
8121 g_object_set(G_OBJECT(notebook), "tab-border", 0, NULL);
8122 #endif
8123 gtk_notebook_set_scrollable(notebook, TRUE);
8124 gtk_notebook_set_show_border(notebook, FALSE);
8125 gtk_widget_set_can_focus(GTK_WIDGET(notebook), FALSE);
8127 abtn = gtk_button_new();
8128 gtk_widget_set_can_focus(abtn, FALSE);
8129 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
8130 gtk_widget_set_name(abtn, "Arrow");
8131 gtk_button_set_image(GTK_BUTTON(abtn), arrow);
8132 gtk_widget_set_size_request(abtn, -1, 20);
8134 #if GTK_CHECK_VERSION(2, 20, 0)
8135 gtk_notebook_set_action_widget(notebook, abtn, GTK_PACK_END);
8136 #endif
8137 /* compact tab bar */
8138 tab_bar = gtk_event_box_new();
8139 #if GTK_CHECK_VERSION(3, 0, 0)
8140 gtk_widget_set_name(tab_bar, "tab_bar");
8141 tab_bar_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
8142 #else
8143 gdk_color_parse(XT_COLOR_CT_SEPARATOR, &color);
8144 gtk_widget_modify_bg(tab_bar, GTK_STATE_NORMAL, &color);
8145 tab_bar_box = gtk_hbox_new(TRUE, 0);
8146 #endif
8147 gtk_container_add(GTK_CONTAINER(tab_bar), tab_bar_box);
8148 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box), TRUE);
8149 gtk_box_set_spacing(GTK_BOX(tab_bar_box), 2);
8151 gtk_box_pack_start(GTK_BOX(vbox), tab_bar, FALSE, FALSE, 0);
8152 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(notebook), TRUE, TRUE, 0);
8154 g_object_connect(G_OBJECT(notebook),
8155 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb), NULL,
8156 (char *)NULL);
8157 g_object_connect(G_OBJECT(notebook),
8158 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb),
8159 NULL, (char *)NULL);
8160 g_signal_connect(G_OBJECT(abtn), "button_press_event",
8161 G_CALLBACK(arrow_cb), NULL);
8163 main_window = create_window("xombrero");
8164 gtk_container_add(GTK_CONTAINER(main_window), vbox);
8165 g_signal_connect(G_OBJECT(main_window), "delete_event",
8166 G_CALLBACK(gtk_main_quit), NULL);
8167 #if GTK_CHECK_VERSION(3, 0, 0)
8168 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window), FALSE);
8169 #endif
8171 /* icons */
8172 for (i = 0; i < LENGTH(icons); i++) {
8173 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, icons[i]);
8174 pb = gdk_pixbuf_new_from_file(file, NULL);
8175 l = g_list_append(l, pb);
8177 gtk_window_set_default_icon_list(l);
8179 for (; l; l = l->next)
8180 g_object_unref(G_OBJECT(l->data));
8182 gtk_widget_show_all(abtn);
8183 gtk_widget_show_all(main_window);
8184 notebook_tab_set_visibility();
8187 #ifndef XT_SOCKET_DISABLE
8189 send_cmd_to_socket(char *cmd)
8191 int s, len, rv = 1;
8192 struct sockaddr_un sa;
8194 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8195 warnx("%s: socket", __func__);
8196 return (rv);
8199 sa.sun_family = AF_UNIX;
8200 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8201 work_dir, XT_SOCKET_FILE);
8202 len = SUN_LEN(&sa);
8204 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8205 warnx("%s: connect", __func__);
8206 goto done;
8209 if (send(s, cmd, strlen(cmd) + 1, 0) == -1) {
8210 warnx("%s: send", __func__);
8211 goto done;
8214 rv = 0;
8215 done:
8216 close(s);
8217 return (rv);
8220 gboolean
8221 socket_watcher(GIOChannel *source, GIOCondition condition, gpointer data)
8223 int s, n;
8224 char str[XT_MAX_URL_LENGTH];
8225 socklen_t t = sizeof(struct sockaddr_un);
8226 struct sockaddr_un sa;
8227 struct passwd *p;
8228 uid_t uid;
8229 gid_t gid;
8230 struct tab *tt;
8231 gint fd = g_io_channel_unix_get_fd(source);
8233 if ((s = accept(fd, (struct sockaddr *)&sa, &t)) == -1) {
8234 warn("accept");
8235 return (FALSE);
8238 if (getpeereid(s, &uid, &gid) == -1) {
8239 warn("getpeereid");
8240 return (FALSE);
8242 if (uid != getuid() || gid != getgid()) {
8243 warnx("unauthorized user");
8244 return (FALSE);
8247 p = getpwuid(uid);
8248 if (p == NULL) {
8249 warnx("not a valid user");
8250 return (FALSE);
8253 n = recv(s, str, sizeof(str), 0);
8254 if (n <= 0)
8255 return (TRUE);
8257 tt = get_current_tab();
8258 cmd_execute(tt, str);
8259 return (TRUE);
8263 is_running(void)
8265 int s, len, rv = 1;
8266 struct sockaddr_un sa;
8268 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8269 warn("is_running: socket");
8270 return (-1);
8273 sa.sun_family = AF_UNIX;
8274 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8275 work_dir, XT_SOCKET_FILE);
8276 len = SUN_LEN(&sa);
8278 /* connect to see if there is a listener */
8279 if (connect(s, (struct sockaddr *)&sa, len) == -1)
8280 rv = 0; /* not running */
8281 else
8282 rv = 1; /* already running */
8284 close(s);
8286 return (rv);
8290 build_socket(void)
8292 int s, len;
8293 struct sockaddr_un sa;
8295 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8296 warn("build_socket: socket");
8297 return (-1);
8300 sa.sun_family = AF_UNIX;
8301 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8302 work_dir, XT_SOCKET_FILE);
8303 len = SUN_LEN(&sa);
8305 /* connect to see if there is a listener */
8306 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8307 /* no listener so we will */
8308 unlink(sa.sun_path);
8310 if (bind(s, (struct sockaddr *)&sa, len) == -1) {
8311 warn("build_socket: bind");
8312 goto done;
8315 if (listen(s, 1) == -1) {
8316 warn("build_socket: listen");
8317 goto done;
8320 return (s);
8323 done:
8324 close(s);
8325 return (-1);
8327 #endif
8329 void
8330 xxx_dir(char *dir)
8332 struct stat sb;
8334 if (stat(dir, &sb)) {
8335 #if defined __MINGW32__
8336 if (mkdir(dir) == -1)
8337 #else
8338 if (mkdir(dir, S_IRWXU) == -1)
8339 #endif
8340 err(1, "mkdir %s", dir);
8341 if (stat(dir, &sb))
8342 err(1, "stat %s", dir);
8344 if (S_ISDIR(sb.st_mode) == 0)
8345 errx(1, "%s not a dir", dir);
8346 #if !defined __MINGW32__
8347 if (((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) != S_IRWXU) {
8348 warnx("fixing invalid permissions on %s", dir);
8349 if (chmod(dir, S_IRWXU) == -1)
8350 err(1, "chmod %s", dir);
8352 #endif
8355 void
8356 usage(void)
8358 fprintf(stderr,
8359 "%s [-nSTVt][-f file][-s session] url ...\n", __progname);
8360 exit(0);
8363 #if GTK_CHECK_VERSION(3, 0, 0)
8364 void
8365 setup_css(void)
8367 GtkCssProvider *provider;
8368 GdkDisplay *display;
8369 GdkScreen *screen;
8370 GFile *file;
8371 char path[PATH_MAX];
8372 #if defined __MINGW32__
8373 GtkCssProvider *windows_hacks;
8374 #endif
8376 provider = gtk_css_provider_new();
8377 display = gdk_display_get_default();
8378 screen = gdk_display_get_default_screen(display);
8379 snprintf(path, sizeof path, "%s" PS "%s", resource_dir, XT_CSS_FILE);
8380 file = g_file_new_for_path(path);
8381 gtk_css_provider_load_from_file(provider, file, NULL);
8382 gtk_style_context_add_provider_for_screen(screen,
8383 GTK_STYLE_PROVIDER(provider),
8384 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8385 g_object_unref(G_OBJECT(provider));
8386 #if defined __MINGW32__
8387 windows_hacks = gtk_css_provider_new();
8388 gtk_css_provider_load_from_data(windows_hacks,
8389 ".notebook tab {\n"
8390 " border-width: 0px;\n"
8391 "}", -1, NULL);
8392 gtk_style_context_add_provider_for_screen(screen,
8393 GTK_STYLE_PROVIDER(windows_hacks),
8394 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8395 g_object_unref(G_OBJECT(windows_hacks));
8396 #endif
8397 g_object_unref(G_OBJECT(file));
8399 #endif
8401 void
8402 welcome(void)
8404 startpage_add("<b>Welcome to xombrero %s!</b><p>", version);
8405 startpage_add("Details at "
8406 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8407 "wiki page</a><p>");
8411 main(int argc, char **argv)
8413 struct stat sb;
8414 int c, optn = 0, opte = 0, focus = 1;
8415 char conf[PATH_MAX] = { '\0' };
8416 char file[PATH_MAX];
8417 char sodversion[32];
8418 char *env_proxy = NULL;
8419 char *path;
8420 FILE *f = NULL;
8421 struct karg a;
8423 start_argv = (char * const *)argv;
8425 if (os_init)
8426 os_init();
8428 /* prepare gtk */
8429 gtk_init(&argc, &argv);
8431 gnutls_global_init();
8433 strlcpy(named_session, XT_SAVED_TABS_FILE, sizeof named_session);
8435 RB_INIT(&hl);
8436 RB_INIT(&downloads);
8437 RB_INIT(&st_tree);
8438 RB_INIT(&svl);
8439 RB_INIT(&ua_list);
8440 RB_INIT(&ha_list);
8441 RB_INIT(&di_list);
8443 TAILQ_INIT(&sessions);
8444 TAILQ_INIT(&tabs);
8445 TAILQ_INIT(&mtl);
8446 TAILQ_INIT(&aliases);
8447 TAILQ_INIT(&undos);
8448 TAILQ_INIT(&kbl);
8449 TAILQ_INIT(&spl);
8450 TAILQ_INIT(&chl);
8451 TAILQ_INIT(&shl);
8452 TAILQ_INIT(&cul);
8453 TAILQ_INIT(&srl);
8454 TAILQ_INIT(&c_wl);
8455 TAILQ_INIT(&js_wl);
8456 TAILQ_INIT(&pl_wl);
8457 TAILQ_INIT(&force_https);
8458 TAILQ_INIT(&svil);
8460 #ifndef XT_RESOURCE_LIMITS_DISABLE
8461 struct rlimit rlp;
8462 char *cmd = NULL;
8463 int s;
8464 GIOChannel *channel;
8466 /* fiddle with ulimits */
8467 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8468 warn("getrlimit");
8469 else {
8470 /* just use them all */
8471 #ifdef __APPLE__
8472 rlp.rlim_cur = OPEN_MAX;
8473 #else
8474 rlp.rlim_cur = rlp.rlim_max;
8475 #endif
8476 if (setrlimit(RLIMIT_NOFILE, &rlp) == -1)
8477 warn("setrlimit");
8478 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8479 warn("getrlimit");
8480 else if (rlp.rlim_cur < 1024)
8481 startpage_add("%s requires at least 1024 "
8482 "(2048 recommended) file " "descriptors, "
8483 "currently it has up to %d available",
8484 __progname, rlp.rlim_cur);
8486 #endif
8488 while ((c = getopt(argc, argv, "STVf:s:tne")) != -1) {
8489 switch (c) {
8490 case 'S':
8491 show_url = 0;
8492 break;
8493 case 'T':
8494 show_tabs = 0;
8495 break;
8496 case 'V':
8497 #ifdef XOMBRERO_BUILDSTR
8498 errx(0 , "Version: %s Build: %s",
8499 version, XOMBRERO_BUILDSTR);
8500 #else
8501 errx(0 , "Version: %s", version);
8502 #endif
8503 break;
8504 case 'f':
8505 strlcpy(conf, optarg, sizeof(conf));
8506 break;
8507 case 's':
8508 strlcpy(named_session, optarg, sizeof(named_session));
8509 break;
8510 case 't':
8511 tabless = 1;
8512 break;
8513 case 'n':
8514 optn = 1;
8515 break;
8516 case 'e':
8517 opte = 1;
8518 break;
8519 default:
8520 usage();
8521 /* NOTREACHED */
8524 argc -= optind;
8525 argv += optind;
8527 init_keybindings();
8529 pwd = getpwuid(getuid());
8530 if (pwd == NULL)
8531 errx(1, "invalid user %d", getuid());
8533 /* set download dir */
8534 if (strlen(download_dir) == 0)
8535 strlcpy(download_dir, pwd->pw_dir, sizeof download_dir);
8537 /* compile buffer command regexes */
8538 buffercmd_init();
8540 /* set default dynamic string settings */
8541 home = g_strdup(XT_DS_HOME);
8542 search_string = g_strdup(XT_DS_SEARCH_STRING);
8543 strlcpy(runtime_settings, "runtime", sizeof runtime_settings);
8544 cmd_font_name = g_strdup(XT_DS_CMD_FONT_NAME);
8545 oops_font_name = g_strdup(XT_DS_OOPS_FONT_NAME);
8546 statusbar_font_name = g_strdup(XT_DS_STATUSBAR_FONT_NAME);
8547 tabbar_font_name = g_strdup(XT_DS_TABBAR_FONT_NAME);
8548 statusbar_elems = g_strdup("BP");
8549 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8550 encoding = g_strdup(XT_DS_ENCODING);
8551 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8552 path = g_strdup_printf("%s" PS "style.css", resource_dir);
8553 userstyle = g_filename_to_uri(path, NULL, NULL);
8554 g_free(path);
8555 stylesheet = g_strdup(userstyle);
8557 /* set statically allocated (struct special) settings */
8558 if (strlen(default_script) == 0)
8559 expand_tilde(default_script, sizeof default_script,
8560 XT_DS_DEFAULT_SCRIPT);
8561 if (strlen(ssl_ca_file) == 0)
8562 expand_tilde(ssl_ca_file, sizeof ssl_ca_file,
8563 XT_DS_SSL_CA_FILE);
8565 /* soup session */
8566 session = webkit_get_default_session();
8568 /* read config file */
8569 if (strlen(conf) == 0)
8570 snprintf(conf, sizeof conf, "%s" PS ".%s",
8571 pwd->pw_dir, XT_CONF_FILE);
8572 config_parse(conf, 0);
8574 /* read preloaded HSTS list */
8575 if (preload_strict_transport) {
8576 snprintf(conf, sizeof conf, "%s" PS "%s",
8577 resource_dir, XT_HSTS_PRELOAD_FILE);
8578 config_parse(conf, 0);
8581 /* check whether to read in a crapton of additional http headers */
8582 if (anonymize_headers) {
8583 snprintf(conf, sizeof conf, "%s" PS "%s",
8584 resource_dir, XT_USER_AGENT_FILE);
8585 config_parse(conf, 0);
8586 snprintf(conf, sizeof conf, "%s" PS "%s",
8587 resource_dir, XT_HTTP_ACCEPT_FILE);
8588 config_parse(conf, 0);
8591 /* init fonts */
8592 cmd_font = pango_font_description_from_string(cmd_font_name);
8593 oops_font = pango_font_description_from_string(oops_font_name);
8594 statusbar_font = pango_font_description_from_string(statusbar_font_name);
8595 tabbar_font = pango_font_description_from_string(tabbar_font_name);
8597 /* working directory */
8598 if (strlen(work_dir) == 0)
8599 snprintf(work_dir, sizeof work_dir, "%s" PS "%s",
8600 pwd->pw_dir, XT_DIR);
8601 xxx_dir(work_dir);
8603 /* icon cache dir */
8604 snprintf(cache_dir, sizeof cache_dir, "%s" PS "%s", work_dir, XT_CACHE_DIR);
8605 xxx_dir(cache_dir);
8607 /* certs dir */
8608 snprintf(certs_dir, sizeof certs_dir, "%s" PS "%s", work_dir, XT_CERT_DIR);
8609 xxx_dir(certs_dir);
8611 /* cert changes dir */
8612 snprintf(certs_cache_dir, sizeof certs_cache_dir, "%s" PS "%s",
8613 work_dir, XT_CERT_CACHE_DIR);
8614 xxx_dir(certs_cache_dir);
8616 /* sessions dir */
8617 snprintf(sessions_dir, sizeof sessions_dir, "%s" PS "%s",
8618 work_dir, XT_SESSIONS_DIR);
8619 xxx_dir(sessions_dir);
8621 /* js dir */
8622 snprintf(js_dir, sizeof js_dir, "%s" PS "%s", work_dir, XT_JS_DIR);
8623 xxx_dir(js_dir);
8625 /* temp dir */
8626 snprintf(temp_dir, sizeof temp_dir, "%s" PS "%s", work_dir, XT_TEMP_DIR);
8627 xxx_dir(temp_dir);
8629 /* runtime settings that can override config file */
8630 if (runtime_settings[0] != '\0')
8631 config_parse(runtime_settings, 1);
8633 /* download dir */
8634 if (!strcmp(download_dir, pwd->pw_dir))
8635 strlcat(download_dir, PS "downloads", sizeof download_dir);
8636 xxx_dir(download_dir);
8638 /* first start file */
8639 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_SOD_FILE);
8640 if (stat(file, &sb)) {
8641 warnx("start of day file doesn't exist, creating it");
8642 if ((f = fopen(file, "w")) == NULL)
8643 err(1, "startofday");
8644 if (fputs(version, f) == EOF)
8645 err(1, "fputs");
8646 fclose(f);
8648 /* welcome user */
8649 welcome();
8650 } else {
8651 if ((f = fopen(file, "r+")) == NULL)
8652 err(1, "startofday");
8653 if (fgets(sodversion, sizeof sodversion, f) == NULL)
8654 err(1, "fgets");
8655 sodversion[strcspn(sodversion, "\n")] = '\0';
8656 if (strcmp(version, sodversion)) {
8657 if ((f = freopen(file, "w", f)) == NULL)
8658 err(1, "startofday");
8659 if (fputs(version, f) == EOF)
8660 err(1, "fputs");
8662 /* upgrade, say something smart */
8663 welcome();
8665 fclose(f);
8668 /* favorites file */
8669 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_FAVS_FILE);
8670 if (stat(file, &sb)) {
8671 warnx("favorites file doesn't exist, creating it");
8672 if ((f = fopen(file, "w")) == NULL)
8673 err(1, "favorites");
8674 fclose(f);
8677 /* quickmarks file */
8678 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
8679 if (stat(file, &sb)) {
8680 warnx("quickmarks file doesn't exist, creating it");
8681 if ((f = fopen(file, "w")) == NULL)
8682 err(1, "quickmarks");
8683 fclose(f);
8686 /* search history */
8687 if (history_autosave) {
8688 snprintf(search_file, sizeof search_file, "%s" PS "%s",
8689 work_dir, XT_SEARCH_FILE);
8690 if (stat(search_file, &sb)) {
8691 warnx("search history file doesn't exist, creating it");
8692 if ((f = fopen(search_file, "w")) == NULL)
8693 err(1, "search_history");
8694 fclose(f);
8696 history_read(&shl, search_file, &search_history_count);
8699 /* command history */
8700 if (history_autosave) {
8701 snprintf(command_file, sizeof command_file, "%s" PS "%s",
8702 work_dir, XT_COMMAND_FILE);
8703 if (stat(command_file, &sb)) {
8704 warnx("command history file doesn't exist, creating it");
8705 if ((f = fopen(command_file, "w")) == NULL)
8706 err(1, "command_history");
8707 fclose(f);
8709 history_read(&chl, command_file, &cmd_history_count);
8712 /* cookies */
8713 setup_cookies();
8715 /* guess_search regex */
8716 if (url_regex == NULL)
8717 url_regex = g_strdup(XT_URL_REGEX);
8718 if (url_regex)
8719 if (regcomp(&url_re, url_regex, REG_EXTENDED | REG_NOSUB))
8720 startpage_add("invalid url regex %s", url_regex);
8722 /* proxy */
8723 env_proxy = getenv("http_proxy");
8724 if (env_proxy)
8725 setup_proxy(env_proxy);
8726 else {
8727 env_proxy = getenv("HTTP_PROXY");
8728 if (env_proxy)
8729 setup_proxy(env_proxy);
8730 else
8731 setup_proxy(http_proxy);
8734 /* the user can optionally have the proxy disabled at startup */
8735 if ((http_proxy_starts_enabled == 0) && (http_proxy != NULL)) {
8736 http_proxy_save = g_strdup(http_proxy);
8737 setup_proxy(NULL);
8740 #ifndef XT_SOCKET_DISABLE
8741 if (opte) {
8742 send_cmd_to_socket(argv[0]);
8743 exit(0);
8745 #else
8746 opte = opte; /* shut mingw up */
8747 #endif
8748 /* set some connection parameters */
8749 g_object_set(session, "max-conns", max_connections, (char *)NULL);
8750 g_object_set(session, "max-conns-per-host", max_host_connections,
8751 (char *)NULL);
8752 g_object_set(session, SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
8753 (char *)NULL);
8755 g_signal_connect(session, "request-queued", G_CALLBACK(session_rq_cb),
8756 NULL);
8758 #ifndef XT_SOCKET_DISABLE
8759 /* see if there is already a xombrero running */
8760 if (single_instance && is_running()) {
8761 optn = 1;
8762 warnx("already running");
8765 if (optn) {
8766 while (argc) {
8767 cmd = g_strdup_printf("%s %s", "tabnew", argv[0]);
8768 send_cmd_to_socket(cmd);
8769 if (cmd)
8770 g_free(cmd);
8772 argc--;
8773 argv++;
8775 exit(0);
8777 #else
8778 optn = optn; /* shut mingw up */
8779 #endif
8780 /* tld list */
8781 tld_tree_init();
8783 /* cache mode */
8784 if (enable_cache)
8785 webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER);
8786 else
8787 webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
8789 if (enable_strict_transport)
8790 strict_transport_init();
8792 /* uri completion */
8793 completion_model = gtk_list_store_new(1, G_TYPE_STRING);
8795 /* buffers */
8796 buffers_store = gtk_list_store_new
8797 (NUM_COLS, G_TYPE_UINT, GDK_TYPE_PIXBUF, G_TYPE_STRING);
8799 qmarks_load();
8801 /* go graphical */
8802 create_canvas();
8803 notebook_tab_set_visibility();
8805 if (save_global_history)
8806 restore_global_history();
8808 /* restore session list */
8809 restore_sessions_list();
8811 if (!strcmp(named_session, XT_SAVED_TABS_FILE))
8812 restore_saved_tabs();
8813 else {
8814 a.s = named_session;
8815 a.i = XT_SES_DONOTHING;
8816 open_tabs(NULL, &a);
8819 /* see if we have an exception */
8820 if (!TAILQ_EMPTY(&spl)) {
8821 create_new_tab("about:startpage", NULL, focus, -1);
8822 focus = 0;
8825 while (argc) {
8826 create_new_tab(argv[0], NULL, focus, -1);
8827 focus = 0;
8829 argc--;
8830 argv++;
8833 if (TAILQ_EMPTY(&tabs))
8834 create_new_tab(home, NULL, 1, -1);
8835 #ifndef XT_SOCKET_DISABLE
8836 if (enable_socket)
8837 if ((s = build_socket()) != -1) {
8838 channel = g_io_channel_unix_new(s);
8839 g_io_add_watch(channel, G_IO_IN, socket_watcher, NULL);
8841 #endif
8843 #if GTK_CHECK_VERSION(3, 0, 0)
8844 setup_css();
8845 #endif
8847 gtk_main();
8849 gnutls_global_deinit();
8851 if (url_regex)
8852 regfree(&url_re);
8854 return (0);