]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
draft 8
[chorizo.git] / browser.c
index a5b49ee927efbe616a112d3a7374be94ac28cde6..93df003240ee8855f62cad9dbc56c951ae324381 100644 (file)
--- a/browser.c
+++ b/browser.c
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <errno.h>
 #include <fcntl.h>
-#include <string.h>
-
-#include <gtk/gtk.h>
-#include <gtk/gtkx.h>
-#include <gdk/gdkkeysyms.h>
-#include <gio/gio.h>
+#include <libgen.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <webkit2/webkit2.h>
 
+#include "config.h"
+#include "chorizo.h"
+
+struct MainWindow mw;
+struct Configuration cfg;
+gboolean switch_tab;
+
+gint clients = 0;
 
-static void client_destroy(GtkWidget *, gpointer);
-static gboolean client_destroy_request(WebKitWebView *, gpointer);
-static WebKitWebView *client_new(const gchar *);
-static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
-                                         gpointer);
-static void cooperation_setup(void);
-static void changed_download_progress(GObject *, GParamSpec *, gpointer);
-static void changed_load_progress(GObject *, GParamSpec *, gpointer);
-static void changed_title(GObject *, GParamSpec *, gpointer);
-static void changed_uri(GObject *, GParamSpec *, gpointer);
-static gboolean crashed_web_view(WebKitWebView *, gpointer);
-static gboolean crashed_web_view_reload(gpointer);
-static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
-                              WebKitPolicyDecisionType, gpointer);
-static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
-static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
-static void downloadmanager_cancel(GtkToolButton *, gpointer data);
-static void downloadmanager_setup(void);
-static gchar *ensure_uri_scheme(const gchar *);
-static void grab_environment_configuration(void);
-static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
-static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
-static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
-static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
-static void keywords_load(void);
-static gboolean keywords_try_search(WebKitWebView *, const gchar *);
-static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
-static void search(gpointer, gint);
-static Window tabbed_launch(void);
-static void trust_user_certs(WebKitWebContext *);
-
-
-struct Client
+int cooperative_pipe_fp = 0;
+gchar *fifopath;
+char **closed_tabs;
+size_t num_closed = 0;
+
+gboolean
+isearch_counted_matches(GtkWidget *widget, guint matches, gpointer data)
 {
-       gchar *hover_uri;
-       GtkWidget *location;
-       GtkWidget *progress;
-       GtkWidget *scroll;
-       GtkWidget *top_box;
-       GtkWidget *vbox;
-       GtkWidget *web_view;
-       GtkWidget *win;
-};
-
-struct DownloadManager
+       struct Client *c = (struct Client *)data;
+       char *text = malloc(12);
+       sprintf(text, "%d matches", matches);
+       gtk_label_set_text(GTK_LABEL(c->isearch_matches), text);
+       free(text);
+       return TRUE;
+}
+
+gboolean
+quit_if_nothing_active(void)
 {
-       GtkWidget *scroll;
-       GtkWidget *toolbar;
-       GtkWidget *win;
-} dm;
-
-
-static const gchar *accepted_language[2] = { NULL, NULL };
-static gint clients = 0;
-static gboolean cooperative_alone = TRUE;
-static gboolean cooperative_instances = TRUE;
-static int cooperative_pipe_fp = 0;
-static int crash_autoreload_delay = 2;
-static gchar *download_dir = "/var/tmp";
-static Window embed = 0;
-static gchar *fifo_suffix = "main";
-static gdouble global_zoom = 1.0;
-static gchar *home_uri = "about:blank";
-static gboolean initial_wc_setup_done = FALSE;
-static GHashTable *keywords = NULL;
-static gchar *search_text = NULL;
-static gboolean tabbed_automagic = TRUE;
-static gchar *user_agent = NULL;
-static gchar *web_extensions_dir = NULL;
+       if (clients == 0) {
+               if (downloads == 0) {
+                       gtk_main_quit();
+                       return TRUE;
+               } else {
+                       downloadmanager_show();
+               }
+       }
+       return FALSE;
+}
+gboolean
+remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
+{
+       gchar *uri = NULL;
+       g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
+       if (uri) {
+               g_strstrip(uri);
+               client_new(uri, NULL);
+               g_free(uri);
+       }
+       return TRUE;
+}
 
+void
+allocfail(void)
+{
+       fprintf(stderr, "chorizo: fatal: alloc failed\n");
+       exit(EXIT_FAILURE);
+}
 
 void
-client_destroy(GtkWidget *obj, gpointer data)
+client_destroy(GtkWidget *widget, gpointer data)
 {
        struct Client *c = (struct Client *)data;
-
+       gint idx;
        g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
-                                            changed_load_progress, c);
+                                            changed_load_progress, c);
+
+       idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
+       if (idx == -1)
+               fprintf(stderr, "chorizo: warning: tab index was -1\n");
+       else
+               gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
+
+       if (!cfg.private && WEBKIT_IS_WEB_VIEW(c->web_view)) {
+               const char *uri =
+                       webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
+
+               // TODO: Shift everything left if over certain amount
+               if (uri != NULL) {
+                       num_closed++;
+                       if (num_closed > cfg_max_tabs_closed) {
+                               memmove(closed_tabs, closed_tabs,
+                                       cfg_max_tabs_closed - 1);
+                               num_closed = cfg_max_tabs_closed;
+                       } else {
+                               closed_tabs = realloc(
+                                       closed_tabs,
+                                       num_closed * sizeof(closed_tabs[0]));
+                               if (!closed_tabs) allocfail();
+                       }
+                       closed_tabs[num_closed - 1] = strdup(uri);
+               }
+       }
 
-       free(c);
        clients--;
+       free(c);
 
-       if (clients == 0)
-               gtk_main_quit();
+       quit_if_nothing_active();
 }
 
-gboolean
-client_destroy_request(WebKitWebView *web_view, gpointer data)
+void
+set_uri(const char *uri, struct Client *c)
 {
-       struct Client *c = (struct Client *)data;
-
-       gtk_widget_destroy(c->win);
-
-       return TRUE;
+       if (!gtk_widget_is_focus(c->location))
+               gtk_entry_set_text(GTK_ENTRY(c->location),
+                                  (uri != NULL) ? uri : "");
 }
 
 WebKitWebView *
-client_new(const gchar *uri)
+client_new(const gchar *uri, WebKitWebView *related_wv)
 {
        struct Client *c;
-       WebKitWebContext *wc;
        gchar *f;
-
-       if (uri != NULL && cooperative_instances && !cooperative_alone)
-       {
-               write(cooperative_pipe_fp, uri, strlen(uri));
+       GtkWidget *evbox, *tabbox;
+       if (uri != NULL && !cfg.noncooperative_instances &&
+           !cfg.cooperative_alone) {
+               f = ensure_uri_scheme(uri);
+               write(cooperative_pipe_fp, f, strlen(f));
                write(cooperative_pipe_fp, "\n", 1);
+               g_free(f);
                return NULL;
        }
+       c = calloc(1, sizeof(struct Client));
+       if (!c) allocfail();
+
+       if (related_wv == NULL) {
+               WebKitUserContentManager *ucm =
+                       webkit_user_content_manager_new();
+               WebKitUserScript *wkscript;
+               WebKitUserStyleSheet *wkstyle;
+               gchar *path = NULL, *source, *base;
+               const gchar *entry = NULL;
+               GDir *dir = NULL;
+               base = g_build_filename(g_get_user_data_dir(), "chorizo",
+                                       "user-scripts", NULL);
+               dir = g_dir_open(base, 0, NULL);
+               if (dir != NULL) {
+                       while ((entry = g_dir_read_name(dir)) != NULL) {
+                               path = g_build_filename(base, entry, NULL);
+                               if (g_str_has_suffix(path, ".js")) {
+                                       g_file_get_contents(path, &source, NULL,
+                                                           NULL);
+                                       // clang-format off
+                                       wkscript = webkit_user_script_new(
+                                               source,
+                                         WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+                                   WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
+                                               NULL, NULL);
+                                       // clang-format on
+                                       webkit_user_content_manager_add_script(
+                                               ucm, wkscript);
+                                       webkit_user_script_unref(wkscript);
+                               }
+                               g_free(path);
+                               if (source) g_free(source);
+                       }
+                       g_dir_close(dir);
+               }
+               base = g_build_filename(g_get_user_data_dir(), "chorizo",
+                                       "user-styles", NULL);
+               dir = g_dir_open(base, 0, NULL);
+               if (dir != NULL) {
+                       while ((entry = g_dir_read_name(dir)) != NULL) {
+                               path = g_build_filename(base, entry, NULL);
+                               if (g_str_has_suffix(path, ".css")) {
+                                       g_file_get_contents(path, &source, NULL,
+                                                           NULL);
+                                       // clang-format off
+                                       wkstyle = webkit_user_style_sheet_new(
+                                               source,
+                                         WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+                                                  WEBKIT_USER_STYLE_LEVEL_USER,
+                                               NULL, NULL);
+                                   webkit_user_content_manager_add_style_sheet(
+                                               ucm, wkstyle);
+                                       // clang-format on
+                                       webkit_user_style_sheet_unref(wkstyle);
+                               }
+                               g_free(path);
+                               g_free(source);
+                       }
+                       g_dir_close(dir);
+               }
+               g_free(base);
 
-       c = malloc(sizeof(struct Client));
-       if (!c)
-       {
-               fprintf(stderr, __NAME__": fatal: malloc failed\n");
-               exit(EXIT_FAILURE);
-       }
+               c->web_view =
+                       webkit_web_view_new_with_user_content_manager(ucm);
 
-       c->hover_uri = NULL;
-       c->win = NULL;
-       if (embed != 0)
-       {
-               c->win = gtk_plug_new(embed);
-               if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
-               {
-                       fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
-                       gtk_widget_destroy(c->win);
-                       c->win = NULL;
-                       embed = 0;
+               c->settings = webkit_web_view_get_settings(
+                       WEBKIT_WEB_VIEW(c->web_view));
+               if (cfg.verbose) {
+                       // clang-format off
+               webkit_settings_set_enable_write_console_messages_to_stdout(
+                               c->settings, true);
+                       // clang-format on
                }
+               webkit_settings_set_enable_developer_extras(c->settings, TRUE);
+       } else {
+               c->web_view = webkit_web_view_new_with_related_view(related_wv);
        }
 
-       if (c->win == NULL)
-       {
-               c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-               gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, __NAME_CAPITALIZED__);
-       }
-
-       g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
-       gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
-
-       c->web_view = webkit_web_view_new();
-       wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
-
-       webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
+       g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
+                        G_CALLBACK(changed_favicon), c);
        g_signal_connect(G_OBJECT(c->web_view), "notify::title",
-                        G_CALLBACK(changed_title), c);
+                        G_CALLBACK(changed_title), c);
        g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
-                        G_CALLBACK(changed_uri), c);
-       g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
-                        G_CALLBACK(changed_load_progress), c);
+                        G_CALLBACK(changed_uri), c);
+       g_signal_connect(G_OBJECT(c->web_view),
+                        "notify::estimated-load-progress",
+                        G_CALLBACK(changed_load_progress), c);
        g_signal_connect(G_OBJECT(c->web_view), "create",
-                        G_CALLBACK(client_new_request), NULL);
+                        G_CALLBACK(client_new_request), c);
        g_signal_connect(G_OBJECT(c->web_view), "close",
-                        G_CALLBACK(client_destroy_request), c);
+                        G_CALLBACK(client_destroy), c);
        g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
-                        G_CALLBACK(decide_policy), NULL);
+                        G_CALLBACK(decide_policy), NULL);
        g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
-                        G_CALLBACK(key_web_view), c);
-       g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
-                        G_CALLBACK(key_web_view), c);
+                        G_CALLBACK(key_web_view), c);
        g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
-                        G_CALLBACK(key_web_view), c);
+                        G_CALLBACK(key_web_view), c);
        g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
-                        G_CALLBACK(hover_web_view), c);
+                        G_CALLBACK(hover_web_view), c);
        g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
-                        G_CALLBACK(crashed_web_view), c);
-
-       if (!initial_wc_setup_done)
-       {
-               if (accepted_language[0] != NULL)
-                       webkit_web_context_set_preferred_languages(wc, accepted_language);
-
-               g_signal_connect(G_OBJECT(wc), "download-started",
-                                G_CALLBACK(download_handle_start), NULL);
-
-               trust_user_certs(wc);
-
-               initial_wc_setup_done = TRUE;
-       }
+                        G_CALLBACK(crashed_web_view), c);
 
-       if (user_agent != NULL)
-               g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
-                            "user-agent", user_agent, NULL);
-
-       c->scroll = gtk_scrolled_window_new(NULL, NULL);
-
-       gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
+       GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
 
        c->location = gtk_entry_new();
-       g_signal_connect(G_OBJECT(c->location), "key-press-event",
-                        G_CALLBACK(key_location), c);
+       gtk_entry_set_placeholder_text(GTK_ENTRY(c->location), "URL");
+       char *location_symbol;
+       if (cfg.private) {
+               location_symbol = "security-high-symbolic";
+               gtk_entry_set_icon_tooltip_markup(
+                       GTK_ENTRY(c->location), GTK_ENTRY_ICON_PRIMARY,
+                       "You are in private mode. No history, caches, or "
+                       "cookies will be saved beyond this session.");
+       } else {
+               location_symbol = "text-x-generic-symbolic";
+       }
+
+       gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
+                                         GTK_ENTRY_ICON_PRIMARY,
+                                         location_symbol);
+       gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
+
+       c->wsearch = gtk_entry_new();
+       gtk_entry_set_placeholder_text(GTK_ENTRY(c->wsearch), "Search the web");
+       gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->wsearch),
+                                         GTK_ENTRY_ICON_PRIMARY,
+                                         "system-search-symbolic");
+       gtk_box_pack_start(GTK_BOX(locbox), c->wsearch, FALSE, TRUE, 0);
 
-       /* XXX Progress bars don't work/look as intended anymore. Level bars
-        * are a dirty workaround (kind of). */
-       c->progress = gtk_level_bar_new();
-       gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), 0);
-       gtk_widget_set_size_request(c->progress, 100, -1);
-
-       c->top_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
-       gtk_box_pack_start(GTK_BOX(c->top_box), c->location, TRUE, TRUE, 2);
-       gtk_box_pack_start(GTK_BOX(c->top_box), c->progress, FALSE, FALSE, 2);
+       g_signal_connect(G_OBJECT(c->location), "key-press-event",
+                        G_CALLBACK(key_location), c);
+       g_signal_connect(G_OBJECT(c->location), "icon-release",
+                        G_CALLBACK(icon_location), c);
+       g_signal_connect(G_OBJECT(c->wsearch), "key-press-event",
+                        G_CALLBACK(key_wsearch), c);
+       /*
+        * XXX This is a workaround. Setting this to NULL (which is done in
+        * grab_feeds_finished() if no feed has been detected) adds a little
+        * padding left of the text. Not sure why. The point of this call
+        * right here is to have that padding right from the start. This
+        * avoids a graphical artifact.
+        */
+
+       gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
+                                         GTK_ENTRY_ICON_SECONDARY, NULL);
+
+       c->isearch_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+       c->isearch = gtk_entry_new();
+       gtk_entry_set_placeholder_text(GTK_ENTRY(c->isearch), "Search page");
+       gtk_box_pack_start(GTK_BOX(c->isearch_box), c->isearch, FALSE, TRUE, 0);
+       gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->isearch),
+                                         GTK_ENTRY_ICON_PRIMARY,
+                                         "system-search-symbolic");
+       g_signal_connect(G_OBJECT(c->isearch), "key-press-event",
+                        G_CALLBACK(key_isearch), c);
+
+       c->isearch_matches = gtk_label_new("0 matches");
+       gtk_box_pack_start(GTK_BOX(c->isearch_box), c->isearch_matches, FALSE,
+                          TRUE, 5);
 
        c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
-       gtk_box_pack_start(GTK_BOX(c->vbox), c->top_box, FALSE, FALSE, 2);
-       gtk_box_pack_start(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 2);
-
-       gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
-
-       gtk_widget_grab_focus(c->web_view);
-       gtk_widget_show_all(c->win);
-
-       if (uri != NULL)
-       {
+       gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(c->vbox), c->isearch_box, FALSE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(c->vbox), locbox, FALSE, FALSE, 0);
+
+       gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
+
+       c->tabicon = gtk_image_new_from_icon_name("text-html",
+                                                 GTK_ICON_SIZE_SMALL_TOOLBAR);
+
+       c->tablabel = gtk_label_new("chorizo");
+       gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
+       gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg_tab_width);
+       gtk_widget_set_has_tooltip(c->tablabel, TRUE);
+
+       /*
+        * XXX I don't own a HiDPI screen, so I don't know if scale_factor
+        * does the right thing.
+        */
+       tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
+                            5 * gtk_widget_get_scale_factor(mw.win));
+       gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
+
+       evbox = gtk_event_box_new();
+       gtk_container_add(GTK_CONTAINER(evbox), tabbox);
+       g_signal_connect(G_OBJECT(evbox), "button-release-event",
+                        G_CALLBACK(key_tablabel), c);
+
+       gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
+       g_signal_connect(G_OBJECT(evbox), "scroll-event",
+                        G_CALLBACK(key_tablabel), c);
+
+       // For easy access, store a reference to our label.
+       g_object_set_data(G_OBJECT(evbox), "chorizo-tab-label", c->tablabel);
+
+       /*
+        * This only shows the event box and the label inside, nothing else.
+        * Needed because the evbox/label is "internal" to the notebook and
+        * not part of the normal "widget tree" (IIUC).
+        */
+       gtk_widget_show_all(evbox);
+
+       int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1;
+       gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
+                                page);
+       gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox,
+                                        TRUE);
+
+       show_web_view(NULL, c);
+
+       if (uri != NULL) {
                f = ensure_uri_scheme(uri);
                webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
                g_free(f);
        }
+       set_uri(uri, c);
 
        clients++;
 
+       if (uri == NULL) gtk_widget_grab_focus(c->location);
+
        return WEBKIT_WEB_VIEW(c->web_view);
 }
 
 WebKitWebView *
 client_new_request(WebKitWebView *web_view,
-                   WebKitNavigationAction *navigation_action, gpointer data)
+                  WebKitNavigationAction *navigation_action, gpointer data)
+{
+       switch_tab = FALSE;
+       WebKitWebView *new_web_view = client_new(NULL, web_view);
+       gtk_widget_grab_focus(GTK_WIDGET(new_web_view));
+       return new_web_view;
+}
+
+void
+mkdirp(const char *dir, mode_t mode)
 {
-       return client_new(NULL);
+       char tmp[256];
+       char *p = NULL;
+       size_t len;
+       snprintf(tmp, sizeof(tmp), "%s", dir);
+       len = strlen(tmp);
+       if (tmp[len - 1] == '/') tmp[len - 1] = 0;
+       for (p = tmp + 1; *p; p++)
+               if (*p == '/') {
+                       *p = 0;
+                       mkdir(tmp, mode);
+                       *p = '/';
+               }
+       mkdir(tmp, S_IRWXU);
 }
 
 void
 cooperation_setup(void)
 {
        GIOChannel *towatch;
-       gchar *fifofilename, *fifopath;
-
-       fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
-       fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
+       gchar *fifofilename;
+
+       gchar *priv = (cfg.private) ? "-private" : "";
+       const gchar *fifo_suffix_env = g_getenv("CHORIZO_FIFO_SUFFIX");
+       const gchar *fifo_suffix = (fifo_suffix_env) ? fifo_suffix_env : "";
+       fifofilename = g_strdup_printf("%s%s%s%s", "chorizo", priv, ".fifo",
+                                      fifo_suffix);
+       fifopath = g_build_filename(g_get_user_runtime_dir(), "chorizo",
+                                   fifofilename, NULL);
+       mkdirp(dirname(fifopath), 0600);
        g_free(fifofilename);
 
-       if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
-               mkfifo(fifopath, 0600);
+       if (!g_file_test(fifopath, G_FILE_TEST_EXISTS)) mkfifo(fifopath, 0600);
 
        cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
-       if (!cooperative_pipe_fp)
-       {
-               fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
-       }
-       else
-       {
-               if (write(cooperative_pipe_fp, "", 0) == -1)
-               {
-                       /* Could not do an empty write to the FIFO which means there's
-                        * no one listening. */
+       if (!cooperative_pipe_fp) {
+               fprintf(stderr, "chorizo: error: can't open FIFO\n");
+       } else {
+               if (write(cooperative_pipe_fp, "", 0) == -1) {
+                       /*
+                        * Could not do an empty write to the FIFO which
+                        * means there's no one listening.
+                        */
                        close(cooperative_pipe_fp);
                        towatch = g_io_channel_new_file(fifopath, "r+", NULL);
-                       g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
+                       g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg,
+                                      NULL);
+               } else {
+                       cfg.cooperative_alone = FALSE;
                }
-               else
-                       cooperative_alone = FALSE;
        }
-
-       g_free(fifopath);
 }
 
 void
-changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
+changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
 {
-       WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
-       WebKitURIResponse *resp;
-       GtkToolItem *tb = GTK_TOOL_ITEM(data);
-       gdouble p, size_mb;
-       const gchar *uri;
-       gchar *t, *filename, *base;
-
-       p = webkit_download_get_estimated_progress(download) * 100;
-       resp = webkit_download_get_response(download);
-       size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
-
-       uri = webkit_download_get_destination(download);
-       filename = g_filename_from_uri(uri, NULL, NULL);
-       if (filename == NULL)
-       {
-               /* This really should not happen because WebKit uses that URI to
-                * write to a file... */
-               fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
-               t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
-                                   webkit_uri_response_get_uri(resp), p, size_mb);
-       }
-       else
-       {
-               base = g_path_get_basename(filename);
-               t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
-               g_free(filename);
-               g_free(base);
-       }
-       gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
-       g_free(t);
+       struct Client *c = (struct Client *)data;
+       gdouble p;
+       gchar *grab_feeds =
+               "a = document.querySelectorAll('"
+               "    html > head > "
+               "link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
+               "    html > head > "
+               "link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
+               "');"
+               "if (a.length == 0)"
+               "    null;"
+               "else {"
+               "    out = '';"
+               "    for (i = 0; i < a.length; i++) {"
+               "        url = encodeURIComponent(a[i].href);"
+               "        if ('title' in a[i] && a[i].title != '')"
+               "            title = encodeURIComponent(a[i].title);"
+               "        else"
+               "            title = url;"
+               "        out += '<li><a href=\"' + url + '\">' + title + "
+               "'</a></li>';"
+               "    }"
+               "    out;"
+               "}";
+       p = webkit_web_view_get_estimated_load_progress(
+               WEBKIT_WEB_VIEW(c->web_view));
+       if (p == 1) {
+               p = 0;
+
+               /*
+                * The page has loaded fully. We now run the short JavaScript
+                * snippet above that operates on the DOM. It tries to grab
+                * all occurences of <link rel="alternate" ...>, i.e.
+                * RSS/Atom feed references.
+                */
+               webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
+                                              grab_feeds, NULL,
+                                              grab_feeds_finished, c);
+       }
+       gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
 }
 
 void
-changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
+changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
 {
        struct Client *c = (struct Client *)data;
-       gdouble p;
-
-       p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
-       gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), p);
+       cairo_surface_t *f;
+       int w, h, w_should, h_should;
+       GdkPixbuf *pb, *pb_scaled;
+       f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
+       if (f == NULL) {
+               gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
+                                            GTK_ICON_SIZE_SMALL_TOOLBAR);
+       } else {
+               w = cairo_image_surface_get_width(f);
+               h = cairo_image_surface_get_height(f);
+               pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
+               if (pb != NULL) {
+                       w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
+                       h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
+                       pb_scaled = gdk_pixbuf_scale_simple(
+                               pb, w_should, h_should, GDK_INTERP_BILINEAR);
+                       gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon),
+                                                 pb_scaled);
+
+                       g_object_unref(pb_scaled);
+                       g_object_unref(pb);
+               }
+       }
 }
 
 void
 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
 {
-       const gchar *t;
+       const gchar *t, *u;
        struct Client *c = (struct Client *)data;
-
+       u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
        t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
-       gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
+
+       if (t == NULL) t = (u == NULL) ? "chorizo" : u;
+
+       gchar *name = malloc(strlen(t) + 4);
+       if (!name) allocfail();
+       gboolean mute =
+               webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
+       gchar *muted = (mute) ? "[M] " : "";
+       sprintf(name, "%s%s", muted, t);
+       gtk_label_set_text(GTK_LABEL(c->tablabel), name);
+       g_free(name);
+
+       gtk_widget_set_tooltip_text(c->tablabel, t);
+       mainwindow_title(
+               gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
 }
 
 void
@@ -337,639 +520,481 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
 {
        const gchar *t;
        struct Client *c = (struct Client *)data;
-
+       FILE *fp;
        t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
-       gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
-}
 
-gboolean
-crashed_web_view(WebKitWebView *web_view, gpointer data)
-{
-       fprintf(stderr, __NAME__": WebView crashed!\n");
-       if (crash_autoreload_delay >= 1)
-       {
-               fprintf(stderr, __NAME__": Reloading WebView in %d seconds.\n",
-                       crash_autoreload_delay);
-               g_timeout_add_seconds(crash_autoreload_delay, crashed_web_view_reload,
-                                     web_view);
+       if (t != NULL && strlen(t) > 0) {
+               set_uri(t, c);
+
+               // No g_get_user_state_dir unfortunately
+               gchar *state_env = getenv("XDG_STATE_DIR");
+               // clang-format off
+               gchar *state_dir = (state_env)
+                                       ? state_env
+                                       : g_build_filename(g_get_home_dir(),
+                                                           ".local", "state",
+                                                           "chorizo", NULL);
+               // clang-format on
+
+               gchar *history_file =
+                       g_build_filename(state_dir, "history", NULL);
+               if (!cfg.private) {
+                       mkdirp(state_dir, 0700);
+                       fp = fopen(history_file, "a");
+                       if (fp != NULL) {
+                               fprintf(fp, "%s\n", t);
+                               fclose(fp);
+                       } else {
+                               perror("chorizo: error: could not open history"
+                                      "file");
+                       }
+               }
+               g_free(history_file);
+               g_free(state_dir);
        }
-
-       return TRUE;
 }
 
 gboolean
-crashed_web_view_reload(gpointer data)
+crashed_web_view(WebKitWebView *web_view, gpointer data)
 {
-       webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(data));
+       GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
+       GtkWidget *dialog = gtk_message_dialog_new(
+               GTK_WINDOW(mw.win), flags, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
+               "ERROR: Web process %s crashed.\n%s",
+               webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)),
+               g_strerror(errno));
+       gtk_dialog_run(GTK_DIALOG(dialog));
+       gtk_widget_destroy(dialog);
 
-       return G_SOURCE_REMOVE;
+       return TRUE;
 }
 
 gboolean
 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
-              WebKitPolicyDecisionType type, gpointer data)
+             WebKitPolicyDecisionType type, gpointer data)
 {
        WebKitResponsePolicyDecision *r;
-
-       switch (type)
-       {
-               case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
-                       r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
-                       if (!webkit_response_policy_decision_is_mime_type_supported(r))
-                               webkit_policy_decision_download(decision);
-                       else
-                               webkit_policy_decision_use(decision);
-                       break;
-               default:
-                       /* Use whatever default there is. */
-                       return FALSE;
+       switch (type) {
+       case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
+               r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
+               if (!webkit_response_policy_decision_is_mime_type_supported(r))
+                       webkit_policy_decision_download(decision);
+               else
+                       webkit_policy_decision_use(decision);
+               break;
+       default:
+               // Use whatever default there is.
+               return FALSE;
        }
        return TRUE;
 }
 
-void
-download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
-                      gpointer data)
+gchar *
+ensure_uri_scheme(const gchar *t)
 {
-       g_signal_connect(G_OBJECT(download), "decide-destination",
-                        G_CALLBACK(download_handle), data);
+       gchar *f, *fabs;
+       f = g_ascii_strdown(t, -1);
+       if (!g_str_has_prefix(f, "http:") && !g_str_has_prefix(f, "https:") &&
+           !g_str_has_prefix(f, "file:") && !g_str_has_prefix(f, "about:") &&
+           !g_str_has_prefix(f, "data:") && !g_str_has_prefix(f, "webkit:")) {
+               g_free(f);
+               fabs = realpath(t, NULL);
+               if (fabs != NULL) {
+                       f = g_strdup_printf("file://%s", fabs);
+                       free(fabs);
+               } else {
+                       f = g_strdup_printf("http://%s", t);
+               }
+               return f;
+       } else
+               return g_strdup(t);
 }
 
-gboolean
-download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
+void
+grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
 {
-       gchar *path, *path2 = NULL, *uri;
-       GtkToolItem *tb;
-       int suffix = 1;
-
-       path = g_build_filename(download_dir, suggested_filename, NULL);
-       path2 = g_strdup(path);
-       while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
-       {
-               g_free(path2);
-
-               path2 = g_strdup_printf("%s.%d", path, suffix);
-               suffix++;
-       }
-
-       if (suffix == 1000)
-       {
-               fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
-               webkit_download_cancel(download);
+       struct Client *c = (struct Client *)data;
+       WebKitJavascriptResult *js_result;
+       JSCValue *value;
+       JSCException *exception;
+       GError *err = NULL;
+       gchar *str_value;
+       g_free(c->feed_html);
+       c->feed_html = NULL;
+
+       /*
+        * This was taken almost verbatim from the example in WebKit's
+        * documentation:
+        *
+        * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
+        */
+
+       js_result = webkit_web_view_run_javascript_finish(
+               WEBKIT_WEB_VIEW(object), result, &err);
+       if (!js_result) {
+               fprintf(stderr,
+                       "chorizo: error: error running javascript: %s\n",
+                       err->message);
+               g_error_free(err);
+               return;
        }
-       else
-       {
-               uri = g_filename_to_uri(path2, NULL, NULL);
-               webkit_download_set_destination(download, uri);
-               g_free(uri);
-
-               tb = gtk_tool_button_new(NULL, NULL);
-               gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
-               gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), suggested_filename);
-               gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
-               gtk_widget_show_all(dm.win);
-
-               g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
-                                G_CALLBACK(changed_download_progress), tb);
+       value = webkit_javascript_result_get_js_value(js_result);
+       if (jsc_value_is_string(value)) {
+               str_value = jsc_value_to_string(value);
+               exception =
+                       jsc_context_get_exception(jsc_value_get_context(value));
+               if (exception != NULL) {
+                       fprintf(stderr,
+                               "chorizo: warning: error running javascript:"
+                               "%s\n",
+                               jsc_exception_get_message(exception));
+               } else {
+                       c->feed_html = str_value;
+               }
 
-               g_object_ref(download);
-               g_signal_connect(G_OBJECT(tb), "clicked",
-                                G_CALLBACK(downloadmanager_cancel), download);
+               gtk_entry_set_icon_from_icon_name(
+                       GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY,
+                       "application-rss+xml-symbolic");
+               gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
+                                              GTK_ENTRY_ICON_SECONDARY, TRUE);
+       } else {
+               gtk_entry_set_icon_from_icon_name(
+                       GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY, NULL);
        }
 
-       g_free(path);
-       g_free(path2);
-
-       /* Propagate -- to whom it may concern. */
-       return FALSE;
+       webkit_javascript_result_unref(js_result);
 }
 
 void
-downloadmanager_cancel(GtkToolButton *tb, gpointer data)
+hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht,
+              guint modifiers, gpointer data)
 {
-       WebKitDownload *download = WEBKIT_DOWNLOAD(data);
+       struct Client *c = (struct Client *)data;
+       const char *to_show;
+       g_free(c->hover_uri);
 
-       webkit_download_cancel(download);
-       g_object_unref(download);
+       if (webkit_hit_test_result_context_is_link(ht)) {
+               to_show = webkit_hit_test_result_get_link_uri(ht);
+               c->hover_uri = g_strdup(to_show);
+       } else {
+               to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
+               c->hover_uri = NULL;
+       }
 
-       gtk_widget_destroy(GTK_WIDGET(tb));
+       if (!gtk_widget_is_focus(c->location)) set_uri(to_show, c);
 }
 
 void
-downloadmanager_setup(void)
-{
-       dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
-       gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
-       gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
-       g_signal_connect(G_OBJECT(dm.win), "delete-event",
-                        G_CALLBACK(gtk_widget_hide_on_delete), NULL);
-       g_signal_connect(G_OBJECT(dm.win), "key-press-event",
-                        G_CALLBACK(key_downloadmanager), NULL);
-
-       dm.toolbar = gtk_toolbar_new();
-       gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
-                                      GTK_ORIENTATION_VERTICAL);
-       gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
-       gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
-
-       dm.scroll = gtk_scrolled_window_new(NULL, NULL);
-       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
-                                      GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
-       gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
-
-       gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
-}
-
-gchar *
-ensure_uri_scheme(const gchar *t)
+icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
+             gpointer data)
 {
-       gchar *f;
-
-       f = g_ascii_strdown(t, -1);
-       if (!g_str_has_prefix(f, "http:") &&
-           !g_str_has_prefix(f, "https:") &&
-           !g_str_has_prefix(f, "file:") &&
-           !g_str_has_prefix(f, "about:"))
-       {
-               g_free(f);
-               f = g_strdup_printf("http://%s", t);
-               return f;
+       struct Client *c = (struct Client *)data;
+       gchar *d;
+       gchar *data_template = "data:text/html,"
+                              "<!DOCTYPE html>"
+                              "<html>"
+                              "    <head>"
+                              "        <meta charset=\"UTF-8\">"
+                              "        <title>Feeds</title>"
+                              "    </head>"
+                              "    <body>"
+                              "        <p>Feeds found on this page:</p>"
+                              "        <ul>"
+                              "        %s"
+                              "        </ul>"
+                              "    </body>"
+                              "</html>";
+       if (c->feed_html != NULL) {
+               /*
+                * What we're actually trying to do is show a simple HTML
+                * page that lists all the feeds on the current page. The
+                * function webkit_web_view_load_html() looks like the proper
+                * way to do that. Sad thing is, it doesn't create a history
+                * entry, but instead simply replaces the content of the
+                * current page. This is not what we want.
+                *
+                * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
+                * mechanism to show my custom HTML snippet* and*create a
+                * history entry.
+                *
+                * [0]: https://tools.ietf.org/html/rfc2397 [1]:
+                * https://en.wikipedia.org/wiki/Data_URI_scheme
+                */
+
+               d = g_strdup_printf(data_template, c->feed_html);
+               webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
+               g_free(d);
        }
-       else
-               return g_strdup(t);
 }
 
 void
-grab_environment_configuration(void)
+init_default_web_context(void)
 {
-       const gchar *e;
-
-       e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
-       if (e != NULL)
-               accepted_language[0] = g_strdup(e);
-
-       e = g_getenv(__NAME_UPPERCASE__"_CRASH_AUTORELOAD_DELAY");
-       if (e != NULL)
-               crash_autoreload_delay = atoi(e);
-
-       e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
-       if (e != NULL)
-               download_dir = g_strdup(e);
-
-       e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
-       if (e != NULL)
-               fifo_suffix = g_strdup(e);
-
-       e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
-       if (e != NULL)
-               home_uri = g_strdup(e);
-
-       e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
-       if (e != NULL)
-               user_agent = g_strdup(e);
-
-       e = g_getenv(__NAME_UPPERCASE__"_WEB_EXTENSIONS_DIR");
-       if (e != NULL)
-               web_extensions_dir = g_strdup(e);
-       else
-               web_extensions_dir = g_build_filename(g_get_user_data_dir(), __NAME__,
-                                                     "web_extensions", NULL);
-
-       e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
-       if (e != NULL)
-               global_zoom = atof(e);
+       gchar *p;
+       WebKitWebContext *wc;
+       WebKitCookieManager *cm;
+       wc = (cfg.private) ? webkit_web_context_new_ephemeral() :
+                                  webkit_web_context_get_default();
+
+       p = g_build_filename(g_get_user_config_dir(), "chorizo", "adblock",
+                            NULL);
+       webkit_web_context_set_sandbox_enabled(wc, TRUE);
+       webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
+       g_free(p);
+
+       webkit_web_context_set_process_model(
+               wc, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
+
+       p = g_build_filename(g_get_user_data_dir(), "chorizo", "web-extensions",
+                            NULL);
+       webkit_web_context_set_web_extensions_directory(wc, p);
+       g_free(p);
+
+       char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
+       g_signal_connect(G_OBJECT(wc), "download-started",
+                        G_CALLBACK(download_start),
+                        (xdg_down) ? xdg_down : "/var/tmp");
+
+       trust_user_certs(wc);
+
+       cm = webkit_web_context_get_cookie_manager(wc);
+       webkit_cookie_manager_set_accept_policy(cm, cfg_cookie_policy);
+
+       if (!cfg.private) {
+               webkit_web_context_set_favicon_database_directory(wc, NULL);
+
+               gchar *fname = g_build_filename("/", g_get_user_data_dir(),
+                                               "chorizo", "cookies.db", NULL);
+               mkdirp(dirname(fname), 0700);
+               WebKitCookiePersistentStorage type =
+                       WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
+               webkit_cookie_manager_set_persistent_storage(cm, fname, type);
+               g_free(fname);
+       }
+       webkit_web_context_set_spell_checking_enabled(wc, TRUE);
 }
 
 void
-hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
-               gpointer data)
+isearch(gpointer data, gint direction)
 {
        struct Client *c = (struct Client *)data;
-
-       if (!gtk_widget_is_focus(c->location))
-       {
-               if (webkit_hit_test_result_context_is_link(ht))
-               {
-                       gtk_entry_set_text(GTK_ENTRY(c->location),
-                                          webkit_hit_test_result_get_link_uri(ht));
-
-                       if (c->hover_uri != NULL)
-                               g_free(c->hover_uri);
-                       c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
-               }
-               else
-               {
-                       gtk_entry_set_text(GTK_ENTRY(c->location),
-                                          webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
-
-                       if (c->hover_uri != NULL)
-                               g_free(c->hover_uri);
-                       c->hover_uri = NULL;
-               }
+       WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
+       WebKitFindController *fc =
+               webkit_web_view_get_find_controller(web_view);
+       const gchar *isearch_text = gtk_entry_get_text(GTK_ENTRY(c->isearch));
+       if (isearch_text == NULL) return;
+
+       switch (direction) {
+       case 0:
+               g_signal_connect(G_OBJECT(fc), "counted-matches",
+                                G_CALLBACK(isearch_counted_matches), c);
+               webkit_find_controller_count_matches(
+                       fc, isearch_text, cfg_isearch_options, G_MAXUINT);
+               webkit_find_controller_search(fc, isearch_text,
+                                             cfg_isearch_options, G_MAXUINT);
+               break;
+       case 1:
+               webkit_find_controller_search_next(fc);
+               break;
+       case -1:
+               webkit_find_controller_search_previous(fc);
+               break;
+       case 2:
+               webkit_find_controller_search_finish(fc);
+               break;
        }
 }
 
-gboolean
-key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-       if (event->type == GDK_KEY_PRESS)
-       {
-               if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
-               {
-                       switch (((GdkEventKey *)event)->keyval)
-                       {
-                               case GDK_KEY_d:  /* close window (left hand) */
-                                       gtk_widget_hide(dm.win);
-                                       return TRUE;
-                       }
-               }
-       }
-
-       return FALSE;
-}
-
-gboolean
-key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
+void
+isearch_init(struct Client *c, int direction)
 {
-       struct Client *c = (struct Client *)data;
-       const gchar *t;
-       gchar *f;
-       WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
-
-       if (event->type == GDK_KEY_PRESS)
-       {
-               if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
-               {
-                       switch (((GdkEventKey *)event)->keyval)
-                       {
-                               case GDK_KEY_q:  /* close window (left hand) */
-                                       gtk_widget_destroy(c->win);
-                                       return TRUE;
-                               case GDK_KEY_d:  /* download manager (left hand) */
-                                       gtk_widget_show_all(dm.win);
-                                       return TRUE;
-                               case GDK_KEY_r:  /* reload (left hand) */
-                                       webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
-                                                                           c->web_view));
-                                       return TRUE;
-                               case GDK_KEY_k:  /* initiate search (BOTH hands) */
-                                       gtk_entry_set_text(GTK_ENTRY(c->location), "/");
-                                       gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
-                                       return TRUE;
-                               case GDK_KEY_c:  /* reload trusted certs (left hand) */
-                                       trust_user_certs(wc);
-                                       return TRUE;
-                       }
-               }
-               else
-               {
-                       switch (((GdkEventKey *)event)->keyval)
-                       {
-                               case GDK_KEY_Return:
-                                       gtk_widget_grab_focus(c->web_view);
-                                       t = gtk_entry_get_text(GTK_ENTRY(c->location));
-                                       if (t != NULL && t[0] == '/')
-                                       {
-                                               if (search_text != NULL)
-                                                       g_free(search_text);
-                                               search_text = g_strdup(t + 1);  /* XXX whacky */
-                                               search(c, 0);
-                                       }
-                                       else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
-                                       {
-                                               f = ensure_uri_scheme(t);
-                                               webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
-                                               g_free(f);
-                                       }
-                                       return TRUE;
-                               case GDK_KEY_Escape:
-                                       t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
-                                       gtk_entry_set_text(GTK_ENTRY(c->location),
-                                                          (t == NULL ? __NAME__ : t));
-                                       return TRUE;
-                       }
-               }
+       if (webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view))) {
+               gtk_widget_show(c->isearch_box);
+               gtk_widget_grab_focus(c->isearch);
        }
-
-       return FALSE;
 }
 
-gboolean
-key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
+void
+reopen_tab(void)
 {
-       struct Client *c = (struct Client *)data;
-       gdouble dx, dy;
-       gchar *f;
-       gfloat z;
-       WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
-
-       if (event->type == GDK_KEY_PRESS)
-       {
-               if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
-               {
-                       switch (((GdkEventKey *)event)->keyval)
-                       {
-                               case GDK_KEY_q:  /* close window (left hand) */
-                                       gtk_widget_destroy(c->win);
-                                       return TRUE;
-                               case GDK_KEY_w:  /* home (left hand) */
-                                       f = ensure_uri_scheme(home_uri);
-                                       webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
-                                       g_free(f);
-                                       return TRUE;
-                               case GDK_KEY_e:  /* new tab (left hand) */
-                                       f = ensure_uri_scheme(home_uri);
-                                       client_new(f);
-                                       g_free(f);
-                                       return TRUE;
-                               case GDK_KEY_r:  /* reload (left hand) */
-                                       webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
-                                                                           c->web_view));
-                                       return TRUE;
-                               case GDK_KEY_d:  /* download manager (left hand) */
-                                       gtk_widget_show_all(dm.win);
-                                       return TRUE;
-                               case GDK_KEY_2:  /* search forward (left hand) */
-                               case GDK_KEY_n:  /* search forward (maybe both hands) */
-                                       search(c, 1);
-                                       return TRUE;
-                               case GDK_KEY_3:  /* search backward (left hand) */
-                                       search(c, -1);
-                                       return TRUE;
-                               case GDK_KEY_l:  /* location (BOTH hands) */
-                                       gtk_widget_grab_focus(c->location);
-                                       return TRUE;
-                               case GDK_KEY_k:  /* initiate search (BOTH hands) */
-                                       gtk_widget_grab_focus(c->location);
-                                       gtk_entry_set_text(GTK_ENTRY(c->location), "/");
-                                       gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
-                                       return TRUE;
-                               case GDK_KEY_c:  /* reload trusted certs (left hand) */
-                                       trust_user_certs(wc);
-                                       return TRUE;
-                       }
-               }
-               else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
-               {
-                       webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
-                       gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), 0);
-               }
-       }
-       else if (event->type == GDK_BUTTON_PRESS)
-       {
-               switch (((GdkEventButton *)event)->button)
-               {
-                       case 2:
-                               if (c->hover_uri != NULL)
-                               {
-                                       client_new(c->hover_uri);
-                                       return TRUE;
-                               }
-                               break;
-                       case 8:
-                               webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
-                               return TRUE;
-                       case 9:
-                               webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
-                               return TRUE;
-               }
-       }
-       else if (event->type == GDK_SCROLL)
-       {
-               if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
-                   ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
-               {
-                       gdk_event_get_scroll_deltas(event, &dx, &dy);
-                       z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
-                       z += -dy * 0.1;
-                       z = dx != 0 ? global_zoom : z;
-                       webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
-                       return TRUE;
-               }
-       }
-
-       return FALSE;
+       if (num_closed == 0) return;
+       client_new(closed_tabs[num_closed - 1], NULL);
+       num_closed--;
+       closed_tabs = realloc(closed_tabs, num_closed * sizeof(closed_tabs[0]));
+       if (!closed_tabs) allocfail();
 }
 
 void
-keywords_load(void)
+mainwindow_setup(void)
 {
-       GError *err = NULL;
-       GIOChannel *channel = NULL;
-       gchar *path = NULL, *buf = NULL;
-       gchar **tokens = NULL;
-
-       keywords = g_hash_table_new(g_str_hash, g_str_equal);
-
-       path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
-                               NULL);
-       channel = g_io_channel_new_file(path, "r", &err);
-       if (channel != NULL)
-       {
-               while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
-                      == G_IO_STATUS_NORMAL)
-               {
-                       g_strstrip(buf);
-                       if (buf[0] != '#')
-                       {
-                               tokens = g_strsplit(buf, " ", 2);
-                               if (tokens[0] != NULL && tokens[1] != NULL)
-                                       g_hash_table_insert(keywords, g_strdup(tokens[0]),
-                                                           g_strdup(tokens[1]));
-                               g_strfreev(tokens);
-                       }
-                       g_free(buf);
-               }
-               g_io_channel_shutdown(channel, FALSE, NULL);
-       }
-       g_free(path);
+       mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+       gtk_window_set_default_size(GTK_WINDOW(mw.win), 1200, 900);
+       g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
+
+       gchar *priv = (cfg.private) ? "-private" : "";
+       gchar *title = malloc(strlen(priv) + 7);
+       if (!title) allocfail();
+       sprintf(title, "%s%s", "chorizo", priv);
+       gtk_window_set_title(GTK_WINDOW(mw.win), title);
+       g_free(title);
+
+       mw.notebook = gtk_notebook_new();
+       gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
+       gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), GTK_POS_LEFT);
+       gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
+       g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
+                        G_CALLBACK(notebook_switch_page), NULL);
+
+       GtkCssProvider *css = gtk_css_provider_new();
+       const char *css_data = "notebook header.left * { \
+                                       margin: 0; \
+                                       padding-top: 0; \
+                                       padding-bottom: 0; \
+                               }";
+       gtk_css_provider_load_from_data(css, css_data, strlen(css_data), NULL);
+       gtk_style_context_add_provider_for_screen(
+               gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
+               GTK_STYLE_PROVIDER_PRIORITY_USER);
+
+       gtk_widget_show_all(mw.win);
 }
 
-gboolean
-keywords_try_search(WebKitWebView *web_view, const gchar *t)
+void
+mainwindow_title(gint idx)
 {
-       gboolean ret = FALSE;
-       gchar **tokens = NULL;
-       gchar *val = NULL, *uri = NULL;
-
-       tokens = g_strsplit(t, " ", 2);
-       if (tokens[0] != NULL && tokens[1] != NULL)
-       {
-               val = g_hash_table_lookup(keywords, tokens[0]);
-               if (val != NULL)
-               {
-                       uri = g_strdup_printf((gchar *)val, tokens[1]);
-                       webkit_web_view_load_uri(web_view, uri);
-                       g_free(uri);
-                       ret = TRUE;
-               }
-       }
-       g_strfreev(tokens);
-
-       return ret;
+       GtkWidget *child, *widg, *tablabel;
+       const gchar *text;
+       child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
+       if (child == NULL) return;
+
+       widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
+       tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg),
+                                                 "chorizo-tab-label");
+       text = gtk_label_get_text(GTK_LABEL(tablabel));
+       gtk_window_set_title(GTK_WINDOW(mw.win), text);
 }
 
-gboolean
-remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
+void
+notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
 {
-       gchar *uri = NULL;
-
-       g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
-       if (uri)
-       {
-               g_strstrip(uri);
-               client_new(uri);
-               g_free(uri);
-       }
-       return TRUE;
+       mainwindow_title(idx);
 }
 
 void
-search(gpointer data, gint direction)
+show_web_view(WebKitWebView *web_view, gpointer data)
 {
        struct Client *c = (struct Client *)data;
-       WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
-       WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
+       (void)web_view;
+       gint idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
 
-       if (search_text == NULL)
-               return;
+       gtk_widget_show_all(GTK_WIDGET(
+               gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx)));
+       gtk_widget_hide(c->isearch_box);
 
-       switch (direction)
-       {
-               case 0:
-                       webkit_find_controller_search(fc, search_text,
-                                                     WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
-                                                     WEBKIT_FIND_OPTIONS_WRAP_AROUND,
-                                                     G_MAXUINT);
-                       break;
-               case 1:
-                       webkit_find_controller_search_next(fc);
-                       break;
-               case -1:
-                       webkit_find_controller_search_previous(fc);
-                       break;
-       }
-}
-
-Window
-tabbed_launch(void)
-{
-       gint tabbed_stdout;
-       GIOChannel *tabbed_stdout_channel;
-       GError *err = NULL;
-       gchar *output = NULL;
-       char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
-       Window plug_into;
-
-       if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
-                                     NULL, NULL, NULL, &tabbed_stdout, NULL,
-                                     &err))
-       {
-               fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
-               g_error_free(err);
-               return 0;
-       }
-
-       tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
-       if (tabbed_stdout_channel == NULL)
-       {
-               fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
-               return 0;
-       }
-       g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
-       g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
-       if (output == NULL)
-       {
-               fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
-               return 0;
-       }
-       g_strstrip(output);
-       plug_into = strtol(output, NULL, 16);
-       g_free(output);
-       if (plug_into == 0)
-               fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
-       return plug_into;
+       if (idx != -1 && switch_tab)
+               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
+       gtk_widget_grab_focus(c->web_view);
 }
 
 void
 trust_user_certs(WebKitWebContext *wc)
 {
        GTlsCertificate *cert;
-       const gchar *basedir, *file, *absfile;
-       GDir *dir;
-
-       basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
+       gchar *basedir, *absfile;
+       const gchar *file;
+       GDir *dir = NULL;
+       basedir = g_build_filename(g_get_user_data_dir(), "chorizo", "certs",
+                                  NULL);
        dir = g_dir_open(basedir, 0, NULL);
-       if (dir != NULL)
-       {
+       g_free(basedir);
+       if (dir != NULL) {
                file = g_dir_read_name(dir);
-               while (file != NULL)
-               {
-                       absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
-                                                  file, NULL);
+               while (file != NULL) {
+                       absfile = g_build_filename(g_get_user_data_dir(),
+                                                  "chorizo", "certs", file,
+                                                  NULL);
                        cert = g_tls_certificate_new_from_file(absfile, NULL);
+                       g_free(absfile);
                        if (cert == NULL)
-                               fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
+                               fprintf(stderr,
+                                       "chorizo: warning: could not load"
+                                       "trusted cert: %s\n",
+                                       file);
                        else
-                               webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
+                               // clang-format off
+                             webkit_web_context_allow_tls_certificate_for_host(
+                                       wc, cert, file);
+                       // clang-format on
                        file = g_dir_read_name(dir);
                }
                g_dir_close(dir);
        }
 }
 
+void
+version(void)
+{
+       printf("%s %s\n", "chorizo", VERSION);
+}
 
 int
 main(int argc, char **argv)
 {
        int opt, i;
 
-       gtk_init(&argc, &argv);
+       // TODO:pretty this
+       cfg.noncooperative_instances = FALSE;
+       cfg.cooperative_alone = TRUE;
+       closed_tabs = malloc(0);
+       if (!closed_tabs) allocfail();
 
-       grab_environment_configuration();
-
-       while ((opt = getopt(argc, argv, "e:CT")) != -1)
-       {
-               switch (opt)
-               {
-                       case 'e':
-                               embed = atol(optarg);
-                               tabbed_automagic = FALSE;
-                               break;
-                       case 'C':
-                               cooperative_instances = FALSE;
-                               break;
-                       case 'T':
-                               tabbed_automagic = FALSE;
-                               break;
-                       default:
-                               fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
-                               exit(EXIT_FAILURE);
+       while ((opt = getopt(argc, argv, "cpvV")) != -1) {
+               switch (opt) {
+               case 'c':
+                       cfg.noncooperative_instances = TRUE;
+                       break;
+               case 'p':
+                       cfg.private = TRUE;
+                       break;
+               case 'v':
+                       cfg.verbose = TRUE;
+                       break;
+               case 'V':
+                       version();
+                       exit(0);
+               default:
+                       fprintf(stderr,
+                               "usage: chorizo [OPTION]... [URI]...\n");
+                       exit(EXIT_FAILURE);
                }
        }
 
-       keywords_load();
-       if (cooperative_instances)
-               cooperation_setup();
-       downloadmanager_setup();
+       if (cfg.verbose) version();
 
-       if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
-               embed = tabbed_launch();
+       gtk_init(&argc, &argv);
 
-       if (!cooperative_instances || cooperative_alone)
-               webkit_web_context_set_web_extensions_directory(webkit_web_context_get_default(),
-                                                               web_extensions_dir);
+       // Keep clipboard contents after program closes
+       gtk_clipboard_store(gtk_clipboard_get_for_display(
+               gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
 
-       if (optind >= argc)
-               client_new(home_uri);
-       else
-       {
-               for (i = optind; i < argc; i++)
-                       client_new(argv[i]);
+       if (!cfg.noncooperative_instances) cooperation_setup();
+
+       if (cfg.noncooperative_instances || cfg.cooperative_alone)
+               init_default_web_context();
+
+       downloadmanager_setup();
+       mainwindow_setup();
+
+       if (optind >= argc) {
+               client_new(cfg_home_uri, NULL);
+       } else {
+               for (i = optind; i < argc; i++) client_new(argv[i], NULL);
        }
 
-       if (!cooperative_instances || cooperative_alone)
+       if (cfg.noncooperative_instances || cfg.cooperative_alone) {
                gtk_main();
+               remove(fifopath);
+       }
+
        exit(EXIT_SUCCESS);
 }