6 #include <webkit2/webkit2.h>
12 struct Configuration cfg;
17 int cooperative_pipe_fp = 0;
20 size_t num_closed = 0;
23 isearch_counted_matches(GtkWidget *widget, guint matches, gpointer data)
25 struct Client *c = (struct Client *)data;
26 char *text = malloc(12);
27 sprintf(text, "%d matches", matches);
28 gtk_label_set_text(GTK_LABEL(c->isearch_matches), text);
34 quit_if_nothing_active(void)
41 downloadmanager_show();
47 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
50 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
53 client_new(uri, NULL);
62 fprintf(stderr, "chorizo: fatal: alloc failed\n");
67 client_destroy(GtkWidget *widget, gpointer data)
69 struct Client *c = (struct Client *)data;
71 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
72 changed_load_progress, c);
74 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
76 fprintf(stderr, "chorizo: warning: tab index was -1\n");
78 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
80 if (!cfg.private && WEBKIT_IS_WEB_VIEW(c->web_view)) {
82 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
84 // TODO: Shift everything left if over certain amount
87 if (num_closed > cfg_max_tabs_closed) {
88 memmove(closed_tabs, closed_tabs,
89 cfg_max_tabs_closed - 1);
90 num_closed = cfg_max_tabs_closed;
92 closed_tabs = realloc(
94 num_closed * sizeof(closed_tabs[0]));
95 if (!closed_tabs) allocfail();
97 closed_tabs[num_closed - 1] = strdup(uri);
104 quit_if_nothing_active();
108 set_uri(const char *uri, struct Client *c)
110 if (!gtk_widget_is_focus(c->location))
111 gtk_entry_set_text(GTK_ENTRY(c->location),
112 (uri != NULL) ? uri : "");
116 client_new(const gchar *uri, WebKitWebView *related_wv)
120 GtkWidget *evbox, *tabbox;
121 if (uri != NULL && !cfg.noncooperative_instances &&
122 !cfg.cooperative_alone) {
123 f = ensure_uri_scheme(uri);
124 write(cooperative_pipe_fp, f, strlen(f));
125 write(cooperative_pipe_fp, "\n", 1);
129 c = calloc(1, sizeof(struct Client));
132 if (related_wv == NULL) {
133 WebKitUserContentManager *ucm =
134 webkit_user_content_manager_new();
135 WebKitUserScript *wkscript;
136 WebKitUserStyleSheet *wkstyle;
137 gchar *path = NULL, *source, *base;
138 const gchar *entry = NULL;
140 base = g_build_filename(g_get_user_data_dir(), "chorizo",
141 "user-scripts", NULL);
142 dir = g_dir_open(base, 0, NULL);
144 while ((entry = g_dir_read_name(dir)) != NULL) {
145 path = g_build_filename(base, entry, NULL);
146 if (g_str_has_suffix(path, ".js")) {
147 g_file_get_contents(path, &source, NULL,
150 wkscript = webkit_user_script_new(
152 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
153 WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
156 webkit_user_content_manager_add_script(
158 webkit_user_script_unref(wkscript);
161 if (source) g_free(source);
165 base = g_build_filename(g_get_user_data_dir(), "chorizo",
166 "user-styles", NULL);
167 dir = g_dir_open(base, 0, NULL);
169 while ((entry = g_dir_read_name(dir)) != NULL) {
170 path = g_build_filename(base, entry, NULL);
171 if (g_str_has_suffix(path, ".css")) {
172 g_file_get_contents(path, &source, NULL,
175 wkstyle = webkit_user_style_sheet_new(
177 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
178 WEBKIT_USER_STYLE_LEVEL_USER,
180 webkit_user_content_manager_add_style_sheet(
183 webkit_user_style_sheet_unref(wkstyle);
193 webkit_web_view_new_with_user_content_manager(ucm);
195 c->settings = webkit_web_view_get_settings(
196 WEBKIT_WEB_VIEW(c->web_view));
199 webkit_settings_set_enable_write_console_messages_to_stdout(
203 webkit_settings_set_enable_developer_extras(c->settings, TRUE);
205 c->web_view = webkit_web_view_new_with_related_view(related_wv);
208 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
209 G_CALLBACK(changed_favicon), c);
210 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
211 G_CALLBACK(changed_title), c);
212 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
213 G_CALLBACK(changed_uri), c);
214 g_signal_connect(G_OBJECT(c->web_view),
215 "notify::estimated-load-progress",
216 G_CALLBACK(changed_load_progress), c);
217 g_signal_connect(G_OBJECT(c->web_view), "create",
218 G_CALLBACK(client_new_request), c);
219 g_signal_connect(G_OBJECT(c->web_view), "close",
220 G_CALLBACK(client_destroy), c);
221 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
222 G_CALLBACK(decide_policy), NULL);
223 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
224 G_CALLBACK(key_web_view), c);
225 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
226 G_CALLBACK(key_web_view), c);
227 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
228 G_CALLBACK(hover_web_view), c);
229 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
230 G_CALLBACK(crashed_web_view), c);
232 GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
234 c->location = gtk_entry_new();
235 gtk_entry_set_placeholder_text(GTK_ENTRY(c->location), "URL");
236 char *location_symbol;
238 location_symbol = "security-high-symbolic";
239 gtk_entry_set_icon_tooltip_markup(
240 GTK_ENTRY(c->location), GTK_ENTRY_ICON_PRIMARY,
241 "You are in private mode. No history, caches, or "
242 "cookies will be saved beyond this session.");
244 location_symbol = "text-x-generic-symbolic";
247 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
248 GTK_ENTRY_ICON_PRIMARY,
250 gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
252 c->wsearch = gtk_entry_new();
253 gtk_entry_set_placeholder_text(GTK_ENTRY(c->wsearch), "Search the web");
254 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->wsearch),
255 GTK_ENTRY_ICON_PRIMARY,
256 "system-search-symbolic");
257 gtk_box_pack_start(GTK_BOX(locbox), c->wsearch, FALSE, TRUE, 0);
259 g_signal_connect(G_OBJECT(c->location), "key-press-event",
260 G_CALLBACK(key_location), c);
261 g_signal_connect(G_OBJECT(c->location), "icon-release",
262 G_CALLBACK(icon_location), c);
263 g_signal_connect(G_OBJECT(c->wsearch), "key-press-event",
264 G_CALLBACK(key_wsearch), c);
266 * XXX This is a workaround. Setting this to NULL (which is done in
267 * grab_feeds_finished() if no feed has been detected) adds a little
268 * padding left of the text. Not sure why. The point of this call
269 * right here is to have that padding right from the start. This
270 * avoids a graphical artifact.
273 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
274 GTK_ENTRY_ICON_SECONDARY, NULL);
276 c->isearch_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
277 c->isearch = gtk_entry_new();
278 gtk_entry_set_placeholder_text(GTK_ENTRY(c->isearch), "Search page");
279 gtk_box_pack_start(GTK_BOX(c->isearch_box), c->isearch, FALSE, TRUE, 0);
280 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->isearch),
281 GTK_ENTRY_ICON_PRIMARY,
282 "system-search-symbolic");
283 g_signal_connect(G_OBJECT(c->isearch), "key-press-event",
284 G_CALLBACK(key_isearch), c);
286 c->isearch_matches = gtk_label_new("0 matches");
287 gtk_box_pack_start(GTK_BOX(c->isearch_box), c->isearch_matches, FALSE,
290 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
291 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
292 gtk_box_pack_start(GTK_BOX(c->vbox), c->isearch_box, FALSE, TRUE, 0);
293 gtk_box_pack_start(GTK_BOX(c->vbox), locbox, FALSE, FALSE, 0);
295 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
297 c->tabicon = gtk_image_new_from_icon_name("text-html",
298 GTK_ICON_SIZE_SMALL_TOOLBAR);
300 c->tablabel = gtk_label_new("chorizo");
301 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
302 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg_tab_width);
303 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
306 * XXX I don't own a HiDPI screen, so I don't know if scale_factor
307 * does the right thing.
309 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
310 5 * gtk_widget_get_scale_factor(mw.win));
311 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
312 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
314 evbox = gtk_event_box_new();
315 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
316 g_signal_connect(G_OBJECT(evbox), "button-release-event",
317 G_CALLBACK(key_tablabel), c);
319 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
320 g_signal_connect(G_OBJECT(evbox), "scroll-event",
321 G_CALLBACK(key_tablabel), c);
323 // For easy access, store a reference to our label.
324 g_object_set_data(G_OBJECT(evbox), "chorizo-tab-label", c->tablabel);
327 * This only shows the event box and the label inside, nothing else.
328 * Needed because the evbox/label is "internal" to the notebook and
329 * not part of the normal "widget tree" (IIUC).
331 gtk_widget_show_all(evbox);
333 int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1;
334 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
336 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox,
339 show_web_view(NULL, c);
342 f = ensure_uri_scheme(uri);
343 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
350 if (uri == NULL) gtk_widget_grab_focus(c->location);
352 return WEBKIT_WEB_VIEW(c->web_view);
356 client_new_request(WebKitWebView *web_view,
357 WebKitNavigationAction *navigation_action, gpointer data)
360 WebKitWebView *new_web_view = client_new(NULL, web_view);
361 gtk_widget_grab_focus(GTK_WIDGET(new_web_view));
366 mkdirp(const char *dir, mode_t mode)
371 snprintf(tmp, sizeof(tmp), "%s", dir);
373 if (tmp[len - 1] == '/') tmp[len - 1] = 0;
374 for (p = tmp + 1; *p; p++)
384 cooperation_setup(void)
389 gchar *priv = (cfg.private) ? "-private" : "";
390 const gchar *fifo_suffix_env = g_getenv("CHORIZO_FIFO_SUFFIX");
391 const gchar *fifo_suffix = (fifo_suffix_env) ? fifo_suffix_env : "";
392 fifofilename = g_strdup_printf("%s%s%s%s", "chorizo", priv, ".fifo",
394 fifopath = g_build_filename(g_get_user_runtime_dir(), "chorizo",
396 mkdirp(dirname(fifopath), 0600);
397 g_free(fifofilename);
399 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS)) mkfifo(fifopath, 0600);
401 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
402 if (!cooperative_pipe_fp) {
403 fprintf(stderr, "chorizo: error: can't open FIFO\n");
405 if (write(cooperative_pipe_fp, "", 0) == -1) {
407 * Could not do an empty write to the FIFO which
408 * means there's no one listening.
410 close(cooperative_pipe_fp);
411 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
412 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg,
415 cfg.cooperative_alone = FALSE;
421 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
423 struct Client *c = (struct Client *)data;
426 "a = document.querySelectorAll('"
428 "link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
430 "link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
436 " for (i = 0; i < a.length; i++) {"
437 " url = encodeURIComponent(a[i].href);"
438 " if ('title' in a[i] && a[i].title != '')"
439 " title = encodeURIComponent(a[i].title);"
442 " out += '<li><a href=\"' + url + '\">' + title + "
447 p = webkit_web_view_get_estimated_load_progress(
448 WEBKIT_WEB_VIEW(c->web_view));
453 * The page has loaded fully. We now run the short JavaScript
454 * snippet above that operates on the DOM. It tries to grab
455 * all occurences of <link rel="alternate" ...>, i.e.
456 * RSS/Atom feed references.
458 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
460 grab_feeds_finished, c);
462 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
466 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
468 struct Client *c = (struct Client *)data;
470 int w, h, w_should, h_should;
471 GdkPixbuf *pb, *pb_scaled;
472 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
474 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
475 GTK_ICON_SIZE_SMALL_TOOLBAR);
477 w = cairo_image_surface_get_width(f);
478 h = cairo_image_surface_get_height(f);
479 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
481 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
482 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
483 pb_scaled = gdk_pixbuf_scale_simple(
484 pb, w_should, h_should, GDK_INTERP_BILINEAR);
485 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon),
488 g_object_unref(pb_scaled);
495 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
498 struct Client *c = (struct Client *)data;
499 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
500 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
502 if (t == NULL) t = (u == NULL) ? "chorizo" : u;
504 gchar *name = malloc(strlen(t) + 4);
505 if (!name) allocfail();
507 webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
508 gchar *muted = (mute) ? "[M] " : "";
509 sprintf(name, "%s%s", muted, t);
510 gtk_label_set_text(GTK_LABEL(c->tablabel), name);
513 gtk_widget_set_tooltip_text(c->tablabel, t);
515 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
519 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
522 struct Client *c = (struct Client *)data;
524 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
526 if (t != NULL && strlen(t) > 0) {
529 // No g_get_user_state_dir unfortunately
530 gchar *state_env = getenv("XDG_STATE_DIR");
532 gchar *state_dir = (state_env)
534 : g_build_filename(g_get_home_dir(),
539 gchar *history_file =
540 g_build_filename(state_dir, "history", NULL);
542 mkdirp(state_dir, 0700);
543 fp = fopen(history_file, "a");
545 fprintf(fp, "%s\n", t);
548 perror("chorizo: error: could not open history"
552 g_free(history_file);
558 crashed_web_view(WebKitWebView *web_view, gpointer data)
560 GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
561 GtkWidget *dialog = gtk_message_dialog_new(
562 GTK_WINDOW(mw.win), flags, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
563 "ERROR: Web process %s crashed.\n%s",
564 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)),
566 gtk_dialog_run(GTK_DIALOG(dialog));
567 gtk_widget_destroy(dialog);
573 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
574 WebKitPolicyDecisionType type, gpointer data)
576 WebKitResponsePolicyDecision *r;
578 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
579 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
580 if (!webkit_response_policy_decision_is_mime_type_supported(r))
581 webkit_policy_decision_download(decision);
583 webkit_policy_decision_use(decision);
586 // Use whatever default there is.
593 ensure_uri_scheme(const gchar *t)
596 f = g_ascii_strdown(t, -1);
597 if (!g_str_has_prefix(f, "http:") && !g_str_has_prefix(f, "https:") &&
598 !g_str_has_prefix(f, "file:") && !g_str_has_prefix(f, "about:") &&
599 !g_str_has_prefix(f, "data:") && !g_str_has_prefix(f, "webkit:")) {
601 fabs = realpath(t, NULL);
603 f = g_strdup_printf("file://%s", fabs);
606 f = g_strdup_printf("http://%s", t);
614 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
616 struct Client *c = (struct Client *)data;
617 WebKitJavascriptResult *js_result;
619 JSCException *exception;
622 g_free(c->feed_html);
626 * This was taken almost verbatim from the example in WebKit's
629 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
632 js_result = webkit_web_view_run_javascript_finish(
633 WEBKIT_WEB_VIEW(object), result, &err);
636 "chorizo: error: error running javascript: %s\n",
641 value = webkit_javascript_result_get_js_value(js_result);
642 if (jsc_value_is_string(value)) {
643 str_value = jsc_value_to_string(value);
645 jsc_context_get_exception(jsc_value_get_context(value));
646 if (exception != NULL) {
648 "chorizo: warning: error running javascript:"
650 jsc_exception_get_message(exception));
652 c->feed_html = str_value;
655 gtk_entry_set_icon_from_icon_name(
656 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY,
657 "application-rss+xml-symbolic");
658 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
659 GTK_ENTRY_ICON_SECONDARY, TRUE);
661 gtk_entry_set_icon_from_icon_name(
662 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY, NULL);
665 webkit_javascript_result_unref(js_result);
669 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht,
670 guint modifiers, gpointer data)
672 struct Client *c = (struct Client *)data;
674 g_free(c->hover_uri);
676 if (webkit_hit_test_result_context_is_link(ht)) {
677 to_show = webkit_hit_test_result_get_link_uri(ht);
678 c->hover_uri = g_strdup(to_show);
680 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
684 if (!gtk_widget_is_focus(c->location)) set_uri(to_show, c);
688 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
691 struct Client *c = (struct Client *)data;
693 gchar *data_template = "data:text/html,"
697 " <meta charset=\"UTF-8\">"
698 " <title>Feeds</title>"
701 " <p>Feeds found on this page:</p>"
707 if (c->feed_html != NULL) {
709 * What we're actually trying to do is show a simple HTML
710 * page that lists all the feeds on the current page. The
711 * function webkit_web_view_load_html() looks like the proper
712 * way to do that. Sad thing is, it doesn't create a history
713 * entry, but instead simply replaces the content of the
714 * current page. This is not what we want.
716 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
717 * mechanism to show my custom HTML snippet* and*create a
720 * [0]: https://tools.ietf.org/html/rfc2397 [1]:
721 * https://en.wikipedia.org/wiki/Data_URI_scheme
724 d = g_strdup_printf(data_template, c->feed_html);
725 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
731 init_default_web_context(void)
734 WebKitWebContext *wc;
735 WebKitCookieManager *cm;
736 wc = (cfg.private) ? webkit_web_context_new_ephemeral() :
737 webkit_web_context_get_default();
739 p = g_build_filename(g_get_user_config_dir(), "chorizo", "adblock",
741 webkit_web_context_set_sandbox_enabled(wc, TRUE);
742 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
745 webkit_web_context_set_process_model(
746 wc, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
748 p = g_build_filename(g_get_user_data_dir(), "chorizo", "web-extensions",
750 webkit_web_context_set_web_extensions_directory(wc, p);
753 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
754 g_signal_connect(G_OBJECT(wc), "download-started",
755 G_CALLBACK(download_start),
756 (xdg_down) ? xdg_down : "/var/tmp");
758 trust_user_certs(wc);
760 cm = webkit_web_context_get_cookie_manager(wc);
761 webkit_cookie_manager_set_accept_policy(cm, cfg_cookie_policy);
764 webkit_web_context_set_favicon_database_directory(wc, NULL);
766 gchar *fname = g_build_filename("/", g_get_user_data_dir(),
767 "chorizo", "cookies.db", NULL);
768 mkdirp(dirname(fname), 0700);
769 WebKitCookiePersistentStorage type =
770 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
771 webkit_cookie_manager_set_persistent_storage(cm, fname, type);
774 webkit_web_context_set_spell_checking_enabled(wc, TRUE);
778 isearch(gpointer data, gint direction)
780 struct Client *c = (struct Client *)data;
781 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
782 WebKitFindController *fc =
783 webkit_web_view_get_find_controller(web_view);
784 const gchar *isearch_text = gtk_entry_get_text(GTK_ENTRY(c->isearch));
785 if (isearch_text == NULL) return;
789 g_signal_connect(G_OBJECT(fc), "counted-matches",
790 G_CALLBACK(isearch_counted_matches), c);
791 webkit_find_controller_count_matches(
792 fc, isearch_text, cfg_isearch_options, G_MAXUINT);
793 webkit_find_controller_search(fc, isearch_text,
794 cfg_isearch_options, G_MAXUINT);
797 webkit_find_controller_search_next(fc);
800 webkit_find_controller_search_previous(fc);
803 webkit_find_controller_search_finish(fc);
809 isearch_init(struct Client *c, int direction)
811 if (webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view))) {
812 gtk_widget_show(c->isearch_box);
813 gtk_widget_grab_focus(c->isearch);
820 if (num_closed == 0) return;
821 client_new(closed_tabs[num_closed - 1], NULL);
823 closed_tabs = realloc(closed_tabs, num_closed * sizeof(closed_tabs[0]));
824 if (!closed_tabs) allocfail();
828 mainwindow_setup(void)
830 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
831 gtk_window_set_default_size(GTK_WINDOW(mw.win), 1200, 900);
832 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
834 gchar *priv = (cfg.private) ? "-private" : "";
835 gchar *title = malloc(strlen(priv) + 7);
836 if (!title) allocfail();
837 sprintf(title, "%s%s", "chorizo", priv);
838 gtk_window_set_title(GTK_WINDOW(mw.win), title);
841 mw.notebook = gtk_notebook_new();
842 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
843 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), GTK_POS_LEFT);
844 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
845 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
846 G_CALLBACK(notebook_switch_page), NULL);
848 GtkCssProvider *css = gtk_css_provider_new();
849 const char *css_data = "notebook header.left * { \
854 gtk_css_provider_load_from_data(css, css_data, strlen(css_data), NULL);
855 gtk_style_context_add_provider_for_screen(
856 gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
857 GTK_STYLE_PROVIDER_PRIORITY_USER);
859 gtk_widget_show_all(mw.win);
863 mainwindow_title(gint idx)
865 GtkWidget *child, *widg, *tablabel;
867 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
868 if (child == NULL) return;
870 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
871 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg),
872 "chorizo-tab-label");
873 text = gtk_label_get_text(GTK_LABEL(tablabel));
874 gtk_window_set_title(GTK_WINDOW(mw.win), text);
878 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
880 mainwindow_title(idx);
884 show_web_view(WebKitWebView *web_view, gpointer data)
886 struct Client *c = (struct Client *)data;
888 gint idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
890 gtk_widget_show_all(GTK_WIDGET(
891 gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx)));
892 gtk_widget_hide(c->isearch_box);
894 if (idx != -1 && switch_tab)
895 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
896 gtk_widget_grab_focus(c->web_view);
900 trust_user_certs(WebKitWebContext *wc)
902 GTlsCertificate *cert;
903 gchar *basedir, *absfile;
906 basedir = g_build_filename(g_get_user_data_dir(), "chorizo", "certs",
908 dir = g_dir_open(basedir, 0, NULL);
911 file = g_dir_read_name(dir);
912 while (file != NULL) {
913 absfile = g_build_filename(g_get_user_data_dir(),
914 "chorizo", "certs", file,
916 cert = g_tls_certificate_new_from_file(absfile, NULL);
920 "chorizo: warning: could not load"
921 "trusted cert: %s\n",
925 webkit_web_context_allow_tls_certificate_for_host(
928 file = g_dir_read_name(dir);
937 printf("%s %s\n", "chorizo", VERSION);
941 main(int argc, char **argv)
946 cfg.noncooperative_instances = FALSE;
947 cfg.cooperative_alone = TRUE;
948 closed_tabs = malloc(0);
949 if (!closed_tabs) allocfail();
951 while ((opt = getopt(argc, argv, "cpvV")) != -1) {
954 cfg.noncooperative_instances = TRUE;
967 "usage: chorizo [OPTION]... [URI]...\n");
972 if (cfg.verbose) version();
974 gtk_init(&argc, &argv);
976 // Keep clipboard contents after program closes
977 gtk_clipboard_store(gtk_clipboard_get_for_display(
978 gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
980 if (!cfg.noncooperative_instances) cooperation_setup();
982 if (cfg.noncooperative_instances || cfg.cooperative_alone)
983 init_default_web_context();
985 downloadmanager_setup();
988 if (optind >= argc) {
989 client_new(cfg_home_uri, NULL);
991 for (i = optind; i < argc; i++) client_new(argv[i], NULL);
994 if (cfg.noncooperative_instances || cfg.cooperative_alone) {