]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
Release v21.01
[chorizo.git] / browser.c
index 2390b1cba0f4cdc862e72c81478e09bc4bb0ed5f..ebab7fb82e66a265ef6fcc3e294ad4beb1d2a5cc 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -38,12 +38,15 @@ void grab_environment_configuration(void);
 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
+void init_default_web_context(void);
 gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
 gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
 void mainwindow_setup(void);
+void mainwindow_title(gint);
+void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer);
 gboolean quit_if_nothing_active(void);
 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
 void run_user_scripts(WebKitWebView *);
@@ -90,7 +93,6 @@ gchar *fifo_suffix = "main";
 gdouble global_zoom = 1.0;
 gchar *history_file = NULL;
 gchar *home_uri = "about:blank";
-gboolean initial_wc_setup_done = FALSE;
 gchar *search_text = NULL;
 GtkPositionType tab_pos = GTK_POS_TOP;
 gint tab_width_chars = 20;
@@ -123,7 +125,6 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
            gboolean focus_tab)
 {
     struct Client *c;
-    WebKitWebContext *wc;
     gchar *f;
     GtkWidget *evbox, *tabbox;
 
@@ -149,7 +150,6 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
         c->web_view = webkit_web_view_new();
     else
         c->web_view = webkit_web_view_new_with_related_view(related_wv);
-    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",
@@ -177,21 +177,6 @@ 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 (!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);
-
-        webkit_web_context_set_favicon_database_directory(wc, NULL);
-
-        initial_wc_setup_done = TRUE;
-    }
-
     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);
@@ -218,6 +203,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
     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), c->web_view, TRUE, TRUE, 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);
 
@@ -242,6 +228,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
     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), "lariza-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). */
@@ -444,6 +433,7 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
 
     gtk_label_set_text(GTK_LABEL(c->tablabel), t);
     gtk_widget_set_tooltip_text(c->tablabel, t);
+    mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
 }
 
 void
@@ -643,7 +633,8 @@ ensure_uri_scheme(const gchar *t)
         !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, "data:") &&
+        !g_str_has_prefix(f, "webkit:"))
     {
         g_free(f);
         fabs = realpath(t, NULL);
@@ -777,28 +768,23 @@ hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers
                gpointer data)
 {
     struct Client *c = (struct Client *)data;
+    const char *to_show;
 
-    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)));
+    g_free(c->hover_uri);
 
-            if (c->hover_uri != NULL)
-                g_free(c->hover_uri);
-            c->hover_uri = NULL;
-        }
+    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;
     }
+
+    if (!gtk_widget_is_focus(c->location))
+        gtk_entry_set_text(GTK_ENTRY(c->location), to_show);
 }
 
 void
@@ -844,6 +830,38 @@ icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
     }
 }
 
+void
+init_default_web_context(void)
+{
+    gchar *p;
+    WebKitWebContext *wc;
+
+    wc = 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);
+    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_config_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);
+
+    g_signal_connect(G_OBJECT(wc), "download-started",
+                     G_CALLBACK(download_handle_start), NULL);
+
+    trust_user_certs(wc);
+
+    webkit_web_context_set_favicon_database_directory(wc, NULL);
+}
+
 gboolean
 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
@@ -1082,6 +1100,30 @@ mainwindow_setup(void)
     gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
     gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
     gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
+    g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
+                     G_CALLBACK(notebook_switch_page), NULL);
+}
+
+void
+mainwindow_title(gint idx)
+{
+    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), "lariza-tab-label");
+    text = gtk_label_get_text(GTK_LABEL(tablabel));
+    gtk_window_set_title(GTK_WINDOW(mw.win), text);
+}
+
+void
+notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
+{
+    mainwindow_title(idx);
 }
 
 gboolean
@@ -1224,13 +1266,9 @@ trust_user_certs(WebKitWebContext *wc)
 int
 main(int argc, char **argv)
 {
-    gchar *c;
     int opt, i;
 
     gtk_init(&argc, &argv);
-    webkit_web_context_set_process_model(webkit_web_context_get_default(),
-        WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
-
     grab_environment_configuration();
 
     while ((opt = getopt(argc, argv, "C")) != -1)
@@ -1248,18 +1286,12 @@ main(int argc, char **argv)
 
     if (cooperative_instances)
         cooperation_setup();
-    downloadmanager_setup();
-
-    mainwindow_setup();
 
     if (!cooperative_instances || cooperative_alone)
-    {
-        c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
-                             NULL);
-        webkit_web_context_set_web_extensions_directory(
-            webkit_web_context_get_default(), c
-        );
-    }
+        init_default_web_context();
+
+    downloadmanager_setup();
+    mainwindow_setup();
 
     if (optind >= argc)
         client_new(home_uri, NULL, TRUE, TRUE);
@@ -1271,5 +1303,6 @@ main(int argc, char **argv)
 
     if (!cooperative_instances || cooperative_alone)
         gtk_main();
+
     exit(EXIT_SUCCESS);
 }