adji

Adji's a Decisive and Joyful Internet browser
git clone https://noxz.tech/git/adji.git
Log | Files | Tags | LICENSE

commit: 1e086ebb73bb388f1522fc538bb8a37dd13e4531
parent: 530a167cb57cf089f2b235dec08bbd817c6bf7df
author: Chris Noxz <chris@noxz.tech>
date:   Thu, 23 Mar 2023 11:18:36 +0100
add state indicators to state file

This commit adds the ability to save more state indicators to the state file,
such as the current notebook page index and whether or not JavaScript is
enabled in a specific web view. This means that the state is also saved when a
new notebook page is selected or opened and when JavaScript is toggled. The
state is also saved in a new format, state_indicators:uri, which means that
adji is no longer compatible with older state files.

The plan was also to include `tls-error-policy` as a state indicator, but this
would require rebuilding how the `website-data-manager` is used. At the moment,
it is globally used, meaning that all web views share the same
`website-data-manager`, so saving the state individually would be pointless.

However, having individual settings for the `tls-error-policy` would be nice,
as one may not want to ignore tls errors for all web views.
Mbrowser.c39+++++++++++++++-----
Mbrowser.h6+++
2 files changed, 36 insertions(+), 9 deletions(-)
diff --git a/browser.c b/browser.c
@@ -684,9 +684,12 @@ load_configuration(void)
 void
 load_state(void)
 {
+	struct Client          *c;
 	char                   *u = NULL;           /* line/uri holder */
 	size_t                  l = URI_MAX;        /* length */
 	FILE                   *fp = NULL;          /* file pointer */
+	int                     x,                  /* state indicators */
+	                        i = 0;              /* current page index */
 
 	/* only load states for ipc host */
 	if (CFG_S(StateFile) == NULL || gl.ipc != IPC_HOST)
@@ -701,12 +704,17 @@ load_state(void)
 		die(__NAME__ ": fatal: malloc failed\n");
 
 	/* load uris into new clients */
-	while (getline(&u, &l, fp) >= 0)
-		client_create(u, NULL, TRUE, TRUE);
+	while (getline(&u, &l, fp) >= 0) {
+		if (sscanf(u, "%d:%s", &x, u) != 2)
+			continue;   /* incorrect format, so skip it */
+		c = client_create(u, NULL, TRUE, TRUE);
+		set_javascript_policy(c, (x & STATE_JAVASCRIPT) != 0);
+		i = x & STATE_CURRENT ? gl.clients - 1 : i;
+	}
 
-	/* select first tab if any */
+	/* select current tab if any */
 	if (gl.clients > 0)
-		gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.nb), 0);
+		gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.nb), i);
 
 	/* enable state save */
 	gl.state_lock = FALSE;
@@ -783,6 +791,7 @@ main_window_setup(void)
 	/* connect signal callbacks */
 	CB(mw.win,              "destroy",          cb_quit, NULL);
 	CB(mw.nb,               "switch-page",      cb_notebook_switch_page, NULL);
+	CBA(mw.nb,              "switch-page",      cb_notebook_modified, NULL);
 	CB(mw.nb,               "page-added",       cb_notebook_modified, NULL);
 	CB(mw.nb,               "page-removed",     cb_notebook_modified, NULL);
 	CB(mw.nb,               "page-reordered",   cb_notebook_modified, NULL);
@@ -953,9 +962,12 @@ void
 save_state(void)
 {
 	GList                  *c;                  /* child (notebook page) */
-	int                     i,
+	int                     x,                  /* state indicators */
+	                        i,
 	                        l;                  /* length (number of tabs) */
 	FILE                   *fp;
+	WebKitWebView          *wv;
+	WebKitSettings         *s;
 
 	/* only save state for ipc host, when state isn't locked for loading */
 	if (CFG_S(StateFile) == NULL || gl.ipc != IPC_HOST || gl.state_lock)
@@ -971,8 +983,16 @@ save_state(void)
 		c = gtk_container_get_children(
 		    GTK_CONTAINER(gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.nb), i))
 		);
-		if (strcmp(gtk_widget_get_name((c->data)), "WebKitWebView") == 0)
-			fprintf(fp, "%s\n", WV_GET_URI(c->data));
+		if (strcmp(gtk_widget_get_name((c->data)), "WebKitWebView") != 0)
+			continue;
+		wv = WEBKIT_WEB_VIEW(c->data);
+		s = webkit_web_view_get_settings(wv);
+		x = 0;
+		if (gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.nb)) == i)
+			x |= STATE_CURRENT;
+		if (webkit_settings_get_enable_javascript_markup(s))
+			x |= STATE_JAVASCRIPT;
+		fprintf(fp, "%d:%s\n", x, WV_GET_URI(wv));
 	}
 
 	fclose(fp);
@@ -1104,7 +1124,7 @@ set_javascript_policy(struct Client            *c,
 	webkit_settings_set_enable_javascript_markup(
 	    c->settings,
 	    policy == JSP_TOGGLE
-	        ? !(webkit_settings_get_enable_javascript_markup(c->settings))
+	        ? !webkit_settings_get_enable_javascript_markup(c->settings)
 	        : policy
 	);
 	webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->wv));
@@ -1116,6 +1136,7 @@ set_javascript_policy(struct Client            *c,
 		gtk_entry_set_icon_from_icon_name(
 		    GTK_ENTRY(c->entry), GTK_ENTRY_ICON_SECONDARY, ICON_JS_OFF
 		);
+	save_state();
 }
 
 void
@@ -1173,7 +1194,7 @@ toggle_tls_error_policy(struct Client *c)
 	        webkit_web_view_get_website_data_manager(WEBKIT_WEB_VIEW(c->wv))
 	    )
 	);
-	webkit_web_view_reload(WEBKIT_WEB_VIEW(c->wv));
+	webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->wv));
 }
 
 gchar *
diff --git a/browser.h b/browser.h
@@ -47,6 +47,10 @@
         {                                                                      \
             g_signal_connect(G_OBJECT(C), (E), G_CALLBACK(F), (D));            \
         }
+#define CBA(C, E, F, D)                                                        \
+        {                                                                      \
+            g_signal_connect_after(G_OBJECT(C), (E), G_CALLBACK(F), (D));      \
+        }
 #define CFG_B(X)            cfg[(X)].v.b
 #define CFG_F(X)            cfg[(X)].v.f
 #define CFG_I(X)            cfg[(X)].v.i
@@ -79,6 +83,8 @@
                             ">  Some error occurred validating the certificate"\
                             ".<br>"
 #define XDG_OPEN            "xdg-open"
+#define STATE_CURRENT       1 << 0
+#define STATE_JAVASCRIPT    1 << 1
 
 enum javascript_policy {
 	JSP_DISABLE             = 0,