Match recent FreeBSD Makefile fix in dragonfly Makefile.
[xombrero.git] / xombrero.c
blobca5a471f66ad408b6b391c34761577c3be631657
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>
9 * Copyright (c) 2013 David Hill <dhill@mindcry.org>
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <xombrero.h>
25 #include "version.h"
27 char *version = XOMBRERO_VERSION;
29 #ifdef XT_DEBUG
30 uint32_t swm_debug = 0
31 | XT_D_MOVE
32 | XT_D_KEY
33 | XT_D_TAB
34 | XT_D_URL
35 | XT_D_CMD
36 | XT_D_NAV
37 | XT_D_DOWNLOAD
38 | XT_D_CONFIG
39 | XT_D_JS
40 | XT_D_FAVORITE
41 | XT_D_PRINTING
42 | XT_D_COOKIE
43 | XT_D_KEYBINDING
44 | XT_D_CLIP
45 | XT_D_BUFFERCMD
46 | XT_D_INSPECTOR
47 | XT_D_VISITED
48 | XT_D_HISTORY
50 #endif
52 char *icons[] = {
53 "xombreroicon16.png",
54 "xombreroicon32.png",
55 "xombreroicon48.png",
56 "xombreroicon64.png",
57 "xombreroicon128.png"
60 struct session {
61 TAILQ_ENTRY(session) entry;
62 const gchar *name;
64 TAILQ_HEAD(session_list, session);
66 struct undo {
67 TAILQ_ENTRY(undo) entry;
68 gchar *uri;
69 GList *history;
70 int back; /* Keeps track of how many back
71 * history items there are. */
73 TAILQ_HEAD(undo_tailq, undo);
75 struct command_entry {
76 char *line;
77 TAILQ_ENTRY(command_entry) entry;
79 TAILQ_HEAD(command_list, command_entry);
81 /* defines */
82 #define XT_CACHE_DIR ("cache")
83 #define XT_CERT_DIR ("certs")
84 #define XT_CERT_CACHE_DIR ("certs_cache")
85 #define XT_JS_DIR ("js")
86 #define XT_SESSIONS_DIR ("sessions")
87 #define XT_TEMP_DIR ("tmp")
88 #define XT_QMARKS_FILE ("quickmarks")
89 #define XT_SAVED_TABS_FILE ("main_session")
90 #define XT_RESTART_TABS_FILE ("restart_tabs")
91 #define XT_SOCKET_FILE ("socket")
92 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
93 #define XT_SEARCH_FILE ("search_history")
94 #define XT_COMMAND_FILE ("command_history")
95 #define XT_DLMAN_REFRESH "10"
96 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
97 #define XT_MAX_UNDO_CLOSE_TAB (32)
98 #define XT_PRINT_EXTRA_MARGIN 10
99 #define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
100 #define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
101 #define XT_MAX_CERTS (32)
103 /* colors */
104 #define XT_COLOR_RED "#cc0000"
105 #define XT_COLOR_YELLOW "#ffff66"
106 #define XT_COLOR_BLUE "lightblue"
107 #define XT_COLOR_GREEN "#99ff66"
108 #define XT_COLOR_WHITE "white"
109 #define XT_COLOR_BLACK "black"
111 #define XT_COLOR_CT_BACKGROUND "#000000"
112 #define XT_COLOR_CT_INACTIVE "#dddddd"
113 #define XT_COLOR_CT_ACTIVE "#bbbb00"
114 #define XT_COLOR_CT_SEPARATOR "#555555"
116 #define XT_COLOR_SB_SEPARATOR "#555555"
118 /* CSS element names */
119 #define XT_CSS_NORMAL ""
120 #define XT_CSS_RED "red"
121 #define XT_CSS_YELLOW "yellow"
122 #define XT_CSS_GREEN "green"
123 #define XT_CSS_BLUE "blue"
124 #define XT_CSS_HIDDEN "hidden"
125 #define XT_CSS_ACTIVE "active"
127 #define XT_PROTO_DELIM "://"
129 /* actions */
130 #define XT_MOVE_INVALID (0)
131 #define XT_MOVE_DOWN (1)
132 #define XT_MOVE_UP (2)
133 #define XT_MOVE_BOTTOM (3)
134 #define XT_MOVE_TOP (4)
135 #define XT_MOVE_PAGEDOWN (5)
136 #define XT_MOVE_PAGEUP (6)
137 #define XT_MOVE_HALFDOWN (7)
138 #define XT_MOVE_HALFUP (8)
139 #define XT_MOVE_LEFT (9)
140 #define XT_MOVE_FARLEFT (10)
141 #define XT_MOVE_RIGHT (11)
142 #define XT_MOVE_FARRIGHT (12)
143 #define XT_MOVE_PERCENT (13)
144 #define XT_MOVE_CENTER (14)
146 #define XT_QMARK_SET (0)
147 #define XT_QMARK_OPEN (1)
148 #define XT_QMARK_TAB (2)
150 #define XT_MARK_SET (0)
151 #define XT_MARK_GOTO (1)
153 #define XT_GO_UP_ROOT (999)
155 #define XT_NAV_INVALID (0)
156 #define XT_NAV_BACK (1)
157 #define XT_NAV_FORWARD (2)
158 #define XT_NAV_RELOAD (3)
159 #define XT_NAV_STOP (4)
161 #define XT_FOCUS_INVALID (0)
162 #define XT_FOCUS_URI (1)
163 #define XT_FOCUS_SEARCH (2)
165 #define XT_SEARCH_INVALID (0)
166 #define XT_SEARCH_NEXT (1)
167 #define XT_SEARCH_PREV (2)
169 #define XT_PASTE_CURRENT_TAB (0)
170 #define XT_PASTE_NEW_TAB (1)
172 #define XT_ZOOM_IN (-1)
173 #define XT_ZOOM_OUT (-2)
174 #define XT_ZOOM_NORMAL (100)
176 #define XT_SES_DONOTHING (0)
177 #define XT_SES_CLOSETABS (1)
179 #define XT_PREFIX (1<<0)
180 #define XT_USERARG (1<<1)
181 #define XT_URLARG (1<<2)
182 #define XT_INTARG (1<<3)
183 #define XT_SESSARG (1<<4)
184 #define XT_SETARG (1<<5)
186 #define XT_HINT_NEWTAB (1<<0)
188 #define XT_BUFCMD_SZ (8)
190 #define XT_EJS_SHOW (1<<0)
192 GtkWidget * create_button(const char *, const char *, int);
194 void recalc_tabs(void);
195 void recolor_compact_tabs(void);
196 void set_current_tab(int page_num);
197 gboolean update_statusbar_position(GtkAdjustment*, gpointer);
198 void marks_clear(struct tab *t);
200 /* globals */
201 extern char *__progname;
202 char * const *start_argv;
203 struct passwd *pwd;
204 GtkWidget *main_window;
205 GtkNotebook *notebook;
206 GtkWidget *tab_bar;
207 GtkWidget *tab_bar_box;
208 GtkWidget *arrow, *abtn;
209 GdkEvent *fevent = NULL;
210 struct tab_list tabs;
211 struct history_list hl;
212 int hl_purge_count = 0;
213 struct session_list sessions;
214 struct wl_list c_wl;
215 struct wl_list js_wl;
216 struct wl_list pl_wl;
217 struct wl_list force_https;
218 struct wl_list svil;
219 struct strict_transport_tree st_tree;
220 struct undo_tailq undos;
221 struct keybinding_list kbl;
222 struct sp_list spl;
223 struct user_agent_list ua_list;
224 struct http_accept_list ha_list;
225 struct domain_id_list di_list;
226 struct cmd_alias_list cal;
227 struct custom_uri_list cul;
228 struct command_list chl;
229 struct command_list shl;
230 struct command_entry *history_at;
231 struct command_entry *search_at;
232 struct secviolation_list svl;
233 struct set_reject_list srl;
234 int undo_count;
235 int cmd_history_count = 0;
236 int search_history_count = 0;
237 char *global_search;
238 uint64_t blocked_cookies = 0;
239 char named_session[PATH_MAX];
240 GtkListStore *completion_model;
241 GtkListStore *buffers_store;
242 char *stylesheet;
244 char *qmarks[XT_NOQMARKS];
245 int btn_down; /* M1 down in any wv */
246 regex_t url_re; /* guess_search regex */
248 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
249 int next_download_id = 1;
251 void xxx_dir(char *);
252 int icon_size_map(int);
253 void activate_uri_entry_cb(GtkWidget*, struct tab *);
255 void
256 history_delete(struct command_list *l, int *counter)
258 struct command_entry *c;
260 if (l == NULL || counter == NULL)
261 return;
263 c = TAILQ_LAST(l, command_list);
264 if (c == NULL)
265 return;
267 TAILQ_REMOVE(l, c, entry);
268 *counter -= 1;
269 g_free(c->line);
270 g_free(c);
273 void
274 history_add(struct command_list *list, char *file, char *l, int *counter)
276 struct command_entry *c;
277 FILE *f;
279 if (list == NULL || l == NULL || counter == NULL)
280 return;
282 /* don't add the same line */
283 c = TAILQ_FIRST(list);
284 if (c)
285 if (!strcmp(c->line + 1 /* skip space */, l))
286 return;
288 c = g_malloc0(sizeof *c);
289 c->line = g_strdup_printf(" %s", l);
291 *counter += 1;
292 TAILQ_INSERT_HEAD(list, c, entry);
294 if (*counter > 1000)
295 history_delete(list, counter);
297 if (history_autosave && file) {
298 f = fopen(file, "w");
299 if (f == NULL) {
300 show_oops(NULL, "couldn't write history %s", file);
301 return;
304 TAILQ_FOREACH_REVERSE(c, list, command_list, entry) {
305 c->line[0] = ' ';
306 fprintf(f, "%s\n", c->line);
309 fclose(f);
314 history_read(struct command_list *list, char *file, int *counter)
316 FILE *f;
317 char *s, line[65536];
319 if (list == NULL || file == NULL)
320 return (1);
322 f = fopen(file, "r");
323 if (f == NULL) {
324 startpage_add("couldn't open history file %s", file);
325 return (1);
328 for (;;) {
329 s = fgets(line, sizeof line, f);
330 if (s == NULL || feof(f) || ferror(f))
331 break;
332 if ((s = strchr(line, '\n')) == NULL) {
333 startpage_add("invalid history file %s", file);
334 fclose(f);
335 return (1);
337 *s = '\0';
339 history_add(list, NULL, line + 1, counter);
342 fclose(f);
344 return (0);
347 /* marks array storage. */
348 char
349 indextomark(int i)
351 if (i < 0 || i >= XT_NOMARKS)
352 return (0);
354 return XT_MARKS[i];
358 marktoindex(char m)
360 char *ret;
362 if ((ret = strchr(XT_MARKS, m)) != NULL)
363 return ret - XT_MARKS;
365 return (-1);
368 /* quickmarks array storage. */
369 char
370 indextoqmark(int i)
372 if (i < 0 || i >= XT_NOQMARKS)
373 return (0);
375 return XT_QMARKS[i];
379 qmarktoindex(char m)
381 char *ret;
383 if ((ret = strchr(XT_QMARKS, m)) != NULL)
384 return ret - XT_QMARKS;
386 return (-1);
390 is_g_object_setting(GObject *o, char *str)
392 guint n_props = 0, i;
393 GParamSpec **proplist;
394 int rv = 0;
396 if (!G_IS_OBJECT(o))
397 return (0);
399 proplist = g_object_class_list_properties(G_OBJECT_GET_CLASS(o),
400 &n_props);
402 for (i = 0; i < n_props; i++) {
403 if (! strcmp(proplist[i]->name, str)) {
404 rv = 1;
405 break;
409 g_free(proplist);
410 return (rv);
413 struct tab *
414 get_current_tab(void)
416 struct tab *t;
418 TAILQ_FOREACH(t, &tabs, entry) {
419 if (t->tab_id == gtk_notebook_get_current_page(notebook))
420 return (t);
423 warnx("%s: no current tab", __func__);
425 return (NULL);
429 set_ssl_ca_file(struct settings *s, char *file)
431 struct stat sb;
433 if (file == NULL || strlen(file) == 0)
434 return (-1);
435 if (stat(file, &sb)) {
436 warnx("no CA file: %s", file);
437 return (-1);
439 expand_tilde(ssl_ca_file, sizeof ssl_ca_file, file);
440 g_object_set(session,
441 SOUP_SESSION_SSL_CA_FILE, ssl_ca_file,
442 SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
443 (void *)NULL);
444 return (0);
447 void
448 set_status(struct tab *t, gchar *fmt, ...)
450 va_list ap;
451 gchar *status;
453 va_start(ap, fmt);
455 status = g_strdup_vprintf(fmt, ap);
457 gtk_entry_set_text(GTK_ENTRY(t->sbe.uri), status);
459 if (!t->status)
460 t->status = status;
461 else if (strcmp(t->status, status)) {
462 /* set new status */
463 g_free(t->status);
464 t->status = status;
465 } else
466 g_free(status);
468 va_end(ap);
471 void
472 hide_cmd(struct tab *t)
474 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
476 history_at = NULL; /* just in case */
477 search_at = NULL; /* just in case */
478 gtk_widget_set_can_focus(t->cmd, FALSE);
479 gtk_widget_hide(t->cmd);
482 void
483 show_cmd(struct tab *t, const char *s)
485 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
487 /* without this you can't middle click in t->cmd to paste */
488 gtk_entry_set_text(GTK_ENTRY(t->cmd), "");
490 history_at = NULL;
491 search_at = NULL;
492 gtk_widget_hide(t->oops);
493 gtk_widget_set_can_focus(t->cmd, TRUE);
494 gtk_widget_show(t->cmd);
496 gtk_widget_grab_focus(GTK_WIDGET(t->cmd));
497 #if GTK_CHECK_VERSION(3, 0, 0)
498 gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
499 #else
500 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
501 &t->default_style->base[GTK_STATE_NORMAL]);
502 #endif
503 gtk_entry_set_text(GTK_ENTRY(t->cmd), s);
504 gtk_editable_set_position(GTK_EDITABLE(t->cmd), -1);
507 void
508 hide_buffers(struct tab *t)
510 gtk_widget_hide(t->buffers);
511 gtk_widget_set_can_focus(t->buffers, FALSE);
512 gtk_list_store_clear(buffers_store);
515 enum {
516 COL_ID = 0,
517 COL_FAVICON,
518 COL_TITLE,
519 NUM_COLS
523 sort_tabs_by_page_num(struct tab ***stabs)
525 int num_tabs = 0;
526 struct tab *t;
528 num_tabs = gtk_notebook_get_n_pages(notebook);
530 *stabs = g_malloc0(num_tabs * sizeof(struct tab *));
532 TAILQ_FOREACH(t, &tabs, entry)
533 (*stabs)[gtk_notebook_page_num(notebook, t->vbox)] = t;
535 return (num_tabs);
538 void
539 buffers_make_list(void)
541 GtkTreeIter iter;
542 const gchar *title = NULL;
543 struct tab **stabs = NULL;
544 int i, num_tabs;
546 num_tabs = sort_tabs_by_page_num(&stabs);
548 for (i = 0; i < num_tabs; i++)
549 if (stabs[i]) {
550 gtk_list_store_append(buffers_store, &iter);
551 title = get_title(stabs[i], FALSE);
552 gtk_list_store_set(buffers_store, &iter,
553 COL_ID, i + 1, /* Enumerate the tabs starting from 1
554 * rather than 0. */
555 COL_FAVICON, gtk_image_get_pixbuf
556 (GTK_IMAGE(stabs[i]->tab_elems.favicon)),
557 COL_TITLE, title,
558 -1);
561 g_free(stabs);
564 void
565 show_buffers(struct tab *t)
567 GtkTreeIter iter;
568 GtkTreeSelection *sel;
569 GtkTreePath *path;
570 int index;
572 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)))
573 return;
575 buffers_make_list();
577 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(t->buffers));
578 index = gtk_notebook_get_current_page(notebook);
579 path = gtk_tree_path_new_from_indices(index, -1);
580 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter, path))
581 gtk_tree_selection_select_iter(sel, &iter);
582 gtk_tree_path_free(path);
584 gtk_widget_show(t->buffers);
585 gtk_widget_set_can_focus(t->buffers, TRUE);
586 gtk_widget_grab_focus(GTK_WIDGET(t->buffers));
589 void
590 toggle_buffers(struct tab *t)
592 if (gtk_widget_get_visible(t->buffers))
593 hide_buffers(t);
594 else
595 show_buffers(t);
599 buffers(struct tab *t, struct karg *args)
601 show_buffers(t);
603 return (0);
607 set_scrollbar_visibility(struct tab *t, int visible)
609 #if GTK_CHECK_VERSION(3, 0, 0)
610 GtkWidget *h_scrollbar, *v_scrollbar;
612 h_scrollbar = gtk_scrolled_window_get_hscrollbar(
613 GTK_SCROLLED_WINDOW(t->browser_win));
614 v_scrollbar = gtk_scrolled_window_get_vscrollbar(
615 GTK_SCROLLED_WINDOW(t->browser_win));
617 if (visible == 0) {
618 gtk_widget_set_name(h_scrollbar, XT_CSS_HIDDEN);
619 gtk_widget_set_name(v_scrollbar, XT_CSS_HIDDEN);
620 } else {
621 gtk_widget_set_name(h_scrollbar, "");
622 gtk_widget_set_name(v_scrollbar, "");
625 return (0);
626 #else
627 return (visible == 0);
628 #endif
631 void
632 hide_oops(struct tab *t)
634 gtk_widget_hide(t->oops);
637 void
638 show_oops(struct tab *at, const char *fmt, ...)
640 va_list ap;
641 char *msg = NULL;
642 struct tab *t = NULL;
644 if (fmt == NULL)
645 return;
647 if (at == NULL) {
648 if ((t = get_current_tab()) == NULL)
649 return;
650 } else
651 t = at;
653 va_start(ap, fmt);
654 if ((msg = g_strdup_vprintf(fmt, ap)) == NULL)
655 errx(1, "show_oops failed");
656 va_end(ap);
658 gtk_entry_set_text(GTK_ENTRY(t->oops), msg);
659 gtk_widget_hide(t->cmd);
660 gtk_widget_show(t->oops);
662 if (msg)
663 g_free(msg);
666 char work_dir[PATH_MAX];
667 char certs_dir[PATH_MAX];
668 char certs_cache_dir[PATH_MAX];
669 char js_dir[PATH_MAX];
670 char cache_dir[PATH_MAX];
671 char sessions_dir[PATH_MAX];
672 char temp_dir[PATH_MAX];
673 char cookie_file[PATH_MAX];
674 char *strict_transport_file = NULL;
675 SoupSession *session;
676 SoupCookieJar *s_cookiejar;
677 SoupCookieJar *p_cookiejar;
678 char rc_fname[PATH_MAX];
680 struct mime_type_list mtl;
681 struct alias_list aliases;
683 /* protos */
684 struct tab *create_new_tab(const char *, struct undo *, int, int);
685 void delete_tab(struct tab *);
686 void setzoom_webkit(struct tab *, int);
687 int download_rb_cmp(struct download *, struct download *);
688 gboolean cmd_execute(struct tab *t, char *str);
691 history_rb_cmp(struct history *h1, struct history *h2)
693 return (strcmp(h1->uri, h2->uri));
695 RB_GENERATE(history_list, history, entry, history_rb_cmp);
698 download_rb_cmp(struct download *e1, struct download *e2)
700 return (e1->id < e2->id ? -1 : e1->id > e2->id);
702 RB_GENERATE(download_list, download, entry, download_rb_cmp);
705 secviolation_rb_cmp(struct secviolation *s1, struct secviolation *s2)
707 return (s1->xtp_arg < s2->xtp_arg ? -1 : s1->xtp_arg > s2->xtp_arg);
709 RB_GENERATE(secviolation_list, secviolation, entry, secviolation_rb_cmp);
712 user_agent_rb_cmp(struct user_agent *ua1, struct user_agent *ua2)
714 return (ua1->id < ua2->id ? -1 : ua1->id > ua2->id);
716 RB_GENERATE(user_agent_list, user_agent, entry, user_agent_rb_cmp);
719 http_accept_rb_cmp(struct http_accept *ha1, struct http_accept *ha2)
721 return (ha1->id < ha2->id ? -1 : ha1->id > ha2->id);
723 RB_GENERATE(http_accept_list, http_accept, entry, http_accept_rb_cmp);
726 domain_id_rb_cmp(struct domain_id *d1, struct domain_id *d2)
728 return (strcmp(d1->domain, d2->domain));
730 RB_GENERATE(domain_id_list, domain_id, entry, domain_id_rb_cmp);
732 struct valid_url_types {
733 char *type;
734 } vut[] = {
735 { "http://" },
736 { "https://" },
737 { "ftp://" },
738 { "file://" },
739 { XT_URI_ABOUT },
740 { XT_XTP_STR },
744 valid_url_type(const char *url)
746 int i;
748 for (i = 0; i < LENGTH(vut); i++)
749 if (!strncasecmp(vut[i].type, url, strlen(vut[i].type)))
750 return (0);
752 return (1);
755 char *
756 match_alias(const char *url_in)
758 struct alias *a;
759 char *arg;
760 char *url_out = NULL, *search, *enc_arg;
761 char **sv;
763 search = g_strdup(url_in);
764 arg = search;
765 if (strsep(&arg, " \t") == NULL) {
766 show_oops(NULL, "match_alias: NULL URL");
767 goto done;
770 TAILQ_FOREACH(a, &aliases, entry) {
771 if (!strcmp(search, a->a_name))
772 break;
775 if (a != NULL) {
776 DNPRINTF(XT_D_URL, "match_alias: matched alias %s\n",
777 a->a_name);
778 if (arg == NULL)
779 arg = "";
780 enc_arg = soup_uri_encode(arg, XT_RESERVED_CHARS);
781 sv = g_strsplit(a->a_uri, "%s", 2);
782 if (arg != NULL)
783 url_out = g_strjoinv(enc_arg, sv);
784 else
785 url_out = g_strjoinv("", sv);
786 g_free(enc_arg);
787 g_strfreev(sv);
789 done:
790 g_free(search);
791 return (url_out);
794 char *
795 guess_url_type(const char *url_in)
797 struct stat sb;
798 char cwd[PATH_MAX] = {0};
799 char *url_out = NULL, *enc_search = NULL;
800 char *path = NULL;
801 char **sv = NULL;
802 int i;
805 /* substitute aliases */
806 url_out = match_alias(url_in);
807 if (url_out != NULL)
808 return (url_out);
810 /* see if we are an about page */
811 if (!strncmp(url_in, XT_URI_ABOUT, XT_URI_ABOUT_LEN))
812 for (i = 0; i < about_list_size(); i++)
813 if (!strcmp(&url_in[XT_URI_ABOUT_LEN],
814 about_list[i].name)) {
815 url_out = g_strdup(url_in);
816 goto done;
819 if (guess_search && url_regex &&
820 !(g_str_has_prefix(url_in, "http://") ||
821 g_str_has_prefix(url_in, "https://"))) {
822 if (regexec(&url_re, url_in, 0, NULL, 0)) {
823 /* invalid URI so search instead */
824 enc_search = soup_uri_encode(url_in, XT_RESERVED_CHARS);
825 sv = g_strsplit(search_string, "%s", 2);
826 url_out = g_strjoinv(enc_search, sv);
827 g_free(enc_search);
828 g_strfreev(sv);
829 goto done;
833 /* XXX not sure about this heuristic */
834 if (stat(url_in, &sb) == 0) {
835 if (url_in[0] == '/')
836 url_out = g_filename_to_uri(url_in, NULL, NULL);
837 else {
838 if (getcwd(cwd, PATH_MAX) != NULL) {
839 path = g_strdup_printf("%s" PS "%s", cwd,
840 url_in);
841 url_out = g_filename_to_uri(path, NULL, NULL);
842 g_free(path);
845 } else
846 url_out = g_strdup_printf("http://%s", url_in); /* guess http */
847 done:
848 DNPRINTF(XT_D_URL, "guess_url_type: guessed %s\n", url_out);
850 return (url_out);
853 void
854 set_normal_tab_meaning(struct tab *t)
856 if (t == NULL)
857 return;
859 t->xtp_meaning = XT_XTP_TAB_MEANING_NORMAL;
860 if (t->session_key != NULL) {
861 g_free(t->session_key);
862 t->session_key = NULL;
866 void
867 load_uri(struct tab *t, const gchar *uri)
869 struct karg args;
870 gchar *newuri = NULL;
871 int i;
873 if (uri == NULL)
874 return;
876 /* Strip leading spaces. */
877 while (*uri && isspace((unsigned char)*uri))
878 uri++;
880 if (strlen(uri) == 0) {
881 blank(t, NULL);
882 return;
885 set_normal_tab_meaning(t);
887 if (valid_url_type(uri)) {
888 if ((newuri = guess_url_type(uri)) != NULL)
889 uri = newuri;
890 else
891 uri = "";
894 /* clear :cert show host */
895 if (t->about_cert_host) {
896 g_free(t->about_cert_host);
897 t->about_cert_host = NULL;
900 if (!strncmp(uri, XT_URI_ABOUT, XT_URI_ABOUT_LEN)) {
901 for (i = 0; i < about_list_size(); i++)
902 if (!strcmp(&uri[XT_URI_ABOUT_LEN], about_list[i].name) &&
903 about_list[i].func != NULL) {
904 bzero(&args, sizeof args);
905 about_list[i].func(t, &args);
906 gtk_widget_set_sensitive(GTK_WIDGET(t->stop),
907 FALSE);
908 goto done;
910 show_oops(t, "invalid about page");
911 goto done;
914 /* remove old HTTPS cert chain (if any) */
915 if (t->pem) {
916 g_free(t->pem);
917 t->pem = NULL;
920 set_status(t, "Loading: %s", (char *)uri);
921 marks_clear(t);
922 webkit_web_view_load_uri(t->wv, uri);
923 done:
924 if (newuri)
925 g_free(newuri);
928 const gchar *
929 get_uri(struct tab *t)
931 const gchar *uri = NULL;
933 if (webkit_web_view_get_load_status(t->wv) == WEBKIT_LOAD_FAILED &&
934 !t->download_requested)
935 return (NULL);
936 if (t->xtp_meaning == XT_XTP_TAB_MEANING_NORMAL)
937 uri = webkit_web_view_get_uri(t->wv);
938 else {
939 /* use tmp_uri to make sure it is g_freed */
940 if (t->tmp_uri)
941 g_free(t->tmp_uri);
942 t->tmp_uri = g_strdup_printf("%s%s", XT_URI_ABOUT,
943 about_list[t->xtp_meaning].name);
944 uri = t->tmp_uri;
946 return (uri);
949 const gchar *
950 get_title(struct tab *t, bool window)
952 const gchar *set = NULL, *title = NULL;
953 WebKitLoadStatus status;
955 status = webkit_web_view_get_load_status(t->wv);
956 if (status == WEBKIT_LOAD_PROVISIONAL ||
957 (status == WEBKIT_LOAD_FAILED && !t->download_requested) ||
958 t->xtp_meaning == XT_XTP_TAB_MEANING_BL)
959 goto notitle;
961 title = webkit_web_view_get_title(t->wv);
962 if ((set = title ? title : get_uri(t)))
963 return set;
965 notitle:
966 set = window ? XT_NAME : "(untitled)";
968 return set;
971 struct mime_type *
972 find_mime_type(char *mime_type)
974 struct mime_type *m, *def = NULL, *rv = NULL;
976 TAILQ_FOREACH(m, &mtl, entry) {
977 if (m->mt_default &&
978 !strncmp(mime_type, m->mt_type, strlen(m->mt_type)))
979 def = m;
981 if (m->mt_default == 0 && !strcmp(mime_type, m->mt_type)) {
982 rv = m;
983 break;
987 if (rv == NULL)
988 rv = def;
990 return (rv);
994 * This only escapes the & and < characters, as per the discussion found here:
995 * http://lists.apple.com/archives/Webkitsdk-dev/2007/May/msg00056.html
997 gchar *
998 html_escape(const char *val)
1000 char *s, *sp;
1001 char **sv;
1003 if (val == NULL)
1004 return NULL;
1006 sv = g_strsplit(val, "&", -1);
1007 s = g_strjoinv("&amp;", sv);
1008 g_strfreev(sv);
1009 sp = s;
1010 sv = g_strsplit(val, "<", -1);
1011 s = g_strjoinv("&lt;", sv);
1012 g_strfreev(sv);
1013 g_free(sp);
1014 return (s);
1017 struct wl_entry *
1018 wl_find_uri(const gchar *s, struct wl_list *wl)
1020 int i;
1021 char *ss;
1022 struct wl_entry *w;
1024 if (s == NULL || wl == NULL)
1025 return (NULL);
1027 if (!strncmp(s, "http://", strlen("http://")))
1028 s = &s[strlen("http://")];
1029 else if (!strncmp(s, "https://", strlen("https://")))
1030 s = &s[strlen("https://")];
1032 if (strlen(s) < 2)
1033 return (NULL);
1035 for (i = 0; i < strlen(s) + 1 /* yes er need this */; i++)
1036 /* chop string at first slash */
1037 if (s[i] == '/' || s[i] == ':' || s[i] == '\0') {
1038 ss = g_strdup(s);
1039 ss[i] = '\0';
1040 w = wl_find(ss, wl);
1041 g_free(ss);
1042 return (w);
1045 return (NULL);
1048 char *
1049 js_ref_to_string(JSContextRef context, JSValueRef ref)
1051 char *s = NULL;
1052 size_t l;
1053 JSStringRef jsref;
1055 jsref = JSValueToStringCopy(context, ref, NULL);
1056 if (jsref == NULL)
1057 return (NULL);
1059 l = JSStringGetMaximumUTF8CStringSize(jsref);
1060 s = g_malloc(l);
1061 if (s)
1062 JSStringGetUTF8CString(jsref, s, l);
1063 JSStringRelease(jsref);
1065 return (s);
1068 #define XT_JS_DONE ("done;")
1069 #define XT_JS_DONE_LEN (strlen(XT_JS_DONE))
1070 #define XT_JS_INSERT ("insert;")
1071 #define XT_JS_INSERT_LEN (strlen(XT_JS_INSERT))
1074 run_script(struct tab *t, char *s)
1076 JSGlobalContextRef ctx;
1077 WebKitWebFrame *frame;
1078 JSStringRef str;
1079 JSValueRef val, exception;
1080 char *es;
1082 DNPRINTF(XT_D_JS, "%s: tab %d %s\n", __func__,
1083 t->tab_id, s == (char *)JS_HINTING ? "JS_HINTING" : s);
1085 frame = webkit_web_view_get_main_frame(t->wv);
1086 ctx = webkit_web_frame_get_global_context(frame);
1088 str = JSStringCreateWithUTF8CString(s);
1089 val = JSEvaluateScript(ctx, str, JSContextGetGlobalObject(ctx),
1090 NULL, 0, &exception);
1091 JSStringRelease(str);
1093 DNPRINTF(XT_D_JS, "%s: val %p\n", __func__, val);
1094 if (val == NULL) {
1095 es = js_ref_to_string(ctx, exception);
1096 if (es) {
1097 DNPRINTF(XT_D_JS, "%s: exception %s\n", __func__, es);
1098 g_free(es);
1100 return (1);
1101 } else {
1102 es = js_ref_to_string(ctx, val);
1103 #if 0
1104 /* return values */
1105 if (!strncmp(es, XT_JS_DONE, XT_JS_DONE_LEN))
1106 ; /* do nothing */
1107 if (!strncmp(es, XT_JS_INSERT, XT_JS_INSERT_LEN))
1108 ; /* do nothing */
1109 #endif
1110 if (es) {
1111 DNPRINTF(XT_D_JS, "%s: val %s\n", __func__, es);
1112 g_free(es);
1116 return (0);
1119 void
1120 enable_hints(struct tab *t)
1122 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1124 if (t->new_tab)
1125 run_script(t, "hints.createHints('', 'F');");
1126 else
1127 run_script(t, "hints.createHints('', 'f');");
1128 t->mode = XT_MODE_HINT;
1131 void
1132 disable_hints(struct tab *t)
1134 DNPRINTF(XT_D_JS, "%s: tab %d\n", __func__, t->tab_id);
1136 run_script(t, "hints.clearHints();");
1137 t->mode = XT_MODE_COMMAND;
1138 t->new_tab = 0;
1142 passthrough(struct tab *t, struct karg *args)
1144 t->mode = XT_MODE_PASSTHROUGH;
1145 return (0);
1149 modurl(struct tab *t, struct karg *args)
1151 const gchar *uri = NULL;
1152 char *u = NULL;
1154 /* XXX kind of a bad hack, but oh well */
1155 if (gtk_widget_has_focus(t->uri_entry)) {
1156 if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) &&
1157 (strlen(uri))) {
1158 u = g_strdup_printf("www.%s.com", uri);
1159 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), u);
1160 g_free(u);
1161 activate_uri_entry_cb(t->uri_entry, t);
1164 return (0);
1168 hint(struct tab *t, struct karg *args)
1171 DNPRINTF(XT_D_JS, "hint: tab %d args %d\n", t->tab_id, args->i);
1173 if (t->mode == XT_MODE_HINT) {
1174 if (args->i == XT_HINT_NEWTAB)
1175 t->new_tab = 1;
1176 enable_hints(t);
1177 } else
1178 disable_hints(t);
1180 return (0);
1183 void
1184 apply_style(struct tab *t)
1186 t->styled = 1;
1187 g_object_set(G_OBJECT(t->settings),
1188 "user-stylesheet-uri", t->stylesheet, (char *)NULL);
1191 void
1192 remove_style(struct tab *t)
1194 t->styled = 0;
1195 g_object_set(G_OBJECT(t->settings),
1196 "user-stylesheet-uri", NULL, (char *)NULL);
1200 userstyle_cmd(struct tab *t, struct karg *args)
1202 char script[PATH_MAX] = {'\0'};
1203 char *script_uri;
1204 struct tab *tt;
1206 DNPRINTF(XT_D_JS, "userstyle_cmd: tab %d\n", t->tab_id);
1208 if (args->s != NULL && strlen(args->s)) {
1209 expand_tilde(script, sizeof script, args->s);
1210 script_uri = g_filename_to_uri(script, NULL, NULL);
1211 } else
1212 script_uri = g_strdup(userstyle);
1214 if (script_uri == NULL)
1215 return (1);
1217 switch (args->i) {
1218 case XT_STYLE_CURRENT_TAB:
1219 if (t->styled && !strcmp(script_uri, t->stylesheet))
1220 remove_style(t);
1221 else {
1222 if (t->stylesheet)
1223 g_free(t->stylesheet);
1224 t->stylesheet = g_strdup(script_uri);
1225 apply_style(t);
1227 break;
1228 case XT_STYLE_GLOBAL:
1229 if (userstyle_global && !strcmp(script_uri, t->stylesheet)) {
1230 userstyle_global = 0;
1231 TAILQ_FOREACH(tt, &tabs, entry)
1232 remove_style(tt);
1233 } else {
1234 userstyle_global = 1;
1236 /* need to save this stylesheet for new tabs */
1237 if (stylesheet)
1238 g_free(stylesheet);
1239 stylesheet = g_strdup(script_uri);
1241 TAILQ_FOREACH(tt, &tabs, entry) {
1242 if (tt->stylesheet)
1243 g_free(tt->stylesheet);
1244 tt->stylesheet = g_strdup(script_uri);
1245 apply_style(tt);
1248 break;
1251 g_free(script_uri);
1253 return (0);
1257 quit(struct tab *t, struct karg *args)
1259 if (save_global_history)
1260 save_global_history_to_disk(t);
1262 gtk_main_quit();
1264 return (1);
1267 void
1268 restore_sessions_list(void)
1270 DIR *sdir = NULL;
1271 struct dirent *dp = NULL;
1272 struct session *s;
1273 int reg;
1275 sdir = opendir(sessions_dir);
1276 if (sdir) {
1277 while ((dp = readdir(sdir)) != NULL) {
1278 #if defined __MINGW32__
1279 reg = 1; /* windows only has regular files */
1280 #else
1281 reg = dp->d_type == DT_REG;
1282 #endif
1283 if (dp && reg) {
1284 s = g_malloc(sizeof(struct session));
1285 s->name = g_strdup(dp->d_name);
1286 TAILQ_INSERT_TAIL(&sessions, s, entry);
1289 closedir(sdir);
1294 open_tabs(struct tab *t, struct karg *a)
1296 char file[PATH_MAX];
1297 FILE *f = NULL;
1298 char *uri = NULL;
1299 int rv = 1;
1300 struct tab *ti, *tt;
1302 if (a == NULL)
1303 goto done;
1305 ti = TAILQ_LAST(&tabs, tab_list);
1307 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, a->s);
1308 if ((f = fopen(file, "r")) == NULL)
1309 goto done;
1311 for (;;) {
1312 if ((uri = fparseln(f, NULL, NULL, "\0\0\0", 0)) == NULL) {
1313 if (feof(f) || ferror(f))
1314 break;
1315 } else {
1316 /* retrieve session name */
1317 if (g_str_has_prefix(uri, XT_SAVE_SESSION_ID)) {
1318 strlcpy(named_session,
1319 &uri[strlen(XT_SAVE_SESSION_ID)],
1320 sizeof named_session);
1321 continue;
1324 if (strlen(uri))
1325 create_new_tab(uri, NULL, 1, -1);
1327 free(uri);
1331 /* close open tabs */
1332 if (a->i == XT_SES_CLOSETABS && ti != NULL) {
1333 for (;;) {
1334 tt = TAILQ_FIRST(&tabs);
1335 if (tt == NULL)
1336 break;
1337 if (tt != ti) {
1338 delete_tab(tt);
1339 continue;
1341 delete_tab(tt);
1342 break;
1346 rv = 0;
1347 done:
1348 if (f)
1349 fclose(f);
1351 return (rv);
1355 restore_saved_tabs(void)
1357 char file[PATH_MAX];
1358 int unlink_file = 0;
1359 struct stat sb;
1360 struct karg a;
1361 int rv = 0;
1363 snprintf(file, sizeof file, "%s" PS "%s",
1364 sessions_dir, XT_RESTART_TABS_FILE);
1365 if (stat(file, &sb) == -1)
1366 a.s = XT_SAVED_TABS_FILE;
1367 else {
1368 unlink_file = 1;
1369 a.s = XT_RESTART_TABS_FILE;
1372 a.i = XT_SES_DONOTHING;
1373 rv = open_tabs(NULL, &a);
1375 if (unlink_file)
1376 unlink(file);
1378 return (rv);
1382 save_tabs(struct tab *t, struct karg *a)
1384 char file[PATH_MAX];
1385 FILE *f;
1386 int num_tabs = 0, i;
1387 struct tab **stabs = NULL;
1389 /* tab may be null here */
1391 if (a == NULL)
1392 return (1);
1393 if (a->s == NULL)
1394 snprintf(file, sizeof file, "%s" PS "%s",
1395 sessions_dir, named_session);
1396 else
1397 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, a->s);
1399 if ((f = fopen(file, "w")) == NULL) {
1400 show_oops(t, "Can't open save_tabs file: %s", strerror(errno));
1401 return (1);
1404 /* save session name */
1405 fprintf(f, "%s%s\n", XT_SAVE_SESSION_ID, named_session);
1407 /* Save tabs, in the order they are arranged in the notebook. */
1408 num_tabs = sort_tabs_by_page_num(&stabs);
1410 for (i = 0; i < num_tabs; i++)
1411 if (stabs[i]) {
1412 if (get_uri(stabs[i]) != NULL)
1413 fprintf(f, "%s\n", get_uri(stabs[i]));
1414 else if (gtk_entry_get_text(GTK_ENTRY(
1415 stabs[i]->uri_entry)))
1416 fprintf(f, "%s\n", gtk_entry_get_text(GTK_ENTRY(
1417 stabs[i]->uri_entry)));
1420 g_free(stabs);
1422 /* try and make sure this gets to disk NOW. XXX Backup first? */
1423 if (fflush(f) != 0 || fsync(fileno(f)) != 0) {
1424 show_oops(t, "May not have managed to save session: %s",
1425 strerror(errno));
1428 fclose(f);
1430 return (0);
1434 save_tabs_and_quit(struct tab *t, struct karg *args)
1436 struct karg a;
1438 a.s = NULL;
1439 save_tabs(t, &a);
1440 quit(t, NULL);
1442 return (1);
1445 void
1446 expand_tilde(char *path, size_t len, const char *s)
1448 struct passwd *pwd;
1449 int i;
1450 char user[LOGIN_NAME_MAX];
1451 const char *sc = s;
1453 if (path == NULL || s == NULL)
1454 errx(1, "expand_tilde");
1456 if (s[0] != '~') {
1457 strlcpy(path, sc, len);
1458 return;
1461 ++s;
1462 for (i = 0; s[i] != PSC && s[i] != '\0'; ++i)
1463 user[i] = s[i];
1464 user[i] = '\0';
1465 s = &s[i];
1467 pwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1468 if (pwd == NULL)
1469 strlcpy(path, sc, len);
1470 else
1471 snprintf(path, len, "%s%s", pwd->pw_dir, s);
1475 run_page_script(struct tab *t, struct karg *args)
1477 const gchar *uri;
1478 char *tmp, script[PATH_MAX];
1479 char *sv[3];
1481 tmp = args->s != NULL && strlen(args->s) > 0 ? args->s : default_script;
1482 if (tmp[0] == '\0') {
1483 show_oops(t, "no script specified");
1484 return (1);
1487 if ((uri = get_uri(t)) == NULL) {
1488 show_oops(t, "tab is empty, not running script");
1489 return (1);
1492 expand_tilde(script, sizeof script, tmp);
1494 sv[0] = script;
1495 sv[1] = (char *)uri;
1496 sv[2] = NULL;
1497 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
1498 NULL, NULL)) {
1499 show_oops(t, "%s: could not spawn process: %s %s", __func__,
1500 sv[0], sv[1]);
1501 return (1);
1502 } else
1503 show_oops(t, "running: %s %s", sv[0], sv[1]);
1505 return (0);
1509 yank_uri(struct tab *t, struct karg *args)
1511 const gchar *uri;
1512 GtkClipboard *clipboard, *primary;
1514 if ((uri = get_uri(t)) == NULL)
1515 return (1);
1517 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1518 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1519 gtk_clipboard_set_text(primary, uri, -1);
1520 gtk_clipboard_set_text(clipboard, uri, -1);
1522 return (0);
1526 paste_uri(struct tab *t, struct karg *args)
1528 GtkClipboard *clipboard, *primary;
1529 gchar *c = NULL, *p = NULL, *uri;
1530 int i;
1532 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1533 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1534 c = gtk_clipboard_wait_for_text(clipboard);
1535 p = gtk_clipboard_wait_for_text(primary);
1537 if (c || p) {
1538 #ifdef __MINGW32__
1539 /* Windows try clipboard first */
1540 uri = c ? c : p;
1541 #else
1542 /* UNIX try primary first */
1543 uri = p ? p : c;
1544 #endif
1545 /* replace all newlines with spaces */
1546 for (i = 0; uri[i] != '\0'; ++i)
1547 if (uri[i] == '\n')
1548 uri[i] = ' ';
1550 while (*uri && isspace((unsigned char)*uri))
1551 uri++;
1552 if (strlen(uri) == 0) {
1553 show_oops(t, "empty paste buffer");
1554 goto done;
1556 if (guess_search == 0 && valid_url_type(uri)) {
1557 /* we can be clever and paste this in search box */
1558 show_oops(t, "not a valid URL");
1559 goto done;
1562 if (args->i == XT_PASTE_CURRENT_TAB)
1563 load_uri(t, uri);
1564 else if (args->i == XT_PASTE_NEW_TAB)
1565 create_new_tab(uri, NULL, 1, -1);
1568 done:
1569 if (c)
1570 g_free(c);
1571 if (p)
1572 g_free(p);
1574 return (0);
1577 void
1578 js_toggle_cb(GtkWidget *w, struct tab *t)
1580 struct karg a;
1581 int es, set;
1583 g_object_get(G_OBJECT(t->settings),
1584 "enable-scripts", &es, (char *)NULL);
1585 es = !es;
1586 if (es)
1587 set = XT_WL_ENABLE;
1588 else
1589 set = XT_WL_DISABLE;
1591 a.i = set | XT_WL_TOPLEVEL;
1592 toggle_pl(t, &a);
1594 a.i = set | XT_WL_TOPLEVEL;
1595 toggle_cwl(t, &a);
1597 a.i = XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD;
1598 toggle_js(t, &a);
1601 void
1602 proxy_toggle_cb(GtkWidget *w, struct tab *t)
1604 struct karg args = {0};
1606 args.i = XT_PRXY_TOGGLE;
1607 proxy_cmd(t, &args);
1611 toggle_src(struct tab *t, struct karg *args)
1613 gboolean mode;
1615 if (t == NULL)
1616 return (0);
1618 mode = webkit_web_view_get_view_source_mode(t->wv);
1619 webkit_web_view_set_view_source_mode(t->wv, !mode);
1620 webkit_web_view_reload(t->wv);
1622 return (0);
1625 void
1626 focus_webview(struct tab *t)
1628 if (t == NULL)
1629 return;
1631 /* only grab focus if we are visible */
1632 if (gtk_notebook_get_current_page(notebook) == t->tab_id)
1633 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
1637 focus(struct tab *t, struct karg *args)
1639 if (t == NULL || args == NULL)
1640 return (1);
1642 if (show_url == 0)
1643 return (0);
1645 if (args->i == XT_FOCUS_URI)
1646 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
1647 else if (args->i == XT_FOCUS_SEARCH)
1648 gtk_widget_grab_focus(GTK_WIDGET(t->search_entry));
1650 return (0);
1653 void
1654 free_connection_certs(gnutls_x509_crt_t *certs, size_t cert_count)
1656 int i;
1658 for (i = 0; i < cert_count; i++)
1659 gnutls_x509_crt_deinit(certs[i]);
1660 gnutls_free(certs);
1663 #if GTK_CHECK_VERSION(3, 0, 0)
1664 void
1665 statusbar_modify_attr(struct tab *t, const char *css_name)
1667 gtk_widget_set_name(t->sbe.ebox, css_name);
1669 #else
1670 void
1671 statusbar_modify_attr(struct tab *t, const char *text, const char *base)
1673 GdkColor c_text, c_base;
1675 gdk_color_parse(text, &c_text);
1676 gdk_color_parse(base, &c_base);
1678 gtk_widget_modify_bg(t->sbe.ebox, GTK_STATE_NORMAL, &c_base);
1679 gtk_widget_modify_base(t->sbe.uri, GTK_STATE_NORMAL, &c_base);
1680 gtk_widget_modify_text(t->sbe.uri, GTK_STATE_NORMAL, &c_text);
1682 #endif
1684 void
1685 save_certs(struct tab *t, gnutls_x509_crt_t *certs,
1686 size_t cert_count, const char *domain, const char *dir)
1688 size_t cert_buf_sz;
1689 char file[PATH_MAX];
1690 char *cert_buf = NULL;
1691 int rv;
1692 int i;
1693 FILE *f;
1695 if (t == NULL || certs == NULL || cert_count <= 0 || domain == NULL)
1696 return;
1698 snprintf(file, sizeof file, "%s" PS "%s", dir, domain);
1699 if ((f = fopen(file, "w")) == NULL) {
1700 show_oops(t, "Can't create cert file %s %s",
1701 file, strerror(errno));
1702 return;
1705 for (i = 0; i < cert_count; i++) {
1707 * Because we support old crap and can't use
1708 * gnutls_x509_crt_export2(), we intentionally use an empty
1709 * buffer and then make a second call with the known size
1710 * needed.
1712 cert_buf_sz = 0;
1713 gnutls_x509_crt_export(certs[i], GNUTLS_X509_FMT_PEM,
1714 NULL, &cert_buf_sz);
1715 if (cert_buf_sz == 0) {
1716 show_oops(t, "no certs found");
1717 goto done;
1719 cert_buf = gnutls_malloc(cert_buf_sz * sizeof(char));
1720 if (cert_buf == NULL) {
1721 show_oops(t, "gnutls_x509_crt_export failed");
1722 goto done;
1724 rv = gnutls_x509_crt_export(certs[i],
1725 GNUTLS_X509_FMT_PEM, cert_buf, &cert_buf_sz);
1726 if (rv != 0) {
1727 show_oops(t, "gnutls_x509_crt_export failure: %s",
1728 gnutls_strerror(rv));
1729 goto done;
1731 cert_buf[cert_buf_sz] = '\0';
1732 if (fwrite(cert_buf, cert_buf_sz, 1, f) != 1) {
1733 show_oops(t, "Can't write certs: %s", strerror(errno));
1734 goto done;
1736 gnutls_free(cert_buf);
1737 cert_buf = NULL;
1740 done:
1741 if (cert_buf)
1742 gnutls_free(cert_buf);
1743 fclose(f);
1746 enum cert_trust {
1747 CERT_LOCAL,
1748 CERT_TRUSTED,
1749 CERT_UNTRUSTED,
1750 CERT_BAD
1753 gnutls_x509_crt_t *
1754 get_local_cert_chain(const char *uri, size_t *ncerts, const char **error_str,
1755 const char *dir)
1757 SoupURI *su;
1758 unsigned char cert_buf[64 * 1024] = {0};
1759 gnutls_datum_t data;
1760 unsigned int max_certs = XT_MAX_CERTS;
1761 int bytes_read;
1762 char file[PATH_MAX];
1763 FILE *f;
1764 gnutls_x509_crt_t *certs;
1766 if ((su = soup_uri_new(uri)) == NULL) {
1767 *error_str = "Invalid URI";
1768 return (NULL);
1771 snprintf(file, sizeof file, "%s" PS "%s", dir, su->host);
1772 soup_uri_free(su);
1773 if ((f = fopen(file, "r")) == NULL) {
1774 *error_str = "Could not read local cert";
1775 return (NULL);
1778 bytes_read = fread(cert_buf, sizeof *cert_buf, sizeof cert_buf, f);
1779 if (bytes_read == 0) {
1780 *error_str = "Could not read local cert";
1781 return (NULL);
1784 certs = gnutls_malloc(sizeof(*certs) * max_certs);
1785 if (certs == NULL) {
1786 *error_str = "Error allocating memory";
1787 return (NULL);
1789 data.data = cert_buf;
1790 data.size = bytes_read;
1791 if (gnutls_x509_crt_list_import(certs, &max_certs, &data,
1792 GNUTLS_X509_FMT_PEM, 0) < 0) {
1793 gnutls_free(certs);
1794 *error_str = "Error reading local cert chain";
1795 return (NULL);
1798 *ncerts = max_certs;
1799 return (certs);
1803 * This uses the pem-encoded cert chain saved in t->pem instead of
1804 * grabbing the remote cert. We save it beforehand and read it here
1805 * so as to not open a side channel that ignores proxy settings.
1807 gnutls_x509_crt_t *
1808 get_chain_for_pem(char *pem, size_t *ncerts, const char **error_str)
1810 gnutls_datum_t data;
1811 unsigned int max_certs = XT_MAX_CERTS;
1812 gnutls_x509_crt_t *certs;
1814 if (pem == NULL) {
1815 *error_str = "Error reading remote cert chain";
1816 return (NULL);
1819 certs = gnutls_malloc(sizeof(*certs) * max_certs);
1820 if (certs == NULL) {
1821 *error_str = "Error allocating memory";
1822 return (NULL);
1824 data.data = (unsigned char *)pem;
1825 data.size = strlen(pem);
1826 if (gnutls_x509_crt_list_import(certs, &max_certs, &data,
1827 GNUTLS_X509_FMT_PEM, 0) < 0) {
1828 gnutls_free(certs);
1829 *error_str = "Error reading remote cert chain";
1830 return (NULL);
1833 *ncerts = max_certs;
1834 return (certs);
1839 cert_cmd(struct tab *t, struct karg *args)
1841 const gchar *uri, *error_str = NULL;
1842 size_t cert_count;
1843 gnutls_x509_crt_t *certs;
1844 SoupURI *su;
1845 char *host;
1846 #if !GTK_CHECK_VERSION(3, 0, 0)
1847 GdkColor color;
1848 #endif
1850 if (t == NULL)
1851 return (1);
1853 if (args->s != NULL) {
1854 uri = args->s;
1855 } else if ((uri = get_uri(t)) == NULL) {
1856 show_oops(t, "Invalid URI");
1857 return (1);
1859 if ((su = soup_uri_new(uri)) == NULL) {
1860 show_oops(t, "Invalid URI");
1861 return (1);
1865 * if we're only showing the local certs, don't open a socket and get
1866 * the remote certs
1868 if (args->i & XT_SHOW && args->i & XT_CACHE) {
1869 certs = get_local_cert_chain(uri, &cert_count, &error_str,
1870 certs_cache_dir);
1871 if (error_str == NULL) {
1872 t->about_cert_host = g_strdup(su->host);
1873 show_certs(t, certs, cert_count, "Certificate Chain");
1874 free_connection_certs(certs, cert_count);
1875 } else {
1876 show_oops(t, "%s", error_str);
1877 soup_uri_free(su);
1878 return (1);
1880 soup_uri_free(su);
1881 return (0);
1884 certs = get_chain_for_pem(t->pem, &cert_count, &error_str);
1885 if (error_str)
1886 goto done;
1888 if (args->i & XT_SHOW) {
1889 t->about_cert_host = g_strdup(su->host);
1890 show_certs(t, certs, cert_count, "Certificate Chain");
1891 } else if (args->i & XT_SAVE) {
1892 host = t->about_cert_host ? t->about_cert_host : su->host;
1893 save_certs(t, certs, cert_count, host, certs_dir);
1894 #if GTK_CHECK_VERSION(3, 0, 0)
1895 gtk_widget_set_name(t->uri_entry, XT_CSS_BLUE);
1896 statusbar_modify_attr(t, XT_CSS_BLUE);
1897 #else
1898 gdk_color_parse(XT_COLOR_BLUE, &color);
1899 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
1900 statusbar_modify_attr(t, XT_COLOR_BLACK, XT_COLOR_BLUE);
1901 #endif
1902 } else if (args->i & XT_CACHE)
1903 save_certs(t, certs, cert_count, su->host, certs_cache_dir);
1905 free_connection_certs(certs, cert_count);
1907 done:
1908 soup_uri_free(su);
1910 if (error_str && strlen(error_str)) {
1911 show_oops(t, "%s", error_str);
1912 return (1);
1914 return (0);
1918 * Checks whether the remote cert is identical to the local saved
1919 * cert. Returns CERT_LOCAL if unchanged, CERT_UNTRUSTED if local
1920 * cert does not exist, and CERT_BAD if different.
1922 * Saves entire cert chain in pem encoding to chain for it to be
1923 * cached later, if needed.
1925 enum cert_trust
1926 check_local_certs(const char *file, GTlsCertificate *cert, char **chain)
1928 char r_cert_buf[64 * 1024];
1929 FILE *f;
1930 GTlsCertificate *tmpcert = NULL;
1931 GTlsCertificate *issuer = NULL;
1932 char *pem = NULL;
1933 char *tmp = NULL;
1934 enum cert_trust rv = CERT_LOCAL;
1935 int i;
1937 if ((f = fopen(file, "r")) == NULL) {
1938 /* no local cert to check */
1939 rv = CERT_UNTRUSTED;
1942 for (i = 0;;) {
1943 if (i == 0) {
1944 g_object_get(G_OBJECT(cert), "certificate-pem", &pem,
1945 NULL);
1946 g_object_get(G_OBJECT(cert), "issuer", &issuer, NULL);
1947 } else {
1948 if (issuer == NULL)
1949 break;
1950 g_object_get(G_OBJECT(tmpcert), "issuer", &issuer,
1951 NULL);
1952 if (issuer == NULL)
1953 break;
1955 g_object_get(G_OBJECT(tmpcert), "certificate-pem", &pem,
1956 NULL);
1958 i++;
1960 if (tmpcert != NULL)
1961 g_object_unref(G_OBJECT(tmpcert));
1962 tmpcert = issuer;
1963 if (f) {
1964 if (fread(r_cert_buf, strlen(pem), 1, f) != 1 && !feof(f))
1965 rv = CERT_BAD;
1966 if (bcmp(r_cert_buf, pem, strlen(pem)))
1967 rv = CERT_BAD;
1969 tmp = g_strdup_printf("%s%s", *chain, pem);
1970 g_free(pem);
1971 g_free(*chain);
1972 *chain = tmp;
1975 if (issuer != NULL)
1976 g_object_unref(G_OBJECT(issuer));
1977 if (f != NULL)
1978 fclose(f);
1979 return (rv);
1983 check_cert_changes(struct tab *t, GTlsCertificate *cert, const char *file, const char *uri)
1985 SoupURI *soupuri = NULL;
1986 struct karg args = {0};
1987 struct wl_entry *w = NULL;
1988 char *chain = NULL;
1989 int ret = 0;
1991 chain = g_strdup("");
1992 switch(check_local_certs(file, cert, &chain)) {
1993 case CERT_LOCAL:
1994 /* The cached certificate is identical */
1995 break;
1996 case CERT_TRUSTED: /* FALLTHROUGH */
1997 case CERT_UNTRUSTED:
1998 /* cache new certificate */
1999 args.i = XT_CACHE;
2000 cert_cmd(t, &args);
2001 break;
2002 case CERT_BAD:
2003 if ((soupuri = soup_uri_new(uri)) == NULL ||
2004 soupuri->host == NULL)
2005 break;
2006 if ((w = wl_find(soupuri->host, &svil)) != NULL)
2007 break;
2008 t->xtp_meaning = XT_XTP_TAB_MEANING_SV;
2009 args.s = g_strdup((char *)uri);
2010 xtp_page_sv(t, &args);
2011 ret = 1;
2012 break;
2015 if (soupuri)
2016 soup_uri_free(soupuri);
2017 if (chain)
2018 g_free(chain);
2019 return (ret);
2023 remove_cookie(int index)
2025 int i, rv = 1;
2026 GSList *cf;
2027 SoupCookie *c;
2029 DNPRINTF(XT_D_COOKIE, "remove_cookie: %d\n", index);
2031 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2033 for (i = 1; cf; cf = cf->next, i++) {
2034 if (i != index)
2035 continue;
2036 c = cf->data;
2037 print_cookie("remove cookie", c);
2038 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2039 rv = 0;
2040 break;
2043 soup_cookies_free(cf);
2045 return (rv);
2049 remove_cookie_domain(int domain_id)
2051 int domain_count, rv = 1;
2052 GSList *cf;
2053 SoupCookie *c;
2054 char *last_domain;
2056 DNPRINTF(XT_D_COOKIE, "remove_cookie_domain: %d\n", domain_id);
2058 last_domain = "";
2059 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2061 for (domain_count = 0; cf; cf = cf->next) {
2062 c = cf->data;
2064 if (strcmp(last_domain, c->domain) != 0) {
2065 domain_count++;
2066 last_domain = c->domain;
2069 if (domain_count < domain_id)
2070 continue;
2071 else if (domain_count > domain_id)
2072 break;
2074 print_cookie("remove cookie", c);
2075 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2076 rv = 0;
2079 soup_cookies_free(cf);
2081 return (rv);
2085 remove_cookie_all()
2087 int rv = 1;
2088 GSList *cf;
2089 SoupCookie *c;
2091 DNPRINTF(XT_D_COOKIE, "remove_cookie_all\n");
2093 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2095 for (; cf; cf = cf->next) {
2096 c = cf->data;
2098 print_cookie("remove cookie", c);
2099 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2100 rv = 0;
2103 soup_cookies_free(cf);
2105 return (rv);
2109 toplevel_cmd(struct tab *t, struct karg *args)
2111 js_toggle_cb(t->js_toggle, t);
2113 return (0);
2117 can_go_back_for_real(struct tab *t)
2119 int i;
2120 WebKitWebHistoryItem *item;
2121 const gchar *uri;
2123 if (t == NULL)
2124 return (FALSE);
2126 if (t->item != NULL)
2127 return (TRUE);
2129 /* rely on webkit to make sure we can go backward when on an about page */
2130 uri = get_uri(t);
2131 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2132 g_str_has_prefix(uri, "xxxt://"))
2133 return (webkit_web_view_can_go_back(t->wv));
2135 /* the back/forward list is stupid so help determine if we can go back */
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 return (TRUE);
2143 return (FALSE);
2147 can_go_forward_for_real(struct tab *t)
2149 int i;
2150 WebKitWebHistoryItem *item;
2151 const gchar *uri;
2153 if (t == NULL)
2154 return (FALSE);
2156 /* rely on webkit to make sure we can go forward when on an about page */
2157 uri = get_uri(t);
2158 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2159 g_str_has_prefix(uri, "xxxt://"))
2160 return (webkit_web_view_can_go_forward(t->wv));
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 return (TRUE);
2170 return (FALSE);
2173 void
2174 go_back_for_real(struct tab *t)
2176 int i;
2177 WebKitWebHistoryItem *item;
2178 const gchar *uri;
2180 if (t == NULL)
2181 return;
2183 uri = get_uri(t);
2184 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2185 g_str_has_prefix(uri, "xxxt://")) {
2186 webkit_web_view_go_back(t->wv);
2187 return;
2189 /* the back/forwars list is stupid so help selecting a different item */
2190 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2191 item != NULL;
2192 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2193 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2194 webkit_web_view_go_to_back_forward_item(t->wv, item);
2195 break;
2200 void
2201 go_forward_for_real(struct tab *t)
2203 int i;
2204 WebKitWebHistoryItem *item;
2205 const gchar *uri;
2207 if (t == NULL)
2208 return;
2210 uri = get_uri(t);
2211 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2212 g_str_has_prefix(uri, "xxxt://")) {
2213 webkit_web_view_go_forward(t->wv);
2214 return;
2216 /* the back/forwars list is stupid so help selecting a different item */
2217 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2218 item != NULL;
2219 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2220 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2221 webkit_web_view_go_to_back_forward_item(t->wv, item);
2222 break;
2228 navaction(struct tab *t, struct karg *args)
2230 WebKitWebHistoryItem *item = NULL;
2231 WebKitWebFrame *frame;
2233 DNPRINTF(XT_D_NAV, "navaction: tab %d opcode %d\n",
2234 t->tab_id, args->i);
2236 hide_oops(t);
2237 set_normal_tab_meaning(t);
2238 if (t->item) {
2239 if (args->i == XT_NAV_BACK)
2240 item = webkit_web_back_forward_list_get_current_item(t->bfl);
2241 else
2242 item = webkit_web_back_forward_list_get_forward_item(t->bfl);
2245 switch (args->i) {
2246 case XT_NAV_BACK:
2247 if (t->item) {
2248 if (item == NULL)
2249 return (XT_CB_PASSTHROUGH);
2250 webkit_web_view_go_to_back_forward_item(t->wv, item);
2251 t->item = NULL;
2252 return (XT_CB_PASSTHROUGH);
2254 marks_clear(t);
2255 go_back_for_real(t);
2256 break;
2257 case XT_NAV_FORWARD:
2258 if (t->item) {
2259 if (item == NULL)
2260 return (XT_CB_PASSTHROUGH);
2261 webkit_web_view_go_to_back_forward_item(t->wv, item);
2262 t->item = NULL;
2263 return (XT_CB_PASSTHROUGH);
2265 marks_clear(t);
2266 go_forward_for_real(t);
2267 break;
2268 case XT_NAV_RELOAD:
2269 frame = webkit_web_view_get_main_frame(t->wv);
2270 webkit_web_frame_reload(frame);
2271 break;
2272 case XT_NAV_STOP:
2273 frame = webkit_web_view_get_main_frame(t->wv);
2274 webkit_web_frame_stop_loading(frame);
2275 break;
2277 return (XT_CB_PASSTHROUGH);
2281 move(struct tab *t, struct karg *args)
2283 GtkAdjustment *adjust;
2284 double pi, si, pos, ps, upper, lower, max;
2285 double percent;
2287 switch (args->i) {
2288 case XT_MOVE_DOWN:
2289 case XT_MOVE_UP:
2290 case XT_MOVE_BOTTOM:
2291 case XT_MOVE_TOP:
2292 case XT_MOVE_PAGEDOWN:
2293 case XT_MOVE_PAGEUP:
2294 case XT_MOVE_HALFDOWN:
2295 case XT_MOVE_HALFUP:
2296 case XT_MOVE_PERCENT:
2297 case XT_MOVE_CENTER:
2298 adjust = t->adjust_v;
2299 break;
2300 default:
2301 adjust = t->adjust_h;
2302 break;
2305 pos = gtk_adjustment_get_value(adjust);
2306 ps = gtk_adjustment_get_page_size(adjust);
2307 upper = gtk_adjustment_get_upper(adjust);
2308 lower = gtk_adjustment_get_lower(adjust);
2309 si = gtk_adjustment_get_step_increment(adjust);
2310 pi = gtk_adjustment_get_page_increment(adjust);
2311 max = upper - ps;
2313 DNPRINTF(XT_D_MOVE, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2314 "max %f si %f pi %f\n",
2315 args->i, adjust == t->adjust_h ? "horizontal" : "vertical",
2316 pos, ps, upper, lower, max, si, pi);
2318 switch (args->i) {
2319 case XT_MOVE_DOWN:
2320 case XT_MOVE_RIGHT:
2321 pos += si;
2322 gtk_adjustment_set_value(adjust, MIN(pos, max));
2323 break;
2324 case XT_MOVE_UP:
2325 case XT_MOVE_LEFT:
2326 pos -= si;
2327 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2328 break;
2329 case XT_MOVE_BOTTOM:
2330 case XT_MOVE_FARRIGHT:
2331 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2332 gtk_adjustment_set_value(adjust, max);
2333 break;
2334 case XT_MOVE_TOP:
2335 case XT_MOVE_FARLEFT:
2336 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2337 gtk_adjustment_set_value(adjust, lower);
2338 break;
2339 case XT_MOVE_PAGEDOWN:
2340 pos += pi;
2341 gtk_adjustment_set_value(adjust, MIN(pos, max));
2342 break;
2343 case XT_MOVE_PAGEUP:
2344 pos -= pi;
2345 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2346 break;
2347 case XT_MOVE_HALFDOWN:
2348 pos += pi / 2;
2349 gtk_adjustment_set_value(adjust, MIN(pos, max));
2350 break;
2351 case XT_MOVE_HALFUP:
2352 pos -= pi / 2;
2353 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2354 break;
2355 case XT_MOVE_CENTER:
2356 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2357 args->s = g_strdup("50.0");
2358 /* FALLTHROUGH */
2359 case XT_MOVE_PERCENT:
2360 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2361 percent = atoi(args->s) / 100.0;
2362 pos = max * percent;
2363 if (pos < 0.0 || pos > max)
2364 break;
2365 gtk_adjustment_set_value(adjust, pos);
2366 break;
2367 default:
2368 return (XT_CB_PASSTHROUGH);
2371 DNPRINTF(XT_D_MOVE, "move: new pos %f %f\n", pos, MIN(pos, max));
2373 return (XT_CB_HANDLED);
2376 void
2377 url_set_visibility(void)
2379 struct tab *t;
2381 TAILQ_FOREACH(t, &tabs, entry)
2382 if (show_url == 0) {
2383 gtk_widget_hide(t->toolbar);
2384 focus_webview(t);
2385 } else
2386 gtk_widget_show(t->toolbar);
2389 void
2390 notebook_tab_set_visibility(void)
2392 if (show_tabs == 0) {
2393 gtk_widget_hide(tab_bar);
2394 gtk_notebook_set_show_tabs(notebook, FALSE);
2395 } else {
2396 if (tab_style == XT_TABS_NORMAL) {
2397 gtk_widget_hide(tab_bar);
2398 gtk_notebook_set_show_tabs(notebook, TRUE);
2399 } else if (tab_style == XT_TABS_COMPACT) {
2400 gtk_widget_show(tab_bar);
2401 gtk_notebook_set_show_tabs(notebook, FALSE);
2406 void
2407 statusbar_set_visibility(void)
2409 struct tab *t;
2411 TAILQ_FOREACH(t, &tabs, entry){
2412 if (show_statusbar == 0)
2413 gtk_widget_hide(t->statusbar);
2414 else
2415 gtk_widget_show(t->statusbar);
2417 focus_webview(t);
2421 void
2422 url_set(struct tab *t, int enable_url_entry)
2424 GdkPixbuf *pixbuf;
2425 int progress;
2427 show_url = enable_url_entry;
2429 if (enable_url_entry) {
2430 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
2431 GTK_ENTRY_ICON_PRIMARY, NULL);
2432 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri), 0);
2433 } else {
2434 pixbuf = gtk_entry_get_icon_pixbuf(GTK_ENTRY(t->uri_entry),
2435 GTK_ENTRY_ICON_PRIMARY);
2436 progress =
2437 gtk_entry_get_progress_fraction(GTK_ENTRY(t->uri_entry));
2438 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
2439 GTK_ENTRY_ICON_PRIMARY, pixbuf);
2440 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
2441 progress);
2446 fullscreen(struct tab *t, struct karg *args)
2448 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2450 if (t == NULL)
2451 return (XT_CB_PASSTHROUGH);
2453 if (show_url == 0) {
2454 url_set(t, 1);
2455 show_tabs = 1;
2456 } else {
2457 url_set(t, 0);
2458 show_tabs = 0;
2461 url_set_visibility();
2462 notebook_tab_set_visibility();
2464 return (XT_CB_HANDLED);
2468 statustoggle(struct tab *t, struct karg *args)
2470 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2472 if (show_statusbar == 1) {
2473 show_statusbar = 0;
2474 statusbar_set_visibility();
2475 } else if (show_statusbar == 0) {
2476 show_statusbar = 1;
2477 statusbar_set_visibility();
2479 return (XT_CB_HANDLED);
2483 urlaction(struct tab *t, struct karg *args)
2485 int rv = XT_CB_HANDLED;
2487 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2489 if (t == NULL)
2490 return (XT_CB_PASSTHROUGH);
2492 switch (args->i) {
2493 case XT_URL_SHOW:
2494 if (show_url == 0) {
2495 url_set(t, 1);
2496 url_set_visibility();
2498 break;
2499 case XT_URL_HIDE:
2500 if (show_url == 1) {
2501 url_set(t, 0);
2502 url_set_visibility();
2504 break;
2506 return (rv);
2510 tabaction(struct tab *t, struct karg *args)
2512 int rv = XT_CB_HANDLED;
2513 char *url = args->s;
2514 struct undo *u;
2515 struct tab *tt, *tv;
2517 DNPRINTF(XT_D_TAB, "tabaction: %p %d\n", t, args->i);
2519 if (t == NULL)
2520 return (XT_CB_PASSTHROUGH);
2522 switch (args->i) {
2523 case XT_TAB_NEW:
2524 if (strlen(url) > 0)
2525 create_new_tab(url, NULL, 1, args->precount);
2526 else
2527 create_new_tab(NULL, NULL, 1, args->precount);
2528 break;
2529 case XT_TAB_DELETE:
2530 if (args->precount < 0)
2531 delete_tab(t);
2532 else
2533 TAILQ_FOREACH(tt, &tabs, entry)
2534 if (tt->tab_id == args->precount - 1) {
2535 delete_tab(tt);
2536 break;
2538 break;
2539 case XT_TAB_DELQUIT:
2540 if (gtk_notebook_get_n_pages(notebook) > 1)
2541 delete_tab(t);
2542 else
2543 quit(t, args);
2544 break;
2545 case XT_TAB_ONLY:
2546 TAILQ_FOREACH_SAFE(tt, &tabs, entry, tv)
2547 if (t != tt)
2548 delete_tab(tt);
2549 break;
2550 case XT_TAB_OPEN:
2551 if (strlen(url) > 0)
2553 else {
2554 rv = XT_CB_PASSTHROUGH;
2555 goto done;
2557 load_uri(t, url);
2558 break;
2559 case XT_TAB_SHOW:
2560 if (show_tabs == 0) {
2561 show_tabs = 1;
2562 notebook_tab_set_visibility();
2564 break;
2565 case XT_TAB_HIDE:
2566 if (show_tabs == 1) {
2567 show_tabs = 0;
2568 notebook_tab_set_visibility();
2570 break;
2571 case XT_TAB_NEXTSTYLE:
2572 if (tab_style == XT_TABS_NORMAL) {
2573 tab_style = XT_TABS_COMPACT;
2574 recolor_compact_tabs();
2576 else
2577 tab_style = XT_TABS_NORMAL;
2578 notebook_tab_set_visibility();
2579 break;
2580 case XT_TAB_UNDO_CLOSE:
2581 if (undo_count == 0) {
2582 DNPRINTF(XT_D_TAB, "%s: no tabs to undo close",
2583 __func__);
2584 goto done;
2585 } else {
2586 undo_count--;
2587 u = TAILQ_FIRST(&undos);
2588 create_new_tab(u->uri, u, 1, -1);
2590 TAILQ_REMOVE(&undos, u, entry);
2591 g_free(u->uri);
2592 /* u->history is freed in create_new_tab() */
2593 g_free(u);
2595 break;
2596 case XT_TAB_LOAD_IMAGES:
2598 if (!auto_load_images) {
2600 /* Enable auto-load images (this will load all
2601 * previously unloaded images). */
2602 g_object_set(G_OBJECT(t->settings),
2603 "auto-load-images", TRUE, (char *)NULL);
2604 webkit_web_view_set_settings(t->wv, t->settings);
2606 webkit_web_view_reload(t->wv);
2608 /* Webkit triggers an event when we change the setting,
2609 * so we can't disable the auto-loading at once.
2611 * Unfortunately, webkit does not tell us when it's done.
2612 * Instead, we wait until the next request, and then
2613 * disable autoloading again.
2615 t->load_images = TRUE;
2617 break;
2618 default:
2619 rv = XT_CB_PASSTHROUGH;
2620 goto done;
2623 done:
2624 if (args->s) {
2625 g_free(args->s);
2626 args->s = NULL;
2629 return (rv);
2633 resizetab(struct tab *t, struct karg *args)
2635 if (t == NULL || args == NULL) {
2636 show_oops(NULL, "resizetab invalid parameters");
2637 return (XT_CB_PASSTHROUGH);
2640 DNPRINTF(XT_D_TAB, "resizetab: tab %d %d\n",
2641 t->tab_id, args->i);
2643 setzoom_webkit(t, args->i);
2645 return (XT_CB_HANDLED);
2649 movetab(struct tab *t, struct karg *args)
2651 int n, dest;
2653 if (t == NULL || args == NULL) {
2654 show_oops(NULL, "movetab invalid parameters");
2655 return (XT_CB_PASSTHROUGH);
2658 DNPRINTF(XT_D_TAB, "movetab: tab %d opcode %d\n",
2659 t->tab_id, args->i);
2661 if (args->i >= XT_TAB_INVALID)
2662 return (XT_CB_PASSTHROUGH);
2664 if (TAILQ_EMPTY(&tabs))
2665 return (XT_CB_PASSTHROUGH);
2667 n = gtk_notebook_get_n_pages(notebook);
2668 dest = gtk_notebook_get_current_page(notebook);
2670 switch (args->i) {
2671 case XT_TAB_NEXT:
2672 if (args->precount < 0)
2673 dest = dest == n - 1 ? 0 : dest + 1;
2674 else
2675 dest = args->precount - 1;
2677 break;
2678 case XT_TAB_PREV:
2679 if (args->precount < 0)
2680 dest -= 1;
2681 else
2682 dest -= args->precount % n;
2684 if (dest < 0)
2685 dest += n;
2687 break;
2688 case XT_TAB_FIRST:
2689 dest = 0;
2690 break;
2691 case XT_TAB_LAST:
2692 dest = n - 1;
2693 break;
2694 default:
2695 return (XT_CB_PASSTHROUGH);
2698 if (dest < 0 || dest >= n)
2699 return (XT_CB_PASSTHROUGH);
2700 if (t->tab_id == dest) {
2701 DNPRINTF(XT_D_TAB, "movetab: do nothing\n");
2702 return (XT_CB_HANDLED);
2705 set_current_tab(dest);
2707 return (XT_CB_HANDLED);
2710 int cmd_prefix = 0;
2712 struct prompt_sub {
2713 const char *s;
2714 const char *(*f)(struct tab *);
2715 } subs[] = {
2716 { "<uri>", get_uri },
2720 command(struct tab *t, struct karg *args)
2722 struct karg a = {0};
2723 int i, cmd_setup = 0;
2724 char *s = NULL, *sp = NULL, *sl = NULL;
2725 gchar **sv;
2727 if (t == NULL || args == NULL) {
2728 show_oops(NULL, "command invalid parameters");
2729 return (XT_CB_PASSTHROUGH);
2732 switch (args->i) {
2733 case '/':
2734 s = "/";
2735 break;
2736 case '?':
2737 s = "?";
2738 break;
2739 case ':':
2740 if (cmd_prefix == 0) {
2741 if (args->s != NULL && strlen(args->s) != 0) {
2742 sp = g_strdup_printf(":%s", args->s);
2743 s = sp;
2744 } else
2745 s = ":";
2746 } else {
2747 sp = g_strdup_printf(":%d", cmd_prefix);
2748 s = sp;
2749 cmd_prefix = 0;
2751 sl = g_strdup(s);
2752 if (sp) {
2753 g_free(sp);
2754 sp = NULL;
2756 s = sl;
2757 for (i = 0; i < LENGTH(subs); ++i) {
2758 sv = g_strsplit(sl, subs[i].s, -1);
2759 if (sl)
2760 g_free(sl);
2761 sl = g_strjoinv(subs[i].f(t), sv);
2762 g_strfreev(sv);
2763 s = sl;
2765 break;
2766 case '.':
2767 t->mode = XT_MODE_HINT;
2768 a.i = 0;
2769 s = ".";
2771 * js code will auto fire() if a single link is visible,
2772 * causing the focus-out-event cb function to be called. Setup
2773 * the cmd _before_ triggering hinting code so the cmd can get
2774 * killed by the cb in this case.
2776 show_cmd(t, s);
2777 cmd_setup = 1;
2778 hint(t, &a);
2779 break;
2780 case ',':
2781 t->mode = XT_MODE_HINT;
2782 a.i = XT_HINT_NEWTAB;
2783 s = ",";
2784 show_cmd(t, s);
2785 cmd_setup = 1;
2786 hint(t, &a);
2787 break;
2788 default:
2789 show_oops(t, "command: invalid opcode %d", args->i);
2790 return (XT_CB_PASSTHROUGH);
2793 DNPRINTF(XT_D_CMD, "%s: tab %d type %s\n", __func__, t->tab_id, s);
2795 if (!cmd_setup)
2796 show_cmd(t, s);
2798 if (sp)
2799 g_free(sp);
2800 if (sl)
2801 g_free(sl);
2803 return (XT_CB_HANDLED);
2807 search(struct tab *t, struct karg *args)
2809 gboolean d;
2811 if (t == NULL || args == NULL) {
2812 show_oops(NULL, "search invalid parameters");
2813 return (1);
2816 switch (args->i) {
2817 case XT_SEARCH_NEXT:
2818 d = t->search_forward;
2819 break;
2820 case XT_SEARCH_PREV:
2821 d = !t->search_forward;
2822 break;
2823 default:
2824 return (XT_CB_PASSTHROUGH);
2827 if (t->search_text == NULL) {
2828 if (global_search == NULL)
2829 return (XT_CB_PASSTHROUGH);
2830 else {
2831 d = t->search_forward = TRUE;
2832 t->search_text = g_strdup(global_search);
2833 webkit_web_view_mark_text_matches(t->wv, global_search, FALSE, 0);
2834 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
2838 DNPRINTF(XT_D_CMD, "search: tab %d opc %d forw %d text %s\n",
2839 t->tab_id, args->i, t->search_forward, t->search_text);
2841 webkit_web_view_search_text(t->wv, t->search_text, FALSE, d, TRUE);
2843 return (XT_CB_HANDLED);
2847 session_save(struct tab *t, char *filename)
2849 struct karg a;
2850 int rv = 1;
2851 struct session *s;
2853 if (strlen(filename) == 0)
2854 goto done;
2856 if (filename[0] == '.' || filename[0] == '/')
2857 goto done;
2859 a.s = filename;
2860 if (save_tabs(t, &a))
2861 goto done;
2862 strlcpy(named_session, filename, sizeof named_session);
2864 /* add the new session to the list of sessions */
2865 s = g_malloc(sizeof(struct session));
2866 s->name = g_strdup(filename);
2867 TAILQ_INSERT_TAIL(&sessions, s, entry);
2869 rv = 0;
2870 done:
2871 return (rv);
2875 session_open(struct tab *t, char *filename)
2877 struct karg a;
2878 int rv = 1;
2880 if (strlen(filename) == 0)
2881 goto done;
2883 if (filename[0] == '.' || filename[0] == '/')
2884 goto done;
2886 a.s = filename;
2887 a.i = XT_SES_CLOSETABS;
2888 if (open_tabs(t, &a))
2889 goto done;
2891 strlcpy(named_session, filename, sizeof named_session);
2893 rv = 0;
2894 done:
2895 return (rv);
2899 session_delete(struct tab *t, char *filename)
2901 char file[PATH_MAX];
2902 int rv = 1;
2903 struct session *s;
2905 if (strlen(filename) == 0)
2906 goto done;
2908 if (filename[0] == '.' || filename[0] == '/')
2909 goto done;
2911 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, filename);
2912 if (unlink(file))
2913 goto done;
2915 if (!strcmp(filename, named_session))
2916 strlcpy(named_session, XT_SAVED_TABS_FILE,
2917 sizeof named_session);
2919 /* remove session from sessions list */
2920 TAILQ_FOREACH(s, &sessions, entry) {
2921 if (!strcmp(s->name, filename))
2922 break;
2924 if (s == NULL)
2925 goto done;
2926 TAILQ_REMOVE(&sessions, s, entry);
2927 g_free((gpointer) s->name);
2928 g_free(s);
2930 rv = 0;
2931 done:
2932 return (rv);
2936 session_cmd(struct tab *t, struct karg *args)
2938 char *filename = args->s;
2940 if (t == NULL)
2941 return (1);
2943 if (args->i & XT_SHOW)
2944 show_oops(t, "Current session: %s", named_session[0] == '\0' ?
2945 XT_SAVED_TABS_FILE : named_session);
2946 else if (args->i & XT_SAVE) {
2947 if (session_save(t, filename)) {
2948 show_oops(t, "Can't save session: %s",
2949 filename ? filename : "INVALID");
2950 goto done;
2952 } else if (args->i & XT_OPEN) {
2953 if (session_open(t, filename)) {
2954 show_oops(t, "Can't open session: %s",
2955 filename ? filename : "INVALID");
2956 goto done;
2958 } else if (args->i & XT_DELETE) {
2959 if (session_delete(t, filename)) {
2960 show_oops(t, "Can't delete session: %s",
2961 filename ? filename : "INVALID");
2962 goto done;
2965 done:
2966 return (XT_CB_PASSTHROUGH);
2970 script_cmd(struct tab *t, struct karg *args)
2972 struct stat sb;
2973 FILE *f = NULL;
2974 char *buf = NULL;
2976 if (t == NULL)
2977 goto done;
2979 if ((f = fopen(args->s, "r")) == NULL) {
2980 show_oops(t, "Can't open script file: %s", args->s);
2981 goto done;
2984 if (fstat(fileno(f), &sb) == -1) {
2985 show_oops(t, "Can't stat script file: %s", args->s);
2986 goto done;
2989 buf = g_malloc0(sb.st_size + 1);
2990 if (fread(buf, 1, sb.st_size, f) != sb.st_size) {
2991 show_oops(t, "Can't read script file: %s", args->s);
2992 goto done;
2995 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
2996 run_script(t, buf);
2998 done:
2999 if (f)
3000 fclose(f);
3001 if (buf)
3002 g_free(buf);
3004 return (XT_CB_PASSTHROUGH);
3008 * Make a hardcopy of the page
3011 print_page(struct tab *t, struct karg *args)
3013 WebKitWebFrame *frame;
3014 GtkPageSetup *ps;
3015 GtkPrintOperation *op;
3016 GtkPrintOperationAction action;
3017 GtkPrintOperationResult print_res;
3018 GError *g_err = NULL;
3019 int marg_l, marg_r, marg_t, marg_b;
3020 int ret = 0;
3022 DNPRINTF(XT_D_PRINTING, "%s:", __func__);
3024 ps = gtk_page_setup_new();
3025 op = gtk_print_operation_new();
3026 action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG;
3027 frame = webkit_web_view_get_main_frame(t->wv);
3029 /* the default margins are too small, so we will bump them */
3030 marg_l = gtk_page_setup_get_left_margin(ps, GTK_UNIT_MM) +
3031 XT_PRINT_EXTRA_MARGIN;
3032 marg_r = gtk_page_setup_get_right_margin(ps, GTK_UNIT_MM) +
3033 XT_PRINT_EXTRA_MARGIN;
3034 marg_t = gtk_page_setup_get_top_margin(ps, GTK_UNIT_MM) +
3035 XT_PRINT_EXTRA_MARGIN;
3036 marg_b = gtk_page_setup_get_bottom_margin(ps, GTK_UNIT_MM) +
3037 XT_PRINT_EXTRA_MARGIN;
3039 /* set margins */
3040 gtk_page_setup_set_left_margin(ps, marg_l, GTK_UNIT_MM);
3041 gtk_page_setup_set_right_margin(ps, marg_r, GTK_UNIT_MM);
3042 gtk_page_setup_set_top_margin(ps, marg_t, GTK_UNIT_MM);
3043 gtk_page_setup_set_bottom_margin(ps, marg_b, GTK_UNIT_MM);
3045 gtk_print_operation_set_default_page_setup(op, ps);
3047 print_res = webkit_web_frame_print_full(frame, op, action, &g_err);
3049 /* check it worked */
3050 if (print_res == GTK_PRINT_OPERATION_RESULT_ERROR) {
3051 show_oops(NULL, "can't print: %s", g_err->message);
3052 g_error_free (g_err);
3053 ret = 1;
3056 g_object_unref(G_OBJECT(ps));
3057 g_object_unref(G_OBJECT(op));
3058 return (ret);
3062 go_home(struct tab *t, struct karg *args)
3064 load_uri(t, home);
3065 return (0);
3069 set_encoding(struct tab *t, struct karg *args)
3071 const gchar *e;
3073 if (args->s && strlen(g_strstrip(args->s)) == 0) {
3074 e = webkit_web_view_get_custom_encoding(t->wv);
3075 if (e == NULL)
3076 e = webkit_web_view_get_encoding(t->wv);
3077 show_oops(t, "encoding: %s", e ? e : "N/A");
3078 } else
3079 webkit_web_view_set_custom_encoding(t->wv, args->s);
3081 return (0);
3085 restart(struct tab *t, struct karg *args)
3087 struct karg a;
3089 a.s = XT_RESTART_TABS_FILE;
3090 save_tabs(t, &a);
3091 execvp(start_argv[0], start_argv);
3092 /* NOTREACHED */
3094 return (0);
3097 char *http_proxy_save; /* not a setting, used to toggle */
3100 proxy_cmd(struct tab *t, struct karg *args)
3102 struct tab *tt;
3104 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
3106 if (t == NULL)
3107 return (1);
3109 /* setup */
3110 if (http_proxy) {
3111 TAILQ_FOREACH(tt, &tabs, entry)
3112 gtk_widget_show(t->proxy_toggle);
3113 if (http_proxy_save)
3114 g_free(http_proxy_save);
3115 http_proxy_save = g_strdup(http_proxy);
3118 if (args->i & XT_PRXY_SHOW) {
3119 if (http_proxy)
3120 show_oops(t, "http_proxy = %s", http_proxy);
3121 else
3122 show_oops(t, "proxy is currently disabled");
3123 } else if (args->i & XT_PRXY_TOGGLE) {
3124 if (http_proxy_save == NULL && http_proxy == NULL) {
3125 show_oops(t, "can't toggle proxy");
3126 goto done;
3128 TAILQ_FOREACH(tt, &tabs, entry)
3129 gtk_widget_show(t->proxy_toggle);
3130 if (http_proxy) {
3131 if (setup_proxy(NULL) == 0)
3132 button_set_file(t->proxy_toggle,
3133 "tordisabled.ico");
3134 show_oops(t, "http proxy disabled");
3135 } else {
3136 if (setup_proxy(http_proxy_save) == 0 && http_proxy) {
3137 button_set_file(t->proxy_toggle,
3138 "torenabled.ico");
3139 show_oops(t, "http_proxy = %s", http_proxy);
3140 } else
3141 show_oops(t, "invalid proxy: %s", http_proxy_save);
3144 done:
3145 return (XT_CB_PASSTHROUGH);
3149 * If you can read this functionthen you are a sick and twisted individual.
3150 * I hope we never meet, it'll be violent.
3152 gboolean
3153 eval_cb(const GMatchInfo *info, GString *res, gpointer data)
3155 gchar *match, *num;
3156 gint start = -1, end = -1, i;
3157 struct karg *args = data;
3160 * match contains the string UP TO the match.
3162 * res is what is returned, note that whatever remains in the sent in
3163 * string is appended on the way out.
3165 * for example /123/456/789/moo came in
3166 * match contains /123/456/789/
3167 * we assign that to res and replace /789/ with the replacement text
3168 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
3171 match = g_match_info_fetch(info, 0);
3172 if (match == NULL)
3173 goto done;
3175 if (g_match_info_fetch_pos(info, 1, &start, &end) == FALSE)
3176 goto freeit;
3178 g_string_assign(res, match);
3180 i = atoi(&match[start + 1]);
3181 if (args->i == XT_URL_PLUS)
3182 i++;
3183 else
3184 i--;
3186 /* preserve whitespace when likely */
3187 num = g_strdup_printf("%0*d", end - start - 2, i);
3188 g_string_overwrite_len(res, start + 1, num, end - start - 2);
3189 g_free(num);
3191 freeit:
3192 g_free(match);
3193 done:
3194 return (FALSE); /* doesn't matter */
3198 urlmod_cmd(struct tab *t, struct karg *args)
3200 const gchar *uri;
3201 GRegex *reg;
3202 gchar *res;
3204 if (t == NULL)
3205 goto done;
3206 if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) == NULL)
3207 goto done;
3208 if (strlen(uri) == 0)
3209 goto done;
3211 reg = g_regex_new(".*(/[0-9]+/)", 0, 0, NULL);
3212 if (reg == NULL)
3213 goto done;
3214 res = g_regex_replace_eval(reg, uri, -1, 0, 0, eval_cb, args, NULL);
3215 if (res == NULL)
3216 goto free_reg;
3218 if (!strcmp(res, uri))
3219 goto free_res;
3221 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), res);
3222 activate_uri_entry_cb(t->uri_entry, t);
3224 free_res:
3225 g_free(res);
3226 free_reg:
3227 g_regex_unref(reg);
3228 done:
3229 return (XT_CB_PASSTHROUGH);
3232 struct cmd {
3233 char *cmd;
3234 int level;
3235 int (*func)(struct tab *, struct karg *);
3236 int arg;
3237 int type;
3238 } cmds[] = {
3239 { "command_mode", 0, command_mode, XT_MODE_COMMAND, 0 },
3240 { "insert_mode", 0, command_mode, XT_MODE_INSERT, 0 },
3241 { "command", 0, command, ':', 0 },
3242 { "search", 0, command, '/', 0 },
3243 { "searchb", 0, command, '?', 0 },
3244 { "hinting", 0, command, '.', 0 },
3245 { "hinting_newtab", 0, command, ',', 0 },
3246 { "togglesrc", 0, toggle_src, 0, 0 },
3247 { "editsrc", 0, edit_src, 0, 0 },
3248 { "editelement", 0, edit_element, 0, 0 },
3249 { "passthrough", 0, passthrough, 0, 0 },
3250 { "modurl", 0, modurl, 0, 0 },
3252 /* yanking and pasting */
3253 { "yankuri", 0, yank_uri, 0, 0 },
3254 { "pasteuricur", 0, paste_uri, XT_PASTE_CURRENT_TAB, 0 },
3255 { "pasteurinew", 0, paste_uri, XT_PASTE_NEW_TAB, 0 },
3257 /* search */
3258 { "searchnext", 0, search, XT_SEARCH_NEXT, 0 },
3259 { "searchprevious", 0, search, XT_SEARCH_PREV, 0 },
3261 /* focus */
3262 { "focusaddress", 0, focus, XT_FOCUS_URI, 0 },
3263 { "focussearch", 0, focus, XT_FOCUS_SEARCH, 0 },
3265 /* hinting */
3266 { "hinting", 0, hint, 0, 0 },
3267 { "hinting_newtab", 0, hint, XT_HINT_NEWTAB, 0 },
3269 /* custom stylesheet */
3270 { "userstyle", 0, userstyle_cmd, XT_STYLE_CURRENT_TAB, XT_USERARG },
3271 { "userstyle_global", 0, userstyle_cmd, XT_STYLE_GLOBAL, XT_USERARG },
3273 /* navigation */
3274 { "goback", 0, navaction, XT_NAV_BACK, 0 },
3275 { "goforward", 0, navaction, XT_NAV_FORWARD, 0 },
3276 { "reload", 0, navaction, XT_NAV_RELOAD, 0 },
3277 { "stop", 0, navaction, XT_NAV_STOP, 0 },
3279 /* vertical movement */
3280 { "scrolldown", 0, move, XT_MOVE_DOWN, 0 },
3281 { "scrollup", 0, move, XT_MOVE_UP, 0 },
3282 { "scrollbottom", 0, move, XT_MOVE_BOTTOM, 0 },
3283 { "scrolltop", 0, move, XT_MOVE_TOP, 0 },
3284 { "1", 0, move, XT_MOVE_TOP, 0 },
3285 { "scrollhalfdown", 0, move, XT_MOVE_HALFDOWN, 0 },
3286 { "scrollhalfup", 0, move, XT_MOVE_HALFUP, 0 },
3287 { "scrollpagedown", 0, move, XT_MOVE_PAGEDOWN, 0 },
3288 { "scrollpageup", 0, move, XT_MOVE_PAGEUP, 0 },
3289 /* horizontal movement */
3290 { "scrollright", 0, move, XT_MOVE_RIGHT, 0 },
3291 { "scrollleft", 0, move, XT_MOVE_LEFT, 0 },
3292 { "scrollfarright", 0, move, XT_MOVE_FARRIGHT, 0 },
3293 { "scrollfarleft", 0, move, XT_MOVE_FARLEFT, 0 },
3295 { "favorites", 0, xtp_page_fl, XT_SHOW, 0 },
3296 { "fav", 0, xtp_page_fl, XT_SHOW, 0 },
3297 { "favedit", 0, xtp_page_fl, XT_SHOW|XT_DELETE, 0 },
3298 { "favadd", 0, add_favorite, 0, XT_USERARG },
3300 { "qall", 0, quit, 0, 0 },
3301 { "quitall", 0, quit, 0, 0 },
3302 { "w", 0, save_tabs, 0, 0 },
3303 { "wq", 0, save_tabs_and_quit, 0, 0 },
3304 { "help", 0, help, 0, 0 },
3305 { "about", 0, xtp_page_ab, 0, 0 },
3306 { "stats", 0, stats, 0, 0 },
3307 { "version", 0, xtp_page_ab, 0, 0 },
3309 /* js command */
3310 { "js", 0, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3311 { "save", 1, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3312 { "domain", 2, js_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3313 { "fqdn", 2, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3314 { "show", 1, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3315 { "all", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3316 { "persistent", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3317 { "session", 2, js_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3318 { "toggle", 1, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3319 { "domain", 2, js_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3320 { "fqdn", 2, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3322 /* cookie command */
3323 { "cookie", 0, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3324 { "save", 1, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3325 { "domain", 2, cookie_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3326 { "fqdn", 2, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3327 { "show", 1, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3328 { "all", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3329 { "persistent", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3330 { "session", 2, cookie_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3331 { "toggle", 1, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3332 { "domain", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3333 { "fqdn", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3334 { "purge", 1, cookie_cmd, XT_DELETE, 0 },
3336 /* plugin command */
3337 { "plugin", 0, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3338 { "save", 1, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3339 { "domain", 2, pl_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3340 { "fqdn", 2, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3341 { "show", 1, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3342 { "all", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3343 { "persistent", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3344 { "session", 2, pl_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3345 { "toggle", 1, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3346 { "domain", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3347 { "fqdn", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3349 /* https command */
3350 { "https", 0, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3351 { "save", 1, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3352 { "domain", 2, https_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3353 { "fqdn", 2, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3354 { "show", 1, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3355 { "all", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3356 { "persistent", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3357 { "session", 2, https_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3358 { "toggle", 1, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3359 { "domain", 2, https_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3360 { "fqdn", 2, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3362 /* toplevel (domain) command */
3363 { "toplevel", 0, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3364 { "toggle", 1, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3366 /* cookie jar */
3367 { "cookiejar", 0, xtp_page_cl, 0, 0 },
3369 /* cert command */
3370 { "cert", 0, cert_cmd, XT_SHOW, 0 },
3371 { "save", 1, cert_cmd, XT_SAVE, 0 },
3372 { "show", 1, cert_cmd, XT_SHOW, 0 },
3374 { "ca", 0, ca_cmd, 0, 0 },
3375 { "downloadmgr", 0, xtp_page_dl, 0, 0 },
3376 { "dl", 0, xtp_page_dl, 0, 0 },
3377 { "h", 0, xtp_page_hl, 0, 0 },
3378 { "history", 0, xtp_page_hl, 0, 0 },
3379 { "home", 0, go_home, 0, 0 },
3380 { "restart", 0, restart, 0, 0 },
3381 { "urlhide", 0, urlaction, XT_URL_HIDE, 0 },
3382 { "urlshow", 0, urlaction, XT_URL_SHOW, 0 },
3383 { "statustoggle", 0, statustoggle, 0, 0 },
3384 { "run_script", 0, run_page_script, 0, XT_USERARG },
3386 { "print", 0, print_page, 0, 0 },
3388 /* tabs */
3389 { "focusin", 0, resizetab, XT_ZOOM_IN, 0 },
3390 { "focusout", 0, resizetab, XT_ZOOM_OUT, 0 },
3391 { "focusreset", 0, resizetab, XT_ZOOM_NORMAL, 0 },
3392 { "q", 0, tabaction, XT_TAB_DELQUIT, 0 },
3393 { "quit", 0, tabaction, XT_TAB_DELQUIT, 0 },
3394 { "open", 0, tabaction, XT_TAB_OPEN, XT_URLARG },
3395 { "tabclose", 0, tabaction, XT_TAB_DELETE, XT_PREFIX | XT_INTARG},
3396 { "tabedit", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3397 { "tabfirst", 0, movetab, XT_TAB_FIRST, 0 },
3398 { "tabhide", 0, tabaction, XT_TAB_HIDE, 0 },
3399 { "tablast", 0, movetab, XT_TAB_LAST, 0 },
3400 { "tabnew", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3401 { "tabnext", 0, movetab, XT_TAB_NEXT, XT_PREFIX | XT_INTARG},
3402 { "tabnextstyle", 0, tabaction, XT_TAB_NEXTSTYLE, 0 },
3403 { "tabonly", 0, tabaction, XT_TAB_ONLY, 0 },
3404 { "tabprevious", 0, movetab, XT_TAB_PREV, XT_PREFIX | XT_INTARG},
3405 { "tabrewind", 0, movetab, XT_TAB_FIRST, 0 },
3406 { "tabshow", 0, tabaction, XT_TAB_SHOW, 0 },
3407 { "tabs", 0, buffers, 0, 0 },
3408 { "tabundoclose", 0, tabaction, XT_TAB_UNDO_CLOSE, 0 },
3409 { "buffers", 0, buffers, 0, 0 },
3410 { "ls", 0, buffers, 0, 0 },
3411 { "encoding", 0, set_encoding, 0, XT_USERARG },
3412 { "loadimages", 0, tabaction, XT_TAB_LOAD_IMAGES, 0 },
3414 /* settings */
3415 { "set", 0, set, 0, XT_SETARG },
3416 { "runtime", 0, xtp_page_rt, 0, 0 },
3418 { "fullscreen", 0, fullscreen, 0, 0 },
3419 { "f", 0, fullscreen, 0, 0 },
3421 /* sessions */
3422 { "session", 0, session_cmd, XT_SHOW, 0 },
3423 { "delete", 1, session_cmd, XT_DELETE, XT_SESSARG },
3424 { "open", 1, session_cmd, XT_OPEN, XT_SESSARG },
3425 { "save", 1, session_cmd, XT_SAVE, XT_USERARG },
3426 { "show", 1, session_cmd, XT_SHOW, 0 },
3428 /* external javascript */
3429 { "script", 0, script_cmd, XT_EJS_SHOW, XT_USERARG },
3431 /* inspector */
3432 { "inspector", 0, inspector_cmd, XT_INS_SHOW, 0 },
3433 { "show", 1, inspector_cmd, XT_INS_SHOW, 0 },
3434 { "hide", 1, inspector_cmd, XT_INS_HIDE, 0 },
3436 /* proxy */
3437 { "proxy", 0, proxy_cmd, XT_PRXY_SHOW, 0 },
3438 { "show", 1, proxy_cmd, XT_PRXY_SHOW, 0 },
3439 { "toggle", 1, proxy_cmd, XT_PRXY_TOGGLE, 0 },
3441 /* url mod */
3442 { "urlmod", 0, urlmod_cmd, XT_URL, 0 },
3443 { "plus", 1, urlmod_cmd, XT_URL_PLUS, 0 },
3444 { "min", 1, urlmod_cmd, XT_URL_MIN, 0 },
3447 struct {
3448 int index;
3449 int len;
3450 gchar *list[256];
3451 } cmd_status = {-1, 0};
3453 gboolean
3454 wv_release_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3457 if (e->type == GDK_BUTTON_RELEASE && e->button == 1)
3458 btn_down = 0;
3460 return (FALSE);
3463 gboolean
3464 wv_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3466 struct karg a;
3467 WebKitHitTestResult *hit_test_result;
3468 guint context;
3470 hit_test_result = webkit_web_view_get_hit_test_result(t->wv, e);
3471 g_object_get(hit_test_result, "context", &context, NULL);
3472 g_object_unref(G_OBJECT(hit_test_result));
3474 hide_oops(t);
3475 hide_buffers(t);
3477 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE)
3478 t->mode = XT_MODE_INSERT;
3479 else
3480 t->mode = XT_MODE_COMMAND;
3482 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3483 btn_down = 1;
3484 else if (e->type == GDK_BUTTON_PRESS && e->button == 8 /* btn 4 */) {
3485 /* go backward */
3486 a.i = XT_NAV_BACK;
3487 navaction(t, &a);
3489 return (TRUE);
3490 } else if (e->type == GDK_BUTTON_PRESS && e->button == 9 /* btn 5 */) {
3491 /* go forward */
3492 a.i = XT_NAV_FORWARD;
3493 navaction(t, &a);
3495 return (TRUE);
3498 return (FALSE);
3501 void
3502 tab_close_cb(GtkWidget *btn, struct tab *t)
3504 DNPRINTF(XT_D_TAB, "tab_close_cb: tab %d\n", t->tab_id);
3506 delete_tab(t);
3510 parse_custom_uri(struct tab *t, const char *uri)
3512 struct custom_uri *u;
3513 int handled = 0;
3514 char *sv[3];
3516 TAILQ_FOREACH(u, &cul, entry) {
3517 if (strncmp(uri, u->uri, strlen(u->uri)))
3518 continue;
3520 handled = 1;
3521 sv[0] = u->cmd;
3522 sv[1] = (char *)uri;
3523 sv[2] = NULL;
3524 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL,
3525 NULL, NULL, NULL))
3526 show_oops(t, "%s: could not spawn process", __func__);
3529 return (handled);
3532 void
3533 activate_uri_entry_cb(GtkWidget* entry, struct tab *t)
3535 const gchar *uri = gtk_entry_get_text(GTK_ENTRY(entry));
3537 DNPRINTF(XT_D_URL, "activate_uri_entry_cb: %s\n", uri);
3539 if (t == NULL) {
3540 show_oops(NULL, "activate_uri_entry_cb invalid parameters");
3541 return;
3544 if (uri == NULL) {
3545 show_oops(t, "activate_uri_entry_cb no uri");
3546 return;
3549 uri += strspn(uri, "\t ");
3551 if (parse_custom_uri(t, uri))
3552 return;
3554 /* otherwise continue to load page normally */
3555 load_uri(t, (gchar *)uri);
3556 focus_webview(t);
3559 void
3560 activate_search_entry_cb(GtkWidget* entry, struct tab *t)
3562 const gchar *search = gtk_entry_get_text(GTK_ENTRY(entry));
3563 char *newuri = NULL;
3564 gchar *enc_search;
3565 char **sv;
3567 DNPRINTF(XT_D_URL, "activate_search_entry_cb: %s\n", search);
3569 if (t == NULL) {
3570 show_oops(NULL, "activate_search_entry_cb invalid parameters");
3571 return;
3574 if (search_string == NULL || strlen(search_string) == 0) {
3575 show_oops(t, "no search_string");
3576 return;
3579 set_normal_tab_meaning(t);
3581 enc_search = soup_uri_encode(search, XT_RESERVED_CHARS);
3582 sv = g_strsplit(search_string, "%s", 2);
3583 newuri = g_strjoinv(enc_search, sv);
3584 g_free(enc_search);
3585 g_strfreev(sv);
3587 marks_clear(t);
3588 load_uri(t, newuri);
3589 focus_webview(t);
3591 if (newuri)
3592 g_free(newuri);
3595 void
3596 check_and_set_cookie(const gchar *uri, struct tab *t)
3598 struct wl_entry *w = NULL;
3599 int es = 0;
3601 if (uri == NULL || t == NULL)
3602 return;
3604 if ((w = wl_find_uri(uri, &c_wl)) == NULL)
3605 es = 0;
3606 else
3607 es = 1;
3609 DNPRINTF(XT_D_COOKIE, "check_and_set_cookie: %s %s\n",
3610 es ? "enable" : "disable", uri);
3612 g_object_set(G_OBJECT(t->settings),
3613 "enable-html5-local-storage", es, (char *)NULL);
3614 webkit_web_view_set_settings(t->wv, t->settings);
3617 void
3618 check_and_set_js(const gchar *uri, struct tab *t)
3620 struct wl_entry *w = NULL;
3621 int es = 0;
3623 if (uri == NULL || t == NULL)
3624 return;
3626 if ((w = wl_find_uri(uri, &js_wl)) == NULL)
3627 es = 0;
3628 else
3629 es = 1;
3631 DNPRINTF(XT_D_JS, "check_and_set_js: %s %s\n",
3632 es ? "enable" : "disable", uri);
3634 g_object_set(G_OBJECT(t->settings),
3635 "enable-scripts", es, (char *)NULL);
3636 webkit_web_view_set_settings(t->wv, t->settings);
3638 button_set_icon_name(t->js_toggle,
3639 es ? "media-playback-start" : "media-playback-pause");
3642 void
3643 check_and_set_pl(const gchar *uri, struct tab *t)
3645 struct wl_entry *w = NULL;
3646 int es = 0;
3648 if (uri == NULL || t == NULL)
3649 return;
3651 if ((w = wl_find_uri(uri, &pl_wl)) == NULL)
3652 es = 0;
3653 else
3654 es = 1;
3656 DNPRINTF(XT_D_JS, "check_and_set_pl: %s %s\n",
3657 es ? "enable" : "disable", uri);
3659 g_object_set(G_OBJECT(t->settings),
3660 "enable-plugins", es, (char *)NULL);
3661 webkit_web_view_set_settings(t->wv, t->settings);
3664 #if GTK_CHECK_VERSION(3, 0, 0)
3665 /* A lot of this can be removed when gtk2 is dropped on the floor */
3666 char *
3667 get_css_name(const char *col_str)
3669 char *name = NULL;
3671 if (!strcmp(col_str, XT_COLOR_WHITE))
3672 name = g_strdup(XT_CSS_NORMAL);
3673 else if (!strcmp(col_str, XT_COLOR_RED))
3674 name = g_strdup(XT_CSS_RED);
3675 else if (!strcmp(col_str, XT_COLOR_YELLOW))
3676 name = g_strdup(XT_CSS_YELLOW);
3677 else if (!strcmp(col_str, XT_COLOR_GREEN))
3678 name = g_strdup(XT_CSS_GREEN);
3679 else if (!strcmp(col_str, XT_COLOR_BLUE))
3680 name = g_strdup(XT_CSS_BLUE);
3681 return (name);
3683 #endif
3685 void
3686 show_ca_status(struct tab *t, const char *uri)
3688 char domain[8182], file[PATH_MAX];
3689 SoupMessage *msg = NULL;
3690 GTlsCertificate *cert = NULL;
3691 GTlsCertificateFlags flags = 0;
3692 gchar *col_str = XT_COLOR_RED;
3693 char *cut_uri;
3694 char *chain;
3695 char *s;
3696 #if GTK_CHECK_VERSION(3, 0, 0)
3697 char *name;
3698 #else
3699 GdkColor color;
3700 gchar *text, *base;
3701 #endif
3702 enum cert_trust trust;
3703 int nocolor = 0;
3704 int i;
3706 DNPRINTF(XT_D_URL, "show_ca_status: %d %s %s\n",
3707 ssl_strict_certs, ssl_ca_file, uri);
3709 if (t == NULL)
3710 return;
3712 if (uri == NULL || g_str_has_prefix(uri, "http://") ||
3713 !g_str_has_prefix(uri, "https://"))
3714 return;
3717 * Cut the uri to get the certs off the homepage. We can't use the
3718 * full URI here since it may include arguments and we don't want to make
3719 * these requests multiple times.
3721 cut_uri = g_strdup(uri);
3722 s = cut_uri;
3723 for (i = 0; i < 3; ++i)
3724 s = strchr(&(s[1]), '/');
3725 s[1] = '\0';
3727 msg = soup_message_new("HEAD", cut_uri);
3728 g_free(cut_uri);
3729 if (msg == NULL)
3730 return;
3731 soup_message_set_flags(msg, SOUP_MESSAGE_NO_REDIRECT);
3732 soup_session_send_message(session, msg);
3733 if (msg->status_code == SOUP_STATUS_SSL_FAILED ||
3734 msg->status_code == SOUP_STATUS_TLS_FAILED) {
3735 DNPRINTF(XT_D_URL, "%s: status not ok: %d\n", uri,
3736 msg->status_code);
3737 goto done;
3739 if (!soup_message_get_https_status(msg, &cert, &flags)) {
3740 DNPRINTF(XT_D_URL, "%s: invalid response\n", uri);
3741 goto done;
3743 if (!G_IS_TLS_CERTIFICATE(cert)) {
3744 DNPRINTF(XT_D_URL, "%s: no cert\n", uri);
3745 goto done;
3748 if (flags == 0)
3749 col_str = XT_COLOR_GREEN;
3750 else
3751 col_str = XT_COLOR_YELLOW;
3753 strlcpy(domain, uri + strlen("https://"), sizeof domain);
3754 for (i = 0; i < strlen(domain); i++)
3755 if (domain[i] == '/') {
3756 domain[i] = '\0';
3757 break;
3760 snprintf(file, sizeof file, "%s" PS "%s", certs_dir, domain);
3761 chain = g_strdup("");
3762 if ((trust = check_local_certs(file, cert, &chain)) == CERT_LOCAL)
3763 col_str = XT_COLOR_BLUE;
3764 if (t->pem)
3765 g_free(t->pem);
3766 t->pem = chain;
3768 snprintf(file, sizeof file, "%s" PS "%s", certs_cache_dir, domain);
3769 if (warn_cert_changes) {
3770 if (check_cert_changes(t, cert, file, uri))
3771 nocolor = 1;
3774 done:
3775 g_object_unref(msg);
3776 if (!strcmp(col_str, XT_COLOR_WHITE) || nocolor) {
3777 #if GTK_CHECK_VERSION(3, 0, 0)
3778 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
3779 statusbar_modify_attr(t, XT_CSS_NORMAL);
3780 #else
3781 text = gdk_color_to_string(
3782 &t->default_style->text[GTK_STATE_NORMAL]);
3783 base = gdk_color_to_string(
3784 &t->default_style->base[GTK_STATE_NORMAL]);
3785 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
3786 &t->default_style->base[GTK_STATE_NORMAL]);
3787 statusbar_modify_attr(t, text, base);
3788 g_free(text);
3789 g_free(base);
3790 #endif
3791 } else {
3792 #if GTK_CHECK_VERSION(3, 0, 0)
3793 name = get_css_name(col_str);
3794 gtk_widget_set_name(t->uri_entry, name);
3795 statusbar_modify_attr(t, name);
3796 g_free(name);
3797 #else
3798 gdk_color_parse(col_str, &color);
3799 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
3800 statusbar_modify_attr(t, XT_COLOR_BLACK, col_str);
3801 #endif
3805 void
3806 free_favicon(struct tab *t)
3808 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p req %p\n",
3809 __func__, t->icon_download, t->icon_request);
3811 if (t->icon_request)
3812 g_object_unref(t->icon_request);
3813 if (t->icon_dest_uri)
3814 g_free(t->icon_dest_uri);
3816 t->icon_request = NULL;
3817 t->icon_dest_uri = NULL;
3820 void
3821 xt_icon_from_name(struct tab *t, gchar *name)
3823 if (!enable_favicon_entry)
3824 return;
3826 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->uri_entry),
3827 GTK_ENTRY_ICON_PRIMARY, "text-html");
3828 if (show_url == 0)
3829 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3830 GTK_ENTRY_ICON_PRIMARY, "text-html");
3831 else
3832 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3833 GTK_ENTRY_ICON_PRIMARY, NULL);
3836 void
3837 xt_icon_from_pixbuf(struct tab *t, GdkPixbuf *pb)
3839 GdkPixbuf *pb_scaled;
3841 if (gdk_pixbuf_get_width(pb) > 16 || gdk_pixbuf_get_height(pb) > 16)
3842 pb_scaled = gdk_pixbuf_scale_simple(pb, 16, 16,
3843 GDK_INTERP_BILINEAR);
3844 else
3845 pb_scaled = pb;
3847 if (enable_favicon_entry) {
3849 /* Classic tabs. */
3850 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->uri_entry),
3851 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3853 /* Minimal tabs. */
3854 if (show_url == 0) {
3855 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
3856 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3857 } else
3858 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3859 GTK_ENTRY_ICON_PRIMARY, NULL);
3862 /* XXX: Only supports the minimal tabs atm. */
3863 if (enable_favicon_tabs)
3864 gtk_image_set_from_pixbuf(GTK_IMAGE(t->tab_elems.favicon),
3865 pb_scaled);
3867 if (pb_scaled != pb)
3868 g_object_unref(pb_scaled);
3871 void
3872 xt_icon_from_file(struct tab *t, char *uri)
3874 GdkPixbuf *pb;
3875 char *file;
3877 if (g_str_has_prefix(uri, "file://"))
3878 file = g_filename_from_uri(uri, NULL, NULL);
3879 else
3880 file = g_strdup(uri);
3882 if (file == NULL)
3883 return;
3885 pb = gdk_pixbuf_new_from_file(file, NULL);
3886 if (pb) {
3887 xt_icon_from_pixbuf(t, pb);
3888 g_object_unref(pb);
3889 } else
3890 xt_icon_from_name(t, "text-html");
3892 g_free(file);
3895 gboolean
3896 is_valid_icon(char *file)
3898 gboolean valid = 0;
3899 const char *mime_type;
3900 GFileInfo *fi;
3901 GFile *gf;
3903 gf = g_file_new_for_path(file);
3904 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
3905 NULL, NULL);
3906 mime_type = g_file_info_get_content_type(fi);
3907 valid = g_strcmp0(mime_type, "image/x-ico") == 0 ||
3908 g_strcmp0(mime_type, "image/vnd.microsoft.icon") == 0 ||
3909 g_strcmp0(mime_type, "image/png") == 0 ||
3910 g_strcmp0(mime_type, "image/gif") == 0 ||
3911 g_strcmp0(mime_type, "application/octet-stream") == 0;
3912 g_object_unref(fi);
3913 g_object_unref(gf);
3915 return (valid);
3918 void
3919 set_favicon_from_file(struct tab *t, char *uri)
3921 struct stat sb;
3922 char *file;
3924 if (t == NULL || uri == NULL)
3925 return;
3927 if (g_str_has_prefix(uri, "file://"))
3928 file = g_filename_from_uri(uri, NULL, NULL);
3929 else
3930 file = g_strdup(uri);
3932 if (file == NULL)
3933 return;
3935 DNPRINTF(XT_D_DOWNLOAD, "%s: loading %s\n", __func__, file);
3937 if (!stat(file, &sb)) {
3938 if (sb.st_size == 0 || !is_valid_icon(file)) {
3939 /* corrupt icon so trash it */
3940 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
3941 __func__, file);
3942 unlink(file);
3943 /* no need to set icon to default here */
3944 goto done;
3947 xt_icon_from_file(t, file);
3948 done:
3949 g_free(file);
3952 void
3953 favicon_download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
3954 WebKitWebView *wv)
3956 WebKitDownloadStatus status = webkit_download_get_status(download);
3957 struct tab *tt = NULL, *t = NULL;
3960 * find the webview instead of passing in the tab as it could have been
3961 * deleted from underneath us.
3963 TAILQ_FOREACH(tt, &tabs, entry) {
3964 if (tt->wv == wv) {
3965 t = tt;
3966 break;
3969 if (t == NULL)
3970 return;
3972 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d status %d\n",
3973 __func__, t->tab_id, status);
3975 switch (status) {
3976 case WEBKIT_DOWNLOAD_STATUS_ERROR:
3977 /* -1 */
3978 t->icon_download = NULL;
3979 free_favicon(t);
3980 break;
3981 case WEBKIT_DOWNLOAD_STATUS_CREATED:
3982 /* 0 */
3983 break;
3984 case WEBKIT_DOWNLOAD_STATUS_STARTED:
3985 /* 1 */
3986 break;
3987 case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
3988 /* 2 */
3989 DNPRINTF(XT_D_DOWNLOAD, "%s: freeing favicon %d\n",
3990 __func__, t->tab_id);
3991 t->icon_download = NULL;
3992 free_favicon(t);
3993 break;
3994 case WEBKIT_DOWNLOAD_STATUS_FINISHED:
3995 /* 3 */
3997 DNPRINTF(XT_D_DOWNLOAD, "%s: setting icon to %s\n",
3998 __func__, t->icon_dest_uri);
3999 set_favicon_from_file(t, t->icon_dest_uri);
4000 /* these will be freed post callback */
4001 t->icon_request = NULL;
4002 t->icon_download = NULL;
4003 break;
4004 default:
4005 break;
4009 void
4010 abort_favicon_download(struct tab *t)
4012 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p\n", __func__, t->icon_download);
4014 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
4015 if (t->icon_download) {
4016 g_signal_handlers_disconnect_by_func(G_OBJECT(t->icon_download),
4017 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
4018 webkit_download_cancel(t->icon_download);
4019 t->icon_download = NULL;
4020 } else
4021 free_favicon(t);
4022 #endif
4024 xt_icon_from_name(t, "text-html");
4027 void
4028 notify_icon_loaded_cb(WebKitWebView *wv, gchar *uri, struct tab *t)
4030 DNPRINTF(XT_D_DOWNLOAD, "%s %s\n", __func__, uri);
4032 if (uri == NULL || t == NULL)
4033 return;
4035 #if WEBKIT_CHECK_VERSION(1, 4, 0)
4036 /* take icon from WebKitIconDatabase */
4037 GdkPixbuf *pb = NULL;
4039 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
4040 #if WEBKIT_CHECK_VERSION(1, 8, 0)
4042 * If the page was not loaded (for example, via ssl_strict_certs), do
4043 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
4044 * glib warning.
4046 if (wv && webkit_web_view_get_uri(wv))
4047 pb = webkit_web_view_try_get_favicon_pixbuf(wv, 0, 0);
4048 #else
4049 if (wv && webkit_web_view_get_uri(wv))
4050 pb = webkit_web_view_get_icon_pixbuf(wv);
4051 #endif
4052 if (pb) {
4053 xt_icon_from_pixbuf(t, pb);
4054 g_object_unref(pb);
4055 } else
4056 xt_icon_from_name(t, "text-html");
4057 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
4058 /* download icon to cache dir */
4059 gchar *name_hash, file[PATH_MAX];
4060 struct stat sb;
4062 if (t->icon_request) {
4063 DNPRINTF(XT_D_DOWNLOAD, "%s: download in progress\n", __func__);
4064 return;
4067 /* check to see if we got the icon in cache */
4068 name_hash = g_compute_checksum_for_string(G_CHECKSUM_SHA256, uri, -1);
4069 snprintf(file, sizeof file, "%s" PS "%s.ico", cache_dir, name_hash);
4070 g_free(name_hash);
4072 if (!stat(file, &sb)) {
4073 if (sb.st_size > 0) {
4074 DNPRINTF(XT_D_DOWNLOAD, "%s: loading from cache %s\n",
4075 __func__, file);
4076 set_favicon_from_file(t, file);
4077 return;
4080 /* corrupt icon so trash it */
4081 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
4082 __func__, file);
4083 unlink(file);
4086 /* create download for icon */
4087 t->icon_request = webkit_network_request_new(uri);
4088 if (t->icon_request == NULL) {
4089 DNPRINTF(XT_D_DOWNLOAD, "%s: invalid uri %s\n",
4090 __func__, uri);
4091 return;
4094 t->icon_download = webkit_download_new(t->icon_request);
4095 if (t->icon_download == NULL)
4096 return;
4098 /* we have to free icon_dest_uri later */
4099 if ((t->icon_dest_uri = g_filename_to_uri(file, NULL, NULL)) == NULL)
4100 return;
4101 webkit_download_set_destination_uri(t->icon_download,
4102 t->icon_dest_uri);
4104 if (webkit_download_get_status(t->icon_download) ==
4105 WEBKIT_DOWNLOAD_STATUS_ERROR) {
4106 g_object_unref(t->icon_request);
4107 g_free(t->icon_dest_uri);
4108 t->icon_request = NULL;
4109 t->icon_dest_uri = NULL;
4110 return;
4113 g_signal_connect(G_OBJECT(t->icon_download), "notify::status",
4114 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
4116 webkit_download_start(t->icon_download);
4117 #endif
4120 void
4121 notify_load_status_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4123 const gchar *uri = NULL;
4124 struct history *h, find;
4125 struct karg a;
4126 gchar *tmp_uri = NULL;
4127 #if !GTK_CHECK_VERSION(3, 0, 0)
4128 gchar *text, *base;
4129 #endif
4131 DNPRINTF(XT_D_URL, "notify_load_status_cb: %d %s\n",
4132 webkit_web_view_get_load_status(wview),
4133 get_uri(t) ? get_uri(t) : "NOTHING");
4135 if (t == NULL) {
4136 show_oops(NULL, "notify_load_status_cb invalid parameters");
4137 return;
4140 switch (webkit_web_view_get_load_status(wview)) {
4141 case WEBKIT_LOAD_PROVISIONAL:
4142 /* 0 */
4143 abort_favicon_download(t);
4144 #if GTK_CHECK_VERSION(2, 20, 0)
4145 gtk_widget_show(t->spinner);
4146 gtk_spinner_start(GTK_SPINNER(t->spinner));
4147 #endif
4148 t->download_requested = 0;
4150 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), TRUE);
4152 /* assume we are a new address */
4153 #if GTK_CHECK_VERSION(3, 0, 0)
4154 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
4155 statusbar_modify_attr(t, XT_CSS_NORMAL);
4156 #else
4157 text = gdk_color_to_string(
4158 &t->default_style->text[GTK_STATE_NORMAL]);
4159 base = gdk_color_to_string(
4160 &t->default_style->base[GTK_STATE_NORMAL]);
4161 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
4162 &t->default_style->base[GTK_STATE_NORMAL]);
4163 statusbar_modify_attr(t, text, base);
4164 g_free(text);
4165 g_free(base);
4166 #endif
4168 /* DOM is changing, unreference the previous focused element */
4169 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4170 if (t->active)
4171 g_object_unref(t->active);
4172 t->active = NULL;
4173 if (t->active_text) {
4174 g_free(t->active_text);
4175 t->active_text = NULL;
4177 #endif
4179 /* take focus if we are visible */
4180 focus_webview(t);
4181 t->focus_wv = 1;
4183 marks_clear(t);
4184 break;
4186 case WEBKIT_LOAD_COMMITTED:
4187 /* 1 */
4188 uri = get_uri(t);
4189 if (uri == NULL)
4190 return;
4191 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), uri);
4193 if (t->status) {
4194 g_free(t->status);
4195 t->status = NULL;
4197 set_status(t, "Loading: %s", (char *)uri);
4199 /* clear t->item, except if we're switching to an about: page */
4200 if (t->item && !g_str_has_prefix(uri, "xxxt://") &&
4201 !g_str_has_prefix(uri, "about:")) {
4202 g_object_unref(t->item);
4203 t->item = NULL;
4206 /* check if js white listing is enabled */
4207 if (enable_plugin_whitelist)
4208 check_and_set_pl(uri, t);
4209 if (enable_cookie_whitelist)
4210 check_and_set_cookie(uri, t);
4211 if (enable_js_whitelist)
4212 check_and_set_js(uri, t);
4214 if (t->styled)
4215 apply_style(t);
4218 /* we know enough to autosave the session */
4219 if (session_autosave) {
4220 a.s = NULL;
4221 save_tabs(t, &a);
4224 show_ca_status(t, uri);
4225 run_script(t, JS_HINTING);
4226 if (enable_autoscroll)
4227 run_script(t, JS_AUTOSCROLL);
4228 break;
4230 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
4231 /* 3 */
4232 if (color_visited_uris) {
4233 color_visited(t, color_visited_helper());
4236 * This colors the links you middle-click (open in new
4237 * tab) in the current tab.
4239 if (t->tab_id != gtk_notebook_get_current_page(notebook) &&
4240 (uri = get_uri(t)) != NULL)
4241 color_visited(get_current_tab(),
4242 g_strdup_printf("{'%s' : 'dummy'}", uri));
4244 break;
4246 case WEBKIT_LOAD_FINISHED:
4247 /* 2 */
4248 if ((uri = get_uri(t)) == NULL)
4249 return;
4251 * js_autorun calls get_uri which frees t->tmp_uri if on an
4252 * "about:" page. On "about:" pages, uri points to t->tmp_uri.
4253 * I.e. we will use freed memory. Prevent that.
4255 tmp_uri = g_strdup(uri);
4257 /* autorun some js if enabled */
4258 js_autorun(t);
4260 input_autofocus(t);
4262 if (!strncmp(tmp_uri, "http://", strlen("http://")) ||
4263 !strncmp(tmp_uri, "https://", strlen("https://")) ||
4264 !strncmp(tmp_uri, "file://", strlen("file://"))) {
4265 find.uri = (gchar *)tmp_uri;
4266 h = RB_FIND(history_list, &hl, &find);
4267 if (!h)
4268 insert_history_item(tmp_uri,
4269 get_title(t, FALSE), time(NULL));
4270 else
4271 h->time = time(NULL);
4274 if (statusbar_style == XT_STATUSBAR_URL)
4275 set_status(t, "%s", (char *)tmp_uri);
4276 else
4277 set_status(t, "%s", get_title(t, FALSE));
4278 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4279 #if GTK_CHECK_VERSION(2, 20, 0)
4280 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4281 gtk_widget_hide(t->spinner);
4282 #endif
4283 g_free(tmp_uri);
4284 break;
4286 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4287 case WEBKIT_LOAD_FAILED:
4288 /* 4 */
4289 if (!t->download_requested) {
4290 gtk_label_set_text(GTK_LABEL(t->label),
4291 get_title(t, FALSE));
4292 gtk_label_set_text(GTK_LABEL(t->tab_elems.label),
4293 get_title(t, FALSE));
4294 set_status(t, "%s", (char *)get_title(t, FALSE));
4295 gtk_window_set_title(GTK_WINDOW(main_window),
4296 get_title(t, TRUE));
4297 } else {
4300 #endif
4301 default:
4302 #if GTK_CHECK_VERSION(2, 20, 0)
4303 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4304 gtk_widget_hide(t->spinner);
4305 #endif
4306 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4307 break;
4310 gtk_widget_set_sensitive(GTK_WIDGET(t->backward),
4311 can_go_back_for_real(t));
4313 gtk_widget_set_sensitive(GTK_WIDGET(t->forward),
4314 can_go_forward_for_real(t));
4317 void
4318 notify_title_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4320 const gchar *title = NULL, *win_title = NULL;
4322 title = get_title(t, FALSE);
4323 win_title = get_title(t, TRUE);
4324 if (title) {
4325 gtk_label_set_text(GTK_LABEL(t->label), title);
4326 gtk_label_set_text(GTK_LABEL(t->tab_elems.label), title);
4329 if (win_title && t->tab_id == gtk_notebook_get_current_page(notebook))
4330 gtk_window_set_title(GTK_WINDOW(main_window), win_title);
4333 char *
4334 get_domain(const gchar *host)
4336 size_t x;
4337 char *p;
4339 /* handle silly domains like .co.uk */
4341 if ((x = strlen(host)) <= 6)
4342 return (g_strdup(host));
4344 if (host[x - 3] == '.' && host[x - 6] == '.') {
4345 x = x - 7;
4346 while (x > 0)
4347 if (host[x] != '.')
4348 x--;
4349 else
4350 return (g_strdup(&host[x + 1]));
4353 /* find first . */
4354 p = g_strrstr(host, ".");
4355 if (p == NULL)
4356 return (g_strdup(""));
4357 p--;
4358 while (p > host)
4359 if (*p != '.')
4360 p--;
4361 else
4362 return (g_strdup(p + 1));
4364 return (g_strdup(host));
4367 void
4368 js_autorun(struct tab *t)
4370 SoupURI *su = NULL;
4371 const gchar *uri;
4372 size_t got_default = 0, got_host = 0;
4373 struct stat sb;
4374 char deff[PATH_MAX], hostf[PATH_MAX];
4375 char *js = NULL, *jsat, *domain = NULL;
4376 FILE *deffile = NULL, *hostfile = NULL;
4378 if (enable_js_autorun == 0)
4379 return;
4381 uri = get_uri(t);
4382 if (uri &&
4383 !(g_str_has_prefix(uri, "http://") ||
4384 g_str_has_prefix(uri, "https://")))
4385 goto done;
4387 su = soup_uri_new(uri);
4388 if (su == NULL)
4389 goto done;
4390 if (!SOUP_URI_VALID_FOR_HTTP(su))
4391 goto done;
4393 DNPRINTF(XT_D_JS, "%s: host: %s domain: %s\n", __func__,
4394 su->host, domain);
4395 domain = get_domain(su->host);
4397 snprintf(deff, sizeof deff, "%s" PS "default.js", js_dir);
4398 if ((deffile = fopen(deff, "r")) != NULL) {
4399 if (fstat(fileno(deffile), &sb) == -1) {
4400 show_oops(t, "can't stat default JS file");
4401 goto done;
4403 got_default = sb.st_size;
4406 /* try host first followed by domain */
4407 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, su->host);
4408 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4409 if ((hostfile = fopen(hostf, "r")) == NULL) {
4410 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, domain);
4411 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4412 if ((hostfile = fopen(hostf, "r")) == NULL)
4413 goto nofile;
4415 DNPRINTF(XT_D_JS, "file: %s\n", hostf);
4416 if (fstat(fileno(hostfile), &sb) == -1) {
4417 show_oops(t, "can't stat %s JS file", hostf);
4418 goto done;
4420 got_host = sb.st_size;
4422 nofile:
4423 if (got_default + got_host == 0)
4424 goto done;
4426 js = g_malloc0(got_default + got_host + 1);
4427 jsat = js;
4429 if (got_default) {
4430 if (fread(js, got_default, 1, deffile) != 1) {
4431 show_oops(t, "default file read error");
4432 goto done;
4434 jsat = js + got_default;
4437 if (got_host) {
4438 if (fread(jsat, got_host, 1, hostfile) != 1) {
4439 show_oops(t, "host file read error");
4440 goto done;
4444 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
4445 run_script(t, js);
4447 done:
4448 if (su)
4449 soup_uri_free(su);
4450 if (js)
4451 g_free(js);
4452 if (deffile)
4453 fclose(deffile);
4454 if (hostfile)
4455 fclose(hostfile);
4456 if (domain)
4457 g_free(domain);
4460 void
4461 webview_progress_changed_cb(WebKitWebView *wv, GParamSpec *pspec, struct tab *t)
4463 gdouble progress;
4465 progress = webkit_web_view_get_progress(wv);
4466 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
4467 progress > (1.0 - 0.0001) ? 0 : progress);
4468 gtk_entry_set_progress_fraction(GTK_ENTRY(t->uri_entry),
4469 progress > (1.0 - 0.0001) ? 0 : progress);
4471 update_statusbar_position(NULL, NULL);
4475 strict_transport_rb_cmp(struct strict_transport *a, struct strict_transport *b)
4477 char *p1, *p2;
4478 int l1, l2;
4480 /* compare strings from the end */
4481 l1 = strlen(a->host);
4482 l2 = strlen(b->host);
4484 p1 = a->host + l1;
4485 p2 = b->host + l2;
4486 for (; *p1 == *p2 && p1 > a->host && p2 > b->host;
4487 p1--, p2--)
4491 * Check if we need to do pattern expansion,
4492 * or if we're just keeping the tree in order
4494 if (a->flags & XT_STS_FLAGS_EXPAND &&
4495 b->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) {
4496 /* Check if we're matching the
4497 * 'host.xyz' part in '*.host.xyz'
4499 if (p2 == b->host && (p1 == a->host || *(p1-1) == '.')) {
4500 return (0);
4504 if (p1 == a->host && p2 == b->host)
4505 return (0);
4506 if (p1 == a->host)
4507 return (1);
4508 if (p2 == b->host)
4509 return (-1);
4511 if (*p1 < *p2)
4512 return (-1);
4513 if (*p1 > *p2)
4514 return (1);
4516 return (0);
4518 RB_GENERATE(strict_transport_tree, strict_transport, entry,
4519 strict_transport_rb_cmp);
4522 strict_transport_add(const char *domain, time_t timeout, int subdomains)
4524 struct strict_transport *d, find;
4525 time_t now;
4526 FILE *f;
4528 if (enable_strict_transport == FALSE)
4529 return (0);
4531 DPRINTF("strict_transport_add(%s,%" PRIi64 ",%d)\n", domain,
4532 (uint64_t)timeout, subdomains);
4534 now = time(NULL);
4535 if (timeout < now)
4536 return (0);
4538 find.host = (char *)domain;
4539 find.flags = 0;
4540 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4542 /* update flags */
4543 if (d) {
4544 /* check if update is needed */
4545 if (d->timeout == timeout &&
4546 (d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) == subdomains)
4547 return (0);
4549 d->timeout = timeout;
4550 if (subdomains)
4551 d->flags |= XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4553 /* We're still initializing */
4554 if (strict_transport_file == NULL)
4555 return (0);
4557 if ((f = fopen(strict_transport_file, "w")) == NULL) {
4558 show_oops(NULL,
4559 "can't open strict-transport rules file");
4560 return (1);
4563 fprintf(f, "# Generated file - do not update unless you know "
4564 "what you're doing\n");
4565 RB_FOREACH(d, strict_transport_tree, &st_tree) {
4566 if (d->timeout < now)
4567 continue;
4568 fprintf(f, "%s\t%" PRIi64 "\t%d\n", d->host,
4569 (uint64_t)d->timeout,
4570 d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS);
4572 fclose(f);
4573 } else {
4574 d = g_malloc(sizeof *d);
4575 d->host= g_strdup(domain);
4576 d->timeout = timeout;
4577 if (subdomains)
4578 d->flags = XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4579 else
4580 d->flags = 0;
4581 RB_INSERT(strict_transport_tree, &st_tree, d);
4583 /* We're still initializing */
4584 if (strict_transport_file == NULL)
4585 return (0);
4587 if ((f = fopen(strict_transport_file, "a+")) == NULL) {
4588 show_oops(NULL,
4589 "can't open strict-transport rules file");
4590 return (1);
4593 fseek(f, 0, SEEK_END);
4594 fprintf(f,"%s\t%" PRIi64 "\t%d\n", d->host, (uint64_t)timeout,
4595 subdomains);
4596 fclose(f);
4598 return (0);
4602 strict_transport_check(const char *host)
4604 static struct strict_transport *d = NULL;
4605 struct strict_transport find;
4607 if (enable_strict_transport == FALSE)
4608 return (0);
4610 find.host = (char *)host;
4612 /* match for domains that include subdomains */
4613 find.flags = XT_STS_FLAGS_EXPAND;
4615 /* First, check if we're already at the right node */
4616 if (d != NULL && strict_transport_rb_cmp(&find, d) == 0) {
4617 return (1);
4620 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4621 if (d != NULL)
4622 return (1);
4624 return (0);
4628 strict_transport_init()
4630 char file[PATH_MAX];
4631 char delim[3];
4632 char *rule;
4633 char *ptr;
4634 FILE *f;
4635 size_t len;
4636 time_t timeout, now;
4637 int subdomains;
4639 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_STS_FILE);
4640 if ((f = fopen(file, "r")) == NULL) {
4641 strict_transport_file = g_strdup(file);
4642 return (0);
4645 delim[0] = '\\';
4646 delim[1] = '\\';
4647 delim[2] = '#';
4648 rule = NULL;
4649 now = time(NULL);
4651 for (;;) {
4652 if ((rule = fparseln(f, &len, NULL, delim, 0)) == NULL) {
4653 if (!feof(f) || ferror(f))
4654 goto corrupt_file;
4655 else
4656 break;
4659 /* get second entry */
4660 if ((ptr = strpbrk(rule, " \t")) == NULL)
4661 goto corrupt_file;
4663 *ptr++ = '\0';
4664 timeout = atoi(ptr);
4666 /* get third entry */
4667 if ((ptr = strpbrk(ptr, " \t")) == NULL)
4668 goto corrupt_file;
4670 ptr ++;
4671 subdomains = atoi(ptr);
4673 if (timeout > now)
4674 strict_transport_add(rule, timeout, subdomains);
4675 free(rule);
4678 fclose(f);
4679 strict_transport_file = g_strdup(file);
4680 return (0);
4682 corrupt_file:
4683 startpage_add("strict-transport rules file ('%s') is corrupt", file);
4684 if (rule)
4685 free(rule);
4686 fclose(f);
4687 return (1);
4691 force_https_check(const char *uri)
4693 struct wl_entry *w = NULL;
4695 if (uri == NULL)
4696 return (0);
4698 if ((w = wl_find_uri(uri, &force_https)) == NULL)
4699 return (0);
4700 else
4701 return (1);
4704 void
4705 strict_transport_security_cb(SoupMessage *msg, gpointer data)
4707 SoupURI *uri;
4708 const char *sts;
4709 char *ptr;
4710 time_t timeout = 0;
4711 int subdomains = FALSE;
4713 if (msg == NULL)
4714 return;
4716 sts = soup_message_headers_get_one(msg->response_headers,
4717 "Strict-Transport-Security");
4718 uri = soup_message_get_uri(msg);
4720 if (sts == NULL || uri == NULL)
4721 return;
4723 if ((ptr = strcasestr(sts, "max-age="))) {
4724 ptr += strlen("max-age=");
4725 timeout = atoll(ptr);
4726 } else
4727 return; /* malformed header - max-age must be included */
4729 if ((ptr = strcasestr(sts, "includeSubDomains")))
4730 subdomains = TRUE;
4732 strict_transport_add(uri->host, timeout + time(NULL), subdomains);
4735 void
4736 session_rq_cb(SoupSession *s, SoupMessage *msg, SoupSocket *socket,
4737 gpointer data)
4739 SoupURI *dest;
4740 SoupURI *ref_uri;
4741 const char *ref;
4743 char *ref_suffix;
4744 char *dest_suffix;
4746 if (s == NULL || msg == NULL)
4747 return;
4749 if (enable_strict_transport) {
4750 soup_message_add_header_handler(msg, "finished",
4751 "Strict-Transport-Security",
4752 G_CALLBACK(strict_transport_security_cb), NULL);
4755 if (referer_mode == XT_REFERER_ALWAYS)
4756 return;
4758 /* Check if referer is set - and what the user requested for referers */
4759 ref = soup_message_headers_get_one(msg->request_headers, "Referer");
4760 if (ref) {
4761 DNPRINTF(XT_D_NAV, "session_rq_cb: Referer: %s\n", ref);
4762 switch (referer_mode) {
4763 case XT_REFERER_NEVER:
4764 DNPRINTF(XT_D_NAV, "session_rq_cb: removing referer\n");
4765 soup_message_headers_remove(msg->request_headers,
4766 "Referer");
4767 break;
4768 case XT_REFERER_SAME_DOMAIN:
4769 ref_uri = soup_uri_new(ref);
4770 dest = soup_message_get_uri(msg);
4772 if (ref_uri == NULL || dest == NULL)
4773 return;
4775 ref_suffix = tld_get_suffix(ref_uri->host);
4776 dest_suffix = tld_get_suffix(dest->host);
4778 if (ref_suffix && dest_suffix &&
4779 strcmp(ref_suffix, dest_suffix) != 0) {
4780 soup_message_headers_remove(msg->request_headers,
4781 "Referer");
4782 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4783 "referer (not same domain) (suffixes: %s - %s)\n",
4784 ref_suffix, dest_suffix);
4786 soup_uri_free(ref_uri);
4787 break;
4788 case XT_REFERER_SAME_FQDN:
4789 ref_uri = soup_uri_new(ref);
4790 dest = soup_message_get_uri(msg);
4792 if (ref_uri == NULL || dest == NULL)
4793 return;
4795 if (strcmp(ref_uri->host, dest->host) != 0) {
4796 soup_message_headers_remove(msg->request_headers,
4797 "Referer");
4798 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4799 "referer (not same fqdn) (should be %s)\n",
4800 dest->host);
4802 soup_uri_free(ref_uri);
4803 break;
4804 case XT_REFERER_CUSTOM:
4805 DNPRINTF(XT_D_NAV, "session_rq_cb: setting referer "
4806 "to %s\n", referer_custom);
4807 soup_message_headers_replace(msg->request_headers,
4808 "Referer", referer_custom);
4809 break;
4815 webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf,
4816 WebKitNetworkRequest *request, WebKitWebNavigationAction *na,
4817 WebKitWebPolicyDecision *pd, struct tab *t)
4819 WebKitWebNavigationReason reason;
4820 char *uri;
4822 if (t == NULL) {
4823 show_oops(NULL, "webview_npd_cb invalid parameters");
4824 return (FALSE);
4827 DNPRINTF(XT_D_NAV, "webview_npd_cb: ctrl_click %d %s\n",
4828 t->ctrl_click,
4829 webkit_network_request_get_uri(request));
4831 uri = (char *)webkit_network_request_get_uri(request);
4833 if (!auto_load_images && t->load_images) {
4835 /* Disable autoloading of images, now that we're done loading
4836 * them. */
4837 g_object_set(G_OBJECT(t->settings),
4838 "auto-load-images", FALSE, (char *)NULL);
4839 webkit_web_view_set_settings(t->wv, t->settings);
4841 t->load_images = FALSE;
4844 /* If this is an xtp url, we don't load anything else. */
4845 if (parse_xtp_url(t, uri)) {
4846 webkit_web_policy_decision_ignore(pd);
4847 return (TRUE);
4850 if (parse_custom_uri(t, uri)) {
4851 webkit_web_policy_decision_ignore(pd);
4852 return (TRUE);
4855 if (valid_url_type(uri)) {
4856 show_oops(t, "Stopping attempt to load an invalid URI (possible"
4857 " bait and switch attack)");
4858 webkit_web_policy_decision_ignore(pd);
4859 return (TRUE);
4862 if ((t->mode == XT_MODE_HINT && t->new_tab) || t->ctrl_click) {
4863 t->ctrl_click = 0;
4864 create_new_tab(uri, NULL, ctrl_click_focus, -1);
4865 webkit_web_policy_decision_ignore(pd);
4866 return (TRUE); /* we made the decission */
4870 * This is a little hairy but it comes down to this:
4871 * when we run in whitelist mode we have to assist the browser in
4872 * opening the URL that it would have opened in a new tab.
4874 reason = webkit_web_navigation_action_get_reason(na);
4875 if (reason == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
4876 set_normal_tab_meaning(t);
4877 if (enable_scripts == 0 && enable_cookie_whitelist == 1)
4878 load_uri(t, uri);
4879 webkit_web_policy_decision_use(pd);
4880 return (TRUE); /* we made the decision */
4883 return (FALSE);
4886 void
4887 webview_rrs_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitWebResource *res,
4888 WebKitNetworkRequest *request, WebKitNetworkResponse *response,
4889 struct tab *t)
4891 SoupMessage *msg = NULL;
4892 SoupURI *uri = NULL;
4893 struct http_accept ha_find, *ha = NULL;
4894 struct user_agent ua_find, *ua = NULL;
4895 struct domain_id di_find, *di = NULL;
4896 char *uri_s = NULL;
4898 msg = webkit_network_request_get_message(request);
4899 if (!msg)
4900 return;
4902 uri = soup_message_get_uri(msg);
4903 if (!uri)
4904 return;
4905 uri_s = soup_uri_to_string(uri, FALSE);
4907 if (strcmp(uri->scheme, SOUP_URI_SCHEME_HTTP) == 0) {
4908 if (strict_transport_check(uri->host) ||
4909 force_https_check(uri_s)) {
4910 DNPRINTF(XT_D_NAV, "webview_rrs_cb: force https for %s\n",
4911 uri->host);
4912 soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTPS);
4916 if (do_not_track)
4917 soup_message_headers_append(msg->request_headers, "DNT", "1");
4920 * Check if resources on this domain have been loaded before. If
4921 * not, add the current tab's http-accept and user-agent id's to a
4922 * new domain_id and insert into the RB tree. Use these http headers
4923 * for all resources loaded from this domain for the lifetime of the
4924 * browser.
4926 if ((di_find.domain = uri->host) == NULL)
4927 goto done;
4928 if ((di = RB_FIND(domain_id_list, &di_list, &di_find)) == NULL) {
4929 di = g_malloc(sizeof *di);
4930 di->domain = g_strdup(uri->host);
4931 di->ua_id = t->user_agent_id++;
4932 di->ha_id = t->http_accept_id++;
4933 RB_INSERT(domain_id_list, &di_list, di);
4935 ua_find.id = t->user_agent_id;
4936 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4937 if (ua == NULL)
4938 t->user_agent_id = 0;
4940 ha_find.id = t->http_accept_id;
4941 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4942 if (ha == NULL)
4943 t->http_accept_id = 0;
4946 ua_find.id = di->ua_id;
4947 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4948 ha_find.id = di->ha_id;
4949 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4951 if (ua != NULL)
4952 soup_message_headers_replace(msg->request_headers,
4953 "User-Agent", ua->value);
4954 if (ha != NULL)
4955 soup_message_headers_replace(msg->request_headers,
4956 "Accept", ha->value);
4958 done:
4959 if (uri_s)
4960 g_free(uri_s);
4963 WebKitWebView *
4964 webview_cwv_cb(WebKitWebView *wv, WebKitWebFrame *wf, struct tab *t)
4966 struct tab *tt;
4967 struct wl_entry *w = NULL;
4968 const gchar *uri;
4969 WebKitWebView *webview = NULL;
4970 int x = 1;
4972 DNPRINTF(XT_D_NAV, "webview_cwv_cb: %s\n",
4973 webkit_web_view_get_uri(wv));
4975 if (tabless) {
4976 /* open in current tab */
4977 webview = t->wv;
4978 } else if (enable_scripts == 0 && enable_js_whitelist == 1) {
4979 uri = webkit_web_view_get_uri(wv);
4980 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
4981 return (NULL);
4983 if (t->ctrl_click) {
4984 x = ctrl_click_focus;
4985 t->ctrl_click = 0;
4987 tt = create_new_tab(NULL, NULL, x, -1);
4988 webview = tt->wv;
4989 } else if (enable_scripts == 1) {
4990 if (t->ctrl_click) {
4991 x = ctrl_click_focus;
4992 t->ctrl_click = 0;
4994 tt = create_new_tab(NULL, NULL, x, -1);
4995 webview = tt->wv;
4998 return (webview);
5001 gboolean
5002 webview_closewv_cb(WebKitWebView *wv, struct tab *t)
5004 const gchar *uri;
5005 struct wl_entry *w = NULL;
5007 DNPRINTF(XT_D_NAV, "webview_close_cb: %d\n", t->tab_id);
5009 if (enable_scripts == 0 && enable_cookie_whitelist == 1) {
5010 uri = webkit_web_view_get_uri(wv);
5011 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
5012 return (FALSE);
5014 delete_tab(t);
5015 } else if (enable_scripts == 1)
5016 delete_tab(t);
5018 return (TRUE);
5022 webview_event_cb(GtkWidget *w, GdkEventButton *e, struct tab *t)
5024 /* we can not eat the event without throwing gtk off so defer it */
5026 /* catch middle click */
5027 if (e->type == GDK_BUTTON_RELEASE && e->button == 2) {
5028 t->ctrl_click = 1;
5029 goto done;
5032 /* catch ctrl click */
5033 if (e->type == GDK_BUTTON_RELEASE &&
5034 CLEAN(e->state) == GDK_CONTROL_MASK)
5035 t->ctrl_click = 1;
5036 else
5037 t->ctrl_click = 0;
5038 done:
5039 return (XT_CB_PASSTHROUGH);
5043 run_mimehandler(struct tab *t, char *mime_type, WebKitNetworkRequest *request)
5045 struct mime_type *m;
5046 char *sv[3];
5047 GError *gerr = NULL;
5049 m = find_mime_type(mime_type);
5050 if (m == NULL)
5051 return (1);
5052 if (m->mt_download)
5053 return (1);
5055 sv[0] = m->mt_action;
5056 sv[1] = (char *)webkit_network_request_get_uri(request);
5057 sv[2] = NULL;
5059 /* ignore donothing from example config */
5060 if (m->mt_action && !strcmp(m->mt_action, "donothing"))
5061 return (0);
5063 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
5064 NULL, &gerr))
5065 show_oops(t, "%s: could not spawn process (%s)", __func__,
5066 gerr ? gerr->message : "N/A");
5067 return (0);
5070 char *
5071 get_mime_type(const char *uri)
5073 GFileInfo *fi;
5074 GFile *gf;
5075 const gchar *m;
5076 char *mime_type = NULL;
5077 char *file;
5079 if (uri == NULL) {
5080 show_oops(NULL, "%s: invalid parameters", __func__);
5081 return (NULL);
5084 if (g_str_has_prefix(uri, "file://"))
5085 file = g_filename_from_uri(uri, NULL, NULL);
5086 else
5087 file = g_strdup(uri);
5089 if (file == NULL)
5090 return (NULL);
5092 gf = g_file_new_for_path(file);
5093 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
5094 NULL, NULL);
5095 if ((m = g_file_info_get_content_type(fi)) != NULL)
5096 mime_type = g_strdup(m);
5097 g_object_unref(fi);
5098 g_object_unref(gf);
5099 g_free(file);
5101 return (mime_type);
5105 run_download_mimehandler(char *mime_type, char *file)
5107 struct mime_type *m;
5108 char *sv[3];
5110 m = find_mime_type(mime_type);
5111 if (m == NULL)
5112 return (1);
5114 sv[0] = m->mt_action;
5115 sv[1] = file;
5116 sv[2] = NULL;
5117 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
5118 NULL, NULL)) {
5119 show_oops(NULL, "%s: could not spawn process: %s %s", __func__,
5120 sv[0], sv[1]);
5121 return (1);
5123 return (0);
5126 void
5127 download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
5128 WebKitWebView *wv)
5130 WebKitDownloadStatus status;
5131 const char *uri;
5132 char *file = NULL;
5133 char *mime = NULL;
5134 char *destination;
5136 if (download == NULL)
5137 return;
5138 status = webkit_download_get_status(download);
5139 if (status != WEBKIT_DOWNLOAD_STATUS_FINISHED)
5140 return;
5142 if (download_notifications) {
5143 /* because basename() takes a char * on linux */
5144 destination = g_strdup(
5145 webkit_download_get_destination_uri(download));
5146 show_oops(NULL, "Download of '%s' finished",
5147 basename(destination));
5148 g_free(destination);
5150 uri = webkit_download_get_destination_uri(download);
5151 if (uri == NULL)
5152 return;
5153 mime = get_mime_type(uri);
5154 if (mime == NULL)
5155 return;
5157 if (g_str_has_prefix(uri, "file://"))
5158 file = g_filename_from_uri(uri, NULL, NULL);
5159 else
5160 file = g_strdup(uri);
5162 if (file == NULL)
5163 goto done;
5165 run_download_mimehandler((char *)mime, file);
5167 done:
5168 g_free(mime);
5172 webview_mimetype_cb(WebKitWebView *wv, WebKitWebFrame *frame,
5173 WebKitNetworkRequest *request, char *mime_type,
5174 WebKitWebPolicyDecision *decision, struct tab *t)
5176 if (t == NULL) {
5177 show_oops(NULL, "webview_mimetype_cb invalid parameters");
5178 return (FALSE);
5181 DNPRINTF(XT_D_DOWNLOAD, "webview_mimetype_cb: tab %d mime %s\n",
5182 t->tab_id, mime_type);
5184 if (run_mimehandler(t, mime_type, request) == 0) {
5185 webkit_web_policy_decision_ignore(decision);
5186 focus_webview(t);
5187 return (TRUE);
5190 if (webkit_web_view_can_show_mime_type(wv, mime_type) == FALSE) {
5191 webkit_web_policy_decision_download(decision);
5192 return (TRUE);
5195 return (FALSE);
5199 download_start(struct tab *t, struct download *d, int flag)
5201 WebKitNetworkRequest *req;
5202 struct stat sb;
5203 const gchar *suggested_name;
5204 gchar *filename = NULL;
5205 char *uri = NULL;
5206 char *path = NULL;
5207 char *destination;
5208 int ret = TRUE;
5209 int i;
5211 if (d == NULL || t == NULL) {
5212 show_oops(NULL, "%s invalid parameters", __func__);
5213 return (FALSE);
5216 suggested_name = webkit_download_get_suggested_filename(d->download);
5217 if (suggested_name == NULL)
5218 return (FALSE); /* abort download */
5220 i = 0;
5221 do {
5222 if (filename) {
5223 g_free(filename);
5224 filename = NULL;
5226 if (i) {
5227 g_free(uri);
5228 uri = NULL;
5229 filename = g_strdup_printf("%d%s", i, suggested_name);
5231 #ifdef __MINGW32__
5232 /* XXX using urls doesn't work properly in windows? */
5233 uri = g_strdup_printf("%s\\%s", download_dir, i ?
5234 filename : suggested_name);
5235 #else
5236 path = g_strdup_printf("%s" PS "%s", download_dir, i ?
5237 filename : suggested_name);
5238 if ((uri = g_filename_to_uri(path, NULL, NULL)) == NULL)
5239 break; /* XXX */
5240 #endif
5241 i++;
5242 #ifdef __MINGW32__
5243 } while (!stat(uri, &sb));
5244 #else
5245 } while (!stat(path, &sb));
5246 #endif
5248 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d filename %s "
5249 "local %s\n", __func__, t->tab_id, filename, uri);
5251 /* if we're restarting the download, or starting
5252 * it after doing something else, we need to recreate
5253 * the download request.
5255 if (flag == XT_DL_RESTART) {
5256 req = webkit_network_request_new(webkit_download_get_uri(d->download));
5257 webkit_download_cancel(d->download);
5258 g_object_unref(d->download);
5259 d->download = webkit_download_new(req);
5260 g_object_unref(req);
5263 webkit_download_set_destination_uri(d->download, uri);
5265 if (webkit_download_get_status(d->download) ==
5266 WEBKIT_DOWNLOAD_STATUS_ERROR) {
5267 show_oops(t, "%s: download failed to start", __func__);
5268 ret = FALSE;
5269 show_oops(t, "Download Failed");
5270 } else {
5271 /* connect "download first" mime handler */
5272 g_signal_connect(G_OBJECT(d->download), "notify::status",
5273 G_CALLBACK(download_status_changed_cb), NULL);
5275 /* get from history */
5276 g_object_ref(d->download);
5277 /* because basename() takes a char * on linux */
5278 destination = g_strdup(
5279 webkit_download_get_destination_uri(d->download));
5280 show_oops(t, "Download of '%s' started...",
5281 basename(destination));
5282 g_free(destination);
5285 if (flag != XT_DL_START)
5286 webkit_download_start(d->download);
5288 DNPRINTF(XT_D_DOWNLOAD, "download status : %d",
5289 webkit_download_get_status(d->download));
5291 /* sync other download manager tabs */
5292 update_download_tabs(NULL);
5294 if (uri)
5295 g_free(uri);
5296 if (path)
5297 g_free(path);
5298 if (filename)
5299 g_free(filename);
5301 return (ret);
5305 download_ask_cb(struct tab *t, GdkEventKey *e, gpointer data)
5307 struct download *d = data;
5309 /* unset mode */
5310 t->mode_cb = NULL;
5311 t->mode_cb_data = NULL;
5313 if (d == NULL) {
5314 if (e != NULL)
5315 e->keyval = GDK_Escape;
5316 return (XT_CB_PASSTHROUGH);
5319 DPRINTF("download_ask_cb: User pressed %c\n", e->keyval);
5320 if (e->keyval == 'y' || e->keyval == 'Y' || e->keyval == GDK_Return)
5321 /* We need to do a RESTART, because we're not calling from
5322 * webview_download_cb
5324 download_start(t, d, XT_DL_RESTART);
5326 /* for all other keyvals, we just let the download be */
5327 e->keyval = GDK_Escape;
5328 return (XT_CB_HANDLED);
5332 download_ask(struct tab *t, struct download *d)
5334 const gchar *suggested_name;
5336 suggested_name = webkit_download_get_suggested_filename(d->download);
5337 if (suggested_name == NULL)
5338 return (FALSE); /* abort download */
5340 show_oops(t, "download file %s [y/n] ?", suggested_name);
5341 t->mode_cb = download_ask_cb;
5342 t->mode_cb_data = d;
5344 return (TRUE);
5348 webview_download_cb(WebKitWebView *wv, WebKitDownload *wk_download,
5349 struct tab *t)
5351 const gchar *suggested_name;
5352 struct download *download_entry;
5353 int ret = TRUE;
5355 if (wk_download == NULL || t == NULL) {
5356 show_oops(NULL, "%s invalid parameters", __func__);
5357 return (FALSE);
5360 suggested_name = webkit_download_get_suggested_filename(wk_download);
5361 if (suggested_name == NULL)
5362 return (FALSE); /* abort download */
5364 download_entry = g_malloc(sizeof(struct download));
5365 download_entry->download = wk_download;
5366 download_entry->tab = t;
5367 download_entry->id = next_download_id++;
5368 RB_INSERT(download_list, &downloads, download_entry);
5369 t->download_requested = 1;
5371 if (download_mode == XT_DM_START)
5372 ret = download_start(t, download_entry, XT_DL_START);
5373 else if (download_mode == XT_DM_ASK)
5374 ret = download_ask(t, download_entry);
5375 else if (download_mode == XT_DM_ADD)
5376 show_oops(t, "added %s to download manager",
5377 suggested_name);
5379 /* sync other download manager tabs */
5380 update_download_tabs(NULL);
5383 * NOTE: never redirect/render the current tab before this
5384 * function returns. This will cause the download to never start.
5386 return (ret); /* start download */
5389 void
5390 webview_hover_cb(WebKitWebView *wv, gchar *title, gchar *uri, struct tab *t)
5392 DNPRINTF(XT_D_KEY, "webview_hover_cb: %s %s\n", title, uri);
5394 if (t == NULL) {
5395 show_oops(NULL, "webview_hover_cb");
5396 return;
5399 if (uri)
5400 set_status(t, "Link: %s", uri);
5401 else {
5402 if (statusbar_style == XT_STATUSBAR_URL) {
5403 const gchar *page_uri;
5405 if ((page_uri = get_uri(t)) != NULL)
5406 set_status(t, "%s", page_uri);
5407 } else
5408 set_status(t, "%s", (char *)get_title(t, FALSE));
5413 mark(struct tab *t, struct karg *arg)
5415 char mark;
5416 int index;
5417 double pos;
5419 mark = arg->s[1];
5420 if ((index = marktoindex(mark)) == -1)
5421 return (-1);
5423 if (arg->i == XT_MARK_SET)
5424 t->mark[index] = gtk_adjustment_get_value(t->adjust_v);
5425 else if (arg->i == XT_MARK_GOTO) {
5426 if (t->mark[index] > (XT_INVALID_MARK - 0.001) &&
5427 t->mark[index] < (XT_INVALID_MARK + 0.001)) {
5428 show_oops(t, "mark '%c' does not exist", mark);
5429 return (-1);
5431 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5432 something changes the document size */
5433 pos = gtk_adjustment_get_value(t->adjust_v);
5434 gtk_adjustment_set_value(t->adjust_v, t->mark[index]);
5435 t->mark[marktoindex('\'')] = pos;
5438 return (0);
5441 void
5442 marks_clear(struct tab *t)
5444 int i;
5446 for (i = 0; i < LENGTH(t->mark); i++)
5447 t->mark[i] = XT_INVALID_MARK;
5451 qmarks_load(void)
5453 char file[PATH_MAX];
5454 char *line = NULL, *p;
5455 int index, i;
5456 FILE *f;
5457 size_t linelen;
5459 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5460 if ((f = fopen(file, "r+")) == NULL) {
5461 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5462 return (1);
5465 for (i = 1; ; i++) {
5466 if ((line = fparseln(f, &linelen, NULL, NULL, 0)) == NULL)
5467 break;
5468 if (strlen(line) == 0 || line[0] == '#') {
5469 free(line);
5470 line = NULL;
5471 continue;
5474 p = strtok(line, " \t");
5476 if (p == NULL || strlen(p) != 1 ||
5477 (index = qmarktoindex(*p)) == -1) {
5478 warnx("corrupt quickmarks file, line %d", i);
5479 break;
5482 p = strtok(NULL, " \t");
5483 if (qmarks[index] != NULL)
5484 g_free(qmarks[index]);
5485 qmarks[index] = g_strdup(p);
5488 fclose(f);
5490 return (0);
5494 qmarks_save(void)
5496 char file[PATH_MAX];
5497 int i;
5498 FILE *f;
5500 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5501 if ((f = fopen(file, "r+")) == NULL) {
5502 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5503 return (1);
5506 for (i = 0; i < XT_NOQMARKS; i++)
5507 if (qmarks[i] != NULL)
5508 fprintf(f, "%c %s\n", indextoqmark(i), qmarks[i]);
5510 fclose(f);
5512 return (0);
5516 qmark(struct tab *t, struct karg *arg)
5518 char mark;
5519 int index;
5521 mark = arg->s[strlen(arg->s)-1];
5522 index = qmarktoindex(mark);
5523 if (index == -1)
5524 return (-1);
5526 switch (arg->i) {
5527 case XT_QMARK_SET:
5528 if (qmarks[index] != NULL) {
5529 g_free(qmarks[index]);
5530 qmarks[index] = NULL;
5533 qmarks_load(); /* sync if multiple instances */
5534 qmarks[index] = g_strdup(get_uri(t));
5535 qmarks_save();
5536 break;
5537 case XT_QMARK_OPEN:
5538 if (qmarks[index] != NULL)
5539 load_uri(t, qmarks[index]);
5540 else {
5541 show_oops(t, "quickmark \"%c\" does not exist",
5542 mark);
5543 return (-1);
5545 break;
5546 case XT_QMARK_TAB:
5547 if (qmarks[index] != NULL)
5548 create_new_tab(qmarks[index], NULL, 1, -1);
5549 else {
5550 show_oops(t, "quickmark \"%c\" does not exist",
5551 mark);
5552 return (-1);
5554 break;
5557 return (0);
5561 go_up(struct tab *t, struct karg *args)
5563 int levels;
5564 int lastidx;
5565 char *uri;
5566 char *tmp;
5567 char *p;
5569 if (args->i == XT_GO_UP_ROOT)
5570 levels = XT_GO_UP_ROOT;
5571 else if ((levels = atoi(args->s)) == 0)
5572 levels = 1;
5574 uri = g_strdup(get_uri(t));
5575 if (uri == NULL)
5576 return (1);
5578 if ((tmp = strstr(uri, XT_PROTO_DELIM)) == NULL)
5579 return (1);
5581 tmp += strlen(XT_PROTO_DELIM);
5583 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5584 lastidx = strlen(tmp) - 1;
5585 if (lastidx > 0) {
5586 if (tmp[lastidx] == '/')
5587 tmp[lastidx] = '\0';
5590 while (levels--) {
5591 p = strrchr(tmp, '/');
5592 if (p == tmp) { /* Are we at the root of a file://-path? */
5593 p[1] = '\0';
5594 break;
5595 } else if (p != NULL)
5596 *p = '\0';
5597 else
5598 break;
5601 load_uri(t, uri);
5602 g_free(uri);
5604 return (0);
5608 gototab(struct tab *t, struct karg *args)
5610 int tab;
5611 struct karg arg = {0, NULL, -1};
5613 tab = atoi(args->s);
5615 if (args->i == 0)
5616 arg.i = XT_TAB_NEXT;
5617 else
5618 arg.i = args->i;
5620 arg.precount = tab;
5622 movetab(t, &arg);
5624 return (0);
5628 zoom_amount(struct tab *t, struct karg *arg)
5630 struct karg narg = {0, NULL, -1};
5632 narg.i = atoi(arg->s);
5633 resizetab(t, &narg);
5635 return (0);
5639 flip_colon(struct tab *t, struct karg *arg)
5641 struct karg narg = {0, NULL, -1};
5642 char *p;
5644 if (t == NULL || arg == NULL)
5645 return (1);
5647 p = strstr(arg->s, ":");
5648 if (p == NULL)
5649 return (1);
5650 *p = '\0';
5652 narg.i = ':';
5653 narg.s = arg->s;
5654 command(t, &narg);
5656 return (0);
5659 /* buffer commands receive the regex that triggered them in arg.s */
5660 char bcmd[XT_BUFCMD_SZ];
5661 struct buffercmd {
5662 char *regex;
5663 int precount;
5664 #define XT_PRE_NO (0)
5665 #define XT_PRE_YES (1)
5666 #define XT_PRE_MAYBE (2)
5667 char *cmd;
5668 int (*func)(struct tab *, struct karg *);
5669 int arg;
5670 regex_t cregex;
5671 } buffercmds[] = {
5672 { "^[0-9]*gu$", XT_PRE_MAYBE, "gu", go_up, 0 },
5673 { "^gU$", XT_PRE_NO, "gU", go_up, XT_GO_UP_ROOT },
5674 { "^gg$", XT_PRE_NO, "gg", move, XT_MOVE_TOP },
5675 { "^gG$", XT_PRE_NO, "gG", move, XT_MOVE_BOTTOM },
5676 { "^[0-9]+%$", XT_PRE_YES, "%", move, XT_MOVE_PERCENT },
5677 { "^zz$", XT_PRE_NO, "zz", move, XT_MOVE_CENTER },
5678 { "^gh$", XT_PRE_NO, "gh", go_home, 0 },
5679 { "^m[a-zA-Z0-9]$", XT_PRE_NO, "m", mark, XT_MARK_SET },
5680 { "^['][a-zA-Z0-9']$", XT_PRE_NO, "'", mark, XT_MARK_GOTO },
5681 { "^[0-9]+t$", XT_PRE_YES, "t", gototab, 0 },
5682 { "^g0$", XT_PRE_YES, "g0", movetab, XT_TAB_FIRST },
5683 { "^g[$]$", XT_PRE_YES, "g$", movetab, XT_TAB_LAST },
5684 { "^[0-9]*gt$", XT_PRE_YES, "t", movetab, XT_TAB_NEXT },
5685 { "^[0-9]*gT$", XT_PRE_YES, "T", movetab, XT_TAB_PREV },
5686 { "^M[a-zA-Z0-9]$", XT_PRE_NO, "M", qmark, XT_QMARK_SET },
5687 { "^go[a-zA-Z0-9]$", XT_PRE_NO, "go", qmark, XT_QMARK_OPEN },
5688 { "^gn[a-zA-Z0-9]$", XT_PRE_NO, "gn", qmark, XT_QMARK_TAB },
5689 { "^ZR$", XT_PRE_NO, "ZR", restart, 0 },
5690 { "^ZZ$", XT_PRE_NO, "ZZ", quit, 0 },
5691 { "^zi$", XT_PRE_NO, "zi", resizetab, XT_ZOOM_IN },
5692 { "^zo$", XT_PRE_NO, "zo", resizetab, XT_ZOOM_OUT },
5693 { "^z0$", XT_PRE_NO, "z0", resizetab, XT_ZOOM_NORMAL },
5694 { "^[0-9]+Z$", XT_PRE_YES, "Z", zoom_amount, 0 },
5695 { "^[0-9]+:$", XT_PRE_YES, ":", flip_colon, 0 },
5698 void
5699 buffercmd_init(void)
5701 int i;
5703 for (i = 0; i < LENGTH(buffercmds); i++)
5704 if (regcomp(&buffercmds[i].cregex, buffercmds[i].regex,
5705 REG_EXTENDED | REG_NOSUB))
5706 startpage_add("invalid buffercmd regex %s",
5707 buffercmds[i].regex);
5710 void
5711 buffercmd_abort(struct tab *t)
5713 int i;
5715 if (t == NULL)
5716 return;
5718 DNPRINTF(XT_D_BUFFERCMD, "%s: clearing buffer\n", __func__);
5720 for (i = 0; i < LENGTH(bcmd); i++)
5721 bcmd[i] = '\0';
5723 cmd_prefix = 0; /* clear prefix for non-buffer commands */
5724 if (t->sbe.buffercmd)
5725 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5728 void
5729 buffercmd_execute(struct tab *t, struct buffercmd *cmd)
5731 struct karg arg = {0, NULL, -1};
5733 arg.i = cmd->arg;
5734 arg.s = g_strdup(bcmd);
5736 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_execute: buffer \"%s\" "
5737 "matches regex \"%s\", executing\n", bcmd, cmd->regex);
5738 cmd->func(t, &arg);
5740 if (arg.s)
5741 g_free(arg.s);
5743 buffercmd_abort(t);
5746 gboolean
5747 buffercmd_addkey(struct tab *t, guint keyval)
5749 int i, c, match ;
5750 char s[XT_BUFCMD_SZ];
5752 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers))) {
5753 buffercmd_abort(t);
5754 return (XT_CB_PASSTHROUGH);
5757 if (keyval == GDK_Escape) {
5758 buffercmd_abort(t);
5759 return (XT_CB_HANDLED);
5762 /* key with modifier or non-ascii character */
5763 if (!isascii(keyval)) {
5765 * XXX this looks wrong but fixes some sites like
5766 * http://www.seslisozluk.com/
5767 * that eat a shift or ctrl and end putting default focus in js
5768 * instead of ignoring the keystroke
5769 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5771 return (XT_CB_HANDLED);
5774 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: adding key \"%c\" "
5775 "to buffer \"%s\"\n", keyval, bcmd);
5777 for (i = 0; i < LENGTH(bcmd); i++)
5778 if (bcmd[i] == '\0') {
5779 bcmd[i] = keyval;
5780 break;
5783 /* buffer full, ignore input */
5784 if (i >= LENGTH(bcmd) -1) {
5785 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: buffer full\n");
5786 buffercmd_abort(t);
5787 return (XT_CB_HANDLED);
5790 if (t->sbe.buffercmd != NULL)
5791 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5793 /* find exact match */
5794 for (i = 0; i < LENGTH(buffercmds); i++)
5795 if (regexec(&buffercmds[i].cregex, bcmd,
5796 (size_t) 0, NULL, 0) == 0) {
5797 buffercmd_execute(t, &buffercmds[i]);
5798 goto done;
5801 /* find non exact matches to see if we need to abort ot not */
5802 for (i = 0, match = 0; i < LENGTH(buffercmds); i++) {
5803 DNPRINTF(XT_D_BUFFERCMD, "trying: %s\n", bcmd);
5804 c = -1;
5805 s[0] = '\0';
5806 if (buffercmds[i].precount == XT_PRE_MAYBE) {
5807 if (isdigit((unsigned char)bcmd[0])) {
5808 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5809 continue;
5810 } else {
5811 c = 0;
5812 if (sscanf(bcmd, "%s", s) == 0)
5813 continue;
5815 } else if (buffercmds[i].precount == XT_PRE_YES) {
5816 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5817 continue;
5818 } else {
5819 if (sscanf(bcmd, "%s", s) == 0)
5820 continue;
5822 if (c == -1 && buffercmds[i].precount)
5823 continue;
5824 if (!strncmp(s, buffercmds[i].cmd, strlen(s)))
5825 match++;
5827 DNPRINTF(XT_D_BUFFERCMD, "got[%d] %d <%s>: %d %s\n",
5828 i, match, buffercmds[i].cmd, c, s);
5830 if (match == 0) {
5831 DNPRINTF(XT_D_BUFFERCMD, "aborting: %s\n", bcmd);
5832 buffercmd_abort(t);
5835 done:
5836 return (XT_CB_HANDLED);
5840 * XXX we were seeing a bunch of focus issues with the toplevel
5841 * main_window losing its is-active and has-toplevel-focus properties.
5842 * This is the most correct and portable solution we could come up with
5843 * without relying on calling internal GTK functions (which we
5844 * couldn't link to in Linux).
5846 #if GTK_CHECK_VERSION(3, 0, 0)
5847 void
5848 fake_focus_in(GtkWidget *w)
5850 if (fevent == NULL) {
5851 fevent = gdk_event_new(GDK_FOCUS_CHANGE);
5852 fevent->focus_change.window =
5853 gtk_widget_get_window(main_window);
5854 fevent->focus_change.type = GDK_FOCUS_CHANGE;
5855 fevent->focus_change.in = TRUE;
5857 gtk_widget_send_focus_change(main_window, fevent);
5859 #endif
5861 gboolean
5862 handle_keypress(struct tab *t, GdkEventKey *e, int entry)
5864 struct karg args;
5865 struct key_binding *k;
5868 * This sometimes gets randomly unset for whatever reason in GTK3.
5869 * If we're handling a keypress, the main window's is-active propery
5870 * *must* be true, or else many things will break.
5872 #if GTK_CHECK_VERSION(3, 0, 0)
5873 fake_focus_in(main_window);
5874 #endif
5876 /* handle keybindings if buffercmd is empty.
5877 if not empty, allow commands like C-n */
5878 if (bcmd[0] == '\0' || ((e->state & (CTRL | MOD1)) != 0))
5879 TAILQ_FOREACH(k, &kbl, entry)
5880 if (e->keyval == k->key
5881 && (entry ? k->use_in_entry : 1)) {
5882 /* when we are edditing eat ctrl/mod keys */
5883 if (edit_mode == XT_EM_VI &&
5884 t->mode == XT_MODE_INSERT &&
5885 (e->state & CTRL || e->state & MOD1))
5886 return (XT_CB_PASSTHROUGH);
5888 if (k->mask == 0) {
5889 if ((e->state & (CTRL | MOD1)) == 0)
5890 goto runcmd;
5891 } else if ((e->state & k->mask) == k->mask) {
5892 goto runcmd;
5896 if (!entry && ((e->state & (CTRL | MOD1)) == 0))
5897 return (buffercmd_addkey(t, e->keyval));
5899 return (XT_CB_PASSTHROUGH);
5901 runcmd:
5902 if (k->cmd[0] == ':') {
5903 args.i = ':';
5904 args.s = &k->cmd[1];
5905 return (command(t, &args));
5906 } else
5907 return (cmd_execute(t, k->cmd));
5911 wv_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5913 char s[2];
5915 /* don't use w directly; use t->whatever instead */
5917 if (t == NULL) {
5918 show_oops(NULL, "wv_keypress_cb");
5919 return (XT_CB_PASSTHROUGH);
5922 hide_oops(t);
5924 if (t->mode_cb)
5925 return (t->mode_cb(t, e, t->mode_cb_data));
5927 DNPRINTF(XT_D_KEY, "wv_keypress_cb: mode %d keyval 0x%x mask "
5928 "0x%x tab %d\n", t->mode, e->keyval, e->state, t->tab_id);
5930 /* Hide buffers, if they are visible, with escape. */
5931 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)) &&
5932 CLEAN(e->state) == 0 && e->keyval == GDK_Escape) {
5933 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
5934 hide_buffers(t);
5935 return (XT_CB_HANDLED);
5938 if ((CLEAN(e->state) == 0 && e->keyval == GDK_Tab) ||
5939 (CLEAN(e->state) == SHFT && e->keyval == GDK_Tab))
5940 /* something focussy is about to happen */
5941 return (XT_CB_PASSTHROUGH);
5943 /* check if we are some sort of text input thing in the dom */
5944 input_check_mode(t);
5946 if (t->mode == XT_MODE_PASSTHROUGH) {
5947 if (CLEAN(e->state) == 0 && e->keyval == GDK_Escape)
5948 t->mode = XT_MODE_COMMAND;
5949 return (XT_CB_PASSTHROUGH);
5950 } else if (t->mode == XT_MODE_COMMAND || t->mode == XT_MODE_HINT) {
5951 /* prefix input */
5952 snprintf(s, sizeof s, "%c", e->keyval);
5953 if (CLEAN(e->state) == 0 && isdigit((unsigned char)s[0]))
5954 cmd_prefix = 10 * cmd_prefix + atoi(s);
5955 return (handle_keypress(t, e, 0));
5956 } else {
5957 /* insert mode */
5958 return (handle_keypress(t, e, 1));
5961 /* NOTREACHED */
5962 return (XT_CB_PASSTHROUGH);
5965 gboolean
5966 hint_continue(struct tab *t)
5968 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5969 char *s;
5970 const gchar *errstr = NULL;
5971 gboolean rv = TRUE;
5972 int i;
5974 if (!(c[0] == '.' || c[0] == ','))
5975 goto done;
5976 if (strlen(c) == 1) {
5977 /* XXX should not happen */
5978 rv = TRUE;
5979 goto done;
5982 if (isdigit((unsigned char)c[1])) {
5983 /* numeric input */
5984 i = strtonum(&c[1], 1, 4096, &errstr);
5985 if (errstr) {
5986 show_oops(t, "invalid numerical hint %s", &c[1]);
5987 goto done;
5989 s = g_strdup_printf("hints.updateHints(%d);", i);
5990 run_script(t, s);
5991 g_free(s);
5992 } else {
5993 /* alphanumeric input */
5994 s = g_strdup_printf("hints.createHints('%s', '%c');",
5995 &c[1], c[0] == '.' ? 'f' : 'F');
5996 run_script(t, s);
5997 g_free(s);
6000 rv = FALSE;
6001 done:
6002 return (rv);
6005 gboolean
6006 search_continue(struct tab *t)
6008 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
6009 gboolean rv = FALSE;
6011 if (c[0] == ':' || c[0] == '.' || c[0] == ',')
6012 goto done;
6013 if (strlen(c) == 1) {
6014 webkit_web_view_unmark_text_matches(t->wv);
6015 goto done;
6018 if (c[0] == '/')
6019 t->search_forward = TRUE;
6020 else if (c[0] == '?')
6021 t->search_forward = FALSE;
6022 else
6023 goto done;
6025 rv = TRUE;
6026 done:
6027 return (rv);
6030 gboolean
6031 search_cb(struct tab *t)
6033 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
6034 #if !GTK_CHECK_VERSION(3, 0, 0)
6035 GdkColor color;
6036 #endif
6038 if (search_continue(t) == FALSE)
6039 goto done;
6041 /* search */
6042 if (webkit_web_view_search_text(t->wv, &c[1], FALSE, t->search_forward,
6043 TRUE) == FALSE) {
6044 /* not found, mark red */
6045 #if GTK_CHECK_VERSION(3, 0, 0)
6046 gtk_widget_set_name(t->cmd, XT_CSS_RED);
6047 #else
6048 gdk_color_parse(XT_COLOR_RED, &color);
6049 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL, &color);
6050 #endif
6051 /* unmark and remove selection */
6052 webkit_web_view_unmark_text_matches(t->wv);
6053 /* my kingdom for a way to unselect text in webview */
6054 } else {
6055 /* found, highlight all */
6056 webkit_web_view_unmark_text_matches(t->wv);
6057 webkit_web_view_mark_text_matches(t->wv, &c[1], FALSE, 0);
6058 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
6059 #if GTK_CHECK_VERSION(3, 0, 0)
6060 gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
6061 #else
6062 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
6063 &t->default_style->base[GTK_STATE_NORMAL]);
6064 #endif
6066 done:
6067 t->search_id = 0;
6068 return (FALSE);
6072 cmd_keyrelease_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6074 const gchar *c = gtk_entry_get_text(w);
6076 if (t == NULL) {
6077 show_oops(NULL, "cmd_keyrelease_cb invalid parameters");
6078 return (XT_CB_PASSTHROUGH);
6081 DNPRINTF(XT_D_CMD, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
6082 e->keyval, e->state, t->tab_id);
6084 /* hinting */
6085 if (!(e->keyval == GDK_Tab || e->keyval == GDK_ISO_Left_Tab)) {
6086 if (hint_continue(t) == FALSE)
6087 goto done;
6090 /* search */
6091 if (search_continue(t) == FALSE)
6092 goto done;
6094 /* if search length is > 4 then no longer play timeout games */
6095 if (strlen(c) > 4) {
6096 if (t->search_id) {
6097 g_source_remove(t->search_id);
6098 t->search_id = 0;
6100 search_cb(t);
6101 goto done;
6104 /* reestablish a new timer if the user types fast */
6105 if (t->search_id)
6106 g_source_remove(t->search_id);
6107 t->search_id = g_timeout_add(250, (GSourceFunc)search_cb, (gpointer)t);
6109 done:
6110 return (XT_CB_PASSTHROUGH);
6113 gboolean
6114 match_uri(const gchar *uri, const gchar *key) {
6115 gchar *voffset;
6116 size_t len;
6117 gboolean match = FALSE;
6119 len = strlen(key);
6121 if (!strncmp(key, uri, len))
6122 match = TRUE;
6123 else {
6124 voffset = strstr(uri, "/") + 2;
6125 if (!strncmp(key, voffset, len))
6126 match = TRUE;
6127 else if (g_str_has_prefix(voffset, "www.")) {
6128 voffset = voffset + strlen("www.");
6129 if (!strncmp(key, voffset, len))
6130 match = TRUE;
6134 return (match);
6137 gboolean
6138 match_session(const gchar *name, const gchar *key) {
6139 char *sub;
6141 sub = strcasestr(name, key);
6143 return sub == name;
6146 void
6147 cmd_getlist(int id, char *key)
6149 int i, dep, c = 0;
6150 struct history *h;
6151 struct session *s;
6153 if (key == NULL)
6154 return;
6156 if (id >= 0) {
6157 if (cmds[id].type & XT_URLARG) {
6158 RB_FOREACH_REVERSE(h, history_list, &hl)
6159 if (match_uri(h->uri, key)) {
6160 cmd_status.list[c] = (char *)h->uri;
6161 if (++c > 255)
6162 break;
6164 cmd_status.len = c;
6165 return;
6166 } else if (cmds[id].type & XT_SESSARG) {
6167 TAILQ_FOREACH(s, &sessions, entry)
6168 if (match_session(s->name, key)) {
6169 cmd_status.list[c] = (char *)s->name;
6170 if (++c > 255)
6171 break;
6173 cmd_status.len = c;
6174 return;
6175 } else if (cmds[id].type & XT_SETARG) {
6176 for (i = 0; i < get_settings_size(); i++)
6177 if (!strncmp(key, get_setting_name(i),
6178 strlen(key)))
6179 cmd_status.list[c++] =
6180 get_setting_name(i);
6181 cmd_status.len = c;
6182 return;
6186 dep = (id == -1) ? 0 : cmds[id].level + 1;
6188 for (i = id + 1; i < LENGTH(cmds); i++) {
6189 if (cmds[i].level < dep)
6190 break;
6191 if (cmds[i].level == dep && !strncmp(key, cmds[i].cmd,
6192 strlen(key)) && !isdigit((unsigned char)cmds[i].cmd[0]))
6193 cmd_status.list[c++] = cmds[i].cmd;
6197 cmd_status.len = c;
6200 char *
6201 cmd_getnext(int dir)
6203 cmd_status.index += dir;
6205 if (cmd_status.index < 0)
6206 cmd_status.index = cmd_status.len - 1;
6207 else if (cmd_status.index >= cmd_status.len)
6208 cmd_status.index = 0;
6210 return cmd_status.list[cmd_status.index];
6214 cmd_tokenize(char *s, char *tokens[])
6216 int i = 0;
6217 char *tok, *last = NULL;
6218 size_t len = strlen(s);
6219 bool blank;
6221 blank = len == 0 || (len > 0 && s[len - 1] == ' ');
6222 for (tok = strtok_r(s, " ", &last); tok && i < 3;
6223 tok = strtok_r(NULL, " ", &last), i++)
6224 tokens[i] = tok;
6226 if (blank && i < 3)
6227 tokens[i++] = "";
6229 return (i);
6232 void
6233 cmd_complete(struct tab *t, char *str, int dir)
6235 GtkEntry *w = GTK_ENTRY(t->cmd);
6236 int i, j, levels, c = 0, dep = 0, parent = -1;
6237 int matchcount = 0;
6238 char *tok, *match, *s = g_strdup(str);
6239 char *tokens[3] = {0};
6240 char res[XT_MAX_URL_LENGTH + 32] = ":";
6241 char *sc = s;
6243 DNPRINTF(XT_D_CMD, "%s: complete %s\n", __func__, str);
6245 /* copy prefix*/
6246 for (i = 0; isdigit((unsigned char)s[i]); i++)
6247 res[i + 1] = s[i];
6249 for (; isspace((unsigned char)s[i]); i++)
6250 res[i + 1] = s[i];
6252 s += i;
6254 levels = cmd_tokenize(s, tokens);
6256 for (i = 0; i < levels - 1; i++) {
6257 tok = tokens[i];
6258 matchcount = 0;
6259 for (j = c; j < LENGTH(cmds); j++) {
6260 if (cmds[j].level < dep)
6261 break;
6262 if (cmds[j].level == dep && !strncmp(tok, cmds[j].cmd,
6263 strlen(tok))) {
6264 matchcount++;
6265 c = j + 1;
6266 if (strlen(tok) == strlen(cmds[j].cmd)) {
6267 matchcount = 1;
6268 break;
6273 if (matchcount == 1) {
6274 strlcat(res, tok, sizeof res);
6275 strlcat(res, " ", sizeof res);
6276 dep++;
6277 } else {
6278 g_free(sc);
6279 return;
6282 parent = c - 1;
6285 if (cmd_status.index == -1)
6286 cmd_getlist(parent, tokens[i]);
6288 if (cmd_status.len > 0) {
6289 match = cmd_getnext(dir);
6290 strlcat(res, match, sizeof res);
6291 gtk_entry_set_text(w, res);
6292 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6295 g_free(sc);
6298 char *
6299 parse_prefix_and_alias(const char *str, int *prefix)
6301 struct cmd_alias *c;
6302 char *s = g_strdup(str), *sc;
6304 g_strstrip(s);
6305 sc = s;
6307 if (isdigit((unsigned char)s[0])) {
6308 sscanf(s, "%d", prefix);
6309 while (isdigit((unsigned char)s[0]) ||
6310 isspace((unsigned char)s[0]))
6311 ++s;
6314 TAILQ_FOREACH(c, &cal, entry) {
6315 if (strncmp(s, c->alias, strlen(c->alias)))
6316 continue;
6318 if (strlen(s) == strlen(c->alias)) {
6319 g_free(sc);
6320 return (g_strdup(c->cmd));
6323 if (!isspace((unsigned char)s[strlen(c->alias)]))
6324 continue;
6326 s = g_strdup_printf("%s %s", c->cmd, &s[strlen(c->alias) + 1]);
6327 g_free(sc);
6328 return (s);
6330 s = g_strdup(s);
6331 g_free(sc);
6332 return (s);
6335 gboolean
6336 cmd_execute(struct tab *t, char *str)
6338 struct cmd *cmd = NULL;
6339 char *tok, *last = NULL, *s = str;
6340 int j = 0, len, c = 0, dep = 0, matchcount = 0;
6341 int prefix = -1, rv = XT_CB_PASSTHROUGH;
6342 struct karg arg = {0, NULL, -1};
6344 s = parse_prefix_and_alias(s, &prefix);
6346 for (tok = strtok_r(s, " ", &last); tok;
6347 tok = strtok_r(NULL, " ", &last)) {
6348 matchcount = 0;
6349 for (j = c; j < LENGTH(cmds); j++) {
6350 if (cmds[j].level < dep)
6351 break;
6352 len = (tok[strlen(tok) - 1] == '!') ? strlen(tok) - 1 :
6353 strlen(tok);
6354 if (cmds[j].level == dep &&
6355 !strncmp(tok, cmds[j].cmd, len)) {
6356 matchcount++;
6357 c = j + 1;
6358 cmd = &cmds[j];
6359 if (len == strlen(cmds[j].cmd)) {
6360 matchcount = 1;
6361 break;
6365 if (matchcount == 1) {
6366 if (cmd->type > 0)
6367 goto execute_cmd;
6368 dep++;
6369 } else {
6370 show_oops(t, "Invalid command: %s", str);
6371 goto done;
6374 execute_cmd:
6375 if (cmd == NULL) {
6376 show_oops(t, "Empty command");
6377 goto done;
6379 arg.i = cmd->arg;
6381 if (prefix != -1)
6382 arg.precount = prefix;
6383 else if (cmd_prefix > 0)
6384 arg.precount = cmd_prefix;
6386 if (j > 0 && !(cmd->type & XT_PREFIX) && arg.precount > -1) {
6387 show_oops(t, "No prefix allowed: %s", str);
6388 goto done;
6390 if (cmd->type > 1)
6391 arg.s = last ? g_strdup(last) : g_strdup("");
6392 if (cmd->type & XT_INTARG && last && strlen(last) > 0) {
6393 if (arg.s == NULL) {
6394 show_oops(t, "Invalid command");
6395 goto done;
6397 arg.precount = atoi(arg.s);
6398 if (arg.precount <= 0) {
6399 if (arg.s[0] == '0')
6400 show_oops(t, "Zero count");
6401 else
6402 show_oops(t, "Trailing characters");
6403 goto done;
6407 DNPRINTF(XT_D_CMD, "%s: prefix %d arg %s\n",
6408 __func__, arg.precount, arg.s);
6410 cmd->func(t, &arg);
6412 rv = XT_CB_HANDLED;
6413 done:
6414 if (j > 0)
6415 cmd_prefix = 0;
6416 g_free(s);
6417 if (arg.s)
6418 g_free(arg.s);
6420 return (rv);
6424 save_runtime_setting(const char *name, const char *val)
6426 struct stat sb;
6427 FILE *f;
6428 size_t linelen;
6429 int found = 0;
6430 char file[PATH_MAX];
6431 char delim[3] = { '\0', '\0', '\0' };
6432 char *line, *lt, *start;
6433 char *contents, *tmp;
6435 if (runtime_settings == NULL || strlen(runtime_settings) == 0)
6436 return (-1);
6438 snprintf(file, sizeof file, "%s" PS "%s", work_dir, runtime_settings);
6439 if (stat(file, &sb) || (f = fopen(file, "r+")) == NULL)
6440 return (-1);
6441 lt = g_strdup_printf("%s=%s", name, val);
6442 contents = g_strdup("");
6443 while (!feof(f)) {
6444 line = fparseln(f, &linelen, NULL, delim, 0);
6445 if (line == NULL || linelen == 0)
6446 continue;
6447 tmp = contents;
6448 start = g_strdup_printf("%s=", name);
6449 if (strstr(line, start) == NULL)
6450 contents = g_strdup_printf("%s%s\n", contents, line);
6451 else {
6452 found = 1;
6453 contents = g_strdup_printf("%s%s\n", contents, lt);
6455 g_free(tmp);
6456 g_free(start);
6457 free(line);
6458 line = NULL;
6460 if (found == 0) {
6461 tmp = contents;
6462 contents = g_strdup_printf("%s%s\n", contents, lt);
6463 g_free(tmp);
6465 if ((f = freopen(file, "w", f)) == NULL)
6466 return (-1);
6467 else {
6468 fputs(contents, f);
6469 fclose(f);
6471 g_free(lt);
6472 g_free(contents);
6474 return (0);
6478 entry_focus_cb(GtkWidget *w, GdkEvent e, struct tab *t)
6481 * This sometimes gets randomly unset for whatever reason in GTK3,
6482 * causing a GtkEntry's text cursor becomes invisible. When we focus
6483 * a GtkEntry, be sure to manually reset the main window's is-active
6484 * property so the cursor is shown correctly.
6486 #if GTK_CHECK_VERSION(3, 0, 0)
6487 fake_focus_in(main_window);
6488 #endif
6489 return (XT_CB_PASSTHROUGH);
6493 entry_key_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6495 if (t == NULL) {
6496 show_oops(NULL, "entry_key_cb invalid parameters");
6497 return (XT_CB_PASSTHROUGH);
6500 DNPRINTF(XT_D_CMD, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6501 e->keyval, e->state, t->tab_id);
6503 hide_oops(t);
6505 if (e->keyval == GDK_Escape) {
6506 /* don't use focus_webview(t) because we want to type :cmds */
6507 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
6510 return (handle_keypress(t, e, 1));
6513 struct command_entry *
6514 history_prev(struct command_list *l, struct command_entry *at)
6516 if (at == NULL)
6517 at = TAILQ_LAST(l, command_list);
6518 else {
6519 at = TAILQ_PREV(at, command_list, entry);
6520 if (at == NULL)
6521 at = TAILQ_LAST(l, command_list);
6524 return (at);
6527 struct command_entry *
6528 history_next(struct command_list *l, struct command_entry *at)
6530 if (at == NULL)
6531 at = TAILQ_FIRST(l);
6532 else {
6533 at = TAILQ_NEXT(at, entry);
6534 if (at == NULL)
6535 at = TAILQ_FIRST(l);
6538 return (at);
6542 cmd_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6544 int rv = XT_CB_HANDLED;
6545 const gchar *c = gtk_entry_get_text(w);
6546 char *s;
6548 if (t == NULL) {
6549 show_oops(NULL, "cmd_keypress_cb parameters");
6550 return (XT_CB_PASSTHROUGH);
6553 DNPRINTF(XT_D_CMD, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6554 e->keyval, e->state, t->tab_id);
6556 /* sanity */
6557 if (c == NULL)
6558 e->keyval = GDK_Escape;
6559 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6560 c[0] == '.' || c[0] == ','))
6561 e->keyval = GDK_Escape;
6563 if (e->keyval != GDK_Tab && e->keyval != GDK_Shift_L &&
6564 e->keyval != GDK_ISO_Left_Tab)
6565 cmd_status.index = -1;
6567 switch (e->keyval) {
6568 case GDK_Tab:
6569 if (c[0] == ':')
6570 cmd_complete(t, (char *)&c[1], 1);
6571 else if (c[0] == '.' || c[0] == ',')
6572 run_script(t, "hints.focusNextHint();");
6573 goto done;
6574 case GDK_ISO_Left_Tab:
6575 if (c[0] == ':')
6576 cmd_complete(t, (char *)&c[1], -1);
6577 else if (c[0] == '.' || c[0] == ',')
6578 run_script(t, "hints.focusPreviousHint();");
6579 goto done;
6580 case GDK_Down:
6581 if (c[0] == '?') {
6582 if ((search_at = history_next(&shl, search_at))) {
6583 search_at->line[0] = c[0];
6584 gtk_entry_set_text(w, search_at->line);
6585 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6587 } else if (c[0] == '/') {
6588 if ((search_at = history_prev(&shl, search_at))) {
6589 search_at->line[0] = c[0];
6590 gtk_entry_set_text(w, search_at->line);
6591 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6593 } else if (c[0] == ':') {
6594 if ((history_at = history_prev(&chl, history_at))) {
6595 history_at->line[0] = c[0];
6596 gtk_entry_set_text(w, history_at->line);
6597 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6600 goto done;
6601 case GDK_Up:
6602 if (c[0] == '/') {
6603 if ((search_at = history_next(&shl, search_at))) {
6604 search_at->line[0] = c[0];
6605 gtk_entry_set_text(w, search_at->line);
6606 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6608 } else if (c[0] == '?') {
6609 if ((search_at = history_prev(&shl, search_at))) {
6610 search_at->line[0] = c[0];
6611 gtk_entry_set_text(w, search_at->line);
6612 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6614 } if (c[0] == ':') {
6615 if ((history_at = history_next(&chl, history_at))) {
6616 history_at->line[0] = c[0];
6617 gtk_entry_set_text(w, history_at->line);
6618 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6621 goto done;
6622 case GDK_BackSpace:
6623 if (!(!strcmp(c, ":") || !strcmp(c, "/") || !strcmp(c, "?") ||
6624 !strcmp(c, ".") || !strcmp(c, ","))) {
6625 /* see if we are doing hinting and reset it */
6626 if (c[0] == '.' || c[0] == ',') {
6627 /* recreate hints */
6628 s = g_strdup_printf("hints.createHints('', "
6629 "'%c');", c[0] == '.' ? 'f' : 'F');
6630 run_script(t, s);
6631 g_free(s);
6633 break;
6636 /* FALLTHROUGH */
6637 case GDK_Escape:
6638 hide_cmd(t);
6640 /* cancel search */
6641 if (c != NULL && (c[0] == '/' || c[0] == '?'))
6642 webkit_web_view_unmark_text_matches(t->wv);
6644 /* no need to cancel hints */
6645 goto done;
6648 rv = XT_CB_PASSTHROUGH;
6649 done:
6650 return (rv);
6653 void
6654 wv_popup_activ_cb(GtkMenuItem *menu, struct tab *t)
6656 GtkClipboard *clipboard, *primary;
6657 const gchar *name, *uri;
6658 #if !GTK_CHECK_VERSION(3, 4, 0)
6659 GtkAction *a;
6661 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
6662 if (a == NULL)
6663 return;
6664 name = gtk_action_get_name(a);
6665 #else
6666 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(menu));
6667 #endif
6668 if (name == NULL)
6669 return;
6671 DNPRINTF(XT_D_CMD, "wv_popup_activ_cb: tab %d action %s\n",
6672 t->tab_id, name);
6675 * context-menu-action-3 copy link location
6676 * context-menu-action-7 copy image address
6677 * context-menu-action-2030 copy video link location
6680 /* copy actions */
6681 if ((g_strcmp0(name, "context-menu-action-3") == 0) ||
6682 (g_strcmp0(name, "context-menu-action-7") == 0) ||
6683 (g_strcmp0(name, "context-menu-action-2030") == 0)) {
6684 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
6685 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
6686 uri = gtk_clipboard_wait_for_text(clipboard);
6687 if (uri == NULL)
6688 return;
6689 gtk_clipboard_set_text(primary, uri, -1);
6693 void
6694 wv_popup_cb(WebKitWebView *wview, GtkMenu *menu, struct tab *t)
6696 GList *items, *l;
6698 DNPRINTF(XT_D_CMD, "wv_popup_cb: tab %d\n", t->tab_id);
6700 items = gtk_container_get_children(GTK_CONTAINER(menu));
6701 for (l = items; l; l = l->next)
6702 g_signal_connect(l->data, "activate",
6703 G_CALLBACK(wv_popup_activ_cb), t);
6704 g_list_free(items);
6707 void
6708 wv_deny_location_cb(WebKitWebView *wv, WebKitWebFrame *frame,
6709 WebKitGeolocationPolicyDecision *pd, struct tab *t)
6711 webkit_geolocation_policy_deny(pd);
6714 void
6715 cmd_popup_cb(GtkEntry *entry, GtkMenu *menu, struct tab *t)
6717 /* popup menu enabled */
6718 t->popup = 1;
6722 cmd_focusout_cb(GtkWidget *w, GdkEventFocus *e, struct tab *t)
6724 if (t == NULL) {
6725 show_oops(NULL, "cmd_focusout_cb invalid parameters");
6726 return (XT_CB_PASSTHROUGH);
6729 DNPRINTF(XT_D_CMD, "cmd_focusout_cb: tab %d popup %d\n",
6730 t->tab_id, t->popup);
6732 /* if popup is enabled don't lose focus */
6733 if (t->popup) {
6734 t->popup = 0;
6735 return (XT_CB_PASSTHROUGH);
6738 hide_cmd(t);
6739 hide_oops(t);
6740 disable_hints(t);
6742 return (XT_CB_PASSTHROUGH);
6745 void
6746 cmd_hide_cb(GtkWidget *w, struct tab *t)
6748 if (t == NULL) {
6749 show_oops(NULL, "%s: invalid parameters", __func__);
6750 return;
6753 if (show_url == 0 || t->focus_wv)
6754 focus_webview(t);
6755 else
6756 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
6759 void
6760 cmd_activate_cb(GtkEntry *entry, struct tab *t)
6762 char *s;
6763 const gchar *c = gtk_entry_get_text(entry);
6765 if (t == NULL) {
6766 show_oops(NULL, "cmd_activate_cb invalid parameters");
6767 return;
6770 DNPRINTF(XT_D_CMD, "cmd_activate_cb: tab %d %s\n", t->tab_id, c);
6772 /* sanity */
6773 if (c == NULL)
6774 goto done;
6775 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6776 c[0] == '.' || c[0] == ','))
6777 goto done;
6778 if (strlen(c) < 1)
6779 goto done;
6780 s = (char *)&c[1];
6782 if (c[0] == '/' || c[0] == '?') {
6783 /* see if there is a timer pending */
6784 if (t->search_id) {
6785 g_source_remove(t->search_id);
6786 t->search_id = 0;
6787 search_cb(t);
6790 if (t->search_text) {
6791 g_free(t->search_text);
6792 t->search_text = NULL;
6795 t->search_text = g_strdup(s);
6796 if (global_search)
6797 g_free(global_search);
6798 global_search = g_strdup(s);
6799 t->search_forward = c[0] == '/';
6801 history_add(&shl, search_file, s, &search_history_count);
6802 } else if (c[0] == '.' || c[0] == ',') {
6803 run_script(t, "hints.fire();");
6804 /* XXX history for link following? */
6805 } else if (c[0] == ':') {
6806 history_add(&chl, command_file, s, &cmd_history_count);
6807 /* can't call hide_cmd after cmd_execute */
6808 hide_cmd(t);
6809 cmd_execute(t, s);
6810 return;
6813 done:
6814 hide_cmd(t);
6817 void
6818 backward_cb(GtkWidget *w, struct tab *t)
6820 struct karg a;
6822 if (t == NULL) {
6823 show_oops(NULL, "backward_cb invalid parameters");
6824 return;
6827 DNPRINTF(XT_D_NAV, "backward_cb: tab %d\n", t->tab_id);
6829 a.i = XT_NAV_BACK;
6830 navaction(t, &a);
6833 void
6834 forward_cb(GtkWidget *w, struct tab *t)
6836 struct karg a;
6838 if (t == NULL) {
6839 show_oops(NULL, "forward_cb invalid parameters");
6840 return;
6843 DNPRINTF(XT_D_NAV, "forward_cb: tab %d\n", t->tab_id);
6845 a.i = XT_NAV_FORWARD;
6846 navaction(t, &a);
6849 void
6850 home_cb(GtkWidget *w, struct tab *t)
6852 if (t == NULL) {
6853 show_oops(NULL, "home_cb invalid parameters");
6854 return;
6857 DNPRINTF(XT_D_NAV, "home_cb: tab %d\n", t->tab_id);
6859 load_uri(t, home);
6862 void
6863 stop_cb(GtkWidget *w, struct tab *t)
6865 WebKitWebFrame *frame;
6867 if (t == NULL) {
6868 show_oops(NULL, "stop_cb invalid parameters");
6869 return;
6872 DNPRINTF(XT_D_NAV, "stop_cb: tab %d\n", t->tab_id);
6874 frame = webkit_web_view_get_main_frame(t->wv);
6875 if (frame == NULL) {
6876 show_oops(t, "stop_cb: no frame");
6877 return;
6880 webkit_web_frame_stop_loading(frame);
6881 abort_favicon_download(t);
6884 void
6885 setup_webkit(struct tab *t)
6887 if (is_g_object_setting(G_OBJECT(t->settings), "enable-dns-prefetching"))
6888 g_object_set(G_OBJECT(t->settings), "enable-dns-prefetching",
6889 FALSE, (char *)NULL);
6890 else
6891 warnx("webkit does not have \"enable-dns-prefetching\" property");
6892 g_object_set(G_OBJECT(t->settings), "default-encoding", encoding,
6893 (char *)NULL);
6894 g_object_set(G_OBJECT(t->settings),
6895 "enable-scripts", enable_scripts, (char *)NULL);
6896 g_object_set(G_OBJECT(t->settings),
6897 "enable-plugins", enable_plugins, (char *)NULL);
6898 g_object_set(G_OBJECT(t->settings),
6899 "javascript-can-open-windows-automatically",
6900 js_auto_open_windows, (char *)NULL);
6901 g_object_set(G_OBJECT(t->settings),
6902 "enable-html5-database", FALSE, (char *)NULL);
6903 g_object_set(G_OBJECT(t->settings),
6904 "enable-html5-local-storage", enable_localstorage, (char *)NULL);
6905 g_object_set(G_OBJECT(t->settings),
6906 "enable_spell_checking", enable_spell_checking, (char *)NULL);
6907 g_object_set(G_OBJECT(t->settings),
6908 "spell_checking_languages", spell_check_languages, (char *)NULL);
6909 g_object_set(G_OBJECT(t->settings),
6910 "enable-developer-extras", TRUE, (char *)NULL);
6911 g_object_set(G_OBJECT(t->wv),
6912 "full-content-zoom", TRUE, (char *)NULL);
6913 g_object_set(G_OBJECT(t->settings),
6914 "auto-load-images", auto_load_images, (char *)NULL);
6915 if (is_g_object_setting(G_OBJECT(t->settings),
6916 "enable-display-of-insecure-content"))
6917 g_object_set(G_OBJECT(t->settings),
6918 "enable-display-of-insecure-content",
6919 allow_insecure_content, (char *)NULL);
6920 if (is_g_object_setting(G_OBJECT(t->settings),
6921 "enable-running-of-insecure-content"))
6922 g_object_set(G_OBJECT(t->settings),
6923 "enable-running-of-insecure-content",
6924 allow_insecure_scripts, (char *)NULL);
6926 webkit_web_view_set_settings(t->wv, t->settings);
6929 gboolean
6930 update_statusbar_position(GtkAdjustment* adjustment, gpointer data)
6932 struct tab *ti, *t = NULL;
6933 gdouble view_size, value, max;
6934 gchar *position;
6936 TAILQ_FOREACH(ti, &tabs, entry)
6937 if (ti->tab_id == gtk_notebook_get_current_page(notebook)) {
6938 t = ti;
6939 break;
6942 if (t == NULL)
6943 return FALSE;
6945 if (adjustment == NULL)
6946 adjustment = gtk_scrolled_window_get_vadjustment(
6947 GTK_SCROLLED_WINDOW(t->browser_win));
6949 view_size = gtk_adjustment_get_page_size(adjustment);
6950 value = gtk_adjustment_get_value(adjustment);
6951 max = gtk_adjustment_get_upper(adjustment) - view_size;
6953 if (max > -0.0001 && max < 0.0001)
6954 position = g_strdup("All");
6955 else if (value > (max - 0.0001) && value < (max + 0.0001))
6956 position = g_strdup("Bot");
6957 else if (value > -0.0001 && value < 0.0001)
6958 position = g_strdup("Top");
6959 else
6960 position = g_strdup_printf("%d%%", (int) ((value / max) * 100));
6962 if (t->sbe.position != NULL)
6963 gtk_label_set_text(GTK_LABEL(t->sbe.position), position);
6964 g_free(position);
6966 return (TRUE);
6969 GtkWidget *
6970 create_window(const gchar *name)
6972 GtkWidget *w;
6974 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
6975 if (window_maximize)
6976 gtk_window_maximize(GTK_WINDOW(w));
6977 else
6978 gtk_window_set_default_size(GTK_WINDOW(w), window_width, window_height);
6979 gtk_widget_set_name(w, name);
6980 gtk_window_set_wmclass(GTK_WINDOW(w), name, "Xombrero");
6982 return (w);
6985 GtkWidget *
6986 create_browser(struct tab *t)
6988 GtkWidget *w;
6989 GtkAdjustment *adjustment;
6991 if (t == NULL) {
6992 show_oops(NULL, "create_browser invalid parameters");
6993 return (NULL);
6996 w = gtk_scrolled_window_new(NULL, NULL);
6997 gtk_widget_set_can_focus(w, FALSE);
6998 t->adjust_h = gtk_scrolled_window_get_hadjustment(
6999 GTK_SCROLLED_WINDOW(w));
7000 t->adjust_v = gtk_scrolled_window_get_vadjustment(
7001 GTK_SCROLLED_WINDOW(w));
7002 #if !GTK_CHECK_VERSION(3, 0, 0)
7003 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
7004 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
7005 #endif
7008 t->wv = WEBKIT_WEB_VIEW(webkit_web_view_new());
7009 gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(t->wv));
7011 /* set defaults */
7012 t->settings = webkit_web_view_get_settings(t->wv);
7013 t->stylesheet = g_strdup(stylesheet);
7014 t->load_images = auto_load_images;
7016 adjustment =
7017 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w));
7018 g_signal_connect(G_OBJECT(adjustment), "value-changed",
7019 G_CALLBACK(update_statusbar_position), NULL);
7021 setup_webkit(t);
7022 setup_inspector(t);
7024 return (w);
7027 GtkWidget *
7028 create_kiosk_toolbar(struct tab *t)
7030 GtkWidget *toolbar = NULL;
7031 GtkBox *b;
7033 #if GTK_CHECK_VERSION(3, 0, 0)
7034 toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
7035 gtk_widget_set_name(toolbar, "toolbar");
7036 #else
7037 toolbar = gtk_hbox_new(FALSE, 1);
7038 #endif
7040 b = GTK_BOX(toolbar);
7041 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
7043 /* backward button */
7044 t->backward = create_button("Back", "go-previous", 0);
7045 gtk_widget_set_sensitive(t->backward, FALSE);
7046 g_signal_connect(G_OBJECT(t->backward), "clicked",
7047 G_CALLBACK(backward_cb), t);
7048 gtk_box_pack_start(b, t->backward, TRUE, TRUE, 0);
7050 /* forward button */
7051 t->forward = create_button("Forward", "go-next", 0);
7052 gtk_widget_set_sensitive(t->forward, FALSE);
7053 g_signal_connect(G_OBJECT(t->forward), "clicked",
7054 G_CALLBACK(forward_cb), t);
7055 gtk_box_pack_start(b, t->forward, TRUE, TRUE, 0);
7057 /* home button */
7058 t->gohome = create_button("Home", "go-home", 0);
7059 gtk_widget_set_sensitive(t->gohome, true);
7060 g_signal_connect(G_OBJECT(t->gohome), "clicked",
7061 G_CALLBACK(home_cb), t);
7062 gtk_box_pack_start(b, t->gohome, TRUE, TRUE, 0);
7065 * Create widgets but don't use them. This is just so we don't get
7066 * loads of errors when trying to do stuff with them. Sink the floating
7067 * reference here, and do a manual unreference when the tab is deleted.
7069 t->uri_entry = gtk_entry_new();
7070 g_object_ref_sink(G_OBJECT(t->uri_entry));
7071 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
7072 G_CALLBACK(entry_focus_cb), t);
7073 t->stop = create_button("Stop", "process-stop", 0);
7074 g_object_ref_sink(G_OBJECT(t->stop));
7075 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
7076 "media-playback-start" : "media-playback-pause", 0);
7077 g_object_ref_sink(G_OBJECT(t->stop));
7079 #if !GTK_CHECK_VERSION(3, 0, 0)
7080 t->default_style = gtk_rc_get_style(t->uri_entry);
7081 #endif
7083 return (toolbar);
7086 GtkWidget *
7087 create_toolbar(struct tab *t)
7089 GtkWidget *toolbar = NULL;
7090 GtkBox *b;
7093 * t->stop, t->js_toggle, and t->uri_entry are shared by the kiosk
7094 * toolbar, but not shown or used. We still create them there so we can
7095 * avoid loads of warnings when trying to use them. Instead, we sink
7096 * the floating reference here, and do the final unreference when
7097 * deleting the tab.
7100 #if GTK_CHECK_VERSION(3, 0, 0)
7101 toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
7102 gtk_widget_set_name(toolbar, "toolbar");
7103 #else
7104 toolbar = gtk_hbox_new(FALSE, 1);
7105 #endif
7107 b = GTK_BOX(toolbar);
7108 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
7110 /* backward button */
7111 t->backward = create_button("Back", "go-previous", 0);
7112 gtk_widget_set_sensitive(t->backward, FALSE);
7113 g_signal_connect(G_OBJECT(t->backward), "clicked",
7114 G_CALLBACK(backward_cb), t);
7115 gtk_box_pack_start(b, t->backward, FALSE, FALSE, 0);
7117 /* forward button */
7118 t->forward = create_button("Forward", "go-next", 0);
7119 gtk_widget_set_sensitive(t->forward, FALSE);
7120 g_signal_connect(G_OBJECT(t->forward), "clicked",
7121 G_CALLBACK(forward_cb), t);
7122 gtk_box_pack_start(b, t->forward, FALSE, FALSE, 0);
7124 /* stop button */
7125 t->stop = create_button("Stop", "process-stop", 0);
7126 g_object_ref_sink(G_OBJECT(t->stop));
7127 gtk_widget_set_sensitive(t->stop, FALSE);
7128 g_signal_connect(G_OBJECT(t->stop), "clicked", G_CALLBACK(stop_cb), t);
7129 gtk_box_pack_start(b, t->stop, FALSE, FALSE, 0);
7131 /* JS button */
7132 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
7133 "media-playback-start" : "media-playback-pause", 0);
7134 g_object_ref_sink(G_OBJECT(t->js_toggle));
7135 gtk_widget_set_sensitive(t->js_toggle, TRUE);
7136 g_signal_connect(G_OBJECT(t->js_toggle), "clicked",
7137 G_CALLBACK(js_toggle_cb), t);
7138 gtk_box_pack_start(b, t->js_toggle, FALSE, FALSE, 0);
7140 /* toggle proxy button */
7141 t->proxy_toggle = create_button("Proxy-Toggle", proxy_uri ?
7142 "gtk-connect" : "gtk-disconnect", 0);
7143 /* override icons */
7144 if (proxy_uri)
7145 button_set_file(t->proxy_toggle, "torenabled.ico");
7146 else
7147 button_set_file(t->proxy_toggle, "tordisabled.ico");
7148 gtk_widget_set_sensitive(t->proxy_toggle, TRUE);
7149 g_signal_connect(G_OBJECT(t->proxy_toggle), "clicked",
7150 G_CALLBACK(proxy_toggle_cb), t);
7151 gtk_box_pack_start(b, t->proxy_toggle, FALSE, FALSE, 0);
7153 t->uri_entry = gtk_entry_new();
7154 g_object_ref_sink(G_OBJECT(t->uri_entry));
7155 g_signal_connect(G_OBJECT(t->uri_entry), "activate",
7156 G_CALLBACK(activate_uri_entry_cb), t);
7157 g_signal_connect(G_OBJECT(t->uri_entry), "key-press-event",
7158 G_CALLBACK(entry_key_cb), t);
7159 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
7160 G_CALLBACK(entry_focus_cb), t);
7161 completion_add(t);
7162 gtk_box_pack_start(b, t->uri_entry, TRUE, TRUE, 0);
7164 /* search entry */
7165 t->search_entry = gtk_entry_new();
7166 gtk_entry_set_width_chars(GTK_ENTRY(t->search_entry), 30);
7167 g_signal_connect(G_OBJECT(t->search_entry), "activate",
7168 G_CALLBACK(activate_search_entry_cb), t);
7169 g_signal_connect(G_OBJECT(t->search_entry), "key-press-event",
7170 G_CALLBACK(entry_key_cb), t);
7171 g_signal_connect(G_OBJECT(t->search_entry), "focus-in-event",
7172 G_CALLBACK(entry_focus_cb), t);
7173 gtk_box_pack_start(b, t->search_entry, FALSE, FALSE, 0);
7175 #if !GTK_CHECK_VERSION(3, 0, 0)
7176 t->default_style = gtk_rc_get_style(t->uri_entry);
7177 #endif
7179 return (toolbar);
7182 GtkWidget *
7183 create_buffers(struct tab *t)
7185 GtkCellRenderer *renderer;
7186 GtkWidget *view;
7188 view = gtk_tree_view_new();
7190 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
7192 renderer = gtk_cell_renderer_text_new();
7193 gtk_tree_view_insert_column_with_attributes
7194 (GTK_TREE_VIEW(view), -1, "Id", renderer, "text", COL_ID, (char *)NULL);
7196 renderer = gtk_cell_renderer_pixbuf_new();
7197 gtk_tree_view_insert_column_with_attributes
7198 (GTK_TREE_VIEW(view), -1, "Favicon", renderer, "pixbuf", COL_FAVICON,
7199 (char *)NULL);
7201 renderer = gtk_cell_renderer_text_new();
7202 gtk_tree_view_insert_column_with_attributes
7203 (GTK_TREE_VIEW(view), -1, "Title", renderer, "text", COL_TITLE,
7204 (char *)NULL);
7206 gtk_tree_view_set_model
7207 (GTK_TREE_VIEW(view), GTK_TREE_MODEL(buffers_store));
7209 return (view);
7212 void
7213 row_activated_cb(GtkTreeView *view, GtkTreePath *path,
7214 GtkTreeViewColumn *col, struct tab *t)
7216 GtkTreeIter iter;
7217 guint id;
7219 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7221 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter,
7222 path)) {
7223 gtk_tree_model_get
7224 (GTK_TREE_MODEL(buffers_store), &iter, COL_ID, &id, -1);
7225 set_current_tab(id - 1);
7228 hide_buffers(t);
7231 /* after tab reordering/creation/removal */
7232 void
7233 recalc_tabs(void)
7235 struct tab *t;
7237 TAILQ_FOREACH(t, &tabs, entry)
7238 t->tab_id = gtk_notebook_page_num(notebook, t->vbox);
7241 void
7242 update_statusbar_tabs(struct tab *t)
7244 int tab_id, max_tab_id;
7245 char s[16];
7247 if (t != NULL)
7248 tab_id = t->tab_id;
7249 else
7250 tab_id = gtk_notebook_get_current_page(notebook);
7252 max_tab_id = gtk_notebook_get_n_pages(notebook);
7253 snprintf(s, sizeof s, "%d/%d", tab_id + 1, max_tab_id);
7255 if (t == NULL)
7256 t = get_current_tab();
7258 if (t != NULL && t->sbe.tabs != NULL)
7259 gtk_label_set_text(GTK_LABEL(t->sbe.tabs), s);
7262 /* after active tab change */
7263 void
7264 recolor_compact_tabs(void)
7266 struct tab *t;
7267 int curid = 0;
7268 #if !GTK_CHECK_VERSION(3, 0, 0)
7269 GdkColor color_active, color_inactive;
7271 gdk_color_parse(XT_COLOR_CT_ACTIVE, &color_active);
7272 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color_inactive);
7273 #endif
7274 curid = gtk_notebook_get_current_page(notebook);
7276 TAILQ_FOREACH(t, &tabs, entry) {
7277 #if GTK_CHECK_VERSION(3, 0, 0)
7278 if (t->tab_id == curid)
7279 gtk_widget_set_name(t->tab_elems.label, XT_CSS_ACTIVE);
7280 else
7281 gtk_widget_set_name(t->tab_elems.label, "");
7282 #else
7283 if (t->tab_id == curid)
7284 gtk_widget_modify_fg(t->tab_elems.label,
7285 GTK_STATE_NORMAL, &color_active);
7286 else
7287 gtk_widget_modify_fg(t->tab_elems.label,
7288 GTK_STATE_NORMAL, &color_inactive);
7289 #endif
7293 void
7294 set_current_tab(int page_num)
7296 buffercmd_abort(get_current_tab());
7297 gtk_notebook_set_current_page(notebook, page_num);
7298 recolor_compact_tabs();
7299 update_statusbar_tabs(NULL);
7303 undo_close_tab_save(struct tab *t)
7305 int m, n;
7306 const gchar *uri;
7307 struct undo *u1, *u2;
7308 GList *items;
7309 GList *item;
7310 WebKitWebHistoryItem *el;
7312 if ((uri = get_uri(t)) == NULL)
7313 return (1);
7315 u1 = g_malloc0(sizeof(struct undo));
7316 u1->uri = g_strdup(uri);
7318 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7320 m = webkit_web_back_forward_list_get_forward_length(t->bfl);
7321 n = webkit_web_back_forward_list_get_back_length(t->bfl);
7322 u1->back = n;
7324 /* forward history */
7325 items = webkit_web_back_forward_list_get_forward_list_with_limit(t->bfl, m);
7326 for (item = g_list_first(items); item; item = item->next) {
7327 u1->history = g_list_prepend(u1->history,
7328 webkit_web_history_item_copy(item->data));
7330 g_list_free(items);
7332 /* current item */
7333 if (m) {
7334 el = webkit_web_back_forward_list_get_current_item(t->bfl);
7335 u1->history = g_list_prepend(u1->history,
7336 webkit_web_history_item_copy(el));
7339 /* back history */
7340 items = webkit_web_back_forward_list_get_back_list_with_limit(t->bfl, n);
7341 for (item = g_list_first(items); item; item = item->next) {
7342 u1->history = g_list_prepend(u1->history,
7343 webkit_web_history_item_copy(item->data));
7345 g_list_free(items);
7347 TAILQ_INSERT_HEAD(&undos, u1, entry);
7349 if (undo_count > XT_MAX_UNDO_CLOSE_TAB) {
7350 u2 = TAILQ_LAST(&undos, undo_tailq);
7351 TAILQ_REMOVE(&undos, u2, entry);
7352 g_free(u2->uri);
7353 g_list_free(u2->history);
7354 g_free(u2);
7355 } else
7356 undo_count++;
7358 return (0);
7361 void
7362 delete_tab(struct tab *t)
7364 struct karg a;
7366 DNPRINTF(XT_D_TAB, "delete_tab: %p\n", t);
7368 if (t == NULL)
7369 return;
7371 TAILQ_REMOVE(&tabs, t, entry);
7372 buffercmd_abort(t);
7374 /* Halt all webkit activity. */
7375 abort_favicon_download(t);
7376 webkit_web_view_stop_loading(t->wv);
7378 /* Save the tab, so we can undo the close. */
7379 undo_close_tab_save(t);
7381 /* just in case */
7382 if (t->search_id)
7383 g_source_remove(t->search_id);
7385 /* session key */
7386 if (t->session_key)
7387 g_free(t->session_key);
7389 /* inspector */
7390 bzero(&a, sizeof a);
7391 a.i = XT_INS_CLOSE;
7392 inspector_cmd(t, &a);
7394 if (browser_mode == XT_BM_KIOSK) {
7395 gtk_widget_destroy(t->uri_entry);
7396 gtk_widget_destroy(t->stop);
7397 gtk_widget_destroy(t->js_toggle);
7400 /* widgets not shown in the kiosk toolbar */
7401 gtk_widget_destroy(t->stop);
7402 g_object_unref(G_OBJECT(t->stop));
7403 gtk_widget_destroy(t->js_toggle);
7404 g_object_unref(G_OBJECT(t->js_toggle));
7405 gtk_widget_destroy(t->uri_entry);
7406 g_object_unref(G_OBJECT(t->uri_entry));
7408 g_object_unref(G_OBJECT(t->completion));
7409 if (t->item)
7410 g_object_unref(t->item);
7412 gtk_widget_destroy(t->tab_elems.eventbox);
7413 gtk_widget_destroy(t->vbox);
7415 g_free(t->stylesheet);
7416 g_free(t->tmp_uri);
7417 g_free(t->status);
7418 g_free(t);
7419 t = NULL;
7421 if (TAILQ_EMPTY(&tabs)) {
7422 if (browser_mode == XT_BM_KIOSK)
7423 create_new_tab(home, NULL, 1, -1);
7424 else
7425 create_new_tab(NULL, NULL, 1, -1);
7428 /* recreate session */
7429 if (session_autosave) {
7430 bzero(&a, sizeof a);
7431 a.s = NULL;
7432 save_tabs(NULL, &a);
7435 recalc_tabs();
7436 recolor_compact_tabs();
7439 void
7440 update_statusbar_zoom(struct tab *t)
7442 gfloat zoom;
7443 char s[16] = { '\0' };
7445 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7446 if ((zoom <= 0.99 || zoom >= 1.01))
7447 snprintf(s, sizeof s, "%d%%", (int)(zoom * 100));
7448 if (t->sbe.zoom != NULL)
7449 gtk_label_set_text(GTK_LABEL(t->sbe.zoom), s);
7452 void
7453 setzoom_webkit(struct tab *t, int adjust)
7455 #define XT_ZOOMPERCENT 0.04
7457 gfloat zoom;
7459 if (t == NULL) {
7460 show_oops(NULL, "setzoom_webkit invalid parameters");
7461 return;
7464 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7465 if (adjust == XT_ZOOM_IN)
7466 zoom += XT_ZOOMPERCENT;
7467 else if (adjust == XT_ZOOM_OUT)
7468 zoom -= XT_ZOOMPERCENT;
7469 else if (adjust > 0)
7470 zoom = default_zoom_level + adjust / 100.0 - 1.0;
7471 else {
7472 show_oops(t, "setzoom_webkit invalid zoom value");
7473 return;
7476 if (zoom < XT_ZOOMPERCENT)
7477 zoom = XT_ZOOMPERCENT;
7478 g_object_set(G_OBJECT(t->wv), "zoom-level", zoom, (char *)NULL);
7479 update_statusbar_zoom(t);
7482 gboolean
7483 tab_clicked_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
7485 struct tab *t = (struct tab *) data;
7487 DNPRINTF(XT_D_TAB, "tab_clicked_cb: tab: %d\n", t->tab_id);
7489 switch (event->button) {
7490 case 1:
7491 set_current_tab(t->tab_id);
7492 break;
7493 case 2:
7494 delete_tab(t);
7495 break;
7498 return TRUE;
7501 void
7502 append_tab(struct tab *t)
7504 if (t == NULL)
7505 return;
7507 TAILQ_INSERT_TAIL(&tabs, t, entry);
7508 t->tab_id = gtk_notebook_append_page(notebook, t->vbox, t->tab_content);
7511 GtkWidget *
7512 create_sbe(void)
7514 GtkWidget *sbe;
7516 sbe = gtk_label_new(NULL);
7517 gtk_widget_set_can_focus(GTK_WIDGET(sbe), FALSE);
7518 modify_font(GTK_WIDGET(sbe), statusbar_font);
7519 return (sbe);
7523 statusbar_create(struct tab *t)
7525 GtkWidget *box; /* container for statusbar elems */
7526 GtkWidget *sep;
7527 GtkBox *b;
7528 char *p;
7529 #if !GTK_CHECK_VERSION(3, 0, 0)
7530 GdkColor color;
7531 #endif
7533 if (t == NULL) {
7534 DPRINTF("%s: invalid parameters", __func__);
7535 return (1);
7538 #if GTK_CHECK_VERSION(3, 0, 0)
7539 t->statusbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7540 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7541 gtk_widget_set_name(GTK_WIDGET(t->statusbar), "statusbar");
7542 #else
7543 t->statusbar = gtk_hbox_new(FALSE, 0);
7544 box = gtk_hbox_new(FALSE, 0);
7545 #endif
7546 t->sbe.ebox = gtk_event_box_new();
7548 gtk_widget_set_can_focus(GTK_WIDGET(t->statusbar), FALSE);
7549 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.ebox), FALSE);
7550 gtk_widget_set_can_focus(GTK_WIDGET(box), FALSE);
7552 gtk_box_set_spacing(GTK_BOX(box), 10);
7553 gtk_box_pack_start(GTK_BOX(t->statusbar), t->sbe.ebox, TRUE, TRUE, 0);
7554 gtk_container_add(GTK_CONTAINER(t->sbe.ebox), box);
7556 t->sbe.uri = gtk_entry_new();
7557 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.uri), FALSE);
7558 modify_font(GTK_WIDGET(t->sbe.uri), statusbar_font);
7559 #if !GTK_CHECK_VERSION(3, 0, 0)
7560 gtk_entry_set_inner_border(GTK_ENTRY(t->sbe.uri), NULL);
7561 gtk_entry_set_has_frame(GTK_ENTRY(t->sbe.uri), FALSE);
7562 #endif
7564 #if GTK_CHECK_VERSION(3, 0, 0)
7565 statusbar_modify_attr(t, XT_CSS_NORMAL);
7566 #else
7567 statusbar_modify_attr(t, XT_COLOR_WHITE, XT_COLOR_BLACK);
7568 #endif
7570 b = GTK_BOX(box);
7571 gtk_box_pack_start(b, t->sbe.uri, TRUE, TRUE, 0);
7573 for (p = statusbar_elems; *p != '\0'; p++) {
7574 switch (*p) {
7575 case '|':
7576 #if GTK_CHECK_VERSION(3, 0, 0)
7577 sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
7578 #else
7579 sep = gtk_vseparator_new();
7580 gdk_color_parse(XT_COLOR_SB_SEPARATOR, &color);
7581 gtk_widget_modify_bg(sep, GTK_STATE_NORMAL, &color);
7582 #endif
7583 gtk_box_pack_start(b, sep, FALSE, FALSE, 0);
7584 break;
7585 case 'P':
7586 if (t->sbe.position == NULL) {
7587 t->sbe.position = create_sbe();
7588 gtk_box_pack_start(b, t->sbe.position, FALSE,
7589 FALSE, 0);
7591 break;
7592 case 'B':
7593 if (t->sbe.buffercmd == NULL) {
7594 t->sbe.buffercmd = create_sbe();
7595 gtk_box_pack_start(b, t->sbe.buffercmd, FALSE,
7596 FALSE, 0);
7598 break;
7599 case 'Z':
7600 if (t->sbe.zoom == NULL) {
7601 t->sbe.zoom = create_sbe();
7602 gtk_box_pack_start(b, t->sbe.zoom, FALSE, FALSE,
7605 break;
7606 case 'T':
7607 if (t->sbe.tabs == NULL) {
7608 t->sbe.tabs = create_sbe();
7609 gtk_box_pack_start(b, t->sbe.tabs, FALSE, FALSE,
7612 break;
7613 case 'p':
7614 if (t->sbe.proxy == NULL) {
7615 t->sbe.proxy = create_sbe();
7616 gtk_box_pack_start(b, t->sbe.proxy, FALSE,
7617 FALSE, 0);
7618 if (proxy_uri)
7619 gtk_label_set_text(
7620 GTK_LABEL(t->sbe.proxy), "proxy");
7622 break;
7623 default:
7624 warnx("illegal flag \"%c\" in statusbar_elems\n", *p);
7625 break;
7629 gtk_box_pack_end(GTK_BOX(t->vbox), t->statusbar, FALSE, FALSE, 0);
7631 return (0);
7634 struct tab *
7635 create_new_tab(const char *title, struct undo *u, int focus, int position)
7637 struct tab *t;
7638 int load = 1, id;
7639 GtkWidget *b, *bb;
7640 WebKitWebHistoryItem *item;
7641 GList *items;
7642 char *sv[3];
7643 #if !GTK_CHECK_VERSION(3, 0, 0)
7644 GdkColor color;
7645 #endif
7647 DNPRINTF(XT_D_TAB, "create_new_tab: title %s focus %d\n", title, focus);
7649 if (tabless && !TAILQ_EMPTY(&tabs)) {
7650 if (single_instance) {
7651 DNPRINTF(XT_D_TAB,
7652 "create_new_tab: new tab rejected\n");
7653 return (NULL);
7655 sv[0] = start_argv[0];
7656 sv[1] = (char *)title;
7657 sv[2] = (char *)NULL;
7658 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH,
7659 NULL, NULL, NULL, NULL))
7660 show_oops(NULL, "%s: could not spawn process",
7661 __func__);
7662 return (NULL);
7665 t = g_malloc0(sizeof *t);
7667 if (title == NULL) {
7668 title = "(untitled)";
7669 load = 0;
7672 #if GTK_CHECK_VERSION(3, 0, 0)
7673 t->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
7674 b = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7675 gtk_widget_set_name(t->vbox, "vbox");
7676 #else
7677 t->vbox = gtk_vbox_new(FALSE, 0);
7678 b = gtk_hbox_new(FALSE, 0);
7679 #endif
7680 gtk_widget_set_can_focus(t->vbox, FALSE);
7682 /* label + button for tab */
7683 t->tab_content = b;
7684 gtk_widget_set_can_focus(t->tab_content, FALSE);
7686 t->user_agent_id = 0;
7687 t->http_accept_id = 0;
7689 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7690 t->active = NULL;
7691 #endif
7693 #if GTK_CHECK_VERSION(2, 20, 0)
7694 t->spinner = gtk_spinner_new();
7695 #endif
7696 t->label = gtk_label_new(title);
7697 bb = create_button("Close", "window-close", 1);
7698 gtk_label_set_max_width_chars(GTK_LABEL(t->label), 20);
7699 gtk_label_set_ellipsize(GTK_LABEL(t->label), PANGO_ELLIPSIZE_END);
7700 gtk_label_set_line_wrap(GTK_LABEL(t->label), FALSE);
7701 gtk_widget_set_size_request(t->tab_content, 130, 0);
7704 * this is a total hack and most likely breaks with other styles but
7705 * is necessary so the text doesn't bounce around when the spinner is
7706 * shown/hidden
7708 #if GTK_CHECK_VERSION(3, 0, 0)
7709 gtk_widget_set_size_request(t->label, 95, 0);
7710 #else
7711 gtk_widget_set_size_request(t->label, 100, 0);
7712 #endif
7714 gtk_box_pack_start(GTK_BOX(t->tab_content), bb, FALSE, FALSE, 0);
7715 gtk_box_pack_start(GTK_BOX(t->tab_content), t->label, FALSE, FALSE, 0);
7716 #if GTK_CHECK_VERSION(2, 20, 0)
7717 gtk_box_pack_end(GTK_BOX(b), t->spinner, FALSE, FALSE, 0);
7718 #endif
7720 /* toolbar */
7721 if (browser_mode == XT_BM_KIOSK) {
7722 t->toolbar = create_kiosk_toolbar(t);
7723 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7725 } else {
7726 t->toolbar = create_toolbar(t);
7727 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7731 /* marks */
7732 marks_clear(t);
7734 /* browser */
7735 t->browser_win = create_browser(t);
7736 set_scrollbar_visibility(t, show_scrollbars);
7737 gtk_box_pack_start(GTK_BOX(t->vbox), t->browser_win, TRUE, TRUE, 0);
7739 /* oops message for user feedback */
7740 t->oops = gtk_entry_new();
7741 gtk_entry_set_inner_border(GTK_ENTRY(t->oops), NULL);
7742 gtk_entry_set_has_frame(GTK_ENTRY(t->oops), FALSE);
7743 gtk_widget_set_can_focus(GTK_WIDGET(t->oops), FALSE);
7744 #if GTK_CHECK_VERSION(3, 0, 0)
7745 gtk_widget_set_name(t->oops, XT_CSS_RED);
7746 #else
7747 gdk_color_parse(XT_COLOR_RED, &color);
7748 gtk_widget_modify_base(t->oops, GTK_STATE_NORMAL, &color);
7749 #endif
7750 gtk_box_pack_end(GTK_BOX(t->vbox), t->oops, FALSE, FALSE, 0);
7751 modify_font(GTK_WIDGET(t->oops), oops_font);
7753 /* command entry */
7754 t->cmd = gtk_entry_new();
7755 g_signal_connect(G_OBJECT(t->cmd), "focus-in-event",
7756 G_CALLBACK(entry_focus_cb), t);
7757 gtk_entry_set_inner_border(GTK_ENTRY(t->cmd), NULL);
7758 gtk_entry_set_has_frame(GTK_ENTRY(t->cmd), FALSE);
7759 gtk_box_pack_end(GTK_BOX(t->vbox), t->cmd, FALSE, FALSE, 0);
7760 modify_font(GTK_WIDGET(t->cmd), cmd_font);
7762 /* status bar */
7763 statusbar_create(t);
7765 /* buffer list */
7766 t->buffers = create_buffers(t);
7767 gtk_box_pack_end(GTK_BOX(t->vbox), t->buffers, FALSE, FALSE, 0);
7769 /* xtp meaning is normal by default */
7770 set_normal_tab_meaning(t);
7772 /* set empty favicon */
7773 xt_icon_from_name(t, "text-html");
7775 /* and show it all */
7776 gtk_widget_show_all(b);
7777 gtk_widget_show_all(t->vbox);
7779 if (!fancy_bar) {
7780 gtk_widget_hide(t->backward);
7781 gtk_widget_hide(t->forward);
7782 gtk_widget_hide(t->stop);
7783 gtk_widget_hide(t->js_toggle);
7785 if (!fancy_bar || (search_string == NULL || strlen(search_string) == 0))
7786 gtk_widget_hide(t->search_entry);
7787 if (http_proxy == NULL && http_proxy_save == NULL)
7788 gtk_widget_hide(t->proxy_toggle);
7790 /* compact tab bar */
7791 t->tab_elems.label = gtk_label_new(title);
7792 t->tab_elems.favicon = gtk_image_new();
7794 t->tab_elems.eventbox = gtk_event_box_new();
7795 gtk_widget_set_name(t->tab_elems.eventbox, "compact_tab");
7796 #if GTK_CHECK_VERSION(3, 0, 0)
7797 t->tab_elems.box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7799 gtk_label_set_ellipsize(GTK_LABEL(t->tab_elems.label),
7800 PANGO_ELLIPSIZE_END);
7801 gtk_widget_override_font(t->tab_elems.label, tabbar_font);
7802 gtk_widget_set_halign(t->tab_elems.label, GTK_ALIGN_START);
7803 gtk_widget_set_valign(t->tab_elems.label, GTK_ALIGN_START);
7804 #else
7805 t->tab_elems.box = gtk_hbox_new(FALSE, 0);
7807 gtk_label_set_width_chars(GTK_LABEL(t->tab_elems.label), 1);
7808 gtk_misc_set_alignment(GTK_MISC(t->tab_elems.label), 0.0, 0.0);
7809 gtk_misc_set_padding(GTK_MISC(t->tab_elems.label), 4.0, 4.0);
7810 modify_font(GTK_WIDGET(t->tab_elems.label), tabbar_font);
7812 gdk_color_parse(XT_COLOR_CT_BACKGROUND, &color);
7813 gtk_widget_modify_bg(t->tab_elems.eventbox, GTK_STATE_NORMAL, &color);
7814 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color);
7815 gtk_widget_modify_fg(t->tab_elems.label, GTK_STATE_NORMAL, &color);
7816 #endif
7818 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.favicon, FALSE,
7819 FALSE, 0);
7820 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.label, TRUE,
7821 TRUE, 0);
7822 gtk_container_add(GTK_CONTAINER(t->tab_elems.eventbox),
7823 t->tab_elems.box);
7825 gtk_box_pack_start(GTK_BOX(tab_bar_box), t->tab_elems.eventbox, TRUE,
7826 TRUE, 0);
7827 gtk_widget_show_all(t->tab_elems.eventbox);
7829 if (append_next == 0 || gtk_notebook_get_n_pages(notebook) == 0)
7830 append_tab(t);
7831 else {
7832 id = position >= 0 ? position :
7833 gtk_notebook_get_current_page(notebook) + 1;
7834 if (id > gtk_notebook_get_n_pages(notebook))
7835 append_tab(t);
7836 else {
7837 TAILQ_INSERT_TAIL(&tabs, t, entry);
7838 gtk_notebook_insert_page(notebook, t->vbox, t->tab_content,
7839 id);
7840 gtk_box_reorder_child(GTK_BOX(tab_bar_box),
7841 t->tab_elems.eventbox, id);
7842 recalc_tabs();
7846 #if GTK_CHECK_VERSION(2, 20, 0)
7847 /* turn spinner off if we are a new tab without uri */
7848 if (!load) {
7849 gtk_spinner_stop(GTK_SPINNER(t->spinner));
7850 gtk_widget_hide(t->spinner);
7852 #endif
7853 /* make notebook tabs reorderable */
7854 gtk_notebook_set_tab_reorderable(notebook, t->vbox, TRUE);
7856 /* compact tabs clickable */
7857 g_signal_connect(G_OBJECT(t->tab_elems.eventbox),
7858 "button_press_event", G_CALLBACK(tab_clicked_cb), t);
7860 g_object_connect(G_OBJECT(t->cmd),
7861 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7862 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb), t,
7863 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7864 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb), t,
7865 "signal::activate", G_CALLBACK(cmd_activate_cb), t,
7866 "signal::populate-popup", G_CALLBACK(cmd_popup_cb), t,
7867 "signal::hide", G_CALLBACK(cmd_hide_cb), t,
7868 (char *)NULL);
7870 /* reuse wv_button_cb to hide oops */
7871 g_object_connect(G_OBJECT(t->oops),
7872 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7873 (char *)NULL);
7875 g_signal_connect(t->buffers,
7876 "row-activated", G_CALLBACK(row_activated_cb), t);
7877 g_object_connect(G_OBJECT(t->buffers),
7878 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t, (char *)NULL);
7880 g_object_connect(G_OBJECT(t->wv),
7881 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t,
7882 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb), t,
7883 "signal::download-requested", G_CALLBACK(webview_download_cb), t,
7884 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb), t,
7885 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7886 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7887 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb), t,
7888 "signal::create-web-view", G_CALLBACK(webview_cwv_cb), t,
7889 "signal::close-web-view", G_CALLBACK(webview_closewv_cb), t,
7890 "signal::event", G_CALLBACK(webview_event_cb), t,
7891 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb), t,
7892 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7893 "signal::button_release_event", G_CALLBACK(wv_release_button_cb), t,
7894 "signal::populate-popup", G_CALLBACK(wv_popup_cb), t,
7895 "signal::geolocation-policy-decision-requested", G_CALLBACK(wv_deny_location_cb), t,
7896 (char *)NULL);
7897 g_signal_connect(t->wv,
7898 "notify::load-status", G_CALLBACK(notify_load_status_cb), t);
7899 g_signal_connect(t->wv,
7900 "notify::title", G_CALLBACK(notify_title_cb), t);
7901 t->progress_handle = g_signal_connect(t->wv,
7902 "notify::progress", G_CALLBACK(webview_progress_changed_cb), t);
7904 /* hijack the unused keys as if we were the browser */
7905 //g_object_connect(G_OBJECT(t->toolbar),
7906 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7907 // (char *)NULL);
7909 g_signal_connect(G_OBJECT(bb), "clicked", G_CALLBACK(tab_close_cb), t);
7911 /* setup history */
7912 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7913 /* restore the tab's history */
7914 if (u && u->history) {
7915 items = u->history;
7916 while (items) {
7917 item = items->data;
7918 webkit_web_back_forward_list_add_item(t->bfl, item);
7919 items = g_list_next(items);
7922 item = g_list_nth_data(u->history, u->back);
7923 if (item)
7924 webkit_web_view_go_to_back_forward_item(t->wv, item);
7926 g_list_free(items);
7927 g_list_free(u->history);
7928 } else
7929 webkit_web_back_forward_list_clear(t->bfl);
7931 /* check and show url and statusbar */
7932 url_set_visibility();
7933 statusbar_set_visibility();
7935 if (focus) {
7936 set_current_tab(t->tab_id);
7937 DNPRINTF(XT_D_TAB, "create_new_tab: going to tab: %d\n",
7938 t->tab_id);
7940 if (load) {
7941 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), title);
7942 load_uri(t, title);
7943 } else {
7944 if (show_url == 1)
7945 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
7946 else
7947 focus_webview(t);
7949 } else if (load)
7950 load_uri(t, title);
7952 if (userstyle_global)
7953 apply_style(t);
7955 recolor_compact_tabs();
7956 setzoom_webkit(t, XT_ZOOM_NORMAL);
7957 return (t);
7960 void
7961 notebook_switchpage_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7962 gpointer *udata)
7964 struct tab *t;
7965 const gchar *uri;
7967 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: tab: %d\n", pn);
7969 if (gtk_notebook_get_current_page(notebook) == -1)
7970 recalc_tabs();
7972 TAILQ_FOREACH(t, &tabs, entry) {
7973 if (t->tab_id == pn) {
7974 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: going to "
7975 "%d\n", pn);
7977 uri = get_title(t, TRUE);
7978 gtk_window_set_title(GTK_WINDOW(main_window), uri);
7980 hide_cmd(t);
7981 hide_oops(t);
7982 hide_buffers(t);
7984 if (t->focus_wv) {
7985 /* can't use focus_webview here */
7986 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7988 update_statusbar_tabs(t);
7989 break;
7994 void
7995 notebook_pagereordered_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7996 gpointer *udata)
7998 struct tab *t = NULL, *tt;
8000 recalc_tabs();
8002 TAILQ_FOREACH(tt, &tabs, entry)
8003 if (tt->tab_id == pn) {
8004 t = tt;
8005 break;
8007 if (t == NULL)
8008 return;
8009 DNPRINTF(XT_D_TAB, "page_reordered_cb: tab: %d\n", t->tab_id);
8011 gtk_box_reorder_child(GTK_BOX(tab_bar_box), t->tab_elems.eventbox,
8012 t->tab_id);
8014 update_statusbar_tabs(t);
8017 void
8018 menuitem_response(struct tab *t)
8020 gtk_notebook_set_current_page(notebook, t->tab_id);
8024 destroy_menu(GtkMenuShell *m, void *notused)
8026 gtk_widget_destroy(GTK_WIDGET(m));
8027 g_object_unref(G_OBJECT(m));
8028 return (XT_CB_PASSTHROUGH);
8031 gboolean
8032 arrow_cb(GtkWidget *w, GdkEventButton *event, gpointer user_data)
8034 GtkWidget *menu = NULL, *menu_items;
8035 GdkEventButton *bevent;
8036 struct tab **stabs = NULL;
8037 int i, num_tabs;
8038 const gchar *uri;
8040 if (event->type == GDK_BUTTON_PRESS) {
8041 bevent = (GdkEventButton *) event;
8042 menu = gtk_menu_new();
8043 g_object_ref_sink(G_OBJECT(menu));
8045 num_tabs = sort_tabs_by_page_num(&stabs);
8046 for (i = 0; i < num_tabs; ++i) {
8047 if (stabs[i] == NULL)
8048 continue;
8049 if ((uri = get_uri(stabs[i])) == NULL)
8050 /* XXX make sure there is something to print */
8051 /* XXX add gui pages in here to look purdy */
8052 uri = "(untitled)";
8053 menu_items = gtk_menu_item_new_with_label(uri);
8054 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_items);
8055 gtk_widget_show(menu_items);
8057 g_signal_connect_swapped(menu_items,
8058 "activate", G_CALLBACK(menuitem_response),
8059 (gpointer)stabs[i]);
8061 g_free(stabs);
8063 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
8064 bevent->button, bevent->time);
8066 g_object_connect(G_OBJECT(menu),
8067 "signal::selection-done", G_CALLBACK(destroy_menu), NULL,
8068 (char *)NULL);
8070 return (TRUE /* eat event */);
8073 return (FALSE /* propagate */);
8077 icon_size_map(int iconsz)
8079 if (iconsz <= GTK_ICON_SIZE_INVALID ||
8080 iconsz > GTK_ICON_SIZE_DIALOG)
8081 return (GTK_ICON_SIZE_SMALL_TOOLBAR);
8083 return (iconsz);
8086 GtkWidget *
8087 create_button(const char *name, const char *icon_name, int size)
8089 GtkWidget *button, *image;
8090 int gtk_icon_size;
8091 #if !GTK_CHECK_VERSION(3, 0, 0)
8092 gchar *newstyle;
8093 #endif
8095 #if !GTK_CHECK_VERSION(3, 0, 0)
8096 newstyle = g_strdup_printf(
8097 "style \"%s-style\"\n"
8098 "{\n"
8099 " GtkWidget::focus-padding = 0\n"
8100 " GtkWidget::focus-line-width = 0\n"
8101 " xthickness = 0\n"
8102 " ythickness = 0\n"
8103 "}\n"
8104 "widget \"*.%s\" style \"%s-style\"", name, name, name);
8105 gtk_rc_parse_string(newstyle);
8106 g_free(newstyle);
8107 #endif
8108 button = gtk_button_new();
8109 gtk_widget_set_can_focus(button, FALSE);
8110 gtk_button_set_focus_on_click(GTK_BUTTON(button), FALSE);
8111 gtk_icon_size = icon_size_map(size ? size : icon_size);
8113 image = gtk_image_new_from_icon_name(icon_name, gtk_icon_size);
8114 gtk_container_set_border_width(GTK_CONTAINER(button), 0);
8115 gtk_button_set_image(GTK_BUTTON(button), GTK_WIDGET(image));
8116 gtk_widget_set_name(button, name);
8117 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
8119 return (button);
8122 void
8123 button_set_file(GtkWidget *button, char *filename)
8125 GtkWidget *image;
8126 char file[PATH_MAX];
8128 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, filename);
8129 image = gtk_image_new_from_file(file);
8130 gtk_button_set_image(GTK_BUTTON(button), image);
8133 void
8134 button_set_icon_name(GtkWidget *button, char *icon_name)
8136 GtkWidget *image = NULL;
8138 image = gtk_image_new_from_icon_name(icon_name,
8139 icon_size_map(icon_size));
8140 gtk_button_set_image(GTK_BUTTON(button), image);
8143 void
8144 create_canvas(void)
8146 GtkWidget *vbox;
8147 GList *l = NULL;
8148 GdkPixbuf *pb;
8149 char file[PATH_MAX];
8150 int i;
8151 #if !GTK_CHECK_VERSION(3, 0, 0)
8152 GdkColor color;
8153 #endif
8155 #if GTK_CHECK_VERSION(3, 0, 0)
8156 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
8157 #else
8158 vbox = gtk_vbox_new(FALSE, 0);
8159 #endif
8160 gtk_box_set_spacing(GTK_BOX(vbox), 0);
8161 gtk_widget_set_can_focus(vbox, FALSE);
8162 notebook = GTK_NOTEBOOK(gtk_notebook_new());
8163 #if !GTK_CHECK_VERSION(3, 0, 0)
8164 /* XXX seems to be needed with gtk+2 */
8165 g_object_set(G_OBJECT(notebook), "tab-border", 0, NULL);
8166 #endif
8167 gtk_notebook_set_scrollable(notebook, TRUE);
8168 gtk_notebook_set_show_border(notebook, FALSE);
8169 gtk_widget_set_can_focus(GTK_WIDGET(notebook), FALSE);
8171 abtn = gtk_button_new();
8172 gtk_widget_set_can_focus(abtn, FALSE);
8173 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
8174 gtk_widget_set_name(abtn, "Arrow");
8175 gtk_button_set_image(GTK_BUTTON(abtn), arrow);
8176 gtk_widget_set_size_request(abtn, -1, 20);
8178 #if GTK_CHECK_VERSION(2, 20, 0)
8179 gtk_notebook_set_action_widget(notebook, abtn, GTK_PACK_END);
8180 #endif
8181 /* compact tab bar */
8182 tab_bar = gtk_event_box_new();
8183 #if GTK_CHECK_VERSION(3, 0, 0)
8184 gtk_widget_set_name(tab_bar, "tab_bar");
8185 tab_bar_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
8186 #else
8187 gdk_color_parse(XT_COLOR_CT_SEPARATOR, &color);
8188 gtk_widget_modify_bg(tab_bar, GTK_STATE_NORMAL, &color);
8189 tab_bar_box = gtk_hbox_new(TRUE, 0);
8190 #endif
8191 gtk_container_add(GTK_CONTAINER(tab_bar), tab_bar_box);
8192 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box), TRUE);
8193 gtk_box_set_spacing(GTK_BOX(tab_bar_box), 2);
8195 gtk_box_pack_start(GTK_BOX(vbox), tab_bar, FALSE, FALSE, 0);
8196 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(notebook), TRUE, TRUE, 0);
8198 g_object_connect(G_OBJECT(notebook),
8199 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb), NULL,
8200 (char *)NULL);
8201 g_object_connect(G_OBJECT(notebook),
8202 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb),
8203 NULL, (char *)NULL);
8204 g_signal_connect(G_OBJECT(abtn), "button_press_event",
8205 G_CALLBACK(arrow_cb), NULL);
8207 main_window = create_window("xombrero");
8208 gtk_container_add(GTK_CONTAINER(main_window), vbox);
8209 g_signal_connect(G_OBJECT(main_window), "delete_event",
8210 G_CALLBACK(gtk_main_quit), NULL);
8211 #if GTK_CHECK_VERSION(3, 0, 0)
8212 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window), FALSE);
8213 #endif
8215 /* icons */
8216 for (i = 0; i < LENGTH(icons); i++) {
8217 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, icons[i]);
8218 pb = gdk_pixbuf_new_from_file(file, NULL);
8219 l = g_list_append(l, pb);
8221 gtk_window_set_default_icon_list(l);
8223 for (; l; l = l->next)
8224 g_object_unref(G_OBJECT(l->data));
8226 gtk_widget_show_all(abtn);
8227 gtk_widget_show_all(main_window);
8228 notebook_tab_set_visibility();
8231 #ifndef XT_SOCKET_DISABLE
8233 send_cmd_to_socket(char *cmd)
8235 int s, len, rv = 1;
8236 struct sockaddr_un sa;
8238 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8239 warnx("%s: socket", __func__);
8240 return (rv);
8243 sa.sun_family = AF_UNIX;
8244 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8245 work_dir, XT_SOCKET_FILE);
8246 len = SUN_LEN(&sa);
8248 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8249 warnx("%s: connect", __func__);
8250 goto done;
8253 if (send(s, cmd, strlen(cmd) + 1, 0) == -1) {
8254 warnx("%s: send", __func__);
8255 goto done;
8258 rv = 0;
8259 done:
8260 close(s);
8261 return (rv);
8264 gboolean
8265 socket_watcher(GIOChannel *source, GIOCondition condition, gpointer data)
8267 int s, n;
8268 char str[XT_MAX_URL_LENGTH];
8269 socklen_t t = sizeof(struct sockaddr_un);
8270 struct sockaddr_un sa;
8271 struct passwd *p;
8272 uid_t uid;
8273 gid_t gid;
8274 struct tab *tt;
8275 gint fd = g_io_channel_unix_get_fd(source);
8277 if ((s = accept(fd, (struct sockaddr *)&sa, &t)) == -1) {
8278 warn("accept");
8279 return (FALSE);
8282 if (getpeereid(s, &uid, &gid) == -1) {
8283 warn("getpeereid");
8284 return (FALSE);
8286 if (uid != getuid() || gid != getgid()) {
8287 warnx("unauthorized user");
8288 return (FALSE);
8291 p = getpwuid(uid);
8292 if (p == NULL) {
8293 warnx("not a valid user");
8294 return (FALSE);
8297 n = recv(s, str, sizeof(str), 0);
8298 if (n <= 0)
8299 return (TRUE);
8301 tt = get_current_tab();
8302 cmd_execute(tt, str);
8303 return (TRUE);
8307 is_running(void)
8309 int s, len, rv = 1;
8310 struct sockaddr_un sa;
8312 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8313 warn("is_running: socket");
8314 return (-1);
8317 sa.sun_family = AF_UNIX;
8318 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8319 work_dir, XT_SOCKET_FILE);
8320 len = SUN_LEN(&sa);
8322 /* connect to see if there is a listener */
8323 if (connect(s, (struct sockaddr *)&sa, len) == -1)
8324 rv = 0; /* not running */
8325 else
8326 rv = 1; /* already running */
8328 close(s);
8330 return (rv);
8334 build_socket(void)
8336 int s, len;
8337 struct sockaddr_un sa;
8339 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8340 warn("build_socket: socket");
8341 return (-1);
8344 sa.sun_family = AF_UNIX;
8345 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8346 work_dir, XT_SOCKET_FILE);
8347 len = SUN_LEN(&sa);
8349 /* connect to see if there is a listener */
8350 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8351 /* no listener so we will */
8352 unlink(sa.sun_path);
8354 if (bind(s, (struct sockaddr *)&sa, len) == -1) {
8355 warn("build_socket: bind");
8356 goto done;
8359 if (listen(s, 1) == -1) {
8360 warn("build_socket: listen");
8361 goto done;
8364 return (s);
8367 done:
8368 close(s);
8369 return (-1);
8371 #endif
8373 void
8374 xxx_dir(char *dir)
8376 struct stat sb;
8378 if (stat(dir, &sb)) {
8379 #if defined __MINGW32__
8380 if (mkdir(dir) == -1)
8381 #else
8382 if (mkdir(dir, S_IRWXU) == -1)
8383 #endif
8384 err(1, "mkdir %s", dir);
8385 if (stat(dir, &sb))
8386 err(1, "stat %s", dir);
8388 if (S_ISDIR(sb.st_mode) == 0)
8389 errx(1, "%s not a dir", dir);
8390 #if !defined __MINGW32__
8391 if (((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) != S_IRWXU) {
8392 warnx("fixing invalid permissions on %s", dir);
8393 if (chmod(dir, S_IRWXU) == -1)
8394 err(1, "chmod %s", dir);
8396 #endif
8399 void
8400 usage(void)
8402 fprintf(stderr,
8403 "%s [-nSTVt][-f file][-s session] url ...\n", __progname);
8404 exit(0);
8407 #if GTK_CHECK_VERSION(3, 0, 0)
8408 void
8409 setup_css(void)
8411 GtkCssProvider *provider;
8412 GdkDisplay *display;
8413 GdkScreen *screen;
8414 GFile *file;
8415 char path[PATH_MAX];
8416 #if defined __MINGW32__
8417 GtkCssProvider *windows_hacks;
8418 #endif
8420 provider = gtk_css_provider_new();
8421 display = gdk_display_get_default();
8422 screen = gdk_display_get_default_screen(display);
8423 snprintf(path, sizeof path, "%s" PS "%s", resource_dir, XT_CSS_FILE);
8424 file = g_file_new_for_path(path);
8425 gtk_css_provider_load_from_file(provider, file, NULL);
8426 gtk_style_context_add_provider_for_screen(screen,
8427 GTK_STYLE_PROVIDER(provider),
8428 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8429 g_object_unref(G_OBJECT(provider));
8430 #if defined __MINGW32__
8431 windows_hacks = gtk_css_provider_new();
8432 gtk_css_provider_load_from_data(windows_hacks,
8433 ".notebook tab {\n"
8434 " border-width: 0px;\n"
8435 "}", -1, NULL);
8436 gtk_style_context_add_provider_for_screen(screen,
8437 GTK_STYLE_PROVIDER(windows_hacks),
8438 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8439 g_object_unref(G_OBJECT(windows_hacks));
8440 #endif
8441 g_object_unref(G_OBJECT(file));
8443 #endif
8445 void
8446 welcome(void)
8448 startpage_add("<b>Welcome to xombrero %s!</b><p>", version);
8449 startpage_add("Details at "
8450 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8451 "wiki page</a><p>");
8455 main(int argc, char **argv)
8457 struct stat sb;
8458 int c, optn = 0, opte = 0, focus = 1;
8459 char conf[PATH_MAX] = { '\0' };
8460 char file[PATH_MAX];
8461 char sodversion[32];
8462 char *env_proxy = NULL;
8463 char *path;
8464 FILE *f = NULL;
8465 struct karg a;
8467 start_argv = (char * const *)argv;
8469 if (os_init)
8470 os_init();
8472 /* prepare gtk */
8473 gtk_init(&argc, &argv);
8475 gnutls_global_init();
8477 strlcpy(named_session, XT_SAVED_TABS_FILE, sizeof named_session);
8479 RB_INIT(&hl);
8480 RB_INIT(&downloads);
8481 RB_INIT(&st_tree);
8482 RB_INIT(&svl);
8483 RB_INIT(&ua_list);
8484 RB_INIT(&ha_list);
8485 RB_INIT(&di_list);
8487 TAILQ_INIT(&sessions);
8488 TAILQ_INIT(&tabs);
8489 TAILQ_INIT(&mtl);
8490 TAILQ_INIT(&aliases);
8491 TAILQ_INIT(&undos);
8492 TAILQ_INIT(&kbl);
8493 TAILQ_INIT(&spl);
8494 TAILQ_INIT(&chl);
8495 TAILQ_INIT(&shl);
8496 TAILQ_INIT(&cul);
8497 TAILQ_INIT(&srl);
8498 TAILQ_INIT(&c_wl);
8499 TAILQ_INIT(&js_wl);
8500 TAILQ_INIT(&pl_wl);
8501 TAILQ_INIT(&force_https);
8502 TAILQ_INIT(&svil);
8504 #ifndef XT_RESOURCE_LIMITS_DISABLE
8505 struct rlimit rlp;
8506 char *cmd = NULL;
8507 int s;
8508 GIOChannel *channel;
8510 /* fiddle with ulimits */
8511 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8512 warn("getrlimit");
8513 else {
8514 /* just use them all */
8515 #ifdef __APPLE__
8516 rlp.rlim_cur = OPEN_MAX;
8517 #else
8518 rlp.rlim_cur = rlp.rlim_max;
8519 #endif
8520 if (setrlimit(RLIMIT_NOFILE, &rlp) == -1)
8521 warn("setrlimit");
8522 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8523 warn("getrlimit");
8524 else if (rlp.rlim_cur < 1024)
8525 startpage_add("%s requires at least 1024 "
8526 "(2048 recommended) file " "descriptors, "
8527 "currently it has up to %d available",
8528 __progname, rlp.rlim_cur);
8530 #endif
8532 while ((c = getopt(argc, argv, "STVf:s:tne")) != -1) {
8533 switch (c) {
8534 case 'S':
8535 show_url = 0;
8536 break;
8537 case 'T':
8538 show_tabs = 0;
8539 break;
8540 case 'V':
8541 #ifdef XOMBRERO_BUILDSTR
8542 errx(0 , "Version: %s Build: %s",
8543 version, XOMBRERO_BUILDSTR);
8544 #else
8545 errx(0 , "Version: %s", version);
8546 #endif
8547 break;
8548 case 'f':
8549 strlcpy(conf, optarg, sizeof(conf));
8550 break;
8551 case 's':
8552 strlcpy(named_session, optarg, sizeof(named_session));
8553 break;
8554 case 't':
8555 tabless = 1;
8556 break;
8557 case 'n':
8558 optn = 1;
8559 break;
8560 case 'e':
8561 opte = 1;
8562 break;
8563 default:
8564 usage();
8565 /* NOTREACHED */
8568 argc -= optind;
8569 argv += optind;
8571 init_keybindings();
8573 pwd = getpwuid(getuid());
8574 if (pwd == NULL)
8575 errx(1, "invalid user %d", getuid());
8577 /* set download dir */
8578 if (strlen(download_dir) == 0)
8579 strlcpy(download_dir, pwd->pw_dir, sizeof download_dir);
8581 /* compile buffer command regexes */
8582 buffercmd_init();
8584 /* set default dynamic string settings */
8585 home = g_strdup(XT_DS_HOME);
8586 search_string = g_strdup(XT_DS_SEARCH_STRING);
8587 strlcpy(runtime_settings, "runtime", sizeof runtime_settings);
8588 cmd_font_name = g_strdup(XT_DS_CMD_FONT_NAME);
8589 oops_font_name = g_strdup(XT_DS_OOPS_FONT_NAME);
8590 statusbar_font_name = g_strdup(XT_DS_STATUSBAR_FONT_NAME);
8591 tabbar_font_name = g_strdup(XT_DS_TABBAR_FONT_NAME);
8592 statusbar_elems = g_strdup("BP");
8593 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8594 encoding = g_strdup(XT_DS_ENCODING);
8595 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8596 path = g_strdup_printf("%s" PS "style.css", resource_dir);
8597 userstyle = g_filename_to_uri(path, NULL, NULL);
8598 g_free(path);
8599 stylesheet = g_strdup(userstyle);
8601 /* set statically allocated (struct special) settings */
8602 if (strlen(default_script) == 0)
8603 expand_tilde(default_script, sizeof default_script,
8604 XT_DS_DEFAULT_SCRIPT);
8605 if (strlen(ssl_ca_file) == 0)
8606 expand_tilde(ssl_ca_file, sizeof ssl_ca_file,
8607 XT_DS_SSL_CA_FILE);
8609 /* soup session */
8610 session = webkit_get_default_session();
8612 /* read config file */
8613 if (strlen(conf) == 0)
8614 snprintf(conf, sizeof conf, "%s" PS ".%s",
8615 pwd->pw_dir, XT_CONF_FILE);
8616 config_parse(conf, 0);
8618 /* read preloaded HSTS list */
8619 if (preload_strict_transport) {
8620 snprintf(conf, sizeof conf, "%s" PS "%s",
8621 resource_dir, XT_HSTS_PRELOAD_FILE);
8622 config_parse(conf, 0);
8625 /* check whether to read in a crapton of additional http headers */
8626 if (anonymize_headers) {
8627 snprintf(conf, sizeof conf, "%s" PS "%s",
8628 resource_dir, XT_USER_AGENT_FILE);
8629 config_parse(conf, 0);
8630 snprintf(conf, sizeof conf, "%s" PS "%s",
8631 resource_dir, XT_HTTP_ACCEPT_FILE);
8632 config_parse(conf, 0);
8635 /* init fonts */
8636 cmd_font = pango_font_description_from_string(cmd_font_name);
8637 oops_font = pango_font_description_from_string(oops_font_name);
8638 statusbar_font = pango_font_description_from_string(statusbar_font_name);
8639 tabbar_font = pango_font_description_from_string(tabbar_font_name);
8641 /* working directory */
8642 if (strlen(work_dir) == 0)
8643 snprintf(work_dir, sizeof work_dir, "%s" PS "%s",
8644 pwd->pw_dir, XT_DIR);
8645 xxx_dir(work_dir);
8647 /* icon cache dir */
8648 snprintf(cache_dir, sizeof cache_dir, "%s" PS "%s", work_dir, XT_CACHE_DIR);
8649 xxx_dir(cache_dir);
8651 /* certs dir */
8652 snprintf(certs_dir, sizeof certs_dir, "%s" PS "%s", work_dir, XT_CERT_DIR);
8653 xxx_dir(certs_dir);
8655 /* cert changes dir */
8656 snprintf(certs_cache_dir, sizeof certs_cache_dir, "%s" PS "%s",
8657 work_dir, XT_CERT_CACHE_DIR);
8658 xxx_dir(certs_cache_dir);
8660 /* sessions dir */
8661 snprintf(sessions_dir, sizeof sessions_dir, "%s" PS "%s",
8662 work_dir, XT_SESSIONS_DIR);
8663 xxx_dir(sessions_dir);
8665 /* js dir */
8666 snprintf(js_dir, sizeof js_dir, "%s" PS "%s", work_dir, XT_JS_DIR);
8667 xxx_dir(js_dir);
8669 /* temp dir */
8670 snprintf(temp_dir, sizeof temp_dir, "%s" PS "%s", work_dir, XT_TEMP_DIR);
8671 xxx_dir(temp_dir);
8673 /* runtime settings that can override config file */
8674 if (runtime_settings[0] != '\0')
8675 config_parse(runtime_settings, 1);
8677 /* download dir */
8678 if (!strcmp(download_dir, pwd->pw_dir))
8679 strlcat(download_dir, PS "downloads", sizeof download_dir);
8680 xxx_dir(download_dir);
8682 /* first start file */
8683 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_SOD_FILE);
8684 if (stat(file, &sb)) {
8685 warnx("start of day file doesn't exist, creating it");
8686 if ((f = fopen(file, "w")) == NULL)
8687 err(1, "startofday");
8688 if (fputs(version, f) == EOF)
8689 err(1, "fputs");
8690 fclose(f);
8692 /* welcome user */
8693 welcome();
8694 } else {
8695 if ((f = fopen(file, "r+")) == NULL)
8696 err(1, "startofday");
8697 if (fgets(sodversion, sizeof sodversion, f) == NULL)
8698 err(1, "fgets");
8699 sodversion[strcspn(sodversion, "\n")] = '\0';
8700 if (strcmp(version, sodversion)) {
8701 if ((f = freopen(file, "w", f)) == NULL)
8702 err(1, "startofday");
8703 if (fputs(version, f) == EOF)
8704 err(1, "fputs");
8706 /* upgrade, say something smart */
8707 welcome();
8709 fclose(f);
8712 /* favorites file */
8713 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_FAVS_FILE);
8714 if (stat(file, &sb)) {
8715 warnx("favorites file doesn't exist, creating it");
8716 if ((f = fopen(file, "w")) == NULL)
8717 err(1, "favorites");
8718 fclose(f);
8721 /* quickmarks file */
8722 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
8723 if (stat(file, &sb)) {
8724 warnx("quickmarks file doesn't exist, creating it");
8725 if ((f = fopen(file, "w")) == NULL)
8726 err(1, "quickmarks");
8727 fclose(f);
8730 /* search history */
8731 if (history_autosave) {
8732 snprintf(search_file, sizeof search_file, "%s" PS "%s",
8733 work_dir, XT_SEARCH_FILE);
8734 if (stat(search_file, &sb)) {
8735 warnx("search history file doesn't exist, creating it");
8736 if ((f = fopen(search_file, "w")) == NULL)
8737 err(1, "search_history");
8738 fclose(f);
8740 history_read(&shl, search_file, &search_history_count);
8743 /* command history */
8744 if (history_autosave) {
8745 snprintf(command_file, sizeof command_file, "%s" PS "%s",
8746 work_dir, XT_COMMAND_FILE);
8747 if (stat(command_file, &sb)) {
8748 warnx("command history file doesn't exist, creating it");
8749 if ((f = fopen(command_file, "w")) == NULL)
8750 err(1, "command_history");
8751 fclose(f);
8753 history_read(&chl, command_file, &cmd_history_count);
8756 /* cookies */
8757 setup_cookies();
8759 /* guess_search regex */
8760 if (url_regex == NULL)
8761 url_regex = g_strdup(XT_URL_REGEX);
8762 if (url_regex)
8763 if (regcomp(&url_re, url_regex, REG_EXTENDED | REG_NOSUB))
8764 startpage_add("invalid url regex %s", url_regex);
8766 /* proxy */
8767 env_proxy = getenv("http_proxy");
8768 if (env_proxy)
8769 setup_proxy(env_proxy);
8770 else {
8771 env_proxy = getenv("HTTP_PROXY");
8772 if (env_proxy)
8773 setup_proxy(env_proxy);
8774 else
8775 setup_proxy(http_proxy);
8778 /* the user can optionally have the proxy disabled at startup */
8779 if ((http_proxy_starts_enabled == 0) && (http_proxy != NULL)) {
8780 http_proxy_save = g_strdup(http_proxy);
8781 setup_proxy(NULL);
8784 #ifndef XT_SOCKET_DISABLE
8785 if (opte) {
8786 send_cmd_to_socket(argv[0]);
8787 exit(0);
8789 #else
8790 opte = opte; /* shut mingw up */
8791 #endif
8792 /* set some connection parameters */
8793 g_object_set(session, "max-conns", max_connections, (char *)NULL);
8794 g_object_set(session, "max-conns-per-host", max_host_connections,
8795 (char *)NULL);
8796 g_object_set(session, SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
8797 (char *)NULL);
8799 g_signal_connect(session, "request-queued", G_CALLBACK(session_rq_cb),
8800 NULL);
8802 #ifndef XT_SOCKET_DISABLE
8803 /* see if there is already a xombrero running */
8804 if (single_instance && is_running()) {
8805 optn = 1;
8806 warnx("already running");
8809 if (optn) {
8810 while (argc) {
8811 cmd = g_strdup_printf("%s %s", "tabnew", argv[0]);
8812 send_cmd_to_socket(cmd);
8813 if (cmd)
8814 g_free(cmd);
8816 argc--;
8817 argv++;
8819 exit(0);
8821 #else
8822 optn = optn; /* shut mingw up */
8823 #endif
8824 /* tld list */
8825 tld_tree_init();
8827 /* cache mode */
8828 if (enable_cache)
8829 webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER);
8830 else
8831 webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
8833 if (enable_strict_transport)
8834 strict_transport_init();
8836 /* uri completion */
8837 completion_model = gtk_list_store_new(1, G_TYPE_STRING);
8839 /* buffers */
8840 buffers_store = gtk_list_store_new
8841 (NUM_COLS, G_TYPE_UINT, GDK_TYPE_PIXBUF, G_TYPE_STRING);
8843 qmarks_load();
8845 /* go graphical */
8846 create_canvas();
8847 notebook_tab_set_visibility();
8849 if (save_global_history)
8850 restore_global_history();
8852 /* restore session list */
8853 restore_sessions_list();
8855 if (!strcmp(named_session, XT_SAVED_TABS_FILE))
8856 restore_saved_tabs();
8857 else {
8858 a.s = named_session;
8859 a.i = XT_SES_DONOTHING;
8860 open_tabs(NULL, &a);
8863 /* see if we have an exception */
8864 if (!TAILQ_EMPTY(&spl)) {
8865 create_new_tab("about:startpage", NULL, focus, -1);
8866 focus = 0;
8869 while (argc) {
8870 create_new_tab(argv[0], NULL, focus, -1);
8871 focus = 0;
8873 argc--;
8874 argv++;
8877 if (TAILQ_EMPTY(&tabs))
8878 create_new_tab(home, NULL, 1, -1);
8879 #ifndef XT_SOCKET_DISABLE
8880 if (enable_socket)
8881 if ((s = build_socket()) != -1) {
8882 channel = g_io_channel_unix_new(s);
8883 g_io_add_watch(channel, G_IO_IN, socket_watcher, NULL);
8885 #endif
8887 #if GTK_CHECK_VERSION(3, 0, 0)
8888 setup_css();
8889 #endif
8891 gtk_main();
8893 gnutls_global_deinit();
8895 if (url_regex)
8896 regfree(&url_re);
8898 return (0);