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.
27 char *version
= XOMBRERO_VERSION
;
30 uint32_t swm_debug
= 0
61 TAILQ_ENTRY(session
) entry
;
64 TAILQ_HEAD(session_list
, session
);
67 TAILQ_ENTRY(undo
) entry
;
70 int back
; /* Keeps track of how many back
71 * history items there are. */
73 TAILQ_HEAD(undo_tailq
, undo
);
75 struct command_entry
{
77 TAILQ_ENTRY(command_entry
) entry
;
79 TAILQ_HEAD(command_list
, command_entry
);
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)
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 "://"
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
);
201 extern char *__progname
;
202 char * const *start_argv
;
204 GtkWidget
*main_window
;
205 GtkNotebook
*notebook
;
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
;
215 struct wl_list js_wl
;
216 struct wl_list pl_wl
;
217 struct wl_list force_https
;
219 struct strict_transport_tree st_tree
;
220 struct undo_tailq undos
;
221 struct keybinding_list kbl
;
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
;
235 int cmd_history_count
= 0;
236 int search_history_count
= 0;
238 uint64_t blocked_cookies
= 0;
239 char named_session
[PATH_MAX
];
240 GtkListStore
*completion_model
;
241 GtkListStore
*buffers_store
;
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
*);
256 history_delete(struct command_list
*l
, int *counter
)
258 struct command_entry
*c
;
260 if (l
== NULL
|| counter
== NULL
)
263 c
= TAILQ_LAST(l
, command_list
);
267 TAILQ_REMOVE(l
, c
, entry
);
274 history_add(struct command_list
*list
, char *file
, char *l
, int *counter
)
276 struct command_entry
*c
;
279 if (list
== NULL
|| l
== NULL
|| counter
== NULL
)
282 /* don't add the same line */
283 c
= TAILQ_FIRST(list
);
285 if (!strcmp(c
->line
+ 1 /* skip space */, l
))
288 c
= g_malloc0(sizeof *c
);
289 c
->line
= g_strdup_printf(" %s", l
);
292 TAILQ_INSERT_HEAD(list
, c
, entry
);
295 history_delete(list
, counter
);
297 if (history_autosave
&& file
) {
298 f
= fopen(file
, "w");
300 show_oops(NULL
, "couldn't write history %s", file
);
304 TAILQ_FOREACH_REVERSE(c
, list
, command_list
, entry
) {
306 fprintf(f
, "%s\n", c
->line
);
314 history_read(struct command_list
*list
, char *file
, int *counter
)
317 char *s
, line
[65536];
319 if (list
== NULL
|| file
== NULL
)
322 f
= fopen(file
, "r");
324 startpage_add("couldn't open history file %s", file
);
329 s
= fgets(line
, sizeof line
, f
);
330 if (s
== NULL
|| feof(f
) || ferror(f
))
332 if ((s
= strchr(line
, '\n')) == NULL
) {
333 startpage_add("invalid history file %s", file
);
339 history_add(list
, NULL
, line
+ 1, counter
);
347 /* marks array storage. */
351 if (i
< 0 || i
>= XT_NOMARKS
)
362 if ((ret
= strchr(XT_MARKS
, m
)) != NULL
)
363 return ret
- XT_MARKS
;
368 /* quickmarks array storage. */
372 if (i
< 0 || i
>= XT_NOQMARKS
)
383 if ((ret
= strchr(XT_QMARKS
, m
)) != NULL
)
384 return ret
- XT_QMARKS
;
390 is_g_object_setting(GObject
*o
, char *str
)
392 guint n_props
= 0, i
;
393 GParamSpec
**proplist
;
399 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
402 for (i
= 0; i
< n_props
; i
++) {
403 if (! strcmp(proplist
[i
]->name
, str
)) {
414 get_current_tab(void)
418 TAILQ_FOREACH(t
, &tabs
, entry
) {
419 if (t
->tab_id
== gtk_notebook_get_current_page(notebook
))
423 warnx("%s: no current tab", __func__
);
429 set_ssl_ca_file(struct settings
*s
, char *file
)
433 if (file
== NULL
|| strlen(file
) == 0)
435 if (stat(file
, &sb
)) {
436 warnx("no CA file: %s", file
);
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
,
448 set_status(struct tab
*t
, gchar
*fmt
, ...)
455 status
= g_strdup_vprintf(fmt
, ap
);
457 gtk_entry_set_text(GTK_ENTRY(t
->sbe
.uri
), status
);
461 else if (strcmp(t
->status
, status
)) {
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
);
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
), "");
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
);
500 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
501 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
503 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
504 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
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
);
523 sort_tabs_by_page_num(struct tab
***stabs
)
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
;
539 buffers_make_list(void)
542 const gchar
*title
= NULL
;
543 struct tab
**stabs
= NULL
;
546 num_tabs
= sort_tabs_by_page_num(&stabs
);
548 for (i
= 0; i
< num_tabs
; 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
555 COL_FAVICON
, gtk_image_get_pixbuf
556 (GTK_IMAGE(stabs
[i
]->tab_elems
.favicon
)),
565 show_buffers(struct tab
*t
)
568 GtkTreeSelection
*sel
;
572 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
)))
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
));
590 toggle_buffers(struct tab
*t
)
592 if (gtk_widget_get_visible(t
->buffers
))
599 buffers(struct tab
*t
, struct karg
*args
)
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
));
618 gtk_widget_set_name(h_scrollbar
, XT_CSS_HIDDEN
);
619 gtk_widget_set_name(v_scrollbar
, XT_CSS_HIDDEN
);
621 gtk_widget_set_name(h_scrollbar
, "");
622 gtk_widget_set_name(v_scrollbar
, "");
627 return (visible
== 0);
632 hide_oops(struct tab
*t
)
634 gtk_widget_hide(t
->oops
);
638 show_oops(struct tab
*at
, const char *fmt
, ...)
642 struct tab
*t
= NULL
;
648 if ((t
= get_current_tab()) == NULL
)
654 if ((msg
= g_strdup_vprintf(fmt
, ap
)) == NULL
)
655 errx(1, "show_oops failed");
658 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
659 gtk_widget_hide(t
->cmd
);
660 gtk_widget_show(t
->oops
);
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
;
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
{
744 valid_url_type(const char *url
)
748 for (i
= 0; i
< LENGTH(vut
); i
++)
749 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
756 match_alias(const char *url_in
)
760 char *url_out
= NULL
, *search
, *enc_arg
;
763 search
= g_strdup(url_in
);
765 if (strsep(&arg
, " \t") == NULL
) {
766 show_oops(NULL
, "match_alias: NULL URL");
770 TAILQ_FOREACH(a
, &aliases
, entry
) {
771 if (!strcmp(search
, a
->a_name
))
776 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
780 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
781 sv
= g_strsplit(a
->a_uri
, "%s", 2);
783 url_out
= g_strjoinv(enc_arg
, sv
);
785 url_out
= g_strjoinv("", sv
);
795 guess_url_type(const char *url_in
)
798 char cwd
[PATH_MAX
] = {0};
799 char *url_out
= NULL
, *enc_search
= NULL
;
805 /* substitute aliases */
806 url_out
= match_alias(url_in
);
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
);
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
);
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
);
838 if (getcwd(cwd
, PATH_MAX
) != NULL
) {
839 path
= g_strdup_printf("%s" PS
"%s", cwd
,
841 url_out
= g_filename_to_uri(path
, NULL
, NULL
);
846 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
848 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
854 set_normal_tab_meaning(struct tab
*t
)
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
;
867 load_uri(struct tab
*t
, const gchar
*uri
)
870 gchar
*newuri
= NULL
;
876 /* Strip leading spaces. */
877 while (*uri
&& isspace((unsigned char)*uri
))
880 if (strlen(uri
) == 0) {
885 set_normal_tab_meaning(t
);
887 if (valid_url_type(uri
)) {
888 if ((newuri
= guess_url_type(uri
)) != NULL
)
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
),
910 show_oops(t
, "invalid about page");
914 /* remove old HTTPS cert chain (if any) */
920 set_status(t
, "Loading: %s", (char *)uri
);
922 webkit_web_view_load_uri(t
->wv
, uri
);
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
)
936 if (t
->xtp_meaning
== XT_XTP_TAB_MEANING_NORMAL
)
937 uri
= webkit_web_view_get_uri(t
->wv
);
939 /* use tmp_uri to make sure it is g_freed */
942 t
->tmp_uri
= g_strdup_printf("%s%s", XT_URI_ABOUT
,
943 about_list
[t
->xtp_meaning
].name
);
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
)
961 title
= webkit_web_view_get_title(t
->wv
);
962 if ((set
= title
? title
: get_uri(t
)))
966 set
= window
? XT_NAME
: "(untitled)";
972 find_mime_type(char *mime_type
)
974 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
976 TAILQ_FOREACH(m
, &mtl
, entry
) {
978 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
981 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
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
998 html_escape(const char *val
)
1006 sv
= g_strsplit(val
, "&", -1);
1007 s
= g_strjoinv("&", sv
);
1010 sv
= g_strsplit(val
, "<", -1);
1011 s
= g_strjoinv("<", sv
);
1018 wl_find_uri(const gchar
*s
, struct wl_list
*wl
)
1024 if (s
== NULL
|| wl
== 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://")];
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') {
1040 w
= wl_find(ss
, wl
);
1049 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
1055 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
1059 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
1062 JSStringGetUTF8CString(jsref
, s
, l
);
1063 JSStringRelease(jsref
);
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
;
1079 JSValueRef val
, exception
;
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
);
1095 es
= js_ref_to_string(ctx
, exception
);
1097 DNPRINTF(XT_D_JS
, "%s: exception %s\n", __func__
, es
);
1102 es
= js_ref_to_string(ctx
, val
);
1105 if (!strncmp(es
, XT_JS_DONE
, XT_JS_DONE_LEN
))
1107 if (!strncmp(es
, XT_JS_INSERT
, XT_JS_INSERT_LEN
))
1111 DNPRINTF(XT_D_JS
, "%s: val %s\n", __func__
, es
);
1120 enable_hints(struct tab
*t
)
1122 DNPRINTF(XT_D_JS
, "%s: tab %d\n", __func__
, t
->tab_id
);
1125 run_script(t
, "hints.createHints('', 'F');");
1127 run_script(t
, "hints.createHints('', 'f');");
1128 t
->mode
= XT_MODE_HINT
;
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
;
1142 passthrough(struct tab
*t
, struct karg
*args
)
1144 t
->mode
= XT_MODE_PASSTHROUGH
;
1149 modurl(struct tab
*t
, struct karg
*args
)
1151 const gchar
*uri
= 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
))) &&
1158 u
= g_strdup_printf("www.%s.com", uri
);
1159 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), u
);
1161 activate_uri_entry_cb(t
->uri_entry
, t
);
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
)
1184 apply_style(struct tab
*t
)
1187 g_object_set(G_OBJECT(t
->settings
),
1188 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1192 remove_style(struct tab
*t
)
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'};
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
);
1212 script_uri
= g_strdup(userstyle
);
1214 if (script_uri
== NULL
)
1218 case XT_STYLE_CURRENT_TAB
:
1219 if (t
->styled
&& !strcmp(script_uri
, t
->stylesheet
))
1223 g_free(t
->stylesheet
);
1224 t
->stylesheet
= g_strdup(script_uri
);
1228 case XT_STYLE_GLOBAL
:
1229 if (userstyle_global
&& !strcmp(script_uri
, t
->stylesheet
)) {
1230 userstyle_global
= 0;
1231 TAILQ_FOREACH(tt
, &tabs
, entry
)
1234 userstyle_global
= 1;
1236 /* need to save this stylesheet for new tabs */
1239 stylesheet
= g_strdup(script_uri
);
1241 TAILQ_FOREACH(tt
, &tabs
, entry
) {
1243 g_free(tt
->stylesheet
);
1244 tt
->stylesheet
= g_strdup(script_uri
);
1257 quit(struct tab
*t
, struct karg
*args
)
1259 if (save_global_history
)
1260 save_global_history_to_disk(t
);
1268 restore_sessions_list(void)
1271 struct dirent
*dp
= NULL
;
1275 sdir
= opendir(sessions_dir
);
1277 while ((dp
= readdir(sdir
)) != NULL
) {
1278 #if defined __MINGW32__
1279 reg
= 1; /* windows only has regular files */
1281 reg
= dp
->d_type
== DT_REG
;
1284 s
= g_malloc(sizeof(struct session
));
1285 s
->name
= g_strdup(dp
->d_name
);
1286 TAILQ_INSERT_TAIL(&sessions
, s
, entry
);
1294 open_tabs(struct tab
*t
, struct karg
*a
)
1296 char file
[PATH_MAX
];
1300 struct tab
*ti
, *tt
;
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
)
1312 if ((uri
= fparseln(f
, NULL
, NULL
, "\0\0\0", 0)) == NULL
) {
1313 if (feof(f
) || ferror(f
))
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
);
1325 create_new_tab(uri
, NULL
, 1, -1);
1331 /* close open tabs */
1332 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
1334 tt
= TAILQ_FIRST(&tabs
);
1355 restore_saved_tabs(void)
1357 char file
[PATH_MAX
];
1358 int unlink_file
= 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
;
1369 a
.s
= XT_RESTART_TABS_FILE
;
1372 a
.i
= XT_SES_DONOTHING
;
1373 rv
= open_tabs(NULL
, &a
);
1382 save_tabs(struct tab
*t
, struct karg
*a
)
1384 char file
[PATH_MAX
];
1386 int num_tabs
= 0, i
;
1387 struct tab
**stabs
= NULL
;
1389 /* tab may be null here */
1394 snprintf(file
, sizeof file
, "%s" PS
"%s",
1395 sessions_dir
, named_session
);
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
));
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
++)
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
)));
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",
1434 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
1446 expand_tilde(char *path
, size_t len
, const char *s
)
1450 char user
[LOGIN_NAME_MAX
];
1453 if (path
== NULL
|| s
== NULL
)
1454 errx(1, "expand_tilde");
1457 strlcpy(path
, sc
, len
);
1462 for (i
= 0; s
[i
] != PSC
&& s
[i
] != '\0'; ++i
)
1467 pwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1469 strlcpy(path
, sc
, len
);
1471 snprintf(path
, len
, "%s%s", pwd
->pw_dir
, s
);
1475 run_page_script(struct tab
*t
, struct karg
*args
)
1478 char *tmp
, script
[PATH_MAX
];
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");
1487 if ((uri
= get_uri(t
)) == NULL
) {
1488 show_oops(t
, "tab is empty, not running script");
1492 expand_tilde(script
, sizeof script
, tmp
);
1495 sv
[1] = (char *)uri
;
1497 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
1499 show_oops(t
, "%s: could not spawn process: %s %s", __func__
,
1503 show_oops(t
, "running: %s %s", sv
[0], sv
[1]);
1509 yank_uri(struct tab
*t
, struct karg
*args
)
1512 GtkClipboard
*clipboard
, *primary
;
1514 if ((uri
= get_uri(t
)) == NULL
)
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);
1526 paste_uri(struct tab
*t
, struct karg
*args
)
1528 GtkClipboard
*clipboard
, *primary
;
1529 gchar
*c
= NULL
, *p
= NULL
, *uri
;
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
);
1539 /* Windows try clipboard first */
1542 /* UNIX try primary first */
1545 /* replace all newlines with spaces */
1546 for (i
= 0; uri
[i
] != '\0'; ++i
)
1550 while (*uri
&& isspace((unsigned char)*uri
))
1552 if (strlen(uri
) == 0) {
1553 show_oops(t
, "empty paste buffer");
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");
1562 if (args
->i
== XT_PASTE_CURRENT_TAB
)
1564 else if (args
->i
== XT_PASTE_NEW_TAB
)
1565 create_new_tab(uri
, NULL
, 1, -1);
1578 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
1583 g_object_get(G_OBJECT(t
->settings
),
1584 "enable-scripts", &es
, (char *)NULL
);
1589 set
= XT_WL_DISABLE
;
1591 a
.i
= set
| XT_WL_TOPLEVEL
;
1594 a
.i
= set
| XT_WL_TOPLEVEL
;
1597 a
.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
| XT_WL_RELOAD
;
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
)
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
);
1626 focus_webview(struct tab
*t
)
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
)
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
));
1654 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
1658 for (i
= 0; i
< cert_count
; i
++)
1659 gnutls_x509_crt_deinit(certs
[i
]);
1663 #if GTK_CHECK_VERSION(3, 0, 0)
1665 statusbar_modify_attr(struct tab
*t
, const char *css_name
)
1667 gtk_widget_set_name(t
->sbe
.ebox
, css_name
);
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
);
1685 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
1686 size_t cert_count
, const char *domain
, const char *dir
)
1689 char file
[PATH_MAX
];
1690 char *cert_buf
= NULL
;
1695 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
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
));
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
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");
1719 cert_buf
= gnutls_malloc(cert_buf_sz
* sizeof(char));
1720 if (cert_buf
== NULL
) {
1721 show_oops(t
, "gnutls_x509_crt_export failed");
1724 rv
= gnutls_x509_crt_export(certs
[i
],
1725 GNUTLS_X509_FMT_PEM
, cert_buf
, &cert_buf_sz
);
1727 show_oops(t
, "gnutls_x509_crt_export failure: %s",
1728 gnutls_strerror(rv
));
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
));
1736 gnutls_free(cert_buf
);
1742 gnutls_free(cert_buf
);
1754 get_local_cert_chain(const char *uri
, size_t *ncerts
, const char **error_str
,
1758 unsigned char cert_buf
[64 * 1024] = {0};
1759 gnutls_datum_t data
;
1760 unsigned int max_certs
= XT_MAX_CERTS
;
1762 char file
[PATH_MAX
];
1764 gnutls_x509_crt_t
*certs
;
1766 if ((su
= soup_uri_new(uri
)) == NULL
) {
1767 *error_str
= "Invalid URI";
1771 snprintf(file
, sizeof file
, "%s" PS
"%s", dir
, su
->host
);
1773 if ((f
= fopen(file
, "r")) == NULL
) {
1774 *error_str
= "Could not read local cert";
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";
1784 certs
= gnutls_malloc(sizeof(*certs
) * max_certs
);
1785 if (certs
== NULL
) {
1786 *error_str
= "Error allocating memory";
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) {
1794 *error_str
= "Error reading local cert chain";
1798 *ncerts
= max_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.
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
;
1815 *error_str
= "Error reading remote cert chain";
1819 certs
= gnutls_malloc(sizeof(*certs
) * max_certs
);
1820 if (certs
== NULL
) {
1821 *error_str
= "Error allocating memory";
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) {
1829 *error_str
= "Error reading remote cert chain";
1833 *ncerts
= max_certs
;
1839 cert_cmd(struct tab
*t
, struct karg
*args
)
1841 const gchar
*uri
, *error_str
= NULL
;
1843 gnutls_x509_crt_t
*certs
;
1846 #if !GTK_CHECK_VERSION(3, 0, 0)
1853 if (args
->s
!= NULL
) {
1855 } else if ((uri
= get_uri(t
)) == NULL
) {
1856 show_oops(t
, "Invalid URI");
1859 if ((su
= soup_uri_new(uri
)) == NULL
) {
1860 show_oops(t
, "Invalid URI");
1865 * if we're only showing the local certs, don't open a socket and get
1868 if (args
->i
& XT_SHOW
&& args
->i
& XT_CACHE
) {
1869 certs
= get_local_cert_chain(uri
, &cert_count
, &error_str
,
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
);
1876 show_oops(t
, "%s", error_str
);
1884 certs
= get_chain_for_pem(t
->pem
, &cert_count
, &error_str
);
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
);
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
);
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
);
1910 if (error_str
&& strlen(error_str
))
1911 show_oops(t
, "%s", error_str
);
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.
1924 check_local_certs(const char *file
, GTlsCertificate
*cert
, char **chain
)
1926 char r_cert_buf
[64 * 1024];
1928 GTlsCertificate
*tmpcert
= NULL
;
1929 GTlsCertificate
*issuer
= NULL
;
1932 enum cert_trust rv
= CERT_LOCAL
;
1935 if ((f
= fopen(file
, "r")) == NULL
) {
1936 /* no local cert to check */
1937 rv
= CERT_UNTRUSTED
;
1942 g_object_get(G_OBJECT(cert
), "certificate-pem", &pem
,
1944 g_object_get(G_OBJECT(cert
), "issuer", &issuer
, NULL
);
1948 g_object_get(G_OBJECT(tmpcert
), "issuer", &issuer
,
1953 g_object_get(G_OBJECT(tmpcert
), "certificate-pem", &pem
,
1958 if (tmpcert
!= NULL
)
1959 g_object_unref(G_OBJECT(tmpcert
));
1962 if (fread(r_cert_buf
, strlen(pem
), 1, f
) != 1 && !feof(f
))
1964 if (bcmp(r_cert_buf
, pem
, strlen(pem
)))
1967 tmp
= g_strdup_printf("%s%s", *chain
, pem
);
1974 g_object_unref(G_OBJECT(issuer
));
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
;
1989 chain
= g_strdup("");
1990 switch(check_local_certs(file
, cert
, &chain
)) {
1992 /* The cached certificate is identical */
1994 case CERT_TRUSTED
: /* FALLTHROUGH */
1995 case CERT_UNTRUSTED
:
1996 /* cache new certificate */
2001 if ((soupuri
= soup_uri_new(uri
)) == NULL
||
2002 soupuri
->host
== NULL
)
2004 if ((w
= wl_find(soupuri
->host
, &svil
)) != NULL
)
2006 t
->xtp_meaning
= XT_XTP_TAB_MEANING_SV
;
2007 args
.s
= g_strdup((char *)uri
);
2008 xtp_page_sv(t
, &args
);
2014 soup_uri_free(soupuri
);
2021 remove_cookie(int index
)
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
++) {
2035 print_cookie("remove cookie", c
);
2036 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2041 soup_cookies_free(cf
);
2047 remove_cookie_domain(int domain_id
)
2049 int domain_count
, rv
= 1;
2054 DNPRINTF(XT_D_COOKIE
, "remove_cookie_domain: %d\n", domain_id
);
2057 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2059 for (domain_count
= 0; cf
; cf
= cf
->next
) {
2062 if (strcmp(last_domain
, c
->domain
) != 0) {
2064 last_domain
= c
->domain
;
2067 if (domain_count
< domain_id
)
2069 else if (domain_count
> domain_id
)
2072 print_cookie("remove cookie", c
);
2073 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2077 soup_cookies_free(cf
);
2089 DNPRINTF(XT_D_COOKIE
, "remove_cookie_all\n");
2091 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
2093 for (; cf
; cf
= cf
->next
) {
2096 print_cookie("remove cookie", c
);
2097 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
2101 soup_cookies_free(cf
);
2107 toplevel_cmd(struct tab
*t
, struct karg
*args
)
2109 js_toggle_cb(t
->js_toggle
, t
);
2115 can_go_back_for_real(struct tab
*t
)
2118 WebKitWebHistoryItem
*item
;
2124 if (t
->item
!= NULL
)
2127 /* rely on webkit to make sure we can go backward when on an about page */
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
);
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
))
2145 can_go_forward_for_real(struct tab
*t
)
2148 WebKitWebHistoryItem
*item
;
2154 /* rely on webkit to make sure we can go forward when on an about page */
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
);
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
))
2172 go_back_for_real(struct tab
*t
)
2175 WebKitWebHistoryItem
*item
;
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
);
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
);
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
);
2199 go_forward_for_real(struct tab
*t
)
2202 WebKitWebHistoryItem
*item
;
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
);
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
);
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
);
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
);
2235 set_normal_tab_meaning(t
);
2237 if (args
->i
== XT_NAV_BACK
)
2238 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
2240 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
2247 return (XT_CB_PASSTHROUGH
);
2248 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2250 return (XT_CB_PASSTHROUGH
);
2253 go_back_for_real(t
);
2255 case XT_NAV_FORWARD
:
2258 return (XT_CB_PASSTHROUGH
);
2259 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
2261 return (XT_CB_PASSTHROUGH
);
2264 go_forward_for_real(t
);
2267 frame
= webkit_web_view_get_main_frame(t
->wv
);
2268 webkit_web_frame_reload(frame
);
2271 frame
= webkit_web_view_get_main_frame(t
->wv
);
2272 webkit_web_frame_stop_loading(frame
);
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
;
2288 case XT_MOVE_BOTTOM
:
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
;
2299 adjust
= t
->adjust_h
;
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
);
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
);
2320 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2325 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
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
);
2333 case XT_MOVE_FARLEFT
:
2334 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2335 gtk_adjustment_set_value(adjust
, lower
);
2337 case XT_MOVE_PAGEDOWN
:
2339 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2341 case XT_MOVE_PAGEUP
:
2343 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2345 case XT_MOVE_HALFDOWN
:
2347 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
2349 case XT_MOVE_HALFUP
:
2351 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
2353 case XT_MOVE_CENTER
:
2354 t
->mark
[marktoindex('\'')] = gtk_adjustment_get_value(t
->adjust_v
);
2355 args
->s
= g_strdup("50.0");
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
)
2363 gtk_adjustment_set_value(adjust
, pos
);
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
);
2375 url_set_visibility(void)
2379 TAILQ_FOREACH(t
, &tabs
, entry
)
2380 if (show_url
== 0) {
2381 gtk_widget_hide(t
->toolbar
);
2384 gtk_widget_show(t
->toolbar
);
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
);
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
);
2405 statusbar_set_visibility(void)
2409 TAILQ_FOREACH(t
, &tabs
, entry
){
2410 if (show_statusbar
== 0)
2411 gtk_widget_hide(t
->statusbar
);
2413 gtk_widget_show(t
->statusbar
);
2420 url_set(struct tab
*t
, int enable_url_entry
)
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);
2432 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
2433 GTK_ENTRY_ICON_PRIMARY
);
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
),
2444 fullscreen(struct tab
*t
, struct karg
*args
)
2446 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
2449 return (XT_CB_PASSTHROUGH
);
2451 if (show_url
== 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) {
2472 statusbar_set_visibility();
2473 } else if (show_statusbar
== 0) {
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
);
2488 return (XT_CB_PASSTHROUGH
);
2492 if (show_url
== 0) {
2494 url_set_visibility();
2498 if (show_url
== 1) {
2500 url_set_visibility();
2508 tabaction(struct tab
*t
, struct karg
*args
)
2510 int rv
= XT_CB_HANDLED
;
2511 char *url
= args
->s
;
2513 struct tab
*tt
, *tv
;
2515 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
2518 return (XT_CB_PASSTHROUGH
);
2522 if (strlen(url
) > 0)
2523 create_new_tab(url
, NULL
, 1, args
->precount
);
2525 create_new_tab(NULL
, NULL
, 1, args
->precount
);
2528 if (args
->precount
< 0)
2531 TAILQ_FOREACH(tt
, &tabs
, entry
)
2532 if (tt
->tab_id
== args
->precount
- 1) {
2537 case XT_TAB_DELQUIT
:
2538 if (gtk_notebook_get_n_pages(notebook
) > 1)
2544 TAILQ_FOREACH_SAFE(tt
, &tabs
, entry
, tv
)
2549 if (strlen(url
) > 0)
2552 rv
= XT_CB_PASSTHROUGH
;
2558 if (show_tabs
== 0) {
2560 notebook_tab_set_visibility();
2564 if (show_tabs
== 1) {
2566 notebook_tab_set_visibility();
2569 case XT_TAB_NEXTSTYLE
:
2570 if (tab_style
== XT_TABS_NORMAL
) {
2571 tab_style
= XT_TABS_COMPACT
;
2572 recolor_compact_tabs();
2575 tab_style
= XT_TABS_NORMAL
;
2576 notebook_tab_set_visibility();
2578 case XT_TAB_UNDO_CLOSE
:
2579 if (undo_count
== 0) {
2580 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close",
2585 u
= TAILQ_FIRST(&undos
);
2586 create_new_tab(u
->uri
, u
, 1, -1);
2588 TAILQ_REMOVE(&undos
, u
, entry
);
2590 /* u->history is freed in create_new_tab() */
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
;
2617 rv
= XT_CB_PASSTHROUGH
;
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
)
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
);
2670 if (args
->precount
< 0)
2671 dest
= dest
== n
- 1 ? 0 : dest
+ 1;
2673 dest
= args
->precount
- 1;
2677 if (args
->precount
< 0)
2680 dest
-= args
->precount
% n
;
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
);
2712 const char *(*f
)(struct tab
*);
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
;
2725 if (t
== NULL
|| args
== NULL
) {
2726 show_oops(NULL
, "command invalid parameters");
2727 return (XT_CB_PASSTHROUGH
);
2738 if (cmd_prefix
== 0) {
2739 if (args
->s
!= NULL
&& strlen(args
->s
) != 0) {
2740 sp
= g_strdup_printf(":%s", args
->s
);
2745 sp
= g_strdup_printf(":%d", cmd_prefix
);
2755 for (i
= 0; i
< LENGTH(subs
); ++i
) {
2756 sv
= g_strsplit(sl
, subs
[i
].s
, -1);
2759 sl
= g_strjoinv(subs
[i
].f(t
), sv
);
2765 t
->mode
= XT_MODE_HINT
;
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.
2779 t
->mode
= XT_MODE_HINT
;
2780 a
.i
= XT_HINT_NEWTAB
;
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
);
2801 return (XT_CB_HANDLED
);
2805 search(struct tab
*t
, struct karg
*args
)
2809 if (t
== NULL
|| args
== NULL
) {
2810 show_oops(NULL
, "search invalid parameters");
2815 case XT_SEARCH_NEXT
:
2816 d
= t
->search_forward
;
2818 case XT_SEARCH_PREV
:
2819 d
= !t
->search_forward
;
2822 return (XT_CB_PASSTHROUGH
);
2825 if (t
->search_text
== NULL
) {
2826 if (global_search
== NULL
)
2827 return (XT_CB_PASSTHROUGH
);
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
)
2851 if (strlen(filename
) == 0)
2854 if (filename
[0] == '.' || filename
[0] == '/')
2858 if (save_tabs(t
, &a
))
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
);
2873 session_open(struct tab
*t
, char *filename
)
2878 if (strlen(filename
) == 0)
2881 if (filename
[0] == '.' || filename
[0] == '/')
2885 a
.i
= XT_SES_CLOSETABS
;
2886 if (open_tabs(t
, &a
))
2889 strlcpy(named_session
, filename
, sizeof named_session
);
2897 session_delete(struct tab
*t
, char *filename
)
2899 char file
[PATH_MAX
];
2903 if (strlen(filename
) == 0)
2906 if (filename
[0] == '.' || filename
[0] == '/')
2909 snprintf(file
, sizeof file
, "%s" PS
"%s", sessions_dir
, filename
);
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
))
2924 TAILQ_REMOVE(&sessions
, s
, entry
);
2925 g_free((gpointer
) s
->name
);
2934 session_cmd(struct tab
*t
, struct karg
*args
)
2936 char *filename
= args
->s
;
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");
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");
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");
2964 return (XT_CB_PASSTHROUGH
);
2968 script_cmd(struct tab
*t
, struct karg
*args
)
2977 if ((f
= fopen(args
->s
, "r")) == NULL
) {
2978 show_oops(t
, "Can't open script file: %s", args
->s
);
2982 if (fstat(fileno(f
), &sb
) == -1) {
2983 show_oops(t
, "Can't stat script file: %s", args
->s
);
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
);
2993 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
3002 return (XT_CB_PASSTHROUGH
);
3006 * Make a hardcopy of the page
3009 print_page(struct tab
*t
, struct karg
*args
)
3011 WebKitWebFrame
*frame
;
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
;
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
;
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
);
3054 g_object_unref(G_OBJECT(ps
));
3055 g_object_unref(G_OBJECT(op
));
3060 go_home(struct tab
*t
, struct karg
*args
)
3067 set_encoding(struct tab
*t
, struct karg
*args
)
3071 if (args
->s
&& strlen(g_strstrip(args
->s
)) == 0) {
3072 e
= webkit_web_view_get_custom_encoding(t
->wv
);
3074 e
= webkit_web_view_get_encoding(t
->wv
);
3075 show_oops(t
, "encoding: %s", e
? e
: "N/A");
3077 webkit_web_view_set_custom_encoding(t
->wv
, args
->s
);
3083 restart(struct tab
*t
, struct karg
*args
)
3087 a
.s
= XT_RESTART_TABS_FILE
;
3089 execvp(start_argv
[0], start_argv
);
3095 char *http_proxy_save
; /* not a setting, used to toggle */
3098 proxy_cmd(struct tab
*t
, struct karg
*args
)
3102 DNPRINTF(XT_D_CMD
, "%s: tab %d\n", __func__
, t
->tab_id
);
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
) {
3118 show_oops(t
, "http_proxy = %s", http_proxy
);
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");
3126 TAILQ_FOREACH(tt
, &tabs
, entry
)
3127 gtk_widget_show(t
->proxy_toggle
);
3129 if (setup_proxy(NULL
) == 0)
3130 button_set_file(t
->proxy_toggle
,
3132 show_oops(t
, "http proxy disabled");
3134 if (setup_proxy(http_proxy_save
) == 0 && http_proxy
) {
3135 button_set_file(t
->proxy_toggle
,
3137 show_oops(t
, "http_proxy = %s", http_proxy
);
3139 show_oops(t
, "invalid proxy: %s", http_proxy_save
);
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.
3151 eval_cb(const GMatchInfo
*info
, GString
*res
, gpointer data
)
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);
3173 if (g_match_info_fetch_pos(info
, 1, &start
, &end
) == FALSE
)
3176 g_string_assign(res
, match
);
3178 i
= atoi(&match
[start
+ 1]);
3179 if (args
->i
== XT_URL_PLUS
)
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);
3192 return (FALSE
); /* doesn't matter */
3196 urlmod_cmd(struct tab
*t
, struct karg
*args
)
3204 if ((uri
= gtk_entry_get_text(GTK_ENTRY(t
->uri_entry
))) == NULL
)
3206 if (strlen(uri
) == 0)
3209 reg
= g_regex_new(".*(/[0-9]+/)", 0, 0, NULL
);
3212 res
= g_regex_replace_eval(reg
, uri
, -1, 0, 0, eval_cb
, args
, NULL
);
3216 if (!strcmp(res
, uri
))
3219 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), res
);
3220 activate_uri_entry_cb(t
->uri_entry
, t
);
3227 return (XT_CB_PASSTHROUGH
);
3233 int (*func
)(struct tab
*, struct karg
*);
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 },
3256 { "searchnext", 0, search
, XT_SEARCH_NEXT
, 0 },
3257 { "searchprevious", 0, search
, XT_SEARCH_PREV
, 0 },
3260 { "focusaddress", 0, focus
, XT_FOCUS_URI
, 0 },
3261 { "focussearch", 0, focus
, XT_FOCUS_SEARCH
, 0 },
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
},
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 },
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 },
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 },
3365 { "cookiejar", 0, xtp_page_cl
, 0, 0 },
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 },
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 },
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 },
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
},
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 },
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 },
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 },
3449 } cmd_status
= {-1, 0};
3452 wv_release_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3455 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 1)
3462 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
3465 WebKitHitTestResult
*hit_test_result
;
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
));
3475 if (context
& WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE
)
3476 t
->mode
= XT_MODE_INSERT
;
3478 t
->mode
= XT_MODE_COMMAND
;
3480 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
3482 else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
3488 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
3490 a
.i
= XT_NAV_FORWARD
;
3500 tab_close_cb(GtkWidget
*btn
, struct tab
*t
)
3502 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
3508 parse_custom_uri(struct tab
*t
, const char *uri
)
3510 struct custom_uri
*u
;
3514 TAILQ_FOREACH(u
, &cul
, entry
) {
3515 if (strncmp(uri
, u
->uri
, strlen(u
->uri
)))
3520 sv
[1] = (char *)uri
;
3522 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
,
3524 show_oops(t
, "%s: could not spawn process", __func__
);
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
);
3538 show_oops(NULL
, "activate_uri_entry_cb invalid parameters");
3543 show_oops(t
, "activate_uri_entry_cb no uri");
3547 uri
+= strspn(uri
, "\t ");
3549 if (parse_custom_uri(t
, uri
))
3552 /* otherwise continue to load page normally */
3553 load_uri(t
, (gchar
*)uri
);
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
;
3565 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
3568 show_oops(NULL
, "activate_search_entry_cb invalid parameters");
3572 if (search_string
== NULL
|| strlen(search_string
) == 0) {
3573 show_oops(t
, "no search_string");
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
);
3586 load_uri(t
, newuri
);
3594 check_and_set_cookie(const gchar
*uri
, struct tab
*t
)
3596 struct wl_entry
*w
= NULL
;
3599 if (uri
== NULL
|| t
== NULL
)
3602 if ((w
= wl_find_uri(uri
, &c_wl
)) == NULL
)
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
);
3616 check_and_set_js(const gchar
*uri
, struct tab
*t
)
3618 struct wl_entry
*w
= NULL
;
3621 if (uri
== NULL
|| t
== NULL
)
3624 if ((w
= wl_find_uri(uri
, &js_wl
)) == NULL
)
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
);
3641 check_and_set_pl(const gchar
*uri
, struct tab
*t
)
3643 struct wl_entry
*w
= NULL
;
3646 if (uri
== NULL
|| t
== NULL
)
3649 if ((w
= wl_find_uri(uri
, &pl_wl
)) == NULL
)
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 */
3665 get_css_name(const char *col_str
)
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
);
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
;
3694 #if GTK_CHECK_VERSION(3, 0, 0)
3700 enum cert_trust trust
;
3704 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
3705 ssl_strict_certs
, ssl_ca_file
, uri
);
3710 if (uri
== NULL
|| g_str_has_prefix(uri
, "http://") ||
3711 !g_str_has_prefix(uri
, "https://"))
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
);
3721 for (i
= 0; i
< 3; ++i
)
3722 s
= strchr(&(s
[1]), '/');
3725 msg
= soup_message_new("HEAD", cut_uri
);
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
,
3737 if (!soup_message_get_https_status(msg
, &cert
, &flags
)) {
3738 DNPRINTF(XT_D_URL
, "%s: invalid response\n", uri
);
3741 if (!G_IS_TLS_CERTIFICATE(cert
)) {
3742 DNPRINTF(XT_D_URL
, "%s: no cert\n", uri
);
3747 col_str
= XT_COLOR_GREEN
;
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
] == '/') {
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
))
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
;
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
);
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
);
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
);
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
);
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
;
3819 xt_icon_from_name(struct tab
*t
, gchar
*name
)
3821 if (!enable_favicon_entry
)
3824 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
3825 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3827 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3828 GTK_ENTRY_ICON_PRIMARY
, "text-html");
3830 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->sbe
.uri
),
3831 GTK_ENTRY_ICON_PRIMARY
, NULL
);
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
);
3845 if (enable_favicon_entry
) {
3848 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
3849 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
3852 if (show_url
== 0) {
3853 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->sbe
.uri
),
3854 GTK_ENTRY_ICON_PRIMARY
, pb_scaled
);
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
),
3865 if (pb_scaled
!= pb
)
3866 g_object_unref(pb_scaled
);
3870 xt_icon_from_file(struct tab
*t
, char *uri
)
3875 if (g_str_has_prefix(uri
, "file://"))
3876 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3878 file
= g_strdup(uri
);
3883 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
3885 xt_icon_from_pixbuf(t
, pb
);
3888 xt_icon_from_name(t
, "text-html");
3894 is_valid_icon(char *file
)
3897 const char *mime_type
;
3901 gf
= g_file_new_for_path(file
);
3902 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
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;
3917 set_favicon_from_file(struct tab
*t
, char *uri
)
3922 if (t
== NULL
|| uri
== NULL
)
3925 if (g_str_has_prefix(uri
, "file://"))
3926 file
= g_filename_from_uri(uri
, NULL
, NULL
);
3928 file
= g_strdup(uri
);
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",
3941 /* no need to set icon to default here */
3945 xt_icon_from_file(t
, file
);
3951 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
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
) {
3970 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
3971 __func__
, t
->tab_id
, status
);
3974 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3976 t
->icon_download
= NULL
;
3979 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3982 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3985 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3987 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
3988 __func__
, t
->tab_id
);
3989 t
->icon_download
= NULL
;
3992 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
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
;
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
;
4022 xt_icon_from_name(t
, "text-html");
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
)
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
4044 if (wv
&& webkit_web_view_get_uri(wv
))
4045 pb
= webkit_web_view_try_get_favicon_pixbuf(wv
, 0, 0);
4047 if (wv
&& webkit_web_view_get_uri(wv
))
4048 pb
= webkit_web_view_get_icon_pixbuf(wv
);
4051 xt_icon_from_pixbuf(t
, pb
);
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
];
4060 if (t
->icon_request
) {
4061 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
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
);
4070 if (!stat(file
, &sb
)) {
4071 if (sb
.st_size
> 0) {
4072 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
4074 set_favicon_from_file(t
, file
);
4078 /* corrupt icon so trash it */
4079 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
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",
4092 t
->icon_download
= webkit_download_new(t
->icon_request
);
4093 if (t
->icon_download
== NULL
)
4096 /* we have to free icon_dest_uri later */
4097 if ((t
->icon_dest_uri
= g_filename_to_uri(file
, NULL
, NULL
)) == NULL
)
4099 webkit_download_set_destination_uri(t
->icon_download
,
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
;
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
);
4119 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
4121 const gchar
*uri
= NULL
;
4122 struct history
*h
, find
;
4124 gchar
*tmp_uri
= NULL
;
4125 #if !GTK_CHECK_VERSION(3, 0, 0)
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");
4134 show_oops(NULL
, "notify_load_status_cb invalid parameters");
4138 switch (webkit_web_view_get_load_status(wview
)) {
4139 case WEBKIT_LOAD_PROVISIONAL
:
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
));
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
);
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
);
4166 /* DOM is changing, unreference the previous focused element */
4167 #if WEBKIT_CHECK_VERSION(1, 5, 0)
4169 g_object_unref(t
->active
);
4171 if (t
->active_text
) {
4172 g_free(t
->active_text
);
4173 t
->active_text
= NULL
;
4177 /* take focus if we are visible */
4184 case WEBKIT_LOAD_COMMITTED
:
4189 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
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
);
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
);
4216 /* we know enough to autosave the session */
4217 if (session_autosave
) {
4222 show_ca_status(t
, uri
);
4223 run_script(t
, JS_HINTING
);
4224 if (enable_autoscroll
)
4225 run_script(t
, JS_AUTOSCROLL
);
4228 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
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
));
4244 case WEBKIT_LOAD_FINISHED
:
4246 if ((uri
= get_uri(t
)) == NULL
)
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 */
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
);
4266 insert_history_item(tmp_uri
,
4267 get_title(t
, FALSE
), time(NULL
));
4269 h
->time
= time(NULL
);
4272 if (statusbar_style
== XT_STATUSBAR_URL
)
4273 set_status(t
, "%s", (char *)tmp_uri
);
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
);
4284 #if WEBKIT_CHECK_VERSION(1, 1, 18)
4285 case WEBKIT_LOAD_FAILED
:
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
));
4300 #if GTK_CHECK_VERSION(2, 20, 0)
4301 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
4302 gtk_widget_hide(t
->spinner
);
4304 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
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
));
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
);
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
);
4332 get_domain(const gchar
*host
)
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] == '.') {
4348 return (g_strdup(&host
[x
+ 1]));
4352 p
= g_strrstr(host
, ".");
4354 return (g_strdup(""));
4360 return (g_strdup(p
+ 1));
4362 return (g_strdup(host
));
4366 js_autorun(struct tab
*t
)
4370 size_t got_default
= 0, got_host
= 0;
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)
4381 !(g_str_has_prefix(uri
, "http://") ||
4382 g_str_has_prefix(uri
, "https://")))
4385 su
= soup_uri_new(uri
);
4388 if (!SOUP_URI_VALID_FOR_HTTP(su
))
4391 DNPRINTF(XT_D_JS
, "%s: host: %s domain: %s\n", __func__
,
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");
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
)
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
);
4418 got_host
= sb
.st_size
;
4421 if (got_default
+ got_host
== 0)
4424 js
= g_malloc0(got_default
+ got_host
+ 1);
4428 if (fread(js
, got_default
, 1, deffile
) != 1) {
4429 show_oops(t
, "default file read error");
4432 jsat
= js
+ got_default
;
4436 if (fread(jsat
, got_host
, 1, hostfile
) != 1) {
4437 show_oops(t
, "host file read error");
4442 DNPRINTF(XT_D_JS
, "%s: about to run script\n", __func__
);
4459 webview_progress_changed_cb(WebKitWebView
*wv
, GParamSpec
*pspec
, struct tab
*t
)
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
)
4478 /* compare strings from the end */
4479 l1
= strlen(a
->host
);
4480 l2
= strlen(b
->host
);
4484 for (; *p1
== *p2
&& p1
> a
->host
&& p2
> b
->host
;
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) == '.')) {
4502 if (p1
== a
->host
&& p2
== b
->host
)
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
;
4526 if (enable_strict_transport
== FALSE
)
4529 DPRINTF("strict_transport_add(%s,%" PRIi64
",%d)\n", domain
,
4530 (uint64_t)timeout
, subdomains
);
4536 find
.host
= (char *)domain
;
4538 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4542 /* check if update is needed */
4543 if (d
->timeout
== timeout
&&
4544 (d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
) == subdomains
)
4547 d
->timeout
= timeout
;
4549 d
->flags
|= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4551 /* We're still initializing */
4552 if (strict_transport_file
== NULL
)
4555 if ((f
= fopen(strict_transport_file
, "w")) == NULL
) {
4557 "can't open strict-transport rules file");
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
)
4566 fprintf(f
, "%s\t%" PRIi64
"\t%d\n", d
->host
,
4567 (uint64_t)d
->timeout
,
4568 d
->flags
& XT_STS_FLAGS_INCLUDE_SUBDOMAINS
);
4572 d
= g_malloc(sizeof *d
);
4573 d
->host
= g_strdup(domain
);
4574 d
->timeout
= timeout
;
4576 d
->flags
= XT_STS_FLAGS_INCLUDE_SUBDOMAINS
;
4579 RB_INSERT(strict_transport_tree
, &st_tree
, d
);
4581 /* We're still initializing */
4582 if (strict_transport_file
== NULL
)
4585 if ((f
= fopen(strict_transport_file
, "a+")) == NULL
) {
4587 "can't open strict-transport rules file");
4591 fseek(f
, 0, SEEK_END
);
4592 fprintf(f
,"%s\t%" PRIi64
"\t%d\n", d
->host
, (uint64_t)timeout
,
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
)
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) {
4618 d
= RB_FIND(strict_transport_tree
, &st_tree
, &find
);
4626 strict_transport_init()
4628 char file
[PATH_MAX
];
4634 time_t timeout
, now
;
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
);
4650 if ((rule
= fparseln(f
, &len
, NULL
, delim
, 0)) == NULL
) {
4651 if (!feof(f
) || ferror(f
))
4657 /* get second entry */
4658 if ((ptr
= strpbrk(rule
, " \t")) == NULL
)
4662 timeout
= atoi(ptr
);
4664 /* get third entry */
4665 if ((ptr
= strpbrk(ptr
, " \t")) == NULL
)
4669 subdomains
= atoi(ptr
);
4672 strict_transport_add(rule
, timeout
, subdomains
);
4677 strict_transport_file
= g_strdup(file
);
4681 startpage_add("strict-transport rules file ('%s') is corrupt", file
);
4689 force_https_check(const char *uri
)
4691 struct wl_entry
*w
= NULL
;
4696 if ((w
= wl_find_uri(uri
, &force_https
)) == NULL
)
4703 strict_transport_security_cb(SoupMessage
*msg
, gpointer data
)
4709 int subdomains
= FALSE
;
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
)
4721 if ((ptr
= strcasestr(sts
, "max-age="))) {
4722 ptr
+= strlen("max-age=");
4723 timeout
= atoll(ptr
);
4725 return; /* malformed header - max-age must be included */
4727 if ((ptr
= strcasestr(sts
, "includeSubDomains")))
4730 strict_transport_add(uri
->host
, timeout
+ time(NULL
), subdomains
);
4734 session_rq_cb(SoupSession
*s
, SoupMessage
*msg
, SoupSocket
*socket
,
4744 if (s
== NULL
|| msg
== NULL
)
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
)
4756 /* Check if referer is set - and what the user requested for referers */
4757 ref
= soup_message_headers_get_one(msg
->request_headers
, "Referer");
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
,
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
,
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
);
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
,
4789 DNPRINTF(XT_D_NAV
, "session_rq_cb: removing "
4790 "referer (not same fqdn) (should be %s)\n",
4793 soup_uri_free(ref_uri
);
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
);
4806 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
4807 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
4808 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
4810 WebKitWebNavigationReason reason
;
4814 show_oops(NULL
, "webview_npd_cb invalid parameters");
4818 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
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
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
);
4841 if (parse_custom_uri(t
, uri
)) {
4842 webkit_web_policy_decision_ignore(pd
);
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
);
4853 if ((t
->mode
== XT_MODE_HINT
&& t
->new_tab
) || t
->ctrl_click
) {
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)
4870 webkit_web_policy_decision_use(pd
);
4871 return (TRUE
); /* we made the decision */
4878 webview_rrs_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, WebKitWebResource
*res
,
4879 WebKitNetworkRequest
*request
, WebKitNetworkResponse
*response
,
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
;
4889 msg
= webkit_network_request_get_message(request
);
4893 uri
= soup_message_get_uri(msg
);
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",
4903 soup_uri_set_scheme(uri
, SOUP_URI_SCHEME_HTTPS
);
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
4917 if ((di_find
.domain
= uri
->host
) == NULL
)
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
);
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
);
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
);
4943 soup_message_headers_replace(msg
->request_headers
,
4944 "User-Agent", ua
->value
);
4946 soup_message_headers_replace(msg
->request_headers
,
4947 "Accept", ha
->value
);
4955 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
4958 struct wl_entry
*w
= NULL
;
4960 WebKitWebView
*webview
= NULL
;
4963 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
4964 webkit_web_view_get_uri(wv
));
4967 /* open in current tab */
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
)
4974 if (t
->ctrl_click
) {
4975 x
= ctrl_click_focus
;
4978 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4980 } else if (enable_scripts
== 1) {
4981 if (t
->ctrl_click
) {
4982 x
= ctrl_click_focus
;
4985 tt
= create_new_tab(NULL
, NULL
, x
, -1);
4993 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
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
)
5006 } else if (enable_scripts
== 1)
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) {
5023 /* catch ctrl click */
5024 if (e
->type
== GDK_BUTTON_RELEASE
&&
5025 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
5030 return (XT_CB_PASSTHROUGH
);
5034 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
5036 struct mime_type
*m
;
5038 GError
*gerr
= NULL
;
5040 m
= find_mime_type(mime_type
);
5046 sv
[0] = m
->mt_action
;
5047 sv
[1] = (char *)webkit_network_request_get_uri(request
);
5050 /* ignore donothing from example config */
5051 if (m
->mt_action
&& !strcmp(m
->mt_action
, "donothing"))
5054 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5056 show_oops(t
, "%s: could not spawn process (%s)", __func__
,
5057 gerr
? gerr
->message
: "N/A");
5062 get_mime_type(const char *uri
)
5067 char *mime_type
= NULL
;
5071 show_oops(NULL
, "%s: invalid parameters", __func__
);
5075 if (g_str_has_prefix(uri
, "file://"))
5076 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5078 file
= g_strdup(uri
);
5083 gf
= g_file_new_for_path(file
);
5084 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
5086 if ((m
= g_file_info_get_content_type(fi
)) != NULL
)
5087 mime_type
= g_strdup(m
);
5096 run_download_mimehandler(char *mime_type
, char *file
)
5098 struct mime_type
*m
;
5101 m
= find_mime_type(mime_type
);
5105 sv
[0] = m
->mt_action
;
5108 if (!g_spawn_async(NULL
, sv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
5110 show_oops(NULL
, "%s: could not spawn process: %s %s", __func__
,
5118 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
5121 WebKitDownloadStatus status
;
5127 if (download
== NULL
)
5129 status
= webkit_download_get_status(download
);
5130 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
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
);
5144 mime
= get_mime_type(uri
);
5148 if (g_str_has_prefix(uri
, "file://"))
5149 file
= g_filename_from_uri(uri
, NULL
, NULL
);
5151 file
= g_strdup(uri
);
5156 run_download_mimehandler((char *)mime
, file
);
5163 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
5164 WebKitNetworkRequest
*request
, char *mime_type
,
5165 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
5168 show_oops(NULL
, "webview_mimetype_cb invalid parameters");
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
);
5181 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
5182 webkit_web_policy_decision_download(decision
);
5190 download_start(struct tab
*t
, struct download
*d
, int flag
)
5192 WebKitNetworkRequest
*req
;
5194 const gchar
*suggested_name
;
5195 gchar
*filename
= NULL
;
5202 if (d
== NULL
|| t
== NULL
) {
5203 show_oops(NULL
, "%s invalid parameters", __func__
);
5207 suggested_name
= webkit_download_get_suggested_filename(d
->download
);
5208 if (suggested_name
== NULL
)
5209 return (FALSE
); /* abort download */
5220 filename
= g_strdup_printf("%d%s", i
, suggested_name
);
5223 /* XXX using urls doesn't work properly in windows? */
5224 uri
= g_strdup_printf("%s\\%s", download_dir
, i
?
5225 filename
: suggested_name
);
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
)
5234 } while (!stat(uri
, &sb
));
5236 } while (!stat(path
, &sb
));
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__
);
5260 show_oops(t
, "Download Failed");
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
);
5296 download_ask_cb(struct tab
*t
, GdkEventKey
*e
, gpointer data
)
5298 struct download
*d
= data
;
5302 t
->mode_cb_data
= 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
;
5339 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
5342 const gchar
*suggested_name
;
5343 struct download
*download_entry
;
5346 if (wk_download
== NULL
|| t
== NULL
) {
5347 show_oops(NULL
, "%s invalid parameters", __func__
);
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",
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 */
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
);
5386 show_oops(NULL
, "webview_hover_cb");
5391 set_status(t
, "Link: %s", uri
);
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
);
5399 set_status(t
, "%s", (char *)get_title(t
, FALSE
));
5404 mark(struct tab
*t
, struct karg
*arg
)
5411 if ((index
= marktoindex(mark
)) == -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
);
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
;
5432 marks_clear(struct tab
*t
)
5436 for (i
= 0; i
< LENGTH(t
->mark
); i
++)
5437 t
->mark
[i
] = XT_INVALID_MARK
;
5443 char file
[PATH_MAX
];
5444 char *line
= NULL
, *p
;
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
));
5455 for (i
= 1; ; i
++) {
5456 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
5458 if (strlen(line
) == 0 || line
[0] == '#') {
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
);
5472 p
= strtok(NULL
, " \t");
5473 if (qmarks
[index
] != NULL
)
5474 g_free(qmarks
[index
]);
5475 qmarks
[index
] = g_strdup(p
);
5486 char file
[PATH_MAX
];
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
));
5496 for (i
= 0; i
< XT_NOQMARKS
; i
++)
5497 if (qmarks
[i
] != NULL
)
5498 fprintf(f
, "%c %s\n", indextoqmark(i
), qmarks
[i
]);
5506 qmark(struct tab
*t
, struct karg
*arg
)
5511 mark
= arg
->s
[strlen(arg
->s
)-1];
5512 index
= qmarktoindex(mark
);
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
));
5528 if (qmarks
[index
] != NULL
)
5529 load_uri(t
, qmarks
[index
]);
5531 show_oops(t
, "quickmark \"%c\" does not exist",
5537 if (qmarks
[index
] != NULL
)
5538 create_new_tab(qmarks
[index
], NULL
, 1, -1);
5540 show_oops(t
, "quickmark \"%c\" does not exist",
5551 go_up(struct tab
*t
, struct karg
*args
)
5559 if (args
->i
== XT_GO_UP_ROOT
)
5560 levels
= XT_GO_UP_ROOT
;
5561 else if ((levels
= atoi(args
->s
)) == 0)
5564 uri
= g_strdup(get_uri(t
));
5568 if ((tmp
= strstr(uri
, XT_PROTO_DELIM
)) == NULL
)
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;
5576 if (tmp
[lastidx
] == '/')
5577 tmp
[lastidx
] = '\0';
5581 p
= strrchr(tmp
, '/');
5582 if (p
== tmp
) { /* Are we at the root of a file://-path? */
5585 } else if (p
!= NULL
)
5598 gototab(struct tab
*t
, struct karg
*args
)
5601 struct karg arg
= {0, NULL
, -1};
5603 tab
= atoi(args
->s
);
5606 arg
.i
= XT_TAB_NEXT
;
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
);
5629 flip_colon(struct tab
*t
, struct karg
*arg
)
5631 struct karg narg
= {0, NULL
, -1};
5634 if (t
== NULL
|| arg
== NULL
)
5637 p
= strstr(arg
->s
, ":");
5649 /* buffer commands receive the regex that triggered them in arg.s */
5650 char bcmd
[XT_BUFCMD_SZ
];
5654 #define XT_PRE_NO (0)
5655 #define XT_PRE_YES (1)
5656 #define XT_PRE_MAYBE (2)
5658 int (*func
)(struct tab
*, struct karg
*);
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 },
5689 buffercmd_init(void)
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
);
5701 buffercmd_abort(struct tab
*t
)
5708 DNPRINTF(XT_D_BUFFERCMD
, "%s: clearing buffer\n", __func__
);
5710 for (i
= 0; i
< LENGTH(bcmd
); i
++)
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
);
5719 buffercmd_execute(struct tab
*t
, struct buffercmd
*cmd
)
5721 struct karg arg
= {0, NULL
, -1};
5724 arg
.s
= g_strdup(bcmd
);
5726 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_execute: buffer \"%s\" "
5727 "matches regex \"%s\", executing\n", bcmd
, cmd
->regex
);
5737 buffercmd_addkey(struct tab
*t
, guint keyval
)
5740 char s
[XT_BUFCMD_SZ
];
5742 if (gtk_widget_get_visible(GTK_WIDGET(t
->buffers
))) {
5744 return (XT_CB_PASSTHROUGH
);
5747 if (keyval
== GDK_Escape
) {
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') {
5773 /* buffer full, ignore input */
5774 if (i
>= LENGTH(bcmd
) -1) {
5775 DNPRINTF(XT_D_BUFFERCMD
, "buffercmd_addkey: buffer full\n");
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
]);
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
);
5796 if (buffercmds
[i
].precount
== XT_PRE_MAYBE
) {
5797 if (isdigit((unsigned char)bcmd
[0])) {
5798 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5802 if (sscanf(bcmd
, "%s", s
) == 0)
5805 } else if (buffercmds
[i
].precount
== XT_PRE_YES
) {
5806 if (sscanf(bcmd
, "%d%s", &c
, s
) == 0)
5809 if (sscanf(bcmd
, "%s", s
) == 0)
5812 if (c
== -1 && buffercmds
[i
].precount
)
5814 if (!strncmp(s
, buffercmds
[i
].cmd
, strlen(s
)))
5817 DNPRINTF(XT_D_BUFFERCMD
, "got[%d] %d <%s>: %d %s\n",
5818 i
, match
, buffercmds
[i
].cmd
, c
, s
);
5821 DNPRINTF(XT_D_BUFFERCMD
, "aborting: %s\n", bcmd
);
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)
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
);
5852 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
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
);
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
);
5879 if ((e
->state
& (CTRL
| MOD1
)) == 0)
5881 } else if ((e
->state
& k
->mask
) == k
->mask
) {
5886 if (!entry
&& ((e
->state
& (CTRL
| MOD1
)) == 0))
5887 return (buffercmd_addkey(t
, e
->keyval
));
5889 return (XT_CB_PASSTHROUGH
);
5892 if (k
->cmd
[0] == ':') {
5894 args
.s
= &k
->cmd
[1];
5895 return (command(t
, &args
));
5897 return (cmd_execute(t
, k
->cmd
));
5901 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
5905 /* don't use w directly; use t->whatever instead */
5908 show_oops(NULL
, "wv_keypress_cb");
5909 return (XT_CB_PASSTHROUGH
);
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
));
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
) {
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));
5948 return (handle_keypress(t
, e
, 1));
5952 return (XT_CB_PASSTHROUGH
);
5956 hint_continue(struct tab
*t
)
5958 const gchar
*c
= gtk_entry_get_text(GTK_ENTRY(t
->cmd
));
5960 const gchar
*errstr
= NULL
;
5964 if (!(c
[0] == '.' || c
[0] == ','))
5966 if (strlen(c
) == 1) {
5967 /* XXX should not happen */
5972 if (isdigit((unsigned char)c
[1])) {
5974 i
= strtonum(&c
[1], 1, 4096, &errstr
);
5976 show_oops(t
, "invalid numerical hint %s", &c
[1]);
5979 s
= g_strdup_printf("hints.updateHints(%d);", i
);
5983 /* alphanumeric input */
5984 s
= g_strdup_printf("hints.createHints('%s', '%c');",
5985 &c
[1], c
[0] == '.' ? 'f' : 'F');
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] == ',')
6003 if (strlen(c
) == 1) {
6004 webkit_web_view_unmark_text_matches(t
->wv
);
6009 t
->search_forward
= TRUE
;
6010 else if (c
[0] == '?')
6011 t
->search_forward
= FALSE
;
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)
6028 if (search_continue(t
) == FALSE
)
6032 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, t
->search_forward
,
6034 /* not found, mark red */
6035 #if GTK_CHECK_VERSION(3, 0, 0)
6036 gtk_widget_set_name(t
->cmd
, XT_CSS_RED
);
6038 gdk_color_parse(XT_COLOR_RED
, &color
);
6039 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
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 */
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
);
6052 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
,
6053 &t
->default_style
->base
[GTK_STATE_NORMAL
]);
6062 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6064 const gchar
*c
= gtk_entry_get_text(w
);
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
);
6075 if (!(e
->keyval
== GDK_Tab
|| e
->keyval
== GDK_ISO_Left_Tab
)) {
6076 if (hint_continue(t
) == FALSE
)
6081 if (search_continue(t
) == FALSE
)
6084 /* if search length is > 4 then no longer play timeout games */
6085 if (strlen(c
) > 4) {
6087 g_source_remove(t
->search_id
);
6094 /* reestablish a new timer if the user types fast */
6096 g_source_remove(t
->search_id
);
6097 t
->search_id
= g_timeout_add(250, (GSourceFunc
)search_cb
, (gpointer
)t
);
6100 return (XT_CB_PASSTHROUGH
);
6104 match_uri(const gchar
*uri
, const gchar
*key
) {
6107 gboolean match
= FALSE
;
6111 if (!strncmp(key
, uri
, len
))
6114 voffset
= strstr(uri
, "/") + 2;
6115 if (!strncmp(key
, voffset
, len
))
6117 else if (g_str_has_prefix(voffset
, "www.")) {
6118 voffset
= voffset
+ strlen("www.");
6119 if (!strncmp(key
, voffset
, len
))
6128 match_session(const gchar
*name
, const gchar
*key
) {
6131 sub
= strcasestr(name
, key
);
6137 cmd_getlist(int id
, char *key
)
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
;
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
;
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
),
6169 cmd_status
.list
[c
++] =
6170 get_setting_name(i
);
6176 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
6178 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
6179 if (cmds
[i
].level
< dep
)
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
;
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
[])
6207 char *tok
, *last
= NULL
;
6208 size_t len
= strlen(s
);
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
++)
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;
6228 char *tok
, *match
, *s
= g_strdup(str
);
6229 char *tokens
[3] = {'\0'};
6230 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
6233 DNPRINTF(XT_D_CMD
, "%s: complete %s\n", __func__
, str
);
6236 for (i
= 0; isdigit((unsigned char)s
[i
]); i
++)
6239 for (; isspace((unsigned char)s
[i
]); i
++)
6244 levels
= cmd_tokenize(s
, tokens
);
6246 for (i
= 0; i
< levels
- 1; i
++) {
6249 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6250 if (cmds
[j
].level
< dep
)
6252 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
,
6256 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
6263 if (matchcount
== 1) {
6264 strlcat(res
, tok
, sizeof res
);
6265 strlcat(res
, " ", sizeof res
);
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);
6289 parse_prefix_and_alias(const char *str
, int *prefix
)
6291 struct cmd_alias
*c
;
6292 char *s
= g_strdup(str
), *sc
;
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]))
6304 TAILQ_FOREACH(c
, &cal
, entry
) {
6305 if (strncmp(s
, c
->alias
, strlen(c
->alias
)))
6308 if (strlen(s
) == strlen(c
->alias
)) {
6310 return (g_strdup(c
->cmd
));
6313 if (!isspace((unsigned char)s
[strlen(c
->alias
)]))
6316 s
= g_strdup_printf("%s %s", c
->cmd
, &s
[strlen(c
->alias
) + 1]);
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
)) {
6339 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6340 if (cmds
[j
].level
< dep
)
6342 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1 :
6344 if (cmds
[j
].level
== dep
&&
6345 !strncmp(tok
, cmds
[j
].cmd
, len
)) {
6349 if (len
== strlen(cmds
[j
].cmd
)) {
6355 if (matchcount
== 1) {
6360 show_oops(t
, "Invalid command: %s", str
);
6366 show_oops(t
, "Empty command");
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
);
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");
6387 arg
.precount
= atoi(arg
.s
);
6388 if (arg
.precount
<= 0) {
6389 if (arg
.s
[0] == '0')
6390 show_oops(t
, "Zero count");
6392 show_oops(t
, "Trailing characters");
6397 DNPRINTF(XT_D_CMD
, "%s: prefix %d arg %s\n",
6398 __func__
, arg
.precount
, arg
.s
);
6414 save_runtime_setting(const char *name
, const char *val
)
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)
6428 snprintf(file
, sizeof file
, "%s" PS
"%s", work_dir
, runtime_settings
);
6429 if (stat(file
, &sb
) || (f
= fopen(file
, "r+")) == NULL
)
6431 lt
= g_strdup_printf("%s=%s", name
, val
);
6432 contents
= g_strdup("");
6434 line
= fparseln(f
, &linelen
, NULL
, delim
, 0);
6435 if (line
== NULL
|| linelen
== 0)
6438 start
= g_strdup_printf("%s=", name
);
6439 if (strstr(line
, start
) == NULL
)
6440 contents
= g_strdup_printf("%s%s\n", contents
, line
);
6443 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6452 contents
= g_strdup_printf("%s%s\n", contents
, lt
);
6455 if ((f
= freopen(file
, "w", f
)) == NULL
)
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
);
6479 return (XT_CB_PASSTHROUGH
);
6483 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
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
);
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
)
6507 at
= TAILQ_LAST(l
, command_list
);
6509 at
= TAILQ_PREV(at
, command_list
, entry
);
6511 at
= TAILQ_LAST(l
, command_list
);
6517 struct command_entry
*
6518 history_next(struct command_list
*l
, struct command_entry
*at
)
6521 at
= TAILQ_FIRST(l
);
6523 at
= TAILQ_NEXT(at
, entry
);
6525 at
= TAILQ_FIRST(l
);
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
);
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
);
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
) {
6560 cmd_complete(t
, (char *)&c
[1], 1);
6561 else if (c
[0] == '.' || c
[0] == ',')
6562 run_script(t
, "hints.focusNextHint();");
6564 case GDK_ISO_Left_Tab
:
6566 cmd_complete(t
, (char *)&c
[1], -1);
6567 else if (c
[0] == '.' || c
[0] == ',')
6568 run_script(t
, "hints.focusPreviousHint();");
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);
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);
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');
6631 if (c
!= NULL
&& (c
[0] == '/' || c
[0] == '?'))
6632 webkit_web_view_unmark_text_matches(t
->wv
);
6634 /* no need to cancel hints */
6638 rv
= XT_CB_PASSTHROUGH
;
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
));
6653 name
= gtk_action_get_name(a
);
6655 DNPRINTF(XT_D_CMD
, "wv_popup_activ_cb: tab %d action %s\n",
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
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
);
6673 gtk_clipboard_set_text(primary
, uri
, -1);
6678 wv_popup_cb(WebKitWebView
*wview
, GtkMenu
*menu
, struct tab
*t
)
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
);
6692 cmd_popup_cb(GtkEntry
*entry
, GtkMenu
*menu
, struct tab
*t
)
6694 /* popup menu enabled */
6699 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
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 */
6712 return (XT_CB_PASSTHROUGH
);
6719 return (XT_CB_PASSTHROUGH
);
6723 cmd_hide_cb(GtkWidget
*w
, struct tab
*t
)
6726 show_oops(NULL
, "%s: invalid parameters", __func__
);
6730 if (show_url
== 0 || t
->focus_wv
)
6733 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6737 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6740 const gchar
*c
= gtk_entry_get_text(entry
);
6743 show_oops(NULL
, "cmd_activate_cb invalid parameters");
6747 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6752 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?' ||
6753 c
[0] == '.' || c
[0] == ','))
6759 if (c
[0] == '/' || c
[0] == '?') {
6760 /* see if there is a timer pending */
6762 g_source_remove(t
->search_id
);
6767 if (t
->search_text
) {
6768 g_free(t
->search_text
);
6769 t
->search_text
= NULL
;
6772 t
->search_text
= g_strdup(s
);
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 */
6795 backward_cb(GtkWidget
*w
, struct tab
*t
)
6800 show_oops(NULL
, "backward_cb invalid parameters");
6804 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6811 forward_cb(GtkWidget
*w
, struct tab
*t
)
6816 show_oops(NULL
, "forward_cb invalid parameters");
6820 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6822 a
.i
= XT_NAV_FORWARD
;
6827 home_cb(GtkWidget
*w
, struct tab
*t
)
6830 show_oops(NULL
, "home_cb invalid parameters");
6834 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6840 stop_cb(GtkWidget
*w
, struct tab
*t
)
6842 WebKitWebFrame
*frame
;
6845 show_oops(NULL
, "stop_cb invalid parameters");
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");
6857 webkit_web_frame_stop_loading(frame
);
6858 abort_favicon_download(t
);
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
);
6868 warnx("webkit does not have \"enable-dns-prefetching\" property");
6869 g_object_set(G_OBJECT(t
->settings
), "default-encoding", encoding
,
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
);
6907 update_statusbar_position(GtkAdjustment
* adjustment
, gpointer data
)
6909 struct tab
*ti
, *t
= NULL
;
6910 gdouble view_size
, value
, max
;
6913 TAILQ_FOREACH(ti
, &tabs
, entry
)
6914 if (ti
->tab_id
== gtk_notebook_get_current_page(notebook
)) {
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
;
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");
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
);
6947 create_window(const gchar
*name
)
6951 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6952 if (window_maximize
)
6953 gtk_window_maximize(GTK_WINDOW(w
));
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");
6963 create_browser(struct tab
*t
)
6966 GtkAdjustment
*adjustment
;
6969 show_oops(NULL
, "create_browser invalid parameters");
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
);
6985 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6986 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6989 t
->settings
= webkit_web_view_get_settings(t
->wv
);
6990 t
->stylesheet
= g_strdup(stylesheet
);
6991 t
->load_images
= auto_load_images
;
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
);
7005 create_kiosk_toolbar(struct tab
*t
)
7007 GtkWidget
*toolbar
= NULL
;
7010 #if GTK_CHECK_VERSION(3, 0, 0)
7011 toolbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
7012 gtk_widget_set_name(toolbar
, "toolbar");
7014 toolbar
= gtk_hbox_new(FALSE
, 1);
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);
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
);
7064 create_toolbar(struct tab
*t
)
7066 GtkWidget
*toolbar
= NULL
;
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
7077 #if GTK_CHECK_VERSION(3, 0, 0)
7078 toolbar
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
7079 gtk_widget_set_name(toolbar
, "toolbar");
7081 toolbar
= gtk_hbox_new(FALSE
, 1);
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);
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);
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 */
7122 button_set_file(t
->proxy_toggle
, "torenabled.ico");
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
);
7139 gtk_box_pack_start(b
, t
->uri_entry
, TRUE
, TRUE
, 0);
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
);
7160 create_buffers(struct tab
*t
)
7162 GtkCellRenderer
*renderer
;
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
,
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
,
7183 gtk_tree_view_set_model
7184 (GTK_TREE_VIEW(view
), GTK_TREE_MODEL(buffers_store
));
7190 row_activated_cb(GtkTreeView
*view
, GtkTreePath
*path
,
7191 GtkTreeViewColumn
*col
, struct tab
*t
)
7196 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7198 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store
), &iter
,
7201 (GTK_TREE_MODEL(buffers_store
), &iter
, COL_ID
, &id
, -1);
7202 set_current_tab(id
- 1);
7208 /* after tab reordering/creation/removal */
7214 TAILQ_FOREACH(t
, &tabs
, entry
)
7215 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
7219 update_statusbar_tabs(struct tab
*t
)
7221 int tab_id
, max_tab_id
;
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
);
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 */
7241 recolor_compact_tabs(void)
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
);
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
);
7258 gtk_widget_set_name(t
->tab_elems
.label
, "");
7260 if (t
->tab_id
== curid
)
7261 gtk_widget_modify_fg(t
->tab_elems
.label
,
7262 GTK_STATE_NORMAL
, &color_active
);
7264 gtk_widget_modify_fg(t
->tab_elems
.label
,
7265 GTK_STATE_NORMAL
, &color_inactive
);
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
)
7284 struct undo
*u1
, *u2
;
7287 WebKitWebHistoryItem
*el
;
7289 if ((uri
= get_uri(t
)) == NULL
)
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
);
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
));
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
));
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
));
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
);
7330 g_list_free(u2
->history
);
7339 delete_tab(struct tab
*t
)
7343 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
7348 TAILQ_REMOVE(&tabs
, t
, entry
);
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
);
7360 g_source_remove(t
->search_id
);
7364 g_free(t
->session_key
);
7367 bzero(&a
, sizeof a
);
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
));
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
);
7398 if (TAILQ_EMPTY(&tabs
)) {
7399 if (browser_mode
== XT_BM_KIOSK
)
7400 create_new_tab(home
, NULL
, 1, -1);
7402 create_new_tab(NULL
, NULL
, 1, -1);
7405 /* recreate session */
7406 if (session_autosave
) {
7407 bzero(&a
, sizeof a
);
7409 save_tabs(NULL
, &a
);
7413 recolor_compact_tabs();
7417 update_statusbar_zoom(struct tab
*t
)
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
);
7430 setzoom_webkit(struct tab
*t
, int adjust
)
7432 #define XT_ZOOMPERCENT 0.04
7437 show_oops(NULL
, "setzoom_webkit invalid parameters");
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;
7449 show_oops(t
, "setzoom_webkit invalid zoom value");
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
);
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
) {
7468 set_current_tab(t
->tab_id
);
7479 append_tab(struct tab
*t
)
7484 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7485 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
7493 sbe
= gtk_label_new(NULL
);
7494 gtk_widget_set_can_focus(GTK_WIDGET(sbe
), FALSE
);
7495 modify_font(GTK_WIDGET(sbe
), statusbar_font
);
7500 statusbar_create(struct tab
*t
)
7502 GtkWidget
*box
; /* container for statusbar elems */
7506 #if !GTK_CHECK_VERSION(3, 0, 0)
7511 DPRINTF("%s: invalid parameters", __func__
);
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");
7520 t
->statusbar
= gtk_hbox_new(FALSE
, 0);
7521 box
= gtk_hbox_new(FALSE
, 0);
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
);
7541 #if GTK_CHECK_VERSION(3, 0, 0)
7542 statusbar_modify_attr(t
, XT_CSS_NORMAL
);
7544 statusbar_modify_attr(t
, XT_COLOR_WHITE
, XT_COLOR_BLACK
);
7548 gtk_box_pack_start(b
, t
->sbe
.uri
, TRUE
, TRUE
, 0);
7550 for (p
= statusbar_elems
; *p
!= '\0'; p
++) {
7553 #if GTK_CHECK_VERSION(3, 0, 0)
7554 sep
= gtk_separator_new(GTK_ORIENTATION_VERTICAL
);
7556 sep
= gtk_vseparator_new();
7557 gdk_color_parse(XT_COLOR_SB_SEPARATOR
, &color
);
7558 gtk_widget_modify_bg(sep
, GTK_STATE_NORMAL
, &color
);
7560 gtk_box_pack_start(b
, sep
, FALSE
, FALSE
, 0);
7563 if (t
->sbe
.position
== NULL
) {
7564 t
->sbe
.position
= create_sbe();
7565 gtk_box_pack_start(b
, t
->sbe
.position
, FALSE
,
7570 if (t
->sbe
.buffercmd
== NULL
) {
7571 t
->sbe
.buffercmd
= create_sbe();
7572 gtk_box_pack_start(b
, t
->sbe
.buffercmd
, FALSE
,
7577 if (t
->sbe
.zoom
== NULL
) {
7578 t
->sbe
.zoom
= create_sbe();
7579 gtk_box_pack_start(b
, t
->sbe
.zoom
, FALSE
, FALSE
,
7584 if (t
->sbe
.tabs
== NULL
) {
7585 t
->sbe
.tabs
= create_sbe();
7586 gtk_box_pack_start(b
, t
->sbe
.tabs
, FALSE
, FALSE
,
7591 if (t
->sbe
.proxy
== NULL
) {
7592 t
->sbe
.proxy
= create_sbe();
7593 gtk_box_pack_start(b
, t
->sbe
.proxy
, FALSE
,
7597 GTK_ENTRY(t
->sbe
.proxy
), "proxy");
7601 warnx("illegal flag \"%c\" in statusbar_elems\n", *p
);
7606 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar
, FALSE
, FALSE
, 0);
7612 create_new_tab(const char *title
, struct undo
*u
, int focus
, int position
)
7617 WebKitWebHistoryItem
*item
;
7620 #if !GTK_CHECK_VERSION(3, 0, 0)
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
) {
7629 "create_new_tab: new tab rejected\n");
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",
7642 t
= g_malloc0(sizeof *t
);
7644 if (title
== NULL
) {
7645 title
= "(untitled)";
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");
7654 t
->vbox
= gtk_vbox_new(FALSE
, 0);
7655 b
= gtk_hbox_new(FALSE
, 0);
7657 gtk_widget_set_can_focus(t
->vbox
, FALSE
);
7659 /* label + button for tab */
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)
7670 #if GTK_CHECK_VERSION(2, 20, 0)
7671 t
->spinner
= gtk_spinner_new();
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
7685 #if GTK_CHECK_VERSION(3, 0, 0)
7686 gtk_widget_set_size_request(t
->label
, 95, 0);
7688 gtk_widget_set_size_request(t
->label
, 100, 0);
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);
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
,
7703 t
->toolbar
= create_toolbar(t
);
7704 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
,
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
);
7724 gdk_color_parse(XT_COLOR_RED
, &color
);
7725 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
7727 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
7728 modify_font(GTK_WIDGET(t
->oops
), oops_font
);
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
);
7740 statusbar_create(t
);
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
);
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
);
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
);
7795 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.favicon
, FALSE
,
7797 gtk_box_pack_start(GTK_BOX(t
->tab_elems
.box
), t
->tab_elems
.label
, TRUE
,
7799 gtk_container_add(GTK_CONTAINER(t
->tab_elems
.eventbox
),
7802 gtk_box_pack_start(GTK_BOX(tab_bar_box
), t
->tab_elems
.eventbox
, TRUE
,
7804 gtk_widget_show_all(t
->tab_elems
.eventbox
);
7806 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7809 id
= position
>= 0 ? position
:
7810 gtk_notebook_get_current_page(notebook
) + 1;
7811 if (id
> gtk_notebook_get_n_pages(notebook
))
7814 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7815 gtk_notebook_insert_page(notebook
, t
->vbox
, t
->tab_content
,
7817 gtk_box_reorder_child(GTK_BOX(tab_bar_box
),
7818 t
->tab_elems
.eventbox
, id
);
7823 #if GTK_CHECK_VERSION(2, 20, 0)
7824 /* turn spinner off if we are a new tab without uri */
7826 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7827 gtk_widget_hide(t
->spinner
);
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
,
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
,
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
,
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,
7885 g_signal_connect(G_OBJECT(bb
), "clicked", G_CALLBACK(tab_close_cb
), t
);
7888 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7889 /* restore the tab's history */
7890 if (u
&& u
->history
) {
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
);
7900 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7903 g_list_free(u
->history
);
7905 webkit_web_back_forward_list_clear(t
->bfl
);
7907 /* check and show url and statusbar */
7908 url_set_visibility();
7909 statusbar_set_visibility();
7912 set_current_tab(t
->tab_id
);
7913 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7917 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7921 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7928 if (userstyle_global
)
7931 recolor_compact_tabs();
7932 setzoom_webkit(t
, XT_ZOOM_NORMAL
);
7937 notebook_switchpage_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7943 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7945 if (gtk_notebook_get_current_page(notebook
) == -1)
7948 TAILQ_FOREACH(t
, &tabs
, entry
) {
7949 if (t
->tab_id
== pn
) {
7950 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7953 uri
= get_title(t
, TRUE
);
7954 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7961 /* can't use focus_webview here */
7962 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
7964 update_statusbar_tabs(t
);
7971 notebook_pagereordered_cb(GtkNotebook
*nb
, GtkWidget
*nbp
, guint pn
,
7974 struct tab
*t
= NULL
, *tt
;
7978 TAILQ_FOREACH(tt
, &tabs
, entry
)
7979 if (tt
->tab_id
== pn
) {
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
,
7990 update_statusbar_tabs(t
);
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
);
8008 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
8010 GtkWidget
*menu
= NULL
, *menu_items
;
8011 GdkEventButton
*bevent
;
8012 struct tab
**stabs
= NULL
;
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
)
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 */
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
]);
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
,
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
);
8063 create_button(const char *name
, const char *stockid
, int size
)
8065 GtkWidget
*button
, *image
;
8067 #if !GTK_CHECK_VERSION(3, 0, 0)
8071 #if !GTK_CHECK_VERSION(3, 0, 0)
8072 newstyle
= g_strdup_printf(
8073 "style \"%s-style\"\n"
8075 " GtkWidget::focus-padding = 0\n"
8076 " GtkWidget::focus-line-width = 0\n"
8080 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
8081 gtk_rc_parse_string(newstyle
);
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
);
8099 button_set_file(GtkWidget
*button
, char *filename
)
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
);
8110 button_set_stockid(GtkWidget
*button
, char *stockid
)
8114 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
8115 gtk_button_set_image(GTK_BUTTON(button
), image
);
8124 char file
[PATH_MAX
];
8126 #if !GTK_CHECK_VERSION(3, 0, 0)
8130 #if GTK_CHECK_VERSION(3, 0, 0)
8131 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
8133 vbox
= gtk_vbox_new(FALSE
, 0);
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
);
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
);
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);
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);
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
,
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
);
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
)
8211 struct sockaddr_un sa
;
8213 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8214 warnx("%s: socket", __func__
);
8218 sa
.sun_family
= AF_UNIX
;
8219 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8220 work_dir
, XT_SOCKET_FILE
);
8223 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
8224 warnx("%s: connect", __func__
);
8228 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
8229 warnx("%s: send", __func__
);
8240 socket_watcher(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
8243 char str
[XT_MAX_URL_LENGTH
];
8244 socklen_t t
= sizeof(struct sockaddr_un
);
8245 struct sockaddr_un sa
;
8250 gint fd
= g_io_channel_unix_get_fd(source
);
8252 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
8257 if (getpeereid(s
, &uid
, &gid
) == -1) {
8261 if (uid
!= getuid() || gid
!= getgid()) {
8262 warnx("unauthorized user");
8268 warnx("not a valid user");
8272 n
= recv(s
, str
, sizeof(str
), 0);
8276 tt
= get_current_tab();
8277 cmd_execute(tt
, str
);
8285 struct sockaddr_un sa
;
8287 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8288 warn("is_running: socket");
8292 sa
.sun_family
= AF_UNIX
;
8293 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8294 work_dir
, XT_SOCKET_FILE
);
8297 /* connect to see if there is a listener */
8298 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
8299 rv
= 0; /* not running */
8301 rv
= 1; /* already running */
8312 struct sockaddr_un sa
;
8314 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
8315 warn("build_socket: socket");
8319 sa
.sun_family
= AF_UNIX
;
8320 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s" PS
"%s",
8321 work_dir
, XT_SOCKET_FILE
);
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");
8334 if (listen(s
, 1) == -1) {
8335 warn("build_socket: listen");
8353 if (stat(dir
, &sb
)) {
8354 #if defined __MINGW32__
8355 if (mkdir(dir
) == -1)
8357 if (mkdir(dir
, S_IRWXU
) == -1)
8359 err(1, "mkdir %s", dir
);
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
);
8378 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
8382 #if GTK_CHECK_VERSION(3, 0, 0)
8386 GtkCssProvider
*provider
;
8387 GdkDisplay
*display
;
8390 char path
[PATH_MAX
];
8391 #if defined __MINGW32__
8392 GtkCssProvider
*windows_hacks
;
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
,
8409 " border-width: 0px;\n"
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
));
8416 g_object_unref(G_OBJECT(file
));
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
)
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
;
8442 start_argv
= (char * const *)argv
;
8448 gtk_init(&argc
, &argv
);
8450 gnutls_global_init();
8452 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
8455 RB_INIT(&downloads
);
8462 TAILQ_INIT(&sessions
);
8465 TAILQ_INIT(&aliases
);
8476 TAILQ_INIT(&force_https
);
8479 #ifndef XT_RESOURCE_LIMITS_DISABLE
8483 GIOChannel
*channel
;
8485 /* fiddle with ulimits */
8486 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8489 /* just use them all */
8491 rlp
.rlim_cur
= OPEN_MAX
;
8493 rlp
.rlim_cur
= rlp
.rlim_max
;
8495 if (setrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
8497 if (getrlimit(RLIMIT_NOFILE
, &rlp
) == -1)
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
);
8507 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
8516 #ifdef XOMBRERO_BUILDSTR
8517 errx(0 , "Version: %s Build: %s",
8518 version
, XOMBRERO_BUILDSTR
);
8520 errx(0 , "Version: %s", version
);
8524 strlcpy(conf
, optarg
, sizeof(conf
));
8527 strlcpy(named_session
, optarg
, sizeof(named_session
));
8548 pwd
= getpwuid(getuid());
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 */
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
);
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
,
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);
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
);
8622 /* icon cache dir */
8623 snprintf(cache_dir
, sizeof cache_dir
, "%s" PS
"%s", work_dir
, XT_CACHE_DIR
);
8627 snprintf(certs_dir
, sizeof certs_dir
, "%s" PS
"%s", work_dir
, XT_CERT_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
);
8636 snprintf(sessions_dir
, sizeof sessions_dir
, "%s" PS
"%s",
8637 work_dir
, XT_SESSIONS_DIR
);
8638 xxx_dir(sessions_dir
);
8641 snprintf(js_dir
, sizeof js_dir
, "%s" PS
"%s", work_dir
, XT_JS_DIR
);
8645 snprintf(temp_dir
, sizeof temp_dir
, "%s" PS
"%s", work_dir
, XT_TEMP_DIR
);
8648 /* runtime settings that can override config file */
8649 if (runtime_settings
[0] != '\0')
8650 config_parse(runtime_settings
, 1);
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
)
8670 if ((f
= fopen(file
, "r+")) == NULL
)
8671 err(1, "startofday");
8672 if (fgets(sodversion
, sizeof sodversion
, f
) == NULL
)
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
)
8681 /* upgrade, say something smart */
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");
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");
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");
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");
8728 history_read(&chl
, command_file
, &cmd_history_count
);
8734 /* guess_search regex */
8735 if (url_regex
== NULL
)
8736 url_regex
= g_strdup(XT_URL_REGEX
);
8738 if (regcomp(&url_re
, url_regex
, REG_EXTENDED
| REG_NOSUB
))
8739 startpage_add("invalid url regex %s", url_regex
);
8742 env_proxy
= getenv("http_proxy");
8744 setup_proxy(env_proxy
);
8746 env_proxy
= getenv("HTTP_PROXY");
8748 setup_proxy(env_proxy
);
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
);
8759 #ifndef XT_SOCKET_DISABLE
8761 send_cmd_to_socket(argv
[0]);
8765 opte
= opte
; /* shut mingw up */
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
,
8771 g_object_set(session
, SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
8774 g_signal_connect(session
, "request-queued", G_CALLBACK(session_rq_cb
),
8777 #ifndef XT_SOCKET_DISABLE
8778 /* see if there is already a xombrero running */
8779 if (single_instance
&& is_running()) {
8781 warnx("already running");
8786 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8787 send_cmd_to_socket(cmd
);
8797 optn
= optn
; /* shut mingw up */
8804 webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER
);
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
);
8815 buffers_store
= gtk_list_store_new
8816 (NUM_COLS
, G_TYPE_UINT
, GDK_TYPE_PIXBUF
, G_TYPE_STRING
);
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();
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);
8845 create_new_tab(argv
[0], NULL
, focus
, -1);
8852 if (TAILQ_EMPTY(&tabs
))
8853 create_new_tab(home
, NULL
, 1, -1);
8854 #ifndef XT_SOCKET_DISABLE
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
);
8862 #if GTK_CHECK_VERSION(3, 0, 0)
8868 gnutls_global_deinit();