add urlmod plus and min
[xombrero.git] / xombrero.c
blob15670eae0b29778cdcc793d33f3a7b95d94ea6c2
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 */
101 /* colors */
102 #define XT_COLOR_RED "#cc0000"
103 #define XT_COLOR_YELLOW "#ffff66"
104 #define XT_COLOR_BLUE "lightblue"
105 #define XT_COLOR_GREEN "#99ff66"
106 #define XT_COLOR_WHITE "white"
107 #define XT_COLOR_BLACK "black"
109 #define XT_COLOR_CT_BACKGROUND "#000000"
110 #define XT_COLOR_CT_INACTIVE "#dddddd"
111 #define XT_COLOR_CT_ACTIVE "#bbbb00"
112 #define XT_COLOR_CT_SEPARATOR "#555555"
114 #define XT_COLOR_SB_SEPARATOR "#555555"
116 /* CSS element names */
117 #define XT_CSS_NORMAL ""
118 #define XT_CSS_RED "red"
119 #define XT_CSS_YELLOW "yellow"
120 #define XT_CSS_GREEN "green"
121 #define XT_CSS_BLUE "blue"
122 #define XT_CSS_HIDDEN "hidden"
123 #define XT_CSS_ACTIVE "active"
125 #define XT_PROTO_DELIM "://"
127 /* actions */
128 #define XT_MOVE_INVALID (0)
129 #define XT_MOVE_DOWN (1)
130 #define XT_MOVE_UP (2)
131 #define XT_MOVE_BOTTOM (3)
132 #define XT_MOVE_TOP (4)
133 #define XT_MOVE_PAGEDOWN (5)
134 #define XT_MOVE_PAGEUP (6)
135 #define XT_MOVE_HALFDOWN (7)
136 #define XT_MOVE_HALFUP (8)
137 #define XT_MOVE_LEFT (9)
138 #define XT_MOVE_FARLEFT (10)
139 #define XT_MOVE_RIGHT (11)
140 #define XT_MOVE_FARRIGHT (12)
141 #define XT_MOVE_PERCENT (13)
142 #define XT_MOVE_CENTER (14)
144 #define XT_QMARK_SET (0)
145 #define XT_QMARK_OPEN (1)
146 #define XT_QMARK_TAB (2)
148 #define XT_MARK_SET (0)
149 #define XT_MARK_GOTO (1)
151 #define XT_GO_UP_ROOT (999)
153 #define XT_NAV_INVALID (0)
154 #define XT_NAV_BACK (1)
155 #define XT_NAV_FORWARD (2)
156 #define XT_NAV_RELOAD (3)
157 #define XT_NAV_STOP (4)
159 #define XT_FOCUS_INVALID (0)
160 #define XT_FOCUS_URI (1)
161 #define XT_FOCUS_SEARCH (2)
163 #define XT_SEARCH_INVALID (0)
164 #define XT_SEARCH_NEXT (1)
165 #define XT_SEARCH_PREV (2)
167 #define XT_PASTE_CURRENT_TAB (0)
168 #define XT_PASTE_NEW_TAB (1)
170 #define XT_ZOOM_IN (-1)
171 #define XT_ZOOM_OUT (-2)
172 #define XT_ZOOM_NORMAL (100)
174 #define XT_SES_DONOTHING (0)
175 #define XT_SES_CLOSETABS (1)
177 #define XT_PREFIX (1<<0)
178 #define XT_USERARG (1<<1)
179 #define XT_URLARG (1<<2)
180 #define XT_INTARG (1<<3)
181 #define XT_SESSARG (1<<4)
182 #define XT_SETARG (1<<5)
184 #define XT_HINT_NEWTAB (1<<0)
186 #define XT_BUFCMD_SZ (8)
188 #define XT_EJS_SHOW (1<<0)
190 GtkWidget * create_button(char *, char *, int);
192 void recalc_tabs(void);
193 void recolor_compact_tabs(void);
194 void set_current_tab(int page_num);
195 gboolean update_statusbar_position(GtkAdjustment*, gpointer);
196 void marks_clear(struct tab *t);
198 /* globals */
199 extern char *__progname;
200 char * const *start_argv;
201 struct passwd *pwd;
202 GtkWidget *main_window;
203 GtkNotebook *notebook;
204 GtkWidget *tab_bar;
205 GtkWidget *tab_bar_box;
206 GtkWidget *arrow, *abtn;
207 GdkEvent *fevent = NULL;
208 struct tab_list tabs;
209 struct history_list hl;
210 int hl_purge_count = 0;
211 struct session_list sessions;
212 struct wl_list c_wl;
213 struct wl_list js_wl;
214 struct wl_list pl_wl;
215 struct wl_list force_https;
216 struct wl_list svil;
217 struct strict_transport_tree st_tree;
218 struct undo_tailq undos;
219 struct keybinding_list kbl;
220 struct sp_list spl;
221 struct user_agent_list ua_list;
222 struct http_accept_list ha_list;
223 struct domain_id_list di_list;
224 struct cmd_alias_list cal;
225 struct custom_uri_list cul;
226 struct command_list chl;
227 struct command_list shl;
228 struct command_entry *history_at;
229 struct command_entry *search_at;
230 struct secviolation_list svl;
231 struct set_reject_list srl;
232 int undo_count;
233 int cmd_history_count = 0;
234 int search_history_count = 0;
235 char *global_search;
236 uint64_t blocked_cookies = 0;
237 char named_session[PATH_MAX];
238 GtkListStore *completion_model;
239 GtkListStore *buffers_store;
240 char *stylesheet;
242 char *qmarks[XT_NOQMARKS];
243 int btn_down; /* M1 down in any wv */
244 regex_t url_re; /* guess_search regex */
246 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
247 int next_download_id = 1;
249 void xxx_dir(char *);
250 int icon_size_map(int);
251 void activate_uri_entry_cb(GtkWidget*, struct tab *);
253 void
254 history_delete(struct command_list *l, int *counter)
256 struct command_entry *c;
258 if (l == NULL || counter == NULL)
259 return;
261 c = TAILQ_LAST(l, command_list);
262 if (c == NULL)
263 return;
265 TAILQ_REMOVE(l, c, entry);
266 *counter -= 1;
267 g_free(c->line);
268 g_free(c);
271 void
272 history_add(struct command_list *list, char *file, char *l, int *counter)
274 struct command_entry *c;
275 FILE *f;
277 if (list == NULL || l == NULL || counter == NULL)
278 return;
280 /* don't add the same line */
281 c = TAILQ_FIRST(list);
282 if (c)
283 if (!strcmp(c->line + 1 /* skip space */, l))
284 return;
286 c = g_malloc0(sizeof *c);
287 c->line = g_strdup_printf(" %s", l);
289 *counter += 1;
290 TAILQ_INSERT_HEAD(list, c, entry);
292 if (*counter > 1000)
293 history_delete(list, counter);
295 if (history_autosave && file) {
296 f = fopen(file, "w");
297 if (f == NULL) {
298 show_oops(NULL, "couldn't write history %s", file);
299 return;
302 TAILQ_FOREACH_REVERSE(c, list, command_list, entry) {
303 c->line[0] = ' ';
304 fprintf(f, "%s\n", c->line);
307 fclose(f);
312 history_read(struct command_list *list, char *file, int *counter)
314 FILE *f;
315 char *s, line[65536];
317 if (list == NULL || file == NULL)
318 return (1);
320 f = fopen(file, "r");
321 if (f == NULL) {
322 startpage_add("couldn't open history file %s", file);
323 return (1);
326 for (;;) {
327 s = fgets(line, sizeof line, f);
328 if (s == NULL || feof(f) || ferror(f))
329 break;
330 if ((s = strchr(line, '\n')) == NULL) {
331 startpage_add("invalid history file %s", file);
332 fclose(f);
333 return (1);
335 *s = '\0';
337 history_add(list, NULL, line + 1, counter);
340 fclose(f);
342 return (0);
345 /* marks array storage. */
346 char
347 indextomark(int i)
349 if (i < 0 || i >= XT_NOMARKS)
350 return (0);
352 return XT_MARKS[i];
356 marktoindex(char m)
358 char *ret;
360 if ((ret = strchr(XT_MARKS, m)) != NULL)
361 return ret - XT_MARKS;
363 return (-1);
366 /* quickmarks array storage. */
367 char
368 indextoqmark(int i)
370 if (i < 0 || i >= XT_NOQMARKS)
371 return (0);
373 return XT_QMARKS[i];
377 qmarktoindex(char m)
379 char *ret;
381 if ((ret = strchr(XT_QMARKS, m)) != NULL)
382 return ret - XT_QMARKS;
384 return (-1);
388 is_g_object_setting(GObject *o, char *str)
390 guint n_props = 0, i;
391 GParamSpec **proplist;
392 int rv = 0;
394 if (!G_IS_OBJECT(o))
395 return (0);
397 proplist = g_object_class_list_properties(G_OBJECT_GET_CLASS(o),
398 &n_props);
400 for (i = 0; i < n_props; i++) {
401 if (! strcmp(proplist[i]->name, str)) {
402 rv = 1;
403 break;
407 g_free(proplist);
408 return (rv);
411 struct tab *
412 get_current_tab(void)
414 struct tab *t;
416 TAILQ_FOREACH(t, &tabs, entry) {
417 if (t->tab_id == gtk_notebook_get_current_page(notebook))
418 return (t);
421 warnx("%s: no current tab", __func__);
423 return (NULL);
427 set_ssl_ca_file(struct settings *s, char *file)
429 struct stat sb;
431 if (file == NULL || strlen(file) == 0)
432 return (-1);
433 if (stat(file, &sb)) {
434 warnx("no CA file: %s", file);
435 return (-1);
437 expand_tilde(ssl_ca_file, sizeof ssl_ca_file, file);
438 g_object_set(session,
439 SOUP_SESSION_SSL_CA_FILE, ssl_ca_file,
440 SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
441 (void *)NULL);
442 return (0);
445 void
446 set_status(struct tab *t, gchar *fmt, ...)
448 va_list ap;
449 gchar *status;
451 va_start(ap, fmt);
453 status = g_strdup_vprintf(fmt, ap);
455 gtk_entry_set_text(GTK_ENTRY(t->sbe.uri), status);
457 if (!t->status)
458 t->status = status;
459 else if (strcmp(t->status, status)) {
460 /* set new status */
461 g_free(t->status);
462 t->status = status;
463 } else
464 g_free(status);
466 va_end(ap);
469 void
470 hide_cmd(struct tab *t)
472 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
474 history_at = NULL; /* just in case */
475 search_at = NULL; /* just in case */
476 gtk_widget_set_can_focus(t->cmd, FALSE);
477 gtk_widget_hide(t->cmd);
480 void
481 show_cmd(struct tab *t, const char *s)
483 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
485 /* without this you can't middle click in t->cmd to paste */
486 gtk_entry_set_text(GTK_ENTRY(t->cmd), "");
488 history_at = NULL;
489 search_at = NULL;
490 gtk_widget_hide(t->oops);
491 gtk_widget_set_can_focus(t->cmd, TRUE);
492 gtk_widget_show(t->cmd);
494 gtk_widget_grab_focus(GTK_WIDGET(t->cmd));
495 #if GTK_CHECK_VERSION(3, 0, 0)
496 gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
497 #else
498 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
499 &t->default_style->base[GTK_STATE_NORMAL]);
500 #endif
501 gtk_entry_set_text(GTK_ENTRY(t->cmd), s);
502 gtk_editable_set_position(GTK_EDITABLE(t->cmd), -1);
505 void
506 hide_buffers(struct tab *t)
508 gtk_widget_hide(t->buffers);
509 gtk_widget_set_can_focus(t->buffers, FALSE);
510 gtk_list_store_clear(buffers_store);
513 enum {
514 COL_ID = 0,
515 COL_FAVICON,
516 COL_TITLE,
517 NUM_COLS
521 sort_tabs_by_page_num(struct tab ***stabs)
523 int num_tabs = 0;
524 struct tab *t;
526 num_tabs = gtk_notebook_get_n_pages(notebook);
528 *stabs = g_malloc0(num_tabs * sizeof(struct tab *));
530 TAILQ_FOREACH(t, &tabs, entry)
531 (*stabs)[gtk_notebook_page_num(notebook, t->vbox)] = t;
533 return (num_tabs);
536 void
537 buffers_make_list(void)
539 int i, num_tabs;
540 const gchar *title = NULL;
541 GtkTreeIter iter;
542 struct tab **stabs = NULL;
544 num_tabs = sort_tabs_by_page_num(&stabs);
546 for (i = 0; i < num_tabs; i++)
547 if (stabs[i]) {
548 gtk_list_store_append(buffers_store, &iter);
549 title = get_title(stabs[i], FALSE);
550 gtk_list_store_set(buffers_store, &iter,
551 COL_ID, i + 1, /* Enumerate the tabs starting from 1
552 * rather than 0. */
553 COL_FAVICON, gtk_image_get_pixbuf
554 (GTK_IMAGE(stabs[i]->tab_elems.favicon)),
555 COL_TITLE, title,
556 -1);
559 g_free(stabs);
562 void
563 show_buffers(struct tab *t)
565 GtkTreeIter iter;
566 GtkTreeSelection *sel;
567 GtkTreePath *path;
568 int index;
570 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)))
571 return;
573 buffers_make_list();
575 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(t->buffers));
576 index = gtk_notebook_get_current_page(notebook);
577 path = gtk_tree_path_new_from_indices(index, -1);
578 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter, path))
579 gtk_tree_selection_select_iter(sel, &iter);
580 gtk_tree_path_free(path);
582 gtk_widget_show(t->buffers);
583 gtk_widget_set_can_focus(t->buffers, TRUE);
584 gtk_widget_grab_focus(GTK_WIDGET(t->buffers));
587 void
588 toggle_buffers(struct tab *t)
590 if (gtk_widget_get_visible(t->buffers))
591 hide_buffers(t);
592 else
593 show_buffers(t);
597 buffers(struct tab *t, struct karg *args)
599 show_buffers(t);
601 return (0);
605 set_scrollbar_visibility(struct tab *t, int visible)
607 #if GTK_CHECK_VERSION(3, 0, 0)
608 GtkWidget *h_scrollbar, *v_scrollbar;
610 h_scrollbar = gtk_scrolled_window_get_hscrollbar(
611 GTK_SCROLLED_WINDOW(t->browser_win));
612 v_scrollbar = gtk_scrolled_window_get_vscrollbar(
613 GTK_SCROLLED_WINDOW(t->browser_win));
615 if (visible == 0) {
616 gtk_widget_set_name(h_scrollbar, XT_CSS_HIDDEN);
617 gtk_widget_set_name(v_scrollbar, XT_CSS_HIDDEN);
618 } else {
619 gtk_widget_set_name(h_scrollbar, "");
620 gtk_widget_set_name(v_scrollbar, "");
623 return (0);
624 #else
625 return (visible == 0);
626 #endif
629 void
630 hide_oops(struct tab *t)
632 gtk_widget_hide(t->oops);
635 void
636 show_oops(struct tab *at, const char *fmt, ...)
638 va_list ap;
639 char *msg = NULL;
640 struct tab *t = NULL;
642 if (fmt == NULL)
643 return;
645 if (at == NULL) {
646 if ((t = get_current_tab()) == NULL)
647 return;
648 } else
649 t = at;
651 va_start(ap, fmt);
652 if ((msg = g_strdup_vprintf(fmt, ap)) == NULL)
653 errx(1, "show_oops failed");
654 va_end(ap);
656 gtk_entry_set_text(GTK_ENTRY(t->oops), msg);
657 gtk_widget_hide(t->cmd);
658 gtk_widget_show(t->oops);
660 if (msg)
661 g_free(msg);
664 char work_dir[PATH_MAX];
665 char certs_dir[PATH_MAX];
666 char certs_cache_dir[PATH_MAX];
667 char js_dir[PATH_MAX];
668 char cache_dir[PATH_MAX];
669 char sessions_dir[PATH_MAX];
670 char temp_dir[PATH_MAX];
671 char cookie_file[PATH_MAX];
672 char *strict_transport_file = NULL;
673 SoupSession *session;
674 SoupCookieJar *s_cookiejar;
675 SoupCookieJar *p_cookiejar;
676 char rc_fname[PATH_MAX];
678 struct mime_type_list mtl;
679 struct alias_list aliases;
681 /* protos */
682 struct tab *create_new_tab(char *, struct undo *, int, int);
683 void delete_tab(struct tab *);
684 void setzoom_webkit(struct tab *, int);
685 int download_rb_cmp(struct download *, struct download *);
686 gboolean cmd_execute(struct tab *t, char *str);
689 history_rb_cmp(struct history *h1, struct history *h2)
691 return (strcmp(h1->uri, h2->uri));
693 RB_GENERATE(history_list, history, entry, history_rb_cmp);
696 download_rb_cmp(struct download *e1, struct download *e2)
698 return (e1->id < e2->id ? -1 : e1->id > e2->id);
700 RB_GENERATE(download_list, download, entry, download_rb_cmp);
703 secviolation_rb_cmp(struct secviolation *s1, struct secviolation *s2)
705 return (s1->xtp_arg < s2->xtp_arg ? -1 : s1->xtp_arg > s2->xtp_arg);
707 RB_GENERATE(secviolation_list, secviolation, entry, secviolation_rb_cmp);
710 user_agent_rb_cmp(struct user_agent *ua1, struct user_agent *ua2)
712 return (ua1->id < ua2->id ? -1 : ua1->id > ua2->id);
714 RB_GENERATE(user_agent_list, user_agent, entry, user_agent_rb_cmp);
717 http_accept_rb_cmp(struct http_accept *ha1, struct http_accept *ha2)
719 return (ha1->id < ha2->id ? -1 : ha1->id > ha2->id);
721 RB_GENERATE(http_accept_list, http_accept, entry, http_accept_rb_cmp);
724 domain_id_rb_cmp(struct domain_id *d1, struct domain_id *d2)
726 return (strcmp(d1->domain, d2->domain));
728 RB_GENERATE(domain_id_list, domain_id, entry, domain_id_rb_cmp);
730 struct valid_url_types {
731 char *type;
732 } vut[] = {
733 { "http://" },
734 { "https://" },
735 { "ftp://" },
736 { "file://" },
737 { XT_URI_ABOUT },
738 { XT_XTP_STR },
742 valid_url_type(char *url)
744 int i;
746 for (i = 0; i < LENGTH(vut); i++)
747 if (!strncasecmp(vut[i].type, url, strlen(vut[i].type)))
748 return (0);
750 return (1);
753 char *
754 match_alias(char *url_in)
756 struct alias *a;
757 char *arg;
758 char *url_out = NULL, *search, *enc_arg;
759 char **sv;
761 search = g_strdup(url_in);
762 arg = search;
763 if (strsep(&arg, " \t") == NULL) {
764 show_oops(NULL, "match_alias: NULL URL");
765 goto done;
768 TAILQ_FOREACH(a, &aliases, entry) {
769 if (!strcmp(search, a->a_name))
770 break;
773 if (a != NULL) {
774 DNPRINTF(XT_D_URL, "match_alias: matched alias %s\n",
775 a->a_name);
776 if (arg == NULL)
777 arg = "";
778 enc_arg = soup_uri_encode(arg, XT_RESERVED_CHARS);
779 sv = g_strsplit(a->a_uri, "%s", 2);
780 if (arg != NULL)
781 url_out = g_strjoinv(enc_arg, sv);
782 else
783 url_out = g_strjoinv("", sv);
784 g_free(enc_arg);
785 g_strfreev(sv);
787 done:
788 g_free(search);
789 return (url_out);
792 char *
793 guess_url_type(char *url_in)
795 struct stat sb;
796 char cwd[PATH_MAX] = {0};
797 char *url_out = NULL, *enc_search = NULL;
798 char *path = NULL;
799 char **sv = NULL;
800 int i;
803 /* substitute aliases */
804 url_out = match_alias(url_in);
805 if (url_out != NULL)
806 return (url_out);
808 /* see if we are an about page */
809 if (!strncmp(url_in, XT_URI_ABOUT, XT_URI_ABOUT_LEN))
810 for (i = 0; i < about_list_size(); i++)
811 if (!strcmp(&url_in[XT_URI_ABOUT_LEN],
812 about_list[i].name)) {
813 url_out = g_strdup(url_in);
814 goto done;
817 if (guess_search && url_regex &&
818 !(g_str_has_prefix(url_in, "http://") ||
819 g_str_has_prefix(url_in, "https://"))) {
820 if (regexec(&url_re, url_in, 0, NULL, 0)) {
821 /* invalid URI so search instead */
822 enc_search = soup_uri_encode(url_in, XT_RESERVED_CHARS);
823 sv = g_strsplit(search_string, "%s", 2);
824 url_out = g_strjoinv(enc_search, sv);
825 g_free(enc_search);
826 g_strfreev(sv);
827 goto done;
831 /* XXX not sure about this heuristic */
832 if (stat(url_in, &sb) == 0) {
833 if (url_in[0] == '/')
834 url_out = g_filename_to_uri(url_in, NULL, NULL);
835 else {
836 if (getcwd(cwd, PATH_MAX) != NULL) {
837 path = g_strdup_printf("%s" PS "%s", cwd,
838 url_in);
839 url_out = g_filename_to_uri(path, NULL, NULL);
840 g_free(path);
843 } else
844 url_out = g_strdup_printf("http://%s", url_in); /* guess http */
845 done:
846 DNPRINTF(XT_D_URL, "guess_url_type: guessed %s\n", url_out);
848 return (url_out);
851 void
852 set_normal_tab_meaning(struct tab *t)
854 if (t == NULL)
855 return;
857 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
858 if (t->session_key != NULL) {
859 g_free(t->session_key);
860 t->session_key = NULL;
864 void
865 load_uri(struct tab *t, gchar *uri)
867 struct karg args;
868 gchar *newuri = NULL;
869 int i;
871 if (uri == NULL)
872 return;
874 /* Strip leading spaces. */
875 while (*uri && isspace(*uri))
876 uri++;
878 if (strlen(uri) == 0) {
879 blank(t, NULL);
880 return;
883 set_normal_tab_meaning(t);
885 if (valid_url_type(uri)) {
886 newuri = guess_url_type(uri);
887 uri = newuri;
890 if (!strncmp(uri, XT_URI_ABOUT, XT_URI_ABOUT_LEN)) {
891 for (i = 0; i < about_list_size(); i++)
892 if (!strcmp(&uri[XT_URI_ABOUT_LEN], about_list[i].name) &&
893 about_list[i].func != NULL) {
894 bzero(&args, sizeof args);
895 about_list[i].func(t, &args);
896 gtk_widget_set_sensitive(GTK_WIDGET(t->stop),
897 FALSE);
898 goto done;
900 show_oops(t, "invalid about page");
901 goto done;
904 /* remove old HTTPS cert chain (if any) */
905 if (t->cert_chain) {
906 g_free(t->cert_chain);
907 t->cert_chain = NULL;
910 set_status(t, "Loading: %s", (char *)uri);
911 marks_clear(t);
912 webkit_web_view_load_uri(t->wv, uri);
913 done:
914 if (newuri)
915 g_free(newuri);
918 const gchar *
919 get_uri(struct tab *t)
921 const gchar *uri = NULL;
923 if (webkit_web_view_get_load_status(t->wv) == WEBKIT_LOAD_FAILED &&
924 !t->download_requested)
925 return (NULL);
926 if (t->xtp_meaning == XT_XTP_TAB_MEANING_NORMAL)
927 uri = webkit_web_view_get_uri(t->wv);
928 else {
929 /* use tmp_uri to make sure it is g_freed */
930 if (t->tmp_uri)
931 g_free(t->tmp_uri);
932 t->tmp_uri = g_strdup_printf("%s%s", XT_URI_ABOUT,
933 about_list[t->xtp_meaning].name);
934 uri = t->tmp_uri;
936 return (uri);
939 const gchar *
940 get_title(struct tab *t, bool window)
942 const gchar *set = NULL, *title = NULL;
943 WebKitLoadStatus status = webkit_web_view_get_load_status(t->wv);
945 if (status == WEBKIT_LOAD_PROVISIONAL ||
946 (status == WEBKIT_LOAD_FAILED && !t->download_requested) ||
947 t->xtp_meaning == XT_XTP_TAB_MEANING_BL)
948 goto notitle;
950 title = webkit_web_view_get_title(t->wv);
951 if ((set = title ? title : get_uri(t)))
952 return set;
954 notitle:
955 set = window ? XT_NAME : "(untitled)";
957 return set;
960 struct mime_type *
961 find_mime_type(char *mime_type)
963 struct mime_type *m, *def = NULL, *rv = NULL;
965 TAILQ_FOREACH(m, &mtl, entry) {
966 if (m->mt_default &&
967 !strncmp(mime_type, m->mt_type, strlen(m->mt_type)))
968 def = m;
970 if (m->mt_default == 0 && !strcmp(mime_type, m->mt_type)) {
971 rv = m;
972 break;
976 if (rv == NULL)
977 rv = def;
979 return (rv);
983 * This only escapes the & and < characters, as per the discussion found here:
984 * http://lists.apple.com/archives/Webkitsdk-dev/2007/May/msg00056.html
986 char *
987 html_escape(const char *val)
989 char *s, *sp;
990 char **sv;
992 if (val == NULL)
993 return NULL;
995 sv = g_strsplit(val, "&", -1);
996 s = g_strjoinv("&amp;", sv);
997 g_strfreev(sv);
998 sp = s;
999 sv = g_strsplit(val, "<", -1);
1000 s = g_strjoinv("&lt;", sv);
1001 g_strfreev(sv);
1002 g_free(sp);
1003 return (s);
1006 struct wl_entry *
1007 wl_find_uri(const gchar *s, struct wl_list *wl)
1009 int i;
1010 char *ss;
1011 struct wl_entry *w;
1013 if (s == NULL || wl == NULL)
1014 return (NULL);
1016 if (!strncmp(s, "http://", strlen("http://")))
1017 s = &s[strlen("http://")];
1018 else if (!strncmp(s, "https://", strlen("https://")))
1019 s = &s[strlen("https://")];
1021 if (strlen(s) < 2)
1022 return (NULL);
1024 for (i = 0; i < strlen(s) + 1 /* yes er need this */; i++)
1025 /* chop string at first slash */
1026 if (s[i] == '/' || s[i] == ':' || s[i] == '\0') {
1027 ss = g_strdup(s);
1028 ss[i] = '\0';
1029 w = wl_find(ss, wl);
1030 g_free(ss);
1031 return (w);
1034 return (NULL);
1037 char *
1038 js_ref_to_string(JSContextRef context, JSValueRef ref)
1040 char *s = NULL;
1041 size_t l;
1042 JSStringRef jsref;
1044 jsref = JSValueToStringCopy(context, ref, NULL);
1045 if (jsref == NULL)
1046 return (NULL);
1048 l = JSStringGetMaximumUTF8CStringSize(jsref);
1049 s = g_malloc(l);
1050 if (s)
1051 JSStringGetUTF8CString(jsref, s, l);
1052 JSStringRelease(jsref);
1054 return (s);
1057 #define XT_JS_DONE ("done;")
1058 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
1059 #define XT_JS_INSERT ("insert;")
1060 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
1063 run_script(struct tab *t, char *s)
1065 JSGlobalContextRef ctx;
1066 WebKitWebFrame *frame;
1067 JSStringRef str;
1068 JSValueRef val, exception;
1069 char *es;
1071 DNPRINTF(XT_D_JS, "%s: tab %d %s\n", __func__,
1072 t->tab_id, s == (char *)JS_HINTING ? "JS_HINTING" : s);
1074 frame = webkit_web_view_get_main_frame(t->wv);
1075 ctx = webkit_web_frame_get_global_context(frame);
1077 str = JSStringCreateWithUTF8CString(s);
1078 val = JSEvaluateScript(ctx, str, JSContextGetGlobalObject(ctx),
1079 NULL, 0, &exception);
1080 JSStringRelease(str);
1082 DNPRINTF(XT_D_JS, "%s: val %p\n", __func__, val);
1083 if (val == NULL) {
1084 es = js_ref_to_string(ctx, exception);
1085 if (es) {
1086 DNPRINTF(XT_D_JS, "%s: exception %s\n", __func__, es);
1087 g_free(es);
1089 return (1);
1090 } else {
1091 es = js_ref_to_string(ctx, val);
1092 #if 0
1093 /* return values */
1094 if (!strncmp(es, XT_JS_DONE, XT_JS_DONE_LEN))
1095 ; /* do nothing */
1096 if (!strncmp(es, XT_JS_INSERT, XT_JS_INSERT_LEN))
1097 ; /* do nothing */
1098 #endif
1099 if (es) {
1100 DNPRINTF(XT_D_JS, "%s: val %s\n", __func__, es);
1101 g_free(es);
1105 return (0);
1108 void
1109 enable_hints(struct tab *t)
1111 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1113 if (t->new_tab)
1114 run_script(t, "hints.createHints('', 'F');");
1115 else
1116 run_script(t, "hints.createHints('', 'f');");
1117 t->mode = XT_MODE_HINT;
1120 void
1121 disable_hints(struct tab *t)
1123 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1125 run_script(t, "hints.clearHints();");
1126 t->mode = XT_MODE_COMMAND;
1127 t->new_tab = 0;
1131 passthrough(struct tab *t, struct karg *args)
1133 t->mode = XT_MODE_PASSTHROUGH;
1134 return (0);
1138 modurl(struct tab *t, struct karg *args)
1140 const gchar *uri = NULL;
1141 char *u = NULL;
1143 /* XXX kind of a bad hack, but oh well */
1144 if (gtk_widget_has_focus(t->uri_entry)) {
1145 if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) &&
1146 (strlen(uri))) {
1147 u = g_strdup_printf("www.%s.com", uri);
1148 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), u);
1149 g_free(u);
1150 activate_uri_entry_cb(t->uri_entry, t);
1153 return (0);
1157 hint(struct tab *t, struct karg *args)
1160 DNPRINTF(XT_D_JS, "hint: tab %d args %d\n", t->tab_id, args->i);
1162 if (t->mode == XT_MODE_HINT) {
1163 if (args->i == XT_HINT_NEWTAB)
1164 t->new_tab = 1;
1165 enable_hints(t);
1166 } else
1167 disable_hints(t);
1169 return (0);
1172 void
1173 apply_style(struct tab *t)
1175 t->styled = 1;
1176 g_object_set(G_OBJECT(t->settings),
1177 "user-stylesheet-uri", t->stylesheet, (char *)NULL);
1180 void
1181 remove_style(struct tab *t)
1183 t->styled = 0;
1184 g_object_set(G_OBJECT(t->settings),
1185 "user-stylesheet-uri", NULL, (char *)NULL);
1189 userstyle_cmd(struct tab *t, struct karg *args)
1191 char script[PATH_MAX] = {'\0'};
1192 char *script_uri;
1193 struct tab *tt;
1195 DNPRINTF(XT_D_JS, "userstyle_cmd: tab %d\n", t->tab_id);
1197 if (args->s != NULL && strlen(args->s)) {
1198 expand_tilde(script, sizeof script, args->s);
1199 script_uri = g_filename_to_uri(script, NULL, NULL);
1200 } else
1201 script_uri = g_strdup(userstyle);
1203 if (script_uri == NULL)
1204 return (1);
1206 switch (args->i) {
1207 case XT_STYLE_CURRENT_TAB:
1208 if (t->styled && !strcmp(script_uri, t->stylesheet))
1209 remove_style(t);
1210 else {
1211 if (t->stylesheet)
1212 g_free(t->stylesheet);
1213 t->stylesheet = g_strdup(script_uri);
1214 apply_style(t);
1216 break;
1217 case XT_STYLE_GLOBAL:
1218 if (userstyle_global && !strcmp(script_uri, t->stylesheet)) {
1219 userstyle_global = 0;
1220 TAILQ_FOREACH(tt, &tabs, entry)
1221 remove_style(tt);
1222 } else {
1223 userstyle_global = 1;
1225 /* need to save this stylesheet for new tabs */
1226 if (stylesheet)
1227 g_free(stylesheet);
1228 stylesheet = g_strdup(script_uri);
1230 TAILQ_FOREACH(tt, &tabs, entry) {
1231 if (tt->stylesheet)
1232 g_free(tt->stylesheet);
1233 tt->stylesheet = g_strdup(script_uri);
1234 apply_style(tt);
1237 break;
1240 g_free(script_uri);
1242 return (0);
1246 quit(struct tab *t, struct karg *args)
1248 if (save_global_history)
1249 save_global_history_to_disk(t);
1251 gtk_main_quit();
1253 return (1);
1256 void
1257 restore_sessions_list(void)
1259 DIR *sdir = NULL;
1260 struct dirent *dp = NULL;
1261 struct session *s;
1262 int reg;
1264 sdir = opendir(sessions_dir);
1265 if (sdir) {
1266 while ((dp = readdir(sdir)) != NULL) {
1267 #if defined __MINGW32__
1268 reg = 1; /* windows only has regular files */
1269 #else
1270 reg = dp->d_type == DT_REG;
1271 #endif
1272 if (dp && reg) {
1273 s = g_malloc(sizeof(struct session));
1274 s->name = g_strdup(dp->d_name);
1275 TAILQ_INSERT_TAIL(&sessions, s, entry);
1278 closedir(sdir);
1283 open_tabs(struct tab *t, struct karg *a)
1285 char file[PATH_MAX];
1286 FILE *f = NULL;
1287 char *uri = NULL;
1288 int rv = 1;
1289 struct tab *ti, *tt;
1291 if (a == NULL)
1292 goto done;
1294 ti = TAILQ_LAST(&tabs, tab_list);
1296 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, a->s);
1297 if ((f = fopen(file, "r")) == NULL)
1298 goto done;
1300 for (;;) {
1301 if ((uri = fparseln(f, NULL, NULL, "\0\0\0", 0)) == NULL) {
1302 if (feof(f) || ferror(f))
1303 break;
1304 } else {
1305 /* retrieve session name */
1306 if (g_str_has_prefix(uri, XT_SAVE_SESSION_ID)) {
1307 strlcpy(named_session,
1308 &uri[strlen(XT_SAVE_SESSION_ID)],
1309 sizeof named_session);
1310 continue;
1313 if (strlen(uri))
1314 create_new_tab(uri, NULL, 1, -1);
1316 free(uri);
1320 /* close open tabs */
1321 if (a->i == XT_SES_CLOSETABS && ti != NULL) {
1322 for (;;) {
1323 tt = TAILQ_FIRST(&tabs);
1324 if (tt == NULL)
1325 break;
1326 if (tt != ti) {
1327 delete_tab(tt);
1328 continue;
1330 delete_tab(tt);
1331 break;
1335 rv = 0;
1336 done:
1337 if (f)
1338 fclose(f);
1340 return (rv);
1344 restore_saved_tabs(void)
1346 char file[PATH_MAX];
1347 int unlink_file = 0;
1348 struct stat sb;
1349 struct karg a;
1350 int rv = 0;
1352 snprintf(file, sizeof file, "%s" PS "%s",
1353 sessions_dir, XT_RESTART_TABS_FILE);
1354 if (stat(file, &sb) == -1)
1355 a.s = XT_SAVED_TABS_FILE;
1356 else {
1357 unlink_file = 1;
1358 a.s = XT_RESTART_TABS_FILE;
1361 a.i = XT_SES_DONOTHING;
1362 rv = open_tabs(NULL, &a);
1364 if (unlink_file)
1365 unlink(file);
1367 return (rv);
1371 save_tabs(struct tab *t, struct karg *a)
1373 char file[PATH_MAX];
1374 FILE *f;
1375 int num_tabs = 0, i;
1376 struct tab **stabs = NULL;
1378 /* tab may be null here */
1380 if (a == NULL)
1381 return (1);
1382 if (a->s == NULL)
1383 snprintf(file, sizeof file, "%s" PS "%s",
1384 sessions_dir, named_session);
1385 else
1386 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, a->s);
1388 if ((f = fopen(file, "w")) == NULL) {
1389 show_oops(t, "Can't open save_tabs file: %s", strerror(errno));
1390 return (1);
1393 /* save session name */
1394 fprintf(f, "%s%s\n", XT_SAVE_SESSION_ID, named_session);
1396 /* Save tabs, in the order they are arranged in the notebook. */
1397 num_tabs = sort_tabs_by_page_num(&stabs);
1399 for (i = 0; i < num_tabs; i++)
1400 if (stabs[i]) {
1401 if (get_uri(stabs[i]) != NULL)
1402 fprintf(f, "%s\n", get_uri(stabs[i]));
1403 else if (gtk_entry_get_text(GTK_ENTRY(
1404 stabs[i]->uri_entry)))
1405 fprintf(f, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1406 stabs[i]->uri_entry)));
1409 g_free(stabs);
1411 /* try and make sure this gets to disk NOW. XXX Backup first? */
1412 if (fflush(f) != 0 || fsync(fileno(f)) != 0) {
1413 show_oops(t, "May not have managed to save session: %s",
1414 strerror(errno));
1417 fclose(f);
1419 return (0);
1423 save_tabs_and_quit(struct tab *t, struct karg *args)
1425 struct karg a;
1427 a.s = NULL;
1428 save_tabs(t, &a);
1429 quit(t, NULL);
1431 return (1);
1434 void
1435 expand_tilde(char *path, size_t len, const char *s)
1437 struct passwd *pwd;
1438 int i;
1439 char user[LOGIN_NAME_MAX];
1440 const char *sc = s;
1442 if (path == NULL || s == NULL)
1443 errx(1, "expand_tilde");
1445 if (s[0] != '~') {
1446 strlcpy(path, sc, len);
1447 return;
1450 ++s;
1451 for (i = 0; s[i] != PSC && s[i] != '\0'; ++i)
1452 user[i] = s[i];
1453 user[i] = '\0';
1454 s = &s[i];
1456 pwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1457 if (pwd == NULL)
1458 strlcpy(path, sc, len);
1459 else
1460 snprintf(path, len, "%s%s", pwd->pw_dir, s);
1464 run_page_script(struct tab *t, struct karg *args)
1466 const gchar *uri;
1467 char *tmp, script[PATH_MAX];
1468 char *sv[3];
1470 tmp = args->s != NULL && strlen(args->s) > 0 ? args->s : default_script;
1471 if (tmp[0] == '\0') {
1472 show_oops(t, "no script specified");
1473 return (1);
1476 if ((uri = get_uri(t)) == NULL) {
1477 show_oops(t, "tab is empty, not running script");
1478 return (1);
1481 expand_tilde(script, sizeof script, tmp);
1483 sv[0] = script;
1484 sv[1] = (char *)uri;
1485 sv[2] = NULL;
1486 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
1487 NULL, NULL)) {
1488 show_oops(t, "%s: could not spawn process: %s %s", __func__,
1489 sv[0], sv[1]);
1490 return (1);
1491 } else
1492 show_oops(t, "running: %s %s", sv[0], sv[1]);
1494 return (0);
1498 yank_uri(struct tab *t, struct karg *args)
1500 const gchar *uri;
1501 GtkClipboard *clipboard, *primary;
1503 if ((uri = get_uri(t)) == NULL)
1504 return (1);
1506 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1507 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1508 gtk_clipboard_set_text(primary, uri, -1);
1509 gtk_clipboard_set_text(clipboard, uri, -1);
1511 return (0);
1515 paste_uri(struct tab *t, struct karg *args)
1517 GtkClipboard *clipboard, *primary;
1518 gchar *c = NULL, *p = NULL, *uri;
1519 int i;
1521 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1522 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1523 c = gtk_clipboard_wait_for_text(clipboard);
1524 p = gtk_clipboard_wait_for_text(primary);
1526 if (c || p) {
1527 #ifdef __MINGW32__
1528 /* Windows try clipboard first */
1529 uri = c ? c : p;
1530 #else
1531 /* UNIX try primary first */
1532 uri = p ? p : c;
1533 #endif
1534 /* replace all newlines with spaces */
1535 for (i = 0; uri[i] != '\0'; ++i)
1536 if (uri[i] == '\n')
1537 uri[i] = ' ';
1539 while (*uri && isspace(*uri))
1540 uri++;
1541 if (strlen(uri) == 0) {
1542 show_oops(t, "empty paste buffer");
1543 goto done;
1545 if (guess_search == 0 && valid_url_type(uri)) {
1546 /* we can be clever and paste this in search box */
1547 show_oops(t, "not a valid URL");
1548 goto done;
1551 if (args->i == XT_PASTE_CURRENT_TAB)
1552 load_uri(t, uri);
1553 else if (args->i == XT_PASTE_NEW_TAB)
1554 create_new_tab(uri, NULL, 1, -1);
1557 done:
1558 if (c)
1559 g_free(c);
1560 if (p)
1561 g_free(p);
1563 return (0);
1566 void
1567 js_toggle_cb(GtkWidget *w, struct tab *t)
1569 struct karg a;
1570 int es, set;
1572 g_object_get(G_OBJECT(t->settings),
1573 "enable-scripts", &es, (char *)NULL);
1574 es = !es;
1575 if (es)
1576 set = XT_WL_ENABLE;
1577 else
1578 set = XT_WL_DISABLE;
1580 a.i = set | XT_WL_TOPLEVEL;
1581 toggle_pl(t, &a);
1583 a.i = set | XT_WL_TOPLEVEL;
1584 toggle_cwl(t, &a);
1586 a.i = XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD;
1587 toggle_js(t, &a);
1590 void
1591 proxy_toggle_cb(GtkWidget *w, struct tab *t)
1593 struct karg args = {0};
1595 args.i = XT_PRXY_TOGGLE;
1596 proxy_cmd(t, &args);
1600 toggle_src(struct tab *t, struct karg *args)
1602 gboolean mode;
1604 if (t == NULL)
1605 return (0);
1607 mode = webkit_web_view_get_view_source_mode(t->wv);
1608 webkit_web_view_set_view_source_mode(t->wv, !mode);
1609 webkit_web_view_reload(t->wv);
1611 return (0);
1614 void
1615 focus_webview(struct tab *t)
1617 if (t == NULL)
1618 return;
1620 /* only grab focus if we are visible */
1621 if (gtk_notebook_get_current_page(notebook) == t->tab_id)
1622 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
1626 focus(struct tab *t, struct karg *args)
1628 if (t == NULL || args == NULL)
1629 return (1);
1631 if (show_url == 0)
1632 return (0);
1634 if (args->i == XT_FOCUS_URI)
1635 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
1636 else if (args->i == XT_FOCUS_SEARCH)
1637 gtk_widget_grab_focus(GTK_WIDGET(t->search_entry));
1639 return (0);
1642 void
1643 free_connection_certs(gnutls_x509_crt_t *certs, size_t cert_count)
1645 int i;
1647 for (i = 0; i < cert_count; i++)
1648 gnutls_x509_crt_deinit(certs[i]);
1649 g_free(certs);
1652 #if GTK_CHECK_VERSION(3, 0, 0)
1653 void
1654 statusbar_modify_attr(struct tab *t, const char *css_name)
1656 gtk_widget_set_name(t->sbe.ebox, css_name);
1658 #else
1659 void
1660 statusbar_modify_attr(struct tab *t, const char *text, const char *base)
1662 GdkColor c_text, c_base;
1664 gdk_color_parse(text, &c_text);
1665 gdk_color_parse(base, &c_base);
1667 gtk_widget_modify_bg(t->sbe.ebox, GTK_STATE_NORMAL, &c_base);
1668 gtk_widget_modify_base(t->sbe.uri, GTK_STATE_NORMAL, &c_base);
1669 gtk_widget_modify_text(t->sbe.uri, GTK_STATE_NORMAL, &c_text);
1671 #endif
1673 void
1674 save_certs(struct tab *t, gnutls_x509_crt_t *certs,
1675 size_t cert_count, const char *domain, const char *dir)
1677 size_t cert_buf_sz;
1678 char cert_buf[64 * 1024], file[PATH_MAX];
1679 int i;
1680 FILE *f;
1682 if (t == NULL || certs == NULL || cert_count <= 0 || domain == NULL)
1683 return;
1685 snprintf(file, sizeof file, "%s" PS "%s", dir, domain);
1686 if ((f = fopen(file, "w")) == NULL) {
1687 show_oops(t, "Can't create cert file %s %s",
1688 file, strerror(errno));
1689 return;
1692 for (i = 0; i < cert_count; i++) {
1693 cert_buf_sz = sizeof cert_buf;
1694 if (gnutls_x509_crt_export(certs[i], GNUTLS_X509_FMT_PEM,
1695 cert_buf, &cert_buf_sz)) {
1696 show_oops(t, "gnutls_x509_crt_export failed");
1697 goto done;
1699 if (fwrite(cert_buf, cert_buf_sz, 1, f) != 1) {
1700 show_oops(t, "Can't write certs: %s", strerror(errno));
1701 goto done;
1705 done:
1706 fclose(f);
1709 enum cert_trust {
1710 CERT_LOCAL,
1711 CERT_TRUSTED,
1712 CERT_UNTRUSTED,
1713 CERT_BAD
1716 gnutls_x509_crt_t *
1717 get_local_cert_chain(const char *uri, size_t *ncerts, const char **error_str,
1718 const char *dir)
1720 SoupURI *su;
1721 unsigned char cert_buf[64 * 1024] = {0};
1722 gnutls_datum_t data;
1723 unsigned int len = UINT_MAX;
1724 int bytes_read;
1725 char file[PATH_MAX];
1726 FILE *f;
1727 gnutls_x509_crt_t *certs;
1729 if ((su = soup_uri_new(uri)) == NULL) {
1730 *error_str = "Invalid URI";
1731 return (NULL);
1734 snprintf(file, sizeof file, "%s" PS "%s", dir, su->host);
1735 soup_uri_free(su);
1736 if ((f = fopen(file, "r")) == NULL) {
1737 *error_str = "Could not read local cert";
1738 return (NULL);
1741 bytes_read = fread(cert_buf, sizeof *cert_buf, sizeof cert_buf, f);
1742 if (bytes_read == 0) {
1743 *error_str = "Could not read local cert";
1744 return (NULL);
1747 data.data = cert_buf;
1748 data.size = bytes_read;
1749 certs = g_malloc(sizeof *certs);
1750 *ncerts = INT_MAX;
1751 if (gnutls_x509_crt_list_import(certs, &len, &data,
1752 GNUTLS_X509_FMT_PEM, 0) < 0) {
1753 g_free(certs);
1754 *error_str = "Error reading local cert chain";
1755 return (NULL);
1758 *ncerts = len;
1759 return (certs);
1763 * This uses the pem-encoded cert chain saved in t->cert_chain instead
1764 * of grabbing the remote cert. We save it beforehand and read it
1765 * here so as to not open a side channel that ignores proxy settings.
1767 gnutls_x509_crt_t *
1768 get_remote_cert_chain(const char *uri, size_t *ncerts, const char **error_str,
1769 char *chain)
1771 gnutls_datum_t data;
1772 unsigned int len = UINT_MAX;
1773 gnutls_x509_crt_t *certs;
1775 if (chain == NULL) {
1776 *error_str = "Error reading remote cert chain";
1777 return (NULL);
1780 data.data = (unsigned char *)chain;
1781 data.size = strlen(chain);
1782 certs = g_malloc(sizeof *certs);
1783 *ncerts = INT_MAX;
1784 if (gnutls_x509_crt_list_import(certs, &len, &data,
1785 GNUTLS_X509_FMT_PEM, 0) < 0) {
1786 g_free(certs);
1787 *error_str = "Error reading remote cert chain";
1788 return (NULL);
1791 *ncerts = len;
1792 return (certs);
1797 cert_cmd(struct tab *t, struct karg *args)
1799 const gchar *uri, *error_str = NULL;
1800 size_t cert_count;
1801 gnutls_x509_crt_t *certs;
1802 SoupURI *su;
1803 #if !GTK_CHECK_VERSION(3, 0, 0)
1804 GdkColor color;
1805 #endif
1807 if (t == NULL)
1808 return (1);
1810 if (args->s != NULL) {
1811 uri = args->s;
1812 } else if ((uri = get_uri(t)) == NULL) {
1813 show_oops(t, "Invalid URI");
1814 return (1);
1816 if ((su = soup_uri_new(uri)) == NULL) {
1817 show_oops(t, "Invalid URI");
1818 return (1);
1822 * if we're only showing the local certs, don't open a socket and get
1823 * the remote certs
1825 if (args->i & XT_SHOW && args->i & XT_CACHE) {
1826 certs = get_local_cert_chain(uri, &cert_count, &error_str,
1827 certs_cache_dir);
1828 if (error_str == NULL) {
1829 show_certs(t, certs, cert_count, "Certificate Chain");
1830 free_connection_certs(certs, cert_count);
1831 } else {
1832 show_oops(t, "%s", error_str);
1833 return (1);
1835 return (0);
1838 certs = get_remote_cert_chain(uri, &cert_count, &error_str,
1839 t->cert_chain);
1840 if (error_str)
1841 goto done;
1843 if (args->i & XT_SHOW)
1844 show_certs(t, certs, cert_count, "Certificate Chain");
1845 else if (args->i & XT_SAVE) {
1846 save_certs(t, certs, cert_count, su->host, certs_dir);
1847 #if GTK_CHECK_VERSION(3, 0, 0)
1848 gtk_widget_set_name(t->uri_entry, XT_CSS_BLUE);
1849 statusbar_modify_attr(t, XT_CSS_BLUE);
1850 #else
1851 gdk_color_parse(XT_COLOR_BLUE, &color);
1852 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
1853 statusbar_modify_attr(t, XT_COLOR_BLACK, XT_COLOR_BLUE);
1854 #endif
1855 } else if (args->i & XT_CACHE)
1856 save_certs(t, certs, cert_count, su->host, certs_cache_dir);
1858 free_connection_certs(certs, cert_count);
1860 done:
1861 soup_uri_free(su);
1863 if (error_str && strlen(error_str))
1864 show_oops(t, "%s", error_str);
1865 return (0);
1869 * Checks whether the remote cert is identical to the local saved
1870 * cert. Returns CERT_LOCAL if unchanged, CERT_UNTRUSTED if local
1871 * cert does not exist, and CERT_BAD if different.
1873 * Saves entire cert chain in pem encoding to chain for it to be
1874 * cached later, if needed.
1876 enum cert_trust
1877 check_local_certs(const char *file, GTlsCertificate *cert, char **chain)
1879 char r_cert_buf[64 * 1024];
1880 FILE *f;
1881 GTlsCertificate *tmpcert = NULL;
1882 GTlsCertificate *issuer;
1883 char *pem;
1884 char *tmp;
1885 enum cert_trust rv = CERT_LOCAL;
1886 int i;
1888 if ((f = fopen(file, "r")) == NULL) {
1889 /* no local cert to check */
1890 rv = CERT_UNTRUSTED;
1893 for (i = 0;;) {
1894 if (i == 0) {
1895 g_object_get(G_OBJECT(cert), "certificate-pem", &pem,
1896 NULL);
1897 g_object_get(G_OBJECT(cert), "issuer", &issuer,NULL);
1898 } else {
1899 if (issuer == NULL)
1900 break;
1901 g_object_get(G_OBJECT(tmpcert), "issuer", &issuer,
1902 NULL);
1903 if (issuer == NULL)
1904 break;
1906 g_object_get(G_OBJECT(tmpcert), "certificate-pem", &pem,
1907 NULL);
1909 i++;
1911 tmpcert = issuer;
1912 if (f) {
1913 if (fread(r_cert_buf, strlen(pem), 1, f) != 1 && !feof(f))
1914 rv = CERT_BAD;
1915 if (bcmp(r_cert_buf, pem, strlen(pem)))
1916 rv = CERT_BAD;
1918 tmp = g_strdup_printf("%s%s", *chain, pem);
1919 g_free(*chain);
1920 *chain = tmp;
1923 if (f != NULL)
1924 fclose(f);
1925 return (rv);
1929 check_cert_changes(struct tab *t, GTlsCertificate *cert, const char *file, const char *uri)
1931 SoupURI *soupuri = NULL;
1932 struct karg args = {0};
1933 struct wl_entry *w = NULL;
1934 char *chain = NULL;
1935 int ret = 0;
1937 chain = g_strdup("");
1938 switch(check_local_certs(file, cert, &chain)) {
1939 case CERT_LOCAL:
1940 /* The cached certificate is identical */
1941 break;
1942 case CERT_TRUSTED: /* FALLTHROUGH */
1943 case CERT_UNTRUSTED:
1944 /* cache new certificate */
1945 args.i = XT_CACHE;
1946 cert_cmd(t, &args);
1947 ret = 1;
1948 break;
1949 case CERT_BAD:
1950 if ((soupuri = soup_uri_new(uri)) == NULL ||
1951 soupuri->host == NULL)
1952 break;
1953 if ((w = wl_find(soupuri->host, &svil)) != NULL)
1954 break;
1955 t->xtp_meaning = XT_XTP_TAB_MEANING_SV;
1956 args.s = g_strdup((char *)uri);
1957 xtp_page_sv(t, &args);
1958 break;
1961 if (soupuri)
1962 soup_uri_free(soupuri);
1963 if (chain)
1964 g_free(chain);
1965 return (0);
1969 remove_cookie(int index)
1971 int i, rv = 1;
1972 GSList *cf;
1973 SoupCookie *c;
1975 DNPRINTF(XT_D_COOKIE, "remove_cookie: %d\n", index);
1977 cf = soup_cookie_jar_all_cookies(s_cookiejar);
1979 for (i = 1; cf; cf = cf->next, i++) {
1980 if (i != index)
1981 continue;
1982 c = cf->data;
1983 print_cookie("remove cookie", c);
1984 soup_cookie_jar_delete_cookie(s_cookiejar, c);
1985 rv = 0;
1986 break;
1989 soup_cookies_free(cf);
1991 return (rv);
1995 remove_cookie_domain(int domain_id)
1997 int domain_count, rv = 1;
1998 GSList *cf;
1999 SoupCookie *c;
2000 char *last_domain;
2002 DNPRINTF(XT_D_COOKIE, "remove_cookie_domain: %d\n", domain_id);
2004 last_domain = "";
2005 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2007 for (domain_count = 0; cf; cf = cf->next) {
2008 c = cf->data;
2010 if (strcmp(last_domain, c->domain) != 0) {
2011 domain_count++;
2012 last_domain = c->domain;
2015 if (domain_count < domain_id)
2016 continue;
2017 else if (domain_count > domain_id)
2018 break;
2020 print_cookie("remove cookie", c);
2021 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2022 rv = 0;
2025 soup_cookies_free(cf);
2027 return (rv);
2031 remove_cookie_all()
2033 int rv = 1;
2034 GSList *cf;
2035 SoupCookie *c;
2037 DNPRINTF(XT_D_COOKIE, "remove_cookie_all\n");
2039 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2041 for (; cf; cf = cf->next) {
2042 c = cf->data;
2044 print_cookie("remove cookie", c);
2045 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2046 rv = 0;
2049 soup_cookies_free(cf);
2051 return (rv);
2055 toplevel_cmd(struct tab *t, struct karg *args)
2057 js_toggle_cb(t->js_toggle, t);
2059 return (0);
2063 can_go_back_for_real(struct tab *t)
2065 int i;
2066 WebKitWebHistoryItem *item;
2067 const gchar *uri;
2069 if (t == NULL)
2070 return (FALSE);
2072 if (t->item != NULL)
2073 return (TRUE);
2075 /* rely on webkit to make sure we can go backward when on an about page */
2076 uri = get_uri(t);
2077 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2078 g_str_has_prefix(uri, "xxxt://"))
2079 return (webkit_web_view_can_go_back(t->wv));
2081 /* the back/forward list is stupid so help determine if we can go back */
2082 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2083 item != NULL;
2084 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2085 if (strcmp(webkit_web_history_item_get_uri(item), uri))
2086 return (TRUE);
2089 return (FALSE);
2093 can_go_forward_for_real(struct tab *t)
2095 int i;
2096 WebKitWebHistoryItem *item;
2097 const gchar *uri;
2099 if (t == NULL)
2100 return (FALSE);
2102 /* rely on webkit to make sure we can go forward when on an about page */
2103 uri = get_uri(t);
2104 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2105 g_str_has_prefix(uri, "xxxt://"))
2106 return (webkit_web_view_can_go_forward(t->wv));
2108 /* the back/forwars list is stupid so help selecting a different item */
2109 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2110 item != NULL;
2111 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2112 if (strcmp(webkit_web_history_item_get_uri(item), uri))
2113 return (TRUE);
2116 return (FALSE);
2119 void
2120 go_back_for_real(struct tab *t)
2122 int i;
2123 WebKitWebHistoryItem *item;
2124 const gchar *uri;
2126 if (t == NULL)
2127 return;
2129 uri = get_uri(t);
2130 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2131 g_str_has_prefix(uri, "xxxt://")) {
2132 webkit_web_view_go_back(t->wv);
2133 return;
2135 /* the back/forwars list is stupid so help selecting a different item */
2136 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2137 item != NULL;
2138 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2139 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2140 webkit_web_view_go_to_back_forward_item(t->wv, item);
2141 break;
2146 void
2147 go_forward_for_real(struct tab *t)
2149 int i;
2150 WebKitWebHistoryItem *item;
2151 const gchar *uri;
2153 if (t == NULL)
2154 return;
2156 uri = get_uri(t);
2157 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2158 g_str_has_prefix(uri, "xxxt://")) {
2159 webkit_web_view_go_forward(t->wv);
2160 return;
2162 /* the back/forwars list is stupid so help selecting a different item */
2163 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2164 item != NULL;
2165 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2166 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2167 webkit_web_view_go_to_back_forward_item(t->wv, item);
2168 break;
2174 navaction(struct tab *t, struct karg *args)
2176 WebKitWebHistoryItem *item = NULL;
2177 WebKitWebFrame *frame;
2179 DNPRINTF(XT_D_NAV, "navaction: tab %d opcode %d\n",
2180 t->tab_id, args->i);
2182 hide_oops(t);
2183 set_normal_tab_meaning(t);
2184 if (t->item) {
2185 if (args->i == XT_NAV_BACK)
2186 item = webkit_web_back_forward_list_get_current_item(t->bfl);
2187 else
2188 item = webkit_web_back_forward_list_get_forward_item(t->bfl);
2191 switch (args->i) {
2192 case XT_NAV_BACK:
2193 if (t->item) {
2194 if (item == NULL)
2195 return (XT_CB_PASSTHROUGH);
2196 webkit_web_view_go_to_back_forward_item(t->wv, item);
2197 t->item = NULL;
2198 return (XT_CB_PASSTHROUGH);
2200 marks_clear(t);
2201 go_back_for_real(t);
2202 break;
2203 case XT_NAV_FORWARD:
2204 if (t->item) {
2205 if (item == NULL)
2206 return (XT_CB_PASSTHROUGH);
2207 webkit_web_view_go_to_back_forward_item(t->wv, item);
2208 t->item = NULL;
2209 return (XT_CB_PASSTHROUGH);
2211 marks_clear(t);
2212 go_forward_for_real(t);
2213 break;
2214 case XT_NAV_RELOAD:
2215 frame = webkit_web_view_get_main_frame(t->wv);
2216 webkit_web_frame_reload(frame);
2217 break;
2218 case XT_NAV_STOP:
2219 frame = webkit_web_view_get_main_frame(t->wv);
2220 webkit_web_frame_stop_loading(frame);
2221 break;
2223 return (XT_CB_PASSTHROUGH);
2227 move(struct tab *t, struct karg *args)
2229 GtkAdjustment *adjust;
2230 double pi, si, pos, ps, upper, lower, max;
2231 double percent;
2233 switch (args->i) {
2234 case XT_MOVE_DOWN:
2235 case XT_MOVE_UP:
2236 case XT_MOVE_BOTTOM:
2237 case XT_MOVE_TOP:
2238 case XT_MOVE_PAGEDOWN:
2239 case XT_MOVE_PAGEUP:
2240 case XT_MOVE_HALFDOWN:
2241 case XT_MOVE_HALFUP:
2242 case XT_MOVE_PERCENT:
2243 case XT_MOVE_CENTER:
2244 adjust = t->adjust_v;
2245 break;
2246 default:
2247 adjust = t->adjust_h;
2248 break;
2251 pos = gtk_adjustment_get_value(adjust);
2252 ps = gtk_adjustment_get_page_size(adjust);
2253 upper = gtk_adjustment_get_upper(adjust);
2254 lower = gtk_adjustment_get_lower(adjust);
2255 si = gtk_adjustment_get_step_increment(adjust);
2256 pi = gtk_adjustment_get_page_increment(adjust);
2257 max = upper - ps;
2259 DNPRINTF(XT_D_MOVE, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2260 "max %f si %f pi %f\n",
2261 args->i, adjust == t->adjust_h ? "horizontal" : "vertical",
2262 pos, ps, upper, lower, max, si, pi);
2264 switch (args->i) {
2265 case XT_MOVE_DOWN:
2266 case XT_MOVE_RIGHT:
2267 pos += si;
2268 gtk_adjustment_set_value(adjust, MIN(pos, max));
2269 break;
2270 case XT_MOVE_UP:
2271 case XT_MOVE_LEFT:
2272 pos -= si;
2273 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2274 break;
2275 case XT_MOVE_BOTTOM:
2276 case XT_MOVE_FARRIGHT:
2277 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2278 gtk_adjustment_set_value(adjust, max);
2279 break;
2280 case XT_MOVE_TOP:
2281 case XT_MOVE_FARLEFT:
2282 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2283 gtk_adjustment_set_value(adjust, lower);
2284 break;
2285 case XT_MOVE_PAGEDOWN:
2286 pos += pi;
2287 gtk_adjustment_set_value(adjust, MIN(pos, max));
2288 break;
2289 case XT_MOVE_PAGEUP:
2290 pos -= pi;
2291 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2292 break;
2293 case XT_MOVE_HALFDOWN:
2294 pos += pi / 2;
2295 gtk_adjustment_set_value(adjust, MIN(pos, max));
2296 break;
2297 case XT_MOVE_HALFUP:
2298 pos -= pi / 2;
2299 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2300 break;
2301 case XT_MOVE_CENTER:
2302 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2303 args->s = g_strdup("50.0");
2304 /* FALLTHROUGH */
2305 case XT_MOVE_PERCENT:
2306 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2307 percent = atoi(args->s) / 100.0;
2308 pos = max * percent;
2309 if (pos < 0.0 || pos > max)
2310 break;
2311 gtk_adjustment_set_value(adjust, pos);
2312 break;
2313 default:
2314 return (XT_CB_PASSTHROUGH);
2317 DNPRINTF(XT_D_MOVE, "move: new pos %f %f\n", pos, MIN(pos, max));
2319 return (XT_CB_HANDLED);
2322 void
2323 url_set_visibility(void)
2325 struct tab *t;
2327 TAILQ_FOREACH(t, &tabs, entry)
2328 if (show_url == 0) {
2329 gtk_widget_hide(t->toolbar);
2330 focus_webview(t);
2331 } else
2332 gtk_widget_show(t->toolbar);
2335 void
2336 notebook_tab_set_visibility(void)
2338 if (show_tabs == 0) {
2339 gtk_widget_hide(tab_bar);
2340 gtk_notebook_set_show_tabs(notebook, FALSE);
2341 } else {
2342 if (tab_style == XT_TABS_NORMAL) {
2343 gtk_widget_hide(tab_bar);
2344 gtk_notebook_set_show_tabs(notebook, TRUE);
2345 } else if (tab_style == XT_TABS_COMPACT) {
2346 gtk_widget_show(tab_bar);
2347 gtk_notebook_set_show_tabs(notebook, FALSE);
2352 void
2353 statusbar_set_visibility(void)
2355 struct tab *t;
2357 TAILQ_FOREACH(t, &tabs, entry){
2358 if (show_statusbar == 0)
2359 gtk_widget_hide(t->statusbar);
2360 else
2361 gtk_widget_show(t->statusbar);
2363 focus_webview(t);
2367 void
2368 url_set(struct tab *t, int enable_url_entry)
2370 GdkPixbuf *pixbuf;
2371 int progress;
2373 show_url = enable_url_entry;
2375 if (enable_url_entry) {
2376 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
2377 GTK_ENTRY_ICON_PRIMARY, NULL);
2378 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri), 0);
2379 } else {
2380 pixbuf = gtk_entry_get_icon_pixbuf(GTK_ENTRY(t->uri_entry),
2381 GTK_ENTRY_ICON_PRIMARY);
2382 progress =
2383 gtk_entry_get_progress_fraction(GTK_ENTRY(t->uri_entry));
2384 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
2385 GTK_ENTRY_ICON_PRIMARY, pixbuf);
2386 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
2387 progress);
2392 fullscreen(struct tab *t, struct karg *args)
2394 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2396 if (t == NULL)
2397 return (XT_CB_PASSTHROUGH);
2399 if (show_url == 0) {
2400 url_set(t, 1);
2401 show_tabs = 1;
2402 } else {
2403 url_set(t, 0);
2404 show_tabs = 0;
2407 url_set_visibility();
2408 notebook_tab_set_visibility();
2410 return (XT_CB_HANDLED);
2414 statustoggle(struct tab *t, struct karg *args)
2416 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2418 if (show_statusbar == 1) {
2419 show_statusbar = 0;
2420 statusbar_set_visibility();
2421 } else if (show_statusbar == 0) {
2422 show_statusbar = 1;
2423 statusbar_set_visibility();
2425 return (XT_CB_HANDLED);
2429 urlaction(struct tab *t, struct karg *args)
2431 int rv = XT_CB_HANDLED;
2433 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2435 if (t == NULL)
2436 return (XT_CB_PASSTHROUGH);
2438 switch (args->i) {
2439 case XT_URL_SHOW:
2440 if (show_url == 0) {
2441 url_set(t, 1);
2442 url_set_visibility();
2444 break;
2445 case XT_URL_HIDE:
2446 if (show_url == 1) {
2447 url_set(t, 0);
2448 url_set_visibility();
2450 break;
2452 return (rv);
2456 tabaction(struct tab *t, struct karg *args)
2458 int rv = XT_CB_HANDLED;
2459 char *url = args->s;
2460 struct undo *u;
2461 struct tab *tt, *tv;
2463 DNPRINTF(XT_D_TAB, "tabaction: %p %d\n", t, args->i);
2465 if (t == NULL)
2466 return (XT_CB_PASSTHROUGH);
2468 switch (args->i) {
2469 case XT_TAB_NEW:
2470 if (strlen(url) > 0)
2471 create_new_tab(url, NULL, 1, args->precount);
2472 else
2473 create_new_tab(NULL, NULL, 1, args->precount);
2474 break;
2475 case XT_TAB_DELETE:
2476 if (args->precount < 0)
2477 delete_tab(t);
2478 else
2479 TAILQ_FOREACH(tt, &tabs, entry)
2480 if (tt->tab_id == args->precount - 1) {
2481 delete_tab(tt);
2482 break;
2484 break;
2485 case XT_TAB_DELQUIT:
2486 if (gtk_notebook_get_n_pages(notebook) > 1)
2487 delete_tab(t);
2488 else
2489 quit(t, args);
2490 break;
2491 case XT_TAB_ONLY:
2492 TAILQ_FOREACH_SAFE(tt, &tabs, entry, tv)
2493 if (t != tt)
2494 delete_tab(tt);
2495 break;
2496 case XT_TAB_OPEN:
2497 if (strlen(url) > 0)
2499 else {
2500 rv = XT_CB_PASSTHROUGH;
2501 goto done;
2503 load_uri(t, url);
2504 break;
2505 case XT_TAB_SHOW:
2506 if (show_tabs == 0) {
2507 show_tabs = 1;
2508 notebook_tab_set_visibility();
2510 break;
2511 case XT_TAB_HIDE:
2512 if (show_tabs == 1) {
2513 show_tabs = 0;
2514 notebook_tab_set_visibility();
2516 break;
2517 case XT_TAB_NEXTSTYLE:
2518 if (tab_style == XT_TABS_NORMAL) {
2519 tab_style = XT_TABS_COMPACT;
2520 recolor_compact_tabs();
2522 else
2523 tab_style = XT_TABS_NORMAL;
2524 notebook_tab_set_visibility();
2525 break;
2526 case XT_TAB_UNDO_CLOSE:
2527 if (undo_count == 0) {
2528 DNPRINTF(XT_D_TAB, "%s: no tabs to undo close",
2529 __func__);
2530 goto done;
2531 } else {
2532 undo_count--;
2533 u = TAILQ_FIRST(&undos);
2534 create_new_tab(u->uri, u, 1, -1);
2536 TAILQ_REMOVE(&undos, u, entry);
2537 g_free(u->uri);
2538 /* u->history is freed in create_new_tab() */
2539 g_free(u);
2541 break;
2542 case XT_TAB_LOAD_IMAGES:
2544 if (!auto_load_images) {
2546 /* Enable auto-load images (this will load all
2547 * previously unloaded images). */
2548 g_object_set(G_OBJECT(t->settings),
2549 "auto-load-images", TRUE, (char *)NULL);
2550 webkit_web_view_set_settings(t->wv, t->settings);
2552 webkit_web_view_reload(t->wv);
2554 /* Webkit triggers an event when we change the setting,
2555 * so we can't disable the auto-loading at once.
2557 * Unfortunately, webkit does not tell us when it's done.
2558 * Instead, we wait until the next request, and then
2559 * disable autoloading again.
2561 t->load_images = TRUE;
2563 break;
2564 default:
2565 rv = XT_CB_PASSTHROUGH;
2566 goto done;
2569 done:
2570 if (args->s) {
2571 g_free(args->s);
2572 args->s = NULL;
2575 return (rv);
2579 resizetab(struct tab *t, struct karg *args)
2581 if (t == NULL || args == NULL) {
2582 show_oops(NULL, "resizetab invalid parameters");
2583 return (XT_CB_PASSTHROUGH);
2586 DNPRINTF(XT_D_TAB, "resizetab: tab %d %d\n",
2587 t->tab_id, args->i);
2589 setzoom_webkit(t, args->i);
2591 return (XT_CB_HANDLED);
2595 movetab(struct tab *t, struct karg *args)
2597 int n, dest;
2599 if (t == NULL || args == NULL) {
2600 show_oops(NULL, "movetab invalid parameters");
2601 return (XT_CB_PASSTHROUGH);
2604 DNPRINTF(XT_D_TAB, "movetab: tab %d opcode %d\n",
2605 t->tab_id, args->i);
2607 if (args->i >= XT_TAB_INVALID)
2608 return (XT_CB_PASSTHROUGH);
2610 if (TAILQ_EMPTY(&tabs))
2611 return (XT_CB_PASSTHROUGH);
2613 n = gtk_notebook_get_n_pages(notebook);
2614 dest = gtk_notebook_get_current_page(notebook);
2616 switch (args->i) {
2617 case XT_TAB_NEXT:
2618 if (args->precount < 0)
2619 dest = dest == n - 1 ? 0 : dest + 1;
2620 else
2621 dest = args->precount - 1;
2623 break;
2624 case XT_TAB_PREV:
2625 if (args->precount < 0)
2626 dest -= 1;
2627 else
2628 dest -= args->precount % n;
2630 if (dest < 0)
2631 dest += n;
2633 break;
2634 case XT_TAB_FIRST:
2635 dest = 0;
2636 break;
2637 case XT_TAB_LAST:
2638 dest = n - 1;
2639 break;
2640 default:
2641 return (XT_CB_PASSTHROUGH);
2644 if (dest < 0 || dest >= n)
2645 return (XT_CB_PASSTHROUGH);
2646 if (t->tab_id == dest) {
2647 DNPRINTF(XT_D_TAB, "movetab: do nothing\n");
2648 return (XT_CB_HANDLED);
2651 set_current_tab(dest);
2653 return (XT_CB_HANDLED);
2656 int cmd_prefix = 0;
2658 struct prompt_sub {
2659 const char *s;
2660 const char *(*f)(struct tab *);
2661 } subs[] = {
2662 { "<uri>", get_uri },
2666 command(struct tab *t, struct karg *args)
2668 struct karg a = {0};
2669 int i, cmd_setup = 0;
2670 char *s = NULL, *sp = NULL, *sl = NULL;
2671 gchar **sv;
2673 if (t == NULL || args == NULL) {
2674 show_oops(NULL, "command invalid parameters");
2675 return (XT_CB_PASSTHROUGH);
2678 switch (args->i) {
2679 case '/':
2680 s = "/";
2681 break;
2682 case '?':
2683 s = "?";
2684 break;
2685 case ':':
2686 if (cmd_prefix == 0) {
2687 if (args->s != NULL && strlen(args->s) != 0) {
2688 sp = g_strdup_printf(":%s", args->s);
2689 s = sp;
2690 } else
2691 s = ":";
2692 } else {
2693 sp = g_strdup_printf(":%d", cmd_prefix);
2694 s = sp;
2695 cmd_prefix = 0;
2697 sl = g_strdup(s);
2698 if (sp) {
2699 g_free(sp);
2700 sp = NULL;
2702 s = sl;
2703 for (i = 0; i < LENGTH(subs); ++i) {
2704 sv = g_strsplit(sl, subs[i].s, -1);
2705 if (sl)
2706 g_free(sl);
2707 sl = g_strjoinv(subs[i].f(t), sv);
2708 g_strfreev(sv);
2709 s = sl;
2711 break;
2712 case '.':
2713 t->mode = XT_MODE_HINT;
2714 a.i = 0;
2715 s = ".";
2717 * js code will auto fire() if a single link is visible,
2718 * causing the focus-out-event cb function to be called. Setup
2719 * the cmd _before_ triggering hinting code so the cmd can get
2720 * killed by the cb in this case.
2722 show_cmd(t, s);
2723 cmd_setup = 1;
2724 hint(t, &a);
2725 break;
2726 case ',':
2727 t->mode = XT_MODE_HINT;
2728 a.i = XT_HINT_NEWTAB;
2729 s = ",";
2730 show_cmd(t, s);
2731 cmd_setup = 1;
2732 hint(t, &a);
2733 break;
2734 default:
2735 show_oops(t, "command: invalid opcode %d", args->i);
2736 return (XT_CB_PASSTHROUGH);
2739 DNPRINTF(XT_D_CMD, "%s: tab %d type %s\n", __func__, t->tab_id, s);
2741 if (!cmd_setup)
2742 show_cmd(t, s);
2744 if (sp)
2745 g_free(sp);
2746 if (sl)
2747 g_free(sl);
2749 return (XT_CB_HANDLED);
2753 search(struct tab *t, struct karg *args)
2755 gboolean d;
2757 if (t == NULL || args == NULL) {
2758 show_oops(NULL, "search invalid parameters");
2759 return (1);
2762 switch (args->i) {
2763 case XT_SEARCH_NEXT:
2764 d = t->search_forward;
2765 break;
2766 case XT_SEARCH_PREV:
2767 d = !t->search_forward;
2768 break;
2769 default:
2770 return (XT_CB_PASSTHROUGH);
2773 if (t->search_text == NULL) {
2774 if (global_search == NULL)
2775 return (XT_CB_PASSTHROUGH);
2776 else {
2777 d = t->search_forward = TRUE;
2778 t->search_text = g_strdup(global_search);
2779 webkit_web_view_mark_text_matches(t->wv, global_search, FALSE, 0);
2780 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
2784 DNPRINTF(XT_D_CMD, "search: tab %d opc %d forw %d text %s\n",
2785 t->tab_id, args->i, t->search_forward, t->search_text);
2787 webkit_web_view_search_text(t->wv, t->search_text, FALSE, d, TRUE);
2789 return (XT_CB_HANDLED);
2793 session_save(struct tab *t, char *filename)
2795 struct karg a;
2796 int rv = 1;
2797 struct session *s;
2799 if (strlen(filename) == 0)
2800 goto done;
2802 if (filename[0] == '.' || filename[0] == '/')
2803 goto done;
2805 a.s = filename;
2806 if (save_tabs(t, &a))
2807 goto done;
2808 strlcpy(named_session, filename, sizeof named_session);
2810 /* add the new session to the list of sessions */
2811 s = g_malloc(sizeof(struct session));
2812 s->name = g_strdup(filename);
2813 TAILQ_INSERT_TAIL(&sessions, s, entry);
2815 rv = 0;
2816 done:
2817 return (rv);
2821 session_open(struct tab *t, char *filename)
2823 struct karg a;
2824 int rv = 1;
2826 if (strlen(filename) == 0)
2827 goto done;
2829 if (filename[0] == '.' || filename[0] == '/')
2830 goto done;
2832 a.s = filename;
2833 a.i = XT_SES_CLOSETABS;
2834 if (open_tabs(t, &a))
2835 goto done;
2837 strlcpy(named_session, filename, sizeof named_session);
2839 rv = 0;
2840 done:
2841 return (rv);
2845 session_delete(struct tab *t, char *filename)
2847 char file[PATH_MAX];
2848 int rv = 1;
2849 struct session *s;
2851 if (strlen(filename) == 0)
2852 goto done;
2854 if (filename[0] == '.' || filename[0] == '/')
2855 goto done;
2857 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, filename);
2858 if (unlink(file))
2859 goto done;
2861 if (!strcmp(filename, named_session))
2862 strlcpy(named_session, XT_SAVED_TABS_FILE,
2863 sizeof named_session);
2865 /* remove session from sessions list */
2866 TAILQ_FOREACH(s, &sessions, entry) {
2867 if (!strcmp(s->name, filename))
2868 break;
2870 if (s == NULL)
2871 goto done;
2872 TAILQ_REMOVE(&sessions, s, entry);
2873 g_free((gpointer) s->name);
2874 g_free(s);
2876 rv = 0;
2877 done:
2878 return (rv);
2882 session_cmd(struct tab *t, struct karg *args)
2884 char *filename = args->s;
2886 if (t == NULL)
2887 return (1);
2889 if (args->i & XT_SHOW)
2890 show_oops(t, "Current session: %s", named_session[0] == '\0' ?
2891 XT_SAVED_TABS_FILE : named_session);
2892 else if (args->i & XT_SAVE) {
2893 if (session_save(t, filename)) {
2894 show_oops(t, "Can't save session: %s",
2895 filename ? filename : "INVALID");
2896 goto done;
2898 } else if (args->i & XT_OPEN) {
2899 if (session_open(t, filename)) {
2900 show_oops(t, "Can't open session: %s",
2901 filename ? filename : "INVALID");
2902 goto done;
2904 } else if (args->i & XT_DELETE) {
2905 if (session_delete(t, filename)) {
2906 show_oops(t, "Can't delete session: %s",
2907 filename ? filename : "INVALID");
2908 goto done;
2911 done:
2912 return (XT_CB_PASSTHROUGH);
2916 script_cmd(struct tab *t, struct karg *args)
2918 struct stat sb;
2919 FILE *f = NULL;
2920 char *buf = NULL;
2922 if (t == NULL)
2923 goto done;
2925 if ((f = fopen(args->s, "r")) == NULL) {
2926 show_oops(t, "Can't open script file: %s", args->s);
2927 goto done;
2930 if (fstat(fileno(f), &sb) == -1) {
2931 show_oops(t, "Can't stat script file: %s", args->s);
2932 goto done;
2935 buf = g_malloc0(sb.st_size + 1);
2936 if (fread(buf, 1, sb.st_size, f) != sb.st_size) {
2937 show_oops(t, "Can't read script file: %s", args->s);
2938 goto done;
2941 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
2942 run_script(t, buf);
2944 done:
2945 if (f)
2946 fclose(f);
2947 if (buf)
2948 g_free(buf);
2950 return (XT_CB_PASSTHROUGH);
2954 * Make a hardcopy of the page
2957 print_page(struct tab *t, struct karg *args)
2959 WebKitWebFrame *frame;
2960 GtkPageSetup *ps;
2961 GtkPrintOperation *op;
2962 GtkPrintOperationAction action;
2963 GtkPrintOperationResult print_res;
2964 GError *g_err = NULL;
2965 int marg_l, marg_r, marg_t, marg_b;
2967 DNPRINTF(XT_D_PRINTING, "%s:", __func__);
2969 ps = gtk_page_setup_new();
2970 op = gtk_print_operation_new();
2971 action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG;
2972 frame = webkit_web_view_get_main_frame(t->wv);
2974 /* the default margins are too small, so we will bump them */
2975 marg_l = gtk_page_setup_get_left_margin(ps, GTK_UNIT_MM) +
2976 XT_PRINT_EXTRA_MARGIN;
2977 marg_r = gtk_page_setup_get_right_margin(ps, GTK_UNIT_MM) +
2978 XT_PRINT_EXTRA_MARGIN;
2979 marg_t = gtk_page_setup_get_top_margin(ps, GTK_UNIT_MM) +
2980 XT_PRINT_EXTRA_MARGIN;
2981 marg_b = gtk_page_setup_get_bottom_margin(ps, GTK_UNIT_MM) +
2982 XT_PRINT_EXTRA_MARGIN;
2984 /* set margins */
2985 gtk_page_setup_set_left_margin(ps, marg_l, GTK_UNIT_MM);
2986 gtk_page_setup_set_right_margin(ps, marg_r, GTK_UNIT_MM);
2987 gtk_page_setup_set_top_margin(ps, marg_t, GTK_UNIT_MM);
2988 gtk_page_setup_set_bottom_margin(ps, marg_b, GTK_UNIT_MM);
2990 gtk_print_operation_set_default_page_setup(op, ps);
2992 /* this appears to free 'op' and 'ps' */
2993 print_res = webkit_web_frame_print_full(frame, op, action, &g_err);
2995 /* check it worked */
2996 if (print_res == GTK_PRINT_OPERATION_RESULT_ERROR) {
2997 show_oops(NULL, "can't print: %s", g_err->message);
2998 g_error_free (g_err);
2999 return (1);
3002 return (0);
3006 go_home(struct tab *t, struct karg *args)
3008 load_uri(t, home);
3009 return (0);
3013 set_encoding(struct tab *t, struct karg *args)
3015 const gchar *e;
3017 if (args->s && strlen(g_strstrip(args->s)) == 0) {
3018 e = webkit_web_view_get_custom_encoding(t->wv);
3019 if (e == NULL)
3020 e = webkit_web_view_get_encoding(t->wv);
3021 show_oops(t, "encoding: %s", e ? e : "N/A");
3022 } else
3023 webkit_web_view_set_custom_encoding(t->wv, args->s);
3025 return (0);
3029 restart(struct tab *t, struct karg *args)
3031 struct karg a;
3033 a.s = XT_RESTART_TABS_FILE;
3034 save_tabs(t, &a);
3035 execvp(start_argv[0], start_argv);
3036 /* NOTREACHED */
3038 return (0);
3041 char *http_proxy_save; /* not a setting, used to toggle */
3044 proxy_cmd(struct tab *t, struct karg *args)
3046 struct tab *tt;
3048 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
3050 if (t == NULL)
3051 return (1);
3053 /* setup */
3054 if (http_proxy) {
3055 TAILQ_FOREACH(tt, &tabs, entry)
3056 gtk_widget_show(t->proxy_toggle);
3057 if (http_proxy_save)
3058 g_free(http_proxy_save);
3059 http_proxy_save = g_strdup(http_proxy);
3062 if (args->i & XT_PRXY_SHOW) {
3063 if (http_proxy)
3064 show_oops(t, "http_proxy = %s", http_proxy);
3065 else
3066 show_oops(t, "proxy is currently disabled");
3067 } else if (args->i & XT_PRXY_TOGGLE) {
3068 if (http_proxy_save == NULL && http_proxy == NULL) {
3069 show_oops(t, "can't toggle proxy");
3070 goto done;
3072 TAILQ_FOREACH(tt, &tabs, entry)
3073 gtk_widget_show(t->proxy_toggle);
3074 if (http_proxy) {
3075 setup_proxy(NULL);
3076 button_set_file(t->proxy_toggle, "tordisabled.ico");
3077 show_oops(t, "http proxy disabled");
3078 } else {
3079 setup_proxy(http_proxy_save);
3080 button_set_file(t->proxy_toggle, "torenabled.ico");
3081 show_oops(t, "http_proxy = %s", http_proxy);
3084 done:
3085 return (XT_CB_PASSTHROUGH);
3089 * If you can read this functionthen you are a sick and twisted individual.
3090 * I hope we never meet, it'll be violent.
3092 gboolean
3093 eval_cb(const GMatchInfo *info, GString *res, gpointer data)
3095 gchar *match, *num;
3096 gint start = -1, end = -1, i;
3097 struct karg *args = data;
3100 * match contains the string UP TO the match.
3102 * res is what is returned, note that whatever remains in the sent in
3103 * string is appended on the way out.
3105 * for example /123/456/789/moo came in
3106 * match contains /123/456/789/
3107 * we assign that to res and replace /789/ with the replacement text
3108 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
3111 match = g_match_info_fetch(info, 0);
3112 if (match == NULL)
3113 goto done;
3115 if (g_match_info_fetch_pos(info, 1, &start, &end) == FALSE)
3116 goto freeit;
3118 g_string_assign(res, match);
3120 i = atoi(&match[start + 1]);
3121 if (args->i == XT_URL_PLUS)
3122 i++;
3123 else
3124 i--;
3126 /* preserve whitespace when likely */
3127 num = g_strdup_printf("%0*d", end - start - 2, i);
3128 g_string_overwrite_len(res, start + 1, num, end - start - 2);
3129 g_free(num);
3131 freeit:
3132 g_free(match);
3133 done:
3134 return (FALSE); /* doesn't matter */
3138 urlmod_cmd(struct tab *t, struct karg *args)
3140 const gchar *uri;
3141 GRegex *reg;
3142 gchar *res;
3144 if (t == NULL)
3145 goto done;
3146 if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) == NULL)
3147 goto done;
3148 if (strlen(uri) == 0)
3149 goto done;
3151 reg = g_regex_new(".*(/[0-9]+/)", 0, 0, NULL);
3152 if (reg == NULL)
3153 goto done;
3154 res = g_regex_replace_eval(reg, uri, -1, 0, 0, eval_cb, args, NULL);
3155 if (res == NULL)
3156 goto free_reg;
3158 if (!strcmp(res, uri))
3159 goto free_res;
3161 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), res);
3162 activate_uri_entry_cb(t->uri_entry, t);
3164 free_res:
3165 g_free(res);
3166 free_reg:
3167 g_regex_unref(reg);
3168 done:
3169 return (XT_CB_PASSTHROUGH);
3172 struct cmd {
3173 char *cmd;
3174 int level;
3175 int (*func)(struct tab *, struct karg *);
3176 int arg;
3177 int type;
3178 } cmds[] = {
3179 { "command_mode", 0, command_mode, XT_MODE_COMMAND, 0 },
3180 { "insert_mode", 0, command_mode, XT_MODE_INSERT, 0 },
3181 { "command", 0, command, ':', 0 },
3182 { "search", 0, command, '/', 0 },
3183 { "searchb", 0, command, '?', 0 },
3184 { "hinting", 0, command, '.', 0 },
3185 { "hinting_newtab", 0, command, ',', 0 },
3186 { "togglesrc", 0, toggle_src, 0, 0 },
3187 { "editsrc", 0, edit_src, 0, 0 },
3188 { "editelement", 0, edit_element, 0, 0 },
3189 { "passthrough", 0, passthrough, 0, 0 },
3190 { "modurl", 0, modurl, 0, 0 },
3192 /* yanking and pasting */
3193 { "yankuri", 0, yank_uri, 0, 0 },
3194 { "pasteuricur", 0, paste_uri, XT_PASTE_CURRENT_TAB, 0 },
3195 { "pasteurinew", 0, paste_uri, XT_PASTE_NEW_TAB, 0 },
3197 /* search */
3198 { "searchnext", 0, search, XT_SEARCH_NEXT, 0 },
3199 { "searchprevious", 0, search, XT_SEARCH_PREV, 0 },
3201 /* focus */
3202 { "focusaddress", 0, focus, XT_FOCUS_URI, 0 },
3203 { "focussearch", 0, focus, XT_FOCUS_SEARCH, 0 },
3205 /* hinting */
3206 { "hinting", 0, hint, 0, 0 },
3207 { "hinting_newtab", 0, hint, XT_HINT_NEWTAB, 0 },
3209 /* custom stylesheet */
3210 { "userstyle", 0, userstyle_cmd, XT_STYLE_CURRENT_TAB, XT_USERARG },
3211 { "userstyle_global", 0, userstyle_cmd, XT_STYLE_GLOBAL, XT_USERARG },
3213 /* navigation */
3214 { "goback", 0, navaction, XT_NAV_BACK, 0 },
3215 { "goforward", 0, navaction, XT_NAV_FORWARD, 0 },
3216 { "reload", 0, navaction, XT_NAV_RELOAD, 0 },
3217 { "stop", 0, navaction, XT_NAV_STOP, 0 },
3219 /* vertical movement */
3220 { "scrolldown", 0, move, XT_MOVE_DOWN, 0 },
3221 { "scrollup", 0, move, XT_MOVE_UP, 0 },
3222 { "scrollbottom", 0, move, XT_MOVE_BOTTOM, 0 },
3223 { "scrolltop", 0, move, XT_MOVE_TOP, 0 },
3224 { "1", 0, move, XT_MOVE_TOP, 0 },
3225 { "scrollhalfdown", 0, move, XT_MOVE_HALFDOWN, 0 },
3226 { "scrollhalfup", 0, move, XT_MOVE_HALFUP, 0 },
3227 { "scrollpagedown", 0, move, XT_MOVE_PAGEDOWN, 0 },
3228 { "scrollpageup", 0, move, XT_MOVE_PAGEUP, 0 },
3229 /* horizontal movement */
3230 { "scrollright", 0, move, XT_MOVE_RIGHT, 0 },
3231 { "scrollleft", 0, move, XT_MOVE_LEFT, 0 },
3232 { "scrollfarright", 0, move, XT_MOVE_FARRIGHT, 0 },
3233 { "scrollfarleft", 0, move, XT_MOVE_FARLEFT, 0 },
3235 { "favorites", 0, xtp_page_fl, XT_SHOW, 0 },
3236 { "fav", 0, xtp_page_fl, XT_SHOW, 0 },
3237 { "favedit", 0, xtp_page_fl, XT_SHOW|XT_DELETE, 0 },
3238 { "favadd", 0, add_favorite, 0, 0 },
3240 { "qall", 0, quit, 0, 0 },
3241 { "quitall", 0, quit, 0, 0 },
3242 { "w", 0, save_tabs, 0, 0 },
3243 { "wq", 0, save_tabs_and_quit, 0, 0 },
3244 { "help", 0, help, 0, 0 },
3245 { "about", 0, xtp_page_ab, 0, 0 },
3246 { "stats", 0, stats, 0, 0 },
3247 { "version", 0, xtp_page_ab, 0, 0 },
3249 /* js command */
3250 { "js", 0, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3251 { "save", 1, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3252 { "domain", 2, js_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3253 { "fqdn", 2, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3254 { "show", 1, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3255 { "all", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3256 { "persistent", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3257 { "session", 2, js_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3258 { "toggle", 1, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3259 { "domain", 2, js_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3260 { "fqdn", 2, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3262 /* cookie command */
3263 { "cookie", 0, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3264 { "save", 1, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3265 { "domain", 2, cookie_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3266 { "fqdn", 2, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3267 { "show", 1, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3268 { "all", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3269 { "persistent", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3270 { "session", 2, cookie_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3271 { "toggle", 1, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3272 { "domain", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3273 { "fqdn", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3274 { "purge", 1, cookie_cmd, XT_DELETE, 0 },
3276 /* plugin command */
3277 { "plugin", 0, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3278 { "save", 1, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3279 { "domain", 2, pl_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3280 { "fqdn", 2, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3281 { "show", 1, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3282 { "all", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3283 { "persistent", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3284 { "session", 2, pl_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3285 { "toggle", 1, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3286 { "domain", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3287 { "fqdn", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3289 /* https command */
3290 { "https", 0, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3291 { "save", 1, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3292 { "domain", 2, https_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3293 { "fqdn", 2, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3294 { "show", 1, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3295 { "all", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3296 { "persistent", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3297 { "session", 2, https_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3298 { "toggle", 1, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3299 { "domain", 2, https_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3300 { "fqdn", 2, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3302 /* toplevel (domain) command */
3303 { "toplevel", 0, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3304 { "toggle", 1, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3306 /* cookie jar */
3307 { "cookiejar", 0, xtp_page_cl, 0, 0 },
3309 /* cert command */
3310 { "cert", 0, cert_cmd, XT_SHOW, 0 },
3311 { "save", 1, cert_cmd, XT_SAVE, 0 },
3312 { "show", 1, cert_cmd, XT_SHOW, 0 },
3314 { "ca", 0, ca_cmd, 0, 0 },
3315 { "downloadmgr", 0, xtp_page_dl, 0, 0 },
3316 { "dl", 0, xtp_page_dl, 0, 0 },
3317 { "h", 0, xtp_page_hl, 0, 0 },
3318 { "history", 0, xtp_page_hl, 0, 0 },
3319 { "home", 0, go_home, 0, 0 },
3320 { "restart", 0, restart, 0, 0 },
3321 { "urlhide", 0, urlaction, XT_URL_HIDE, 0 },
3322 { "urlshow", 0, urlaction, XT_URL_SHOW, 0 },
3323 { "statustoggle", 0, statustoggle, 0, 0 },
3324 { "run_script", 0, run_page_script, 0, XT_USERARG },
3326 { "print", 0, print_page, 0, 0 },
3328 /* tabs */
3329 { "focusin", 0, resizetab, XT_ZOOM_IN, 0 },
3330 { "focusout", 0, resizetab, XT_ZOOM_OUT, 0 },
3331 { "focusreset", 0, resizetab, XT_ZOOM_NORMAL, 0 },
3332 { "q", 0, tabaction, XT_TAB_DELQUIT, 0 },
3333 { "quit", 0, tabaction, XT_TAB_DELQUIT, 0 },
3334 { "open", 0, tabaction, XT_TAB_OPEN, XT_URLARG },
3335 { "tabclose", 0, tabaction, XT_TAB_DELETE, XT_PREFIX | XT_INTARG},
3336 { "tabedit", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3337 { "tabfirst", 0, movetab, XT_TAB_FIRST, 0 },
3338 { "tabhide", 0, tabaction, XT_TAB_HIDE, 0 },
3339 { "tablast", 0, movetab, XT_TAB_LAST, 0 },
3340 { "tabnew", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3341 { "tabnext", 0, movetab, XT_TAB_NEXT, XT_PREFIX | XT_INTARG},
3342 { "tabnextstyle", 0, tabaction, XT_TAB_NEXTSTYLE, 0 },
3343 { "tabonly", 0, tabaction, XT_TAB_ONLY, 0 },
3344 { "tabprevious", 0, movetab, XT_TAB_PREV, XT_PREFIX | XT_INTARG},
3345 { "tabrewind", 0, movetab, XT_TAB_FIRST, 0 },
3346 { "tabshow", 0, tabaction, XT_TAB_SHOW, 0 },
3347 { "tabs", 0, buffers, 0, 0 },
3348 { "tabundoclose", 0, tabaction, XT_TAB_UNDO_CLOSE, 0 },
3349 { "buffers", 0, buffers, 0, 0 },
3350 { "ls", 0, buffers, 0, 0 },
3351 { "encoding", 0, set_encoding, 0, XT_USERARG },
3352 { "loadimages", 0, tabaction, XT_TAB_LOAD_IMAGES, 0 },
3354 /* settings */
3355 { "set", 0, set, 0, XT_SETARG },
3356 { "runtime", 0, xtp_page_rt, 0, 0 },
3358 { "fullscreen", 0, fullscreen, 0, 0 },
3359 { "f", 0, fullscreen, 0, 0 },
3361 /* sessions */
3362 { "session", 0, session_cmd, XT_SHOW, 0 },
3363 { "delete", 1, session_cmd, XT_DELETE, XT_SESSARG },
3364 { "open", 1, session_cmd, XT_OPEN, XT_SESSARG },
3365 { "save", 1, session_cmd, XT_SAVE, XT_USERARG },
3366 { "show", 1, session_cmd, XT_SHOW, 0 },
3368 /* external javascript */
3369 { "script", 0, script_cmd, XT_EJS_SHOW, XT_USERARG },
3371 /* inspector */
3372 { "inspector", 0, inspector_cmd, XT_INS_SHOW, 0 },
3373 { "show", 1, inspector_cmd, XT_INS_SHOW, 0 },
3374 { "hide", 1, inspector_cmd, XT_INS_HIDE, 0 },
3376 /* proxy */
3377 { "proxy", 0, proxy_cmd, XT_PRXY_SHOW, 0 },
3378 { "show", 1, proxy_cmd, XT_PRXY_SHOW, 0 },
3379 { "toggle", 1, proxy_cmd, XT_PRXY_TOGGLE, 0 },
3381 /* url mod */
3382 { "urlmod", 0, urlmod_cmd, XT_URL, 0 },
3383 { "plus", 1, urlmod_cmd, XT_URL_PLUS, 0 },
3384 { "min", 1, urlmod_cmd, XT_URL_MIN, 0 },
3387 struct {
3388 int index;
3389 int len;
3390 gchar *list[256];
3391 } cmd_status = {-1, 0};
3393 gboolean
3394 wv_release_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3397 if (e->type == GDK_BUTTON_RELEASE && e->button == 1)
3398 btn_down = 0;
3400 return (FALSE);
3403 gboolean
3404 wv_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3406 struct karg a;
3407 WebKitHitTestResult *hit_test_result;
3408 guint context;
3410 hit_test_result = webkit_web_view_get_hit_test_result(t->wv, e);
3411 g_object_get(hit_test_result, "context", &context, NULL);
3413 hide_oops(t);
3414 hide_buffers(t);
3416 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE)
3417 t->mode = XT_MODE_INSERT;
3418 else
3419 t->mode = XT_MODE_COMMAND;
3421 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3422 btn_down = 1;
3423 else if (e->type == GDK_BUTTON_PRESS && e->button == 8 /* btn 4 */) {
3424 /* go backward */
3425 a.i = XT_NAV_BACK;
3426 navaction(t, &a);
3428 return (TRUE);
3429 } else if (e->type == GDK_BUTTON_PRESS && e->button == 9 /* btn 5 */) {
3430 /* go forward */
3431 a.i = XT_NAV_FORWARD;
3432 navaction(t, &a);
3434 return (TRUE);
3437 return (FALSE);
3440 gboolean
3441 tab_close_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3443 DNPRINTF(XT_D_TAB, "tab_close_cb: tab %d\n", t->tab_id);
3445 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3446 delete_tab(t);
3448 return (FALSE);
3452 parse_custom_uri(struct tab *t, const char *uri)
3454 struct custom_uri *u;
3455 int handled = 0;
3456 char *sv[3];
3458 TAILQ_FOREACH(u, &cul, entry) {
3459 if (strncmp(uri, u->uri, strlen(u->uri)))
3460 continue;
3462 handled = 1;
3463 sv[0] = u->cmd;
3464 sv[1] = (char *)uri;
3465 sv[2] = NULL;
3466 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL,
3467 NULL, NULL, NULL))
3468 show_oops(t, "%s: could not spawn process", __func__);
3471 return (handled);
3474 void
3475 activate_uri_entry_cb(GtkWidget* entry, struct tab *t)
3477 const gchar *uri = gtk_entry_get_text(GTK_ENTRY(entry));
3479 DNPRINTF(XT_D_URL, "activate_uri_entry_cb: %s\n", uri);
3481 if (t == NULL) {
3482 show_oops(NULL, "activate_uri_entry_cb invalid parameters");
3483 return;
3486 if (uri == NULL) {
3487 show_oops(t, "activate_uri_entry_cb no uri");
3488 return;
3491 uri += strspn(uri, "\t ");
3493 if (parse_custom_uri(t, uri))
3494 return;
3496 /* otherwise continue to load page normally */
3497 load_uri(t, (gchar *)uri);
3498 focus_webview(t);
3501 void
3502 activate_search_entry_cb(GtkWidget* entry, struct tab *t)
3504 const gchar *search = gtk_entry_get_text(GTK_ENTRY(entry));
3505 char *newuri = NULL;
3506 gchar *enc_search;
3507 char **sv;
3509 DNPRINTF(XT_D_URL, "activate_search_entry_cb: %s\n", search);
3511 if (t == NULL) {
3512 show_oops(NULL, "activate_search_entry_cb invalid parameters");
3513 return;
3516 if (search_string == NULL || strlen(search_string) == 0) {
3517 show_oops(t, "no search_string");
3518 return;
3521 set_normal_tab_meaning(t);
3523 enc_search = soup_uri_encode(search, XT_RESERVED_CHARS);
3524 sv = g_strsplit(search_string, "%s", 2);
3525 newuri = g_strjoinv(enc_search, sv);
3526 g_free(enc_search);
3527 g_strfreev(sv);
3529 marks_clear(t);
3530 load_uri(t, newuri);
3531 focus_webview(t);
3533 if (newuri)
3534 g_free(newuri);
3537 void
3538 check_and_set_cookie(const gchar *uri, struct tab *t)
3540 struct wl_entry *w = NULL;
3541 int es = 0;
3543 if (uri == NULL || t == NULL)
3544 return;
3546 if ((w = wl_find_uri(uri, &c_wl)) == NULL)
3547 es = 0;
3548 else
3549 es = 1;
3551 DNPRINTF(XT_D_COOKIE, "check_and_set_cookie: %s %s\n",
3552 es ? "enable" : "disable", uri);
3554 g_object_set(G_OBJECT(t->settings),
3555 "enable-html5-local-storage", es, (char *)NULL);
3556 webkit_web_view_set_settings(t->wv, t->settings);
3559 void
3560 check_and_set_js(const gchar *uri, struct tab *t)
3562 struct wl_entry *w = NULL;
3563 int es = 0;
3565 if (uri == NULL || t == NULL)
3566 return;
3568 if ((w = wl_find_uri(uri, &js_wl)) == NULL)
3569 es = 0;
3570 else
3571 es = 1;
3573 DNPRINTF(XT_D_JS, "check_and_set_js: %s %s\n",
3574 es ? "enable" : "disable", uri);
3576 g_object_set(G_OBJECT(t->settings),
3577 "enable-scripts", es, (char *)NULL);
3578 g_object_set(G_OBJECT(t->settings),
3579 "javascript-can-open-windows-automatically", es, (char *)NULL);
3580 webkit_web_view_set_settings(t->wv, t->settings);
3582 button_set_stockid(t->js_toggle,
3583 es ? GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE);
3586 void
3587 check_and_set_pl(const gchar *uri, struct tab *t)
3589 struct wl_entry *w = NULL;
3590 int es = 0;
3592 if (uri == NULL || t == NULL)
3593 return;
3595 if ((w = wl_find_uri(uri, &pl_wl)) == NULL)
3596 es = 0;
3597 else
3598 es = 1;
3600 DNPRINTF(XT_D_JS, "check_and_set_pl: %s %s\n",
3601 es ? "enable" : "disable", uri);
3603 g_object_set(G_OBJECT(t->settings),
3604 "enable-plugins", es, (char *)NULL);
3605 webkit_web_view_set_settings(t->wv, t->settings);
3608 #if GTK_CHECK_VERSION(3, 0, 0)
3609 /* A lot of this can be removed when gtk2 is dropped on the floor */
3610 char *
3611 get_css_name(const char *col_str)
3613 char *name = NULL;
3615 if (!strcmp(col_str, XT_COLOR_WHITE))
3616 name = g_strdup(XT_CSS_NORMAL);
3617 else if (!strcmp(col_str, XT_COLOR_RED))
3618 name = g_strdup(XT_CSS_RED);
3619 else if (!strcmp(col_str, XT_COLOR_YELLOW))
3620 name = g_strdup(XT_CSS_YELLOW);
3621 else if (!strcmp(col_str, XT_COLOR_GREEN))
3622 name = g_strdup(XT_CSS_GREEN);
3623 else if (!strcmp(col_str, XT_COLOR_BLUE))
3624 name = g_strdup(XT_CSS_BLUE);
3625 return (name);
3627 #endif
3629 void
3630 show_ca_status(struct tab *t, const char *uri)
3632 char domain[8182], file[PATH_MAX];
3633 SoupMessage *msg = NULL;
3634 GTlsCertificate *cert = NULL;
3635 GTlsCertificateFlags flags = 0;
3636 gchar *col_str = XT_COLOR_RED;
3637 char *chain;
3638 #if GTK_CHECK_VERSION(3, 0, 0)
3639 char *name;
3640 #else
3641 GdkColor color;
3642 gchar *text, *base;
3643 #endif
3644 enum cert_trust trust;
3645 int nocolor = 0;
3646 int i;
3648 DNPRINTF(XT_D_URL, "show_ca_status: %d %s %s\n",
3649 ssl_strict_certs, ssl_ca_file, uri);
3651 if (t == NULL)
3652 return;
3654 if (uri == NULL || g_str_has_prefix(uri, "http://") ||
3655 !g_str_has_prefix(uri, "https://"))
3656 return;
3658 msg = soup_message_new("GET", uri);
3659 if (msg == NULL)
3660 return;
3661 soup_session_send_message(session, msg);
3662 if (msg->status_code == SOUP_STATUS_SSL_FAILED ||
3663 msg->status_code == SOUP_STATUS_TLS_FAILED) {
3664 DNPRINTF(XT_D_URL, "%s: status not ok: %d\n", uri,
3665 msg->status_code);
3666 goto done;
3668 if (!soup_message_get_https_status(msg, &cert, &flags)) {
3669 DNPRINTF(XT_D_URL, "%s: invalid response\n", uri);
3670 goto done;
3672 if (!G_IS_TLS_CERTIFICATE(cert)) {
3673 DNPRINTF(XT_D_URL, "%s: no cert\n", uri);
3674 goto done;
3677 if (flags == 0)
3678 col_str = XT_COLOR_GREEN;
3679 else
3680 col_str = XT_COLOR_YELLOW;
3682 strlcpy(domain, uri + strlen("https://"), sizeof domain);
3683 for (i = 0; i < strlen(domain); i++)
3684 if (domain[i] == '/') {
3685 domain[i] = '\0';
3686 break;
3689 snprintf(file, sizeof file, "%s" PS "%s", certs_cache_dir, domain);
3690 if (warn_cert_changes) {
3691 if (check_cert_changes(t, cert, file, uri))
3692 nocolor = 1;
3695 snprintf(file, sizeof file, "%s" PS "%s", certs_dir, domain);
3696 chain = g_strdup("");
3697 if ((trust = check_local_certs(file, cert, &chain)) == CERT_LOCAL)
3698 col_str = XT_COLOR_BLUE;
3699 if (t->cert_chain)
3700 g_free(t->cert_chain);
3701 t->cert_chain = chain;
3703 done:
3704 g_object_unref(msg);
3705 if (!strcmp(col_str, XT_COLOR_WHITE) || nocolor) {
3706 #if GTK_CHECK_VERSION(3, 0, 0)
3707 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
3708 statusbar_modify_attr(t, XT_CSS_NORMAL);
3709 #else
3710 text = gdk_color_to_string(
3711 &t->default_style->text[GTK_STATE_NORMAL]);
3712 base = gdk_color_to_string(
3713 &t->default_style->base[GTK_STATE_NORMAL]);
3714 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
3715 &t->default_style->base[GTK_STATE_NORMAL]);
3716 statusbar_modify_attr(t, text, base);
3717 g_free(text);
3718 g_free(base);
3719 #endif
3720 } else {
3721 #if GTK_CHECK_VERSION(3, 0, 0)
3722 name = get_css_name(col_str);
3723 gtk_widget_set_name(t->uri_entry, name);
3724 statusbar_modify_attr(t, name);
3725 g_free(name);
3726 #else
3727 gdk_color_parse(col_str, &color);
3728 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
3729 statusbar_modify_attr(t, XT_COLOR_BLACK, col_str);
3730 #endif
3734 void
3735 free_favicon(struct tab *t)
3737 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p req %p\n",
3738 __func__, t->icon_download, t->icon_request);
3740 if (t->icon_request)
3741 g_object_unref(t->icon_request);
3742 if (t->icon_dest_uri)
3743 g_free(t->icon_dest_uri);
3745 t->icon_request = NULL;
3746 t->icon_dest_uri = NULL;
3749 void
3750 xt_icon_from_name(struct tab *t, gchar *name)
3752 if (!enable_favicon_entry)
3753 return;
3755 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->uri_entry),
3756 GTK_ENTRY_ICON_PRIMARY, "text-html");
3757 if (show_url == 0)
3758 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3759 GTK_ENTRY_ICON_PRIMARY, "text-html");
3760 else
3761 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3762 GTK_ENTRY_ICON_PRIMARY, NULL);
3765 void
3766 xt_icon_from_pixbuf(struct tab *t, GdkPixbuf *pb)
3768 GdkPixbuf *pb_scaled;
3770 if (gdk_pixbuf_get_width(pb) > 16 || gdk_pixbuf_get_height(pb) > 16)
3771 pb_scaled = gdk_pixbuf_scale_simple(pb, 16, 16,
3772 GDK_INTERP_BILINEAR);
3773 else
3774 pb_scaled = pb;
3776 if (enable_favicon_entry) {
3778 /* Classic tabs. */
3779 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->uri_entry),
3780 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3782 /* Minimal tabs. */
3783 if (show_url == 0) {
3784 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
3785 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3786 } else
3787 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3788 GTK_ENTRY_ICON_PRIMARY, NULL);
3791 /* XXX: Only supports the minimal tabs atm. */
3792 if (enable_favicon_tabs)
3793 gtk_image_set_from_pixbuf(GTK_IMAGE(t->tab_elems.favicon),
3794 pb_scaled);
3796 if (pb_scaled != pb)
3797 g_object_unref(pb_scaled);
3800 void
3801 xt_icon_from_file(struct tab *t, char *uri)
3803 GdkPixbuf *pb;
3804 char *file;
3806 if (g_str_has_prefix(uri, "file://"))
3807 file = g_filename_from_uri(uri, NULL, NULL);
3808 else
3809 file = g_strdup(uri);
3811 if (file == NULL)
3812 return;
3814 pb = gdk_pixbuf_new_from_file(file, NULL);
3815 if (pb) {
3816 xt_icon_from_pixbuf(t, pb);
3817 g_object_unref(pb);
3818 } else
3819 xt_icon_from_name(t, "text-html");
3821 g_free(file);
3824 gboolean
3825 is_valid_icon(char *file)
3827 gboolean valid = 0;
3828 const char *mime_type;
3829 GFileInfo *fi;
3830 GFile *gf;
3832 gf = g_file_new_for_path(file);
3833 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
3834 NULL, NULL);
3835 mime_type = g_file_info_get_content_type(fi);
3836 valid = g_strcmp0(mime_type, "image/x-ico") == 0 ||
3837 g_strcmp0(mime_type, "image/vnd.microsoft.icon") == 0 ||
3838 g_strcmp0(mime_type, "image/png") == 0 ||
3839 g_strcmp0(mime_type, "image/gif") == 0 ||
3840 g_strcmp0(mime_type, "application/octet-stream") == 0;
3841 g_object_unref(fi);
3842 g_object_unref(gf);
3844 return (valid);
3847 void
3848 set_favicon_from_file(struct tab *t, char *uri)
3850 struct stat sb;
3851 char *file;
3853 if (t == NULL || uri == NULL)
3854 return;
3856 if (g_str_has_prefix(uri, "file://"))
3857 file = g_filename_from_uri(uri, NULL, NULL);
3858 else
3859 file = g_strdup(uri);
3861 if (file == NULL)
3862 return;
3864 DNPRINTF(XT_D_DOWNLOAD, "%s: loading %s\n", __func__, file);
3866 if (!stat(file, &sb)) {
3867 if (sb.st_size == 0 || !is_valid_icon(file)) {
3868 /* corrupt icon so trash it */
3869 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
3870 __func__, file);
3871 unlink(file);
3872 /* no need to set icon to default here */
3873 goto done;
3876 xt_icon_from_file(t, file);
3877 done:
3878 g_free(file);
3881 void
3882 favicon_download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
3883 WebKitWebView *wv)
3885 WebKitDownloadStatus status = webkit_download_get_status(download);
3886 struct tab *tt = NULL, *t = NULL;
3889 * find the webview instead of passing in the tab as it could have been
3890 * deleted from underneath us.
3892 TAILQ_FOREACH(tt, &tabs, entry) {
3893 if (tt->wv == wv) {
3894 t = tt;
3895 break;
3898 if (t == NULL)
3899 return;
3901 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d status %d\n",
3902 __func__, t->tab_id, status);
3904 switch (status) {
3905 case WEBKIT_DOWNLOAD_STATUS_ERROR:
3906 /* -1 */
3907 t->icon_download = NULL;
3908 free_favicon(t);
3909 break;
3910 case WEBKIT_DOWNLOAD_STATUS_CREATED:
3911 /* 0 */
3912 break;
3913 case WEBKIT_DOWNLOAD_STATUS_STARTED:
3914 /* 1 */
3915 break;
3916 case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
3917 /* 2 */
3918 DNPRINTF(XT_D_DOWNLOAD, "%s: freeing favicon %d\n",
3919 __func__, t->tab_id);
3920 t->icon_download = NULL;
3921 free_favicon(t);
3922 break;
3923 case WEBKIT_DOWNLOAD_STATUS_FINISHED:
3924 /* 3 */
3926 DNPRINTF(XT_D_DOWNLOAD, "%s: setting icon to %s\n",
3927 __func__, t->icon_dest_uri);
3928 set_favicon_from_file(t, t->icon_dest_uri);
3929 /* these will be freed post callback */
3930 t->icon_request = NULL;
3931 t->icon_download = NULL;
3932 break;
3933 default:
3934 break;
3938 void
3939 abort_favicon_download(struct tab *t)
3941 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p\n", __func__, t->icon_download);
3943 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
3944 if (t->icon_download) {
3945 g_signal_handlers_disconnect_by_func(G_OBJECT(t->icon_download),
3946 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
3947 webkit_download_cancel(t->icon_download);
3948 t->icon_download = NULL;
3949 } else
3950 free_favicon(t);
3951 #endif
3953 xt_icon_from_name(t, "text-html");
3956 void
3957 notify_icon_loaded_cb(WebKitWebView *wv, gchar *uri, struct tab *t)
3959 DNPRINTF(XT_D_DOWNLOAD, "%s %s\n", __func__, uri);
3961 if (uri == NULL || t == NULL)
3962 return;
3964 #if WEBKIT_CHECK_VERSION(1, 4, 0)
3965 /* take icon from WebKitIconDatabase */
3966 GdkPixbuf *pb = NULL;
3968 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
3969 #if WEBKIT_CHECK_VERSION(1, 8, 0)
3971 * If the page was not loaded (for example, via ssl_strict_certs), do
3972 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
3973 * glib warning.
3975 if (wv && webkit_web_view_get_uri(wv))
3976 pb = webkit_web_view_try_get_favicon_pixbuf(wv, 0, 0);
3977 #else
3978 if (wv && webkit_web_view_get_uri(wv))
3979 pb = webkit_web_view_get_icon_pixbuf(wv);
3980 #endif
3981 if (pb) {
3982 xt_icon_from_pixbuf(t, pb);
3983 g_object_unref(pb);
3984 } else
3985 xt_icon_from_name(t, "text-html");
3986 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
3987 /* download icon to cache dir */
3988 gchar *name_hash, file[PATH_MAX];
3989 struct stat sb;
3991 if (t->icon_request) {
3992 DNPRINTF(XT_D_DOWNLOAD, "%s: download in progress\n", __func__);
3993 return;
3996 /* check to see if we got the icon in cache */
3997 name_hash = g_compute_checksum_for_string(G_CHECKSUM_SHA256, uri, -1);
3998 snprintf(file, sizeof file, "%s" PS "%s.ico", cache_dir, name_hash);
3999 g_free(name_hash);
4001 if (!stat(file, &sb)) {
4002 if (sb.st_size > 0) {
4003 DNPRINTF(XT_D_DOWNLOAD, "%s: loading from cache %s\n",
4004 __func__, file);
4005 set_favicon_from_file(t, file);
4006 return;
4009 /* corrupt icon so trash it */
4010 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
4011 __func__, file);
4012 unlink(file);
4015 /* create download for icon */
4016 t->icon_request = webkit_network_request_new(uri);
4017 if (t->icon_request == NULL) {
4018 DNPRINTF(XT_D_DOWNLOAD, "%s: invalid uri %s\n",
4019 __func__, uri);
4020 return;
4023 t->icon_download = webkit_download_new(t->icon_request);
4024 if (t->icon_download == NULL)
4025 return;
4027 /* we have to free icon_dest_uri later */
4028 if ((t->icon_dest_uri = g_filename_to_uri(file, NULL, NULL)) == NULL)
4029 return;
4030 webkit_download_set_destination_uri(t->icon_download,
4031 t->icon_dest_uri);
4033 if (webkit_download_get_status(t->icon_download) ==
4034 WEBKIT_DOWNLOAD_STATUS_ERROR) {
4035 g_object_unref(t->icon_request);
4036 g_free(t->icon_dest_uri);
4037 t->icon_request = NULL;
4038 t->icon_dest_uri = NULL;
4039 return;
4042 g_signal_connect(G_OBJECT(t->icon_download), "notify::status",
4043 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
4045 webkit_download_start(t->icon_download);
4046 #endif
4049 void
4050 notify_load_status_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4052 const gchar *uri = NULL;
4053 struct history *h, find;
4054 struct karg a;
4055 gchar *tmp_uri = NULL;
4056 #if !GTK_CHECK_VERSION(3, 0, 0)
4057 gchar *text, *base;
4058 #endif
4060 DNPRINTF(XT_D_URL, "notify_load_status_cb: %d %s\n",
4061 webkit_web_view_get_load_status(wview),
4062 get_uri(t) ? get_uri(t) : "NOTHING");
4064 if (t == NULL) {
4065 show_oops(NULL, "notify_load_status_cb invalid parameters");
4066 return;
4069 switch (webkit_web_view_get_load_status(wview)) {
4070 case WEBKIT_LOAD_PROVISIONAL:
4071 /* 0 */
4072 abort_favicon_download(t);
4073 #if GTK_CHECK_VERSION(2, 20, 0)
4074 gtk_widget_show(t->spinner);
4075 gtk_spinner_start(GTK_SPINNER(t->spinner));
4076 #endif
4077 t->download_requested = 0;
4079 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), TRUE);
4081 /* assume we are a new address */
4082 #if GTK_CHECK_VERSION(3, 0, 0)
4083 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
4084 statusbar_modify_attr(t, XT_CSS_NORMAL);
4085 #else
4086 text = gdk_color_to_string(
4087 &t->default_style->text[GTK_STATE_NORMAL]);
4088 base = gdk_color_to_string(
4089 &t->default_style->base[GTK_STATE_NORMAL]);
4090 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
4091 &t->default_style->base[GTK_STATE_NORMAL]);
4092 statusbar_modify_attr(t, text, base);
4093 g_free(text);
4094 g_free(base);
4095 #endif
4097 /* DOM is changing, unreference the previous focused element */
4098 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4099 if (t->active)
4100 g_object_unref(t->active);
4101 t->active = NULL;
4102 if (t->active_text) {
4103 g_free(t->active_text);
4104 t->active_text = NULL;
4106 #endif
4108 /* take focus if we are visible */
4109 focus_webview(t);
4110 t->focus_wv = 1;
4112 marks_clear(t);
4113 break;
4115 case WEBKIT_LOAD_COMMITTED:
4116 /* 1 */
4117 uri = get_uri(t);
4118 if (uri == NULL)
4119 return;
4120 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), uri);
4122 if (t->status) {
4123 g_free(t->status);
4124 t->status = NULL;
4126 set_status(t, "Loading: %s", (char *)uri);
4128 /* clear t->item, except if we're switching to an about: page */
4129 if (t->item && !g_str_has_prefix(uri, "xxxt://") &&
4130 !g_str_has_prefix(uri, "about:")) {
4131 g_object_unref(t->item);
4132 t->item = NULL;
4135 /* check if js white listing is enabled */
4136 if (enable_plugin_whitelist)
4137 check_and_set_pl(uri, t);
4138 if (enable_cookie_whitelist)
4139 check_and_set_cookie(uri, t);
4140 if (enable_js_whitelist)
4141 check_and_set_js(uri, t);
4143 if (t->styled)
4144 apply_style(t);
4147 /* we know enough to autosave the session */
4148 if (session_autosave) {
4149 a.s = NULL;
4150 save_tabs(t, &a);
4153 show_ca_status(t, uri);
4154 run_script(t, JS_HINTING);
4155 if (enable_autoscroll)
4156 run_script(t, JS_AUTOSCROLL);
4157 break;
4159 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
4160 /* 3 */
4161 if (color_visited_uris) {
4162 color_visited(t, color_visited_helper());
4165 * This colors the links you middle-click (open in new
4166 * tab) in the current tab.
4168 if (t->tab_id != gtk_notebook_get_current_page(notebook) &&
4169 (uri = get_uri(t)) != NULL)
4170 color_visited(get_current_tab(),
4171 g_strdup_printf("{'%s' : 'dummy'}", uri));
4173 break;
4175 case WEBKIT_LOAD_FINISHED:
4176 /* 2 */
4177 if ((uri = get_uri(t)) == NULL)
4178 return;
4180 * js_autorun calls get_uri which frees t->tmp_uri if on an
4181 * "about:" page. On "about:" pages, uri points to t->tmp_uri.
4182 * I.e. we will use freed memory. Prevent that.
4184 tmp_uri = g_strdup(uri);
4186 /* autorun some js if enabled */
4187 js_autorun(t);
4189 input_autofocus(t);
4191 if (!strncmp(tmp_uri, "http://", strlen("http://")) ||
4192 !strncmp(tmp_uri, "https://", strlen("https://")) ||
4193 !strncmp(tmp_uri, "file://", strlen("file://"))) {
4194 find.uri = (gchar *)tmp_uri;
4195 h = RB_FIND(history_list, &hl, &find);
4196 if (!h)
4197 insert_history_item(tmp_uri,
4198 get_title(t, FALSE), time(NULL));
4199 else
4200 h->time = time(NULL);
4203 if (statusbar_style == XT_STATUSBAR_URL)
4204 set_status(t, "%s", (char *)tmp_uri);
4205 else
4206 set_status(t, "%s", (char *)get_title(t, FALSE));
4207 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4208 #if GTK_CHECK_VERSION(2, 20, 0)
4209 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4210 gtk_widget_hide(t->spinner);
4211 #endif
4212 g_free(tmp_uri);
4213 break;
4215 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4216 case WEBKIT_LOAD_FAILED:
4217 /* 4 */
4218 if (!t->download_requested) {
4219 gtk_label_set_text(GTK_LABEL(t->label),
4220 get_title(t, FALSE));
4221 gtk_label_set_text(GTK_LABEL(t->tab_elems.label),
4222 get_title(t, FALSE));
4223 set_status(t, "%s", (char *)get_title(t, FALSE));
4224 gtk_window_set_title(GTK_WINDOW(main_window),
4225 get_title(t, TRUE));
4226 } else {
4229 #endif
4230 default:
4231 #if GTK_CHECK_VERSION(2, 20, 0)
4232 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4233 gtk_widget_hide(t->spinner);
4234 #endif
4235 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4236 break;
4239 gtk_widget_set_sensitive(GTK_WIDGET(t->backward),
4240 can_go_back_for_real(t));
4242 gtk_widget_set_sensitive(GTK_WIDGET(t->forward),
4243 can_go_forward_for_real(t));
4246 void
4247 notify_title_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4249 const gchar *title = NULL, *win_title = NULL;
4251 title = get_title(t, FALSE);
4252 win_title = get_title(t, TRUE);
4253 if (title) {
4254 gtk_label_set_text(GTK_LABEL(t->label), title);
4255 gtk_label_set_text(GTK_LABEL(t->tab_elems.label), title);
4258 if (win_title && t->tab_id == gtk_notebook_get_current_page(notebook))
4259 gtk_window_set_title(GTK_WINDOW(main_window), win_title);
4262 char *
4263 get_domain(const gchar *host)
4265 size_t x;
4266 char *p;
4268 /* handle silly domains like .co.uk */
4270 if ((x = strlen(host)) <= 6)
4271 return (g_strdup(host));
4273 if (host[x - 3] == '.' && host[x - 6] == '.') {
4274 x = x - 7;
4275 while (x > 0)
4276 if (host[x] != '.')
4277 x--;
4278 else
4279 return (g_strdup(&host[x + 1]));
4282 /* find first . */
4283 p = g_strrstr(host, ".");
4284 if (p == NULL)
4285 return (g_strdup(""));
4286 p--;
4287 while (p > host)
4288 if (*p != '.')
4289 p--;
4290 else
4291 return (g_strdup(p + 1));
4293 return (g_strdup(host));
4296 void
4297 js_autorun(struct tab *t)
4299 SoupURI *su = NULL;
4300 const gchar *uri;
4301 size_t got_default = 0, got_host = 0;
4302 struct stat sb;
4303 char deff[PATH_MAX], hostf[PATH_MAX];
4304 char *js = NULL, *jsat, *domain = NULL;
4305 FILE *deffile = NULL, *hostfile = NULL;
4307 if (enable_js_autorun == 0)
4308 return;
4310 uri = get_uri(t);
4311 if (uri &&
4312 !(g_str_has_prefix(uri, "http://") ||
4313 g_str_has_prefix(uri, "https://")))
4314 goto done;
4316 su = soup_uri_new(uri);
4317 if (su == NULL)
4318 goto done;
4319 if (!SOUP_URI_VALID_FOR_HTTP(su))
4320 goto done;
4322 DNPRINTF(XT_D_JS, "%s: host: %s domain: %s\n", __func__,
4323 su->host, domain);
4324 domain = get_domain(su->host);
4326 snprintf(deff, sizeof deff, "%s" PS "default.js", js_dir);
4327 if ((deffile = fopen(deff, "r")) != NULL) {
4328 if (fstat(fileno(deffile), &sb) == -1) {
4329 show_oops(t, "can't stat default JS file");
4330 goto done;
4332 got_default = sb.st_size;
4335 /* try host first followed by domain */
4336 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, su->host);
4337 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4338 if ((hostfile = fopen(hostf, "r")) == NULL) {
4339 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, domain);
4340 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4341 if ((hostfile = fopen(hostf, "r")) == NULL)
4342 goto nofile;
4344 DNPRINTF(XT_D_JS, "file: %s\n", hostf);
4345 if (fstat(fileno(hostfile), &sb) == -1) {
4346 show_oops(t, "can't stat %s JS file", hostf);
4347 goto done;
4349 got_host = sb.st_size;
4351 nofile:
4352 if (got_default + got_host == 0)
4353 goto done;
4355 js = g_malloc0(got_default + got_host + 1);
4356 jsat = js;
4358 if (got_default) {
4359 if (fread(js, got_default, 1, deffile) != 1) {
4360 show_oops(t, "default file read error");
4361 goto done;
4363 jsat = js + got_default;
4366 if (got_host) {
4367 if (fread(jsat, got_host, 1, hostfile) != 1) {
4368 show_oops(t, "host file read error");
4369 goto done;
4373 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
4374 run_script(t, js);
4376 done:
4377 if (su)
4378 soup_uri_free(su);
4379 if (js)
4380 g_free(js);
4381 if (deffile)
4382 fclose(deffile);
4383 if (hostfile)
4384 fclose(hostfile);
4385 if (domain)
4386 g_free(domain);
4389 void
4390 webview_progress_changed_cb(WebKitWebView *wv, GParamSpec *pspec, struct tab *t)
4392 gdouble progress;
4394 progress = webkit_web_view_get_progress(wv);
4395 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
4396 progress == 1.0 ? 0 : progress);
4397 gtk_entry_set_progress_fraction(GTK_ENTRY(t->uri_entry),
4398 progress == 1.0 ? 0 : progress);
4400 update_statusbar_position(NULL, NULL);
4404 strict_transport_rb_cmp(struct strict_transport *a, struct strict_transport *b)
4406 char *p1, *p2;
4407 int l1, l2;
4409 /* compare strings from the end */
4410 l1 = strlen(a->host);
4411 l2 = strlen(b->host);
4413 p1 = a->host + l1;
4414 p2 = b->host + l2;
4415 for (; *p1 == *p2 && p1 > a->host && p2 > b->host;
4416 p1--, p2--)
4420 * Check if we need to do pattern expansion,
4421 * or if we're just keeping the tree in order
4423 if (a->flags & XT_STS_FLAGS_EXPAND &&
4424 b->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) {
4425 /* Check if we're matching the
4426 * 'host.xyz' part in '*.host.xyz'
4428 if (p2 == b->host && (p1 == a->host || *(p1-1) == '.')) {
4429 return (0);
4433 if (p1 == a->host && p2 == b->host)
4434 return (0);
4435 if (p1 == a->host)
4436 return (1);
4437 if (p2 == b->host)
4438 return (-1);
4440 if (*p1 < *p2)
4441 return (-1);
4442 if (*p1 > *p2)
4443 return (1);
4445 return (0);
4447 RB_GENERATE(strict_transport_tree, strict_transport, entry,
4448 strict_transport_rb_cmp);
4451 strict_transport_add(const char *domain, time_t timeout, int subdomains)
4453 struct strict_transport *d, find;
4454 time_t now;
4455 FILE *f;
4457 if (enable_strict_transport == FALSE)
4458 return (0);
4460 DPRINTF("strict_transport_add(%s,%" PRIi64 ",%d)\n", domain,
4461 (uint64_t)timeout, subdomains);
4463 now = time(NULL);
4464 if (timeout < now)
4465 return (0);
4467 find.host = (char *)domain;
4468 find.flags = 0;
4469 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4471 /* update flags */
4472 if (d) {
4473 /* check if update is needed */
4474 if (d->timeout == timeout &&
4475 (d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) == subdomains)
4476 return (0);
4478 d->timeout = timeout;
4479 if (subdomains)
4480 d->flags |= XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4482 /* We're still initializing */
4483 if (strict_transport_file == NULL)
4484 return (0);
4486 if ((f = fopen(strict_transport_file, "w")) == NULL) {
4487 show_oops(NULL,
4488 "can't open strict-transport rules file");
4489 return (1);
4492 fprintf(f, "# Generated file - do not update unless you know "
4493 "what you're doing\n");
4494 RB_FOREACH(d, strict_transport_tree, &st_tree) {
4495 if (d->timeout < now)
4496 continue;
4497 fprintf(f, "%s\t%" PRIi64 "\t%d\n", d->host,
4498 (uint64_t)d->timeout,
4499 d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS);
4501 fclose(f);
4502 } else {
4503 d = g_malloc(sizeof *d);
4504 d->host= g_strdup(domain);
4505 d->timeout = timeout;
4506 if (subdomains)
4507 d->flags = XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4508 else
4509 d->flags = 0;
4510 RB_INSERT(strict_transport_tree, &st_tree, d);
4512 /* We're still initializing */
4513 if (strict_transport_file == NULL)
4514 return (0);
4516 if ((f = fopen(strict_transport_file, "a+")) == NULL) {
4517 show_oops(NULL,
4518 "can't open strict-transport rules file");
4519 return (1);
4522 fseek(f, 0, SEEK_END);
4523 fprintf(f,"%s\t%" PRIi64 "\t%d\n", d->host, (uint64_t)timeout,
4524 subdomains);
4525 fclose(f);
4527 return (0);
4531 strict_transport_check(const char *host)
4533 static struct strict_transport *d = NULL;
4534 struct strict_transport find;
4536 if (enable_strict_transport == FALSE)
4537 return (0);
4539 find.host = (char *)host;
4541 /* match for domains that include subdomains */
4542 find.flags = XT_STS_FLAGS_EXPAND;
4544 /* First, check if we're already at the right node */
4545 if (d != NULL && strict_transport_rb_cmp(&find, d) == 0) {
4546 return (1);
4549 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4550 if (d != NULL)
4551 return (1);
4553 return (0);
4557 strict_transport_init()
4559 char file[PATH_MAX];
4560 char delim[3];
4561 char *rule;
4562 char *ptr;
4563 FILE *f;
4564 size_t len;
4565 time_t timeout, now;
4566 int subdomains;
4568 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_STS_FILE);
4569 if ((f = fopen(file, "r")) == NULL) {
4570 strict_transport_file = g_strdup(file);
4571 return (0);
4574 delim[0] = '\\';
4575 delim[1] = '\\';
4576 delim[2] = '#';
4577 rule = NULL;
4578 now = time(NULL);
4580 for (;;) {
4581 if ((rule = fparseln(f, &len, NULL, delim, 0)) == NULL) {
4582 if (!feof(f) || ferror(f))
4583 goto corrupt_file;
4584 else
4585 break;
4588 /* get second entry */
4589 if ((ptr = strpbrk(rule, " \t")) == NULL)
4590 goto corrupt_file;
4592 *ptr++ = '\0';
4593 timeout = atoi(ptr);
4595 /* get third entry */
4596 if ((ptr = strpbrk(ptr, " \t")) == NULL)
4597 goto corrupt_file;
4599 ptr ++;
4600 subdomains = atoi(ptr);
4602 if (timeout > now)
4603 strict_transport_add(rule, timeout, subdomains);
4604 free(rule);
4607 fclose(f);
4608 strict_transport_file = g_strdup(file);
4609 return (0);
4611 corrupt_file:
4612 startpage_add("strict-transport rules file ('%s') is corrupt", file);
4613 if (rule)
4614 free(rule);
4615 fclose(f);
4616 return (1);
4620 force_https_check(const char *uri)
4622 struct wl_entry *w = NULL;
4624 if (uri == NULL)
4625 return (0);
4627 if ((w = wl_find_uri(uri, &force_https)) == NULL)
4628 return (0);
4629 else
4630 return (1);
4633 void
4634 strict_transport_security_cb(SoupMessage *msg, gpointer data)
4636 SoupURI *uri;
4637 const char *sts;
4638 char *ptr;
4639 time_t timeout = 0;
4640 int subdomains = FALSE;
4642 if (msg == NULL)
4643 return;
4645 sts = soup_message_headers_get_one(msg->response_headers,
4646 "Strict-Transport-Security");
4647 uri = soup_message_get_uri(msg);
4649 if (sts == NULL || uri == NULL)
4650 return;
4652 if ((ptr = strcasestr(sts, "max-age="))) {
4653 ptr += strlen("max-age=");
4654 timeout = atoll(ptr);
4655 } else
4656 return; /* malformed header - max-age must be included */
4658 if ((ptr = strcasestr(sts, "includeSubDomains")))
4659 subdomains = TRUE;
4661 strict_transport_add(uri->host, timeout + time(NULL), subdomains);
4664 void
4665 session_rq_cb(SoupSession *s, SoupMessage *msg, SoupSocket *socket,
4666 gpointer data)
4668 SoupURI *dest;
4669 SoupURI *ref_uri;
4670 const char *ref;
4672 char *ref_suffix;
4673 char *dest_suffix;
4675 if (s == NULL || msg == NULL)
4676 return;
4678 if (enable_strict_transport) {
4679 soup_message_add_header_handler(msg, "finished",
4680 "Strict-Transport-Security",
4681 G_CALLBACK(strict_transport_security_cb), NULL);
4684 if (referer_mode == XT_REFERER_ALWAYS)
4685 return;
4687 /* Check if referer is set - and what the user requested for referers */
4688 ref = soup_message_headers_get_one(msg->request_headers, "Referer");
4689 if (ref) {
4690 DNPRINTF(XT_D_NAV, "session_rq_cb: Referer: %s\n", ref);
4691 switch (referer_mode) {
4692 case XT_REFERER_NEVER:
4693 DNPRINTF(XT_D_NAV, "session_rq_cb: removing referer\n");
4694 soup_message_headers_remove(msg->request_headers,
4695 "Referer");
4696 break;
4697 case XT_REFERER_SAME_DOMAIN:
4698 ref_uri = soup_uri_new(ref);
4699 dest = soup_message_get_uri(msg);
4701 ref_suffix = tld_get_suffix(ref_uri->host);
4702 dest_suffix = tld_get_suffix(dest->host);
4704 if (dest && ref_suffix && dest_suffix &&
4705 strcmp(ref_suffix, dest_suffix) != 0) {
4706 soup_message_headers_remove(msg->request_headers,
4707 "Referer");
4708 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4709 "referer (not same domain) (suffixes: %s - %s)\n",
4710 ref_suffix, dest_suffix);
4712 soup_uri_free(ref_uri);
4713 break;
4714 case XT_REFERER_SAME_FQDN:
4715 ref_uri = soup_uri_new(ref);
4716 dest = soup_message_get_uri(msg);
4717 if (dest && strcmp(ref_uri->host, dest->host) != 0) {
4718 soup_message_headers_remove(msg->request_headers,
4719 "Referer");
4720 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4721 "referer (not same fqdn) (should be %s)\n",
4722 dest->host);
4724 soup_uri_free(ref_uri);
4725 break;
4726 case XT_REFERER_CUSTOM:
4727 DNPRINTF(XT_D_NAV, "session_rq_cb: setting referer "
4728 "to %s\n", referer_custom);
4729 soup_message_headers_replace(msg->request_headers,
4730 "Referer", referer_custom);
4731 break;
4737 webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf,
4738 WebKitNetworkRequest *request, WebKitWebNavigationAction *na,
4739 WebKitWebPolicyDecision *pd, struct tab *t)
4741 WebKitWebNavigationReason reason;
4742 char *uri;
4744 if (t == NULL) {
4745 show_oops(NULL, "webview_npd_cb invalid parameters");
4746 return (FALSE);
4749 DNPRINTF(XT_D_NAV, "webview_npd_cb: ctrl_click %d %s\n",
4750 t->ctrl_click,
4751 webkit_network_request_get_uri(request));
4753 uri = (char *)webkit_network_request_get_uri(request);
4755 if (!auto_load_images && t->load_images) {
4757 /* Disable autoloading of images, now that we're done loading
4758 * them. */
4759 g_object_set(G_OBJECT(t->settings),
4760 "auto-load-images", FALSE, (char *)NULL);
4761 webkit_web_view_set_settings(t->wv, t->settings);
4763 t->load_images = FALSE;
4766 /* If this is an xtp url, we don't load anything else. */
4767 if (parse_xtp_url(t, uri)) {
4768 webkit_web_policy_decision_ignore(pd);
4769 return (TRUE);
4772 if (parse_custom_uri(t, uri)) {
4773 webkit_web_policy_decision_ignore(pd);
4774 return (TRUE);
4777 if (valid_url_type(uri)) {
4778 show_oops(t, "Stopping attempt to load an invalid URI (possible"
4779 " bait and switch attack)");
4780 webkit_web_policy_decision_ignore(pd);
4781 return (TRUE);
4784 if ((t->mode == XT_MODE_HINT && t->new_tab) || t->ctrl_click) {
4785 t->ctrl_click = 0;
4786 create_new_tab(uri, NULL, ctrl_click_focus, -1);
4787 webkit_web_policy_decision_ignore(pd);
4788 return (TRUE); /* we made the decission */
4792 * This is a little hairy but it comes down to this:
4793 * when we run in whitelist mode we have to assist the browser in
4794 * opening the URL that it would have opened in a new tab.
4796 reason = webkit_web_navigation_action_get_reason(na);
4797 if (reason == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
4798 set_normal_tab_meaning(t);
4799 if (enable_scripts == 0 && enable_cookie_whitelist == 1)
4800 load_uri(t, uri);
4801 webkit_web_policy_decision_use(pd);
4802 return (TRUE); /* we made the decision */
4805 return (FALSE);
4808 void
4809 webview_rrs_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitWebResource *res,
4810 WebKitNetworkRequest *request, WebKitNetworkResponse *response,
4811 struct tab *t)
4813 SoupMessage *msg = NULL;
4814 SoupURI *uri = NULL;
4815 struct http_accept ha_find, *ha = NULL;
4816 struct user_agent ua_find, *ua = NULL;
4817 struct domain_id di_find, *di = NULL;
4818 char *uri_s = NULL;
4820 msg = webkit_network_request_get_message(request);
4821 if (!msg) return;
4823 uri = soup_message_get_uri(msg);
4824 if (!uri)
4825 return;
4826 uri_s = soup_uri_to_string(uri, FALSE);
4828 if (strcmp(uri->scheme, SOUP_URI_SCHEME_HTTP) == 0) {
4829 if (strict_transport_check(uri->host) ||
4830 force_https_check(uri_s)) {
4831 DNPRINTF(XT_D_NAV, "webview_rrs_cb: force https for %s\n",
4832 uri->host);
4833 soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTPS);
4837 if (do_not_track)
4838 soup_message_headers_append(msg->request_headers, "DNT", "1");
4841 * Check if resources on this domain have been loaded before. If
4842 * not, add the current tab's http-accept and user-agent id's to a
4843 * new domain_id and insert into the RB tree. Use these http headers
4844 * for all resources loaded from this domain for the lifetime of the
4845 * browser.
4847 if ((di_find.domain = uri->host) == NULL)
4848 goto done;
4849 if ((di = RB_FIND(domain_id_list, &di_list, &di_find)) == NULL) {
4850 di = g_malloc(sizeof *di);
4851 di->domain = g_strdup(uri->host);
4852 di->ua_id = t->user_agent_id++;
4853 di->ha_id = t->http_accept_id++;
4854 RB_INSERT(domain_id_list, &di_list, di);
4856 ua_find.id = t->user_agent_id;
4857 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4858 if (ua == NULL)
4859 t->user_agent_id = 0;
4861 ha_find.id = t->http_accept_id;
4862 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4863 if (ha == NULL)
4864 t->http_accept_id = 0;
4867 ua_find.id = di->ua_id;
4868 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4869 ha_find.id = di->ha_id;
4870 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4872 if (ua != NULL)
4873 soup_message_headers_replace(msg->request_headers,
4874 "User-Agent", ua->value);
4875 if (ha != NULL)
4876 soup_message_headers_replace(msg->request_headers,
4877 "Accept", ha->value);
4879 done:
4880 if (uri_s)
4881 g_free(uri_s);
4884 WebKitWebView *
4885 webview_cwv_cb(WebKitWebView *wv, WebKitWebFrame *wf, struct tab *t)
4887 struct tab *tt;
4888 struct wl_entry *w = NULL;
4889 const gchar *uri;
4890 WebKitWebView *webview = NULL;
4891 int x = 1;
4893 DNPRINTF(XT_D_NAV, "webview_cwv_cb: %s\n",
4894 webkit_web_view_get_uri(wv));
4896 if (tabless) {
4897 /* open in current tab */
4898 webview = t->wv;
4899 } else if (enable_scripts == 0 && enable_js_whitelist == 1) {
4900 uri = webkit_web_view_get_uri(wv);
4901 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
4902 return (NULL);
4904 if (t->ctrl_click) {
4905 x = ctrl_click_focus;
4906 t->ctrl_click = 0;
4908 tt = create_new_tab(NULL, NULL, x, -1);
4909 webview = tt->wv;
4910 } else if (enable_scripts == 1) {
4911 if (t->ctrl_click) {
4912 x = ctrl_click_focus;
4913 t->ctrl_click = 0;
4915 tt = create_new_tab(NULL, NULL, x, -1);
4916 webview = tt->wv;
4919 return (webview);
4922 gboolean
4923 webview_closewv_cb(WebKitWebView *wv, struct tab *t)
4925 const gchar *uri;
4926 struct wl_entry *w = NULL;
4928 DNPRINTF(XT_D_NAV, "webview_close_cb: %d\n", t->tab_id);
4930 if (enable_scripts == 0 && enable_cookie_whitelist == 1) {
4931 uri = webkit_web_view_get_uri(wv);
4932 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
4933 return (FALSE);
4935 delete_tab(t);
4936 } else if (enable_scripts == 1)
4937 delete_tab(t);
4939 return (TRUE);
4943 webview_event_cb(GtkWidget *w, GdkEventButton *e, struct tab *t)
4945 /* we can not eat the event without throwing gtk off so defer it */
4947 /* catch middle click */
4948 if (e->type == GDK_BUTTON_RELEASE && e->button == 2) {
4949 t->ctrl_click = 1;
4950 goto done;
4953 /* catch ctrl click */
4954 if (e->type == GDK_BUTTON_RELEASE &&
4955 CLEAN(e->state) == GDK_CONTROL_MASK)
4956 t->ctrl_click = 1;
4957 else
4958 t->ctrl_click = 0;
4959 done:
4960 return (XT_CB_PASSTHROUGH);
4964 run_mimehandler(struct tab *t, char *mime_type, WebKitNetworkRequest *request)
4966 struct mime_type *m;
4967 char *sv[3];
4968 GError *gerr = NULL;
4970 m = find_mime_type(mime_type);
4971 if (m == NULL)
4972 return (1);
4973 if (m->mt_download)
4974 return (1);
4976 sv[0] = m->mt_action;
4977 sv[1] = (char *)webkit_network_request_get_uri(request);
4978 sv[2] = NULL;
4980 /* ignore donothing from example config */
4981 if (m->mt_action && !strcmp(m->mt_action, "donothing"))
4982 return (0);
4984 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
4985 NULL, &gerr))
4986 show_oops(t, "%s: could not spawn process (%s)", __func__,
4987 gerr ? gerr->message : "N/A");
4988 return (0);
4991 char *
4992 get_mime_type(const char *uri)
4994 GFileInfo *fi;
4995 GFile *gf;
4996 const gchar *m;
4997 char *mime_type = NULL;
4998 char *file;
5000 if (uri == NULL) {
5001 show_oops(NULL, "%s: invalid parameters", __func__);
5002 return (NULL);
5005 if (g_str_has_prefix(uri, "file://"))
5006 file = g_filename_from_uri(uri, NULL, NULL);
5007 else
5008 file = g_strdup(uri);
5010 if (file == NULL)
5011 return (NULL);
5013 gf = g_file_new_for_path(file);
5014 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
5015 NULL, NULL);
5016 if ((m = g_file_info_get_content_type(fi)) != NULL)
5017 mime_type = g_strdup(m);
5018 g_object_unref(fi);
5019 g_object_unref(gf);
5020 g_free(file);
5022 return (mime_type);
5026 run_download_mimehandler(char *mime_type, char *file)
5028 struct mime_type *m;
5029 char *sv[3];
5031 m = find_mime_type(mime_type);
5032 if (m == NULL)
5033 return (1);
5035 sv[0] = m->mt_action;
5036 sv[1] = file;
5037 sv[2] = NULL;
5038 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
5039 NULL, NULL)) {
5040 show_oops(NULL, "%s: could not spawn process: %s %s", __func__,
5041 sv[0], sv[1]);
5042 return (1);
5044 return (0);
5047 void
5048 download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
5049 WebKitWebView *wv)
5051 WebKitDownloadStatus status;
5052 const char *uri;
5053 char *file = NULL;
5054 char *mime = NULL;
5056 if (download == NULL)
5057 return;
5058 status = webkit_download_get_status(download);
5059 if (status != WEBKIT_DOWNLOAD_STATUS_FINISHED)
5060 return;
5062 if (download_notifications)
5063 show_oops(NULL, "Download of '%s' finished",
5064 basename((char *)webkit_download_get_destination_uri(download)));
5065 uri = webkit_download_get_destination_uri(download);
5066 if (uri == NULL)
5067 return;
5068 mime = get_mime_type(uri);
5069 if (mime == NULL)
5070 return;
5072 if (g_str_has_prefix(uri, "file://"))
5073 file = g_filename_from_uri(uri, NULL, NULL);
5074 else
5075 file = g_strdup(uri);
5077 if (file == NULL)
5078 goto done;
5080 run_download_mimehandler((char *)mime, file);
5082 done:
5083 g_free(mime);
5087 webview_mimetype_cb(WebKitWebView *wv, WebKitWebFrame *frame,
5088 WebKitNetworkRequest *request, char *mime_type,
5089 WebKitWebPolicyDecision *decision, struct tab *t)
5091 if (t == NULL) {
5092 show_oops(NULL, "webview_mimetype_cb invalid parameters");
5093 return (FALSE);
5096 DNPRINTF(XT_D_DOWNLOAD, "webview_mimetype_cb: tab %d mime %s\n",
5097 t->tab_id, mime_type);
5099 if (run_mimehandler(t, mime_type, request) == 0) {
5100 webkit_web_policy_decision_ignore(decision);
5101 focus_webview(t);
5102 return (TRUE);
5105 if (webkit_web_view_can_show_mime_type(wv, mime_type) == FALSE) {
5106 webkit_web_policy_decision_download(decision);
5107 return (TRUE);
5110 return (FALSE);
5114 download_start(struct tab *t, struct download *d, int flag)
5116 WebKitNetworkRequest *req;
5117 struct stat sb;
5118 const gchar *suggested_name;
5119 gchar *filename = NULL;
5120 char *uri = NULL;
5121 char *path = NULL;
5122 int ret = TRUE;
5123 int i;
5125 if (d == NULL || t == NULL) {
5126 show_oops(NULL, "%s invalid parameters", __func__);
5127 return (FALSE);
5130 suggested_name = webkit_download_get_suggested_filename(d->download);
5131 if (suggested_name == NULL)
5132 return (FALSE); /* abort download */
5134 i = 0;
5135 do {
5136 if (filename) {
5137 g_free(filename);
5138 filename = NULL;
5140 if (i) {
5141 g_free(uri);
5142 uri = NULL;
5143 filename = g_strdup_printf("%d%s", i, suggested_name);
5145 #ifdef __MINGW32__
5146 /* XXX using urls doesn't work properly in windows? */
5147 uri = g_strdup_printf("%s\\%s", download_dir, i ?
5148 filename : suggested_name);
5149 #else
5150 path = g_strdup_printf("%s" PS "%s", download_dir, i ?
5151 filename : suggested_name);
5152 if ((uri = g_filename_to_uri(path, NULL, NULL)) == NULL)
5153 break; /* XXX */
5154 #endif
5155 i++;
5156 #ifdef __MINGW32__
5157 } while (!stat(uri, &sb));
5158 #else
5159 } while (!stat(path, &sb));
5160 #endif
5162 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d filename %s "
5163 "local %s\n", __func__, t->tab_id, filename, uri);
5165 /* if we're restarting the download, or starting
5166 * it after doing something else, we need to recreate
5167 * the download request.
5169 if (flag == XT_DL_RESTART) {
5170 req = webkit_network_request_new(webkit_download_get_uri(d->download));
5171 webkit_download_cancel(d->download);
5172 g_object_unref(d->download);
5173 d->download = webkit_download_new(req);
5176 webkit_download_set_destination_uri(d->download, uri);
5178 if (webkit_download_get_status(d->download) ==
5179 WEBKIT_DOWNLOAD_STATUS_ERROR) {
5180 show_oops(t, "%s: download failed to start", __func__);
5181 ret = FALSE;
5182 show_oops(t, "Download Failed");
5183 } else {
5184 /* connect "download first" mime handler */
5185 g_signal_connect(G_OBJECT(d->download), "notify::status",
5186 G_CALLBACK(download_status_changed_cb), NULL);
5188 /* get from history */
5189 g_object_ref(d->download);
5190 show_oops(t, "Download of '%s' started...",
5191 basename((char *)webkit_download_get_destination_uri(d->download)));
5194 if (flag != XT_DL_START)
5195 webkit_download_start(d->download);
5197 DNPRINTF(XT_D_DOWNLOAD, "download status : %d",
5198 webkit_download_get_status(d->download));
5200 /* sync other download manager tabs */
5201 update_download_tabs(NULL);
5203 if (uri)
5204 g_free(uri);
5205 if (path)
5206 g_free(path);
5207 if (filename)
5208 g_free(filename);
5210 return (ret);
5214 download_ask_cb(struct tab *t, GdkEventKey *e, gpointer data)
5216 struct download *d = data;
5218 /* unset mode */
5219 t->mode_cb = NULL;
5220 t->mode_cb_data = NULL;
5222 if (!e || !d) {
5223 e->keyval = GDK_Escape;
5224 return (XT_CB_PASSTHROUGH);
5227 DPRINTF("download_ask_cb: User pressed %c\n", e->keyval);
5228 if (e->keyval == 'y' || e->keyval == 'Y' || e->keyval == GDK_Return)
5229 /* We need to do a RESTART, because we're not calling from
5230 * webview_download_cb
5232 download_start(t, d, XT_DL_RESTART);
5234 /* for all other keyvals, we just let the download be */
5235 e->keyval = GDK_Escape;
5236 return (XT_CB_HANDLED);
5240 download_ask(struct tab *t, struct download *d)
5242 const gchar *suggested_name;
5244 suggested_name = webkit_download_get_suggested_filename(d->download);
5245 if (suggested_name == NULL)
5246 return (FALSE); /* abort download */
5248 show_oops(t, "download file %s [y/n] ?", suggested_name);
5249 t->mode_cb = download_ask_cb;
5250 t->mode_cb_data = d;
5252 return (TRUE);
5256 webview_download_cb(WebKitWebView *wv, WebKitDownload *wk_download,
5257 struct tab *t)
5259 const gchar *suggested_name;
5260 struct download *download_entry;
5261 int ret = TRUE;
5263 if (wk_download == NULL || t == NULL) {
5264 show_oops(NULL, "%s invalid parameters", __func__);
5265 return (FALSE);
5268 suggested_name = webkit_download_get_suggested_filename(wk_download);
5269 if (suggested_name == NULL)
5270 return (FALSE); /* abort download */
5272 download_entry = g_malloc(sizeof(struct download));
5273 download_entry->download = wk_download;
5274 download_entry->tab = t;
5275 download_entry->id = next_download_id++;
5276 RB_INSERT(download_list, &downloads, download_entry);
5277 t->download_requested = 1;
5279 if (download_mode == XT_DM_START)
5280 ret = download_start(t, download_entry, XT_DL_START);
5281 else if (download_mode == XT_DM_ASK)
5282 ret = download_ask(t, download_entry);
5283 else if (download_mode == XT_DM_ADD)
5284 show_oops(t, "added %s to download manager",
5285 suggested_name);
5287 /* sync other download manager tabs */
5288 update_download_tabs(NULL);
5291 * NOTE: never redirect/render the current tab before this
5292 * function returns. This will cause the download to never start.
5294 return (ret); /* start download */
5297 void
5298 webview_hover_cb(WebKitWebView *wv, gchar *title, gchar *uri, struct tab *t)
5300 DNPRINTF(XT_D_KEY, "webview_hover_cb: %s %s\n", title, uri);
5302 if (t == NULL) {
5303 show_oops(NULL, "webview_hover_cb");
5304 return;
5307 if (uri)
5308 set_status(t, "Link: %s", uri);
5309 else {
5310 if (statusbar_style == XT_STATUSBAR_URL) {
5311 const gchar *page_uri;
5313 if ((page_uri = get_uri(t)) != NULL)
5314 set_status(t, "%s", page_uri);
5315 } else
5316 set_status(t, "%s", (char *)get_title(t, FALSE));
5321 mark(struct tab *t, struct karg *arg)
5323 char mark;
5324 int index;
5325 double pos;
5327 mark = arg->s[1];
5328 if ((index = marktoindex(mark)) == -1)
5329 return (-1);
5331 if (arg->i == XT_MARK_SET)
5332 t->mark[index] = gtk_adjustment_get_value(t->adjust_v);
5333 else if (arg->i == XT_MARK_GOTO) {
5334 if (t->mark[index] == XT_INVALID_MARK) {
5335 show_oops(t, "mark '%c' does not exist", mark);
5336 return (-1);
5338 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5339 something changes the document size */
5340 pos = gtk_adjustment_get_value(t->adjust_v);
5341 gtk_adjustment_set_value(t->adjust_v, t->mark[index]);
5342 t->mark[marktoindex('\'')] = pos;
5345 return (0);
5348 void
5349 marks_clear(struct tab *t)
5351 int i;
5353 for (i = 0; i < LENGTH(t->mark); i++)
5354 t->mark[i] = XT_INVALID_MARK;
5358 qmarks_load(void)
5360 char file[PATH_MAX];
5361 char *line = NULL, *p;
5362 int index, i;
5363 FILE *f;
5364 size_t linelen;
5366 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5367 if ((f = fopen(file, "r+")) == NULL) {
5368 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5369 return (1);
5372 for (i = 1; ; i++) {
5373 if ((line = fparseln(f, &linelen, NULL, NULL, 0)) == NULL)
5374 break;
5375 if (strlen(line) == 0 || line[0] == '#') {
5376 free(line);
5377 line = NULL;
5378 continue;
5381 p = strtok(line, " \t");
5383 if (p == NULL || strlen(p) != 1 ||
5384 (index = qmarktoindex(*p)) == -1) {
5385 warnx("corrupt quickmarks file, line %d", i);
5386 break;
5389 p = strtok(NULL, " \t");
5390 if (qmarks[index] != NULL)
5391 g_free(qmarks[index]);
5392 qmarks[index] = g_strdup(p);
5395 fclose(f);
5397 return (0);
5401 qmarks_save(void)
5403 char file[PATH_MAX];
5404 int i;
5405 FILE *f;
5407 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5408 if ((f = fopen(file, "r+")) == NULL) {
5409 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5410 return (1);
5413 for (i = 0; i < XT_NOQMARKS; i++)
5414 if (qmarks[i] != NULL)
5415 fprintf(f, "%c %s\n", indextoqmark(i), qmarks[i]);
5417 fclose(f);
5419 return (0);
5423 qmark(struct tab *t, struct karg *arg)
5425 char mark;
5426 int index;
5428 mark = arg->s[strlen(arg->s)-1];
5429 index = qmarktoindex(mark);
5430 if (index == -1)
5431 return (-1);
5433 switch (arg->i) {
5434 case XT_QMARK_SET:
5435 if (qmarks[index] != NULL) {
5436 g_free(qmarks[index]);
5437 qmarks[index] = NULL;
5440 qmarks_load(); /* sync if multiple instances */
5441 qmarks[index] = g_strdup(get_uri(t));
5442 qmarks_save();
5443 break;
5444 case XT_QMARK_OPEN:
5445 if (qmarks[index] != NULL)
5446 load_uri(t, qmarks[index]);
5447 else {
5448 show_oops(t, "quickmark \"%c\" does not exist",
5449 mark);
5450 return (-1);
5452 break;
5453 case XT_QMARK_TAB:
5454 if (qmarks[index] != NULL)
5455 create_new_tab(qmarks[index], NULL, 1, -1);
5456 else {
5457 show_oops(t, "quickmark \"%c\" does not exist",
5458 mark);
5459 return (-1);
5461 break;
5464 return (0);
5468 go_up(struct tab *t, struct karg *args)
5470 int levels;
5471 int lastidx;
5472 char *uri;
5473 char *tmp;
5474 char *p;
5476 if (args->i == XT_GO_UP_ROOT)
5477 levels = XT_GO_UP_ROOT;
5478 else if ((levels = atoi(args->s)) == 0)
5479 levels = 1;
5481 uri = g_strdup(get_uri(t));
5482 if (uri == NULL)
5483 return (1);
5485 if ((tmp = strstr(uri, XT_PROTO_DELIM)) == NULL)
5486 return (1);
5488 tmp += strlen(XT_PROTO_DELIM);
5490 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5491 lastidx = strlen(tmp) - 1;
5492 if (lastidx > 0) {
5493 if (tmp[lastidx] == '/')
5494 tmp[lastidx] = '\0';
5497 while (levels--) {
5498 p = strrchr(tmp, '/');
5499 if (p == tmp) { /* Are we at the root of a file://-path? */
5500 p[1] = '\0';
5501 break;
5502 } else if (p != NULL)
5503 *p = '\0';
5504 else
5505 break;
5508 load_uri(t, uri);
5509 g_free(uri);
5511 return (0);
5515 gototab(struct tab *t, struct karg *args)
5517 int tab;
5518 struct karg arg = {0, NULL, -1};
5520 tab = atoi(args->s);
5522 if (args->i == 0)
5523 arg.i = XT_TAB_NEXT;
5524 else
5525 arg.i = args->i;
5527 arg.precount = tab;
5529 movetab(t, &arg);
5531 return (0);
5535 zoom_amount(struct tab *t, struct karg *arg)
5537 struct karg narg = {0, NULL, -1};
5539 narg.i = atoi(arg->s);
5540 resizetab(t, &narg);
5542 return (0);
5546 flip_colon(struct tab *t, struct karg *arg)
5548 struct karg narg = {0, NULL, -1};
5549 char *p;
5551 if (t == NULL || arg == NULL)
5552 return (1);
5554 p = strstr(arg->s, ":");
5555 if (p == NULL)
5556 return (1);
5557 *p = '\0';
5559 narg.i = ':';
5560 narg.s = arg->s;
5561 command(t, &narg);
5563 return (0);
5566 /* buffer commands receive the regex that triggered them in arg.s */
5567 char bcmd[XT_BUFCMD_SZ];
5568 struct buffercmd {
5569 char *regex;
5570 int precount;
5571 #define XT_PRE_NO (0)
5572 #define XT_PRE_YES (1)
5573 #define XT_PRE_MAYBE (2)
5574 char *cmd;
5575 int (*func)(struct tab *, struct karg *);
5576 int arg;
5577 regex_t cregex;
5578 } buffercmds[] = {
5579 { "^[0-9]*gu$", XT_PRE_MAYBE, "gu", go_up, 0 },
5580 { "^gU$", XT_PRE_NO, "gU", go_up, XT_GO_UP_ROOT },
5581 { "^gg$", XT_PRE_NO, "gg", move, XT_MOVE_TOP },
5582 { "^gG$", XT_PRE_NO, "gG", move, XT_MOVE_BOTTOM },
5583 { "^[0-9]+%$", XT_PRE_YES, "%", move, XT_MOVE_PERCENT },
5584 { "^zz$", XT_PRE_NO, "zz", move, XT_MOVE_CENTER },
5585 { "^gh$", XT_PRE_NO, "gh", go_home, 0 },
5586 { "^m[a-zA-Z0-9]$", XT_PRE_NO, "m", mark, XT_MARK_SET },
5587 { "^['][a-zA-Z0-9']$", XT_PRE_NO, "'", mark, XT_MARK_GOTO },
5588 { "^[0-9]+t$", XT_PRE_YES, "t", gototab, 0 },
5589 { "^g0$", XT_PRE_YES, "g0", movetab, XT_TAB_FIRST },
5590 { "^g[$]$", XT_PRE_YES, "g$", movetab, XT_TAB_LAST },
5591 { "^[0-9]*gt$", XT_PRE_YES, "t", movetab, XT_TAB_NEXT },
5592 { "^[0-9]*gT$", XT_PRE_YES, "T", movetab, XT_TAB_PREV },
5593 { "^M[a-zA-Z0-9]$", XT_PRE_NO, "M", qmark, XT_QMARK_SET },
5594 { "^go[a-zA-Z0-9]$", XT_PRE_NO, "go", qmark, XT_QMARK_OPEN },
5595 { "^gn[a-zA-Z0-9]$", XT_PRE_NO, "gn", qmark, XT_QMARK_TAB },
5596 { "^ZR$", XT_PRE_NO, "ZR", restart, 0 },
5597 { "^ZZ$", XT_PRE_NO, "ZZ", quit, 0 },
5598 { "^zi$", XT_PRE_NO, "zi", resizetab, XT_ZOOM_IN },
5599 { "^zo$", XT_PRE_NO, "zo", resizetab, XT_ZOOM_OUT },
5600 { "^z0$", XT_PRE_NO, "z0", resizetab, XT_ZOOM_NORMAL },
5601 { "^[0-9]+Z$", XT_PRE_YES, "Z", zoom_amount, 0 },
5602 { "^[0-9]+:$", XT_PRE_YES, ":", flip_colon, 0 },
5605 void
5606 buffercmd_init(void)
5608 int i;
5610 for (i = 0; i < LENGTH(buffercmds); i++)
5611 if (regcomp(&buffercmds[i].cregex, buffercmds[i].regex,
5612 REG_EXTENDED | REG_NOSUB))
5613 startpage_add("invalid buffercmd regex %s",
5614 buffercmds[i].regex);
5617 void
5618 buffercmd_abort(struct tab *t)
5620 int i;
5622 if (t == NULL)
5623 return;
5625 DNPRINTF(XT_D_BUFFERCMD, "%s: clearing buffer\n", __func__);
5627 for (i = 0; i < LENGTH(bcmd); i++)
5628 bcmd[i] = '\0';
5630 cmd_prefix = 0; /* clear prefix for non-buffer commands */
5631 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5634 void
5635 buffercmd_execute(struct tab *t, struct buffercmd *cmd)
5637 struct karg arg = {0, NULL, -1};
5639 arg.i = cmd->arg;
5640 arg.s = g_strdup(bcmd);
5642 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_execute: buffer \"%s\" "
5643 "matches regex \"%s\", executing\n", bcmd, cmd->regex);
5644 cmd->func(t, &arg);
5646 if (arg.s)
5647 g_free(arg.s);
5649 buffercmd_abort(t);
5652 gboolean
5653 buffercmd_addkey(struct tab *t, guint keyval)
5655 int i, c, match ;
5656 char s[XT_BUFCMD_SZ];
5658 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers))) {
5659 buffercmd_abort(t);
5660 return (XT_CB_PASSTHROUGH);
5663 if (keyval == GDK_Escape) {
5664 buffercmd_abort(t);
5665 return (XT_CB_HANDLED);
5668 /* key with modifier or non-ascii character */
5669 if (!isascii(keyval)) {
5671 * XXX this looks wrong but fixes some sites like
5672 * http://www.seslisozluk.com/
5673 * that eat a shift or ctrl and end putting default focus in js
5674 * instead of ignoring the keystroke
5675 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5677 return (XT_CB_HANDLED);
5680 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: adding key \"%c\" "
5681 "to buffer \"%s\"\n", keyval, bcmd);
5683 for (i = 0; i < LENGTH(bcmd); i++)
5684 if (bcmd[i] == '\0') {
5685 bcmd[i] = keyval;
5686 break;
5689 /* buffer full, ignore input */
5690 if (i >= LENGTH(bcmd) -1) {
5691 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: buffer full\n");
5692 buffercmd_abort(t);
5693 return (XT_CB_HANDLED);
5696 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5698 /* find exact match */
5699 for (i = 0; i < LENGTH(buffercmds); i++)
5700 if (regexec(&buffercmds[i].cregex, bcmd,
5701 (size_t) 0, NULL, 0) == 0) {
5702 buffercmd_execute(t, &buffercmds[i]);
5703 goto done;
5706 /* find non exact matches to see if we need to abort ot not */
5707 for (i = 0, match = 0; i < LENGTH(buffercmds); i++) {
5708 DNPRINTF(XT_D_BUFFERCMD, "trying: %s\n", bcmd);
5709 c = -1;
5710 s[0] = '\0';
5711 if (buffercmds[i].precount == XT_PRE_MAYBE) {
5712 if (isdigit(bcmd[0])) {
5713 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5714 continue;
5715 } else {
5716 c = 0;
5717 if (sscanf(bcmd, "%s", s) == 0)
5718 continue;
5720 } else if (buffercmds[i].precount == XT_PRE_YES) {
5721 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5722 continue;
5723 } else {
5724 if (sscanf(bcmd, "%s", s) == 0)
5725 continue;
5727 if (c == -1 && buffercmds[i].precount)
5728 continue;
5729 if (!strncmp(s, buffercmds[i].cmd, strlen(s)))
5730 match++;
5732 DNPRINTF(XT_D_BUFFERCMD, "got[%d] %d <%s>: %d %s\n",
5733 i, match, buffercmds[i].cmd, c, s);
5735 if (match == 0) {
5736 DNPRINTF(XT_D_BUFFERCMD, "aborting: %s\n", bcmd);
5737 buffercmd_abort(t);
5740 done:
5741 return (XT_CB_HANDLED);
5745 * XXX we were seeing a bunch of focus issues with the toplevel
5746 * main_window losing its is-active and has-toplevel-focus properties.
5747 * This is the most correct and portable solution we could come up with
5748 * without relying on calling internal GTK functions (which we
5749 * couldn't link to in Linux).
5751 #if GTK_CHECK_VERSION(3, 0, 0)
5752 void
5753 fake_focus_in(GtkWidget *w)
5755 if (fevent == NULL) {
5756 fevent = gdk_event_new(GDK_FOCUS_CHANGE);
5757 fevent->focus_change.window =
5758 gtk_widget_get_window(main_window);
5759 fevent->focus_change.type = GDK_FOCUS_CHANGE;
5760 fevent->focus_change.in = TRUE;
5762 gtk_widget_send_focus_change(main_window, fevent);
5764 #endif
5766 gboolean
5767 handle_keypress(struct tab *t, GdkEventKey *e, int entry)
5769 struct karg args;
5770 struct key_binding *k;
5773 * This sometimes gets randomly unset for whatever reason in GTK3.
5774 * If we're handling a keypress, the main window's is-active propery
5775 * *must* be true, or else many things will break.
5777 #if GTK_CHECK_VERSION(3, 0, 0)
5778 fake_focus_in(main_window);
5779 #endif
5781 /* handle keybindings if buffercmd is empty.
5782 if not empty, allow commands like C-n */
5783 if (bcmd[0] == '\0' || ((e->state & (CTRL | MOD1)) != 0))
5784 TAILQ_FOREACH(k, &kbl, entry)
5785 if (e->keyval == k->key
5786 && (entry ? k->use_in_entry : 1)) {
5787 /* when we are edditing eat ctrl/mod keys */
5788 if (edit_mode == XT_EM_VI &&
5789 t->mode == XT_MODE_INSERT &&
5790 (e->state & CTRL || e->state & MOD1))
5791 return (XT_CB_PASSTHROUGH);
5793 if (k->mask == 0) {
5794 if ((e->state & (CTRL | MOD1)) == 0)
5795 goto runcmd;
5796 } else if ((e->state & k->mask) == k->mask) {
5797 goto runcmd;
5801 if (!entry && ((e->state & (CTRL | MOD1)) == 0))
5802 return (buffercmd_addkey(t, e->keyval));
5804 return (XT_CB_PASSTHROUGH);
5806 runcmd:
5807 if (k->cmd[0] == ':') {
5808 args.i = ':';
5809 args.s = &k->cmd[1];
5810 return (command(t, &args));
5811 } else
5812 return (cmd_execute(t, k->cmd));
5816 wv_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5818 char s[2];
5820 /* don't use w directly; use t->whatever instead */
5822 if (t == NULL) {
5823 show_oops(NULL, "wv_keypress_cb");
5824 return (XT_CB_PASSTHROUGH);
5827 hide_oops(t);
5829 if (t->mode_cb)
5830 return (t->mode_cb(t, e, t->mode_cb_data));
5832 DNPRINTF(XT_D_KEY, "wv_keypress_cb: mode %d keyval 0x%x mask "
5833 "0x%x tab %d\n", t->mode, e->keyval, e->state, t->tab_id);
5835 /* Hide buffers, if they are visible, with escape. */
5836 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)) &&
5837 CLEAN(e->state) == 0 && e->keyval == GDK_Escape) {
5838 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
5839 hide_buffers(t);
5840 return (XT_CB_HANDLED);
5843 if ((CLEAN(e->state) == 0 && e->keyval == GDK_Tab) ||
5844 (CLEAN(e->state) == SHFT && e->keyval == GDK_Tab))
5845 /* something focussy is about to happen */
5846 return (XT_CB_PASSTHROUGH);
5848 /* check if we are some sort of text input thing in the dom */
5849 input_check_mode(t);
5851 if (t->mode == XT_MODE_PASSTHROUGH) {
5852 if (CLEAN(e->state) == 0 && e->keyval == GDK_Escape)
5853 t->mode = XT_MODE_COMMAND;
5854 return (XT_CB_PASSTHROUGH);
5855 } else if (t->mode == XT_MODE_COMMAND || t->mode == XT_MODE_HINT) {
5856 /* prefix input */
5857 snprintf(s, sizeof s, "%c", e->keyval);
5858 if (CLEAN(e->state) == 0 && isdigit(s[0]))
5859 cmd_prefix = 10 * cmd_prefix + atoi(s);
5860 return (handle_keypress(t, e, 0));
5861 } else {
5862 /* insert mode */
5863 return (handle_keypress(t, e, 1));
5866 /* NOTREACHED */
5867 return (XT_CB_PASSTHROUGH);
5870 gboolean
5871 hint_continue(struct tab *t)
5873 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5874 char *s;
5875 const gchar *errstr = NULL;
5876 gboolean rv = TRUE;
5877 int i;
5879 if (!(c[0] == '.' || c[0] == ','))
5880 goto done;
5881 if (strlen(c) == 1) {
5882 /* XXX should not happen */
5883 rv = TRUE;
5884 goto done;
5887 if (isdigit(c[1])) {
5888 /* numeric input */
5889 i = strtonum(&c[1], 1, 4096, &errstr);
5890 if (errstr) {
5891 show_oops(t, "invalid numerical hint %s", &c[1]);
5892 goto done;
5894 s = g_strdup_printf("hints.updateHints(%d);", i);
5895 run_script(t, s);
5896 g_free(s);
5897 } else {
5898 /* alphanumeric input */
5899 s = g_strdup_printf("hints.createHints('%s', '%c');",
5900 &c[1], c[0] == '.' ? 'f' : 'F');
5901 run_script(t, s);
5902 g_free(s);
5905 rv = FALSE;
5906 done:
5907 return (rv);
5910 gboolean
5911 search_continue(struct tab *t)
5913 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5914 gboolean rv = FALSE;
5916 if (c[0] == ':' || c[0] == '.' || c[0] == ',')
5917 goto done;
5918 if (strlen(c) == 1) {
5919 webkit_web_view_unmark_text_matches(t->wv);
5920 goto done;
5923 if (c[0] == '/')
5924 t->search_forward = TRUE;
5925 else if (c[0] == '?')
5926 t->search_forward = FALSE;
5927 else
5928 goto done;
5930 rv = TRUE;
5931 done:
5932 return (rv);
5935 gboolean
5936 search_cb(struct tab *t)
5938 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5939 #if !GTK_CHECK_VERSION(3, 0, 0)
5940 GdkColor color;
5941 #endif
5943 if (search_continue(t) == FALSE)
5944 goto done;
5946 /* search */
5947 if (webkit_web_view_search_text(t->wv, &c[1], FALSE, t->search_forward,
5948 TRUE) == FALSE) {
5949 /* not found, mark red */
5950 #if GTK_CHECK_VERSION(3, 0, 0)
5951 gtk_widget_set_name(t->cmd, XT_CSS_RED);
5952 #else
5953 gdk_color_parse(XT_COLOR_RED, &color);
5954 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL, &color);
5955 #endif
5956 /* unmark and remove selection */
5957 webkit_web_view_unmark_text_matches(t->wv);
5958 /* my kingdom for a way to unselect text in webview */
5959 } else {
5960 /* found, highlight all */
5961 webkit_web_view_unmark_text_matches(t->wv);
5962 webkit_web_view_mark_text_matches(t->wv, &c[1], FALSE, 0);
5963 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
5964 #if GTK_CHECK_VERSION(3, 0, 0)
5965 gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
5966 #else
5967 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
5968 &t->default_style->base[GTK_STATE_NORMAL]);
5969 #endif
5971 done:
5972 t->search_id = 0;
5973 return (FALSE);
5977 cmd_keyrelease_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5979 const gchar *c = gtk_entry_get_text(w);
5981 if (t == NULL) {
5982 show_oops(NULL, "cmd_keyrelease_cb invalid parameters");
5983 return (XT_CB_PASSTHROUGH);
5986 DNPRINTF(XT_D_CMD, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
5987 e->keyval, e->state, t->tab_id);
5989 /* hinting */
5990 if (!(e->keyval == GDK_Tab || e->keyval == GDK_ISO_Left_Tab)) {
5991 if (hint_continue(t) == FALSE)
5992 goto done;
5995 /* search */
5996 if (search_continue(t) == FALSE)
5997 goto done;
5999 /* if search length is > 4 then no longer play timeout games */
6000 if (strlen(c) > 4) {
6001 if (t->search_id) {
6002 g_source_remove(t->search_id);
6003 t->search_id = 0;
6005 search_cb(t);
6006 goto done;
6009 /* reestablish a new timer if the user types fast */
6010 if (t->search_id)
6011 g_source_remove(t->search_id);
6012 t->search_id = g_timeout_add(250, (GSourceFunc)search_cb, (gpointer)t);
6014 done:
6015 return (XT_CB_PASSTHROUGH);
6018 gboolean
6019 match_uri(const gchar *uri, const gchar *key) {
6020 gchar *voffset;
6021 size_t len;
6022 gboolean match = FALSE;
6024 len = strlen(key);
6026 if (!strncmp(key, uri, len))
6027 match = TRUE;
6028 else {
6029 voffset = strstr(uri, "/") + 2;
6030 if (!strncmp(key, voffset, len))
6031 match = TRUE;
6032 else if (g_str_has_prefix(voffset, "www.")) {
6033 voffset = voffset + strlen("www.");
6034 if (!strncmp(key, voffset, len))
6035 match = TRUE;
6039 return (match);
6042 gboolean
6043 match_session(const gchar *name, const gchar *key) {
6044 char *sub;
6046 sub = strcasestr(name, key);
6048 return sub == name;
6051 void
6052 cmd_getlist(int id, char *key)
6054 int i, dep, c = 0;
6055 struct history *h;
6056 struct session *s;
6058 if (id >= 0) {
6059 if (cmds[id].type & XT_URLARG) {
6060 RB_FOREACH_REVERSE(h, history_list, &hl)
6061 if (match_uri(h->uri, key)) {
6062 cmd_status.list[c] = (char *)h->uri;
6063 if (++c > 255)
6064 break;
6066 cmd_status.len = c;
6067 return;
6068 } else if (cmds[id].type & XT_SESSARG) {
6069 TAILQ_FOREACH(s, &sessions, entry)
6070 if (match_session(s->name, key)) {
6071 cmd_status.list[c] = (char *)s->name;
6072 if (++c > 255)
6073 break;
6075 cmd_status.len = c;
6076 return;
6077 } else if (cmds[id].type & XT_SETARG) {
6078 for (i = 0; i < get_settings_size(); i++)
6079 if (!strncmp(key, get_setting_name(i),
6080 strlen(key)))
6081 cmd_status.list[c++] =
6082 get_setting_name(i);
6083 cmd_status.len = c;
6084 return;
6088 dep = (id == -1) ? 0 : cmds[id].level + 1;
6090 for (i = id + 1; i < LENGTH(cmds); i++) {
6091 if (cmds[i].level < dep)
6092 break;
6093 if (cmds[i].level == dep && !strncmp(key, cmds[i].cmd,
6094 strlen(key)) && !isdigit(cmds[i].cmd[0]))
6095 cmd_status.list[c++] = cmds[i].cmd;
6099 cmd_status.len = c;
6102 char *
6103 cmd_getnext(int dir)
6105 cmd_status.index += dir;
6107 if (cmd_status.index < 0)
6108 cmd_status.index = cmd_status.len - 1;
6109 else if (cmd_status.index >= cmd_status.len)
6110 cmd_status.index = 0;
6112 return cmd_status.list[cmd_status.index];
6116 cmd_tokenize(char *s, char *tokens[])
6118 int i = 0;
6119 char *tok, *last = NULL;
6120 size_t len = strlen(s);
6121 bool blank;
6123 blank = len == 0 || (len > 0 && s[len - 1] == ' ');
6124 for (tok = strtok_r(s, " ", &last); tok && i < 3;
6125 tok = strtok_r(NULL, " ", &last), i++)
6126 tokens[i] = tok;
6128 if (blank && i < 3)
6129 tokens[i++] = "";
6131 return (i);
6134 void
6135 cmd_complete(struct tab *t, char *str, int dir)
6137 GtkEntry *w = GTK_ENTRY(t->cmd);
6138 int i, j, levels, c = 0, dep = 0, parent = -1;
6139 int matchcount = 0;
6140 char *tok, *match, *s = g_strdup(str);
6141 char *tokens[3];
6142 char res[XT_MAX_URL_LENGTH + 32] = ":";
6143 char *sc = s;
6145 DNPRINTF(XT_D_CMD, "%s: complete %s\n", __func__, str);
6147 /* copy prefix*/
6148 for (i = 0; isdigit(s[i]); i++)
6149 res[i + 1] = s[i];
6151 for (; isspace(s[i]); i++)
6152 res[i + 1] = s[i];
6154 s += i;
6156 levels = cmd_tokenize(s, tokens);
6158 for (i = 0; i < levels - 1; i++) {
6159 tok = tokens[i];
6160 matchcount = 0;
6161 for (j = c; j < LENGTH(cmds); j++) {
6162 if (cmds[j].level < dep)
6163 break;
6164 if (cmds[j].level == dep && !strncmp(tok, cmds[j].cmd,
6165 strlen(tok))) {
6166 matchcount++;
6167 c = j + 1;
6168 if (strlen(tok) == strlen(cmds[j].cmd)) {
6169 matchcount = 1;
6170 break;
6175 if (matchcount == 1) {
6176 strlcat(res, tok, sizeof res);
6177 strlcat(res, " ", sizeof res);
6178 dep++;
6179 } else {
6180 g_free(sc);
6181 return;
6184 parent = c - 1;
6187 if (cmd_status.index == -1)
6188 cmd_getlist(parent, tokens[i]);
6190 if (cmd_status.len > 0) {
6191 match = cmd_getnext(dir);
6192 strlcat(res, match, sizeof res);
6193 gtk_entry_set_text(w, res);
6194 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6197 g_free(sc);
6200 char *
6201 parse_prefix_and_alias(const char *str, int *prefix)
6203 struct cmd_alias *c;
6204 char *s = g_strdup(str), *sc;
6206 g_strstrip(s);
6207 sc = s;
6209 if (isdigit(s[0])) {
6210 sscanf(s, "%d", prefix);
6211 while (isdigit(s[0]) || isspace(s[0]))
6212 ++s;
6215 TAILQ_FOREACH(c, &cal, entry) {
6216 if (strncmp(s, c->alias, strlen(c->alias)))
6217 continue;
6219 if (strlen(s) == strlen(c->alias)) {
6220 g_free(sc);
6221 return (g_strdup(c->cmd));
6224 if (!isspace(s[strlen(c->alias)]))
6225 continue;
6227 s = g_strdup_printf("%s %s", c->cmd, &s[strlen(c->alias) + 1]);
6228 g_free(sc);
6229 return (s);
6231 s = g_strdup(s);
6232 g_free(sc);
6233 return (s);
6236 gboolean
6237 cmd_execute(struct tab *t, char *str)
6239 struct cmd *cmd = NULL;
6240 char *tok, *last = NULL, *s = str;
6241 int j = 0, len, c = 0, dep = 0, matchcount = 0;
6242 int prefix = -1, rv = XT_CB_PASSTHROUGH;
6243 struct karg arg = {0, NULL, -1};
6245 s = parse_prefix_and_alias(s, &prefix);
6247 for (tok = strtok_r(s, " ", &last); tok;
6248 tok = strtok_r(NULL, " ", &last)) {
6249 matchcount = 0;
6250 for (j = c; j < LENGTH(cmds); j++) {
6251 if (cmds[j].level < dep)
6252 break;
6253 len = (tok[strlen(tok) - 1] == '!') ? strlen(tok) - 1 :
6254 strlen(tok);
6255 if (cmds[j].level == dep &&
6256 !strncmp(tok, cmds[j].cmd, len)) {
6257 matchcount++;
6258 c = j + 1;
6259 cmd = &cmds[j];
6260 if (len == strlen(cmds[j].cmd)) {
6261 matchcount = 1;
6262 break;
6266 if (matchcount == 1) {
6267 if (cmd->type > 0)
6268 goto execute_cmd;
6269 dep++;
6270 } else {
6271 show_oops(t, "Invalid command: %s", str);
6272 goto done;
6275 execute_cmd:
6276 if (cmd == NULL) {
6277 show_oops(t, "Empty command");
6278 goto done;
6280 arg.i = cmd->arg;
6282 if (prefix != -1)
6283 arg.precount = prefix;
6284 else if (cmd_prefix > 0)
6285 arg.precount = cmd_prefix;
6287 if (j > 0 && !(cmd->type & XT_PREFIX) && arg.precount > -1) {
6288 show_oops(t, "No prefix allowed: %s", str);
6289 goto done;
6291 if (cmd->type > 1)
6292 arg.s = last ? g_strdup(last) : g_strdup("");
6293 if (cmd->type & XT_INTARG && last && strlen(last) > 0) {
6294 if (arg.s == NULL) {
6295 show_oops(t, "Invalid command");
6296 goto done;
6298 arg.precount = atoi(arg.s);
6299 if (arg.precount <= 0) {
6300 if (arg.s[0] == '0')
6301 show_oops(t, "Zero count");
6302 else
6303 show_oops(t, "Trailing characters");
6304 goto done;
6308 DNPRINTF(XT_D_CMD, "%s: prefix %d arg %s\n",
6309 __func__, arg.precount, arg.s);
6311 cmd->func(t, &arg);
6313 rv = XT_CB_HANDLED;
6314 done:
6315 if (j > 0)
6316 cmd_prefix = 0;
6317 g_free(s);
6318 if (arg.s)
6319 g_free(arg.s);
6321 return (rv);
6325 save_runtime_setting(const char *name, const char *val)
6327 struct stat sb;
6328 FILE *f;
6329 size_t linelen;
6330 int found = 0;
6331 char file[PATH_MAX];
6332 char delim[3] = { '\0', '\0', '\0' };
6333 char *line, *lt, *start;
6334 char *contents, *tmp;
6336 if (runtime_settings == NULL || strlen(runtime_settings) == 0)
6337 return (-1);
6339 snprintf(file, sizeof file, "%s" PS "%s", work_dir, runtime_settings);
6340 if (stat(file, &sb) || (f = fopen(file, "r+")) == NULL)
6341 return (-1);
6342 lt = g_strdup_printf("%s=%s", name, val);
6343 contents = g_strdup("");
6344 while (!feof(f)) {
6345 line = fparseln(f, &linelen, NULL, delim, 0);
6346 if (line == NULL || linelen == 0)
6347 continue;
6348 tmp = contents;
6349 start = g_strdup_printf("%s=", name);
6350 if (strstr(line, start) == NULL)
6351 contents = g_strdup_printf("%s%s\n", contents, line);
6352 else {
6353 found = 1;
6354 contents = g_strdup_printf("%s%s\n", contents, lt);
6356 g_free(tmp);
6357 g_free(start);
6358 free(line);
6359 line = NULL;
6361 if (found == 0) {
6362 tmp = contents;
6363 contents = g_strdup_printf("%s%s\n", contents, lt);
6364 g_free(tmp);
6366 if ((f = freopen(file, "w", f)) == NULL)
6367 return (-1);
6368 else {
6369 fputs(contents, f);
6370 fclose(f);
6372 g_free(lt);
6373 g_free(contents);
6375 return (0);
6379 entry_focus_cb(GtkWidget *w, GdkEvent e, struct tab *t)
6382 * This sometimes gets randomly unset for whatever reason in GTK3,
6383 * causing a GtkEntry's text cursor becomes invisible. When we focus
6384 * a GtkEntry, be sure to manually reset the main window's is-active
6385 * property so the cursor is shown correctly.
6387 #if GTK_CHECK_VERSION(3, 0, 0)
6388 fake_focus_in(main_window);
6389 #endif
6390 return (XT_CB_PASSTHROUGH);
6394 entry_key_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6396 if (t == NULL) {
6397 show_oops(NULL, "entry_key_cb invalid parameters");
6398 return (XT_CB_PASSTHROUGH);
6401 DNPRINTF(XT_D_CMD, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6402 e->keyval, e->state, t->tab_id);
6404 hide_oops(t);
6406 if (e->keyval == GDK_Escape) {
6407 /* don't use focus_webview(t) because we want to type :cmds */
6408 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
6411 return (handle_keypress(t, e, 1));
6414 struct command_entry *
6415 history_prev(struct command_list *l, struct command_entry *at)
6417 if (at == NULL)
6418 at = TAILQ_LAST(l, command_list);
6419 else {
6420 at = TAILQ_PREV(at, command_list, entry);
6421 if (at == NULL)
6422 at = TAILQ_LAST(l, command_list);
6425 return (at);
6428 struct command_entry *
6429 history_next(struct command_list *l, struct command_entry *at)
6431 if (at == NULL)
6432 at = TAILQ_FIRST(l);
6433 else {
6434 at = TAILQ_NEXT(at, entry);
6435 if (at == NULL)
6436 at = TAILQ_FIRST(l);
6439 return (at);
6443 cmd_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6445 int rv = XT_CB_HANDLED;
6446 const gchar *c = gtk_entry_get_text(w);
6447 char *s;
6449 if (t == NULL) {
6450 show_oops(NULL, "cmd_keypress_cb parameters");
6451 return (XT_CB_PASSTHROUGH);
6454 DNPRINTF(XT_D_CMD, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6455 e->keyval, e->state, t->tab_id);
6457 /* sanity */
6458 if (c == NULL)
6459 e->keyval = GDK_Escape;
6460 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6461 c[0] == '.' || c[0] == ','))
6462 e->keyval = GDK_Escape;
6464 if (e->keyval != GDK_Tab && e->keyval != GDK_Shift_L &&
6465 e->keyval != GDK_ISO_Left_Tab)
6466 cmd_status.index = -1;
6468 switch (e->keyval) {
6469 case GDK_Tab:
6470 if (c[0] == ':')
6471 cmd_complete(t, (char *)&c[1], 1);
6472 else if (c[0] == '.' || c[0] == ',')
6473 run_script(t, "hints.focusNextHint();");
6474 goto done;
6475 case GDK_ISO_Left_Tab:
6476 if (c[0] == ':')
6477 cmd_complete(t, (char *)&c[1], -1);
6478 else if (c[0] == '.' || c[0] == ',')
6479 run_script(t, "hints.focusPreviousHint();");
6480 goto done;
6481 case GDK_Down:
6482 if (c[0] == '?') {
6483 if ((search_at = history_next(&shl, search_at))) {
6484 search_at->line[0] = c[0];
6485 gtk_entry_set_text(w, search_at->line);
6486 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6488 } else if (c[0] == '/') {
6489 if ((search_at = history_prev(&shl, search_at))) {
6490 search_at->line[0] = c[0];
6491 gtk_entry_set_text(w, search_at->line);
6492 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6494 } else if (c[0] == ':') {
6495 if ((history_at = history_prev(&chl, history_at))) {
6496 history_at->line[0] = c[0];
6497 gtk_entry_set_text(w, history_at->line);
6498 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6501 goto done;
6502 case GDK_Up:
6503 if (c[0] == '/') {
6504 if ((search_at = history_next(&shl, search_at))) {
6505 search_at->line[0] = c[0];
6506 gtk_entry_set_text(w, search_at->line);
6507 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6509 } else if (c[0] == '?') {
6510 if ((search_at = history_prev(&shl, search_at))) {
6511 search_at->line[0] = c[0];
6512 gtk_entry_set_text(w, search_at->line);
6513 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6515 } if (c[0] == ':') {
6516 if ((history_at = history_next(&chl, history_at))) {
6517 history_at->line[0] = c[0];
6518 gtk_entry_set_text(w, history_at->line);
6519 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6522 goto done;
6523 case GDK_BackSpace:
6524 if (!(!strcmp(c, ":") || !strcmp(c, "/") || !strcmp(c, "?") ||
6525 !strcmp(c, ".") || !strcmp(c, ","))) {
6526 /* see if we are doing hinting and reset it */
6527 if (c[0] == '.' || c[0] == ',') {
6528 /* recreate hints */
6529 s = g_strdup_printf("hints.createHints('', "
6530 "'%c');", c[0] == '.' ? 'f' : 'F');
6531 run_script(t, s);
6532 g_free(s);
6534 break;
6537 /* FALLTHROUGH */
6538 case GDK_Escape:
6539 hide_cmd(t);
6541 /* cancel search */
6542 if (c != NULL && (c[0] == '/' || c[0] == '?'))
6543 webkit_web_view_unmark_text_matches(t->wv);
6545 /* no need to cancel hints */
6546 goto done;
6549 rv = XT_CB_PASSTHROUGH;
6550 done:
6551 return (rv);
6554 void
6555 wv_popup_activ_cb(GtkMenuItem *menu, struct tab *t)
6557 GtkAction *a = NULL;
6558 GtkClipboard *clipboard, *primary;
6559 const gchar *name, *uri;
6561 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
6562 if (a == NULL)
6563 return;
6564 name = gtk_action_get_name(a);
6566 DNPRINTF(XT_D_CMD, "wv_popup_activ_cb: tab %d action %s\n",
6567 t->tab_id, name);
6570 * context-menu-action-3 copy link location
6571 * context-menu-action-7 copy image address
6572 * context-menu-action-2030 copy video link location
6575 /* copy actions */
6576 if ((g_strcmp0(name, "context-menu-action-3") == 0) ||
6577 (g_strcmp0(name, "context-menu-action-7") == 0) ||
6578 (g_strcmp0(name, "context-menu-action-2030") == 0)) {
6579 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
6580 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
6581 uri = gtk_clipboard_wait_for_text(clipboard);
6582 if (uri == NULL)
6583 return;
6584 gtk_clipboard_set_text(primary, uri, -1);
6588 void
6589 wv_popup_cb(WebKitWebView *wview, GtkMenu *menu, struct tab *t)
6591 GList *items, *l;
6593 DNPRINTF(XT_D_CMD, "wv_popup_cb: tab %d\n", t->tab_id);
6595 items = gtk_container_get_children(GTK_CONTAINER(menu));
6596 for (l = items; l; l = l->next)
6597 g_signal_connect(l->data, "activate",
6598 G_CALLBACK(wv_popup_activ_cb), t);
6599 g_list_free(items);
6602 void
6603 cmd_popup_cb(GtkEntry *entry, GtkMenu *menu, struct tab *t)
6605 /* popup menu enabled */
6606 t->popup = 1;
6610 cmd_focusout_cb(GtkWidget *w, GdkEventFocus *e, struct tab *t)
6612 if (t == NULL) {
6613 show_oops(NULL, "cmd_focusout_cb invalid parameters");
6614 return (XT_CB_PASSTHROUGH);
6617 DNPRINTF(XT_D_CMD, "cmd_focusout_cb: tab %d popup %d\n",
6618 t->tab_id, t->popup);
6620 /* if popup is enabled don't lose focus */
6621 if (t->popup) {
6622 t->popup = 0;
6623 return (XT_CB_PASSTHROUGH);
6626 hide_cmd(t);
6627 hide_oops(t);
6628 disable_hints(t);
6630 return (XT_CB_PASSTHROUGH);
6633 void
6634 cmd_hide_cb(GtkWidget *w, struct tab *t)
6636 if (t == NULL) {
6637 show_oops(NULL, "%s: invalid parameters", __func__);
6638 return;
6641 if (show_url == 0 || t->focus_wv)
6642 focus_webview(t);
6643 else
6644 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
6647 void
6648 cmd_activate_cb(GtkEntry *entry, struct tab *t)
6650 char *s;
6651 const gchar *c = gtk_entry_get_text(entry);
6653 if (t == NULL) {
6654 show_oops(NULL, "cmd_activate_cb invalid parameters");
6655 return;
6658 DNPRINTF(XT_D_CMD, "cmd_activate_cb: tab %d %s\n", t->tab_id, c);
6660 /* sanity */
6661 if (c == NULL)
6662 goto done;
6663 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6664 c[0] == '.' || c[0] == ','))
6665 goto done;
6666 if (strlen(c) < 1)
6667 goto done;
6668 s = (char *)&c[1];
6670 if (c[0] == '/' || c[0] == '?') {
6671 /* see if there is a timer pending */
6672 if (t->search_id) {
6673 g_source_remove(t->search_id);
6674 t->search_id = 0;
6675 search_cb(t);
6678 if (t->search_text) {
6679 g_free(t->search_text);
6680 t->search_text = NULL;
6683 t->search_text = g_strdup(s);
6684 if (global_search)
6685 g_free(global_search);
6686 global_search = g_strdup(s);
6687 t->search_forward = c[0] == '/';
6689 history_add(&shl, search_file, s, &search_history_count);
6690 } else if (c[0] == '.' || c[0] == ',') {
6691 run_script(t, "hints.fire();");
6692 /* XXX history for link following? */
6693 } else if (c[0] == ':') {
6694 history_add(&chl, command_file, s, &cmd_history_count);
6695 /* can't call hide_cmd after cmd_execute */
6696 hide_cmd(t);
6697 cmd_execute(t, s);
6698 return;
6701 done:
6702 hide_cmd(t);
6705 void
6706 backward_cb(GtkWidget *w, struct tab *t)
6708 struct karg a;
6710 if (t == NULL) {
6711 show_oops(NULL, "backward_cb invalid parameters");
6712 return;
6715 DNPRINTF(XT_D_NAV, "backward_cb: tab %d\n", t->tab_id);
6717 a.i = XT_NAV_BACK;
6718 navaction(t, &a);
6721 void
6722 forward_cb(GtkWidget *w, struct tab *t)
6724 struct karg a;
6726 if (t == NULL) {
6727 show_oops(NULL, "forward_cb invalid parameters");
6728 return;
6731 DNPRINTF(XT_D_NAV, "forward_cb: tab %d\n", t->tab_id);
6733 a.i = XT_NAV_FORWARD;
6734 navaction(t, &a);
6737 void
6738 home_cb(GtkWidget *w, struct tab *t)
6740 if (t == NULL) {
6741 show_oops(NULL, "home_cb invalid parameters");
6742 return;
6745 DNPRINTF(XT_D_NAV, "home_cb: tab %d\n", t->tab_id);
6747 load_uri(t, home);
6750 void
6751 stop_cb(GtkWidget *w, struct tab *t)
6753 WebKitWebFrame *frame;
6755 if (t == NULL) {
6756 show_oops(NULL, "stop_cb invalid parameters");
6757 return;
6760 DNPRINTF(XT_D_NAV, "stop_cb: tab %d\n", t->tab_id);
6762 frame = webkit_web_view_get_main_frame(t->wv);
6763 if (frame == NULL) {
6764 show_oops(t, "stop_cb: no frame");
6765 return;
6768 webkit_web_frame_stop_loading(frame);
6769 abort_favicon_download(t);
6772 void
6773 setup_webkit(struct tab *t)
6775 if (is_g_object_setting(G_OBJECT(t->settings), "enable-dns-prefetching"))
6776 g_object_set(G_OBJECT(t->settings), "enable-dns-prefetching",
6777 FALSE, (char *)NULL);
6778 else
6779 warnx("webkit does not have \"enable-dns-prefetching\" property");
6780 g_object_set(G_OBJECT(t->settings),
6781 "enable-scripts", enable_scripts, (char *)NULL);
6782 g_object_set(G_OBJECT(t->settings),
6783 "enable-plugins", enable_plugins, (char *)NULL);
6784 g_object_set(G_OBJECT(t->settings),
6785 "javascript-can-open-windows-automatically", enable_scripts,
6786 (char *)NULL);
6787 g_object_set(G_OBJECT(t->settings),
6788 "enable-html5-database", FALSE, (char *)NULL);
6789 g_object_set(G_OBJECT(t->settings),
6790 "enable-html5-local-storage", enable_localstorage, (char *)NULL);
6791 g_object_set(G_OBJECT(t->settings),
6792 "enable_spell_checking", enable_spell_checking, (char *)NULL);
6793 g_object_set(G_OBJECT(t->settings),
6794 "spell_checking_languages", spell_check_languages, (char *)NULL);
6795 g_object_set(G_OBJECT(t->settings),
6796 "enable-developer-extras", TRUE, (char *)NULL);
6797 g_object_set(G_OBJECT(t->wv),
6798 "full-content-zoom", TRUE, (char *)NULL);
6799 g_object_set(G_OBJECT(t->settings),
6800 "auto-load-images", auto_load_images, (char *)NULL);
6801 if (is_g_object_setting(G_OBJECT(t->settings),
6802 "enable-display-of-insecure-content"))
6803 g_object_set(G_OBJECT(t->settings),
6804 "enable-display-of-insecure-content",
6805 allow_insecure_content, (char *)NULL);
6806 if (is_g_object_setting(G_OBJECT(t->settings),
6807 "enable-running-of-insecure-content"))
6808 g_object_set(G_OBJECT(t->settings),
6809 "enable-running-of-insecure-content",
6810 allow_insecure_scripts, (char *)NULL);
6812 webkit_web_view_set_settings(t->wv, t->settings);
6815 gboolean
6816 update_statusbar_position(GtkAdjustment* adjustment, gpointer data)
6818 struct tab *ti, *t = NULL;
6819 gdouble view_size, value, max;
6820 gchar *position;
6822 TAILQ_FOREACH(ti, &tabs, entry)
6823 if (ti->tab_id == gtk_notebook_get_current_page(notebook)) {
6824 t = ti;
6825 break;
6828 if (t == NULL)
6829 return FALSE;
6831 if (adjustment == NULL)
6832 adjustment = gtk_scrolled_window_get_vadjustment(
6833 GTK_SCROLLED_WINDOW(t->browser_win));
6835 view_size = gtk_adjustment_get_page_size(adjustment);
6836 value = gtk_adjustment_get_value(adjustment);
6837 max = gtk_adjustment_get_upper(adjustment) - view_size;
6839 if (max == 0)
6840 position = g_strdup("All");
6841 else if (value == max)
6842 position = g_strdup("Bot");
6843 else if (value == 0)
6844 position = g_strdup("Top");
6845 else
6846 position = g_strdup_printf("%d%%", (int) ((value / max) * 100));
6848 gtk_label_set_text(GTK_LABEL(t->sbe.position), position);
6849 g_free(position);
6851 return (TRUE);
6854 GtkWidget *
6855 create_window(const gchar *name)
6857 GtkWidget *w;
6859 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
6860 if (window_maximize)
6861 gtk_window_maximize(GTK_WINDOW(w));
6862 else
6863 gtk_window_set_default_size(GTK_WINDOW(w), window_width, window_height);
6864 gtk_widget_set_name(w, name);
6865 gtk_window_set_wmclass(GTK_WINDOW(w), name, "Xombrero");
6867 return (w);
6870 GtkWidget *
6871 create_browser(struct tab *t)
6873 GtkWidget *w;
6874 GtkAdjustment *adjustment;
6876 if (t == NULL) {
6877 show_oops(NULL, "create_browser invalid parameters");
6878 return (NULL);
6881 w = gtk_scrolled_window_new(NULL, NULL);
6882 gtk_widget_set_can_focus(w, FALSE);
6883 t->adjust_h = gtk_scrolled_window_get_hadjustment(
6884 GTK_SCROLLED_WINDOW(w));
6885 t->adjust_v = gtk_scrolled_window_get_vadjustment(
6886 GTK_SCROLLED_WINDOW(w));
6887 #if !GTK_CHECK_VERSION(3, 0, 0)
6888 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
6889 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
6890 #endif
6893 t->wv = WEBKIT_WEB_VIEW(webkit_web_view_new());
6894 gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(t->wv));
6896 /* set defaults */
6897 t->settings = webkit_web_settings_new();
6899 g_object_set(t->settings, "default-encoding", encoding, (char *)NULL);
6901 t->stylesheet = g_strdup(stylesheet);
6902 t->load_images = auto_load_images;
6904 adjustment =
6905 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w));
6906 g_signal_connect(G_OBJECT(adjustment), "value-changed",
6907 G_CALLBACK(update_statusbar_position), NULL);
6909 setup_webkit(t);
6910 setup_inspector(t);
6912 return (w);
6915 GtkWidget *
6916 create_kiosk_toolbar(struct tab *t)
6918 GtkWidget *toolbar = NULL, *b;
6920 #if GTK_CHECK_VERSION(3, 0, 0)
6921 b = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
6922 gtk_widget_set_name(GTK_WIDGET(b), "toolbar");
6923 #else
6924 b = gtk_hbox_new(FALSE, 0);
6925 #endif
6926 toolbar = b;
6927 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
6929 /* backward button */
6930 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
6931 gtk_widget_set_sensitive(t->backward, FALSE);
6932 g_signal_connect(G_OBJECT(t->backward), "clicked",
6933 G_CALLBACK(backward_cb), t);
6934 gtk_box_pack_start(GTK_BOX(b), t->backward, TRUE, TRUE, 0);
6936 /* forward button */
6937 t->forward = create_button("Forward", GTK_STOCK_GO_FORWARD, 0);
6938 gtk_widget_set_sensitive(t->forward, FALSE);
6939 g_signal_connect(G_OBJECT(t->forward), "clicked",
6940 G_CALLBACK(forward_cb), t);
6941 gtk_box_pack_start(GTK_BOX(b), t->forward, TRUE, TRUE, 0);
6943 /* home button */
6944 t->gohome = create_button("Home", GTK_STOCK_HOME, 0);
6945 gtk_widget_set_sensitive(t->gohome, true);
6946 g_signal_connect(G_OBJECT(t->gohome), "clicked",
6947 G_CALLBACK(home_cb), t);
6948 gtk_box_pack_start(GTK_BOX(b), t->gohome, TRUE, TRUE, 0);
6950 /* create widgets but don't use them */
6951 t->uri_entry = gtk_entry_new();
6952 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
6953 G_CALLBACK(entry_focus_cb), t);
6954 #if !GTK_CHECK_VERSION(3, 0, 0)
6955 t->default_style = gtk_rc_get_style(t->uri_entry);
6956 #endif
6957 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
6958 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
6959 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
6961 return (toolbar);
6964 GtkWidget *
6965 create_toolbar(struct tab *t)
6967 GtkWidget *toolbar = NULL, *b;
6969 #if GTK_CHECK_VERSION(3, 0, 0)
6970 b = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
6971 gtk_widget_set_name(GTK_WIDGET(b), "toolbar");
6972 #else
6973 b = gtk_hbox_new(FALSE, 0);
6974 #endif
6975 toolbar = b;
6976 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
6978 /* backward button */
6979 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
6980 gtk_widget_set_sensitive(t->backward, FALSE);
6981 g_signal_connect(G_OBJECT(t->backward), "clicked",
6982 G_CALLBACK(backward_cb), t);
6983 gtk_box_pack_start(GTK_BOX(b), t->backward, FALSE, FALSE, 0);
6985 /* forward button */
6986 t->forward = create_button("Forward",GTK_STOCK_GO_FORWARD, 0);
6987 gtk_widget_set_sensitive(t->forward, FALSE);
6988 g_signal_connect(G_OBJECT(t->forward), "clicked",
6989 G_CALLBACK(forward_cb), t);
6990 gtk_box_pack_start(GTK_BOX(b), t->forward, FALSE, FALSE, 0);
6992 /* stop button */
6993 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
6994 gtk_widget_set_sensitive(t->stop, FALSE);
6995 g_signal_connect(G_OBJECT(t->stop), "clicked", G_CALLBACK(stop_cb), t);
6996 gtk_box_pack_start(GTK_BOX(b), t->stop, FALSE, FALSE, 0);
6998 /* JS button */
6999 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
7000 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
7001 gtk_widget_set_sensitive(t->js_toggle, TRUE);
7002 g_signal_connect(G_OBJECT(t->js_toggle), "clicked",
7003 G_CALLBACK(js_toggle_cb), t);
7004 gtk_box_pack_start(GTK_BOX(b), t->js_toggle, FALSE, FALSE, 0);
7006 /* toggle proxy button */
7007 t->proxy_toggle = create_button("Proxy-Toggle", proxy_uri ?
7008 GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT, 0);
7009 /* override icons */
7010 if (proxy_uri)
7011 button_set_file(t->proxy_toggle, "torenabled.ico");
7012 else
7013 button_set_file(t->proxy_toggle, "tordisabled.ico");
7014 gtk_widget_set_sensitive(t->proxy_toggle, TRUE);
7015 g_signal_connect(G_OBJECT(t->proxy_toggle), "clicked",
7016 G_CALLBACK(proxy_toggle_cb), t);
7017 gtk_box_pack_start(GTK_BOX(b), t->proxy_toggle, FALSE, FALSE, 0);
7019 t->uri_entry = gtk_entry_new();
7020 g_signal_connect(G_OBJECT(t->uri_entry), "activate",
7021 G_CALLBACK(activate_uri_entry_cb), t);
7022 g_signal_connect(G_OBJECT(t->uri_entry), "key-press-event",
7023 G_CALLBACK(entry_key_cb), t);
7024 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
7025 G_CALLBACK(entry_focus_cb), t);
7026 completion_add(t);
7027 gtk_box_pack_start(GTK_BOX(b), t->uri_entry, TRUE, TRUE, 0);
7029 /* search entry */
7030 t->search_entry = gtk_entry_new();
7031 gtk_entry_set_width_chars(GTK_ENTRY(t->search_entry), 30);
7032 g_signal_connect(G_OBJECT(t->search_entry), "activate",
7033 G_CALLBACK(activate_search_entry_cb), t);
7034 g_signal_connect(G_OBJECT(t->search_entry), "key-press-event",
7035 G_CALLBACK(entry_key_cb), t);
7036 g_signal_connect(G_OBJECT(t->search_entry), "focus-in-event",
7037 G_CALLBACK(entry_focus_cb), t);
7038 gtk_box_pack_start(GTK_BOX(b), t->search_entry, FALSE, FALSE, 0);
7040 #if !GTK_CHECK_VERSION(3, 0, 0)
7041 t->default_style = gtk_rc_get_style(t->uri_entry);
7042 #endif
7044 return (toolbar);
7047 GtkWidget *
7048 create_buffers(struct tab *t)
7050 GtkCellRenderer *renderer;
7051 GtkWidget *view;
7053 view = gtk_tree_view_new();
7055 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
7057 renderer = gtk_cell_renderer_text_new();
7058 gtk_tree_view_insert_column_with_attributes
7059 (GTK_TREE_VIEW(view), -1, "Id", renderer, "text", COL_ID, (char *)NULL);
7061 renderer = gtk_cell_renderer_pixbuf_new();
7062 gtk_tree_view_insert_column_with_attributes
7063 (GTK_TREE_VIEW(view), -1, "Favicon", renderer, "pixbuf", COL_FAVICON,
7064 (char *)NULL);
7066 renderer = gtk_cell_renderer_text_new();
7067 gtk_tree_view_insert_column_with_attributes
7068 (GTK_TREE_VIEW(view), -1, "Title", renderer, "text", COL_TITLE,
7069 (char *)NULL);
7071 gtk_tree_view_set_model
7072 (GTK_TREE_VIEW(view), GTK_TREE_MODEL(buffers_store));
7074 return (view);
7077 void
7078 row_activated_cb(GtkTreeView *view, GtkTreePath *path,
7079 GtkTreeViewColumn *col, struct tab *t)
7081 GtkTreeIter iter;
7082 guint id;
7084 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7086 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter,
7087 path)) {
7088 gtk_tree_model_get
7089 (GTK_TREE_MODEL(buffers_store), &iter, COL_ID, &id, -1);
7090 set_current_tab(id - 1);
7093 hide_buffers(t);
7096 /* after tab reordering/creation/removal */
7097 void
7098 recalc_tabs(void)
7100 struct tab *t;
7102 TAILQ_FOREACH(t, &tabs, entry)
7103 t->tab_id = gtk_notebook_page_num(notebook, t->vbox);
7106 void
7107 update_statusbar_tabs(struct tab *t)
7109 int tab_id, max_tab_id;
7110 char s[16];
7112 if (t != NULL)
7113 tab_id = t->tab_id;
7114 else
7115 tab_id = gtk_notebook_get_current_page(notebook);
7117 max_tab_id = gtk_notebook_get_n_pages(notebook);
7118 snprintf(s, sizeof s, "%d/%d", tab_id + 1, max_tab_id);
7120 if (t == NULL)
7121 t = get_current_tab();
7123 if (t != NULL)
7124 gtk_label_set_text(GTK_LABEL(t->sbe.tabs), s);
7127 /* after active tab change */
7128 void
7129 recolor_compact_tabs(void)
7131 struct tab *t;
7132 int curid = 0;
7133 #if !GTK_CHECK_VERSION(3, 0, 0)
7134 GdkColor color_active, color_inactive;
7136 gdk_color_parse(XT_COLOR_CT_ACTIVE, &color_active);
7137 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color_inactive);
7138 #endif
7139 curid = gtk_notebook_get_current_page(notebook);
7141 TAILQ_FOREACH(t, &tabs, entry) {
7142 #if GTK_CHECK_VERSION(3, 0, 0)
7143 if (t->tab_id == curid)
7144 gtk_widget_set_name(t->tab_elems.label, XT_CSS_ACTIVE);
7145 else
7146 gtk_widget_set_name(t->tab_elems.label, "");
7147 #else
7148 if (t->tab_id == curid)
7149 gtk_widget_modify_fg(t->tab_elems.label,
7150 GTK_STATE_NORMAL, &color_active);
7151 else
7152 gtk_widget_modify_fg(t->tab_elems.label,
7153 GTK_STATE_NORMAL, &color_inactive);
7154 #endif
7158 void
7159 set_current_tab(int page_num)
7161 buffercmd_abort(get_current_tab());
7162 gtk_notebook_set_current_page(notebook, page_num);
7163 recolor_compact_tabs();
7164 update_statusbar_tabs(NULL);
7168 undo_close_tab_save(struct tab *t)
7170 int m, n;
7171 const gchar *uri;
7172 struct undo *u1, *u2;
7173 GList *items;
7174 WebKitWebHistoryItem *item;
7176 if ((uri = get_uri(t)) == NULL)
7177 return (1);
7179 u1 = g_malloc0(sizeof(struct undo));
7180 u1->uri = g_strdup(uri);
7182 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7184 m = webkit_web_back_forward_list_get_forward_length(t->bfl);
7185 n = webkit_web_back_forward_list_get_back_length(t->bfl);
7186 u1->back = n;
7188 /* forward history */
7189 items = webkit_web_back_forward_list_get_forward_list_with_limit(t->bfl, m);
7191 while (items) {
7192 item = items->data;
7193 u1->history = g_list_prepend(u1->history,
7194 webkit_web_history_item_copy(item));
7195 items = g_list_next(items);
7198 /* current item */
7199 if (m) {
7200 item = webkit_web_back_forward_list_get_current_item(t->bfl);
7201 u1->history = g_list_prepend(u1->history,
7202 webkit_web_history_item_copy(item));
7205 /* back history */
7206 items = webkit_web_back_forward_list_get_back_list_with_limit(t->bfl, n);
7208 while (items) {
7209 item = items->data;
7210 u1->history = g_list_prepend(u1->history,
7211 webkit_web_history_item_copy(item));
7212 items = g_list_next(items);
7215 TAILQ_INSERT_HEAD(&undos, u1, entry);
7217 if (undo_count > XT_MAX_UNDO_CLOSE_TAB) {
7218 u2 = TAILQ_LAST(&undos, undo_tailq);
7219 TAILQ_REMOVE(&undos, u2, entry);
7220 g_free(u2->uri);
7221 g_list_free(u2->history);
7222 g_free(u2);
7223 } else
7224 undo_count++;
7226 return (0);
7229 void
7230 delete_tab(struct tab *t)
7232 struct karg a;
7234 DNPRINTF(XT_D_TAB, "delete_tab: %p\n", t);
7236 if (t == NULL)
7237 return;
7239 TAILQ_REMOVE(&tabs, t, entry);
7240 buffercmd_abort(t);
7242 /* Halt all webkit activity. */
7243 abort_favicon_download(t);
7244 webkit_web_view_stop_loading(t->wv);
7246 /* Save the tab, so we can undo the close. */
7247 undo_close_tab_save(t);
7249 /* just in case */
7250 if (t->search_id)
7251 g_source_remove(t->search_id);
7253 /* session key */
7254 if (t->session_key)
7255 g_free(t->session_key);
7257 /* inspector */
7258 bzero(&a, sizeof a);
7259 a.i = XT_INS_CLOSE;
7260 inspector_cmd(t, &a);
7262 if (browser_mode == XT_BM_KIOSK) {
7263 gtk_widget_destroy(t->uri_entry);
7264 gtk_widget_destroy(t->stop);
7265 gtk_widget_destroy(t->js_toggle);
7268 g_object_unref(t->completion);
7269 if (t->item)
7270 g_object_unref(t->item);
7272 gtk_widget_destroy(t->tab_elems.eventbox);
7273 gtk_widget_destroy(t->vbox);
7275 g_free(t->stylesheet);
7276 g_free(t->tmp_uri);
7277 g_free(t->status);
7278 g_free(t);
7279 t = NULL;
7281 if (TAILQ_EMPTY(&tabs)) {
7282 if (browser_mode == XT_BM_KIOSK)
7283 create_new_tab(home, NULL, 1, -1);
7284 else
7285 create_new_tab(NULL, NULL, 1, -1);
7288 /* recreate session */
7289 if (session_autosave) {
7290 bzero(&a, sizeof a);
7291 a.s = NULL;
7292 save_tabs(NULL, &a);
7295 recalc_tabs();
7296 recolor_compact_tabs();
7299 void
7300 update_statusbar_zoom(struct tab *t)
7302 gfloat zoom;
7303 char s[16] = { '\0' };
7305 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7306 if ((zoom <= 0.99 || zoom >= 1.01))
7307 snprintf(s, sizeof s, "%d%%", (int)(zoom * 100));
7308 gtk_label_set_text(GTK_LABEL(t->sbe.zoom), s);
7311 void
7312 setzoom_webkit(struct tab *t, int adjust)
7314 #define XT_ZOOMPERCENT 0.04
7316 gfloat zoom;
7318 if (t == NULL) {
7319 show_oops(NULL, "setzoom_webkit invalid parameters");
7320 return;
7323 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7324 if (adjust == XT_ZOOM_IN)
7325 zoom += XT_ZOOMPERCENT;
7326 else if (adjust == XT_ZOOM_OUT)
7327 zoom -= XT_ZOOMPERCENT;
7328 else if (adjust > 0)
7329 zoom = default_zoom_level + adjust / 100.0 - 1.0;
7330 else {
7331 show_oops(t, "setzoom_webkit invalid zoom value");
7332 return;
7335 if (zoom < XT_ZOOMPERCENT)
7336 zoom = XT_ZOOMPERCENT;
7337 g_object_set(G_OBJECT(t->wv), "zoom-level", zoom, (char *)NULL);
7338 update_statusbar_zoom(t);
7341 gboolean
7342 tab_clicked_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
7344 struct tab *t = (struct tab *) data;
7346 DNPRINTF(XT_D_TAB, "tab_clicked_cb: tab: %d\n", t->tab_id);
7348 switch (event->button) {
7349 case 1:
7350 set_current_tab(t->tab_id);
7351 break;
7352 case 2:
7353 delete_tab(t);
7354 break;
7357 return TRUE;
7360 void
7361 append_tab(struct tab *t)
7363 if (t == NULL)
7364 return;
7366 TAILQ_INSERT_TAIL(&tabs, t, entry);
7367 t->tab_id = gtk_notebook_append_page(notebook, t->vbox, t->tab_content);
7370 GtkWidget *
7371 create_sbe(void)
7373 GtkWidget *sbe;
7375 sbe = gtk_label_new(NULL);
7376 gtk_widget_set_can_focus(GTK_WIDGET(sbe), FALSE);
7377 gtk_widget_modify_font(GTK_WIDGET(sbe), statusbar_font);
7378 return (sbe);
7382 add_sbe(GtkWidget *box, char flag, int *used, GtkWidget *sbe)
7384 if (box == NULL || used == NULL || sbe == NULL) {
7385 DPRINTF("%s: invalid parameters", __func__);
7386 return (1);
7389 if (*used) {
7390 warnx("flag \"%c\" specified more than "
7391 "once in statusbar_elems\n", flag);
7392 return (1);
7395 gtk_box_pack_start(GTK_BOX(box), sbe, FALSE, FALSE, 0);
7396 *used = 1;
7398 return (0);
7402 statusbar_create(struct tab *t)
7404 GtkWidget *box; /* container for statusbar elems */
7405 GtkWidget *sep;
7406 char *p;
7407 int sbe_P = 0, sbe_B = 0, sbe_Z = 0, sbe_T = 0,
7408 sbe_p = 0;
7409 #if !GTK_CHECK_VERSION(3, 0, 0)
7410 GdkColor color;
7411 #endif
7413 if (t == NULL) {
7414 DPRINTF("%s: invalid parameters", __func__);
7415 return (1);
7418 #if GTK_CHECK_VERSION(3, 0, 0)
7419 t->statusbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7420 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7421 gtk_widget_set_name(GTK_WIDGET(t->statusbar), "statusbar");
7422 #else
7423 t->statusbar = gtk_hbox_new(FALSE, 0);
7424 box = gtk_hbox_new(FALSE, 0);
7425 #endif
7426 t->sbe.ebox = gtk_event_box_new();
7428 gtk_widget_set_can_focus(GTK_WIDGET(t->statusbar), FALSE);
7429 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.ebox), FALSE);
7430 gtk_widget_set_can_focus(GTK_WIDGET(box), FALSE);
7432 gtk_box_set_spacing(GTK_BOX(box), 10);
7433 gtk_box_pack_start(GTK_BOX(t->statusbar), t->sbe.ebox, TRUE, TRUE, 0);
7434 gtk_container_add(GTK_CONTAINER(t->sbe.ebox), box);
7436 /* create these widgets only if specified in statusbar_elems */
7437 t->sbe.uri = gtk_entry_new();
7438 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.uri), FALSE);
7439 gtk_widget_modify_font(GTK_WIDGET(t->sbe.uri), statusbar_font);
7440 #if !GTK_CHECK_VERSION(3, 0, 0)
7441 gtk_entry_set_inner_border(GTK_ENTRY(t->sbe.uri), NULL);
7442 gtk_entry_set_has_frame(GTK_ENTRY(t->sbe.uri), FALSE);
7443 #endif
7444 t->sbe.position = create_sbe();
7445 t->sbe.zoom = create_sbe();
7446 t->sbe.buffercmd = create_sbe();
7447 t->sbe.tabs = create_sbe();
7448 t->sbe.proxy = create_sbe();
7450 #if GTK_CHECK_VERSION(3, 0, 0)
7451 statusbar_modify_attr(t, XT_CSS_NORMAL);
7452 #else
7453 statusbar_modify_attr(t, XT_COLOR_WHITE, XT_COLOR_BLACK);
7454 #endif
7456 gtk_box_pack_start(GTK_BOX(box), t->sbe.uri, TRUE, TRUE, 0);
7459 * gtk widgets cannot be added to a box twice. The sbe_* variables
7460 * make sure of this
7462 for (p = statusbar_elems; *p != '\0'; p++) {
7463 switch (*p) {
7464 case '|':
7465 #if GTK_CHECK_VERSION(3, 0, 0)
7466 sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
7467 #else
7468 sep = gtk_vseparator_new();
7469 gdk_color_parse(XT_COLOR_SB_SEPARATOR, &color);
7470 gtk_widget_modify_bg(sep, GTK_STATE_NORMAL, &color);
7471 #endif
7472 gtk_box_pack_start(GTK_BOX(box), sep, FALSE, FALSE, 0);
7473 break;
7474 case 'P':
7475 add_sbe(box, *p, &sbe_P, t->sbe.position);
7476 break;
7477 case 'B':
7478 add_sbe(box, *p, &sbe_B, t->sbe.buffercmd);
7479 break;
7480 case 'Z':
7481 add_sbe(box, *p, &sbe_Z, t->sbe.zoom);
7482 break;
7483 case 'T':
7484 add_sbe(box, *p, &sbe_T, t->sbe.tabs);
7485 break;
7486 case 'p':
7487 if (add_sbe(box, *p, &sbe_p, t->sbe.proxy) == 0)
7488 if (proxy_uri)
7489 gtk_entry_set_text(
7490 GTK_ENTRY(t->sbe.proxy), "proxy");
7491 break;
7492 default:
7493 warnx("illegal flag \"%c\" in statusbar_elems\n", *p);
7494 break;
7498 gtk_box_pack_end(GTK_BOX(t->vbox), t->statusbar, FALSE, FALSE, 0);
7500 return (0);
7503 struct tab *
7504 create_new_tab(char *title, struct undo *u, int focus, int position)
7506 struct tab *t;
7507 int load = 1, id;
7508 GtkWidget *b, *bb;
7509 WebKitWebHistoryItem *item;
7510 GList *items;
7511 char *sv[3];
7512 #if !GTK_CHECK_VERSION(3, 0, 0)
7513 GdkColor color;
7514 #endif
7516 DNPRINTF(XT_D_TAB, "create_new_tab: title %s focus %d\n", title, focus);
7518 if (tabless && !TAILQ_EMPTY(&tabs)) {
7519 if (single_instance) {
7520 DNPRINTF(XT_D_TAB,
7521 "create_new_tab: new tab rejected\n");
7522 return (NULL);
7524 sv[0] = start_argv[0];
7525 sv[1] = title;
7526 sv[2] = (char *)NULL;
7527 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH,
7528 NULL, NULL, NULL, NULL))
7529 show_oops(NULL, "%s: could not spawn process",
7530 __func__);
7531 return (NULL);
7534 t = g_malloc0(sizeof *t);
7536 if (title == NULL) {
7537 title = "(untitled)";
7538 load = 0;
7541 #if GTK_CHECK_VERSION(3, 0, 0)
7542 t->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
7543 b = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7544 gtk_widget_set_name(t->vbox, "vbox");
7545 #else
7546 t->vbox = gtk_vbox_new(FALSE, 0);
7547 b = gtk_hbox_new(FALSE, 0);
7548 #endif
7549 gtk_widget_set_can_focus(t->vbox, FALSE);
7551 /* label + button for tab */
7552 t->tab_content = b;
7553 gtk_widget_set_can_focus(t->tab_content, FALSE);
7555 t->user_agent_id = 0;
7556 t->http_accept_id = 0;
7558 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7559 t->active = NULL;
7560 #endif
7562 #if GTK_CHECK_VERSION(2, 20, 0)
7563 t->spinner = gtk_spinner_new();
7564 #endif
7565 t->label = gtk_label_new(title);
7566 bb = create_button("Close", GTK_STOCK_CLOSE, 1);
7567 gtk_label_set_max_width_chars(GTK_LABEL(t->label), 20);
7568 gtk_label_set_ellipsize(GTK_LABEL(t->label), PANGO_ELLIPSIZE_END);
7569 gtk_label_set_line_wrap(GTK_LABEL(t->label), FALSE);
7570 gtk_widget_set_size_request(b, 130, 0);
7573 * this is a total hack and most likely breaks with other styles but
7574 * is necessary so the text doesn't bounce around when the spinner is
7575 * shown/hidden
7577 #if GTK_CHECK_VERSION(3, 0, 0)
7578 gtk_widget_set_size_request(t->label, 95, 0);
7579 #else
7580 gtk_widget_set_size_request(t->label, 100, 0);
7581 #endif
7583 gtk_box_pack_start(GTK_BOX(b), bb, FALSE, FALSE, 0);
7584 gtk_box_pack_start(GTK_BOX(b), t->label, FALSE, FALSE, 0);
7585 #if GTK_CHECK_VERSION(2, 20, 0)
7586 gtk_box_pack_end(GTK_BOX(b), t->spinner, FALSE, FALSE, 0);
7587 #endif
7589 /* toolbar */
7590 if (browser_mode == XT_BM_KIOSK) {
7591 t->toolbar = create_kiosk_toolbar(t);
7592 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7594 } else {
7595 t->toolbar = create_toolbar(t);
7596 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7600 /* marks */
7601 marks_clear(t);
7603 /* browser */
7604 t->browser_win = create_browser(t);
7605 set_scrollbar_visibility(t, show_scrollbars);
7606 gtk_box_pack_start(GTK_BOX(t->vbox), t->browser_win, TRUE, TRUE, 0);
7608 /* oops message for user feedback */
7609 t->oops = gtk_entry_new();
7610 gtk_entry_set_inner_border(GTK_ENTRY(t->oops), NULL);
7611 gtk_entry_set_has_frame(GTK_ENTRY(t->oops), FALSE);
7612 gtk_widget_set_can_focus(GTK_WIDGET(t->oops), FALSE);
7613 #if GTK_CHECK_VERSION(3, 0, 0)
7614 gtk_widget_set_name(t->oops, XT_CSS_RED);
7615 #else
7616 gdk_color_parse(XT_COLOR_RED, &color);
7617 gtk_widget_modify_base(t->oops, GTK_STATE_NORMAL, &color);
7618 #endif
7619 gtk_box_pack_end(GTK_BOX(t->vbox), t->oops, FALSE, FALSE, 0);
7620 gtk_widget_modify_font(GTK_WIDGET(t->oops), oops_font);
7622 /* command entry */
7623 t->cmd = gtk_entry_new();
7624 g_signal_connect(G_OBJECT(t->cmd), "focus-in-event",
7625 G_CALLBACK(entry_focus_cb), t);
7626 gtk_entry_set_inner_border(GTK_ENTRY(t->cmd), NULL);
7627 gtk_entry_set_has_frame(GTK_ENTRY(t->cmd), FALSE);
7628 gtk_box_pack_end(GTK_BOX(t->vbox), t->cmd, FALSE, FALSE, 0);
7629 gtk_widget_modify_font(GTK_WIDGET(t->cmd), cmd_font);
7631 /* status bar */
7632 statusbar_create(t);
7634 /* buffer list */
7635 t->buffers = create_buffers(t);
7636 gtk_box_pack_end(GTK_BOX(t->vbox), t->buffers, FALSE, FALSE, 0);
7638 /* xtp meaning is normal by default */
7639 set_normal_tab_meaning(t);
7641 /* set empty favicon */
7642 xt_icon_from_name(t, "text-html");
7644 /* and show it all */
7645 gtk_widget_show_all(b);
7646 gtk_widget_show_all(t->vbox);
7648 if (!fancy_bar) {
7649 gtk_widget_hide(t->backward);
7650 gtk_widget_hide(t->forward);
7651 gtk_widget_hide(t->stop);
7652 gtk_widget_hide(t->js_toggle);
7654 if (!fancy_bar || (search_string == NULL || strlen(search_string) == 0))
7655 gtk_widget_hide(t->search_entry);
7656 if (http_proxy == NULL && http_proxy_save == NULL)
7657 gtk_widget_hide(t->proxy_toggle);
7659 /* compact tab bar */
7660 t->tab_elems.label = gtk_label_new(title);
7661 t->tab_elems.favicon = gtk_image_new();
7663 t->tab_elems.eventbox = gtk_event_box_new();
7664 gtk_widget_set_name(t->tab_elems.eventbox, "compact_tab");
7665 #if GTK_CHECK_VERSION(3, 0, 0)
7666 t->tab_elems.box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7668 gtk_label_set_ellipsize(GTK_LABEL(t->tab_elems.label),
7669 PANGO_ELLIPSIZE_END);
7670 gtk_widget_override_font(t->tab_elems.label, tabbar_font);
7671 gtk_widget_set_halign(t->tab_elems.label, GTK_ALIGN_START);
7672 gtk_widget_set_valign(t->tab_elems.label, GTK_ALIGN_START);
7673 #else
7674 t->tab_elems.box = gtk_hbox_new(FALSE, 0);
7676 gtk_label_set_width_chars(GTK_LABEL(t->tab_elems.label), 1);
7677 gtk_misc_set_alignment(GTK_MISC(t->tab_elems.label), 0.0, 0.0);
7678 gtk_misc_set_padding(GTK_MISC(t->tab_elems.label), 4.0, 4.0);
7679 gtk_widget_modify_font(GTK_WIDGET(t->tab_elems.label), tabbar_font);
7681 gdk_color_parse(XT_COLOR_CT_BACKGROUND, &color);
7682 gtk_widget_modify_bg(t->tab_elems.eventbox, GTK_STATE_NORMAL, &color);
7683 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color);
7684 gtk_widget_modify_fg(t->tab_elems.label, GTK_STATE_NORMAL, &color);
7685 #endif
7687 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.favicon, FALSE,
7688 FALSE, 0);
7689 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.label, TRUE,
7690 TRUE, 0);
7691 gtk_container_add(GTK_CONTAINER(t->tab_elems.eventbox),
7692 t->tab_elems.box);
7694 gtk_box_pack_start(GTK_BOX(tab_bar_box), t->tab_elems.eventbox, TRUE,
7695 TRUE, 0);
7696 gtk_widget_show_all(t->tab_elems.eventbox);
7698 if (append_next == 0 || gtk_notebook_get_n_pages(notebook) == 0)
7699 append_tab(t);
7700 else {
7701 id = position >= 0 ? position :
7702 gtk_notebook_get_current_page(notebook) + 1;
7703 if (id > gtk_notebook_get_n_pages(notebook))
7704 append_tab(t);
7705 else {
7706 TAILQ_INSERT_TAIL(&tabs, t, entry);
7707 gtk_notebook_insert_page(notebook, t->vbox, b, id);
7708 gtk_box_reorder_child(GTK_BOX(tab_bar_box),
7709 t->tab_elems.eventbox, id);
7710 recalc_tabs();
7714 #if GTK_CHECK_VERSION(2, 20, 0)
7715 /* turn spinner off if we are a new tab without uri */
7716 if (!load) {
7717 gtk_spinner_stop(GTK_SPINNER(t->spinner));
7718 gtk_widget_hide(t->spinner);
7720 #endif
7721 /* make notebook tabs reorderable */
7722 gtk_notebook_set_tab_reorderable(notebook, t->vbox, TRUE);
7724 /* compact tabs clickable */
7725 g_signal_connect(G_OBJECT(t->tab_elems.eventbox),
7726 "button_press_event", G_CALLBACK(tab_clicked_cb), t);
7728 g_object_connect(G_OBJECT(t->cmd),
7729 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7730 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb), t,
7731 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7732 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb), t,
7733 "signal::activate", G_CALLBACK(cmd_activate_cb), t,
7734 "signal::populate-popup", G_CALLBACK(cmd_popup_cb), t,
7735 "signal::hide", G_CALLBACK(cmd_hide_cb), t,
7736 (char *)NULL);
7738 /* reuse wv_button_cb to hide oops */
7739 g_object_connect(G_OBJECT(t->oops),
7740 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7741 (char *)NULL);
7743 g_signal_connect(t->buffers,
7744 "row-activated", G_CALLBACK(row_activated_cb), t);
7745 g_object_connect(G_OBJECT(t->buffers),
7746 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t, (char *)NULL);
7748 g_object_connect(G_OBJECT(t->wv),
7749 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t,
7750 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb), t,
7751 "signal::download-requested", G_CALLBACK(webview_download_cb), t,
7752 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb), t,
7753 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7754 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7755 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb), t,
7756 "signal::create-web-view", G_CALLBACK(webview_cwv_cb), t,
7757 "signal::close-web-view", G_CALLBACK(webview_closewv_cb), t,
7758 "signal::event", G_CALLBACK(webview_event_cb), t,
7759 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb), t,
7760 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7761 "signal::button_release_event", G_CALLBACK(wv_release_button_cb), t,
7762 "signal::populate-popup", G_CALLBACK(wv_popup_cb), t,
7763 (char *)NULL);
7764 g_signal_connect(t->wv,
7765 "notify::load-status", G_CALLBACK(notify_load_status_cb), t);
7766 g_signal_connect(t->wv,
7767 "notify::title", G_CALLBACK(notify_title_cb), t);
7768 t->progress_handle = g_signal_connect(t->wv,
7769 "notify::progress", G_CALLBACK(webview_progress_changed_cb), t);
7771 /* hijack the unused keys as if we were the browser */
7772 //g_object_connect(G_OBJECT(t->toolbar),
7773 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7774 // (char *)NULL);
7776 g_signal_connect(G_OBJECT(bb), "button_press_event",
7777 G_CALLBACK(tab_close_cb), t);
7779 /* setup history */
7780 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7781 /* restore the tab's history */
7782 if (u && u->history) {
7783 items = u->history;
7784 while (items) {
7785 item = items->data;
7786 webkit_web_back_forward_list_add_item(t->bfl, item);
7787 items = g_list_next(items);
7790 item = g_list_nth_data(u->history, u->back);
7791 if (item)
7792 webkit_web_view_go_to_back_forward_item(t->wv, item);
7794 g_list_free(items);
7795 g_list_free(u->history);
7796 } else
7797 webkit_web_back_forward_list_clear(t->bfl);
7799 /* check and show url and statusbar */
7800 url_set_visibility();
7801 statusbar_set_visibility();
7803 if (focus) {
7804 set_current_tab(t->tab_id);
7805 DNPRINTF(XT_D_TAB, "create_new_tab: going to tab: %d\n",
7806 t->tab_id);
7808 if (load) {
7809 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), title);
7810 load_uri(t, title);
7811 } else {
7812 if (show_url == 1)
7813 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
7814 else
7815 focus_webview(t);
7817 } else if (load)
7818 load_uri(t, title);
7820 if (userstyle_global)
7821 apply_style(t);
7823 recolor_compact_tabs();
7824 setzoom_webkit(t, XT_ZOOM_NORMAL);
7825 return (t);
7828 void
7829 notebook_switchpage_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7830 gpointer *udata)
7832 struct tab *t;
7833 const gchar *uri;
7835 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: tab: %d\n", pn);
7837 if (gtk_notebook_get_current_page(notebook) == -1)
7838 recalc_tabs();
7840 TAILQ_FOREACH(t, &tabs, entry) {
7841 if (t->tab_id == pn) {
7842 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: going to "
7843 "%d\n", pn);
7845 uri = get_title(t, TRUE);
7846 gtk_window_set_title(GTK_WINDOW(main_window), uri);
7848 hide_cmd(t);
7849 hide_oops(t);
7850 hide_buffers(t);
7852 if (t->focus_wv) {
7853 /* can't use focus_webview here */
7854 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7856 update_statusbar_tabs(t);
7857 break;
7862 void
7863 notebook_pagereordered_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7864 gpointer *udata)
7866 struct tab *t = NULL, *tt;
7868 recalc_tabs();
7870 TAILQ_FOREACH(tt, &tabs, entry)
7871 if (tt->tab_id == pn) {
7872 t = tt;
7873 break;
7875 if (t == NULL)
7876 return;
7877 DNPRINTF(XT_D_TAB, "page_reordered_cb: tab: %d\n", t->tab_id);
7879 gtk_box_reorder_child(GTK_BOX(tab_bar_box), t->tab_elems.eventbox,
7880 t->tab_id);
7882 update_statusbar_tabs(t);
7885 void
7886 menuitem_response(struct tab *t)
7888 gtk_notebook_set_current_page(notebook, t->tab_id);
7892 destroy_menu(GtkMenuShell *m, void *notused)
7894 gtk_widget_destroy(GTK_WIDGET(m));
7895 return (XT_CB_PASSTHROUGH);
7898 gboolean
7899 arrow_cb(GtkWidget *w, GdkEventButton *event, gpointer user_data)
7901 GtkWidget *menu = NULL, *menu_items;
7902 GdkEventButton *bevent;
7903 struct tab **stabs = NULL;
7904 int i, num_tabs;
7905 const gchar *uri;
7907 if (event->type == GDK_BUTTON_PRESS) {
7908 bevent = (GdkEventButton *) event;
7909 menu = gtk_menu_new();
7911 num_tabs = sort_tabs_by_page_num(&stabs);
7912 for (i = 0; i < num_tabs; ++i) {
7913 if (stabs[i] == NULL)
7914 continue;
7915 if ((uri = get_uri(stabs[i])) == NULL)
7916 /* XXX make sure there is something to print */
7917 /* XXX add gui pages in here to look purdy */
7918 uri = "(untitled)";
7919 menu_items = gtk_menu_item_new_with_label(uri);
7920 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_items);
7921 gtk_widget_show(menu_items);
7923 g_signal_connect_swapped(menu_items,
7924 "activate", G_CALLBACK(menuitem_response),
7925 (gpointer)stabs[i]);
7927 g_free(stabs);
7929 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
7930 bevent->button, bevent->time);
7932 g_object_connect(G_OBJECT(menu),
7933 "signal::selection-done", G_CALLBACK(destroy_menu), NULL,
7934 (char *)NULL);
7936 return (TRUE /* eat event */);
7939 return (FALSE /* propagate */);
7943 icon_size_map(int iconsz)
7945 if (iconsz <= GTK_ICON_SIZE_INVALID ||
7946 iconsz > GTK_ICON_SIZE_DIALOG)
7947 return (GTK_ICON_SIZE_SMALL_TOOLBAR);
7949 return (iconsz);
7952 GtkWidget *
7953 create_button(char *name, char *stockid, int size)
7955 GtkWidget *button, *image;
7956 int gtk_icon_size;
7957 #if !GTK_CHECK_VERSION(3, 0, 0)
7958 gchar *newstyle;
7959 #endif
7961 #if !GTK_CHECK_VERSION(3, 0, 0)
7962 newstyle = g_strdup_printf(
7963 "style \"%s-style\"\n"
7964 "{\n"
7965 " GtkWidget::focus-padding = 0\n"
7966 " GtkWidget::focus-line-width = 0\n"
7967 " xthickness = 0\n"
7968 " ythickness = 0\n"
7969 "}\n"
7970 "widget \"*.%s\" style \"%s-style\"", name, name, name);
7971 gtk_rc_parse_string(newstyle);
7972 g_free(newstyle);
7973 #endif
7974 button = gtk_button_new();
7975 gtk_widget_set_can_focus(button, FALSE);
7976 gtk_button_set_focus_on_click(GTK_BUTTON(button), FALSE);
7977 gtk_icon_size = icon_size_map(size ? size : icon_size);
7979 image = gtk_image_new_from_stock(stockid, gtk_icon_size);
7980 gtk_container_set_border_width(GTK_CONTAINER(button), 1);
7981 gtk_container_add(GTK_CONTAINER(button), GTK_WIDGET(image));
7982 gtk_widget_set_name(button, name);
7983 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
7985 return (button);
7988 void
7989 button_set_file(GtkWidget *button, char *filename)
7991 GtkWidget *image;
7992 char file[PATH_MAX];
7994 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, filename);
7995 image = gtk_image_new_from_file(file);
7996 gtk_button_set_image(GTK_BUTTON(button), image);
7999 void
8000 button_set_stockid(GtkWidget *button, char *stockid)
8002 GtkWidget *image;
8004 image = gtk_image_new_from_stock(stockid, icon_size_map(icon_size));
8005 gtk_button_set_image(GTK_BUTTON(button), image);
8008 void
8009 create_canvas(void)
8011 GtkWidget *vbox;
8012 GList *l = NULL;
8013 GdkPixbuf *pb;
8014 char file[PATH_MAX];
8015 int i;
8016 #if !GTK_CHECK_VERSION(3, 0, 0)
8017 GdkColor color;
8018 #endif
8020 #if GTK_CHECK_VERSION(3, 0, 0)
8021 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
8022 #else
8023 vbox = gtk_vbox_new(FALSE, 0);
8024 #endif
8025 gtk_box_set_spacing(GTK_BOX(vbox), 0);
8026 gtk_widget_set_can_focus(vbox, FALSE);
8027 notebook = GTK_NOTEBOOK(gtk_notebook_new());
8028 #if !GTK_CHECK_VERSION(3, 0, 0)
8029 /* XXX seems to be needed with gtk+2 */
8030 g_object_set(G_OBJECT(notebook), "tab-border", 0, NULL);
8031 #endif
8032 gtk_notebook_set_scrollable(notebook, TRUE);
8033 gtk_notebook_set_show_border(notebook, FALSE);
8034 gtk_widget_set_can_focus(GTK_WIDGET(notebook), FALSE);
8036 abtn = gtk_button_new();
8037 gtk_widget_set_can_focus(abtn, FALSE);
8038 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
8039 gtk_widget_set_name(abtn, "Arrow");
8040 gtk_container_add(GTK_CONTAINER(abtn), arrow);
8041 gtk_widget_set_size_request(abtn, -1, 20);
8043 #if GTK_CHECK_VERSION(2, 20, 0)
8044 gtk_notebook_set_action_widget(notebook, abtn, GTK_PACK_END);
8045 #endif
8046 /* compact tab bar */
8047 tab_bar = gtk_event_box_new();
8048 #if GTK_CHECK_VERSION(3, 0, 0)
8049 gtk_widget_set_name(tab_bar, "tab_bar");
8050 tab_bar_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
8051 #else
8052 gdk_color_parse(XT_COLOR_CT_SEPARATOR, &color);
8053 gtk_widget_modify_bg(tab_bar, GTK_STATE_NORMAL, &color);
8054 tab_bar_box = gtk_hbox_new(TRUE, 0);
8055 #endif
8056 gtk_container_add(GTK_CONTAINER(tab_bar), tab_bar_box);
8057 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box), TRUE);
8058 gtk_box_set_spacing(GTK_BOX(tab_bar_box), 2);
8060 gtk_box_pack_start(GTK_BOX(vbox), tab_bar, FALSE, FALSE, 0);
8061 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(notebook), TRUE, TRUE, 0);
8063 g_object_connect(G_OBJECT(notebook),
8064 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb), NULL,
8065 (char *)NULL);
8066 g_object_connect(G_OBJECT(notebook),
8067 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb),
8068 NULL, (char *)NULL);
8069 g_signal_connect(G_OBJECT(abtn), "button_press_event",
8070 G_CALLBACK(arrow_cb), NULL);
8072 main_window = create_window("xombrero");
8073 gtk_container_add(GTK_CONTAINER(main_window), vbox);
8074 g_signal_connect(G_OBJECT(main_window), "delete_event",
8075 G_CALLBACK(gtk_main_quit), NULL);
8076 #if GTK_CHECK_VERSION(3, 0, 0)
8077 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window), FALSE);
8078 #endif
8080 /* icons */
8081 for (i = 0; i < LENGTH(icons); i++) {
8082 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, icons[i]);
8083 pb = gdk_pixbuf_new_from_file(file, NULL);
8084 l = g_list_append(l, pb);
8086 gtk_window_set_default_icon_list(l);
8088 gtk_widget_show_all(abtn);
8089 gtk_widget_show_all(main_window);
8090 notebook_tab_set_visibility();
8093 #ifndef XT_SOCKET_DISABLE
8095 send_cmd_to_socket(char *cmd)
8097 int s, len, rv = 1;
8098 struct sockaddr_un sa;
8100 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8101 warnx("%s: socket", __func__);
8102 return (rv);
8105 sa.sun_family = AF_UNIX;
8106 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8107 work_dir, XT_SOCKET_FILE);
8108 len = SUN_LEN(&sa);
8110 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8111 warnx("%s: connect", __func__);
8112 goto done;
8115 if (send(s, cmd, strlen(cmd) + 1, 0) == -1) {
8116 warnx("%s: send", __func__);
8117 goto done;
8120 rv = 0;
8121 done:
8122 close(s);
8123 return (rv);
8126 gboolean
8127 socket_watcher(GIOChannel *source, GIOCondition condition, gpointer data)
8129 int s, n;
8130 char str[XT_MAX_URL_LENGTH];
8131 socklen_t t = sizeof(struct sockaddr_un);
8132 struct sockaddr_un sa;
8133 struct passwd *p;
8134 uid_t uid;
8135 gid_t gid;
8136 struct tab *tt;
8137 gint fd = g_io_channel_unix_get_fd(source);
8139 if ((s = accept(fd, (struct sockaddr *)&sa, &t)) == -1) {
8140 warn("accept");
8141 return (FALSE);
8144 if (getpeereid(s, &uid, &gid) == -1) {
8145 warn("getpeereid");
8146 return (FALSE);
8148 if (uid != getuid() || gid != getgid()) {
8149 warnx("unauthorized user");
8150 return (FALSE);
8153 p = getpwuid(uid);
8154 if (p == NULL) {
8155 warnx("not a valid user");
8156 return (FALSE);
8159 n = recv(s, str, sizeof(str), 0);
8160 if (n <= 0)
8161 return (TRUE);
8163 tt = get_current_tab();
8164 cmd_execute(tt, str);
8165 return (TRUE);
8169 is_running(void)
8171 int s, len, rv = 1;
8172 struct sockaddr_un sa;
8174 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8175 warn("is_running: socket");
8176 return (-1);
8179 sa.sun_family = AF_UNIX;
8180 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8181 work_dir, XT_SOCKET_FILE);
8182 len = SUN_LEN(&sa);
8184 /* connect to see if there is a listener */
8185 if (connect(s, (struct sockaddr *)&sa, len) == -1)
8186 rv = 0; /* not running */
8187 else
8188 rv = 1; /* already running */
8190 close(s);
8192 return (rv);
8196 build_socket(void)
8198 int s, len;
8199 struct sockaddr_un sa;
8201 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8202 warn("build_socket: socket");
8203 return (-1);
8206 sa.sun_family = AF_UNIX;
8207 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8208 work_dir, XT_SOCKET_FILE);
8209 len = SUN_LEN(&sa);
8211 /* connect to see if there is a listener */
8212 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8213 /* no listener so we will */
8214 unlink(sa.sun_path);
8216 if (bind(s, (struct sockaddr *)&sa, len) == -1) {
8217 warn("build_socket: bind");
8218 goto done;
8221 if (listen(s, 1) == -1) {
8222 warn("build_socket: listen");
8223 goto done;
8226 return (s);
8229 done:
8230 close(s);
8231 return (-1);
8233 #endif
8235 void
8236 xxx_dir(char *dir)
8238 struct stat sb;
8240 if (stat(dir, &sb)) {
8241 #if defined __MINGW32__
8242 if (mkdir(dir) == -1)
8243 #else
8244 if (mkdir(dir, S_IRWXU) == -1)
8245 #endif
8246 err(1, "mkdir %s", dir);
8247 if (stat(dir, &sb))
8248 err(1, "stat %s", dir);
8250 if (S_ISDIR(sb.st_mode) == 0)
8251 errx(1, "%s not a dir", dir);
8252 #if !defined __MINGW32__
8253 if (((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) != S_IRWXU) {
8254 warnx("fixing invalid permissions on %s", dir);
8255 if (chmod(dir, S_IRWXU) == -1)
8256 err(1, "chmod %s", dir);
8258 #endif
8261 void
8262 usage(void)
8264 fprintf(stderr,
8265 "%s [-nSTVt][-f file][-s session] url ...\n", __progname);
8266 exit(0);
8269 #if GTK_CHECK_VERSION(3, 0, 0)
8270 void
8271 setup_css(void)
8273 GtkCssProvider *provider;
8274 GdkDisplay *display;
8275 GdkScreen *screen;
8276 GFile *file;
8277 char path[PATH_MAX];
8278 #if defined __MINGW32__
8279 GtkCssProvider *windows_hacks;
8280 #endif
8282 provider = gtk_css_provider_new();
8283 display = gdk_display_get_default();
8284 screen = gdk_display_get_default_screen(display);
8285 snprintf(path, sizeof path, "%s" PS "%s", resource_dir, XT_CSS_FILE);
8286 file = g_file_new_for_path(path);
8287 gtk_css_provider_load_from_file(provider, file, NULL);
8288 gtk_style_context_add_provider_for_screen(screen,
8289 GTK_STYLE_PROVIDER(provider),
8290 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8291 #if defined __MINGW32__
8292 windows_hacks = gtk_css_provider_new();
8293 gtk_css_provider_load_from_data(windows_hacks,
8294 ".notebook tab {\n"
8295 " border-width: 0px;\n"
8296 "}", -1, NULL);
8297 gtk_style_context_add_provider_for_screen(screen,
8298 GTK_STYLE_PROVIDER(windows_hacks),
8299 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8300 #endif
8301 g_object_unref(file);
8303 #endif
8305 void
8306 welcome(void)
8308 startpage_add("<b>Welcome to xombrero %s!</b><p>", version);
8309 startpage_add("Details at "
8310 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8311 "wiki page</a><p>");
8315 main(int argc, char **argv)
8317 struct stat sb;
8318 int c, optn = 0, opte = 0, focus = 1;
8319 char conf[PATH_MAX] = { '\0' };
8320 char file[PATH_MAX];
8321 char sodversion[32];
8322 char *env_proxy = NULL;
8323 char *path;
8324 FILE *f = NULL;
8325 struct karg a;
8327 start_argv = (char * const *)argv;
8329 if (os_init)
8330 os_init();
8332 /* prepare gtk */
8333 gtk_init(&argc, &argv);
8335 gnutls_global_init();
8337 strlcpy(named_session, XT_SAVED_TABS_FILE, sizeof named_session);
8339 RB_INIT(&hl);
8340 RB_INIT(&downloads);
8341 RB_INIT(&st_tree);
8342 RB_INIT(&svl);
8343 RB_INIT(&ua_list);
8344 RB_INIT(&ha_list);
8345 RB_INIT(&di_list);
8347 TAILQ_INIT(&sessions);
8348 TAILQ_INIT(&tabs);
8349 TAILQ_INIT(&mtl);
8350 TAILQ_INIT(&aliases);
8351 TAILQ_INIT(&undos);
8352 TAILQ_INIT(&kbl);
8353 TAILQ_INIT(&spl);
8354 TAILQ_INIT(&chl);
8355 TAILQ_INIT(&shl);
8356 TAILQ_INIT(&cul);
8357 TAILQ_INIT(&srl);
8358 TAILQ_INIT(&c_wl);
8359 TAILQ_INIT(&js_wl);
8360 TAILQ_INIT(&pl_wl);
8361 TAILQ_INIT(&force_https);
8362 TAILQ_INIT(&svil);
8364 #ifndef XT_RESOURCE_LIMITS_DISABLE
8365 struct rlimit rlp;
8366 char *cmd = NULL;
8367 int s;
8368 GIOChannel *channel;
8370 /* fiddle with ulimits */
8371 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8372 warn("getrlimit");
8373 else {
8374 /* just use them all */
8375 #ifdef __APPLE__
8376 rlp.rlim_cur = OPEN_MAX;
8377 #else
8378 rlp.rlim_cur = rlp.rlim_max;
8379 #endif
8380 if (setrlimit(RLIMIT_NOFILE, &rlp) == -1)
8381 warn("setrlimit");
8382 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8383 warn("getrlimit");
8384 else if (rlp.rlim_cur < 1024)
8385 startpage_add("%s requires at least 1024 "
8386 "(2048 recommended) file " "descriptors, "
8387 "currently it has up to %d available",
8388 __progname, rlp.rlim_cur);
8390 #endif
8392 while ((c = getopt(argc, argv, "STVf:s:tne")) != -1) {
8393 switch (c) {
8394 case 'S':
8395 show_url = 0;
8396 break;
8397 case 'T':
8398 show_tabs = 0;
8399 break;
8400 case 'V':
8401 #ifdef XOMBRERO_BUILDSTR
8402 errx(0 , "Version: %s Build: %s",
8403 version, XOMBRERO_BUILDSTR);
8404 #else
8405 errx(0 , "Version: %s", version);
8406 #endif
8407 break;
8408 case 'f':
8409 strlcpy(conf, optarg, sizeof(conf));
8410 break;
8411 case 's':
8412 strlcpy(named_session, optarg, sizeof(named_session));
8413 break;
8414 case 't':
8415 tabless = 1;
8416 break;
8417 case 'n':
8418 optn = 1;
8419 break;
8420 case 'e':
8421 opte = 1;
8422 break;
8423 default:
8424 usage();
8425 /* NOTREACHED */
8428 argc -= optind;
8429 argv += optind;
8431 init_keybindings();
8433 pwd = getpwuid(getuid());
8434 if (pwd == NULL)
8435 errx(1, "invalid user %d", getuid());
8437 /* set download dir */
8438 if (strlen(download_dir) == 0)
8439 strlcpy(download_dir, pwd->pw_dir, sizeof download_dir);
8441 /* compile buffer command regexes */
8442 buffercmd_init();
8444 /* set default dynamic string settings */
8445 home = g_strdup(XT_DS_HOME);
8446 search_string = g_strdup(XT_DS_SEARCH_STRING);
8447 strlcpy(runtime_settings, "runtime", sizeof runtime_settings);
8448 cmd_font_name = g_strdup(XT_DS_CMD_FONT_NAME);
8449 oops_font_name = g_strdup(XT_DS_OOPS_FONT_NAME);
8450 statusbar_font_name = g_strdup(XT_DS_STATUSBAR_FONT_NAME);
8451 tabbar_font_name = g_strdup(XT_DS_TABBAR_FONT_NAME);
8452 statusbar_elems = g_strdup("BP");
8453 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8454 encoding = g_strdup(XT_DS_ENCODING);
8455 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8456 path = g_strdup_printf("%s" PS "style.css", resource_dir);
8457 userstyle = g_filename_to_uri(path, NULL, NULL);
8458 g_free(path);
8459 stylesheet = g_strdup(userstyle);
8461 /* set statically allocated (struct special) settings */
8462 if (strlen(default_script) == 0)
8463 expand_tilde(default_script, sizeof default_script,
8464 XT_DS_DEFAULT_SCRIPT);
8465 if (strlen(ssl_ca_file) == 0)
8466 expand_tilde(ssl_ca_file, sizeof ssl_ca_file,
8467 XT_DS_SSL_CA_FILE);
8469 /* soup session */
8470 session = webkit_get_default_session();
8472 /* read config file */
8473 if (strlen(conf) == 0)
8474 snprintf(conf, sizeof conf, "%s" PS ".%s",
8475 pwd->pw_dir, XT_CONF_FILE);
8476 config_parse(conf, 0);
8478 /* read preloaded HSTS list */
8479 if (preload_strict_transport) {
8480 snprintf(conf, sizeof conf, "%s" PS "%s",
8481 resource_dir, XT_HSTS_PRELOAD_FILE);
8482 config_parse(conf, 0);
8485 /* check whether to read in a crapton of additional http headers */
8486 if (anonymize_headers) {
8487 snprintf(conf, sizeof conf, "%s" PS "%s",
8488 resource_dir, XT_USER_AGENT_FILE);
8489 config_parse(conf, 0);
8490 snprintf(conf, sizeof conf, "%s" PS "%s",
8491 resource_dir, XT_HTTP_ACCEPT_FILE);
8492 config_parse(conf, 0);
8495 /* init fonts */
8496 cmd_font = pango_font_description_from_string(cmd_font_name);
8497 oops_font = pango_font_description_from_string(oops_font_name);
8498 statusbar_font = pango_font_description_from_string(statusbar_font_name);
8499 tabbar_font = pango_font_description_from_string(tabbar_font_name);
8501 /* working directory */
8502 if (strlen(work_dir) == 0)
8503 snprintf(work_dir, sizeof work_dir, "%s" PS "%s",
8504 pwd->pw_dir, XT_DIR);
8505 xxx_dir(work_dir);
8507 /* icon cache dir */
8508 snprintf(cache_dir, sizeof cache_dir, "%s" PS "%s", work_dir, XT_CACHE_DIR);
8509 xxx_dir(cache_dir);
8511 /* certs dir */
8512 snprintf(certs_dir, sizeof certs_dir, "%s" PS "%s", work_dir, XT_CERT_DIR);
8513 xxx_dir(certs_dir);
8515 /* cert changes dir */
8516 snprintf(certs_cache_dir, sizeof certs_cache_dir, "%s" PS "%s",
8517 work_dir, XT_CERT_CACHE_DIR);
8518 xxx_dir(certs_cache_dir);
8520 /* sessions dir */
8521 snprintf(sessions_dir, sizeof sessions_dir, "%s" PS "%s",
8522 work_dir, XT_SESSIONS_DIR);
8523 xxx_dir(sessions_dir);
8525 /* js dir */
8526 snprintf(js_dir, sizeof js_dir, "%s" PS "%s", work_dir, XT_JS_DIR);
8527 xxx_dir(js_dir);
8529 /* temp dir */
8530 snprintf(temp_dir, sizeof temp_dir, "%s" PS "%s", work_dir, XT_TEMP_DIR);
8531 xxx_dir(temp_dir);
8533 /* runtime settings that can override config file */
8534 if (runtime_settings[0] != '\0')
8535 config_parse(runtime_settings, 1);
8537 /* download dir */
8538 if (!strcmp(download_dir, pwd->pw_dir))
8539 strlcat(download_dir, PS "downloads", sizeof download_dir);
8540 xxx_dir(download_dir);
8542 /* first start file */
8543 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_SOD_FILE);
8544 if (stat(file, &sb)) {
8545 warnx("start of day file doesn't exist, creating it");
8546 if ((f = fopen(file, "w")) == NULL)
8547 err(1, "startofday");
8548 if (fputs(version, f) == EOF)
8549 err(1, "fputs");
8550 fclose(f);
8552 /* welcome user */
8553 welcome();
8554 } else {
8555 if ((f = fopen(file, "r+")) == NULL)
8556 err(1, "startofday");
8557 if (fgets(sodversion, sizeof sodversion, f) == NULL)
8558 err(1, "fgets");
8559 sodversion[strcspn(sodversion, "\n")] = '\0';
8560 if (strcmp(version, sodversion)) {
8561 if ((f = freopen(file, "w", f)) == NULL)
8562 err(1, "startofday");
8563 if (fputs(version, f) == EOF)
8564 err(1, "fputs");
8566 /* upgrade, say something smart */
8567 welcome();
8569 fclose(f);
8572 /* favorites file */
8573 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_FAVS_FILE);
8574 if (stat(file, &sb)) {
8575 warnx("favorites file doesn't exist, creating it");
8576 if ((f = fopen(file, "w")) == NULL)
8577 err(1, "favorites");
8578 fclose(f);
8581 /* quickmarks file */
8582 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
8583 if (stat(file, &sb)) {
8584 warnx("quickmarks file doesn't exist, creating it");
8585 if ((f = fopen(file, "w")) == NULL)
8586 err(1, "quickmarks");
8587 fclose(f);
8590 /* search history */
8591 if (history_autosave) {
8592 snprintf(search_file, sizeof search_file, "%s" PS "%s",
8593 work_dir, XT_SEARCH_FILE);
8594 if (stat(search_file, &sb)) {
8595 warnx("search history file doesn't exist, creating it");
8596 if ((f = fopen(search_file, "w")) == NULL)
8597 err(1, "search_history");
8598 fclose(f);
8600 history_read(&shl, search_file, &search_history_count);
8603 /* command history */
8604 if (history_autosave) {
8605 snprintf(command_file, sizeof command_file, "%s" PS "%s",
8606 work_dir, XT_COMMAND_FILE);
8607 if (stat(command_file, &sb)) {
8608 warnx("command history file doesn't exist, creating it");
8609 if ((f = fopen(command_file, "w")) == NULL)
8610 err(1, "command_history");
8611 fclose(f);
8613 history_read(&chl, command_file, &cmd_history_count);
8616 /* cookies */
8617 setup_cookies();
8619 /* guess_search regex */
8620 if (url_regex == NULL)
8621 url_regex = g_strdup(XT_URL_REGEX);
8622 if (url_regex)
8623 if (regcomp(&url_re, url_regex, REG_EXTENDED | REG_NOSUB))
8624 startpage_add("invalid url regex %s", url_regex);
8626 /* proxy */
8627 env_proxy = getenv("http_proxy");
8628 if (env_proxy)
8629 setup_proxy(env_proxy);
8630 else {
8631 env_proxy = getenv("HTTP_PROXY");
8632 if (env_proxy)
8633 setup_proxy(env_proxy);
8634 else
8635 setup_proxy(http_proxy);
8638 /* the user can optionally have the proxy disabled at startup */
8639 if ((http_proxy_starts_enabled == 0) && (http_proxy != NULL)) {
8640 http_proxy_save = g_strdup(http_proxy);
8641 setup_proxy(NULL);
8644 #ifndef XT_SOCKET_DISABLE
8645 if (opte) {
8646 send_cmd_to_socket(argv[0]);
8647 exit(0);
8649 #else
8650 opte = opte; /* shut mingw up */
8651 #endif
8652 /* set some connection parameters */
8653 g_object_set(session, "max-conns", max_connections, (char *)NULL);
8654 g_object_set(session, "max-conns-per-host", max_host_connections,
8655 (char *)NULL);
8656 g_object_set(session, SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
8657 (char *)NULL);
8659 g_signal_connect(session, "request-queued", G_CALLBACK(session_rq_cb),
8660 NULL);
8662 #ifndef XT_SOCKET_DISABLE
8663 /* see if there is already a xombrero running */
8664 if (single_instance && is_running()) {
8665 optn = 1;
8666 warnx("already running");
8669 if (optn) {
8670 while (argc) {
8671 cmd = g_strdup_printf("%s %s", "tabnew", argv[0]);
8672 send_cmd_to_socket(cmd);
8673 if (cmd)
8674 g_free(cmd);
8676 argc--;
8677 argv++;
8679 exit(0);
8681 #else
8682 optn = optn; /* shut mingw up */
8683 #endif
8684 /* tld list */
8685 tld_tree_init();
8687 if (enable_strict_transport)
8688 strict_transport_init();
8690 /* uri completion */
8691 completion_model = gtk_list_store_new(1, G_TYPE_STRING);
8693 /* buffers */
8694 buffers_store = gtk_list_store_new
8695 (NUM_COLS, G_TYPE_UINT, GDK_TYPE_PIXBUF, G_TYPE_STRING);
8697 qmarks_load();
8699 /* go graphical */
8700 create_canvas();
8701 notebook_tab_set_visibility();
8703 if (save_global_history)
8704 restore_global_history();
8706 /* restore session list */
8707 restore_sessions_list();
8709 if (!strcmp(named_session, XT_SAVED_TABS_FILE))
8710 restore_saved_tabs();
8711 else {
8712 a.s = named_session;
8713 a.i = XT_SES_DONOTHING;
8714 open_tabs(NULL, &a);
8717 /* see if we have an exception */
8718 if (!TAILQ_EMPTY(&spl)) {
8719 create_new_tab("about:startpage", NULL, focus, -1);
8720 focus = 0;
8723 while (argc) {
8724 create_new_tab(argv[0], NULL, focus, -1);
8725 focus = 0;
8727 argc--;
8728 argv++;
8731 if (TAILQ_EMPTY(&tabs))
8732 create_new_tab(home, NULL, 1, -1);
8733 #ifndef XT_SOCKET_DISABLE
8734 if (enable_socket)
8735 if ((s = build_socket()) != -1) {
8736 channel = g_io_channel_unix_new(s);
8737 g_io_add_watch(channel, G_IO_IN, socket_watcher, NULL);
8739 #endif
8741 #if GTK_CHECK_VERSION(3, 0, 0)
8742 setup_css();
8743 #endif
8745 gtk_main();
8747 gnutls_global_deinit();
8749 if (url_regex)
8750 regfree(&url_re);
8752 return (0);