]> git.armaanb.net Git - chorizo.git/blobdiff - src/browser.c
Add toggle images option
[chorizo.git] / src / browser.c
index 8650a61d10292780d9788b5c5211e770a718181a..4542eb7c3a9f2e1290b73924bc2da564154b0584 100644 (file)
@@ -38,15 +38,18 @@ GKeyFile *get_ini(void);
 GKeyFile *config;
 
 struct Client {
-    gchar *external_handler_uri;
-    gchar *feed_html;
-    gchar *hover_uri;
+    GtkWidget *imgbutton;
+    GtkWidget *jsbutton;
     GtkWidget *location;
     GtkWidget *tabicon;
     GtkWidget *tablabel;
     GtkWidget *vbox;
     GtkWidget *web_view;
+    WebKitSettings *settings;
     gboolean focus_new_tab;
+    gchar *external_handler_uri;
+    gchar *feed_html;
+    gchar *hover_uri;
 };
 
 struct Configuration {
@@ -55,7 +58,9 @@ struct Configuration {
     gboolean cooperative_alone;
     gboolean cooperative_instances;
     gboolean enable_console_to_stdout;
+    gboolean images_disabled;
     gboolean javascript_disabled;
+    gboolean private;
     gboolean spellcheck_disabled;
     gchar *download_dir;
     gchar *fifo_suffix;
@@ -78,6 +83,22 @@ gint clients = 0;
 int cooperative_pipe_fp = 0;
 gchar *search_text;
 
+void
+togglejs(GtkButton *jsbutton, gpointer data) {
+    struct Client *c = (struct Client *)data;
+    webkit_settings_set_enable_javascript(
+        c->settings, !webkit_settings_get_enable_javascript(c->settings));
+    webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), c->settings);
+}
+
+void
+toggleimg(GtkButton *imgbutton, gpointer data) {
+    struct Client *c = (struct Client *)data;
+    webkit_settings_set_auto_load_images(
+        c->settings, !webkit_settings_get_auto_load_images(c->settings));
+    webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), c->settings);
+}
+
 void
 client_destroy(GtkWidget *widget, gpointer data) {
     struct Client *c = (struct Client *)data;
@@ -126,11 +147,13 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
     else
         c->web_view = webkit_web_view_new_with_related_view(related_wv);
 
+    c->settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
+
     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, !cfg.javascript_disabled);
+    webkit_settings_set_enable_javascript(c->settings,
+                                          !cfg.javascript_disabled);
+    webkit_settings_set_auto_load_images(c->settings, !cfg.images_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",
@@ -157,16 +180,41 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
                      G_CALLBACK(crashed_web_view), c);
 
     if (cfg.user_agent != NULL) {
-        g_object_set(settings, "user-agent", cfg.user_agent, NULL);
+        g_object_set(c->settings, "user-agent", cfg.user_agent, NULL);
     }
 
     if (cfg.enable_console_to_stdout)
-        webkit_settings_set_enable_write_console_messages_to_stdout(settings,
+        webkit_settings_set_enable_write_console_messages_to_stdout(c->settings,
                                                                     TRUE);
 
-    webkit_settings_set_enable_developer_extras(settings, TRUE);
+    webkit_settings_set_enable_developer_extras(c->settings, TRUE);
+
+    GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+
+    c->jsbutton = gtk_toggle_button_new_with_label("JS");
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton),
+                                 !cfg.javascript_disabled);
+    g_signal_connect(G_OBJECT(c->jsbutton), "toggled", G_CALLBACK(togglejs), c);
+
+    c->imgbutton = gtk_toggle_button_new_with_label("IMG");
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->imgbutton),
+                                 !cfg.images_disabled);
+    g_signal_connect(G_OBJECT(c->imgbutton), "toggled", G_CALLBACK(toggleimg),
+                     c);
 
     c->location = gtk_entry_new();
+    gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 5);
+    gtk_box_pack_end(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 0);
+    gtk_box_pack_end(GTK_BOX(locbox), c->imgbutton, FALSE, FALSE, 0);
+
+    if (cfg.private) {
+        GtkWidget *privindicator = gtk_label_new("Private mode");
+        gtk_widget_set_tooltip_text(
+            privindicator, "You are in private mode. No history, caches, or "
+                           "cookies will be saved beyond this session.");
+        gtk_box_pack_end(GTK_BOX(locbox), privindicator, FALSE, FALSE, 5);
+    }
+
     g_signal_connect(G_OBJECT(c->location), "key-press-event",
                      G_CALLBACK(key_location), c);
     g_signal_connect(G_OBJECT(c->location), "icon-release",
@@ -177,10 +225,10 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
      * 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_PRIMARY, NULL);
+                                      GTK_ENTRY_ICON_SECONDARY, NULL);
 
     c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
-    gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
+    gtk_box_pack_start(GTK_BOX(c->vbox), locbox, FALSE, FALSE, 0);
     gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
     gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
 
@@ -248,7 +296,9 @@ cooperation_setup(void) {
     GIOChannel *towatch;
     gchar *fifofilename, *fifopath;
 
-    fifofilename = g_strdup_printf("%s-%s", __NAME__ ".fifo", cfg.fifo_suffix);
+    gchar *priv = (cfg.private) ? "-private" : "";
+    fifofilename =
+        g_strdup_printf("%s%s%s-%s", __NAME__, priv, ".fifo", cfg.fifo_suffix);
     fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
     g_free(fifofilename);
 
@@ -385,7 +435,7 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data) {
     if (t != NULL && strlen(t) > 0) {
         gtk_entry_set_text(GTK_ENTRY(c->location), t);
 
-        if (cfg.history_file != NULL) {
+        if (cfg.history_file != NULL && !cfg.private) {
             fp = fopen(cfg.history_file, "a");
             if (fp != NULL) {
                 fprintf(fp, "%s\n", t);
@@ -479,6 +529,8 @@ get_config(void) {
         g_key_file_get_string(config, "browser", "user_agent", NULL);
     cfg.javascript_disabled =
         g_key_file_get_boolean(config, "browser", "javascript_disabled", NULL);
+    cfg.images_disabled =
+        g_key_file_get_boolean(config, "browser", "images_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;
@@ -548,13 +600,13 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) {
         }
 
         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
-                                          GTK_ENTRY_ICON_PRIMARY,
+                                          GTK_ENTRY_ICON_SECONDARY,
                                           "application-rss+xml-symbolic");
         gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
                                        GTK_ENTRY_ICON_PRIMARY, TRUE);
     } else {
         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
-                                          GTK_ENTRY_ICON_PRIMARY, NULL);
+                                          GTK_ENTRY_ICON_SECONDARY, NULL);
     }
 
     webkit_javascript_result_unref(js_result);
@@ -626,7 +678,8 @@ init_default_web_context(void) {
     WebKitWebContext *wc;
     WebKitCookieManager *cm;
 
-    wc = webkit_web_context_get_default();
+    wc = (cfg.private) ? webkit_web_context_new_ephemeral()
+                       : webkit_web_context_get_default();
 
     p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock", NULL);
     webkit_web_context_set_sandbox_enabled(wc, TRUE);
@@ -653,13 +706,15 @@ init_default_web_context(void) {
     cm = webkit_web_context_get_cookie_manager(wc);
     webkit_cookie_manager_set_accept_policy(cm, cfg.cookie_policy);
 
-    webkit_web_context_set_favicon_database_directory(wc, NULL);
-    gchar *fname = g_build_filename("/", g_get_user_cache_dir(), __NAME__,
-                                    "cookies.db", NULL);
+    if (!cfg.private) {
+        webkit_web_context_set_favicon_database_directory(wc, NULL);
 
-    WebKitCookiePersistentStorage type =
-        WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
-    webkit_cookie_manager_set_persistent_storage(cm, fname, type);
+        gchar *fname = g_build_filename("/", g_get_user_cache_dir(), __NAME__,
+                                        "cookies.db", NULL);
+        WebKitCookiePersistentStorage type =
+            WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
+        webkit_cookie_manager_set_persistent_storage(cm, fname, type);
+    }
 
     const gchar *const languages[2] = {(const gchar *)cfg.spellcheck_language,
                                        NULL};
@@ -722,9 +777,6 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) {
 
     if (event->type == GDK_KEY_PRESS) {
         if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
-            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("download_manager", GDK_KEY_y) == key) {
                 downloadmanager_show();
@@ -842,11 +894,20 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) {
                 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);
+                gboolean on =
+                    webkit_settings_get_enable_javascript(c->settings);
+                webkit_settings_set_enable_javascript(c->settings, !on);
                 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view),
-                                             settings);
-                return TRUE;
+                                             c->settings);
+                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton),
+                                             !on);
+            } else if (def_key("toggle_img", -1) == key) {
+                gboolean on = webkit_settings_get_auto_load_images(c->settings);
+                webkit_settings_set_auto_load_images(c->settings, !on);
+                webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view),
+                                             c->settings);
+                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->imgbutton),
+                                             !on);
             } 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/");
@@ -991,7 +1052,11 @@ mainwindow_setup(void) {
     mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
     g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
-    gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
+
+    gchar *priv = (cfg.private) ? "-private" : "";
+    gchar *title = malloc(strlen(priv) + strlen(__NAME__));
+    sprintf(title, "%s%s", __NAME__, priv);
+    gtk_window_set_title(GTK_WINDOW(mw.win), title);
 
     mw.notebook = gtk_notebook_new();
     gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
@@ -1148,11 +1213,14 @@ int
 main(int argc, char **argv) {
     int opt, i;
 
-    while ((opt = getopt(argc, argv, "Cv")) != -1) {
+    while ((opt = getopt(argc, argv, "Cpv")) != -1) {
         switch (opt) {
         case 'C':
             cfg.cooperative_instances = FALSE;
             break;
+        case 'p':
+            cfg.private = true;
+            break;
         case 'v':
             printf("%s %s\n", __NAME__, VERSION);
             exit(0);