Be sure not to malloc nothing
[xombrero.git] / xombrero.c
blob631016e2c1935e717308c53acaa77ec71bcf8068
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 (0);
1916 * Checks whether the remote cert is identical to the local saved
1917 * cert. Returns CERT_LOCAL if unchanged, CERT_UNTRUSTED if local
1918 * cert does not exist, and CERT_BAD if different.
1920 * Saves entire cert chain in pem encoding to chain for it to be
1921 * cached later, if needed.
1923 enum cert_trust
1924 check_local_certs(const char *file, GTlsCertificate *cert, char **chain)
1926 char r_cert_buf[64 * 1024];
1927 FILE *f;
1928 GTlsCertificate *tmpcert = NULL;
1929 GTlsCertificate *issuer = NULL;
1930 char *pem = NULL;
1931 char *tmp = NULL;
1932 enum cert_trust rv = CERT_LOCAL;
1933 int i;
1935 if ((f = fopen(file, "r")) == NULL) {
1936 /* no local cert to check */
1937 rv = CERT_UNTRUSTED;
1940 for (i = 0;;) {
1941 if (i == 0) {
1942 g_object_get(G_OBJECT(cert), "certificate-pem", &pem,
1943 NULL);
1944 g_object_get(G_OBJECT(cert), "issuer", &issuer, NULL);
1945 } else {
1946 if (issuer == NULL)
1947 break;
1948 g_object_get(G_OBJECT(tmpcert), "issuer", &issuer,
1949 NULL);
1950 if (issuer == NULL)
1951 break;
1953 g_object_get(G_OBJECT(tmpcert), "certificate-pem", &pem,
1954 NULL);
1956 i++;
1958 if (tmpcert != NULL)
1959 g_object_unref(G_OBJECT(tmpcert));
1960 tmpcert = issuer;
1961 if (f) {
1962 if (fread(r_cert_buf, strlen(pem), 1, f) != 1 && !feof(f))
1963 rv = CERT_BAD;
1964 if (bcmp(r_cert_buf, pem, strlen(pem)))
1965 rv = CERT_BAD;
1967 tmp = g_strdup_printf("%s%s", *chain, pem);
1968 g_free(pem);
1969 g_free(*chain);
1970 *chain = tmp;
1973 if (issuer != NULL)
1974 g_object_unref(G_OBJECT(issuer));
1975 if (f != NULL)
1976 fclose(f);
1977 return (rv);
1981 check_cert_changes(struct tab *t, GTlsCertificate *cert, const char *file, const char *uri)
1983 SoupURI *soupuri = NULL;
1984 struct karg args = {0};
1985 struct wl_entry *w = NULL;
1986 char *chain = NULL;
1987 int ret = 0;
1989 chain = g_strdup("");
1990 switch(check_local_certs(file, cert, &chain)) {
1991 case CERT_LOCAL:
1992 /* The cached certificate is identical */
1993 break;
1994 case CERT_TRUSTED: /* FALLTHROUGH */
1995 case CERT_UNTRUSTED:
1996 /* cache new certificate */
1997 args.i = XT_CACHE;
1998 cert_cmd(t, &args);
1999 break;
2000 case CERT_BAD:
2001 if ((soupuri = soup_uri_new(uri)) == NULL ||
2002 soupuri->host == NULL)
2003 break;
2004 if ((w = wl_find(soupuri->host, &svil)) != NULL)
2005 break;
2006 t->xtp_meaning = XT_XTP_TAB_MEANING_SV;
2007 args.s = g_strdup((char *)uri);
2008 xtp_page_sv(t, &args);
2009 ret = 1;
2010 break;
2013 if (soupuri)
2014 soup_uri_free(soupuri);
2015 if (chain)
2016 g_free(chain);
2017 return (ret);
2021 remove_cookie(int index)
2023 int i, rv = 1;
2024 GSList *cf;
2025 SoupCookie *c;
2027 DNPRINTF(XT_D_COOKIE, "remove_cookie: %d\n", index);
2029 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2031 for (i = 1; cf; cf = cf->next, i++) {
2032 if (i != index)
2033 continue;
2034 c = cf->data;
2035 print_cookie("remove cookie", c);
2036 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2037 rv = 0;
2038 break;
2041 soup_cookies_free(cf);
2043 return (rv);
2047 remove_cookie_domain(int domain_id)
2049 int domain_count, rv = 1;
2050 GSList *cf;
2051 SoupCookie *c;
2052 char *last_domain;
2054 DNPRINTF(XT_D_COOKIE, "remove_cookie_domain: %d\n", domain_id);
2056 last_domain = "";
2057 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2059 for (domain_count = 0; cf; cf = cf->next) {
2060 c = cf->data;
2062 if (strcmp(last_domain, c->domain) != 0) {
2063 domain_count++;
2064 last_domain = c->domain;
2067 if (domain_count < domain_id)
2068 continue;
2069 else if (domain_count > domain_id)
2070 break;
2072 print_cookie("remove cookie", c);
2073 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2074 rv = 0;
2077 soup_cookies_free(cf);
2079 return (rv);
2083 remove_cookie_all()
2085 int rv = 1;
2086 GSList *cf;
2087 SoupCookie *c;
2089 DNPRINTF(XT_D_COOKIE, "remove_cookie_all\n");
2091 cf = soup_cookie_jar_all_cookies(s_cookiejar);
2093 for (; cf; cf = cf->next) {
2094 c = cf->data;
2096 print_cookie("remove cookie", c);
2097 soup_cookie_jar_delete_cookie(s_cookiejar, c);
2098 rv = 0;
2101 soup_cookies_free(cf);
2103 return (rv);
2107 toplevel_cmd(struct tab *t, struct karg *args)
2109 js_toggle_cb(t->js_toggle, t);
2111 return (0);
2115 can_go_back_for_real(struct tab *t)
2117 int i;
2118 WebKitWebHistoryItem *item;
2119 const gchar *uri;
2121 if (t == NULL)
2122 return (FALSE);
2124 if (t->item != NULL)
2125 return (TRUE);
2127 /* rely on webkit to make sure we can go backward when on an about page */
2128 uri = get_uri(t);
2129 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2130 g_str_has_prefix(uri, "xxxt://"))
2131 return (webkit_web_view_can_go_back(t->wv));
2133 /* the back/forward list is stupid so help determine if we can go back */
2134 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2135 item != NULL;
2136 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2137 if (strcmp(webkit_web_history_item_get_uri(item), uri))
2138 return (TRUE);
2141 return (FALSE);
2145 can_go_forward_for_real(struct tab *t)
2147 int i;
2148 WebKitWebHistoryItem *item;
2149 const gchar *uri;
2151 if (t == NULL)
2152 return (FALSE);
2154 /* rely on webkit to make sure we can go forward when on an about page */
2155 uri = get_uri(t);
2156 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2157 g_str_has_prefix(uri, "xxxt://"))
2158 return (webkit_web_view_can_go_forward(t->wv));
2160 /* the back/forwars list is stupid so help selecting a different item */
2161 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2162 item != NULL;
2163 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2164 if (strcmp(webkit_web_history_item_get_uri(item), uri))
2165 return (TRUE);
2168 return (FALSE);
2171 void
2172 go_back_for_real(struct tab *t)
2174 int i;
2175 WebKitWebHistoryItem *item;
2176 const gchar *uri;
2178 if (t == NULL)
2179 return;
2181 uri = get_uri(t);
2182 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2183 g_str_has_prefix(uri, "xxxt://")) {
2184 webkit_web_view_go_back(t->wv);
2185 return;
2187 /* the back/forwars list is stupid so help selecting a different item */
2188 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2189 item != NULL;
2190 i--, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2191 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2192 webkit_web_view_go_to_back_forward_item(t->wv, item);
2193 break;
2198 void
2199 go_forward_for_real(struct tab *t)
2201 int i;
2202 WebKitWebHistoryItem *item;
2203 const gchar *uri;
2205 if (t == NULL)
2206 return;
2208 uri = get_uri(t);
2209 if (uri == NULL || g_str_has_prefix(uri, "about:") ||
2210 g_str_has_prefix(uri, "xxxt://")) {
2211 webkit_web_view_go_forward(t->wv);
2212 return;
2214 /* the back/forwars list is stupid so help selecting a different item */
2215 for (i = 0, item = webkit_web_back_forward_list_get_current_item(t->bfl);
2216 item != NULL;
2217 i++, item = webkit_web_back_forward_list_get_nth_item(t->bfl, i)) {
2218 if (strcmp(webkit_web_history_item_get_uri(item), uri)) {
2219 webkit_web_view_go_to_back_forward_item(t->wv, item);
2220 break;
2226 navaction(struct tab *t, struct karg *args)
2228 WebKitWebHistoryItem *item = NULL;
2229 WebKitWebFrame *frame;
2231 DNPRINTF(XT_D_NAV, "navaction: tab %d opcode %d\n",
2232 t->tab_id, args->i);
2234 hide_oops(t);
2235 set_normal_tab_meaning(t);
2236 if (t->item) {
2237 if (args->i == XT_NAV_BACK)
2238 item = webkit_web_back_forward_list_get_current_item(t->bfl);
2239 else
2240 item = webkit_web_back_forward_list_get_forward_item(t->bfl);
2243 switch (args->i) {
2244 case XT_NAV_BACK:
2245 if (t->item) {
2246 if (item == NULL)
2247 return (XT_CB_PASSTHROUGH);
2248 webkit_web_view_go_to_back_forward_item(t->wv, item);
2249 t->item = NULL;
2250 return (XT_CB_PASSTHROUGH);
2252 marks_clear(t);
2253 go_back_for_real(t);
2254 break;
2255 case XT_NAV_FORWARD:
2256 if (t->item) {
2257 if (item == NULL)
2258 return (XT_CB_PASSTHROUGH);
2259 webkit_web_view_go_to_back_forward_item(t->wv, item);
2260 t->item = NULL;
2261 return (XT_CB_PASSTHROUGH);
2263 marks_clear(t);
2264 go_forward_for_real(t);
2265 break;
2266 case XT_NAV_RELOAD:
2267 frame = webkit_web_view_get_main_frame(t->wv);
2268 webkit_web_frame_reload(frame);
2269 break;
2270 case XT_NAV_STOP:
2271 frame = webkit_web_view_get_main_frame(t->wv);
2272 webkit_web_frame_stop_loading(frame);
2273 break;
2275 return (XT_CB_PASSTHROUGH);
2279 move(struct tab *t, struct karg *args)
2281 GtkAdjustment *adjust;
2282 double pi, si, pos, ps, upper, lower, max;
2283 double percent;
2285 switch (args->i) {
2286 case XT_MOVE_DOWN:
2287 case XT_MOVE_UP:
2288 case XT_MOVE_BOTTOM:
2289 case XT_MOVE_TOP:
2290 case XT_MOVE_PAGEDOWN:
2291 case XT_MOVE_PAGEUP:
2292 case XT_MOVE_HALFDOWN:
2293 case XT_MOVE_HALFUP:
2294 case XT_MOVE_PERCENT:
2295 case XT_MOVE_CENTER:
2296 adjust = t->adjust_v;
2297 break;
2298 default:
2299 adjust = t->adjust_h;
2300 break;
2303 pos = gtk_adjustment_get_value(adjust);
2304 ps = gtk_adjustment_get_page_size(adjust);
2305 upper = gtk_adjustment_get_upper(adjust);
2306 lower = gtk_adjustment_get_lower(adjust);
2307 si = gtk_adjustment_get_step_increment(adjust);
2308 pi = gtk_adjustment_get_page_increment(adjust);
2309 max = upper - ps;
2311 DNPRINTF(XT_D_MOVE, "move: opcode %d %s pos %f ps %f upper %f lower %f "
2312 "max %f si %f pi %f\n",
2313 args->i, adjust == t->adjust_h ? "horizontal" : "vertical",
2314 pos, ps, upper, lower, max, si, pi);
2316 switch (args->i) {
2317 case XT_MOVE_DOWN:
2318 case XT_MOVE_RIGHT:
2319 pos += si;
2320 gtk_adjustment_set_value(adjust, MIN(pos, max));
2321 break;
2322 case XT_MOVE_UP:
2323 case XT_MOVE_LEFT:
2324 pos -= si;
2325 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2326 break;
2327 case XT_MOVE_BOTTOM:
2328 case XT_MOVE_FARRIGHT:
2329 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2330 gtk_adjustment_set_value(adjust, max);
2331 break;
2332 case XT_MOVE_TOP:
2333 case XT_MOVE_FARLEFT:
2334 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2335 gtk_adjustment_set_value(adjust, lower);
2336 break;
2337 case XT_MOVE_PAGEDOWN:
2338 pos += pi;
2339 gtk_adjustment_set_value(adjust, MIN(pos, max));
2340 break;
2341 case XT_MOVE_PAGEUP:
2342 pos -= pi;
2343 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2344 break;
2345 case XT_MOVE_HALFDOWN:
2346 pos += pi / 2;
2347 gtk_adjustment_set_value(adjust, MIN(pos, max));
2348 break;
2349 case XT_MOVE_HALFUP:
2350 pos -= pi / 2;
2351 gtk_adjustment_set_value(adjust, MAX(pos, lower));
2352 break;
2353 case XT_MOVE_CENTER:
2354 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2355 args->s = g_strdup("50.0");
2356 /* FALLTHROUGH */
2357 case XT_MOVE_PERCENT:
2358 t->mark[marktoindex('\'')] = gtk_adjustment_get_value(t->adjust_v);
2359 percent = atoi(args->s) / 100.0;
2360 pos = max * percent;
2361 if (pos < 0.0 || pos > max)
2362 break;
2363 gtk_adjustment_set_value(adjust, pos);
2364 break;
2365 default:
2366 return (XT_CB_PASSTHROUGH);
2369 DNPRINTF(XT_D_MOVE, "move: new pos %f %f\n", pos, MIN(pos, max));
2371 return (XT_CB_HANDLED);
2374 void
2375 url_set_visibility(void)
2377 struct tab *t;
2379 TAILQ_FOREACH(t, &tabs, entry)
2380 if (show_url == 0) {
2381 gtk_widget_hide(t->toolbar);
2382 focus_webview(t);
2383 } else
2384 gtk_widget_show(t->toolbar);
2387 void
2388 notebook_tab_set_visibility(void)
2390 if (show_tabs == 0) {
2391 gtk_widget_hide(tab_bar);
2392 gtk_notebook_set_show_tabs(notebook, FALSE);
2393 } else {
2394 if (tab_style == XT_TABS_NORMAL) {
2395 gtk_widget_hide(tab_bar);
2396 gtk_notebook_set_show_tabs(notebook, TRUE);
2397 } else if (tab_style == XT_TABS_COMPACT) {
2398 gtk_widget_show(tab_bar);
2399 gtk_notebook_set_show_tabs(notebook, FALSE);
2404 void
2405 statusbar_set_visibility(void)
2407 struct tab *t;
2409 TAILQ_FOREACH(t, &tabs, entry){
2410 if (show_statusbar == 0)
2411 gtk_widget_hide(t->statusbar);
2412 else
2413 gtk_widget_show(t->statusbar);
2415 focus_webview(t);
2419 void
2420 url_set(struct tab *t, int enable_url_entry)
2422 GdkPixbuf *pixbuf;
2423 int progress;
2425 show_url = enable_url_entry;
2427 if (enable_url_entry) {
2428 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
2429 GTK_ENTRY_ICON_PRIMARY, NULL);
2430 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri), 0);
2431 } else {
2432 pixbuf = gtk_entry_get_icon_pixbuf(GTK_ENTRY(t->uri_entry),
2433 GTK_ENTRY_ICON_PRIMARY);
2434 progress =
2435 gtk_entry_get_progress_fraction(GTK_ENTRY(t->uri_entry));
2436 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
2437 GTK_ENTRY_ICON_PRIMARY, pixbuf);
2438 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
2439 progress);
2444 fullscreen(struct tab *t, struct karg *args)
2446 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2448 if (t == NULL)
2449 return (XT_CB_PASSTHROUGH);
2451 if (show_url == 0) {
2452 url_set(t, 1);
2453 show_tabs = 1;
2454 } else {
2455 url_set(t, 0);
2456 show_tabs = 0;
2459 url_set_visibility();
2460 notebook_tab_set_visibility();
2462 return (XT_CB_HANDLED);
2466 statustoggle(struct tab *t, struct karg *args)
2468 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2470 if (show_statusbar == 1) {
2471 show_statusbar = 0;
2472 statusbar_set_visibility();
2473 } else if (show_statusbar == 0) {
2474 show_statusbar = 1;
2475 statusbar_set_visibility();
2477 return (XT_CB_HANDLED);
2481 urlaction(struct tab *t, struct karg *args)
2483 int rv = XT_CB_HANDLED;
2485 DNPRINTF(XT_D_TAB, "%s: %p %d\n", __func__, t, args->i);
2487 if (t == NULL)
2488 return (XT_CB_PASSTHROUGH);
2490 switch (args->i) {
2491 case XT_URL_SHOW:
2492 if (show_url == 0) {
2493 url_set(t, 1);
2494 url_set_visibility();
2496 break;
2497 case XT_URL_HIDE:
2498 if (show_url == 1) {
2499 url_set(t, 0);
2500 url_set_visibility();
2502 break;
2504 return (rv);
2508 tabaction(struct tab *t, struct karg *args)
2510 int rv = XT_CB_HANDLED;
2511 char *url = args->s;
2512 struct undo *u;
2513 struct tab *tt, *tv;
2515 DNPRINTF(XT_D_TAB, "tabaction: %p %d\n", t, args->i);
2517 if (t == NULL)
2518 return (XT_CB_PASSTHROUGH);
2520 switch (args->i) {
2521 case XT_TAB_NEW:
2522 if (strlen(url) > 0)
2523 create_new_tab(url, NULL, 1, args->precount);
2524 else
2525 create_new_tab(NULL, NULL, 1, args->precount);
2526 break;
2527 case XT_TAB_DELETE:
2528 if (args->precount < 0)
2529 delete_tab(t);
2530 else
2531 TAILQ_FOREACH(tt, &tabs, entry)
2532 if (tt->tab_id == args->precount - 1) {
2533 delete_tab(tt);
2534 break;
2536 break;
2537 case XT_TAB_DELQUIT:
2538 if (gtk_notebook_get_n_pages(notebook) > 1)
2539 delete_tab(t);
2540 else
2541 quit(t, args);
2542 break;
2543 case XT_TAB_ONLY:
2544 TAILQ_FOREACH_SAFE(tt, &tabs, entry, tv)
2545 if (t != tt)
2546 delete_tab(tt);
2547 break;
2548 case XT_TAB_OPEN:
2549 if (strlen(url) > 0)
2551 else {
2552 rv = XT_CB_PASSTHROUGH;
2553 goto done;
2555 load_uri(t, url);
2556 break;
2557 case XT_TAB_SHOW:
2558 if (show_tabs == 0) {
2559 show_tabs = 1;
2560 notebook_tab_set_visibility();
2562 break;
2563 case XT_TAB_HIDE:
2564 if (show_tabs == 1) {
2565 show_tabs = 0;
2566 notebook_tab_set_visibility();
2568 break;
2569 case XT_TAB_NEXTSTYLE:
2570 if (tab_style == XT_TABS_NORMAL) {
2571 tab_style = XT_TABS_COMPACT;
2572 recolor_compact_tabs();
2574 else
2575 tab_style = XT_TABS_NORMAL;
2576 notebook_tab_set_visibility();
2577 break;
2578 case XT_TAB_UNDO_CLOSE:
2579 if (undo_count == 0) {
2580 DNPRINTF(XT_D_TAB, "%s: no tabs to undo close",
2581 __func__);
2582 goto done;
2583 } else {
2584 undo_count--;
2585 u = TAILQ_FIRST(&undos);
2586 create_new_tab(u->uri, u, 1, -1);
2588 TAILQ_REMOVE(&undos, u, entry);
2589 g_free(u->uri);
2590 /* u->history is freed in create_new_tab() */
2591 g_free(u);
2593 break;
2594 case XT_TAB_LOAD_IMAGES:
2596 if (!auto_load_images) {
2598 /* Enable auto-load images (this will load all
2599 * previously unloaded images). */
2600 g_object_set(G_OBJECT(t->settings),
2601 "auto-load-images", TRUE, (char *)NULL);
2602 webkit_web_view_set_settings(t->wv, t->settings);
2604 webkit_web_view_reload(t->wv);
2606 /* Webkit triggers an event when we change the setting,
2607 * so we can't disable the auto-loading at once.
2609 * Unfortunately, webkit does not tell us when it's done.
2610 * Instead, we wait until the next request, and then
2611 * disable autoloading again.
2613 t->load_images = TRUE;
2615 break;
2616 default:
2617 rv = XT_CB_PASSTHROUGH;
2618 goto done;
2621 done:
2622 if (args->s) {
2623 g_free(args->s);
2624 args->s = NULL;
2627 return (rv);
2631 resizetab(struct tab *t, struct karg *args)
2633 if (t == NULL || args == NULL) {
2634 show_oops(NULL, "resizetab invalid parameters");
2635 return (XT_CB_PASSTHROUGH);
2638 DNPRINTF(XT_D_TAB, "resizetab: tab %d %d\n",
2639 t->tab_id, args->i);
2641 setzoom_webkit(t, args->i);
2643 return (XT_CB_HANDLED);
2647 movetab(struct tab *t, struct karg *args)
2649 int n, dest;
2651 if (t == NULL || args == NULL) {
2652 show_oops(NULL, "movetab invalid parameters");
2653 return (XT_CB_PASSTHROUGH);
2656 DNPRINTF(XT_D_TAB, "movetab: tab %d opcode %d\n",
2657 t->tab_id, args->i);
2659 if (args->i >= XT_TAB_INVALID)
2660 return (XT_CB_PASSTHROUGH);
2662 if (TAILQ_EMPTY(&tabs))
2663 return (XT_CB_PASSTHROUGH);
2665 n = gtk_notebook_get_n_pages(notebook);
2666 dest = gtk_notebook_get_current_page(notebook);
2668 switch (args->i) {
2669 case XT_TAB_NEXT:
2670 if (args->precount < 0)
2671 dest = dest == n - 1 ? 0 : dest + 1;
2672 else
2673 dest = args->precount - 1;
2675 break;
2676 case XT_TAB_PREV:
2677 if (args->precount < 0)
2678 dest -= 1;
2679 else
2680 dest -= args->precount % n;
2682 if (dest < 0)
2683 dest += n;
2685 break;
2686 case XT_TAB_FIRST:
2687 dest = 0;
2688 break;
2689 case XT_TAB_LAST:
2690 dest = n - 1;
2691 break;
2692 default:
2693 return (XT_CB_PASSTHROUGH);
2696 if (dest < 0 || dest >= n)
2697 return (XT_CB_PASSTHROUGH);
2698 if (t->tab_id == dest) {
2699 DNPRINTF(XT_D_TAB, "movetab: do nothing\n");
2700 return (XT_CB_HANDLED);
2703 set_current_tab(dest);
2705 return (XT_CB_HANDLED);
2708 int cmd_prefix = 0;
2710 struct prompt_sub {
2711 const char *s;
2712 const char *(*f)(struct tab *);
2713 } subs[] = {
2714 { "<uri>", get_uri },
2718 command(struct tab *t, struct karg *args)
2720 struct karg a = {0};
2721 int i, cmd_setup = 0;
2722 char *s = NULL, *sp = NULL, *sl = NULL;
2723 gchar **sv;
2725 if (t == NULL || args == NULL) {
2726 show_oops(NULL, "command invalid parameters");
2727 return (XT_CB_PASSTHROUGH);
2730 switch (args->i) {
2731 case '/':
2732 s = "/";
2733 break;
2734 case '?':
2735 s = "?";
2736 break;
2737 case ':':
2738 if (cmd_prefix == 0) {
2739 if (args->s != NULL && strlen(args->s) != 0) {
2740 sp = g_strdup_printf(":%s", args->s);
2741 s = sp;
2742 } else
2743 s = ":";
2744 } else {
2745 sp = g_strdup_printf(":%d", cmd_prefix);
2746 s = sp;
2747 cmd_prefix = 0;
2749 sl = g_strdup(s);
2750 if (sp) {
2751 g_free(sp);
2752 sp = NULL;
2754 s = sl;
2755 for (i = 0; i < LENGTH(subs); ++i) {
2756 sv = g_strsplit(sl, subs[i].s, -1);
2757 if (sl)
2758 g_free(sl);
2759 sl = g_strjoinv(subs[i].f(t), sv);
2760 g_strfreev(sv);
2761 s = sl;
2763 break;
2764 case '.':
2765 t->mode = XT_MODE_HINT;
2766 a.i = 0;
2767 s = ".";
2769 * js code will auto fire() if a single link is visible,
2770 * causing the focus-out-event cb function to be called. Setup
2771 * the cmd _before_ triggering hinting code so the cmd can get
2772 * killed by the cb in this case.
2774 show_cmd(t, s);
2775 cmd_setup = 1;
2776 hint(t, &a);
2777 break;
2778 case ',':
2779 t->mode = XT_MODE_HINT;
2780 a.i = XT_HINT_NEWTAB;
2781 s = ",";
2782 show_cmd(t, s);
2783 cmd_setup = 1;
2784 hint(t, &a);
2785 break;
2786 default:
2787 show_oops(t, "command: invalid opcode %d", args->i);
2788 return (XT_CB_PASSTHROUGH);
2791 DNPRINTF(XT_D_CMD, "%s: tab %d type %s\n", __func__, t->tab_id, s);
2793 if (!cmd_setup)
2794 show_cmd(t, s);
2796 if (sp)
2797 g_free(sp);
2798 if (sl)
2799 g_free(sl);
2801 return (XT_CB_HANDLED);
2805 search(struct tab *t, struct karg *args)
2807 gboolean d;
2809 if (t == NULL || args == NULL) {
2810 show_oops(NULL, "search invalid parameters");
2811 return (1);
2814 switch (args->i) {
2815 case XT_SEARCH_NEXT:
2816 d = t->search_forward;
2817 break;
2818 case XT_SEARCH_PREV:
2819 d = !t->search_forward;
2820 break;
2821 default:
2822 return (XT_CB_PASSTHROUGH);
2825 if (t->search_text == NULL) {
2826 if (global_search == NULL)
2827 return (XT_CB_PASSTHROUGH);
2828 else {
2829 d = t->search_forward = TRUE;
2830 t->search_text = g_strdup(global_search);
2831 webkit_web_view_mark_text_matches(t->wv, global_search, FALSE, 0);
2832 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
2836 DNPRINTF(XT_D_CMD, "search: tab %d opc %d forw %d text %s\n",
2837 t->tab_id, args->i, t->search_forward, t->search_text);
2839 webkit_web_view_search_text(t->wv, t->search_text, FALSE, d, TRUE);
2841 return (XT_CB_HANDLED);
2845 session_save(struct tab *t, char *filename)
2847 struct karg a;
2848 int rv = 1;
2849 struct session *s;
2851 if (strlen(filename) == 0)
2852 goto done;
2854 if (filename[0] == '.' || filename[0] == '/')
2855 goto done;
2857 a.s = filename;
2858 if (save_tabs(t, &a))
2859 goto done;
2860 strlcpy(named_session, filename, sizeof named_session);
2862 /* add the new session to the list of sessions */
2863 s = g_malloc(sizeof(struct session));
2864 s->name = g_strdup(filename);
2865 TAILQ_INSERT_TAIL(&sessions, s, entry);
2867 rv = 0;
2868 done:
2869 return (rv);
2873 session_open(struct tab *t, char *filename)
2875 struct karg a;
2876 int rv = 1;
2878 if (strlen(filename) == 0)
2879 goto done;
2881 if (filename[0] == '.' || filename[0] == '/')
2882 goto done;
2884 a.s = filename;
2885 a.i = XT_SES_CLOSETABS;
2886 if (open_tabs(t, &a))
2887 goto done;
2889 strlcpy(named_session, filename, sizeof named_session);
2891 rv = 0;
2892 done:
2893 return (rv);
2897 session_delete(struct tab *t, char *filename)
2899 char file[PATH_MAX];
2900 int rv = 1;
2901 struct session *s;
2903 if (strlen(filename) == 0)
2904 goto done;
2906 if (filename[0] == '.' || filename[0] == '/')
2907 goto done;
2909 snprintf(file, sizeof file, "%s" PS "%s", sessions_dir, filename);
2910 if (unlink(file))
2911 goto done;
2913 if (!strcmp(filename, named_session))
2914 strlcpy(named_session, XT_SAVED_TABS_FILE,
2915 sizeof named_session);
2917 /* remove session from sessions list */
2918 TAILQ_FOREACH(s, &sessions, entry) {
2919 if (!strcmp(s->name, filename))
2920 break;
2922 if (s == NULL)
2923 goto done;
2924 TAILQ_REMOVE(&sessions, s, entry);
2925 g_free((gpointer) s->name);
2926 g_free(s);
2928 rv = 0;
2929 done:
2930 return (rv);
2934 session_cmd(struct tab *t, struct karg *args)
2936 char *filename = args->s;
2938 if (t == NULL)
2939 return (1);
2941 if (args->i & XT_SHOW)
2942 show_oops(t, "Current session: %s", named_session[0] == '\0' ?
2943 XT_SAVED_TABS_FILE : named_session);
2944 else if (args->i & XT_SAVE) {
2945 if (session_save(t, filename)) {
2946 show_oops(t, "Can't save session: %s",
2947 filename ? filename : "INVALID");
2948 goto done;
2950 } else if (args->i & XT_OPEN) {
2951 if (session_open(t, filename)) {
2952 show_oops(t, "Can't open session: %s",
2953 filename ? filename : "INVALID");
2954 goto done;
2956 } else if (args->i & XT_DELETE) {
2957 if (session_delete(t, filename)) {
2958 show_oops(t, "Can't delete session: %s",
2959 filename ? filename : "INVALID");
2960 goto done;
2963 done:
2964 return (XT_CB_PASSTHROUGH);
2968 script_cmd(struct tab *t, struct karg *args)
2970 struct stat sb;
2971 FILE *f = NULL;
2972 char *buf = NULL;
2974 if (t == NULL)
2975 goto done;
2977 if ((f = fopen(args->s, "r")) == NULL) {
2978 show_oops(t, "Can't open script file: %s", args->s);
2979 goto done;
2982 if (fstat(fileno(f), &sb) == -1) {
2983 show_oops(t, "Can't stat script file: %s", args->s);
2984 goto done;
2987 buf = g_malloc0(sb.st_size + 1);
2988 if (fread(buf, 1, sb.st_size, f) != sb.st_size) {
2989 show_oops(t, "Can't read script file: %s", args->s);
2990 goto done;
2993 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
2994 run_script(t, buf);
2996 done:
2997 if (f)
2998 fclose(f);
2999 if (buf)
3000 g_free(buf);
3002 return (XT_CB_PASSTHROUGH);
3006 * Make a hardcopy of the page
3009 print_page(struct tab *t, struct karg *args)
3011 WebKitWebFrame *frame;
3012 GtkPageSetup *ps;
3013 GtkPrintOperation *op;
3014 GtkPrintOperationAction action;
3015 GtkPrintOperationResult print_res;
3016 GError *g_err = NULL;
3017 int marg_l, marg_r, marg_t, marg_b;
3018 int ret = 0;
3020 DNPRINTF(XT_D_PRINTING, "%s:", __func__);
3022 ps = gtk_page_setup_new();
3023 op = gtk_print_operation_new();
3024 action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG;
3025 frame = webkit_web_view_get_main_frame(t->wv);
3027 /* the default margins are too small, so we will bump them */
3028 marg_l = gtk_page_setup_get_left_margin(ps, GTK_UNIT_MM) +
3029 XT_PRINT_EXTRA_MARGIN;
3030 marg_r = gtk_page_setup_get_right_margin(ps, GTK_UNIT_MM) +
3031 XT_PRINT_EXTRA_MARGIN;
3032 marg_t = gtk_page_setup_get_top_margin(ps, GTK_UNIT_MM) +
3033 XT_PRINT_EXTRA_MARGIN;
3034 marg_b = gtk_page_setup_get_bottom_margin(ps, GTK_UNIT_MM) +
3035 XT_PRINT_EXTRA_MARGIN;
3037 /* set margins */
3038 gtk_page_setup_set_left_margin(ps, marg_l, GTK_UNIT_MM);
3039 gtk_page_setup_set_right_margin(ps, marg_r, GTK_UNIT_MM);
3040 gtk_page_setup_set_top_margin(ps, marg_t, GTK_UNIT_MM);
3041 gtk_page_setup_set_bottom_margin(ps, marg_b, GTK_UNIT_MM);
3043 gtk_print_operation_set_default_page_setup(op, ps);
3045 print_res = webkit_web_frame_print_full(frame, op, action, &g_err);
3047 /* check it worked */
3048 if (print_res == GTK_PRINT_OPERATION_RESULT_ERROR) {
3049 show_oops(NULL, "can't print: %s", g_err->message);
3050 g_error_free (g_err);
3051 ret = 1;
3054 g_object_unref(G_OBJECT(ps));
3055 g_object_unref(G_OBJECT(op));
3056 return (ret);
3060 go_home(struct tab *t, struct karg *args)
3062 load_uri(t, home);
3063 return (0);
3067 set_encoding(struct tab *t, struct karg *args)
3069 const gchar *e;
3071 if (args->s && strlen(g_strstrip(args->s)) == 0) {
3072 e = webkit_web_view_get_custom_encoding(t->wv);
3073 if (e == NULL)
3074 e = webkit_web_view_get_encoding(t->wv);
3075 show_oops(t, "encoding: %s", e ? e : "N/A");
3076 } else
3077 webkit_web_view_set_custom_encoding(t->wv, args->s);
3079 return (0);
3083 restart(struct tab *t, struct karg *args)
3085 struct karg a;
3087 a.s = XT_RESTART_TABS_FILE;
3088 save_tabs(t, &a);
3089 execvp(start_argv[0], start_argv);
3090 /* NOTREACHED */
3092 return (0);
3095 char *http_proxy_save; /* not a setting, used to toggle */
3098 proxy_cmd(struct tab *t, struct karg *args)
3100 struct tab *tt;
3102 DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
3104 if (t == NULL)
3105 return (1);
3107 /* setup */
3108 if (http_proxy) {
3109 TAILQ_FOREACH(tt, &tabs, entry)
3110 gtk_widget_show(t->proxy_toggle);
3111 if (http_proxy_save)
3112 g_free(http_proxy_save);
3113 http_proxy_save = g_strdup(http_proxy);
3116 if (args->i & XT_PRXY_SHOW) {
3117 if (http_proxy)
3118 show_oops(t, "http_proxy = %s", http_proxy);
3119 else
3120 show_oops(t, "proxy is currently disabled");
3121 } else if (args->i & XT_PRXY_TOGGLE) {
3122 if (http_proxy_save == NULL && http_proxy == NULL) {
3123 show_oops(t, "can't toggle proxy");
3124 goto done;
3126 TAILQ_FOREACH(tt, &tabs, entry)
3127 gtk_widget_show(t->proxy_toggle);
3128 if (http_proxy) {
3129 if (setup_proxy(NULL) == 0)
3130 button_set_file(t->proxy_toggle,
3131 "tordisabled.ico");
3132 show_oops(t, "http proxy disabled");
3133 } else {
3134 if (setup_proxy(http_proxy_save) == 0 && http_proxy) {
3135 button_set_file(t->proxy_toggle,
3136 "torenabled.ico");
3137 show_oops(t, "http_proxy = %s", http_proxy);
3138 } else
3139 show_oops(t, "invalid proxy: %s", http_proxy_save);
3142 done:
3143 return (XT_CB_PASSTHROUGH);
3147 * If you can read this functionthen you are a sick and twisted individual.
3148 * I hope we never meet, it'll be violent.
3150 gboolean
3151 eval_cb(const GMatchInfo *info, GString *res, gpointer data)
3153 gchar *match, *num;
3154 gint start = -1, end = -1, i;
3155 struct karg *args = data;
3158 * match contains the string UP TO the match.
3160 * res is what is returned, note that whatever remains in the sent in
3161 * string is appended on the way out.
3163 * for example /123/456/789/moo came in
3164 * match contains /123/456/789/
3165 * we assign that to res and replace /789/ with the replacement text
3166 * then g_regex_replace_eval on the way out has /123/456/replacement/moo
3169 match = g_match_info_fetch(info, 0);
3170 if (match == NULL)
3171 goto done;
3173 if (g_match_info_fetch_pos(info, 1, &start, &end) == FALSE)
3174 goto freeit;
3176 g_string_assign(res, match);
3178 i = atoi(&match[start + 1]);
3179 if (args->i == XT_URL_PLUS)
3180 i++;
3181 else
3182 i--;
3184 /* preserve whitespace when likely */
3185 num = g_strdup_printf("%0*d", end - start - 2, i);
3186 g_string_overwrite_len(res, start + 1, num, end - start - 2);
3187 g_free(num);
3189 freeit:
3190 g_free(match);
3191 done:
3192 return (FALSE); /* doesn't matter */
3196 urlmod_cmd(struct tab *t, struct karg *args)
3198 const gchar *uri;
3199 GRegex *reg;
3200 gchar *res;
3202 if (t == NULL)
3203 goto done;
3204 if ((uri = gtk_entry_get_text(GTK_ENTRY(t->uri_entry))) == NULL)
3205 goto done;
3206 if (strlen(uri) == 0)
3207 goto done;
3209 reg = g_regex_new(".*(/[0-9]+/)", 0, 0, NULL);
3210 if (reg == NULL)
3211 goto done;
3212 res = g_regex_replace_eval(reg, uri, -1, 0, 0, eval_cb, args, NULL);
3213 if (res == NULL)
3214 goto free_reg;
3216 if (!strcmp(res, uri))
3217 goto free_res;
3219 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), res);
3220 activate_uri_entry_cb(t->uri_entry, t);
3222 free_res:
3223 g_free(res);
3224 free_reg:
3225 g_regex_unref(reg);
3226 done:
3227 return (XT_CB_PASSTHROUGH);
3230 struct cmd {
3231 char *cmd;
3232 int level;
3233 int (*func)(struct tab *, struct karg *);
3234 int arg;
3235 int type;
3236 } cmds[] = {
3237 { "command_mode", 0, command_mode, XT_MODE_COMMAND, 0 },
3238 { "insert_mode", 0, command_mode, XT_MODE_INSERT, 0 },
3239 { "command", 0, command, ':', 0 },
3240 { "search", 0, command, '/', 0 },
3241 { "searchb", 0, command, '?', 0 },
3242 { "hinting", 0, command, '.', 0 },
3243 { "hinting_newtab", 0, command, ',', 0 },
3244 { "togglesrc", 0, toggle_src, 0, 0 },
3245 { "editsrc", 0, edit_src, 0, 0 },
3246 { "editelement", 0, edit_element, 0, 0 },
3247 { "passthrough", 0, passthrough, 0, 0 },
3248 { "modurl", 0, modurl, 0, 0 },
3250 /* yanking and pasting */
3251 { "yankuri", 0, yank_uri, 0, 0 },
3252 { "pasteuricur", 0, paste_uri, XT_PASTE_CURRENT_TAB, 0 },
3253 { "pasteurinew", 0, paste_uri, XT_PASTE_NEW_TAB, 0 },
3255 /* search */
3256 { "searchnext", 0, search, XT_SEARCH_NEXT, 0 },
3257 { "searchprevious", 0, search, XT_SEARCH_PREV, 0 },
3259 /* focus */
3260 { "focusaddress", 0, focus, XT_FOCUS_URI, 0 },
3261 { "focussearch", 0, focus, XT_FOCUS_SEARCH, 0 },
3263 /* hinting */
3264 { "hinting", 0, hint, 0, 0 },
3265 { "hinting_newtab", 0, hint, XT_HINT_NEWTAB, 0 },
3267 /* custom stylesheet */
3268 { "userstyle", 0, userstyle_cmd, XT_STYLE_CURRENT_TAB, XT_USERARG },
3269 { "userstyle_global", 0, userstyle_cmd, XT_STYLE_GLOBAL, XT_USERARG },
3271 /* navigation */
3272 { "goback", 0, navaction, XT_NAV_BACK, 0 },
3273 { "goforward", 0, navaction, XT_NAV_FORWARD, 0 },
3274 { "reload", 0, navaction, XT_NAV_RELOAD, 0 },
3275 { "stop", 0, navaction, XT_NAV_STOP, 0 },
3277 /* vertical movement */
3278 { "scrolldown", 0, move, XT_MOVE_DOWN, 0 },
3279 { "scrollup", 0, move, XT_MOVE_UP, 0 },
3280 { "scrollbottom", 0, move, XT_MOVE_BOTTOM, 0 },
3281 { "scrolltop", 0, move, XT_MOVE_TOP, 0 },
3282 { "1", 0, move, XT_MOVE_TOP, 0 },
3283 { "scrollhalfdown", 0, move, XT_MOVE_HALFDOWN, 0 },
3284 { "scrollhalfup", 0, move, XT_MOVE_HALFUP, 0 },
3285 { "scrollpagedown", 0, move, XT_MOVE_PAGEDOWN, 0 },
3286 { "scrollpageup", 0, move, XT_MOVE_PAGEUP, 0 },
3287 /* horizontal movement */
3288 { "scrollright", 0, move, XT_MOVE_RIGHT, 0 },
3289 { "scrollleft", 0, move, XT_MOVE_LEFT, 0 },
3290 { "scrollfarright", 0, move, XT_MOVE_FARRIGHT, 0 },
3291 { "scrollfarleft", 0, move, XT_MOVE_FARLEFT, 0 },
3293 { "favorites", 0, xtp_page_fl, XT_SHOW, 0 },
3294 { "fav", 0, xtp_page_fl, XT_SHOW, 0 },
3295 { "favedit", 0, xtp_page_fl, XT_SHOW|XT_DELETE, 0 },
3296 { "favadd", 0, add_favorite, 0, XT_USERARG },
3298 { "qall", 0, quit, 0, 0 },
3299 { "quitall", 0, quit, 0, 0 },
3300 { "w", 0, save_tabs, 0, 0 },
3301 { "wq", 0, save_tabs_and_quit, 0, 0 },
3302 { "help", 0, help, 0, 0 },
3303 { "about", 0, xtp_page_ab, 0, 0 },
3304 { "stats", 0, stats, 0, 0 },
3305 { "version", 0, xtp_page_ab, 0, 0 },
3307 /* js command */
3308 { "js", 0, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3309 { "save", 1, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3310 { "domain", 2, js_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3311 { "fqdn", 2, js_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3312 { "show", 1, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3313 { "all", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3314 { "persistent", 2, js_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3315 { "session", 2, js_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3316 { "toggle", 1, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3317 { "domain", 2, js_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3318 { "fqdn", 2, js_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3320 /* cookie command */
3321 { "cookie", 0, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3322 { "save", 1, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3323 { "domain", 2, cookie_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3324 { "fqdn", 2, cookie_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3325 { "show", 1, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3326 { "all", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3327 { "persistent", 2, cookie_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3328 { "session", 2, cookie_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3329 { "toggle", 1, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3330 { "domain", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3331 { "fqdn", 2, cookie_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3332 { "purge", 1, cookie_cmd, XT_DELETE, 0 },
3334 /* plugin command */
3335 { "plugin", 0, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3336 { "save", 1, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3337 { "domain", 2, pl_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3338 { "fqdn", 2, pl_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3339 { "show", 1, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3340 { "all", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3341 { "persistent", 2, pl_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3342 { "session", 2, pl_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3343 { "toggle", 1, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3344 { "domain", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3345 { "fqdn", 2, pl_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3347 /* https command */
3348 { "https", 0, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3349 { "save", 1, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3350 { "domain", 2, https_cmd, XT_SAVE | XT_WL_TOPLEVEL, 0 },
3351 { "fqdn", 2, https_cmd, XT_SAVE | XT_WL_FQDN, 0 },
3352 { "show", 1, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3353 { "all", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT | XT_WL_SESSION, 0 },
3354 { "persistent", 2, https_cmd, XT_SHOW | XT_WL_PERSISTENT, 0 },
3355 { "session", 2, https_cmd, XT_SHOW | XT_WL_SESSION, 0 },
3356 { "toggle", 1, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3357 { "domain", 2, https_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL, 0 },
3358 { "fqdn", 2, https_cmd, XT_WL_TOGGLE | XT_WL_FQDN, 0 },
3360 /* toplevel (domain) command */
3361 { "toplevel", 0, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3362 { "toggle", 1, toplevel_cmd, XT_WL_TOGGLE | XT_WL_TOPLEVEL | XT_WL_RELOAD, 0 },
3364 /* cookie jar */
3365 { "cookiejar", 0, xtp_page_cl, 0, 0 },
3367 /* cert command */
3368 { "cert", 0, cert_cmd, XT_SHOW, 0 },
3369 { "save", 1, cert_cmd, XT_SAVE, 0 },
3370 { "show", 1, cert_cmd, XT_SHOW, 0 },
3372 { "ca", 0, ca_cmd, 0, 0 },
3373 { "downloadmgr", 0, xtp_page_dl, 0, 0 },
3374 { "dl", 0, xtp_page_dl, 0, 0 },
3375 { "h", 0, xtp_page_hl, 0, 0 },
3376 { "history", 0, xtp_page_hl, 0, 0 },
3377 { "home", 0, go_home, 0, 0 },
3378 { "restart", 0, restart, 0, 0 },
3379 { "urlhide", 0, urlaction, XT_URL_HIDE, 0 },
3380 { "urlshow", 0, urlaction, XT_URL_SHOW, 0 },
3381 { "statustoggle", 0, statustoggle, 0, 0 },
3382 { "run_script", 0, run_page_script, 0, XT_USERARG },
3384 { "print", 0, print_page, 0, 0 },
3386 /* tabs */
3387 { "focusin", 0, resizetab, XT_ZOOM_IN, 0 },
3388 { "focusout", 0, resizetab, XT_ZOOM_OUT, 0 },
3389 { "focusreset", 0, resizetab, XT_ZOOM_NORMAL, 0 },
3390 { "q", 0, tabaction, XT_TAB_DELQUIT, 0 },
3391 { "quit", 0, tabaction, XT_TAB_DELQUIT, 0 },
3392 { "open", 0, tabaction, XT_TAB_OPEN, XT_URLARG },
3393 { "tabclose", 0, tabaction, XT_TAB_DELETE, XT_PREFIX | XT_INTARG},
3394 { "tabedit", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3395 { "tabfirst", 0, movetab, XT_TAB_FIRST, 0 },
3396 { "tabhide", 0, tabaction, XT_TAB_HIDE, 0 },
3397 { "tablast", 0, movetab, XT_TAB_LAST, 0 },
3398 { "tabnew", 0, tabaction, XT_TAB_NEW, XT_PREFIX | XT_URLARG },
3399 { "tabnext", 0, movetab, XT_TAB_NEXT, XT_PREFIX | XT_INTARG},
3400 { "tabnextstyle", 0, tabaction, XT_TAB_NEXTSTYLE, 0 },
3401 { "tabonly", 0, tabaction, XT_TAB_ONLY, 0 },
3402 { "tabprevious", 0, movetab, XT_TAB_PREV, XT_PREFIX | XT_INTARG},
3403 { "tabrewind", 0, movetab, XT_TAB_FIRST, 0 },
3404 { "tabshow", 0, tabaction, XT_TAB_SHOW, 0 },
3405 { "tabs", 0, buffers, 0, 0 },
3406 { "tabundoclose", 0, tabaction, XT_TAB_UNDO_CLOSE, 0 },
3407 { "buffers", 0, buffers, 0, 0 },
3408 { "ls", 0, buffers, 0, 0 },
3409 { "encoding", 0, set_encoding, 0, XT_USERARG },
3410 { "loadimages", 0, tabaction, XT_TAB_LOAD_IMAGES, 0 },
3412 /* settings */
3413 { "set", 0, set, 0, XT_SETARG },
3414 { "runtime", 0, xtp_page_rt, 0, 0 },
3416 { "fullscreen", 0, fullscreen, 0, 0 },
3417 { "f", 0, fullscreen, 0, 0 },
3419 /* sessions */
3420 { "session", 0, session_cmd, XT_SHOW, 0 },
3421 { "delete", 1, session_cmd, XT_DELETE, XT_SESSARG },
3422 { "open", 1, session_cmd, XT_OPEN, XT_SESSARG },
3423 { "save", 1, session_cmd, XT_SAVE, XT_USERARG },
3424 { "show", 1, session_cmd, XT_SHOW, 0 },
3426 /* external javascript */
3427 { "script", 0, script_cmd, XT_EJS_SHOW, XT_USERARG },
3429 /* inspector */
3430 { "inspector", 0, inspector_cmd, XT_INS_SHOW, 0 },
3431 { "show", 1, inspector_cmd, XT_INS_SHOW, 0 },
3432 { "hide", 1, inspector_cmd, XT_INS_HIDE, 0 },
3434 /* proxy */
3435 { "proxy", 0, proxy_cmd, XT_PRXY_SHOW, 0 },
3436 { "show", 1, proxy_cmd, XT_PRXY_SHOW, 0 },
3437 { "toggle", 1, proxy_cmd, XT_PRXY_TOGGLE, 0 },
3439 /* url mod */
3440 { "urlmod", 0, urlmod_cmd, XT_URL, 0 },
3441 { "plus", 1, urlmod_cmd, XT_URL_PLUS, 0 },
3442 { "min", 1, urlmod_cmd, XT_URL_MIN, 0 },
3445 struct {
3446 int index;
3447 int len;
3448 gchar *list[256];
3449 } cmd_status = {-1, 0};
3451 gboolean
3452 wv_release_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3455 if (e->type == GDK_BUTTON_RELEASE && e->button == 1)
3456 btn_down = 0;
3458 return (FALSE);
3461 gboolean
3462 wv_button_cb(GtkWidget *btn, GdkEventButton *e, struct tab *t)
3464 struct karg a;
3465 WebKitHitTestResult *hit_test_result;
3466 guint context;
3468 hit_test_result = webkit_web_view_get_hit_test_result(t->wv, e);
3469 g_object_get(hit_test_result, "context", &context, NULL);
3470 g_object_unref(G_OBJECT(hit_test_result));
3472 hide_oops(t);
3473 hide_buffers(t);
3475 if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE)
3476 t->mode = XT_MODE_INSERT;
3477 else
3478 t->mode = XT_MODE_COMMAND;
3480 if (e->type == GDK_BUTTON_PRESS && e->button == 1)
3481 btn_down = 1;
3482 else if (e->type == GDK_BUTTON_PRESS && e->button == 8 /* btn 4 */) {
3483 /* go backward */
3484 a.i = XT_NAV_BACK;
3485 navaction(t, &a);
3487 return (TRUE);
3488 } else if (e->type == GDK_BUTTON_PRESS && e->button == 9 /* btn 5 */) {
3489 /* go forward */
3490 a.i = XT_NAV_FORWARD;
3491 navaction(t, &a);
3493 return (TRUE);
3496 return (FALSE);
3499 void
3500 tab_close_cb(GtkWidget *btn, struct tab *t)
3502 DNPRINTF(XT_D_TAB, "tab_close_cb: tab %d\n", t->tab_id);
3504 delete_tab(t);
3508 parse_custom_uri(struct tab *t, const char *uri)
3510 struct custom_uri *u;
3511 int handled = 0;
3512 char *sv[3];
3514 TAILQ_FOREACH(u, &cul, entry) {
3515 if (strncmp(uri, u->uri, strlen(u->uri)))
3516 continue;
3518 handled = 1;
3519 sv[0] = u->cmd;
3520 sv[1] = (char *)uri;
3521 sv[2] = NULL;
3522 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL,
3523 NULL, NULL, NULL))
3524 show_oops(t, "%s: could not spawn process", __func__);
3527 return (handled);
3530 void
3531 activate_uri_entry_cb(GtkWidget* entry, struct tab *t)
3533 const gchar *uri = gtk_entry_get_text(GTK_ENTRY(entry));
3535 DNPRINTF(XT_D_URL, "activate_uri_entry_cb: %s\n", uri);
3537 if (t == NULL) {
3538 show_oops(NULL, "activate_uri_entry_cb invalid parameters");
3539 return;
3542 if (uri == NULL) {
3543 show_oops(t, "activate_uri_entry_cb no uri");
3544 return;
3547 uri += strspn(uri, "\t ");
3549 if (parse_custom_uri(t, uri))
3550 return;
3552 /* otherwise continue to load page normally */
3553 load_uri(t, (gchar *)uri);
3554 focus_webview(t);
3557 void
3558 activate_search_entry_cb(GtkWidget* entry, struct tab *t)
3560 const gchar *search = gtk_entry_get_text(GTK_ENTRY(entry));
3561 char *newuri = NULL;
3562 gchar *enc_search;
3563 char **sv;
3565 DNPRINTF(XT_D_URL, "activate_search_entry_cb: %s\n", search);
3567 if (t == NULL) {
3568 show_oops(NULL, "activate_search_entry_cb invalid parameters");
3569 return;
3572 if (search_string == NULL || strlen(search_string) == 0) {
3573 show_oops(t, "no search_string");
3574 return;
3577 set_normal_tab_meaning(t);
3579 enc_search = soup_uri_encode(search, XT_RESERVED_CHARS);
3580 sv = g_strsplit(search_string, "%s", 2);
3581 newuri = g_strjoinv(enc_search, sv);
3582 g_free(enc_search);
3583 g_strfreev(sv);
3585 marks_clear(t);
3586 load_uri(t, newuri);
3587 focus_webview(t);
3589 if (newuri)
3590 g_free(newuri);
3593 void
3594 check_and_set_cookie(const gchar *uri, struct tab *t)
3596 struct wl_entry *w = NULL;
3597 int es = 0;
3599 if (uri == NULL || t == NULL)
3600 return;
3602 if ((w = wl_find_uri(uri, &c_wl)) == NULL)
3603 es = 0;
3604 else
3605 es = 1;
3607 DNPRINTF(XT_D_COOKIE, "check_and_set_cookie: %s %s\n",
3608 es ? "enable" : "disable", uri);
3610 g_object_set(G_OBJECT(t->settings),
3611 "enable-html5-local-storage", es, (char *)NULL);
3612 webkit_web_view_set_settings(t->wv, t->settings);
3615 void
3616 check_and_set_js(const gchar *uri, struct tab *t)
3618 struct wl_entry *w = NULL;
3619 int es = 0;
3621 if (uri == NULL || t == NULL)
3622 return;
3624 if ((w = wl_find_uri(uri, &js_wl)) == NULL)
3625 es = 0;
3626 else
3627 es = 1;
3629 DNPRINTF(XT_D_JS, "check_and_set_js: %s %s\n",
3630 es ? "enable" : "disable", uri);
3632 g_object_set(G_OBJECT(t->settings),
3633 "enable-scripts", es, (char *)NULL);
3634 webkit_web_view_set_settings(t->wv, t->settings);
3636 button_set_stockid(t->js_toggle,
3637 es ? GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE);
3640 void
3641 check_and_set_pl(const gchar *uri, struct tab *t)
3643 struct wl_entry *w = NULL;
3644 int es = 0;
3646 if (uri == NULL || t == NULL)
3647 return;
3649 if ((w = wl_find_uri(uri, &pl_wl)) == NULL)
3650 es = 0;
3651 else
3652 es = 1;
3654 DNPRINTF(XT_D_JS, "check_and_set_pl: %s %s\n",
3655 es ? "enable" : "disable", uri);
3657 g_object_set(G_OBJECT(t->settings),
3658 "enable-plugins", es, (char *)NULL);
3659 webkit_web_view_set_settings(t->wv, t->settings);
3662 #if GTK_CHECK_VERSION(3, 0, 0)
3663 /* A lot of this can be removed when gtk2 is dropped on the floor */
3664 char *
3665 get_css_name(const char *col_str)
3667 char *name = NULL;
3669 if (!strcmp(col_str, XT_COLOR_WHITE))
3670 name = g_strdup(XT_CSS_NORMAL);
3671 else if (!strcmp(col_str, XT_COLOR_RED))
3672 name = g_strdup(XT_CSS_RED);
3673 else if (!strcmp(col_str, XT_COLOR_YELLOW))
3674 name = g_strdup(XT_CSS_YELLOW);
3675 else if (!strcmp(col_str, XT_COLOR_GREEN))
3676 name = g_strdup(XT_CSS_GREEN);
3677 else if (!strcmp(col_str, XT_COLOR_BLUE))
3678 name = g_strdup(XT_CSS_BLUE);
3679 return (name);
3681 #endif
3683 void
3684 show_ca_status(struct tab *t, const char *uri)
3686 char domain[8182], file[PATH_MAX];
3687 SoupMessage *msg = NULL;
3688 GTlsCertificate *cert = NULL;
3689 GTlsCertificateFlags flags = 0;
3690 gchar *col_str = XT_COLOR_RED;
3691 char *cut_uri;
3692 char *chain;
3693 char *s;
3694 #if GTK_CHECK_VERSION(3, 0, 0)
3695 char *name;
3696 #else
3697 GdkColor color;
3698 gchar *text, *base;
3699 #endif
3700 enum cert_trust trust;
3701 int nocolor = 0;
3702 int i;
3704 DNPRINTF(XT_D_URL, "show_ca_status: %d %s %s\n",
3705 ssl_strict_certs, ssl_ca_file, uri);
3707 if (t == NULL)
3708 return;
3710 if (uri == NULL || g_str_has_prefix(uri, "http://") ||
3711 !g_str_has_prefix(uri, "https://"))
3712 return;
3715 * Cut the uri to get the certs off the homepage. We can't use the
3716 * full URI here since it may include arguments and we don't want to make
3717 * these requests multiple times.
3719 cut_uri = g_strdup(uri);
3720 s = cut_uri;
3721 for (i = 0; i < 3; ++i)
3722 s = strchr(&(s[1]), '/');
3723 s[1] = '\0';
3725 msg = soup_message_new("HEAD", cut_uri);
3726 g_free(cut_uri);
3727 if (msg == NULL)
3728 return;
3729 soup_message_set_flags(msg, SOUP_MESSAGE_NO_REDIRECT);
3730 soup_session_send_message(session, msg);
3731 if (msg->status_code == SOUP_STATUS_SSL_FAILED ||
3732 msg->status_code == SOUP_STATUS_TLS_FAILED) {
3733 DNPRINTF(XT_D_URL, "%s: status not ok: %d\n", uri,
3734 msg->status_code);
3735 goto done;
3737 if (!soup_message_get_https_status(msg, &cert, &flags)) {
3738 DNPRINTF(XT_D_URL, "%s: invalid response\n", uri);
3739 goto done;
3741 if (!G_IS_TLS_CERTIFICATE(cert)) {
3742 DNPRINTF(XT_D_URL, "%s: no cert\n", uri);
3743 goto done;
3746 if (flags == 0)
3747 col_str = XT_COLOR_GREEN;
3748 else
3749 col_str = XT_COLOR_YELLOW;
3751 strlcpy(domain, uri + strlen("https://"), sizeof domain);
3752 for (i = 0; i < strlen(domain); i++)
3753 if (domain[i] == '/') {
3754 domain[i] = '\0';
3755 break;
3758 snprintf(file, sizeof file, "%s" PS "%s", certs_cache_dir, domain);
3759 if (warn_cert_changes) {
3760 if (check_cert_changes(t, cert, file, uri))
3761 nocolor = 1;
3764 snprintf(file, sizeof file, "%s" PS "%s", certs_dir, domain);
3765 chain = g_strdup("");
3766 if ((trust = check_local_certs(file, cert, &chain)) == CERT_LOCAL)
3767 col_str = XT_COLOR_BLUE;
3768 if (t->pem)
3769 g_free(t->pem);
3770 t->pem = chain;
3772 done:
3773 g_object_unref(msg);
3774 if (!strcmp(col_str, XT_COLOR_WHITE) || nocolor) {
3775 #if GTK_CHECK_VERSION(3, 0, 0)
3776 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
3777 statusbar_modify_attr(t, XT_CSS_NORMAL);
3778 #else
3779 text = gdk_color_to_string(
3780 &t->default_style->text[GTK_STATE_NORMAL]);
3781 base = gdk_color_to_string(
3782 &t->default_style->base[GTK_STATE_NORMAL]);
3783 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
3784 &t->default_style->base[GTK_STATE_NORMAL]);
3785 statusbar_modify_attr(t, text, base);
3786 g_free(text);
3787 g_free(base);
3788 #endif
3789 } else {
3790 #if GTK_CHECK_VERSION(3, 0, 0)
3791 name = get_css_name(col_str);
3792 gtk_widget_set_name(t->uri_entry, name);
3793 statusbar_modify_attr(t, name);
3794 g_free(name);
3795 #else
3796 gdk_color_parse(col_str, &color);
3797 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL, &color);
3798 statusbar_modify_attr(t, XT_COLOR_BLACK, col_str);
3799 #endif
3803 void
3804 free_favicon(struct tab *t)
3806 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p req %p\n",
3807 __func__, t->icon_download, t->icon_request);
3809 if (t->icon_request)
3810 g_object_unref(t->icon_request);
3811 if (t->icon_dest_uri)
3812 g_free(t->icon_dest_uri);
3814 t->icon_request = NULL;
3815 t->icon_dest_uri = NULL;
3818 void
3819 xt_icon_from_name(struct tab *t, gchar *name)
3821 if (!enable_favicon_entry)
3822 return;
3824 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->uri_entry),
3825 GTK_ENTRY_ICON_PRIMARY, "text-html");
3826 if (show_url == 0)
3827 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3828 GTK_ENTRY_ICON_PRIMARY, "text-html");
3829 else
3830 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3831 GTK_ENTRY_ICON_PRIMARY, NULL);
3834 void
3835 xt_icon_from_pixbuf(struct tab *t, GdkPixbuf *pb)
3837 GdkPixbuf *pb_scaled;
3839 if (gdk_pixbuf_get_width(pb) > 16 || gdk_pixbuf_get_height(pb) > 16)
3840 pb_scaled = gdk_pixbuf_scale_simple(pb, 16, 16,
3841 GDK_INTERP_BILINEAR);
3842 else
3843 pb_scaled = pb;
3845 if (enable_favicon_entry) {
3847 /* Classic tabs. */
3848 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->uri_entry),
3849 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3851 /* Minimal tabs. */
3852 if (show_url == 0) {
3853 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t->sbe.uri),
3854 GTK_ENTRY_ICON_PRIMARY, pb_scaled);
3855 } else
3856 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t->sbe.uri),
3857 GTK_ENTRY_ICON_PRIMARY, NULL);
3860 /* XXX: Only supports the minimal tabs atm. */
3861 if (enable_favicon_tabs)
3862 gtk_image_set_from_pixbuf(GTK_IMAGE(t->tab_elems.favicon),
3863 pb_scaled);
3865 if (pb_scaled != pb)
3866 g_object_unref(pb_scaled);
3869 void
3870 xt_icon_from_file(struct tab *t, char *uri)
3872 GdkPixbuf *pb;
3873 char *file;
3875 if (g_str_has_prefix(uri, "file://"))
3876 file = g_filename_from_uri(uri, NULL, NULL);
3877 else
3878 file = g_strdup(uri);
3880 if (file == NULL)
3881 return;
3883 pb = gdk_pixbuf_new_from_file(file, NULL);
3884 if (pb) {
3885 xt_icon_from_pixbuf(t, pb);
3886 g_object_unref(pb);
3887 } else
3888 xt_icon_from_name(t, "text-html");
3890 g_free(file);
3893 gboolean
3894 is_valid_icon(char *file)
3896 gboolean valid = 0;
3897 const char *mime_type;
3898 GFileInfo *fi;
3899 GFile *gf;
3901 gf = g_file_new_for_path(file);
3902 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
3903 NULL, NULL);
3904 mime_type = g_file_info_get_content_type(fi);
3905 valid = g_strcmp0(mime_type, "image/x-ico") == 0 ||
3906 g_strcmp0(mime_type, "image/vnd.microsoft.icon") == 0 ||
3907 g_strcmp0(mime_type, "image/png") == 0 ||
3908 g_strcmp0(mime_type, "image/gif") == 0 ||
3909 g_strcmp0(mime_type, "application/octet-stream") == 0;
3910 g_object_unref(fi);
3911 g_object_unref(gf);
3913 return (valid);
3916 void
3917 set_favicon_from_file(struct tab *t, char *uri)
3919 struct stat sb;
3920 char *file;
3922 if (t == NULL || uri == NULL)
3923 return;
3925 if (g_str_has_prefix(uri, "file://"))
3926 file = g_filename_from_uri(uri, NULL, NULL);
3927 else
3928 file = g_strdup(uri);
3930 if (file == NULL)
3931 return;
3933 DNPRINTF(XT_D_DOWNLOAD, "%s: loading %s\n", __func__, file);
3935 if (!stat(file, &sb)) {
3936 if (sb.st_size == 0 || !is_valid_icon(file)) {
3937 /* corrupt icon so trash it */
3938 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
3939 __func__, file);
3940 unlink(file);
3941 /* no need to set icon to default here */
3942 goto done;
3945 xt_icon_from_file(t, file);
3946 done:
3947 g_free(file);
3950 void
3951 favicon_download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
3952 WebKitWebView *wv)
3954 WebKitDownloadStatus status = webkit_download_get_status(download);
3955 struct tab *tt = NULL, *t = NULL;
3958 * find the webview instead of passing in the tab as it could have been
3959 * deleted from underneath us.
3961 TAILQ_FOREACH(tt, &tabs, entry) {
3962 if (tt->wv == wv) {
3963 t = tt;
3964 break;
3967 if (t == NULL)
3968 return;
3970 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d status %d\n",
3971 __func__, t->tab_id, status);
3973 switch (status) {
3974 case WEBKIT_DOWNLOAD_STATUS_ERROR:
3975 /* -1 */
3976 t->icon_download = NULL;
3977 free_favicon(t);
3978 break;
3979 case WEBKIT_DOWNLOAD_STATUS_CREATED:
3980 /* 0 */
3981 break;
3982 case WEBKIT_DOWNLOAD_STATUS_STARTED:
3983 /* 1 */
3984 break;
3985 case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
3986 /* 2 */
3987 DNPRINTF(XT_D_DOWNLOAD, "%s: freeing favicon %d\n",
3988 __func__, t->tab_id);
3989 t->icon_download = NULL;
3990 free_favicon(t);
3991 break;
3992 case WEBKIT_DOWNLOAD_STATUS_FINISHED:
3993 /* 3 */
3995 DNPRINTF(XT_D_DOWNLOAD, "%s: setting icon to %s\n",
3996 __func__, t->icon_dest_uri);
3997 set_favicon_from_file(t, t->icon_dest_uri);
3998 /* these will be freed post callback */
3999 t->icon_request = NULL;
4000 t->icon_download = NULL;
4001 break;
4002 default:
4003 break;
4007 void
4008 abort_favicon_download(struct tab *t)
4010 DNPRINTF(XT_D_DOWNLOAD, "%s: down %p\n", __func__, t->icon_download);
4012 #if !WEBKIT_CHECK_VERSION(1, 4, 0)
4013 if (t->icon_download) {
4014 g_signal_handlers_disconnect_by_func(G_OBJECT(t->icon_download),
4015 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
4016 webkit_download_cancel(t->icon_download);
4017 t->icon_download = NULL;
4018 } else
4019 free_favicon(t);
4020 #endif
4022 xt_icon_from_name(t, "text-html");
4025 void
4026 notify_icon_loaded_cb(WebKitWebView *wv, gchar *uri, struct tab *t)
4028 DNPRINTF(XT_D_DOWNLOAD, "%s %s\n", __func__, uri);
4030 if (uri == NULL || t == NULL)
4031 return;
4033 #if WEBKIT_CHECK_VERSION(1, 4, 0)
4034 /* take icon from WebKitIconDatabase */
4035 GdkPixbuf *pb = NULL;
4037 /* webkit_web_view_get_icon_pixbuf is depreciated in 1.8 */
4038 #if WEBKIT_CHECK_VERSION(1, 8, 0)
4040 * If the page was not loaded (for example, via ssl_strict_certs), do
4041 * not attempt to get the webview's pixbuf. This prevents a CRITICAL
4042 * glib warning.
4044 if (wv && webkit_web_view_get_uri(wv))
4045 pb = webkit_web_view_try_get_favicon_pixbuf(wv, 0, 0);
4046 #else
4047 if (wv && webkit_web_view_get_uri(wv))
4048 pb = webkit_web_view_get_icon_pixbuf(wv);
4049 #endif
4050 if (pb) {
4051 xt_icon_from_pixbuf(t, pb);
4052 g_object_unref(pb);
4053 } else
4054 xt_icon_from_name(t, "text-html");
4055 #elif WEBKIT_CHECK_VERSION(1, 1, 18)
4056 /* download icon to cache dir */
4057 gchar *name_hash, file[PATH_MAX];
4058 struct stat sb;
4060 if (t->icon_request) {
4061 DNPRINTF(XT_D_DOWNLOAD, "%s: download in progress\n", __func__);
4062 return;
4065 /* check to see if we got the icon in cache */
4066 name_hash = g_compute_checksum_for_string(G_CHECKSUM_SHA256, uri, -1);
4067 snprintf(file, sizeof file, "%s" PS "%s.ico", cache_dir, name_hash);
4068 g_free(name_hash);
4070 if (!stat(file, &sb)) {
4071 if (sb.st_size > 0) {
4072 DNPRINTF(XT_D_DOWNLOAD, "%s: loading from cache %s\n",
4073 __func__, file);
4074 set_favicon_from_file(t, file);
4075 return;
4078 /* corrupt icon so trash it */
4079 DNPRINTF(XT_D_DOWNLOAD, "%s: corrupt icon %s\n",
4080 __func__, file);
4081 unlink(file);
4084 /* create download for icon */
4085 t->icon_request = webkit_network_request_new(uri);
4086 if (t->icon_request == NULL) {
4087 DNPRINTF(XT_D_DOWNLOAD, "%s: invalid uri %s\n",
4088 __func__, uri);
4089 return;
4092 t->icon_download = webkit_download_new(t->icon_request);
4093 if (t->icon_download == NULL)
4094 return;
4096 /* we have to free icon_dest_uri later */
4097 if ((t->icon_dest_uri = g_filename_to_uri(file, NULL, NULL)) == NULL)
4098 return;
4099 webkit_download_set_destination_uri(t->icon_download,
4100 t->icon_dest_uri);
4102 if (webkit_download_get_status(t->icon_download) ==
4103 WEBKIT_DOWNLOAD_STATUS_ERROR) {
4104 g_object_unref(t->icon_request);
4105 g_free(t->icon_dest_uri);
4106 t->icon_request = NULL;
4107 t->icon_dest_uri = NULL;
4108 return;
4111 g_signal_connect(G_OBJECT(t->icon_download), "notify::status",
4112 G_CALLBACK(favicon_download_status_changed_cb), t->wv);
4114 webkit_download_start(t->icon_download);
4115 #endif
4118 void
4119 notify_load_status_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4121 const gchar *uri = NULL;
4122 struct history *h, find;
4123 struct karg a;
4124 gchar *tmp_uri = NULL;
4125 #if !GTK_CHECK_VERSION(3, 0, 0)
4126 gchar *text, *base;
4127 #endif
4129 DNPRINTF(XT_D_URL, "notify_load_status_cb: %d %s\n",
4130 webkit_web_view_get_load_status(wview),
4131 get_uri(t) ? get_uri(t) : "NOTHING");
4133 if (t == NULL) {
4134 show_oops(NULL, "notify_load_status_cb invalid parameters");
4135 return;
4138 switch (webkit_web_view_get_load_status(wview)) {
4139 case WEBKIT_LOAD_PROVISIONAL:
4140 /* 0 */
4141 abort_favicon_download(t);
4142 #if GTK_CHECK_VERSION(2, 20, 0)
4143 gtk_widget_show(t->spinner);
4144 gtk_spinner_start(GTK_SPINNER(t->spinner));
4145 #endif
4146 t->download_requested = 0;
4148 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), TRUE);
4150 /* assume we are a new address */
4151 #if GTK_CHECK_VERSION(3, 0, 0)
4152 gtk_widget_set_name(t->uri_entry, XT_CSS_NORMAL);
4153 statusbar_modify_attr(t, XT_CSS_NORMAL);
4154 #else
4155 text = gdk_color_to_string(
4156 &t->default_style->text[GTK_STATE_NORMAL]);
4157 base = gdk_color_to_string(
4158 &t->default_style->base[GTK_STATE_NORMAL]);
4159 gtk_widget_modify_base(t->uri_entry, GTK_STATE_NORMAL,
4160 &t->default_style->base[GTK_STATE_NORMAL]);
4161 statusbar_modify_attr(t, text, base);
4162 g_free(text);
4163 g_free(base);
4164 #endif
4166 /* DOM is changing, unreference the previous focused element */
4167 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4168 if (t->active)
4169 g_object_unref(t->active);
4170 t->active = NULL;
4171 if (t->active_text) {
4172 g_free(t->active_text);
4173 t->active_text = NULL;
4175 #endif
4177 /* take focus if we are visible */
4178 focus_webview(t);
4179 t->focus_wv = 1;
4181 marks_clear(t);
4182 break;
4184 case WEBKIT_LOAD_COMMITTED:
4185 /* 1 */
4186 uri = get_uri(t);
4187 if (uri == NULL)
4188 return;
4189 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), uri);
4191 if (t->status) {
4192 g_free(t->status);
4193 t->status = NULL;
4195 set_status(t, "Loading: %s", (char *)uri);
4197 /* clear t->item, except if we're switching to an about: page */
4198 if (t->item && !g_str_has_prefix(uri, "xxxt://") &&
4199 !g_str_has_prefix(uri, "about:")) {
4200 g_object_unref(t->item);
4201 t->item = NULL;
4204 /* check if js white listing is enabled */
4205 if (enable_plugin_whitelist)
4206 check_and_set_pl(uri, t);
4207 if (enable_cookie_whitelist)
4208 check_and_set_cookie(uri, t);
4209 if (enable_js_whitelist)
4210 check_and_set_js(uri, t);
4212 if (t->styled)
4213 apply_style(t);
4216 /* we know enough to autosave the session */
4217 if (session_autosave) {
4218 a.s = NULL;
4219 save_tabs(t, &a);
4222 show_ca_status(t, uri);
4223 run_script(t, JS_HINTING);
4224 if (enable_autoscroll)
4225 run_script(t, JS_AUTOSCROLL);
4226 break;
4228 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
4229 /* 3 */
4230 if (color_visited_uris) {
4231 color_visited(t, color_visited_helper());
4234 * This colors the links you middle-click (open in new
4235 * tab) in the current tab.
4237 if (t->tab_id != gtk_notebook_get_current_page(notebook) &&
4238 (uri = get_uri(t)) != NULL)
4239 color_visited(get_current_tab(),
4240 g_strdup_printf("{'%s' : 'dummy'}", uri));
4242 break;
4244 case WEBKIT_LOAD_FINISHED:
4245 /* 2 */
4246 if ((uri = get_uri(t)) == NULL)
4247 return;
4249 * js_autorun calls get_uri which frees t->tmp_uri if on an
4250 * "about:" page. On "about:" pages, uri points to t->tmp_uri.
4251 * I.e. we will use freed memory. Prevent that.
4253 tmp_uri = g_strdup(uri);
4255 /* autorun some js if enabled */
4256 js_autorun(t);
4258 input_autofocus(t);
4260 if (!strncmp(tmp_uri, "http://", strlen("http://")) ||
4261 !strncmp(tmp_uri, "https://", strlen("https://")) ||
4262 !strncmp(tmp_uri, "file://", strlen("file://"))) {
4263 find.uri = (gchar *)tmp_uri;
4264 h = RB_FIND(history_list, &hl, &find);
4265 if (!h)
4266 insert_history_item(tmp_uri,
4267 get_title(t, FALSE), time(NULL));
4268 else
4269 h->time = time(NULL);
4272 if (statusbar_style == XT_STATUSBAR_URL)
4273 set_status(t, "%s", (char *)tmp_uri);
4274 else
4275 set_status(t, "%s", get_title(t, FALSE));
4276 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4277 #if GTK_CHECK_VERSION(2, 20, 0)
4278 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4279 gtk_widget_hide(t->spinner);
4280 #endif
4281 g_free(tmp_uri);
4282 break;
4284 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4285 case WEBKIT_LOAD_FAILED:
4286 /* 4 */
4287 if (!t->download_requested) {
4288 gtk_label_set_text(GTK_LABEL(t->label),
4289 get_title(t, FALSE));
4290 gtk_label_set_text(GTK_LABEL(t->tab_elems.label),
4291 get_title(t, FALSE));
4292 set_status(t, "%s", (char *)get_title(t, FALSE));
4293 gtk_window_set_title(GTK_WINDOW(main_window),
4294 get_title(t, TRUE));
4295 } else {
4298 #endif
4299 default:
4300 #if GTK_CHECK_VERSION(2, 20, 0)
4301 gtk_spinner_stop(GTK_SPINNER(t->spinner));
4302 gtk_widget_hide(t->spinner);
4303 #endif
4304 gtk_widget_set_sensitive(GTK_WIDGET(t->stop), FALSE);
4305 break;
4308 gtk_widget_set_sensitive(GTK_WIDGET(t->backward),
4309 can_go_back_for_real(t));
4311 gtk_widget_set_sensitive(GTK_WIDGET(t->forward),
4312 can_go_forward_for_real(t));
4315 void
4316 notify_title_cb(WebKitWebView* wview, GParamSpec* pspec, struct tab *t)
4318 const gchar *title = NULL, *win_title = NULL;
4320 title = get_title(t, FALSE);
4321 win_title = get_title(t, TRUE);
4322 if (title) {
4323 gtk_label_set_text(GTK_LABEL(t->label), title);
4324 gtk_label_set_text(GTK_LABEL(t->tab_elems.label), title);
4327 if (win_title && t->tab_id == gtk_notebook_get_current_page(notebook))
4328 gtk_window_set_title(GTK_WINDOW(main_window), win_title);
4331 char *
4332 get_domain(const gchar *host)
4334 size_t x;
4335 char *p;
4337 /* handle silly domains like .co.uk */
4339 if ((x = strlen(host)) <= 6)
4340 return (g_strdup(host));
4342 if (host[x - 3] == '.' && host[x - 6] == '.') {
4343 x = x - 7;
4344 while (x > 0)
4345 if (host[x] != '.')
4346 x--;
4347 else
4348 return (g_strdup(&host[x + 1]));
4351 /* find first . */
4352 p = g_strrstr(host, ".");
4353 if (p == NULL)
4354 return (g_strdup(""));
4355 p--;
4356 while (p > host)
4357 if (*p != '.')
4358 p--;
4359 else
4360 return (g_strdup(p + 1));
4362 return (g_strdup(host));
4365 void
4366 js_autorun(struct tab *t)
4368 SoupURI *su = NULL;
4369 const gchar *uri;
4370 size_t got_default = 0, got_host = 0;
4371 struct stat sb;
4372 char deff[PATH_MAX], hostf[PATH_MAX];
4373 char *js = NULL, *jsat, *domain = NULL;
4374 FILE *deffile = NULL, *hostfile = NULL;
4376 if (enable_js_autorun == 0)
4377 return;
4379 uri = get_uri(t);
4380 if (uri &&
4381 !(g_str_has_prefix(uri, "http://") ||
4382 g_str_has_prefix(uri, "https://")))
4383 goto done;
4385 su = soup_uri_new(uri);
4386 if (su == NULL)
4387 goto done;
4388 if (!SOUP_URI_VALID_FOR_HTTP(su))
4389 goto done;
4391 DNPRINTF(XT_D_JS, "%s: host: %s domain: %s\n", __func__,
4392 su->host, domain);
4393 domain = get_domain(su->host);
4395 snprintf(deff, sizeof deff, "%s" PS "default.js", js_dir);
4396 if ((deffile = fopen(deff, "r")) != NULL) {
4397 if (fstat(fileno(deffile), &sb) == -1) {
4398 show_oops(t, "can't stat default JS file");
4399 goto done;
4401 got_default = sb.st_size;
4404 /* try host first followed by domain */
4405 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, su->host);
4406 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4407 if ((hostfile = fopen(hostf, "r")) == NULL) {
4408 snprintf(hostf, sizeof hostf, "%s" PS "%s.js", js_dir, domain);
4409 DNPRINTF(XT_D_JS, "trying file: %s\n", hostf);
4410 if ((hostfile = fopen(hostf, "r")) == NULL)
4411 goto nofile;
4413 DNPRINTF(XT_D_JS, "file: %s\n", hostf);
4414 if (fstat(fileno(hostfile), &sb) == -1) {
4415 show_oops(t, "can't stat %s JS file", hostf);
4416 goto done;
4418 got_host = sb.st_size;
4420 nofile:
4421 if (got_default + got_host == 0)
4422 goto done;
4424 js = g_malloc0(got_default + got_host + 1);
4425 jsat = js;
4427 if (got_default) {
4428 if (fread(js, got_default, 1, deffile) != 1) {
4429 show_oops(t, "default file read error");
4430 goto done;
4432 jsat = js + got_default;
4435 if (got_host) {
4436 if (fread(jsat, got_host, 1, hostfile) != 1) {
4437 show_oops(t, "host file read error");
4438 goto done;
4442 DNPRINTF(XT_D_JS, "%s: about to run script\n", __func__);
4443 run_script(t, js);
4445 done:
4446 if (su)
4447 soup_uri_free(su);
4448 if (js)
4449 g_free(js);
4450 if (deffile)
4451 fclose(deffile);
4452 if (hostfile)
4453 fclose(hostfile);
4454 if (domain)
4455 g_free(domain);
4458 void
4459 webview_progress_changed_cb(WebKitWebView *wv, GParamSpec *pspec, struct tab *t)
4461 gdouble progress;
4463 progress = webkit_web_view_get_progress(wv);
4464 gtk_entry_set_progress_fraction(GTK_ENTRY(t->sbe.uri),
4465 progress == 1.0 ? 0 : progress);
4466 gtk_entry_set_progress_fraction(GTK_ENTRY(t->uri_entry),
4467 progress == 1.0 ? 0 : progress);
4469 update_statusbar_position(NULL, NULL);
4473 strict_transport_rb_cmp(struct strict_transport *a, struct strict_transport *b)
4475 char *p1, *p2;
4476 int l1, l2;
4478 /* compare strings from the end */
4479 l1 = strlen(a->host);
4480 l2 = strlen(b->host);
4482 p1 = a->host + l1;
4483 p2 = b->host + l2;
4484 for (; *p1 == *p2 && p1 > a->host && p2 > b->host;
4485 p1--, p2--)
4489 * Check if we need to do pattern expansion,
4490 * or if we're just keeping the tree in order
4492 if (a->flags & XT_STS_FLAGS_EXPAND &&
4493 b->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) {
4494 /* Check if we're matching the
4495 * 'host.xyz' part in '*.host.xyz'
4497 if (p2 == b->host && (p1 == a->host || *(p1-1) == '.')) {
4498 return (0);
4502 if (p1 == a->host && p2 == b->host)
4503 return (0);
4504 if (p1 == a->host)
4505 return (1);
4506 if (p2 == b->host)
4507 return (-1);
4509 if (*p1 < *p2)
4510 return (-1);
4511 if (*p1 > *p2)
4512 return (1);
4514 return (0);
4516 RB_GENERATE(strict_transport_tree, strict_transport, entry,
4517 strict_transport_rb_cmp);
4520 strict_transport_add(const char *domain, time_t timeout, int subdomains)
4522 struct strict_transport *d, find;
4523 time_t now;
4524 FILE *f;
4526 if (enable_strict_transport == FALSE)
4527 return (0);
4529 DPRINTF("strict_transport_add(%s,%" PRIi64 ",%d)\n", domain,
4530 (uint64_t)timeout, subdomains);
4532 now = time(NULL);
4533 if (timeout < now)
4534 return (0);
4536 find.host = (char *)domain;
4537 find.flags = 0;
4538 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4540 /* update flags */
4541 if (d) {
4542 /* check if update is needed */
4543 if (d->timeout == timeout &&
4544 (d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS) == subdomains)
4545 return (0);
4547 d->timeout = timeout;
4548 if (subdomains)
4549 d->flags |= XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4551 /* We're still initializing */
4552 if (strict_transport_file == NULL)
4553 return (0);
4555 if ((f = fopen(strict_transport_file, "w")) == NULL) {
4556 show_oops(NULL,
4557 "can't open strict-transport rules file");
4558 return (1);
4561 fprintf(f, "# Generated file - do not update unless you know "
4562 "what you're doing\n");
4563 RB_FOREACH(d, strict_transport_tree, &st_tree) {
4564 if (d->timeout < now)
4565 continue;
4566 fprintf(f, "%s\t%" PRIi64 "\t%d\n", d->host,
4567 (uint64_t)d->timeout,
4568 d->flags & XT_STS_FLAGS_INCLUDE_SUBDOMAINS);
4570 fclose(f);
4571 } else {
4572 d = g_malloc(sizeof *d);
4573 d->host= g_strdup(domain);
4574 d->timeout = timeout;
4575 if (subdomains)
4576 d->flags = XT_STS_FLAGS_INCLUDE_SUBDOMAINS;
4577 else
4578 d->flags = 0;
4579 RB_INSERT(strict_transport_tree, &st_tree, d);
4581 /* We're still initializing */
4582 if (strict_transport_file == NULL)
4583 return (0);
4585 if ((f = fopen(strict_transport_file, "a+")) == NULL) {
4586 show_oops(NULL,
4587 "can't open strict-transport rules file");
4588 return (1);
4591 fseek(f, 0, SEEK_END);
4592 fprintf(f,"%s\t%" PRIi64 "\t%d\n", d->host, (uint64_t)timeout,
4593 subdomains);
4594 fclose(f);
4596 return (0);
4600 strict_transport_check(const char *host)
4602 static struct strict_transport *d = NULL;
4603 struct strict_transport find;
4605 if (enable_strict_transport == FALSE)
4606 return (0);
4608 find.host = (char *)host;
4610 /* match for domains that include subdomains */
4611 find.flags = XT_STS_FLAGS_EXPAND;
4613 /* First, check if we're already at the right node */
4614 if (d != NULL && strict_transport_rb_cmp(&find, d) == 0) {
4615 return (1);
4618 d = RB_FIND(strict_transport_tree, &st_tree, &find);
4619 if (d != NULL)
4620 return (1);
4622 return (0);
4626 strict_transport_init()
4628 char file[PATH_MAX];
4629 char delim[3];
4630 char *rule;
4631 char *ptr;
4632 FILE *f;
4633 size_t len;
4634 time_t timeout, now;
4635 int subdomains;
4637 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_STS_FILE);
4638 if ((f = fopen(file, "r")) == NULL) {
4639 strict_transport_file = g_strdup(file);
4640 return (0);
4643 delim[0] = '\\';
4644 delim[1] = '\\';
4645 delim[2] = '#';
4646 rule = NULL;
4647 now = time(NULL);
4649 for (;;) {
4650 if ((rule = fparseln(f, &len, NULL, delim, 0)) == NULL) {
4651 if (!feof(f) || ferror(f))
4652 goto corrupt_file;
4653 else
4654 break;
4657 /* get second entry */
4658 if ((ptr = strpbrk(rule, " \t")) == NULL)
4659 goto corrupt_file;
4661 *ptr++ = '\0';
4662 timeout = atoi(ptr);
4664 /* get third entry */
4665 if ((ptr = strpbrk(ptr, " \t")) == NULL)
4666 goto corrupt_file;
4668 ptr ++;
4669 subdomains = atoi(ptr);
4671 if (timeout > now)
4672 strict_transport_add(rule, timeout, subdomains);
4673 free(rule);
4676 fclose(f);
4677 strict_transport_file = g_strdup(file);
4678 return (0);
4680 corrupt_file:
4681 startpage_add("strict-transport rules file ('%s') is corrupt", file);
4682 if (rule)
4683 free(rule);
4684 fclose(f);
4685 return (1);
4689 force_https_check(const char *uri)
4691 struct wl_entry *w = NULL;
4693 if (uri == NULL)
4694 return (0);
4696 if ((w = wl_find_uri(uri, &force_https)) == NULL)
4697 return (0);
4698 else
4699 return (1);
4702 void
4703 strict_transport_security_cb(SoupMessage *msg, gpointer data)
4705 SoupURI *uri;
4706 const char *sts;
4707 char *ptr;
4708 time_t timeout = 0;
4709 int subdomains = FALSE;
4711 if (msg == NULL)
4712 return;
4714 sts = soup_message_headers_get_one(msg->response_headers,
4715 "Strict-Transport-Security");
4716 uri = soup_message_get_uri(msg);
4718 if (sts == NULL || uri == NULL)
4719 return;
4721 if ((ptr = strcasestr(sts, "max-age="))) {
4722 ptr += strlen("max-age=");
4723 timeout = atoll(ptr);
4724 } else
4725 return; /* malformed header - max-age must be included */
4727 if ((ptr = strcasestr(sts, "includeSubDomains")))
4728 subdomains = TRUE;
4730 strict_transport_add(uri->host, timeout + time(NULL), subdomains);
4733 void
4734 session_rq_cb(SoupSession *s, SoupMessage *msg, SoupSocket *socket,
4735 gpointer data)
4737 SoupURI *dest;
4738 SoupURI *ref_uri;
4739 const char *ref;
4741 char *ref_suffix;
4742 char *dest_suffix;
4744 if (s == NULL || msg == NULL)
4745 return;
4747 if (enable_strict_transport) {
4748 soup_message_add_header_handler(msg, "finished",
4749 "Strict-Transport-Security",
4750 G_CALLBACK(strict_transport_security_cb), NULL);
4753 if (referer_mode == XT_REFERER_ALWAYS)
4754 return;
4756 /* Check if referer is set - and what the user requested for referers */
4757 ref = soup_message_headers_get_one(msg->request_headers, "Referer");
4758 if (ref) {
4759 DNPRINTF(XT_D_NAV, "session_rq_cb: Referer: %s\n", ref);
4760 switch (referer_mode) {
4761 case XT_REFERER_NEVER:
4762 DNPRINTF(XT_D_NAV, "session_rq_cb: removing referer\n");
4763 soup_message_headers_remove(msg->request_headers,
4764 "Referer");
4765 break;
4766 case XT_REFERER_SAME_DOMAIN:
4767 ref_uri = soup_uri_new(ref);
4768 dest = soup_message_get_uri(msg);
4770 ref_suffix = tld_get_suffix(ref_uri->host);
4771 dest_suffix = tld_get_suffix(dest->host);
4773 if (dest && ref_suffix && dest_suffix &&
4774 strcmp(ref_suffix, dest_suffix) != 0) {
4775 soup_message_headers_remove(msg->request_headers,
4776 "Referer");
4777 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4778 "referer (not same domain) (suffixes: %s - %s)\n",
4779 ref_suffix, dest_suffix);
4781 soup_uri_free(ref_uri);
4782 break;
4783 case XT_REFERER_SAME_FQDN:
4784 ref_uri = soup_uri_new(ref);
4785 dest = soup_message_get_uri(msg);
4786 if (dest && strcmp(ref_uri->host, dest->host) != 0) {
4787 soup_message_headers_remove(msg->request_headers,
4788 "Referer");
4789 DNPRINTF(XT_D_NAV, "session_rq_cb: removing "
4790 "referer (not same fqdn) (should be %s)\n",
4791 dest->host);
4793 soup_uri_free(ref_uri);
4794 break;
4795 case XT_REFERER_CUSTOM:
4796 DNPRINTF(XT_D_NAV, "session_rq_cb: setting referer "
4797 "to %s\n", referer_custom);
4798 soup_message_headers_replace(msg->request_headers,
4799 "Referer", referer_custom);
4800 break;
4806 webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf,
4807 WebKitNetworkRequest *request, WebKitWebNavigationAction *na,
4808 WebKitWebPolicyDecision *pd, struct tab *t)
4810 WebKitWebNavigationReason reason;
4811 char *uri;
4813 if (t == NULL) {
4814 show_oops(NULL, "webview_npd_cb invalid parameters");
4815 return (FALSE);
4818 DNPRINTF(XT_D_NAV, "webview_npd_cb: ctrl_click %d %s\n",
4819 t->ctrl_click,
4820 webkit_network_request_get_uri(request));
4822 uri = (char *)webkit_network_request_get_uri(request);
4824 if (!auto_load_images && t->load_images) {
4826 /* Disable autoloading of images, now that we're done loading
4827 * them. */
4828 g_object_set(G_OBJECT(t->settings),
4829 "auto-load-images", FALSE, (char *)NULL);
4830 webkit_web_view_set_settings(t->wv, t->settings);
4832 t->load_images = FALSE;
4835 /* If this is an xtp url, we don't load anything else. */
4836 if (parse_xtp_url(t, uri)) {
4837 webkit_web_policy_decision_ignore(pd);
4838 return (TRUE);
4841 if (parse_custom_uri(t, uri)) {
4842 webkit_web_policy_decision_ignore(pd);
4843 return (TRUE);
4846 if (valid_url_type(uri)) {
4847 show_oops(t, "Stopping attempt to load an invalid URI (possible"
4848 " bait and switch attack)");
4849 webkit_web_policy_decision_ignore(pd);
4850 return (TRUE);
4853 if ((t->mode == XT_MODE_HINT && t->new_tab) || t->ctrl_click) {
4854 t->ctrl_click = 0;
4855 create_new_tab(uri, NULL, ctrl_click_focus, -1);
4856 webkit_web_policy_decision_ignore(pd);
4857 return (TRUE); /* we made the decission */
4861 * This is a little hairy but it comes down to this:
4862 * when we run in whitelist mode we have to assist the browser in
4863 * opening the URL that it would have opened in a new tab.
4865 reason = webkit_web_navigation_action_get_reason(na);
4866 if (reason == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
4867 set_normal_tab_meaning(t);
4868 if (enable_scripts == 0 && enable_cookie_whitelist == 1)
4869 load_uri(t, uri);
4870 webkit_web_policy_decision_use(pd);
4871 return (TRUE); /* we made the decision */
4874 return (FALSE);
4877 void
4878 webview_rrs_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitWebResource *res,
4879 WebKitNetworkRequest *request, WebKitNetworkResponse *response,
4880 struct tab *t)
4882 SoupMessage *msg = NULL;
4883 SoupURI *uri = NULL;
4884 struct http_accept ha_find, *ha = NULL;
4885 struct user_agent ua_find, *ua = NULL;
4886 struct domain_id di_find, *di = NULL;
4887 char *uri_s = NULL;
4889 msg = webkit_network_request_get_message(request);
4890 if (!msg)
4891 return;
4893 uri = soup_message_get_uri(msg);
4894 if (!uri)
4895 return;
4896 uri_s = soup_uri_to_string(uri, FALSE);
4898 if (strcmp(uri->scheme, SOUP_URI_SCHEME_HTTP) == 0) {
4899 if (strict_transport_check(uri->host) ||
4900 force_https_check(uri_s)) {
4901 DNPRINTF(XT_D_NAV, "webview_rrs_cb: force https for %s\n",
4902 uri->host);
4903 soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTPS);
4907 if (do_not_track)
4908 soup_message_headers_append(msg->request_headers, "DNT", "1");
4911 * Check if resources on this domain have been loaded before. If
4912 * not, add the current tab's http-accept and user-agent id's to a
4913 * new domain_id and insert into the RB tree. Use these http headers
4914 * for all resources loaded from this domain for the lifetime of the
4915 * browser.
4917 if ((di_find.domain = uri->host) == NULL)
4918 goto done;
4919 if ((di = RB_FIND(domain_id_list, &di_list, &di_find)) == NULL) {
4920 di = g_malloc(sizeof *di);
4921 di->domain = g_strdup(uri->host);
4922 di->ua_id = t->user_agent_id++;
4923 di->ha_id = t->http_accept_id++;
4924 RB_INSERT(domain_id_list, &di_list, di);
4926 ua_find.id = t->user_agent_id;
4927 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4928 if (ua == NULL)
4929 t->user_agent_id = 0;
4931 ha_find.id = t->http_accept_id;
4932 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4933 if (ha == NULL)
4934 t->http_accept_id = 0;
4937 ua_find.id = di->ua_id;
4938 ua = RB_FIND(user_agent_list, &ua_list, &ua_find);
4939 ha_find.id = di->ha_id;
4940 ha = RB_FIND(http_accept_list, &ha_list, &ha_find);
4942 if (ua != NULL)
4943 soup_message_headers_replace(msg->request_headers,
4944 "User-Agent", ua->value);
4945 if (ha != NULL)
4946 soup_message_headers_replace(msg->request_headers,
4947 "Accept", ha->value);
4949 done:
4950 if (uri_s)
4951 g_free(uri_s);
4954 WebKitWebView *
4955 webview_cwv_cb(WebKitWebView *wv, WebKitWebFrame *wf, struct tab *t)
4957 struct tab *tt;
4958 struct wl_entry *w = NULL;
4959 const gchar *uri;
4960 WebKitWebView *webview = NULL;
4961 int x = 1;
4963 DNPRINTF(XT_D_NAV, "webview_cwv_cb: %s\n",
4964 webkit_web_view_get_uri(wv));
4966 if (tabless) {
4967 /* open in current tab */
4968 webview = t->wv;
4969 } else if (enable_scripts == 0 && enable_js_whitelist == 1) {
4970 uri = webkit_web_view_get_uri(wv);
4971 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
4972 return (NULL);
4974 if (t->ctrl_click) {
4975 x = ctrl_click_focus;
4976 t->ctrl_click = 0;
4978 tt = create_new_tab(NULL, NULL, x, -1);
4979 webview = tt->wv;
4980 } else if (enable_scripts == 1) {
4981 if (t->ctrl_click) {
4982 x = ctrl_click_focus;
4983 t->ctrl_click = 0;
4985 tt = create_new_tab(NULL, NULL, x, -1);
4986 webview = tt->wv;
4989 return (webview);
4992 gboolean
4993 webview_closewv_cb(WebKitWebView *wv, struct tab *t)
4995 const gchar *uri;
4996 struct wl_entry *w = NULL;
4998 DNPRINTF(XT_D_NAV, "webview_close_cb: %d\n", t->tab_id);
5000 if (enable_scripts == 0 && enable_cookie_whitelist == 1) {
5001 uri = webkit_web_view_get_uri(wv);
5002 if (uri && (w = wl_find_uri(uri, &js_wl)) == NULL)
5003 return (FALSE);
5005 delete_tab(t);
5006 } else if (enable_scripts == 1)
5007 delete_tab(t);
5009 return (TRUE);
5013 webview_event_cb(GtkWidget *w, GdkEventButton *e, struct tab *t)
5015 /* we can not eat the event without throwing gtk off so defer it */
5017 /* catch middle click */
5018 if (e->type == GDK_BUTTON_RELEASE && e->button == 2) {
5019 t->ctrl_click = 1;
5020 goto done;
5023 /* catch ctrl click */
5024 if (e->type == GDK_BUTTON_RELEASE &&
5025 CLEAN(e->state) == GDK_CONTROL_MASK)
5026 t->ctrl_click = 1;
5027 else
5028 t->ctrl_click = 0;
5029 done:
5030 return (XT_CB_PASSTHROUGH);
5034 run_mimehandler(struct tab *t, char *mime_type, WebKitNetworkRequest *request)
5036 struct mime_type *m;
5037 char *sv[3];
5038 GError *gerr = NULL;
5040 m = find_mime_type(mime_type);
5041 if (m == NULL)
5042 return (1);
5043 if (m->mt_download)
5044 return (1);
5046 sv[0] = m->mt_action;
5047 sv[1] = (char *)webkit_network_request_get_uri(request);
5048 sv[2] = NULL;
5050 /* ignore donothing from example config */
5051 if (m->mt_action && !strcmp(m->mt_action, "donothing"))
5052 return (0);
5054 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
5055 NULL, &gerr))
5056 show_oops(t, "%s: could not spawn process (%s)", __func__,
5057 gerr ? gerr->message : "N/A");
5058 return (0);
5061 char *
5062 get_mime_type(const char *uri)
5064 GFileInfo *fi;
5065 GFile *gf;
5066 const gchar *m;
5067 char *mime_type = NULL;
5068 char *file;
5070 if (uri == NULL) {
5071 show_oops(NULL, "%s: invalid parameters", __func__);
5072 return (NULL);
5075 if (g_str_has_prefix(uri, "file://"))
5076 file = g_filename_from_uri(uri, NULL, NULL);
5077 else
5078 file = g_strdup(uri);
5080 if (file == NULL)
5081 return (NULL);
5083 gf = g_file_new_for_path(file);
5084 fi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0,
5085 NULL, NULL);
5086 if ((m = g_file_info_get_content_type(fi)) != NULL)
5087 mime_type = g_strdup(m);
5088 g_object_unref(fi);
5089 g_object_unref(gf);
5090 g_free(file);
5092 return (mime_type);
5096 run_download_mimehandler(char *mime_type, char *file)
5098 struct mime_type *m;
5099 char *sv[3];
5101 m = find_mime_type(mime_type);
5102 if (m == NULL)
5103 return (1);
5105 sv[0] = m->mt_action;
5106 sv[1] = file;
5107 sv[2] = NULL;
5108 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
5109 NULL, NULL)) {
5110 show_oops(NULL, "%s: could not spawn process: %s %s", __func__,
5111 sv[0], sv[1]);
5112 return (1);
5114 return (0);
5117 void
5118 download_status_changed_cb(WebKitDownload *download, GParamSpec *spec,
5119 WebKitWebView *wv)
5121 WebKitDownloadStatus status;
5122 const char *uri;
5123 char *file = NULL;
5124 char *mime = NULL;
5125 char *destination;
5127 if (download == NULL)
5128 return;
5129 status = webkit_download_get_status(download);
5130 if (status != WEBKIT_DOWNLOAD_STATUS_FINISHED)
5131 return;
5133 if (download_notifications) {
5134 /* because basename() takes a char * on linux */
5135 destination = g_strdup(
5136 webkit_download_get_destination_uri(download));
5137 show_oops(NULL, "Download of '%s' finished",
5138 basename(destination));
5139 g_free(destination);
5141 uri = webkit_download_get_destination_uri(download);
5142 if (uri == NULL)
5143 return;
5144 mime = get_mime_type(uri);
5145 if (mime == NULL)
5146 return;
5148 if (g_str_has_prefix(uri, "file://"))
5149 file = g_filename_from_uri(uri, NULL, NULL);
5150 else
5151 file = g_strdup(uri);
5153 if (file == NULL)
5154 goto done;
5156 run_download_mimehandler((char *)mime, file);
5158 done:
5159 g_free(mime);
5163 webview_mimetype_cb(WebKitWebView *wv, WebKitWebFrame *frame,
5164 WebKitNetworkRequest *request, char *mime_type,
5165 WebKitWebPolicyDecision *decision, struct tab *t)
5167 if (t == NULL) {
5168 show_oops(NULL, "webview_mimetype_cb invalid parameters");
5169 return (FALSE);
5172 DNPRINTF(XT_D_DOWNLOAD, "webview_mimetype_cb: tab %d mime %s\n",
5173 t->tab_id, mime_type);
5175 if (run_mimehandler(t, mime_type, request) == 0) {
5176 webkit_web_policy_decision_ignore(decision);
5177 focus_webview(t);
5178 return (TRUE);
5181 if (webkit_web_view_can_show_mime_type(wv, mime_type) == FALSE) {
5182 webkit_web_policy_decision_download(decision);
5183 return (TRUE);
5186 return (FALSE);
5190 download_start(struct tab *t, struct download *d, int flag)
5192 WebKitNetworkRequest *req;
5193 struct stat sb;
5194 const gchar *suggested_name;
5195 gchar *filename = NULL;
5196 char *uri = NULL;
5197 char *path = NULL;
5198 char *destination;
5199 int ret = TRUE;
5200 int i;
5202 if (d == NULL || t == NULL) {
5203 show_oops(NULL, "%s invalid parameters", __func__);
5204 return (FALSE);
5207 suggested_name = webkit_download_get_suggested_filename(d->download);
5208 if (suggested_name == NULL)
5209 return (FALSE); /* abort download */
5211 i = 0;
5212 do {
5213 if (filename) {
5214 g_free(filename);
5215 filename = NULL;
5217 if (i) {
5218 g_free(uri);
5219 uri = NULL;
5220 filename = g_strdup_printf("%d%s", i, suggested_name);
5222 #ifdef __MINGW32__
5223 /* XXX using urls doesn't work properly in windows? */
5224 uri = g_strdup_printf("%s\\%s", download_dir, i ?
5225 filename : suggested_name);
5226 #else
5227 path = g_strdup_printf("%s" PS "%s", download_dir, i ?
5228 filename : suggested_name);
5229 if ((uri = g_filename_to_uri(path, NULL, NULL)) == NULL)
5230 break; /* XXX */
5231 #endif
5232 i++;
5233 #ifdef __MINGW32__
5234 } while (!stat(uri, &sb));
5235 #else
5236 } while (!stat(path, &sb));
5237 #endif
5239 DNPRINTF(XT_D_DOWNLOAD, "%s: tab %d filename %s "
5240 "local %s\n", __func__, t->tab_id, filename, uri);
5242 /* if we're restarting the download, or starting
5243 * it after doing something else, we need to recreate
5244 * the download request.
5246 if (flag == XT_DL_RESTART) {
5247 req = webkit_network_request_new(webkit_download_get_uri(d->download));
5248 webkit_download_cancel(d->download);
5249 g_object_unref(d->download);
5250 d->download = webkit_download_new(req);
5251 g_object_unref(req);
5254 webkit_download_set_destination_uri(d->download, uri);
5256 if (webkit_download_get_status(d->download) ==
5257 WEBKIT_DOWNLOAD_STATUS_ERROR) {
5258 show_oops(t, "%s: download failed to start", __func__);
5259 ret = FALSE;
5260 show_oops(t, "Download Failed");
5261 } else {
5262 /* connect "download first" mime handler */
5263 g_signal_connect(G_OBJECT(d->download), "notify::status",
5264 G_CALLBACK(download_status_changed_cb), NULL);
5266 /* get from history */
5267 g_object_ref(d->download);
5268 /* because basename() takes a char * on linux */
5269 destination = g_strdup(
5270 webkit_download_get_destination_uri(d->download));
5271 show_oops(t, "Download of '%s' started...",
5272 basename(destination));
5273 g_free(destination);
5276 if (flag != XT_DL_START)
5277 webkit_download_start(d->download);
5279 DNPRINTF(XT_D_DOWNLOAD, "download status : %d",
5280 webkit_download_get_status(d->download));
5282 /* sync other download manager tabs */
5283 update_download_tabs(NULL);
5285 if (uri)
5286 g_free(uri);
5287 if (path)
5288 g_free(path);
5289 if (filename)
5290 g_free(filename);
5292 return (ret);
5296 download_ask_cb(struct tab *t, GdkEventKey *e, gpointer data)
5298 struct download *d = data;
5300 /* unset mode */
5301 t->mode_cb = NULL;
5302 t->mode_cb_data = NULL;
5304 if (d == NULL) {
5305 if (e != NULL)
5306 e->keyval = GDK_Escape;
5307 return (XT_CB_PASSTHROUGH);
5310 DPRINTF("download_ask_cb: User pressed %c\n", e->keyval);
5311 if (e->keyval == 'y' || e->keyval == 'Y' || e->keyval == GDK_Return)
5312 /* We need to do a RESTART, because we're not calling from
5313 * webview_download_cb
5315 download_start(t, d, XT_DL_RESTART);
5317 /* for all other keyvals, we just let the download be */
5318 e->keyval = GDK_Escape;
5319 return (XT_CB_HANDLED);
5323 download_ask(struct tab *t, struct download *d)
5325 const gchar *suggested_name;
5327 suggested_name = webkit_download_get_suggested_filename(d->download);
5328 if (suggested_name == NULL)
5329 return (FALSE); /* abort download */
5331 show_oops(t, "download file %s [y/n] ?", suggested_name);
5332 t->mode_cb = download_ask_cb;
5333 t->mode_cb_data = d;
5335 return (TRUE);
5339 webview_download_cb(WebKitWebView *wv, WebKitDownload *wk_download,
5340 struct tab *t)
5342 const gchar *suggested_name;
5343 struct download *download_entry;
5344 int ret = TRUE;
5346 if (wk_download == NULL || t == NULL) {
5347 show_oops(NULL, "%s invalid parameters", __func__);
5348 return (FALSE);
5351 suggested_name = webkit_download_get_suggested_filename(wk_download);
5352 if (suggested_name == NULL)
5353 return (FALSE); /* abort download */
5355 download_entry = g_malloc(sizeof(struct download));
5356 download_entry->download = wk_download;
5357 download_entry->tab = t;
5358 download_entry->id = next_download_id++;
5359 RB_INSERT(download_list, &downloads, download_entry);
5360 t->download_requested = 1;
5362 if (download_mode == XT_DM_START)
5363 ret = download_start(t, download_entry, XT_DL_START);
5364 else if (download_mode == XT_DM_ASK)
5365 ret = download_ask(t, download_entry);
5366 else if (download_mode == XT_DM_ADD)
5367 show_oops(t, "added %s to download manager",
5368 suggested_name);
5370 /* sync other download manager tabs */
5371 update_download_tabs(NULL);
5374 * NOTE: never redirect/render the current tab before this
5375 * function returns. This will cause the download to never start.
5377 return (ret); /* start download */
5380 void
5381 webview_hover_cb(WebKitWebView *wv, gchar *title, gchar *uri, struct tab *t)
5383 DNPRINTF(XT_D_KEY, "webview_hover_cb: %s %s\n", title, uri);
5385 if (t == NULL) {
5386 show_oops(NULL, "webview_hover_cb");
5387 return;
5390 if (uri)
5391 set_status(t, "Link: %s", uri);
5392 else {
5393 if (statusbar_style == XT_STATUSBAR_URL) {
5394 const gchar *page_uri;
5396 if ((page_uri = get_uri(t)) != NULL)
5397 set_status(t, "%s", page_uri);
5398 } else
5399 set_status(t, "%s", (char *)get_title(t, FALSE));
5404 mark(struct tab *t, struct karg *arg)
5406 char mark;
5407 int index;
5408 double pos;
5410 mark = arg->s[1];
5411 if ((index = marktoindex(mark)) == -1)
5412 return (-1);
5414 if (arg->i == XT_MARK_SET)
5415 t->mark[index] = gtk_adjustment_get_value(t->adjust_v);
5416 else if (arg->i == XT_MARK_GOTO) {
5417 if (t->mark[index] == XT_INVALID_MARK) {
5418 show_oops(t, "mark '%c' does not exist", mark);
5419 return (-1);
5421 /* XXX t->mark[index] can be bigger than the maximum if ajax or
5422 something changes the document size */
5423 pos = gtk_adjustment_get_value(t->adjust_v);
5424 gtk_adjustment_set_value(t->adjust_v, t->mark[index]);
5425 t->mark[marktoindex('\'')] = pos;
5428 return (0);
5431 void
5432 marks_clear(struct tab *t)
5434 int i;
5436 for (i = 0; i < LENGTH(t->mark); i++)
5437 t->mark[i] = XT_INVALID_MARK;
5441 qmarks_load(void)
5443 char file[PATH_MAX];
5444 char *line = NULL, *p;
5445 int index, i;
5446 FILE *f;
5447 size_t linelen;
5449 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5450 if ((f = fopen(file, "r+")) == NULL) {
5451 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5452 return (1);
5455 for (i = 1; ; i++) {
5456 if ((line = fparseln(f, &linelen, NULL, NULL, 0)) == NULL)
5457 break;
5458 if (strlen(line) == 0 || line[0] == '#') {
5459 free(line);
5460 line = NULL;
5461 continue;
5464 p = strtok(line, " \t");
5466 if (p == NULL || strlen(p) != 1 ||
5467 (index = qmarktoindex(*p)) == -1) {
5468 warnx("corrupt quickmarks file, line %d", i);
5469 break;
5472 p = strtok(NULL, " \t");
5473 if (qmarks[index] != NULL)
5474 g_free(qmarks[index]);
5475 qmarks[index] = g_strdup(p);
5478 fclose(f);
5480 return (0);
5484 qmarks_save(void)
5486 char file[PATH_MAX];
5487 int i;
5488 FILE *f;
5490 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
5491 if ((f = fopen(file, "r+")) == NULL) {
5492 show_oops(NULL, "Can't open quickmarks file: %s", strerror(errno));
5493 return (1);
5496 for (i = 0; i < XT_NOQMARKS; i++)
5497 if (qmarks[i] != NULL)
5498 fprintf(f, "%c %s\n", indextoqmark(i), qmarks[i]);
5500 fclose(f);
5502 return (0);
5506 qmark(struct tab *t, struct karg *arg)
5508 char mark;
5509 int index;
5511 mark = arg->s[strlen(arg->s)-1];
5512 index = qmarktoindex(mark);
5513 if (index == -1)
5514 return (-1);
5516 switch (arg->i) {
5517 case XT_QMARK_SET:
5518 if (qmarks[index] != NULL) {
5519 g_free(qmarks[index]);
5520 qmarks[index] = NULL;
5523 qmarks_load(); /* sync if multiple instances */
5524 qmarks[index] = g_strdup(get_uri(t));
5525 qmarks_save();
5526 break;
5527 case XT_QMARK_OPEN:
5528 if (qmarks[index] != NULL)
5529 load_uri(t, qmarks[index]);
5530 else {
5531 show_oops(t, "quickmark \"%c\" does not exist",
5532 mark);
5533 return (-1);
5535 break;
5536 case XT_QMARK_TAB:
5537 if (qmarks[index] != NULL)
5538 create_new_tab(qmarks[index], NULL, 1, -1);
5539 else {
5540 show_oops(t, "quickmark \"%c\" does not exist",
5541 mark);
5542 return (-1);
5544 break;
5547 return (0);
5551 go_up(struct tab *t, struct karg *args)
5553 int levels;
5554 int lastidx;
5555 char *uri;
5556 char *tmp;
5557 char *p;
5559 if (args->i == XT_GO_UP_ROOT)
5560 levels = XT_GO_UP_ROOT;
5561 else if ((levels = atoi(args->s)) == 0)
5562 levels = 1;
5564 uri = g_strdup(get_uri(t));
5565 if (uri == NULL)
5566 return (1);
5568 if ((tmp = strstr(uri, XT_PROTO_DELIM)) == NULL)
5569 return (1);
5571 tmp += strlen(XT_PROTO_DELIM);
5573 /* it makes no sense to strip the last slash from ".../dir/", skip it */
5574 lastidx = strlen(tmp) - 1;
5575 if (lastidx > 0) {
5576 if (tmp[lastidx] == '/')
5577 tmp[lastidx] = '\0';
5580 while (levels--) {
5581 p = strrchr(tmp, '/');
5582 if (p == tmp) { /* Are we at the root of a file://-path? */
5583 p[1] = '\0';
5584 break;
5585 } else if (p != NULL)
5586 *p = '\0';
5587 else
5588 break;
5591 load_uri(t, uri);
5592 g_free(uri);
5594 return (0);
5598 gototab(struct tab *t, struct karg *args)
5600 int tab;
5601 struct karg arg = {0, NULL, -1};
5603 tab = atoi(args->s);
5605 if (args->i == 0)
5606 arg.i = XT_TAB_NEXT;
5607 else
5608 arg.i = args->i;
5610 arg.precount = tab;
5612 movetab(t, &arg);
5614 return (0);
5618 zoom_amount(struct tab *t, struct karg *arg)
5620 struct karg narg = {0, NULL, -1};
5622 narg.i = atoi(arg->s);
5623 resizetab(t, &narg);
5625 return (0);
5629 flip_colon(struct tab *t, struct karg *arg)
5631 struct karg narg = {0, NULL, -1};
5632 char *p;
5634 if (t == NULL || arg == NULL)
5635 return (1);
5637 p = strstr(arg->s, ":");
5638 if (p == NULL)
5639 return (1);
5640 *p = '\0';
5642 narg.i = ':';
5643 narg.s = arg->s;
5644 command(t, &narg);
5646 return (0);
5649 /* buffer commands receive the regex that triggered them in arg.s */
5650 char bcmd[XT_BUFCMD_SZ];
5651 struct buffercmd {
5652 char *regex;
5653 int precount;
5654 #define XT_PRE_NO (0)
5655 #define XT_PRE_YES (1)
5656 #define XT_PRE_MAYBE (2)
5657 char *cmd;
5658 int (*func)(struct tab *, struct karg *);
5659 int arg;
5660 regex_t cregex;
5661 } buffercmds[] = {
5662 { "^[0-9]*gu$", XT_PRE_MAYBE, "gu", go_up, 0 },
5663 { "^gU$", XT_PRE_NO, "gU", go_up, XT_GO_UP_ROOT },
5664 { "^gg$", XT_PRE_NO, "gg", move, XT_MOVE_TOP },
5665 { "^gG$", XT_PRE_NO, "gG", move, XT_MOVE_BOTTOM },
5666 { "^[0-9]+%$", XT_PRE_YES, "%", move, XT_MOVE_PERCENT },
5667 { "^zz$", XT_PRE_NO, "zz", move, XT_MOVE_CENTER },
5668 { "^gh$", XT_PRE_NO, "gh", go_home, 0 },
5669 { "^m[a-zA-Z0-9]$", XT_PRE_NO, "m", mark, XT_MARK_SET },
5670 { "^['][a-zA-Z0-9']$", XT_PRE_NO, "'", mark, XT_MARK_GOTO },
5671 { "^[0-9]+t$", XT_PRE_YES, "t", gototab, 0 },
5672 { "^g0$", XT_PRE_YES, "g0", movetab, XT_TAB_FIRST },
5673 { "^g[$]$", XT_PRE_YES, "g$", movetab, XT_TAB_LAST },
5674 { "^[0-9]*gt$", XT_PRE_YES, "t", movetab, XT_TAB_NEXT },
5675 { "^[0-9]*gT$", XT_PRE_YES, "T", movetab, XT_TAB_PREV },
5676 { "^M[a-zA-Z0-9]$", XT_PRE_NO, "M", qmark, XT_QMARK_SET },
5677 { "^go[a-zA-Z0-9]$", XT_PRE_NO, "go", qmark, XT_QMARK_OPEN },
5678 { "^gn[a-zA-Z0-9]$", XT_PRE_NO, "gn", qmark, XT_QMARK_TAB },
5679 { "^ZR$", XT_PRE_NO, "ZR", restart, 0 },
5680 { "^ZZ$", XT_PRE_NO, "ZZ", quit, 0 },
5681 { "^zi$", XT_PRE_NO, "zi", resizetab, XT_ZOOM_IN },
5682 { "^zo$", XT_PRE_NO, "zo", resizetab, XT_ZOOM_OUT },
5683 { "^z0$", XT_PRE_NO, "z0", resizetab, XT_ZOOM_NORMAL },
5684 { "^[0-9]+Z$", XT_PRE_YES, "Z", zoom_amount, 0 },
5685 { "^[0-9]+:$", XT_PRE_YES, ":", flip_colon, 0 },
5688 void
5689 buffercmd_init(void)
5691 int i;
5693 for (i = 0; i < LENGTH(buffercmds); i++)
5694 if (regcomp(&buffercmds[i].cregex, buffercmds[i].regex,
5695 REG_EXTENDED | REG_NOSUB))
5696 startpage_add("invalid buffercmd regex %s",
5697 buffercmds[i].regex);
5700 void
5701 buffercmd_abort(struct tab *t)
5703 int i;
5705 if (t == NULL)
5706 return;
5708 DNPRINTF(XT_D_BUFFERCMD, "%s: clearing buffer\n", __func__);
5710 for (i = 0; i < LENGTH(bcmd); i++)
5711 bcmd[i] = '\0';
5713 cmd_prefix = 0; /* clear prefix for non-buffer commands */
5714 if (t->sbe.buffercmd)
5715 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5718 void
5719 buffercmd_execute(struct tab *t, struct buffercmd *cmd)
5721 struct karg arg = {0, NULL, -1};
5723 arg.i = cmd->arg;
5724 arg.s = g_strdup(bcmd);
5726 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_execute: buffer \"%s\" "
5727 "matches regex \"%s\", executing\n", bcmd, cmd->regex);
5728 cmd->func(t, &arg);
5730 if (arg.s)
5731 g_free(arg.s);
5733 buffercmd_abort(t);
5736 gboolean
5737 buffercmd_addkey(struct tab *t, guint keyval)
5739 int i, c, match ;
5740 char s[XT_BUFCMD_SZ];
5742 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers))) {
5743 buffercmd_abort(t);
5744 return (XT_CB_PASSTHROUGH);
5747 if (keyval == GDK_Escape) {
5748 buffercmd_abort(t);
5749 return (XT_CB_HANDLED);
5752 /* key with modifier or non-ascii character */
5753 if (!isascii(keyval)) {
5755 * XXX this looks wrong but fixes some sites like
5756 * http://www.seslisozluk.com/
5757 * that eat a shift or ctrl and end putting default focus in js
5758 * instead of ignoring the keystroke
5759 * so instead of return (XT_CB_PASSTHROUGH); eat the key
5761 return (XT_CB_HANDLED);
5764 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: adding key \"%c\" "
5765 "to buffer \"%s\"\n", keyval, bcmd);
5767 for (i = 0; i < LENGTH(bcmd); i++)
5768 if (bcmd[i] == '\0') {
5769 bcmd[i] = keyval;
5770 break;
5773 /* buffer full, ignore input */
5774 if (i >= LENGTH(bcmd) -1) {
5775 DNPRINTF(XT_D_BUFFERCMD, "buffercmd_addkey: buffer full\n");
5776 buffercmd_abort(t);
5777 return (XT_CB_HANDLED);
5780 if (t->sbe.buffercmd != NULL)
5781 gtk_label_set_text(GTK_LABEL(t->sbe.buffercmd), bcmd);
5783 /* find exact match */
5784 for (i = 0; i < LENGTH(buffercmds); i++)
5785 if (regexec(&buffercmds[i].cregex, bcmd,
5786 (size_t) 0, NULL, 0) == 0) {
5787 buffercmd_execute(t, &buffercmds[i]);
5788 goto done;
5791 /* find non exact matches to see if we need to abort ot not */
5792 for (i = 0, match = 0; i < LENGTH(buffercmds); i++) {
5793 DNPRINTF(XT_D_BUFFERCMD, "trying: %s\n", bcmd);
5794 c = -1;
5795 s[0] = '\0';
5796 if (buffercmds[i].precount == XT_PRE_MAYBE) {
5797 if (isdigit((unsigned char)bcmd[0])) {
5798 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5799 continue;
5800 } else {
5801 c = 0;
5802 if (sscanf(bcmd, "%s", s) == 0)
5803 continue;
5805 } else if (buffercmds[i].precount == XT_PRE_YES) {
5806 if (sscanf(bcmd, "%d%s", &c, s) == 0)
5807 continue;
5808 } else {
5809 if (sscanf(bcmd, "%s", s) == 0)
5810 continue;
5812 if (c == -1 && buffercmds[i].precount)
5813 continue;
5814 if (!strncmp(s, buffercmds[i].cmd, strlen(s)))
5815 match++;
5817 DNPRINTF(XT_D_BUFFERCMD, "got[%d] %d <%s>: %d %s\n",
5818 i, match, buffercmds[i].cmd, c, s);
5820 if (match == 0) {
5821 DNPRINTF(XT_D_BUFFERCMD, "aborting: %s\n", bcmd);
5822 buffercmd_abort(t);
5825 done:
5826 return (XT_CB_HANDLED);
5830 * XXX we were seeing a bunch of focus issues with the toplevel
5831 * main_window losing its is-active and has-toplevel-focus properties.
5832 * This is the most correct and portable solution we could come up with
5833 * without relying on calling internal GTK functions (which we
5834 * couldn't link to in Linux).
5836 #if GTK_CHECK_VERSION(3, 0, 0)
5837 void
5838 fake_focus_in(GtkWidget *w)
5840 if (fevent == NULL) {
5841 fevent = gdk_event_new(GDK_FOCUS_CHANGE);
5842 fevent->focus_change.window =
5843 gtk_widget_get_window(main_window);
5844 fevent->focus_change.type = GDK_FOCUS_CHANGE;
5845 fevent->focus_change.in = TRUE;
5847 gtk_widget_send_focus_change(main_window, fevent);
5849 #endif
5851 gboolean
5852 handle_keypress(struct tab *t, GdkEventKey *e, int entry)
5854 struct karg args;
5855 struct key_binding *k;
5858 * This sometimes gets randomly unset for whatever reason in GTK3.
5859 * If we're handling a keypress, the main window's is-active propery
5860 * *must* be true, or else many things will break.
5862 #if GTK_CHECK_VERSION(3, 0, 0)
5863 fake_focus_in(main_window);
5864 #endif
5866 /* handle keybindings if buffercmd is empty.
5867 if not empty, allow commands like C-n */
5868 if (bcmd[0] == '\0' || ((e->state & (CTRL | MOD1)) != 0))
5869 TAILQ_FOREACH(k, &kbl, entry)
5870 if (e->keyval == k->key
5871 && (entry ? k->use_in_entry : 1)) {
5872 /* when we are edditing eat ctrl/mod keys */
5873 if (edit_mode == XT_EM_VI &&
5874 t->mode == XT_MODE_INSERT &&
5875 (e->state & CTRL || e->state & MOD1))
5876 return (XT_CB_PASSTHROUGH);
5878 if (k->mask == 0) {
5879 if ((e->state & (CTRL | MOD1)) == 0)
5880 goto runcmd;
5881 } else if ((e->state & k->mask) == k->mask) {
5882 goto runcmd;
5886 if (!entry && ((e->state & (CTRL | MOD1)) == 0))
5887 return (buffercmd_addkey(t, e->keyval));
5889 return (XT_CB_PASSTHROUGH);
5891 runcmd:
5892 if (k->cmd[0] == ':') {
5893 args.i = ':';
5894 args.s = &k->cmd[1];
5895 return (command(t, &args));
5896 } else
5897 return (cmd_execute(t, k->cmd));
5901 wv_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
5903 char s[2];
5905 /* don't use w directly; use t->whatever instead */
5907 if (t == NULL) {
5908 show_oops(NULL, "wv_keypress_cb");
5909 return (XT_CB_PASSTHROUGH);
5912 hide_oops(t);
5914 if (t->mode_cb)
5915 return (t->mode_cb(t, e, t->mode_cb_data));
5917 DNPRINTF(XT_D_KEY, "wv_keypress_cb: mode %d keyval 0x%x mask "
5918 "0x%x tab %d\n", t->mode, e->keyval, e->state, t->tab_id);
5920 /* Hide buffers, if they are visible, with escape. */
5921 if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)) &&
5922 CLEAN(e->state) == 0 && e->keyval == GDK_Escape) {
5923 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
5924 hide_buffers(t);
5925 return (XT_CB_HANDLED);
5928 if ((CLEAN(e->state) == 0 && e->keyval == GDK_Tab) ||
5929 (CLEAN(e->state) == SHFT && e->keyval == GDK_Tab))
5930 /* something focussy is about to happen */
5931 return (XT_CB_PASSTHROUGH);
5933 /* check if we are some sort of text input thing in the dom */
5934 input_check_mode(t);
5936 if (t->mode == XT_MODE_PASSTHROUGH) {
5937 if (CLEAN(e->state) == 0 && e->keyval == GDK_Escape)
5938 t->mode = XT_MODE_COMMAND;
5939 return (XT_CB_PASSTHROUGH);
5940 } else if (t->mode == XT_MODE_COMMAND || t->mode == XT_MODE_HINT) {
5941 /* prefix input */
5942 snprintf(s, sizeof s, "%c", e->keyval);
5943 if (CLEAN(e->state) == 0 && isdigit((unsigned char)s[0]))
5944 cmd_prefix = 10 * cmd_prefix + atoi(s);
5945 return (handle_keypress(t, e, 0));
5946 } else {
5947 /* insert mode */
5948 return (handle_keypress(t, e, 1));
5951 /* NOTREACHED */
5952 return (XT_CB_PASSTHROUGH);
5955 gboolean
5956 hint_continue(struct tab *t)
5958 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5959 char *s;
5960 const gchar *errstr = NULL;
5961 gboolean rv = TRUE;
5962 int i;
5964 if (!(c[0] == '.' || c[0] == ','))
5965 goto done;
5966 if (strlen(c) == 1) {
5967 /* XXX should not happen */
5968 rv = TRUE;
5969 goto done;
5972 if (isdigit((unsigned char)c[1])) {
5973 /* numeric input */
5974 i = strtonum(&c[1], 1, 4096, &errstr);
5975 if (errstr) {
5976 show_oops(t, "invalid numerical hint %s", &c[1]);
5977 goto done;
5979 s = g_strdup_printf("hints.updateHints(%d);", i);
5980 run_script(t, s);
5981 g_free(s);
5982 } else {
5983 /* alphanumeric input */
5984 s = g_strdup_printf("hints.createHints('%s', '%c');",
5985 &c[1], c[0] == '.' ? 'f' : 'F');
5986 run_script(t, s);
5987 g_free(s);
5990 rv = FALSE;
5991 done:
5992 return (rv);
5995 gboolean
5996 search_continue(struct tab *t)
5998 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
5999 gboolean rv = FALSE;
6001 if (c[0] == ':' || c[0] == '.' || c[0] == ',')
6002 goto done;
6003 if (strlen(c) == 1) {
6004 webkit_web_view_unmark_text_matches(t->wv);
6005 goto done;
6008 if (c[0] == '/')
6009 t->search_forward = TRUE;
6010 else if (c[0] == '?')
6011 t->search_forward = FALSE;
6012 else
6013 goto done;
6015 rv = TRUE;
6016 done:
6017 return (rv);
6020 gboolean
6021 search_cb(struct tab *t)
6023 const gchar *c = gtk_entry_get_text(GTK_ENTRY(t->cmd));
6024 #if !GTK_CHECK_VERSION(3, 0, 0)
6025 GdkColor color;
6026 #endif
6028 if (search_continue(t) == FALSE)
6029 goto done;
6031 /* search */
6032 if (webkit_web_view_search_text(t->wv, &c[1], FALSE, t->search_forward,
6033 TRUE) == FALSE) {
6034 /* not found, mark red */
6035 #if GTK_CHECK_VERSION(3, 0, 0)
6036 gtk_widget_set_name(t->cmd, XT_CSS_RED);
6037 #else
6038 gdk_color_parse(XT_COLOR_RED, &color);
6039 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL, &color);
6040 #endif
6041 /* unmark and remove selection */
6042 webkit_web_view_unmark_text_matches(t->wv);
6043 /* my kingdom for a way to unselect text in webview */
6044 } else {
6045 /* found, highlight all */
6046 webkit_web_view_unmark_text_matches(t->wv);
6047 webkit_web_view_mark_text_matches(t->wv, &c[1], FALSE, 0);
6048 webkit_web_view_set_highlight_text_matches(t->wv, TRUE);
6049 #if GTK_CHECK_VERSION(3, 0, 0)
6050 gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
6051 #else
6052 gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
6053 &t->default_style->base[GTK_STATE_NORMAL]);
6054 #endif
6056 done:
6057 t->search_id = 0;
6058 return (FALSE);
6062 cmd_keyrelease_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6064 const gchar *c = gtk_entry_get_text(w);
6066 if (t == NULL) {
6067 show_oops(NULL, "cmd_keyrelease_cb invalid parameters");
6068 return (XT_CB_PASSTHROUGH);
6071 DNPRINTF(XT_D_CMD, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x tab %d\n",
6072 e->keyval, e->state, t->tab_id);
6074 /* hinting */
6075 if (!(e->keyval == GDK_Tab || e->keyval == GDK_ISO_Left_Tab)) {
6076 if (hint_continue(t) == FALSE)
6077 goto done;
6080 /* search */
6081 if (search_continue(t) == FALSE)
6082 goto done;
6084 /* if search length is > 4 then no longer play timeout games */
6085 if (strlen(c) > 4) {
6086 if (t->search_id) {
6087 g_source_remove(t->search_id);
6088 t->search_id = 0;
6090 search_cb(t);
6091 goto done;
6094 /* reestablish a new timer if the user types fast */
6095 if (t->search_id)
6096 g_source_remove(t->search_id);
6097 t->search_id = g_timeout_add(250, (GSourceFunc)search_cb, (gpointer)t);
6099 done:
6100 return (XT_CB_PASSTHROUGH);
6103 gboolean
6104 match_uri(const gchar *uri, const gchar *key) {
6105 gchar *voffset;
6106 size_t len;
6107 gboolean match = FALSE;
6109 len = strlen(key);
6111 if (!strncmp(key, uri, len))
6112 match = TRUE;
6113 else {
6114 voffset = strstr(uri, "/") + 2;
6115 if (!strncmp(key, voffset, len))
6116 match = TRUE;
6117 else if (g_str_has_prefix(voffset, "www.")) {
6118 voffset = voffset + strlen("www.");
6119 if (!strncmp(key, voffset, len))
6120 match = TRUE;
6124 return (match);
6127 gboolean
6128 match_session(const gchar *name, const gchar *key) {
6129 char *sub;
6131 sub = strcasestr(name, key);
6133 return sub == name;
6136 void
6137 cmd_getlist(int id, char *key)
6139 int i, dep, c = 0;
6140 struct history *h;
6141 struct session *s;
6143 if (key == NULL)
6144 return;
6146 if (id >= 0) {
6147 if (cmds[id].type & XT_URLARG) {
6148 RB_FOREACH_REVERSE(h, history_list, &hl)
6149 if (match_uri(h->uri, key)) {
6150 cmd_status.list[c] = (char *)h->uri;
6151 if (++c > 255)
6152 break;
6154 cmd_status.len = c;
6155 return;
6156 } else if (cmds[id].type & XT_SESSARG) {
6157 TAILQ_FOREACH(s, &sessions, entry)
6158 if (match_session(s->name, key)) {
6159 cmd_status.list[c] = (char *)s->name;
6160 if (++c > 255)
6161 break;
6163 cmd_status.len = c;
6164 return;
6165 } else if (cmds[id].type & XT_SETARG) {
6166 for (i = 0; i < get_settings_size(); i++)
6167 if (!strncmp(key, get_setting_name(i),
6168 strlen(key)))
6169 cmd_status.list[c++] =
6170 get_setting_name(i);
6171 cmd_status.len = c;
6172 return;
6176 dep = (id == -1) ? 0 : cmds[id].level + 1;
6178 for (i = id + 1; i < LENGTH(cmds); i++) {
6179 if (cmds[i].level < dep)
6180 break;
6181 if (cmds[i].level == dep && !strncmp(key, cmds[i].cmd,
6182 strlen(key)) && !isdigit((unsigned char)cmds[i].cmd[0]))
6183 cmd_status.list[c++] = cmds[i].cmd;
6187 cmd_status.len = c;
6190 char *
6191 cmd_getnext(int dir)
6193 cmd_status.index += dir;
6195 if (cmd_status.index < 0)
6196 cmd_status.index = cmd_status.len - 1;
6197 else if (cmd_status.index >= cmd_status.len)
6198 cmd_status.index = 0;
6200 return cmd_status.list[cmd_status.index];
6204 cmd_tokenize(char *s, char *tokens[])
6206 int i = 0;
6207 char *tok, *last = NULL;
6208 size_t len = strlen(s);
6209 bool blank;
6211 blank = len == 0 || (len > 0 && s[len - 1] == ' ');
6212 for (tok = strtok_r(s, " ", &last); tok && i < 3;
6213 tok = strtok_r(NULL, " ", &last), i++)
6214 tokens[i] = tok;
6216 if (blank && i < 3)
6217 tokens[i++] = "";
6219 return (i);
6222 void
6223 cmd_complete(struct tab *t, char *str, int dir)
6225 GtkEntry *w = GTK_ENTRY(t->cmd);
6226 int i, j, levels, c = 0, dep = 0, parent = -1;
6227 int matchcount = 0;
6228 char *tok, *match, *s = g_strdup(str);
6229 char *tokens[3] = {'\0'};
6230 char res[XT_MAX_URL_LENGTH + 32] = ":";
6231 char *sc = s;
6233 DNPRINTF(XT_D_CMD, "%s: complete %s\n", __func__, str);
6235 /* copy prefix*/
6236 for (i = 0; isdigit((unsigned char)s[i]); i++)
6237 res[i + 1] = s[i];
6239 for (; isspace((unsigned char)s[i]); i++)
6240 res[i + 1] = s[i];
6242 s += i;
6244 levels = cmd_tokenize(s, tokens);
6246 for (i = 0; i < levels - 1; i++) {
6247 tok = tokens[i];
6248 matchcount = 0;
6249 for (j = c; j < LENGTH(cmds); j++) {
6250 if (cmds[j].level < dep)
6251 break;
6252 if (cmds[j].level == dep && !strncmp(tok, cmds[j].cmd,
6253 strlen(tok))) {
6254 matchcount++;
6255 c = j + 1;
6256 if (strlen(tok) == strlen(cmds[j].cmd)) {
6257 matchcount = 1;
6258 break;
6263 if (matchcount == 1) {
6264 strlcat(res, tok, sizeof res);
6265 strlcat(res, " ", sizeof res);
6266 dep++;
6267 } else {
6268 g_free(sc);
6269 return;
6272 parent = c - 1;
6275 if (cmd_status.index == -1)
6276 cmd_getlist(parent, tokens[i]);
6278 if (cmd_status.len > 0) {
6279 match = cmd_getnext(dir);
6280 strlcat(res, match, sizeof res);
6281 gtk_entry_set_text(w, res);
6282 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6285 g_free(sc);
6288 char *
6289 parse_prefix_and_alias(const char *str, int *prefix)
6291 struct cmd_alias *c;
6292 char *s = g_strdup(str), *sc;
6294 g_strstrip(s);
6295 sc = s;
6297 if (isdigit((unsigned char)s[0])) {
6298 sscanf(s, "%d", prefix);
6299 while (isdigit((unsigned char)s[0]) ||
6300 isspace((unsigned char)s[0]))
6301 ++s;
6304 TAILQ_FOREACH(c, &cal, entry) {
6305 if (strncmp(s, c->alias, strlen(c->alias)))
6306 continue;
6308 if (strlen(s) == strlen(c->alias)) {
6309 g_free(sc);
6310 return (g_strdup(c->cmd));
6313 if (!isspace((unsigned char)s[strlen(c->alias)]))
6314 continue;
6316 s = g_strdup_printf("%s %s", c->cmd, &s[strlen(c->alias) + 1]);
6317 g_free(sc);
6318 return (s);
6320 s = g_strdup(s);
6321 g_free(sc);
6322 return (s);
6325 gboolean
6326 cmd_execute(struct tab *t, char *str)
6328 struct cmd *cmd = NULL;
6329 char *tok, *last = NULL, *s = str;
6330 int j = 0, len, c = 0, dep = 0, matchcount = 0;
6331 int prefix = -1, rv = XT_CB_PASSTHROUGH;
6332 struct karg arg = {0, NULL, -1};
6334 s = parse_prefix_and_alias(s, &prefix);
6336 for (tok = strtok_r(s, " ", &last); tok;
6337 tok = strtok_r(NULL, " ", &last)) {
6338 matchcount = 0;
6339 for (j = c; j < LENGTH(cmds); j++) {
6340 if (cmds[j].level < dep)
6341 break;
6342 len = (tok[strlen(tok) - 1] == '!') ? strlen(tok) - 1 :
6343 strlen(tok);
6344 if (cmds[j].level == dep &&
6345 !strncmp(tok, cmds[j].cmd, len)) {
6346 matchcount++;
6347 c = j + 1;
6348 cmd = &cmds[j];
6349 if (len == strlen(cmds[j].cmd)) {
6350 matchcount = 1;
6351 break;
6355 if (matchcount == 1) {
6356 if (cmd->type > 0)
6357 goto execute_cmd;
6358 dep++;
6359 } else {
6360 show_oops(t, "Invalid command: %s", str);
6361 goto done;
6364 execute_cmd:
6365 if (cmd == NULL) {
6366 show_oops(t, "Empty command");
6367 goto done;
6369 arg.i = cmd->arg;
6371 if (prefix != -1)
6372 arg.precount = prefix;
6373 else if (cmd_prefix > 0)
6374 arg.precount = cmd_prefix;
6376 if (j > 0 && !(cmd->type & XT_PREFIX) && arg.precount > -1) {
6377 show_oops(t, "No prefix allowed: %s", str);
6378 goto done;
6380 if (cmd->type > 1)
6381 arg.s = last ? g_strdup(last) : g_strdup("");
6382 if (cmd->type & XT_INTARG && last && strlen(last) > 0) {
6383 if (arg.s == NULL) {
6384 show_oops(t, "Invalid command");
6385 goto done;
6387 arg.precount = atoi(arg.s);
6388 if (arg.precount <= 0) {
6389 if (arg.s[0] == '0')
6390 show_oops(t, "Zero count");
6391 else
6392 show_oops(t, "Trailing characters");
6393 goto done;
6397 DNPRINTF(XT_D_CMD, "%s: prefix %d arg %s\n",
6398 __func__, arg.precount, arg.s);
6400 cmd->func(t, &arg);
6402 rv = XT_CB_HANDLED;
6403 done:
6404 if (j > 0)
6405 cmd_prefix = 0;
6406 g_free(s);
6407 if (arg.s)
6408 g_free(arg.s);
6410 return (rv);
6414 save_runtime_setting(const char *name, const char *val)
6416 struct stat sb;
6417 FILE *f;
6418 size_t linelen;
6419 int found = 0;
6420 char file[PATH_MAX];
6421 char delim[3] = { '\0', '\0', '\0' };
6422 char *line, *lt, *start;
6423 char *contents, *tmp;
6425 if (runtime_settings == NULL || strlen(runtime_settings) == 0)
6426 return (-1);
6428 snprintf(file, sizeof file, "%s" PS "%s", work_dir, runtime_settings);
6429 if (stat(file, &sb) || (f = fopen(file, "r+")) == NULL)
6430 return (-1);
6431 lt = g_strdup_printf("%s=%s", name, val);
6432 contents = g_strdup("");
6433 while (!feof(f)) {
6434 line = fparseln(f, &linelen, NULL, delim, 0);
6435 if (line == NULL || linelen == 0)
6436 continue;
6437 tmp = contents;
6438 start = g_strdup_printf("%s=", name);
6439 if (strstr(line, start) == NULL)
6440 contents = g_strdup_printf("%s%s\n", contents, line);
6441 else {
6442 found = 1;
6443 contents = g_strdup_printf("%s%s\n", contents, lt);
6445 g_free(tmp);
6446 g_free(start);
6447 free(line);
6448 line = NULL;
6450 if (found == 0) {
6451 tmp = contents;
6452 contents = g_strdup_printf("%s%s\n", contents, lt);
6453 g_free(tmp);
6455 if ((f = freopen(file, "w", f)) == NULL)
6456 return (-1);
6457 else {
6458 fputs(contents, f);
6459 fclose(f);
6461 g_free(lt);
6462 g_free(contents);
6464 return (0);
6468 entry_focus_cb(GtkWidget *w, GdkEvent e, struct tab *t)
6471 * This sometimes gets randomly unset for whatever reason in GTK3,
6472 * causing a GtkEntry's text cursor becomes invisible. When we focus
6473 * a GtkEntry, be sure to manually reset the main window's is-active
6474 * property so the cursor is shown correctly.
6476 #if GTK_CHECK_VERSION(3, 0, 0)
6477 fake_focus_in(main_window);
6478 #endif
6479 return (XT_CB_PASSTHROUGH);
6483 entry_key_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6485 if (t == NULL) {
6486 show_oops(NULL, "entry_key_cb invalid parameters");
6487 return (XT_CB_PASSTHROUGH);
6490 DNPRINTF(XT_D_CMD, "entry_key_cb: keyval 0x%x mask 0x%x tab %d\n",
6491 e->keyval, e->state, t->tab_id);
6493 hide_oops(t);
6495 if (e->keyval == GDK_Escape) {
6496 /* don't use focus_webview(t) because we want to type :cmds */
6497 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
6500 return (handle_keypress(t, e, 1));
6503 struct command_entry *
6504 history_prev(struct command_list *l, struct command_entry *at)
6506 if (at == NULL)
6507 at = TAILQ_LAST(l, command_list);
6508 else {
6509 at = TAILQ_PREV(at, command_list, entry);
6510 if (at == NULL)
6511 at = TAILQ_LAST(l, command_list);
6514 return (at);
6517 struct command_entry *
6518 history_next(struct command_list *l, struct command_entry *at)
6520 if (at == NULL)
6521 at = TAILQ_FIRST(l);
6522 else {
6523 at = TAILQ_NEXT(at, entry);
6524 if (at == NULL)
6525 at = TAILQ_FIRST(l);
6528 return (at);
6532 cmd_keypress_cb(GtkEntry *w, GdkEventKey *e, struct tab *t)
6534 int rv = XT_CB_HANDLED;
6535 const gchar *c = gtk_entry_get_text(w);
6536 char *s;
6538 if (t == NULL) {
6539 show_oops(NULL, "cmd_keypress_cb parameters");
6540 return (XT_CB_PASSTHROUGH);
6543 DNPRINTF(XT_D_CMD, "cmd_keypress_cb: keyval 0x%x mask 0x%x tab %d\n",
6544 e->keyval, e->state, t->tab_id);
6546 /* sanity */
6547 if (c == NULL)
6548 e->keyval = GDK_Escape;
6549 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6550 c[0] == '.' || c[0] == ','))
6551 e->keyval = GDK_Escape;
6553 if (e->keyval != GDK_Tab && e->keyval != GDK_Shift_L &&
6554 e->keyval != GDK_ISO_Left_Tab)
6555 cmd_status.index = -1;
6557 switch (e->keyval) {
6558 case GDK_Tab:
6559 if (c[0] == ':')
6560 cmd_complete(t, (char *)&c[1], 1);
6561 else if (c[0] == '.' || c[0] == ',')
6562 run_script(t, "hints.focusNextHint();");
6563 goto done;
6564 case GDK_ISO_Left_Tab:
6565 if (c[0] == ':')
6566 cmd_complete(t, (char *)&c[1], -1);
6567 else if (c[0] == '.' || c[0] == ',')
6568 run_script(t, "hints.focusPreviousHint();");
6569 goto done;
6570 case GDK_Down:
6571 if (c[0] == '?') {
6572 if ((search_at = history_next(&shl, search_at))) {
6573 search_at->line[0] = c[0];
6574 gtk_entry_set_text(w, search_at->line);
6575 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6577 } else if (c[0] == '/') {
6578 if ((search_at = history_prev(&shl, search_at))) {
6579 search_at->line[0] = c[0];
6580 gtk_entry_set_text(w, search_at->line);
6581 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6583 } else if (c[0] == ':') {
6584 if ((history_at = history_prev(&chl, history_at))) {
6585 history_at->line[0] = c[0];
6586 gtk_entry_set_text(w, history_at->line);
6587 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6590 goto done;
6591 case GDK_Up:
6592 if (c[0] == '/') {
6593 if ((search_at = history_next(&shl, search_at))) {
6594 search_at->line[0] = c[0];
6595 gtk_entry_set_text(w, search_at->line);
6596 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6598 } else if (c[0] == '?') {
6599 if ((search_at = history_prev(&shl, search_at))) {
6600 search_at->line[0] = c[0];
6601 gtk_entry_set_text(w, search_at->line);
6602 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6604 } if (c[0] == ':') {
6605 if ((history_at = history_next(&chl, history_at))) {
6606 history_at->line[0] = c[0];
6607 gtk_entry_set_text(w, history_at->line);
6608 gtk_editable_set_position(GTK_EDITABLE(w), -1);
6611 goto done;
6612 case GDK_BackSpace:
6613 if (!(!strcmp(c, ":") || !strcmp(c, "/") || !strcmp(c, "?") ||
6614 !strcmp(c, ".") || !strcmp(c, ","))) {
6615 /* see if we are doing hinting and reset it */
6616 if (c[0] == '.' || c[0] == ',') {
6617 /* recreate hints */
6618 s = g_strdup_printf("hints.createHints('', "
6619 "'%c');", c[0] == '.' ? 'f' : 'F');
6620 run_script(t, s);
6621 g_free(s);
6623 break;
6626 /* FALLTHROUGH */
6627 case GDK_Escape:
6628 hide_cmd(t);
6630 /* cancel search */
6631 if (c != NULL && (c[0] == '/' || c[0] == '?'))
6632 webkit_web_view_unmark_text_matches(t->wv);
6634 /* no need to cancel hints */
6635 goto done;
6638 rv = XT_CB_PASSTHROUGH;
6639 done:
6640 return (rv);
6643 void
6644 wv_popup_activ_cb(GtkMenuItem *menu, struct tab *t)
6646 GtkAction *a = NULL;
6647 GtkClipboard *clipboard, *primary;
6648 const gchar *name, *uri;
6650 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
6651 if (a == NULL)
6652 return;
6653 name = gtk_action_get_name(a);
6655 DNPRINTF(XT_D_CMD, "wv_popup_activ_cb: tab %d action %s\n",
6656 t->tab_id, name);
6659 * context-menu-action-3 copy link location
6660 * context-menu-action-7 copy image address
6661 * context-menu-action-2030 copy video link location
6664 /* copy actions */
6665 if ((g_strcmp0(name, "context-menu-action-3") == 0) ||
6666 (g_strcmp0(name, "context-menu-action-7") == 0) ||
6667 (g_strcmp0(name, "context-menu-action-2030") == 0)) {
6668 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
6669 primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
6670 uri = gtk_clipboard_wait_for_text(clipboard);
6671 if (uri == NULL)
6672 return;
6673 gtk_clipboard_set_text(primary, uri, -1);
6677 void
6678 wv_popup_cb(WebKitWebView *wview, GtkMenu *menu, struct tab *t)
6680 GList *items, *l;
6682 DNPRINTF(XT_D_CMD, "wv_popup_cb: tab %d\n", t->tab_id);
6684 items = gtk_container_get_children(GTK_CONTAINER(menu));
6685 for (l = items; l; l = l->next)
6686 g_signal_connect(l->data, "activate",
6687 G_CALLBACK(wv_popup_activ_cb), t);
6688 g_list_free(items);
6691 void
6692 cmd_popup_cb(GtkEntry *entry, GtkMenu *menu, struct tab *t)
6694 /* popup menu enabled */
6695 t->popup = 1;
6699 cmd_focusout_cb(GtkWidget *w, GdkEventFocus *e, struct tab *t)
6701 if (t == NULL) {
6702 show_oops(NULL, "cmd_focusout_cb invalid parameters");
6703 return (XT_CB_PASSTHROUGH);
6706 DNPRINTF(XT_D_CMD, "cmd_focusout_cb: tab %d popup %d\n",
6707 t->tab_id, t->popup);
6709 /* if popup is enabled don't lose focus */
6710 if (t->popup) {
6711 t->popup = 0;
6712 return (XT_CB_PASSTHROUGH);
6715 hide_cmd(t);
6716 hide_oops(t);
6717 disable_hints(t);
6719 return (XT_CB_PASSTHROUGH);
6722 void
6723 cmd_hide_cb(GtkWidget *w, struct tab *t)
6725 if (t == NULL) {
6726 show_oops(NULL, "%s: invalid parameters", __func__);
6727 return;
6730 if (show_url == 0 || t->focus_wv)
6731 focus_webview(t);
6732 else
6733 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
6736 void
6737 cmd_activate_cb(GtkEntry *entry, struct tab *t)
6739 char *s;
6740 const gchar *c = gtk_entry_get_text(entry);
6742 if (t == NULL) {
6743 show_oops(NULL, "cmd_activate_cb invalid parameters");
6744 return;
6747 DNPRINTF(XT_D_CMD, "cmd_activate_cb: tab %d %s\n", t->tab_id, c);
6749 /* sanity */
6750 if (c == NULL)
6751 goto done;
6752 else if (!(c[0] == ':' || c[0] == '/' || c[0] == '?' ||
6753 c[0] == '.' || c[0] == ','))
6754 goto done;
6755 if (strlen(c) < 1)
6756 goto done;
6757 s = (char *)&c[1];
6759 if (c[0] == '/' || c[0] == '?') {
6760 /* see if there is a timer pending */
6761 if (t->search_id) {
6762 g_source_remove(t->search_id);
6763 t->search_id = 0;
6764 search_cb(t);
6767 if (t->search_text) {
6768 g_free(t->search_text);
6769 t->search_text = NULL;
6772 t->search_text = g_strdup(s);
6773 if (global_search)
6774 g_free(global_search);
6775 global_search = g_strdup(s);
6776 t->search_forward = c[0] == '/';
6778 history_add(&shl, search_file, s, &search_history_count);
6779 } else if (c[0] == '.' || c[0] == ',') {
6780 run_script(t, "hints.fire();");
6781 /* XXX history for link following? */
6782 } else if (c[0] == ':') {
6783 history_add(&chl, command_file, s, &cmd_history_count);
6784 /* can't call hide_cmd after cmd_execute */
6785 hide_cmd(t);
6786 cmd_execute(t, s);
6787 return;
6790 done:
6791 hide_cmd(t);
6794 void
6795 backward_cb(GtkWidget *w, struct tab *t)
6797 struct karg a;
6799 if (t == NULL) {
6800 show_oops(NULL, "backward_cb invalid parameters");
6801 return;
6804 DNPRINTF(XT_D_NAV, "backward_cb: tab %d\n", t->tab_id);
6806 a.i = XT_NAV_BACK;
6807 navaction(t, &a);
6810 void
6811 forward_cb(GtkWidget *w, struct tab *t)
6813 struct karg a;
6815 if (t == NULL) {
6816 show_oops(NULL, "forward_cb invalid parameters");
6817 return;
6820 DNPRINTF(XT_D_NAV, "forward_cb: tab %d\n", t->tab_id);
6822 a.i = XT_NAV_FORWARD;
6823 navaction(t, &a);
6826 void
6827 home_cb(GtkWidget *w, struct tab *t)
6829 if (t == NULL) {
6830 show_oops(NULL, "home_cb invalid parameters");
6831 return;
6834 DNPRINTF(XT_D_NAV, "home_cb: tab %d\n", t->tab_id);
6836 load_uri(t, home);
6839 void
6840 stop_cb(GtkWidget *w, struct tab *t)
6842 WebKitWebFrame *frame;
6844 if (t == NULL) {
6845 show_oops(NULL, "stop_cb invalid parameters");
6846 return;
6849 DNPRINTF(XT_D_NAV, "stop_cb: tab %d\n", t->tab_id);
6851 frame = webkit_web_view_get_main_frame(t->wv);
6852 if (frame == NULL) {
6853 show_oops(t, "stop_cb: no frame");
6854 return;
6857 webkit_web_frame_stop_loading(frame);
6858 abort_favicon_download(t);
6861 void
6862 setup_webkit(struct tab *t)
6864 if (is_g_object_setting(G_OBJECT(t->settings), "enable-dns-prefetching"))
6865 g_object_set(G_OBJECT(t->settings), "enable-dns-prefetching",
6866 FALSE, (char *)NULL);
6867 else
6868 warnx("webkit does not have \"enable-dns-prefetching\" property");
6869 g_object_set(G_OBJECT(t->settings), "default-encoding", encoding,
6870 (char *)NULL);
6871 g_object_set(G_OBJECT(t->settings),
6872 "enable-scripts", enable_scripts, (char *)NULL);
6873 g_object_set(G_OBJECT(t->settings),
6874 "enable-plugins", enable_plugins, (char *)NULL);
6875 g_object_set(G_OBJECT(t->settings),
6876 "javascript-can-open-windows-automatically",
6877 js_auto_open_windows, (char *)NULL);
6878 g_object_set(G_OBJECT(t->settings),
6879 "enable-html5-database", FALSE, (char *)NULL);
6880 g_object_set(G_OBJECT(t->settings),
6881 "enable-html5-local-storage", enable_localstorage, (char *)NULL);
6882 g_object_set(G_OBJECT(t->settings),
6883 "enable_spell_checking", enable_spell_checking, (char *)NULL);
6884 g_object_set(G_OBJECT(t->settings),
6885 "spell_checking_languages", spell_check_languages, (char *)NULL);
6886 g_object_set(G_OBJECT(t->settings),
6887 "enable-developer-extras", TRUE, (char *)NULL);
6888 g_object_set(G_OBJECT(t->wv),
6889 "full-content-zoom", TRUE, (char *)NULL);
6890 g_object_set(G_OBJECT(t->settings),
6891 "auto-load-images", auto_load_images, (char *)NULL);
6892 if (is_g_object_setting(G_OBJECT(t->settings),
6893 "enable-display-of-insecure-content"))
6894 g_object_set(G_OBJECT(t->settings),
6895 "enable-display-of-insecure-content",
6896 allow_insecure_content, (char *)NULL);
6897 if (is_g_object_setting(G_OBJECT(t->settings),
6898 "enable-running-of-insecure-content"))
6899 g_object_set(G_OBJECT(t->settings),
6900 "enable-running-of-insecure-content",
6901 allow_insecure_scripts, (char *)NULL);
6903 webkit_web_view_set_settings(t->wv, t->settings);
6906 gboolean
6907 update_statusbar_position(GtkAdjustment* adjustment, gpointer data)
6909 struct tab *ti, *t = NULL;
6910 gdouble view_size, value, max;
6911 gchar *position;
6913 TAILQ_FOREACH(ti, &tabs, entry)
6914 if (ti->tab_id == gtk_notebook_get_current_page(notebook)) {
6915 t = ti;
6916 break;
6919 if (t == NULL)
6920 return FALSE;
6922 if (adjustment == NULL)
6923 adjustment = gtk_scrolled_window_get_vadjustment(
6924 GTK_SCROLLED_WINDOW(t->browser_win));
6926 view_size = gtk_adjustment_get_page_size(adjustment);
6927 value = gtk_adjustment_get_value(adjustment);
6928 max = gtk_adjustment_get_upper(adjustment) - view_size;
6930 if (max == 0)
6931 position = g_strdup("All");
6932 else if (value == max)
6933 position = g_strdup("Bot");
6934 else if (value == 0)
6935 position = g_strdup("Top");
6936 else
6937 position = g_strdup_printf("%d%%", (int) ((value / max) * 100));
6939 if (t->sbe.position != NULL)
6940 gtk_label_set_text(GTK_LABEL(t->sbe.position), position);
6941 g_free(position);
6943 return (TRUE);
6946 GtkWidget *
6947 create_window(const gchar *name)
6949 GtkWidget *w;
6951 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
6952 if (window_maximize)
6953 gtk_window_maximize(GTK_WINDOW(w));
6954 else
6955 gtk_window_set_default_size(GTK_WINDOW(w), window_width, window_height);
6956 gtk_widget_set_name(w, name);
6957 gtk_window_set_wmclass(GTK_WINDOW(w), name, "Xombrero");
6959 return (w);
6962 GtkWidget *
6963 create_browser(struct tab *t)
6965 GtkWidget *w;
6966 GtkAdjustment *adjustment;
6968 if (t == NULL) {
6969 show_oops(NULL, "create_browser invalid parameters");
6970 return (NULL);
6973 w = gtk_scrolled_window_new(NULL, NULL);
6974 gtk_widget_set_can_focus(w, FALSE);
6975 t->adjust_h = gtk_scrolled_window_get_hadjustment(
6976 GTK_SCROLLED_WINDOW(w));
6977 t->adjust_v = gtk_scrolled_window_get_vadjustment(
6978 GTK_SCROLLED_WINDOW(w));
6979 #if !GTK_CHECK_VERSION(3, 0, 0)
6980 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
6981 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
6982 #endif
6985 t->wv = WEBKIT_WEB_VIEW(webkit_web_view_new());
6986 gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(t->wv));
6988 /* set defaults */
6989 t->settings = webkit_web_view_get_settings(t->wv);
6990 t->stylesheet = g_strdup(stylesheet);
6991 t->load_images = auto_load_images;
6993 adjustment =
6994 gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(w));
6995 g_signal_connect(G_OBJECT(adjustment), "value-changed",
6996 G_CALLBACK(update_statusbar_position), NULL);
6998 setup_webkit(t);
6999 setup_inspector(t);
7001 return (w);
7004 GtkWidget *
7005 create_kiosk_toolbar(struct tab *t)
7007 GtkWidget *toolbar = NULL;
7008 GtkBox *b;
7010 #if GTK_CHECK_VERSION(3, 0, 0)
7011 toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
7012 gtk_widget_set_name(toolbar, "toolbar");
7013 #else
7014 toolbar = gtk_hbox_new(FALSE, 1);
7015 #endif
7017 b = GTK_BOX(toolbar);
7018 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
7020 /* backward button */
7021 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
7022 gtk_widget_set_sensitive(t->backward, FALSE);
7023 g_signal_connect(G_OBJECT(t->backward), "clicked",
7024 G_CALLBACK(backward_cb), t);
7025 gtk_box_pack_start(b, t->backward, TRUE, TRUE, 0);
7027 /* forward button */
7028 t->forward = create_button("Forward", GTK_STOCK_GO_FORWARD, 0);
7029 gtk_widget_set_sensitive(t->forward, FALSE);
7030 g_signal_connect(G_OBJECT(t->forward), "clicked",
7031 G_CALLBACK(forward_cb), t);
7032 gtk_box_pack_start(b, t->forward, TRUE, TRUE, 0);
7034 /* home button */
7035 t->gohome = create_button("Home", GTK_STOCK_HOME, 0);
7036 gtk_widget_set_sensitive(t->gohome, true);
7037 g_signal_connect(G_OBJECT(t->gohome), "clicked",
7038 G_CALLBACK(home_cb), t);
7039 gtk_box_pack_start(b, t->gohome, TRUE, TRUE, 0);
7042 * Create widgets but don't use them. This is just so we don't get
7043 * loads of errors when trying to do stuff with them. Sink the floating
7044 * reference here, and do a manual unreference when the tab is deleted.
7046 t->uri_entry = gtk_entry_new();
7047 g_object_ref_sink(G_OBJECT(t->uri_entry));
7048 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
7049 G_CALLBACK(entry_focus_cb), t);
7050 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
7051 g_object_ref_sink(G_OBJECT(t->stop));
7052 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
7053 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
7054 g_object_ref_sink(G_OBJECT(t->stop));
7056 #if !GTK_CHECK_VERSION(3, 0, 0)
7057 t->default_style = gtk_rc_get_style(t->uri_entry);
7058 #endif
7060 return (toolbar);
7063 GtkWidget *
7064 create_toolbar(struct tab *t)
7066 GtkWidget *toolbar = NULL;
7067 GtkBox *b;
7070 * t->stop, t->js_toggle, and t->uri_entry are shared by the kiosk
7071 * toolbar, but not shown or used. We still create them there so we can
7072 * avoid loads of warnings when trying to use them. Instead, we sink
7073 * the floating reference here, and do the final unreference when
7074 * deleting the tab.
7077 #if GTK_CHECK_VERSION(3, 0, 0)
7078 toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
7079 gtk_widget_set_name(toolbar, "toolbar");
7080 #else
7081 toolbar = gtk_hbox_new(FALSE, 1);
7082 #endif
7084 b = GTK_BOX(toolbar);
7085 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
7087 /* backward button */
7088 t->backward = create_button("Back", GTK_STOCK_GO_BACK, 0);
7089 gtk_widget_set_sensitive(t->backward, FALSE);
7090 g_signal_connect(G_OBJECT(t->backward), "clicked",
7091 G_CALLBACK(backward_cb), t);
7092 gtk_box_pack_start(b, t->backward, FALSE, FALSE, 0);
7094 /* forward button */
7095 t->forward = create_button("Forward",GTK_STOCK_GO_FORWARD, 0);
7096 gtk_widget_set_sensitive(t->forward, FALSE);
7097 g_signal_connect(G_OBJECT(t->forward), "clicked",
7098 G_CALLBACK(forward_cb), t);
7099 gtk_box_pack_start(b, t->forward, FALSE, FALSE, 0);
7101 /* stop button */
7102 t->stop = create_button("Stop", GTK_STOCK_STOP, 0);
7103 g_object_ref_sink(G_OBJECT(t->stop));
7104 gtk_widget_set_sensitive(t->stop, FALSE);
7105 g_signal_connect(G_OBJECT(t->stop), "clicked", G_CALLBACK(stop_cb), t);
7106 gtk_box_pack_start(b, t->stop, FALSE, FALSE, 0);
7108 /* JS button */
7109 t->js_toggle = create_button("JS-Toggle", enable_scripts ?
7110 GTK_STOCK_MEDIA_PLAY : GTK_STOCK_MEDIA_PAUSE, 0);
7111 g_object_ref_sink(G_OBJECT(t->js_toggle));
7112 gtk_widget_set_sensitive(t->js_toggle, TRUE);
7113 g_signal_connect(G_OBJECT(t->js_toggle), "clicked",
7114 G_CALLBACK(js_toggle_cb), t);
7115 gtk_box_pack_start(b, t->js_toggle, FALSE, FALSE, 0);
7117 /* toggle proxy button */
7118 t->proxy_toggle = create_button("Proxy-Toggle", proxy_uri ?
7119 GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT, 0);
7120 /* override icons */
7121 if (proxy_uri)
7122 button_set_file(t->proxy_toggle, "torenabled.ico");
7123 else
7124 button_set_file(t->proxy_toggle, "tordisabled.ico");
7125 gtk_widget_set_sensitive(t->proxy_toggle, TRUE);
7126 g_signal_connect(G_OBJECT(t->proxy_toggle), "clicked",
7127 G_CALLBACK(proxy_toggle_cb), t);
7128 gtk_box_pack_start(b, t->proxy_toggle, FALSE, FALSE, 0);
7130 t->uri_entry = gtk_entry_new();
7131 g_object_ref_sink(G_OBJECT(t->uri_entry));
7132 g_signal_connect(G_OBJECT(t->uri_entry), "activate",
7133 G_CALLBACK(activate_uri_entry_cb), t);
7134 g_signal_connect(G_OBJECT(t->uri_entry), "key-press-event",
7135 G_CALLBACK(entry_key_cb), t);
7136 g_signal_connect(G_OBJECT(t->uri_entry), "focus-in-event",
7137 G_CALLBACK(entry_focus_cb), t);
7138 completion_add(t);
7139 gtk_box_pack_start(b, t->uri_entry, TRUE, TRUE, 0);
7141 /* search entry */
7142 t->search_entry = gtk_entry_new();
7143 gtk_entry_set_width_chars(GTK_ENTRY(t->search_entry), 30);
7144 g_signal_connect(G_OBJECT(t->search_entry), "activate",
7145 G_CALLBACK(activate_search_entry_cb), t);
7146 g_signal_connect(G_OBJECT(t->search_entry), "key-press-event",
7147 G_CALLBACK(entry_key_cb), t);
7148 g_signal_connect(G_OBJECT(t->search_entry), "focus-in-event",
7149 G_CALLBACK(entry_focus_cb), t);
7150 gtk_box_pack_start(b, t->search_entry, FALSE, FALSE, 0);
7152 #if !GTK_CHECK_VERSION(3, 0, 0)
7153 t->default_style = gtk_rc_get_style(t->uri_entry);
7154 #endif
7156 return (toolbar);
7159 GtkWidget *
7160 create_buffers(struct tab *t)
7162 GtkCellRenderer *renderer;
7163 GtkWidget *view;
7165 view = gtk_tree_view_new();
7167 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
7169 renderer = gtk_cell_renderer_text_new();
7170 gtk_tree_view_insert_column_with_attributes
7171 (GTK_TREE_VIEW(view), -1, "Id", renderer, "text", COL_ID, (char *)NULL);
7173 renderer = gtk_cell_renderer_pixbuf_new();
7174 gtk_tree_view_insert_column_with_attributes
7175 (GTK_TREE_VIEW(view), -1, "Favicon", renderer, "pixbuf", COL_FAVICON,
7176 (char *)NULL);
7178 renderer = gtk_cell_renderer_text_new();
7179 gtk_tree_view_insert_column_with_attributes
7180 (GTK_TREE_VIEW(view), -1, "Title", renderer, "text", COL_TITLE,
7181 (char *)NULL);
7183 gtk_tree_view_set_model
7184 (GTK_TREE_VIEW(view), GTK_TREE_MODEL(buffers_store));
7186 return (view);
7189 void
7190 row_activated_cb(GtkTreeView *view, GtkTreePath *path,
7191 GtkTreeViewColumn *col, struct tab *t)
7193 GtkTreeIter iter;
7194 guint id;
7196 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7198 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter,
7199 path)) {
7200 gtk_tree_model_get
7201 (GTK_TREE_MODEL(buffers_store), &iter, COL_ID, &id, -1);
7202 set_current_tab(id - 1);
7205 hide_buffers(t);
7208 /* after tab reordering/creation/removal */
7209 void
7210 recalc_tabs(void)
7212 struct tab *t;
7214 TAILQ_FOREACH(t, &tabs, entry)
7215 t->tab_id = gtk_notebook_page_num(notebook, t->vbox);
7218 void
7219 update_statusbar_tabs(struct tab *t)
7221 int tab_id, max_tab_id;
7222 char s[16];
7224 if (t != NULL)
7225 tab_id = t->tab_id;
7226 else
7227 tab_id = gtk_notebook_get_current_page(notebook);
7229 max_tab_id = gtk_notebook_get_n_pages(notebook);
7230 snprintf(s, sizeof s, "%d/%d", tab_id + 1, max_tab_id);
7232 if (t == NULL)
7233 t = get_current_tab();
7235 if (t != NULL && t->sbe.tabs != NULL)
7236 gtk_label_set_text(GTK_LABEL(t->sbe.tabs), s);
7239 /* after active tab change */
7240 void
7241 recolor_compact_tabs(void)
7243 struct tab *t;
7244 int curid = 0;
7245 #if !GTK_CHECK_VERSION(3, 0, 0)
7246 GdkColor color_active, color_inactive;
7248 gdk_color_parse(XT_COLOR_CT_ACTIVE, &color_active);
7249 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color_inactive);
7250 #endif
7251 curid = gtk_notebook_get_current_page(notebook);
7253 TAILQ_FOREACH(t, &tabs, entry) {
7254 #if GTK_CHECK_VERSION(3, 0, 0)
7255 if (t->tab_id == curid)
7256 gtk_widget_set_name(t->tab_elems.label, XT_CSS_ACTIVE);
7257 else
7258 gtk_widget_set_name(t->tab_elems.label, "");
7259 #else
7260 if (t->tab_id == curid)
7261 gtk_widget_modify_fg(t->tab_elems.label,
7262 GTK_STATE_NORMAL, &color_active);
7263 else
7264 gtk_widget_modify_fg(t->tab_elems.label,
7265 GTK_STATE_NORMAL, &color_inactive);
7266 #endif
7270 void
7271 set_current_tab(int page_num)
7273 buffercmd_abort(get_current_tab());
7274 gtk_notebook_set_current_page(notebook, page_num);
7275 recolor_compact_tabs();
7276 update_statusbar_tabs(NULL);
7280 undo_close_tab_save(struct tab *t)
7282 int m, n;
7283 const gchar *uri;
7284 struct undo *u1, *u2;
7285 GList *items;
7286 GList *item;
7287 WebKitWebHistoryItem *el;
7289 if ((uri = get_uri(t)) == NULL)
7290 return (1);
7292 u1 = g_malloc0(sizeof(struct undo));
7293 u1->uri = g_strdup(uri);
7295 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7297 m = webkit_web_back_forward_list_get_forward_length(t->bfl);
7298 n = webkit_web_back_forward_list_get_back_length(t->bfl);
7299 u1->back = n;
7301 /* forward history */
7302 items = webkit_web_back_forward_list_get_forward_list_with_limit(t->bfl, m);
7303 for (item = g_list_first(items); item; item = item->next) {
7304 u1->history = g_list_prepend(u1->history,
7305 webkit_web_history_item_copy(item->data));
7307 g_list_free(items);
7309 /* current item */
7310 if (m) {
7311 el = webkit_web_back_forward_list_get_current_item(t->bfl);
7312 u1->history = g_list_prepend(u1->history,
7313 webkit_web_history_item_copy(el));
7316 /* back history */
7317 items = webkit_web_back_forward_list_get_back_list_with_limit(t->bfl, n);
7318 for (item = g_list_first(items); item; item = item->next) {
7319 u1->history = g_list_prepend(u1->history,
7320 webkit_web_history_item_copy(item->data));
7322 g_list_free(items);
7324 TAILQ_INSERT_HEAD(&undos, u1, entry);
7326 if (undo_count > XT_MAX_UNDO_CLOSE_TAB) {
7327 u2 = TAILQ_LAST(&undos, undo_tailq);
7328 TAILQ_REMOVE(&undos, u2, entry);
7329 g_free(u2->uri);
7330 g_list_free(u2->history);
7331 g_free(u2);
7332 } else
7333 undo_count++;
7335 return (0);
7338 void
7339 delete_tab(struct tab *t)
7341 struct karg a;
7343 DNPRINTF(XT_D_TAB, "delete_tab: %p\n", t);
7345 if (t == NULL)
7346 return;
7348 TAILQ_REMOVE(&tabs, t, entry);
7349 buffercmd_abort(t);
7351 /* Halt all webkit activity. */
7352 abort_favicon_download(t);
7353 webkit_web_view_stop_loading(t->wv);
7355 /* Save the tab, so we can undo the close. */
7356 undo_close_tab_save(t);
7358 /* just in case */
7359 if (t->search_id)
7360 g_source_remove(t->search_id);
7362 /* session key */
7363 if (t->session_key)
7364 g_free(t->session_key);
7366 /* inspector */
7367 bzero(&a, sizeof a);
7368 a.i = XT_INS_CLOSE;
7369 inspector_cmd(t, &a);
7371 if (browser_mode == XT_BM_KIOSK) {
7372 gtk_widget_destroy(t->uri_entry);
7373 gtk_widget_destroy(t->stop);
7374 gtk_widget_destroy(t->js_toggle);
7377 /* widgets not shown in the kiosk toolbar */
7378 gtk_widget_destroy(t->stop);
7379 g_object_unref(G_OBJECT(t->stop));
7380 gtk_widget_destroy(t->js_toggle);
7381 g_object_unref(G_OBJECT(t->js_toggle));
7382 gtk_widget_destroy(t->uri_entry);
7383 g_object_unref(G_OBJECT(t->uri_entry));
7385 g_object_unref(G_OBJECT(t->completion));
7386 if (t->item)
7387 g_object_unref(t->item);
7389 gtk_widget_destroy(t->tab_elems.eventbox);
7390 gtk_widget_destroy(t->vbox);
7392 g_free(t->stylesheet);
7393 g_free(t->tmp_uri);
7394 g_free(t->status);
7395 g_free(t);
7396 t = NULL;
7398 if (TAILQ_EMPTY(&tabs)) {
7399 if (browser_mode == XT_BM_KIOSK)
7400 create_new_tab(home, NULL, 1, -1);
7401 else
7402 create_new_tab(NULL, NULL, 1, -1);
7405 /* recreate session */
7406 if (session_autosave) {
7407 bzero(&a, sizeof a);
7408 a.s = NULL;
7409 save_tabs(NULL, &a);
7412 recalc_tabs();
7413 recolor_compact_tabs();
7416 void
7417 update_statusbar_zoom(struct tab *t)
7419 gfloat zoom;
7420 char s[16] = { '\0' };
7422 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7423 if ((zoom <= 0.99 || zoom >= 1.01))
7424 snprintf(s, sizeof s, "%d%%", (int)(zoom * 100));
7425 if (t->sbe.zoom != NULL)
7426 gtk_label_set_text(GTK_LABEL(t->sbe.zoom), s);
7429 void
7430 setzoom_webkit(struct tab *t, int adjust)
7432 #define XT_ZOOMPERCENT 0.04
7434 gfloat zoom;
7436 if (t == NULL) {
7437 show_oops(NULL, "setzoom_webkit invalid parameters");
7438 return;
7441 g_object_get(G_OBJECT(t->wv), "zoom-level", &zoom, (char *)NULL);
7442 if (adjust == XT_ZOOM_IN)
7443 zoom += XT_ZOOMPERCENT;
7444 else if (adjust == XT_ZOOM_OUT)
7445 zoom -= XT_ZOOMPERCENT;
7446 else if (adjust > 0)
7447 zoom = default_zoom_level + adjust / 100.0 - 1.0;
7448 else {
7449 show_oops(t, "setzoom_webkit invalid zoom value");
7450 return;
7453 if (zoom < XT_ZOOMPERCENT)
7454 zoom = XT_ZOOMPERCENT;
7455 g_object_set(G_OBJECT(t->wv), "zoom-level", zoom, (char *)NULL);
7456 update_statusbar_zoom(t);
7459 gboolean
7460 tab_clicked_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
7462 struct tab *t = (struct tab *) data;
7464 DNPRINTF(XT_D_TAB, "tab_clicked_cb: tab: %d\n", t->tab_id);
7466 switch (event->button) {
7467 case 1:
7468 set_current_tab(t->tab_id);
7469 break;
7470 case 2:
7471 delete_tab(t);
7472 break;
7475 return TRUE;
7478 void
7479 append_tab(struct tab *t)
7481 if (t == NULL)
7482 return;
7484 TAILQ_INSERT_TAIL(&tabs, t, entry);
7485 t->tab_id = gtk_notebook_append_page(notebook, t->vbox, t->tab_content);
7488 GtkWidget *
7489 create_sbe(void)
7491 GtkWidget *sbe;
7493 sbe = gtk_label_new(NULL);
7494 gtk_widget_set_can_focus(GTK_WIDGET(sbe), FALSE);
7495 modify_font(GTK_WIDGET(sbe), statusbar_font);
7496 return (sbe);
7500 statusbar_create(struct tab *t)
7502 GtkWidget *box; /* container for statusbar elems */
7503 GtkWidget *sep;
7504 GtkBox *b;
7505 char *p;
7506 #if !GTK_CHECK_VERSION(3, 0, 0)
7507 GdkColor color;
7508 #endif
7510 if (t == NULL) {
7511 DPRINTF("%s: invalid parameters", __func__);
7512 return (1);
7515 #if GTK_CHECK_VERSION(3, 0, 0)
7516 t->statusbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7517 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7518 gtk_widget_set_name(GTK_WIDGET(t->statusbar), "statusbar");
7519 #else
7520 t->statusbar = gtk_hbox_new(FALSE, 0);
7521 box = gtk_hbox_new(FALSE, 0);
7522 #endif
7523 t->sbe.ebox = gtk_event_box_new();
7525 gtk_widget_set_can_focus(GTK_WIDGET(t->statusbar), FALSE);
7526 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.ebox), FALSE);
7527 gtk_widget_set_can_focus(GTK_WIDGET(box), FALSE);
7529 gtk_box_set_spacing(GTK_BOX(box), 10);
7530 gtk_box_pack_start(GTK_BOX(t->statusbar), t->sbe.ebox, TRUE, TRUE, 0);
7531 gtk_container_add(GTK_CONTAINER(t->sbe.ebox), box);
7533 t->sbe.uri = gtk_entry_new();
7534 gtk_widget_set_can_focus(GTK_WIDGET(t->sbe.uri), FALSE);
7535 modify_font(GTK_WIDGET(t->sbe.uri), statusbar_font);
7536 #if !GTK_CHECK_VERSION(3, 0, 0)
7537 gtk_entry_set_inner_border(GTK_ENTRY(t->sbe.uri), NULL);
7538 gtk_entry_set_has_frame(GTK_ENTRY(t->sbe.uri), FALSE);
7539 #endif
7541 #if GTK_CHECK_VERSION(3, 0, 0)
7542 statusbar_modify_attr(t, XT_CSS_NORMAL);
7543 #else
7544 statusbar_modify_attr(t, XT_COLOR_WHITE, XT_COLOR_BLACK);
7545 #endif
7547 b = GTK_BOX(box);
7548 gtk_box_pack_start(b, t->sbe.uri, TRUE, TRUE, 0);
7550 for (p = statusbar_elems; *p != '\0'; p++) {
7551 switch (*p) {
7552 case '|':
7553 #if GTK_CHECK_VERSION(3, 0, 0)
7554 sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
7555 #else
7556 sep = gtk_vseparator_new();
7557 gdk_color_parse(XT_COLOR_SB_SEPARATOR, &color);
7558 gtk_widget_modify_bg(sep, GTK_STATE_NORMAL, &color);
7559 #endif
7560 gtk_box_pack_start(b, sep, FALSE, FALSE, 0);
7561 break;
7562 case 'P':
7563 if (t->sbe.position == NULL) {
7564 t->sbe.position = create_sbe();
7565 gtk_box_pack_start(b, t->sbe.position, FALSE,
7566 FALSE, 0);
7568 break;
7569 case 'B':
7570 if (t->sbe.buffercmd == NULL) {
7571 t->sbe.buffercmd = create_sbe();
7572 gtk_box_pack_start(b, t->sbe.buffercmd, FALSE,
7573 FALSE, 0);
7575 break;
7576 case 'Z':
7577 if (t->sbe.zoom == NULL) {
7578 t->sbe.zoom = create_sbe();
7579 gtk_box_pack_start(b, t->sbe.zoom, FALSE, FALSE,
7582 break;
7583 case 'T':
7584 if (t->sbe.tabs == NULL) {
7585 t->sbe.tabs = create_sbe();
7586 gtk_box_pack_start(b, t->sbe.tabs, FALSE, FALSE,
7589 break;
7590 case 'p':
7591 if (t->sbe.proxy == NULL) {
7592 t->sbe.proxy = create_sbe();
7593 gtk_box_pack_start(b, t->sbe.proxy, FALSE,
7594 FALSE, 0);
7595 if (proxy_uri)
7596 gtk_entry_set_text(
7597 GTK_ENTRY(t->sbe.proxy), "proxy");
7599 break;
7600 default:
7601 warnx("illegal flag \"%c\" in statusbar_elems\n", *p);
7602 break;
7606 gtk_box_pack_end(GTK_BOX(t->vbox), t->statusbar, FALSE, FALSE, 0);
7608 return (0);
7611 struct tab *
7612 create_new_tab(const char *title, struct undo *u, int focus, int position)
7614 struct tab *t;
7615 int load = 1, id;
7616 GtkWidget *b, *bb;
7617 WebKitWebHistoryItem *item;
7618 GList *items;
7619 char *sv[3];
7620 #if !GTK_CHECK_VERSION(3, 0, 0)
7621 GdkColor color;
7622 #endif
7624 DNPRINTF(XT_D_TAB, "create_new_tab: title %s focus %d\n", title, focus);
7626 if (tabless && !TAILQ_EMPTY(&tabs)) {
7627 if (single_instance) {
7628 DNPRINTF(XT_D_TAB,
7629 "create_new_tab: new tab rejected\n");
7630 return (NULL);
7632 sv[0] = start_argv[0];
7633 sv[1] = (char *)title;
7634 sv[2] = (char *)NULL;
7635 if (!g_spawn_async(NULL, sv, NULL, G_SPAWN_SEARCH_PATH,
7636 NULL, NULL, NULL, NULL))
7637 show_oops(NULL, "%s: could not spawn process",
7638 __func__);
7639 return (NULL);
7642 t = g_malloc0(sizeof *t);
7644 if (title == NULL) {
7645 title = "(untitled)";
7646 load = 0;
7649 #if GTK_CHECK_VERSION(3, 0, 0)
7650 t->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
7651 b = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7652 gtk_widget_set_name(t->vbox, "vbox");
7653 #else
7654 t->vbox = gtk_vbox_new(FALSE, 0);
7655 b = gtk_hbox_new(FALSE, 0);
7656 #endif
7657 gtk_widget_set_can_focus(t->vbox, FALSE);
7659 /* label + button for tab */
7660 t->tab_content = b;
7661 gtk_widget_set_can_focus(t->tab_content, FALSE);
7663 t->user_agent_id = 0;
7664 t->http_accept_id = 0;
7666 #if WEBKIT_CHECK_VERSION(1, 5, 0)
7667 t->active = NULL;
7668 #endif
7670 #if GTK_CHECK_VERSION(2, 20, 0)
7671 t->spinner = gtk_spinner_new();
7672 #endif
7673 t->label = gtk_label_new(title);
7674 bb = create_button("Close", GTK_STOCK_CLOSE, 1);
7675 gtk_label_set_max_width_chars(GTK_LABEL(t->label), 20);
7676 gtk_label_set_ellipsize(GTK_LABEL(t->label), PANGO_ELLIPSIZE_END);
7677 gtk_label_set_line_wrap(GTK_LABEL(t->label), FALSE);
7678 gtk_widget_set_size_request(t->tab_content, 130, 0);
7681 * this is a total hack and most likely breaks with other styles but
7682 * is necessary so the text doesn't bounce around when the spinner is
7683 * shown/hidden
7685 #if GTK_CHECK_VERSION(3, 0, 0)
7686 gtk_widget_set_size_request(t->label, 95, 0);
7687 #else
7688 gtk_widget_set_size_request(t->label, 100, 0);
7689 #endif
7691 gtk_box_pack_start(GTK_BOX(t->tab_content), bb, FALSE, FALSE, 0);
7692 gtk_box_pack_start(GTK_BOX(t->tab_content), t->label, FALSE, FALSE, 0);
7693 #if GTK_CHECK_VERSION(2, 20, 0)
7694 gtk_box_pack_end(GTK_BOX(b), t->spinner, FALSE, FALSE, 0);
7695 #endif
7697 /* toolbar */
7698 if (browser_mode == XT_BM_KIOSK) {
7699 t->toolbar = create_kiosk_toolbar(t);
7700 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7702 } else {
7703 t->toolbar = create_toolbar(t);
7704 gtk_box_pack_start(GTK_BOX(t->vbox), t->toolbar, FALSE, FALSE,
7708 /* marks */
7709 marks_clear(t);
7711 /* browser */
7712 t->browser_win = create_browser(t);
7713 set_scrollbar_visibility(t, show_scrollbars);
7714 gtk_box_pack_start(GTK_BOX(t->vbox), t->browser_win, TRUE, TRUE, 0);
7716 /* oops message for user feedback */
7717 t->oops = gtk_entry_new();
7718 gtk_entry_set_inner_border(GTK_ENTRY(t->oops), NULL);
7719 gtk_entry_set_has_frame(GTK_ENTRY(t->oops), FALSE);
7720 gtk_widget_set_can_focus(GTK_WIDGET(t->oops), FALSE);
7721 #if GTK_CHECK_VERSION(3, 0, 0)
7722 gtk_widget_set_name(t->oops, XT_CSS_RED);
7723 #else
7724 gdk_color_parse(XT_COLOR_RED, &color);
7725 gtk_widget_modify_base(t->oops, GTK_STATE_NORMAL, &color);
7726 #endif
7727 gtk_box_pack_end(GTK_BOX(t->vbox), t->oops, FALSE, FALSE, 0);
7728 modify_font(GTK_WIDGET(t->oops), oops_font);
7730 /* command entry */
7731 t->cmd = gtk_entry_new();
7732 g_signal_connect(G_OBJECT(t->cmd), "focus-in-event",
7733 G_CALLBACK(entry_focus_cb), t);
7734 gtk_entry_set_inner_border(GTK_ENTRY(t->cmd), NULL);
7735 gtk_entry_set_has_frame(GTK_ENTRY(t->cmd), FALSE);
7736 gtk_box_pack_end(GTK_BOX(t->vbox), t->cmd, FALSE, FALSE, 0);
7737 modify_font(GTK_WIDGET(t->cmd), cmd_font);
7739 /* status bar */
7740 statusbar_create(t);
7742 /* buffer list */
7743 t->buffers = create_buffers(t);
7744 gtk_box_pack_end(GTK_BOX(t->vbox), t->buffers, FALSE, FALSE, 0);
7746 /* xtp meaning is normal by default */
7747 set_normal_tab_meaning(t);
7749 /* set empty favicon */
7750 xt_icon_from_name(t, "text-html");
7752 /* and show it all */
7753 gtk_widget_show_all(b);
7754 gtk_widget_show_all(t->vbox);
7756 if (!fancy_bar) {
7757 gtk_widget_hide(t->backward);
7758 gtk_widget_hide(t->forward);
7759 gtk_widget_hide(t->stop);
7760 gtk_widget_hide(t->js_toggle);
7762 if (!fancy_bar || (search_string == NULL || strlen(search_string) == 0))
7763 gtk_widget_hide(t->search_entry);
7764 if (http_proxy == NULL && http_proxy_save == NULL)
7765 gtk_widget_hide(t->proxy_toggle);
7767 /* compact tab bar */
7768 t->tab_elems.label = gtk_label_new(title);
7769 t->tab_elems.favicon = gtk_image_new();
7771 t->tab_elems.eventbox = gtk_event_box_new();
7772 gtk_widget_set_name(t->tab_elems.eventbox, "compact_tab");
7773 #if GTK_CHECK_VERSION(3, 0, 0)
7774 t->tab_elems.box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
7776 gtk_label_set_ellipsize(GTK_LABEL(t->tab_elems.label),
7777 PANGO_ELLIPSIZE_END);
7778 gtk_widget_override_font(t->tab_elems.label, tabbar_font);
7779 gtk_widget_set_halign(t->tab_elems.label, GTK_ALIGN_START);
7780 gtk_widget_set_valign(t->tab_elems.label, GTK_ALIGN_START);
7781 #else
7782 t->tab_elems.box = gtk_hbox_new(FALSE, 0);
7784 gtk_label_set_width_chars(GTK_LABEL(t->tab_elems.label), 1);
7785 gtk_misc_set_alignment(GTK_MISC(t->tab_elems.label), 0.0, 0.0);
7786 gtk_misc_set_padding(GTK_MISC(t->tab_elems.label), 4.0, 4.0);
7787 modify_font(GTK_WIDGET(t->tab_elems.label), tabbar_font);
7789 gdk_color_parse(XT_COLOR_CT_BACKGROUND, &color);
7790 gtk_widget_modify_bg(t->tab_elems.eventbox, GTK_STATE_NORMAL, &color);
7791 gdk_color_parse(XT_COLOR_CT_INACTIVE, &color);
7792 gtk_widget_modify_fg(t->tab_elems.label, GTK_STATE_NORMAL, &color);
7793 #endif
7795 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.favicon, FALSE,
7796 FALSE, 0);
7797 gtk_box_pack_start(GTK_BOX(t->tab_elems.box), t->tab_elems.label, TRUE,
7798 TRUE, 0);
7799 gtk_container_add(GTK_CONTAINER(t->tab_elems.eventbox),
7800 t->tab_elems.box);
7802 gtk_box_pack_start(GTK_BOX(tab_bar_box), t->tab_elems.eventbox, TRUE,
7803 TRUE, 0);
7804 gtk_widget_show_all(t->tab_elems.eventbox);
7806 if (append_next == 0 || gtk_notebook_get_n_pages(notebook) == 0)
7807 append_tab(t);
7808 else {
7809 id = position >= 0 ? position :
7810 gtk_notebook_get_current_page(notebook) + 1;
7811 if (id > gtk_notebook_get_n_pages(notebook))
7812 append_tab(t);
7813 else {
7814 TAILQ_INSERT_TAIL(&tabs, t, entry);
7815 gtk_notebook_insert_page(notebook, t->vbox, t->tab_content,
7816 id);
7817 gtk_box_reorder_child(GTK_BOX(tab_bar_box),
7818 t->tab_elems.eventbox, id);
7819 recalc_tabs();
7823 #if GTK_CHECK_VERSION(2, 20, 0)
7824 /* turn spinner off if we are a new tab without uri */
7825 if (!load) {
7826 gtk_spinner_stop(GTK_SPINNER(t->spinner));
7827 gtk_widget_hide(t->spinner);
7829 #endif
7830 /* make notebook tabs reorderable */
7831 gtk_notebook_set_tab_reorderable(notebook, t->vbox, TRUE);
7833 /* compact tabs clickable */
7834 g_signal_connect(G_OBJECT(t->tab_elems.eventbox),
7835 "button_press_event", G_CALLBACK(tab_clicked_cb), t);
7837 g_object_connect(G_OBJECT(t->cmd),
7838 "signal::button-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7839 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb), t,
7840 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb), t,
7841 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb), t,
7842 "signal::activate", G_CALLBACK(cmd_activate_cb), t,
7843 "signal::populate-popup", G_CALLBACK(cmd_popup_cb), t,
7844 "signal::hide", G_CALLBACK(cmd_hide_cb), t,
7845 (char *)NULL);
7847 /* reuse wv_button_cb to hide oops */
7848 g_object_connect(G_OBJECT(t->oops),
7849 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7850 (char *)NULL);
7852 g_signal_connect(t->buffers,
7853 "row-activated", G_CALLBACK(row_activated_cb), t);
7854 g_object_connect(G_OBJECT(t->buffers),
7855 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t, (char *)NULL);
7857 g_object_connect(G_OBJECT(t->wv),
7858 "signal::key-press-event", G_CALLBACK(wv_keypress_cb), t,
7859 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb), t,
7860 "signal::download-requested", G_CALLBACK(webview_download_cb), t,
7861 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb), t,
7862 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7863 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb), t,
7864 "signal::resource-request-starting", G_CALLBACK(webview_rrs_cb), t,
7865 "signal::create-web-view", G_CALLBACK(webview_cwv_cb), t,
7866 "signal::close-web-view", G_CALLBACK(webview_closewv_cb), t,
7867 "signal::event", G_CALLBACK(webview_event_cb), t,
7868 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb), t,
7869 "signal::button_press_event", G_CALLBACK(wv_button_cb), t,
7870 "signal::button_release_event", G_CALLBACK(wv_release_button_cb), t,
7871 "signal::populate-popup", G_CALLBACK(wv_popup_cb), t,
7872 (char *)NULL);
7873 g_signal_connect(t->wv,
7874 "notify::load-status", G_CALLBACK(notify_load_status_cb), t);
7875 g_signal_connect(t->wv,
7876 "notify::title", G_CALLBACK(notify_title_cb), t);
7877 t->progress_handle = g_signal_connect(t->wv,
7878 "notify::progress", G_CALLBACK(webview_progress_changed_cb), t);
7880 /* hijack the unused keys as if we were the browser */
7881 //g_object_connect(G_OBJECT(t->toolbar),
7882 // "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb), t,
7883 // (char *)NULL);
7885 g_signal_connect(G_OBJECT(bb), "clicked", G_CALLBACK(tab_close_cb), t);
7887 /* setup history */
7888 t->bfl = webkit_web_view_get_back_forward_list(t->wv);
7889 /* restore the tab's history */
7890 if (u && u->history) {
7891 items = u->history;
7892 while (items) {
7893 item = items->data;
7894 webkit_web_back_forward_list_add_item(t->bfl, item);
7895 items = g_list_next(items);
7898 item = g_list_nth_data(u->history, u->back);
7899 if (item)
7900 webkit_web_view_go_to_back_forward_item(t->wv, item);
7902 g_list_free(items);
7903 g_list_free(u->history);
7904 } else
7905 webkit_web_back_forward_list_clear(t->bfl);
7907 /* check and show url and statusbar */
7908 url_set_visibility();
7909 statusbar_set_visibility();
7911 if (focus) {
7912 set_current_tab(t->tab_id);
7913 DNPRINTF(XT_D_TAB, "create_new_tab: going to tab: %d\n",
7914 t->tab_id);
7916 if (load) {
7917 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), title);
7918 load_uri(t, title);
7919 } else {
7920 if (show_url == 1)
7921 gtk_widget_grab_focus(GTK_WIDGET(t->uri_entry));
7922 else
7923 focus_webview(t);
7925 } else if (load)
7926 load_uri(t, title);
7928 if (userstyle_global)
7929 apply_style(t);
7931 recolor_compact_tabs();
7932 setzoom_webkit(t, XT_ZOOM_NORMAL);
7933 return (t);
7936 void
7937 notebook_switchpage_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7938 gpointer *udata)
7940 struct tab *t;
7941 const gchar *uri;
7943 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: tab: %d\n", pn);
7945 if (gtk_notebook_get_current_page(notebook) == -1)
7946 recalc_tabs();
7948 TAILQ_FOREACH(t, &tabs, entry) {
7949 if (t->tab_id == pn) {
7950 DNPRINTF(XT_D_TAB, "notebook_switchpage_cb: going to "
7951 "%d\n", pn);
7953 uri = get_title(t, TRUE);
7954 gtk_window_set_title(GTK_WINDOW(main_window), uri);
7956 hide_cmd(t);
7957 hide_oops(t);
7958 hide_buffers(t);
7960 if (t->focus_wv) {
7961 /* can't use focus_webview here */
7962 gtk_widget_grab_focus(GTK_WIDGET(t->wv));
7964 update_statusbar_tabs(t);
7965 break;
7970 void
7971 notebook_pagereordered_cb(GtkNotebook *nb, GtkWidget *nbp, guint pn,
7972 gpointer *udata)
7974 struct tab *t = NULL, *tt;
7976 recalc_tabs();
7978 TAILQ_FOREACH(tt, &tabs, entry)
7979 if (tt->tab_id == pn) {
7980 t = tt;
7981 break;
7983 if (t == NULL)
7984 return;
7985 DNPRINTF(XT_D_TAB, "page_reordered_cb: tab: %d\n", t->tab_id);
7987 gtk_box_reorder_child(GTK_BOX(tab_bar_box), t->tab_elems.eventbox,
7988 t->tab_id);
7990 update_statusbar_tabs(t);
7993 void
7994 menuitem_response(struct tab *t)
7996 gtk_notebook_set_current_page(notebook, t->tab_id);
8000 destroy_menu(GtkMenuShell *m, void *notused)
8002 gtk_widget_destroy(GTK_WIDGET(m));
8003 g_object_unref(G_OBJECT(m));
8004 return (XT_CB_PASSTHROUGH);
8007 gboolean
8008 arrow_cb(GtkWidget *w, GdkEventButton *event, gpointer user_data)
8010 GtkWidget *menu = NULL, *menu_items;
8011 GdkEventButton *bevent;
8012 struct tab **stabs = NULL;
8013 int i, num_tabs;
8014 const gchar *uri;
8016 if (event->type == GDK_BUTTON_PRESS) {
8017 bevent = (GdkEventButton *) event;
8018 menu = gtk_menu_new();
8019 g_object_ref_sink(G_OBJECT(menu));
8021 num_tabs = sort_tabs_by_page_num(&stabs);
8022 for (i = 0; i < num_tabs; ++i) {
8023 if (stabs[i] == NULL)
8024 continue;
8025 if ((uri = get_uri(stabs[i])) == NULL)
8026 /* XXX make sure there is something to print */
8027 /* XXX add gui pages in here to look purdy */
8028 uri = "(untitled)";
8029 menu_items = gtk_menu_item_new_with_label(uri);
8030 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_items);
8031 gtk_widget_show(menu_items);
8033 g_signal_connect_swapped(menu_items,
8034 "activate", G_CALLBACK(menuitem_response),
8035 (gpointer)stabs[i]);
8037 g_free(stabs);
8039 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
8040 bevent->button, bevent->time);
8042 g_object_connect(G_OBJECT(menu),
8043 "signal::selection-done", G_CALLBACK(destroy_menu), NULL,
8044 (char *)NULL);
8046 return (TRUE /* eat event */);
8049 return (FALSE /* propagate */);
8053 icon_size_map(int iconsz)
8055 if (iconsz <= GTK_ICON_SIZE_INVALID ||
8056 iconsz > GTK_ICON_SIZE_DIALOG)
8057 return (GTK_ICON_SIZE_SMALL_TOOLBAR);
8059 return (iconsz);
8062 GtkWidget *
8063 create_button(const char *name, const char *stockid, int size)
8065 GtkWidget *button, *image;
8066 int gtk_icon_size;
8067 #if !GTK_CHECK_VERSION(3, 0, 0)
8068 gchar *newstyle;
8069 #endif
8071 #if !GTK_CHECK_VERSION(3, 0, 0)
8072 newstyle = g_strdup_printf(
8073 "style \"%s-style\"\n"
8074 "{\n"
8075 " GtkWidget::focus-padding = 0\n"
8076 " GtkWidget::focus-line-width = 0\n"
8077 " xthickness = 0\n"
8078 " ythickness = 0\n"
8079 "}\n"
8080 "widget \"*.%s\" style \"%s-style\"", name, name, name);
8081 gtk_rc_parse_string(newstyle);
8082 g_free(newstyle);
8083 #endif
8084 button = gtk_button_new();
8085 gtk_widget_set_can_focus(button, FALSE);
8086 gtk_button_set_focus_on_click(GTK_BUTTON(button), FALSE);
8087 gtk_icon_size = icon_size_map(size ? size : icon_size);
8089 image = gtk_image_new_from_stock(stockid, gtk_icon_size);
8090 gtk_container_set_border_width(GTK_CONTAINER(button), 0);
8091 gtk_button_set_image(GTK_BUTTON(button), GTK_WIDGET(image));
8092 gtk_widget_set_name(button, name);
8093 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
8095 return (button);
8098 void
8099 button_set_file(GtkWidget *button, char *filename)
8101 GtkWidget *image;
8102 char file[PATH_MAX];
8104 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, filename);
8105 image = gtk_image_new_from_file(file);
8106 gtk_button_set_image(GTK_BUTTON(button), image);
8109 void
8110 button_set_stockid(GtkWidget *button, char *stockid)
8112 GtkWidget *image;
8114 image = gtk_image_new_from_stock(stockid, icon_size_map(icon_size));
8115 gtk_button_set_image(GTK_BUTTON(button), image);
8118 void
8119 create_canvas(void)
8121 GtkWidget *vbox;
8122 GList *l = NULL;
8123 GdkPixbuf *pb;
8124 char file[PATH_MAX];
8125 int i;
8126 #if !GTK_CHECK_VERSION(3, 0, 0)
8127 GdkColor color;
8128 #endif
8130 #if GTK_CHECK_VERSION(3, 0, 0)
8131 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
8132 #else
8133 vbox = gtk_vbox_new(FALSE, 0);
8134 #endif
8135 gtk_box_set_spacing(GTK_BOX(vbox), 0);
8136 gtk_widget_set_can_focus(vbox, FALSE);
8137 notebook = GTK_NOTEBOOK(gtk_notebook_new());
8138 #if !GTK_CHECK_VERSION(3, 0, 0)
8139 /* XXX seems to be needed with gtk+2 */
8140 g_object_set(G_OBJECT(notebook), "tab-border", 0, NULL);
8141 #endif
8142 gtk_notebook_set_scrollable(notebook, TRUE);
8143 gtk_notebook_set_show_border(notebook, FALSE);
8144 gtk_widget_set_can_focus(GTK_WIDGET(notebook), FALSE);
8146 abtn = gtk_button_new();
8147 gtk_widget_set_can_focus(abtn, FALSE);
8148 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
8149 gtk_widget_set_name(abtn, "Arrow");
8150 gtk_button_set_image(GTK_BUTTON(abtn), arrow);
8151 gtk_widget_set_size_request(abtn, -1, 20);
8153 #if GTK_CHECK_VERSION(2, 20, 0)
8154 gtk_notebook_set_action_widget(notebook, abtn, GTK_PACK_END);
8155 #endif
8156 /* compact tab bar */
8157 tab_bar = gtk_event_box_new();
8158 #if GTK_CHECK_VERSION(3, 0, 0)
8159 gtk_widget_set_name(tab_bar, "tab_bar");
8160 tab_bar_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
8161 #else
8162 gdk_color_parse(XT_COLOR_CT_SEPARATOR, &color);
8163 gtk_widget_modify_bg(tab_bar, GTK_STATE_NORMAL, &color);
8164 tab_bar_box = gtk_hbox_new(TRUE, 0);
8165 #endif
8166 gtk_container_add(GTK_CONTAINER(tab_bar), tab_bar_box);
8167 gtk_box_set_homogeneous(GTK_BOX(tab_bar_box), TRUE);
8168 gtk_box_set_spacing(GTK_BOX(tab_bar_box), 2);
8170 gtk_box_pack_start(GTK_BOX(vbox), tab_bar, FALSE, FALSE, 0);
8171 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(notebook), TRUE, TRUE, 0);
8173 g_object_connect(G_OBJECT(notebook),
8174 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb), NULL,
8175 (char *)NULL);
8176 g_object_connect(G_OBJECT(notebook),
8177 "signal::page-reordered", G_CALLBACK(notebook_pagereordered_cb),
8178 NULL, (char *)NULL);
8179 g_signal_connect(G_OBJECT(abtn), "button_press_event",
8180 G_CALLBACK(arrow_cb), NULL);
8182 main_window = create_window("xombrero");
8183 gtk_container_add(GTK_CONTAINER(main_window), vbox);
8184 g_signal_connect(G_OBJECT(main_window), "delete_event",
8185 G_CALLBACK(gtk_main_quit), NULL);
8186 #if GTK_CHECK_VERSION(3, 0, 0)
8187 gtk_window_set_has_resize_grip(GTK_WINDOW(main_window), FALSE);
8188 #endif
8190 /* icons */
8191 for (i = 0; i < LENGTH(icons); i++) {
8192 snprintf(file, sizeof file, "%s" PS "%s", resource_dir, icons[i]);
8193 pb = gdk_pixbuf_new_from_file(file, NULL);
8194 l = g_list_append(l, pb);
8196 gtk_window_set_default_icon_list(l);
8198 for (; l; l = l->next)
8199 g_object_unref(G_OBJECT(l->data));
8201 gtk_widget_show_all(abtn);
8202 gtk_widget_show_all(main_window);
8203 notebook_tab_set_visibility();
8206 #ifndef XT_SOCKET_DISABLE
8208 send_cmd_to_socket(char *cmd)
8210 int s, len, rv = 1;
8211 struct sockaddr_un sa;
8213 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8214 warnx("%s: socket", __func__);
8215 return (rv);
8218 sa.sun_family = AF_UNIX;
8219 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8220 work_dir, XT_SOCKET_FILE);
8221 len = SUN_LEN(&sa);
8223 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8224 warnx("%s: connect", __func__);
8225 goto done;
8228 if (send(s, cmd, strlen(cmd) + 1, 0) == -1) {
8229 warnx("%s: send", __func__);
8230 goto done;
8233 rv = 0;
8234 done:
8235 close(s);
8236 return (rv);
8239 gboolean
8240 socket_watcher(GIOChannel *source, GIOCondition condition, gpointer data)
8242 int s, n;
8243 char str[XT_MAX_URL_LENGTH];
8244 socklen_t t = sizeof(struct sockaddr_un);
8245 struct sockaddr_un sa;
8246 struct passwd *p;
8247 uid_t uid;
8248 gid_t gid;
8249 struct tab *tt;
8250 gint fd = g_io_channel_unix_get_fd(source);
8252 if ((s = accept(fd, (struct sockaddr *)&sa, &t)) == -1) {
8253 warn("accept");
8254 return (FALSE);
8257 if (getpeereid(s, &uid, &gid) == -1) {
8258 warn("getpeereid");
8259 return (FALSE);
8261 if (uid != getuid() || gid != getgid()) {
8262 warnx("unauthorized user");
8263 return (FALSE);
8266 p = getpwuid(uid);
8267 if (p == NULL) {
8268 warnx("not a valid user");
8269 return (FALSE);
8272 n = recv(s, str, sizeof(str), 0);
8273 if (n <= 0)
8274 return (TRUE);
8276 tt = get_current_tab();
8277 cmd_execute(tt, str);
8278 return (TRUE);
8282 is_running(void)
8284 int s, len, rv = 1;
8285 struct sockaddr_un sa;
8287 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8288 warn("is_running: socket");
8289 return (-1);
8292 sa.sun_family = AF_UNIX;
8293 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8294 work_dir, XT_SOCKET_FILE);
8295 len = SUN_LEN(&sa);
8297 /* connect to see if there is a listener */
8298 if (connect(s, (struct sockaddr *)&sa, len) == -1)
8299 rv = 0; /* not running */
8300 else
8301 rv = 1; /* already running */
8303 close(s);
8305 return (rv);
8309 build_socket(void)
8311 int s, len;
8312 struct sockaddr_un sa;
8314 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
8315 warn("build_socket: socket");
8316 return (-1);
8319 sa.sun_family = AF_UNIX;
8320 snprintf(sa.sun_path, sizeof(sa.sun_path), "%s" PS "%s",
8321 work_dir, XT_SOCKET_FILE);
8322 len = SUN_LEN(&sa);
8324 /* connect to see if there is a listener */
8325 if (connect(s, (struct sockaddr *)&sa, len) == -1) {
8326 /* no listener so we will */
8327 unlink(sa.sun_path);
8329 if (bind(s, (struct sockaddr *)&sa, len) == -1) {
8330 warn("build_socket: bind");
8331 goto done;
8334 if (listen(s, 1) == -1) {
8335 warn("build_socket: listen");
8336 goto done;
8339 return (s);
8342 done:
8343 close(s);
8344 return (-1);
8346 #endif
8348 void
8349 xxx_dir(char *dir)
8351 struct stat sb;
8353 if (stat(dir, &sb)) {
8354 #if defined __MINGW32__
8355 if (mkdir(dir) == -1)
8356 #else
8357 if (mkdir(dir, S_IRWXU) == -1)
8358 #endif
8359 err(1, "mkdir %s", dir);
8360 if (stat(dir, &sb))
8361 err(1, "stat %s", dir);
8363 if (S_ISDIR(sb.st_mode) == 0)
8364 errx(1, "%s not a dir", dir);
8365 #if !defined __MINGW32__
8366 if (((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) != S_IRWXU) {
8367 warnx("fixing invalid permissions on %s", dir);
8368 if (chmod(dir, S_IRWXU) == -1)
8369 err(1, "chmod %s", dir);
8371 #endif
8374 void
8375 usage(void)
8377 fprintf(stderr,
8378 "%s [-nSTVt][-f file][-s session] url ...\n", __progname);
8379 exit(0);
8382 #if GTK_CHECK_VERSION(3, 0, 0)
8383 void
8384 setup_css(void)
8386 GtkCssProvider *provider;
8387 GdkDisplay *display;
8388 GdkScreen *screen;
8389 GFile *file;
8390 char path[PATH_MAX];
8391 #if defined __MINGW32__
8392 GtkCssProvider *windows_hacks;
8393 #endif
8395 provider = gtk_css_provider_new();
8396 display = gdk_display_get_default();
8397 screen = gdk_display_get_default_screen(display);
8398 snprintf(path, sizeof path, "%s" PS "%s", resource_dir, XT_CSS_FILE);
8399 file = g_file_new_for_path(path);
8400 gtk_css_provider_load_from_file(provider, file, NULL);
8401 gtk_style_context_add_provider_for_screen(screen,
8402 GTK_STYLE_PROVIDER(provider),
8403 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8404 g_object_unref(G_OBJECT(provider));
8405 #if defined __MINGW32__
8406 windows_hacks = gtk_css_provider_new();
8407 gtk_css_provider_load_from_data(windows_hacks,
8408 ".notebook tab {\n"
8409 " border-width: 0px;\n"
8410 "}", -1, NULL);
8411 gtk_style_context_add_provider_for_screen(screen,
8412 GTK_STYLE_PROVIDER(windows_hacks),
8413 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8414 g_object_unref(G_OBJECT(windows_hacks));
8415 #endif
8416 g_object_unref(G_OBJECT(file));
8418 #endif
8420 void
8421 welcome(void)
8423 startpage_add("<b>Welcome to xombrero %s!</b><p>", version);
8424 startpage_add("Details at "
8425 "<a href=https://opensource.conformal.com/wiki/xombrero>xombrero "
8426 "wiki page</a><p>");
8430 main(int argc, char **argv)
8432 struct stat sb;
8433 int c, optn = 0, opte = 0, focus = 1;
8434 char conf[PATH_MAX] = { '\0' };
8435 char file[PATH_MAX];
8436 char sodversion[32];
8437 char *env_proxy = NULL;
8438 char *path;
8439 FILE *f = NULL;
8440 struct karg a;
8442 start_argv = (char * const *)argv;
8444 if (os_init)
8445 os_init();
8447 /* prepare gtk */
8448 gtk_init(&argc, &argv);
8450 gnutls_global_init();
8452 strlcpy(named_session, XT_SAVED_TABS_FILE, sizeof named_session);
8454 RB_INIT(&hl);
8455 RB_INIT(&downloads);
8456 RB_INIT(&st_tree);
8457 RB_INIT(&svl);
8458 RB_INIT(&ua_list);
8459 RB_INIT(&ha_list);
8460 RB_INIT(&di_list);
8462 TAILQ_INIT(&sessions);
8463 TAILQ_INIT(&tabs);
8464 TAILQ_INIT(&mtl);
8465 TAILQ_INIT(&aliases);
8466 TAILQ_INIT(&undos);
8467 TAILQ_INIT(&kbl);
8468 TAILQ_INIT(&spl);
8469 TAILQ_INIT(&chl);
8470 TAILQ_INIT(&shl);
8471 TAILQ_INIT(&cul);
8472 TAILQ_INIT(&srl);
8473 TAILQ_INIT(&c_wl);
8474 TAILQ_INIT(&js_wl);
8475 TAILQ_INIT(&pl_wl);
8476 TAILQ_INIT(&force_https);
8477 TAILQ_INIT(&svil);
8479 #ifndef XT_RESOURCE_LIMITS_DISABLE
8480 struct rlimit rlp;
8481 char *cmd = NULL;
8482 int s;
8483 GIOChannel *channel;
8485 /* fiddle with ulimits */
8486 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8487 warn("getrlimit");
8488 else {
8489 /* just use them all */
8490 #ifdef __APPLE__
8491 rlp.rlim_cur = OPEN_MAX;
8492 #else
8493 rlp.rlim_cur = rlp.rlim_max;
8494 #endif
8495 if (setrlimit(RLIMIT_NOFILE, &rlp) == -1)
8496 warn("setrlimit");
8497 if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
8498 warn("getrlimit");
8499 else if (rlp.rlim_cur < 1024)
8500 startpage_add("%s requires at least 1024 "
8501 "(2048 recommended) file " "descriptors, "
8502 "currently it has up to %d available",
8503 __progname, rlp.rlim_cur);
8505 #endif
8507 while ((c = getopt(argc, argv, "STVf:s:tne")) != -1) {
8508 switch (c) {
8509 case 'S':
8510 show_url = 0;
8511 break;
8512 case 'T':
8513 show_tabs = 0;
8514 break;
8515 case 'V':
8516 #ifdef XOMBRERO_BUILDSTR
8517 errx(0 , "Version: %s Build: %s",
8518 version, XOMBRERO_BUILDSTR);
8519 #else
8520 errx(0 , "Version: %s", version);
8521 #endif
8522 break;
8523 case 'f':
8524 strlcpy(conf, optarg, sizeof(conf));
8525 break;
8526 case 's':
8527 strlcpy(named_session, optarg, sizeof(named_session));
8528 break;
8529 case 't':
8530 tabless = 1;
8531 break;
8532 case 'n':
8533 optn = 1;
8534 break;
8535 case 'e':
8536 opte = 1;
8537 break;
8538 default:
8539 usage();
8540 /* NOTREACHED */
8543 argc -= optind;
8544 argv += optind;
8546 init_keybindings();
8548 pwd = getpwuid(getuid());
8549 if (pwd == NULL)
8550 errx(1, "invalid user %d", getuid());
8552 /* set download dir */
8553 if (strlen(download_dir) == 0)
8554 strlcpy(download_dir, pwd->pw_dir, sizeof download_dir);
8556 /* compile buffer command regexes */
8557 buffercmd_init();
8559 /* set default dynamic string settings */
8560 home = g_strdup(XT_DS_HOME);
8561 search_string = g_strdup(XT_DS_SEARCH_STRING);
8562 strlcpy(runtime_settings, "runtime", sizeof runtime_settings);
8563 cmd_font_name = g_strdup(XT_DS_CMD_FONT_NAME);
8564 oops_font_name = g_strdup(XT_DS_OOPS_FONT_NAME);
8565 statusbar_font_name = g_strdup(XT_DS_STATUSBAR_FONT_NAME);
8566 tabbar_font_name = g_strdup(XT_DS_TABBAR_FONT_NAME);
8567 statusbar_elems = g_strdup("BP");
8568 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8569 encoding = g_strdup(XT_DS_ENCODING);
8570 spell_check_languages = g_strdup(XT_DS_SPELL_CHECK_LANGUAGES);
8571 path = g_strdup_printf("%s" PS "style.css", resource_dir);
8572 userstyle = g_filename_to_uri(path, NULL, NULL);
8573 g_free(path);
8574 stylesheet = g_strdup(userstyle);
8576 /* set statically allocated (struct special) settings */
8577 if (strlen(default_script) == 0)
8578 expand_tilde(default_script, sizeof default_script,
8579 XT_DS_DEFAULT_SCRIPT);
8580 if (strlen(ssl_ca_file) == 0)
8581 expand_tilde(ssl_ca_file, sizeof ssl_ca_file,
8582 XT_DS_SSL_CA_FILE);
8584 /* soup session */
8585 session = webkit_get_default_session();
8587 /* read config file */
8588 if (strlen(conf) == 0)
8589 snprintf(conf, sizeof conf, "%s" PS ".%s",
8590 pwd->pw_dir, XT_CONF_FILE);
8591 config_parse(conf, 0);
8593 /* read preloaded HSTS list */
8594 if (preload_strict_transport) {
8595 snprintf(conf, sizeof conf, "%s" PS "%s",
8596 resource_dir, XT_HSTS_PRELOAD_FILE);
8597 config_parse(conf, 0);
8600 /* check whether to read in a crapton of additional http headers */
8601 if (anonymize_headers) {
8602 snprintf(conf, sizeof conf, "%s" PS "%s",
8603 resource_dir, XT_USER_AGENT_FILE);
8604 config_parse(conf, 0);
8605 snprintf(conf, sizeof conf, "%s" PS "%s",
8606 resource_dir, XT_HTTP_ACCEPT_FILE);
8607 config_parse(conf, 0);
8610 /* init fonts */
8611 cmd_font = pango_font_description_from_string(cmd_font_name);
8612 oops_font = pango_font_description_from_string(oops_font_name);
8613 statusbar_font = pango_font_description_from_string(statusbar_font_name);
8614 tabbar_font = pango_font_description_from_string(tabbar_font_name);
8616 /* working directory */
8617 if (strlen(work_dir) == 0)
8618 snprintf(work_dir, sizeof work_dir, "%s" PS "%s",
8619 pwd->pw_dir, XT_DIR);
8620 xxx_dir(work_dir);
8622 /* icon cache dir */
8623 snprintf(cache_dir, sizeof cache_dir, "%s" PS "%s", work_dir, XT_CACHE_DIR);
8624 xxx_dir(cache_dir);
8626 /* certs dir */
8627 snprintf(certs_dir, sizeof certs_dir, "%s" PS "%s", work_dir, XT_CERT_DIR);
8628 xxx_dir(certs_dir);
8630 /* cert changes dir */
8631 snprintf(certs_cache_dir, sizeof certs_cache_dir, "%s" PS "%s",
8632 work_dir, XT_CERT_CACHE_DIR);
8633 xxx_dir(certs_cache_dir);
8635 /* sessions dir */
8636 snprintf(sessions_dir, sizeof sessions_dir, "%s" PS "%s",
8637 work_dir, XT_SESSIONS_DIR);
8638 xxx_dir(sessions_dir);
8640 /* js dir */
8641 snprintf(js_dir, sizeof js_dir, "%s" PS "%s", work_dir, XT_JS_DIR);
8642 xxx_dir(js_dir);
8644 /* temp dir */
8645 snprintf(temp_dir, sizeof temp_dir, "%s" PS "%s", work_dir, XT_TEMP_DIR);
8646 xxx_dir(temp_dir);
8648 /* runtime settings that can override config file */
8649 if (runtime_settings[0] != '\0')
8650 config_parse(runtime_settings, 1);
8652 /* download dir */
8653 if (!strcmp(download_dir, pwd->pw_dir))
8654 strlcat(download_dir, PS "downloads", sizeof download_dir);
8655 xxx_dir(download_dir);
8657 /* first start file */
8658 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_SOD_FILE);
8659 if (stat(file, &sb)) {
8660 warnx("start of day file doesn't exist, creating it");
8661 if ((f = fopen(file, "w")) == NULL)
8662 err(1, "startofday");
8663 if (fputs(version, f) == EOF)
8664 err(1, "fputs");
8665 fclose(f);
8667 /* welcome user */
8668 welcome();
8669 } else {
8670 if ((f = fopen(file, "r+")) == NULL)
8671 err(1, "startofday");
8672 if (fgets(sodversion, sizeof sodversion, f) == NULL)
8673 err(1, "fgets");
8674 sodversion[strcspn(sodversion, "\n")] = '\0';
8675 if (strcmp(version, sodversion)) {
8676 if ((f = freopen(file, "w", f)) == NULL)
8677 err(1, "startofday");
8678 if (fputs(version, f) == EOF)
8679 err(1, "fputs");
8681 /* upgrade, say something smart */
8682 welcome();
8684 fclose(f);
8687 /* favorites file */
8688 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_FAVS_FILE);
8689 if (stat(file, &sb)) {
8690 warnx("favorites file doesn't exist, creating it");
8691 if ((f = fopen(file, "w")) == NULL)
8692 err(1, "favorites");
8693 fclose(f);
8696 /* quickmarks file */
8697 snprintf(file, sizeof file, "%s" PS "%s", work_dir, XT_QMARKS_FILE);
8698 if (stat(file, &sb)) {
8699 warnx("quickmarks file doesn't exist, creating it");
8700 if ((f = fopen(file, "w")) == NULL)
8701 err(1, "quickmarks");
8702 fclose(f);
8705 /* search history */
8706 if (history_autosave) {
8707 snprintf(search_file, sizeof search_file, "%s" PS "%s",
8708 work_dir, XT_SEARCH_FILE);
8709 if (stat(search_file, &sb)) {
8710 warnx("search history file doesn't exist, creating it");
8711 if ((f = fopen(search_file, "w")) == NULL)
8712 err(1, "search_history");
8713 fclose(f);
8715 history_read(&shl, search_file, &search_history_count);
8718 /* command history */
8719 if (history_autosave) {
8720 snprintf(command_file, sizeof command_file, "%s" PS "%s",
8721 work_dir, XT_COMMAND_FILE);
8722 if (stat(command_file, &sb)) {
8723 warnx("command history file doesn't exist, creating it");
8724 if ((f = fopen(command_file, "w")) == NULL)
8725 err(1, "command_history");
8726 fclose(f);
8728 history_read(&chl, command_file, &cmd_history_count);
8731 /* cookies */
8732 setup_cookies();
8734 /* guess_search regex */
8735 if (url_regex == NULL)
8736 url_regex = g_strdup(XT_URL_REGEX);
8737 if (url_regex)
8738 if (regcomp(&url_re, url_regex, REG_EXTENDED | REG_NOSUB))
8739 startpage_add("invalid url regex %s", url_regex);
8741 /* proxy */
8742 env_proxy = getenv("http_proxy");
8743 if (env_proxy)
8744 setup_proxy(env_proxy);
8745 else {
8746 env_proxy = getenv("HTTP_PROXY");
8747 if (env_proxy)
8748 setup_proxy(env_proxy);
8749 else
8750 setup_proxy(http_proxy);
8753 /* the user can optionally have the proxy disabled at startup */
8754 if ((http_proxy_starts_enabled == 0) && (http_proxy != NULL)) {
8755 http_proxy_save = g_strdup(http_proxy);
8756 setup_proxy(NULL);
8759 #ifndef XT_SOCKET_DISABLE
8760 if (opte) {
8761 send_cmd_to_socket(argv[0]);
8762 exit(0);
8764 #else
8765 opte = opte; /* shut mingw up */
8766 #endif
8767 /* set some connection parameters */
8768 g_object_set(session, "max-conns", max_connections, (char *)NULL);
8769 g_object_set(session, "max-conns-per-host", max_host_connections,
8770 (char *)NULL);
8771 g_object_set(session, SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
8772 (char *)NULL);
8774 g_signal_connect(session, "request-queued", G_CALLBACK(session_rq_cb),
8775 NULL);
8777 #ifndef XT_SOCKET_DISABLE
8778 /* see if there is already a xombrero running */
8779 if (single_instance && is_running()) {
8780 optn = 1;
8781 warnx("already running");
8784 if (optn) {
8785 while (argc) {
8786 cmd = g_strdup_printf("%s %s", "tabnew", argv[0]);
8787 send_cmd_to_socket(cmd);
8788 if (cmd)
8789 g_free(cmd);
8791 argc--;
8792 argv++;
8794 exit(0);
8796 #else
8797 optn = optn; /* shut mingw up */
8798 #endif
8799 /* tld list */
8800 tld_tree_init();
8802 /* cache mode */
8803 if (enable_cache)
8804 webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER);
8805 else
8806 webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
8808 if (enable_strict_transport)
8809 strict_transport_init();
8811 /* uri completion */
8812 completion_model = gtk_list_store_new(1, G_TYPE_STRING);
8814 /* buffers */
8815 buffers_store = gtk_list_store_new
8816 (NUM_COLS, G_TYPE_UINT, GDK_TYPE_PIXBUF, G_TYPE_STRING);
8818 qmarks_load();
8820 /* go graphical */
8821 create_canvas();
8822 notebook_tab_set_visibility();
8824 if (save_global_history)
8825 restore_global_history();
8827 /* restore session list */
8828 restore_sessions_list();
8830 if (!strcmp(named_session, XT_SAVED_TABS_FILE))
8831 restore_saved_tabs();
8832 else {
8833 a.s = named_session;
8834 a.i = XT_SES_DONOTHING;
8835 open_tabs(NULL, &a);
8838 /* see if we have an exception */
8839 if (!TAILQ_EMPTY(&spl)) {
8840 create_new_tab("about:startpage", NULL, focus, -1);
8841 focus = 0;
8844 while (argc) {
8845 create_new_tab(argv[0], NULL, focus, -1);
8846 focus = 0;
8848 argc--;
8849 argv++;
8852 if (TAILQ_EMPTY(&tabs))
8853 create_new_tab(home, NULL, 1, -1);
8854 #ifndef XT_SOCKET_DISABLE
8855 if (enable_socket)
8856 if ((s = build_socket()) != -1) {
8857 channel = g_io_channel_unix_new(s);
8858 g_io_add_watch(channel, G_IO_IN, socket_watcher, NULL);
8860 #endif
8862 #if GTK_CHECK_VERSION(3, 0, 0)
8863 setup_css();
8864 #endif
8866 gtk_main();
8868 gnutls_global_deinit();
8870 if (url_regex)
8871 regfree(&url_re);
8873 return (0);