]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
Revamp downloads manager
[chorizo.git] / browser.c
index 5731056992339696c2162cdbb4585dd3acc339ce..ede3ec69230dbe31d6c89e910ba32b130472d94a 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -1,18 +1,17 @@
+#include <fcntl.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
-#include <gtk/gtk.h>
-#include <gtk/gtkx.h>
+#include <JavaScriptCore/JavaScript.h>
 #include <gdk/gdkkeysyms.h>
 #include <gio/gio.h>
+#include <gtk/gtk.h>
+#include <gtk/gtkx.h>
 #include <webkit2/webkit2.h>
-#include <JavaScriptCore/JavaScript.h>
-
 
 void client_destroy(GtkWidget *, gpointer);
 WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean,
@@ -29,8 +28,9 @@ gboolean crashed_web_view(WebKitWebView *, gpointer);
 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
                                                                                         WebKitPolicyDecisionType, gpointer);
 gboolean download_handle(WebKitDownload *, gchar *, gpointer);
-void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
-void downloadmanager_cancel(GtkToolButton *, gpointer);
+void download_click(GtkToolButton *, gpointer);
+void download_start(WebKitWebView *, WebKitDownload *, gpointer);
+void download_cancel(GtkMenuItem *, gpointer);
 gboolean downloadmanager_delete(GtkWidget *, gpointer);
 void downloadmanager_setup(void);
 gchar *ensure_uri_scheme(const gchar *);
@@ -59,8 +59,8 @@ GKeyFile *config;
 struct Client
 {
        gchar *external_handler_uri;
-       gchar *hover_uri;
        gchar *feed_html;
+       gchar *hover_uri;
        GtkWidget *location;
        GtkWidget *tabicon;
        GtkWidget *tablabel;
@@ -82,22 +82,35 @@ struct DownloadManager
        GtkWidget *win;
 } dm;
 
+struct Configuration
+{
+       WebKitCookieAcceptPolicy cookie_policy;
+       const gchar *accepted_language[2];
+       gboolean cooperative_alone;
+       gboolean cooperative_instances;
+       gboolean enable_console_to_stdout;
+       gboolean javascript_disabled;
+       gboolean spellcheck_disabled;
+       gchar *download_dir;
+       gchar *fifo_suffix;
+       gchar *history_file;
+       gchar *home_uri;
+       gchar *search_engine;
+       gchar *spellcheck_language;
+       gchar *user_agent;
+       gdouble global_zoom;
+       gint tab_width_chars;
+} cfg;
+
+struct DownloadItem
+{
+       GtkToolButton *tb;
+       WebKitDownload *download;
+};
 
-const gchar *accepted_language[2] = { NULL, NULL };
 gint clients = 0, downloads = 0;
-gboolean cooperative_alone = TRUE;
-gboolean cooperative_instances = TRUE;
 int cooperative_pipe_fp = 0;
-gboolean enable_console_to_stdout;
-gchar *fifo_suffix = "main";
-gdouble global_zoom;
-gchar *download_dir;
-gchar *history_file;
-gchar *home_uri;
 gchar *search_text;
-gint tab_width_chars;
-gchar *user_agent;
-gboolean javascript_disabled;
 
 void
 client_destroy(GtkWidget *widget, gpointer data)
@@ -128,7 +141,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
        gchar *f;
        GtkWidget *evbox, *tabbox;
 
-       if (uri != NULL && cooperative_instances && !cooperative_alone) {
+       if (uri != NULL && cfg.cooperative_instances && !cfg.cooperative_alone) {
                f = ensure_uri_scheme(uri);
                write(cooperative_pipe_fp, f, strlen(f));
                write(cooperative_pipe_fp, "\n", 1);
@@ -149,9 +162,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
        else
                c->web_view = webkit_web_view_new_with_related_view(related_wv);
 
-       webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
+       webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), cfg.global_zoom);
        WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
-       webkit_settings_set_enable_javascript(settings, !javascript_disabled);
+       webkit_settings_set_enable_javascript(settings, !cfg.javascript_disabled);
        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",
@@ -177,11 +190,11 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
        g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
                                                                         G_CALLBACK(crashed_web_view), c);
 
-       if (user_agent != NULL)
+       if (cfg.user_agent != NULL)
                g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
-                                                                "user-agent", user_agent, NULL);
+                                                                "user-agent", cfg.user_agent, NULL);
 
-       if (enable_console_to_stdout)
+       if (cfg.enable_console_to_stdout)
                webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
 
        webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
@@ -209,7 +222,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
 
        c->tablabel = gtk_label_new(__NAME__);
        gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
-       gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
+       gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg.tab_width_chars);
        gtk_widget_set_has_tooltip(c->tablabel, TRUE);
 
        /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
@@ -270,7 +283,7 @@ cooperation_setup(void)
        GIOChannel *towatch;
        gchar *fifofilename, *fifopath;
 
-       fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
+       fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", cfg.fifo_suffix);
        fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
        g_free(fifofilename);
 
@@ -288,7 +301,7 @@ cooperation_setup(void)
                        towatch = g_io_channel_new_file(fifopath, "r+", NULL);
                        g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
                } else {
-                       cooperative_alone = FALSE;
+                       cfg.cooperative_alone = FALSE;
                }
        }
 
@@ -440,8 +453,8 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
        if (t != NULL && strlen(t) > 0) {
                gtk_entry_set_text(GTK_ENTRY(c->location), t);
 
-               if (history_file != NULL) {
-                       fp = fopen(history_file, "a");
+               if (cfg.history_file != NULL) {
+                       fp = fopen(cfg.history_file, "a");
                        if (fp != NULL) {
                                fprintf(fp, "%s\n", t);
                                fclose(fp);
@@ -488,21 +501,25 @@ decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
 }
 
 void
-download_handle_finished(WebKitDownload *download, gpointer data)
+download_finished(WebKitDownload *download, gpointer data)
 {
+       if (strcmp(gtk_tool_button_get_icon_name(GTK_TOOL_BUTTON(data)),
+                                                "dialog-error") != 0)
+               gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(data), "emblem-downloads");
        downloads--;
 }
 
 void
-download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
-                                                                                       gpointer data)
+download_start(WebKitWebView *web_view, WebKitDownload *download,
+                                                        gpointer data)
 {
        g_signal_connect(G_OBJECT(download), "decide-destination",
                                                                         G_CALLBACK(download_handle), data);
 }
 
 gboolean
-download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
+download_handle(WebKitDownload *download, gchar *suggested_filename,
+                                                               gpointer data)
 {
        gchar *sug_clean, *path, *path2 = NULL, *uri;
        GtkToolItem *tb;
@@ -514,7 +531,7 @@ download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer da
                if (sug_clean[i] == G_DIR_SEPARATOR)
                        sug_clean[i] = '_';
 
-       path = g_build_filename(download_dir, sug_clean, NULL);
+       path = g_build_filename(cfg.download_dir, sug_clean, NULL);
        path2 = g_strdup(path);
        while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) {
                g_free(path2);
@@ -532,7 +549,7 @@ download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer da
                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_icon_name(GTK_TOOL_BUTTON(tb), "network-receive");
                gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
                gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
                gtk_widget_show_all(dm.win);
@@ -542,11 +559,19 @@ download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer da
 
                downloads++;
                g_signal_connect(G_OBJECT(download), "finished",
-                                                                                G_CALLBACK(download_handle_finished), NULL);
+                                                                                G_CALLBACK(download_finished), tb);
 
                g_object_ref(download);
-               g_signal_connect(G_OBJECT(tb), "clicked",
-                                                                                G_CALLBACK(downloadmanager_cancel), download);
+
+               struct DownloadItem *payload = malloc(sizeof(*payload));
+               payload->tb = (GtkToolButton *)tb;
+               payload->download = download;
+               g_signal_connect(G_OBJECT(tb), "clicked", G_CALLBACK(download_click),
+                                                                                payload);
+               g_signal_connect(G_OBJECT(tb), "failed", G_CALLBACK(download_cancel),
+                                                                                payload);
+               g_signal_connect(G_OBJECT(tb), "destroy_event", G_CALLBACK(g_free),
+                                                                                payload);
        }
 
        g_free(sug_clean);
@@ -558,14 +583,72 @@ download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer da
 }
 
 void
-downloadmanager_cancel(GtkToolButton *tb, gpointer data)
+download_cancel(GtkMenuItem *tb, gpointer data)
 {
-       WebKitDownload *download = WEBKIT_DOWNLOAD(data);
+       struct DownloadItem *payload = data;
+       gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(payload->tb), "dialog-error");
+       webkit_download_cancel(payload->download);
+}
 
-       webkit_download_cancel(download);
-       g_object_unref(download);
+void
+download_remove(GtkMenuItem *tb, gpointer data)
+{
+       struct DownloadItem *payload = data;
+       g_object_unref(payload->download);
+       gtk_widget_destroy(GTK_WIDGET(payload->tb));
+}
 
-       gtk_widget_destroy(GTK_WIDGET(tb));
+void
+download_copy_url(GtkMenuItem *tb, gpointer data)
+{
+       struct DownloadItem *payload = data;
+       WebKitURIRequest *req = webkit_download_get_request(payload->download);
+       const gchar *uri = webkit_uri_request_get_uri(req);
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri,
+                                                                                                strlen(uri));
+}
+
+void
+download_copy_path(GtkMenuItem *tb, gpointer data)
+{
+       struct DownloadItem *payload = data;
+       const gchar *path = webkit_download_get_destination(payload->download);
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), path + 7,
+                                                                                                strlen(path) - 7); // Offset by 7 to remove "file://"
+}
+
+void
+download_click(GtkToolButton *tb, gpointer data)
+{
+  GtkWidget *pmenu = gtk_menu_new();
+       GtkWidget *option;
+
+       if (strcmp(gtk_tool_button_get_icon_name(GTK_TOOL_BUTTON(tb)),
+                                                "network-receive") == 0) {
+               option = gtk_menu_item_new_with_label("Cancel download");
+               g_signal_connect(G_OBJECT(option), "activate",
+                                                                                G_CALLBACK(download_cancel), data);
+       } else {
+               option = gtk_menu_item_new_with_label("Remove download");
+               g_signal_connect(G_OBJECT(option), "activate",
+                                                                                G_CALLBACK(download_remove), data);
+       }
+       gtk_widget_show(option);
+       gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option);
+
+       option = gtk_menu_item_new_with_label("Copy download URL");
+       gtk_widget_show(option);
+       gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option);
+       g_signal_connect(G_OBJECT(option), "activate",
+                                                                        G_CALLBACK(download_copy_url), data);
+
+       option = gtk_menu_item_new_with_label("Copy local path");
+       gtk_widget_show(option);
+       gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option);
+       g_signal_connect(G_OBJECT(option), "activate",
+                                                                        G_CALLBACK(download_copy_path), data);
+
+       gtk_menu_popup_at_pointer(GTK_MENU(pmenu), NULL);
 }
 
 gboolean
@@ -631,42 +714,58 @@ ensure_uri_scheme(const gchar *t)
 void
 get_config(void)
 {
-       const gchar *e;
+       cfg.accepted_language[0] = NULL;
+       cfg.accepted_language[1] = NULL;
+       cfg.cooperative_alone = TRUE;
+       cfg.cooperative_alone = TRUE;
+       cfg.fifo_suffix = "main";
 
-       e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
+       const char *e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
        if (e != NULL)
-               fifo_suffix = g_strdup(e);
+               cfg.fifo_suffix = g_strdup(e);
 
        char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
-       download_dir = (xdg_down) ? xdg_down : "/var/tmp";
+       cfg.download_dir = (xdg_down) ? xdg_down : "/var/tmp";
 
        config = get_ini();
-       accepted_language[0] = g_key_file_get_string(config, "browser",
-                                                                                                                                                                                        "accepted_language", NULL);
-       history_file = g_key_file_get_string(config, "browser", "history_file", NULL);
-       home_uri = g_key_file_get_string(config, "browser", "homepage", NULL);
-       enable_console_to_stdout = g_key_file_get_boolean(config, "browser",
-                                                                                                                                                                                                               "console_to_stdout", NULL);
-       user_agent = g_key_file_get_string(config, "browser", "user_agent", NULL);
-       javascript_disabled = g_key_file_get_boolean(config, "browser",
-                                                                                                                                                                                        "javascript_disabled", NULL);
+       cfg.accepted_language[0] = g_key_file_get_string(config, "browser",
+                                                                                                                                                                                                        "accepted_language", NULL);
+       cfg.history_file = g_key_file_get_string(config, "browser", "history_file",
+                                                                                                                                                                        NULL);
+       cfg.home_uri = g_key_file_get_string(config, "browser", "homepage", NULL);
+       cfg.home_uri = (cfg.home_uri) ? cfg.home_uri : "about:blank";
+       cfg.enable_console_to_stdout = g_key_file_get_boolean(config, "browser",
+                                                                                                                                                                                                                               "console_to_stdout",
+                                                                                                                                                                                                                               NULL);
+       cfg.user_agent = g_key_file_get_string(config, "browser", "user_agent", NULL);
+       cfg.javascript_disabled = g_key_file_get_boolean(config, "browser",
+                                                                                                                                                                                                        "javascript_disabled", NULL);
        char *input_cookie_policy = g_key_file_get_string(config, "browser",
                                                                                                                                                                                                                "cookie_policy", NULL);
-       cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
+       cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
        if (input_cookie_policy) {
                if (strcmp(input_cookie_policy, "all") == 0) {
-                       cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
+                       cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
                } else if (strcmp(input_cookie_policy, "none") == 0) {
-                       cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
+                       cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
                }
        }
 
-       tab_width_chars = g_key_file_get_integer(config, "ui", "tab_width", NULL);
-       tab_width_chars = (tab_width_chars) ? tab_width_chars : 20;
-       global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL);
-       global_zoom = (global_zoom) ? global_zoom : 1.0;
-       search_engine   = g_key_file_get_string(config, "ui", "search_engine", NULL);
-       search_engine   = (search_engine) ? search_engine : "https://duckduckgo.com?q=";
+       cfg.tab_width_chars = g_key_file_get_integer(config, "ui", "tab_width", NULL);
+       cfg.tab_width_chars = (cfg.tab_width_chars) ? cfg.tab_width_chars : 20;
+       cfg.global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL);
+       cfg.global_zoom = (cfg.global_zoom) ? cfg.global_zoom : 1.0;
+       cfg.search_engine = g_key_file_get_string(config, "ui", "search_engine",
+                                                                                                                                                                               NULL);
+       cfg.search_engine = (cfg.search_engine) ? cfg.search_engine :
+               "https://duckduckgo.com?q=";
+       cfg.spellcheck_disabled = g_key_file_get_boolean(config, "ui",
+                                                                                                                                                                                                        "spellcheck_disabled", NULL);
+       cfg.spellcheck_language = g_key_file_get_string(config, "ui",
+                                                                                                                                                                                                                               "spellcheck_language",
+                                                                                                                                                                                                                               NULL);
+       cfg.spellcheck_language = (cfg.spellcheck_language) ? cfg.spellcheck_language
+               : "en_US";
 }
 
 void
@@ -802,21 +901,21 @@ init_default_web_context(void)
        webkit_web_context_set_process_model(wc,
                                                                                                                                                         WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
 
-       p = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
+       p = g_build_filename(g_get_user_data_dir(), __NAME__, "web_extensions",
                                                                                         NULL);
        webkit_web_context_set_web_extensions_directory(wc, p);
        g_free(p);
 
-       if (accepted_language[0] != NULL)
-               webkit_web_context_set_preferred_languages(wc, accepted_language);
+       if (cfg.accepted_language[0] != NULL)
+               webkit_web_context_set_preferred_languages(wc, cfg.accepted_language);
 
        g_signal_connect(G_OBJECT(wc), "download-started",
-                                                                        G_CALLBACK(download_handle_start), NULL);
+                                                                        G_CALLBACK(download_start), NULL);
 
        trust_user_certs(wc);
 
        cm = webkit_web_context_get_cookie_manager(wc);
-       webkit_cookie_manager_set_accept_policy(cm, cookie_policy);
+       webkit_cookie_manager_set_accept_policy(cm, cfg.cookie_policy);
 
        webkit_web_context_set_favicon_database_directory(wc, NULL);
        webkit_cookie_manager_set_persistent_storage(cm,
@@ -826,6 +925,11 @@ init_default_web_context(void)
                                                                                                                                                                                                                                                                "cookies.db",
                                                                                                                                                                                                                                                                NULL),
                                                                                                                                                                                         WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
+
+       const gchar * const languages[2] = {(const gchar *)cfg.spellcheck_language,
+               NULL};
+       webkit_web_context_set_spell_checking_languages(wc, languages);
+       webkit_web_context_set_spell_checking_enabled(wc, !cfg.spellcheck_disabled);
 }
 
 void
@@ -890,30 +994,14 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
                        WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
                        gboolean js = webkit_settings_get_enable_javascript(settings);
                        int key = ((GdkEventKey *)event)->keyval;
-                       if (def_key("close_tab", GDK_KEY_q) == key) {
-                               client_destroy(NULL, c);
-                               return TRUE;
-                       } else if (def_key("new_tab", GDK_KEY_w) == key) {
-                               f = ensure_uri_scheme(home_uri);
-                               client_new(f, NULL, TRUE, TRUE);
-                               g_free(f);
-                               return TRUE;
-                       } else if (def_key("reload", GDK_KEY_e) == key) {
-                               webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->web_view));
-                               return TRUE;
-                       } else if (def_key("download_manager", GDK_KEY_y) == key) {
+                       if (def_key("download_manager", GDK_KEY_y) == key) {
                                gtk_widget_show_all(dm.win);
                                return TRUE;
-                       } else if (def_key("web_search", GDK_KEY_d) == key) {
-                               gtk_widget_grab_focus(c->location);
-                               gtk_entry_set_text(GTK_ENTRY(c->location), "w/");
-                               gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
-                               return TRUE;
-                       } else if (def_key("search_forwards", GDK_KEY_s) == key) {
-                               search_init(c, 1);
+                       } else if (def_key("history_back", GDK_KEY_h) == key) {
+                               webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
                                return TRUE;
-                       } else if (def_key("search_backwards", GDK_KEY_r) == key) {
-                               search_init(c, -1);
+                       } else if (def_key("history_forwards", GDK_KEY_l) == key) {
+                               webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
                                return TRUE;
                        } else if (def_key("location", GDK_KEY_t) == key) {
                                gtk_widget_grab_focus(c->location);
@@ -922,25 +1010,30 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
                                gtk_entry_set_text(GTK_ENTRY(c->location), goal);
                                gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
                                return TRUE;
-                       } else if (def_key("previous_tab", GDK_KEY_u) == key) {
-                               gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
-                               return TRUE;
-                       } else if (def_key("next_tab", GDK_KEY_i) == key) {
-                               gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
+                       } else if (def_key("print", GDK_KEY_Print) == key) {
+                               webkit_print_operation_run_dialog(webkit_print_operation_new(WEBKIT_WEB_VIEW(c->web_view)),
+                                                                                                                                                                       GTK_WINDOW(gtk_widget_get_toplevel(mw.win)));
                                return TRUE;
-                       } else if (def_key("history_back", GDK_KEY_h) == key) {
-                               webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
+                       } else if (def_key("quit", GDK_KEY_g) == key) {
+                               search(c, 2);
+                               gtk_widget_grab_focus(c->web_view);
+                               gtk_entry_set_text(GTK_ENTRY(c->location),
+                                                                                                        webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
+                               webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
+                                                                                                                                                        "window.getSelection().removeAllRanges();"
+                                                                                                                                                        "document.activeElement.blur();",
+                                                                                                                                                        NULL, NULL, c);
                                return TRUE;
-                       } else if (def_key("history_forwards", GDK_KEY_l) == key) {
-                               webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
+                       } else if (def_key("reload", GDK_KEY_e) == key) {
+                               webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->web_view));
                                return TRUE;
-                       } else if (def_key("scroll_down", GDK_KEY_j) == key) {
+                       } else if (def_key("scroll_line_down", GDK_KEY_j) == key) {
                                for (int i = 0; i < 2; i++) {
                                        event->key.keyval = GDK_KEY_Down;
                                        gdk_event_put(event);
                                }
                                return TRUE;
-                       } else if (def_key("scroll_up", GDK_KEY_k) == key) {
+                       } else if (def_key("scroll_line_up", GDK_KEY_k) == key) {
                                event->key.keyval = GDK_KEY_Up;
                                gdk_event_put(event);
                                return TRUE;
@@ -952,19 +1045,61 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
                                event->key.keyval = GDK_KEY_Page_Up;
                                gdk_event_put(event);
                                return TRUE;
+                       } else if (def_key("search_forwards", GDK_KEY_s) == key) {
+                               search_init(c, 1);
+                               return TRUE;
+                       } else if (def_key("search_backwards", GDK_KEY_r) == key) {
+                               search_init(c, -1);
+                               return TRUE;
+                       } else if (def_key("tab_close", GDK_KEY_q) == key) {
+                               client_destroy(NULL, c);
+                               return TRUE;
+                       } else if (def_key("tab_switch_1", GDK_KEY_1) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 0);
+                               return TRUE;
+                       } else if (def_key("tab_switch_2", GDK_KEY_2) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 1);
+                               return TRUE;
+                       } else if (def_key("tab_switch_3", GDK_KEY_3) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 2);
+                               return TRUE;
+                       } else if (def_key("tab_switch_4", GDK_KEY_4) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 3);
+                               return TRUE;
+                       } else if (def_key("tab_switch_5", GDK_KEY_5) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 4);
+                               return TRUE;
+                       } else if (def_key("tab_switch_6", GDK_KEY_6) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 5);
+                               return TRUE;
+                       } else if (def_key("tab_switch_7", GDK_KEY_7) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 6);
+                               return TRUE;
+                       } else if (def_key("tab_switch_8", GDK_KEY_8) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 7);
+                               return TRUE;
+                       } else if (def_key("tab_switch_9", GDK_KEY_9) == key) {
+                               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 8);
+                               return TRUE;
+                       } else if (def_key("tab_previous", GDK_KEY_u) == key) {
+                               gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
+                               return TRUE;
+                       } else if (def_key("tab_new", GDK_KEY_w) == key) {
+                               f = ensure_uri_scheme(cfg.home_uri);
+                               client_new(f, NULL, TRUE, TRUE);
+                               g_free(f);
+                               return TRUE;
+                       } else if (def_key("tab_next", GDK_KEY_i) == key) {
+                               gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
+                               return TRUE;
                        } else if (def_key("toggle_js", GDK_KEY_o) == key) {
                                webkit_settings_set_enable_javascript(settings, (js) ? FALSE : TRUE);
                                webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), settings);
                                return TRUE;
-                       } else if (def_key("quit", GDK_KEY_g) == key) {
-                               search(c, 2);
-                               gtk_widget_grab_focus(c->web_view);
-                               gtk_entry_set_text(GTK_ENTRY(c->location),
-                                                                                                        webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
-                               webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
-                                                                                                                                                        "window.getSelection().removeAllRanges();"
-                                                                                                                                                        "document.activeElement.blur();",
-                                                                                                                                                        NULL, NULL, c);
+                       } else if (def_key("web_search", GDK_KEY_d) == key) {
+                               gtk_widget_grab_focus(c->location);
+                               gtk_entry_set_text(GTK_ENTRY(c->location), "w/");
+                               gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
                                return TRUE;
                        } else if (def_key("zoom_in", GDK_KEY_equal) == key) {
                                now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
@@ -976,7 +1111,7 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
                                return TRUE;
                        } else if (def_key("zoom_reset", GDK_KEY_0) == key) {
                                webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
-                                                                                                                                                        global_zoom);
+                                                                                                                                                        cfg.global_zoom);
                                return TRUE;
                        }
                }
@@ -1022,7 +1157,7 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
                                search_text = g_strdup(t + 2);
                                search(c, 0);
                        } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
-                               const char *engine = search_engine;
+                               const char *engine = cfg.search_engine;
                                int len = strlen(engine) + strlen(t) - 2;
                                char *f = (char *) malloc(len);
                                snprintf(f, len + 1, "%s%s", engine, t + 2);
@@ -1109,7 +1244,7 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
                        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;
+                       z = dx != 0 ? cfg.global_zoom : z;
                        webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
                        return TRUE;
                }
@@ -1191,7 +1326,7 @@ run_user_scripts(WebKitWebView *web_view)
        const gchar *entry = NULL;
        GDir *scriptdir = NULL;
 
-       base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts",
+       base = g_build_filename(g_get_user_data_dir(), __NAME__, "user-scripts",
                                                                                                        NULL);
        scriptdir = g_dir_open(base, 0, NULL);
        if (scriptdir != NULL) {
@@ -1241,12 +1376,12 @@ trust_user_certs(WebKitWebContext *wc)
        const gchar *basedir, *file, *absfile;
        GDir *dir;
 
-       basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
+       basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL);
        dir = g_dir_open(basedir, 0, NULL);
        if (dir != NULL) {
                file = g_dir_read_name(dir);
                while (file != NULL) {
-                       absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
+                       absfile = g_build_filename(g_get_user_data_dir(), __NAME__, "certs",
 
                                                                                                                                 file, NULL);
                        cert = g_tls_certificate_new_from_file(absfile, NULL);
@@ -1292,7 +1427,7 @@ main(int argc, char **argv)
        while ((opt = getopt(argc, argv, "C")) != -1) {
                switch (opt) {
                case 'C':
-                       cooperative_instances = FALSE;
+                       cfg.cooperative_instances = FALSE;
                        break;
                default:
                        fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
@@ -1300,23 +1435,23 @@ main(int argc, char **argv)
                }
        }
 
-       if (cooperative_instances)
+       if (cfg.cooperative_instances)
                cooperation_setup();
 
-       if (!cooperative_instances || cooperative_alone)
+       if (!cfg.cooperative_instances || cfg.cooperative_alone)
                init_default_web_context();
 
        downloadmanager_setup();
        mainwindow_setup();
 
        if (optind >= argc) {
-               client_new(home_uri, NULL, TRUE, TRUE);
+               client_new(cfg.home_uri, NULL, TRUE, TRUE);
        } else {
                for (i = optind; i < argc; i++)
                        client_new(argv[i], NULL, TRUE, TRUE);
        }
 
-       if (!cooperative_instances || cooperative_alone)
+       if (!cfg.cooperative_instances || cfg.cooperative_alone)
                gtk_main();
 
        exit(EXIT_SUCCESS);