]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
Add mute keybinding
[chorizo.git] / browser.c
index 9486b9d4ad0d482f040dfcd909d5a83af39ede2b..37ffdb5ede8dd9cd3a54655ea7f15a11d2c58c53 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -28,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 *);
@@ -89,16 +90,24 @@ struct Configuration
        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;
+};
+
 gint clients = 0, downloads = 0;
 int cooperative_pipe_fp = 0;
 gchar *search_text;
@@ -423,7 +432,13 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
        t = t == NULL ? u : t;
        t = t[0] == 0 ? u : t;
 
-       gtk_label_set_text(GTK_LABEL(c->tablabel), t);
+       gchar *name = malloc(strlen(t) + 4);
+       gchar *muted = (webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view)))
+               ? "[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)));
 }
@@ -492,21 +507,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;
@@ -536,7 +555,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);
@@ -546,11 +565,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);
@@ -562,14 +589,72 @@ download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer da
 }
 
 void
-downloadmanager_cancel(GtkToolButton *tb, gpointer data)
+download_cancel(GtkMenuItem *tb, gpointer data)
+{
+       struct DownloadItem *payload = data;
+       gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(payload->tb), "dialog-error");
+       webkit_download_cancel(payload->download);
+}
+
+void
+download_remove(GtkMenuItem *tb, gpointer data)
+{
+       struct DownloadItem *payload = data;
+       g_object_unref(payload->download);
+       gtk_widget_destroy(GTK_WIDGET(payload->tb));
+}
+
+void
+download_copy_url(GtkMenuItem *tb, gpointer data)
 {
-       WebKitDownload *download = WEBKIT_DOWNLOAD(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));
+}
 
-       webkit_download_cancel(download);
-       g_object_unref(download);
+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://"
+}
 
-       gtk_widget_destroy(GTK_WIDGET(tb));
+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
@@ -650,15 +735,17 @@ get_config(void)
 
        config = get_ini();
        cfg.accepted_language[0] = g_key_file_get_string(config, "browser",
-                                                                                                                                                                                        "accepted_language", NULL);
+                                                                                                                                                                                                        "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);
+                                                                                                                                                                                                                               "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);
+                                                                                                                                                                                                        "javascript_disabled", NULL);
        char *input_cookie_policy = g_key_file_get_string(config, "browser",
                                                                                                                                                                                                                "cookie_policy", NULL);
        cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
@@ -674,9 +761,17 @@ get_config(void)
        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 = 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
@@ -812,7 +907,7 @@ 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);
@@ -821,7 +916,7 @@ init_default_web_context(void)
                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);
 
@@ -836,6 +931,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
@@ -900,30 +1000,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(cfg.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);
@@ -932,25 +1016,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;
@@ -962,19 +1051,67 @@ 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_mute", GDK_KEY_m) == key) {
+                               gboolean muted =
+                                       webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
+                               webkit_web_view_set_is_muted(WEBKIT_WEB_VIEW(c->web_view), !muted);
+                               changed_title(G_OBJECT(c->web_view), NULL, c);
+                               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));
@@ -1201,7 +1338,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) {
@@ -1251,12 +1388,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);