Some cleanup in style.
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index a229994..36f243e 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -95,7 +95,7 @@ static SoupCache *diskcache = NULL;
 static void addaccelgroup(Client *c);
 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
                WebKitWebResource *r, WebKitNetworkRequest *req,
-               WebKitNetworkResponse *resp, gpointer d);
+               WebKitNetworkResponse *resp, Client *c);
 static char *buildpath(const char *path);
 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
                GList *gl);
@@ -138,6 +138,8 @@ static void getpagestat(Client *c);
 static char *geturi(Client *c);
 static gchar *getstyle(const char *uri);
 
+static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
+
 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
 
 static void inspector(Client *c, const Arg *arg);
@@ -209,11 +211,30 @@ addaccelgroup(Client *c) {
 static void
 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
                WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
-               gpointer d) {
+               Client *c) {
        const gchar *uri = webkit_network_request_get_uri(req);
+       int i, isascii = 1;
 
        if(g_str_has_suffix(uri, "/favicon.ico"))
                webkit_network_request_set_uri(req, "about:blank");
+
+       if(!g_str_has_prefix(uri, "http://") \
+                       && !g_str_has_prefix(uri, "https://") \
+                       && !g_str_has_prefix(uri, "about:") \
+                       && !g_str_has_prefix(uri, "file://") \
+                       && !g_str_has_prefix(uri, "data:") \
+                       && !g_str_has_prefix(uri, "blob:") \
+                       && strlen(uri) > 0) {
+
+               for(i = 0; i < strlen(uri); i++) {
+                       if(!g_ascii_isprint(uri[i])) {
+                               isascii = 0;
+                               break;
+                       }
+               }
+               if(isascii)
+                       handleplumb(c, w, uri);
+       }
 }
 
 static char *
@@ -271,7 +292,7 @@ buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
 
 static void
 cleanup(void) {
-       if (diskcache) {
+       if(diskcache) {
                soup_cache_flush(diskcache);
                soup_cache_dump(diskcache);
        }
@@ -552,6 +573,9 @@ static gchar *
 getstyle(const char *uri) {
        int i;
 
+       if(stylefile != NULL)
+               return g_strconcat("file://", stylefile, NULL);
+
        for(i = 0; i < LENGTH(styles); i++) {
                if(styles[i].regex && !regexec(&(styles[i].re), uri, 0,
                                        NULL, 0)) {
@@ -561,6 +585,15 @@ getstyle(const char *uri) {
        return g_strdup("");
 }
 
+static void
+handleplumb(Client *c, WebKitWebView *w, const gchar *uri) {
+       Arg arg;
+
+       webkit_web_view_stop_loading(w);
+       arg = (Arg)PLUMB((char *)uri);
+       spawn(c, &arg);
+}
+
 static gboolean
 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
        Arg arg;
@@ -674,7 +707,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
                }
                setatom(c, AtomUri, uri);
 
-               if(stylefile == NULL && enablestyles) {
+               if(enablestyles) {
                        g_object_set(G_OBJECT(set), "user-stylesheet-uri",
                                        getstyle(uri), NULL);
                }
@@ -682,7 +715,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
        case WEBKIT_LOAD_FINISHED:
                c->progress = 100;
                updatetitle(c);
-               if (diskcache) {
+               if(diskcache) {
                        soup_cache_flush(diskcache);
                        soup_cache_dump(diskcache);
                }
@@ -740,7 +773,7 @@ newclient(void) {
        GdkGeometry hints = { 1, 1 };
        GdkScreen *screen;
        gdouble dpi;
-       char *uri = NULL, *ua;
+       char *ua;
 
        if(!(c = calloc(1, sizeof(Client))))
                die("Cannot malloc!\n");
@@ -870,9 +903,9 @@ newclient(void) {
        if(!(ua = getenv("SURF_USERAGENT")))
                ua = useragent;
        g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
-       if (stylefile != NULL) {
-               uri = g_strconcat("file://", stylefile, NULL);
-               g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
+       if (enablestyles) {
+               g_object_set(G_OBJECT(settings), "user-stylesheet-uri",
+                                        getstyle("about:blank"), NULL);
        }
        g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
                        NULL);
@@ -928,9 +961,6 @@ newclient(void) {
                fullscreen(c, NULL);
        }
 
-       if(stylefile != NULL)
-               g_free(uri);
-
        setatom(c, AtomFind, "");
        setatom(c, AtomUri, "about:blank");
        if(hidebackground)
@@ -955,7 +985,7 @@ newclient(void) {
 static void
 newwindow(Client *c, const Arg *arg, gboolean noembed) {
        guint i = 0;
-       const char *cmd[16], *uri;
+       const char *cmd[18], *uri;
        const Arg a = { .v = (void *)cmd };
        char tmp[64];
 
@@ -1162,7 +1192,7 @@ setup(void) {
        scriptfile = buildpath(scriptfile);
        cachefolder = buildpath(cachefolder);
        styledir = buildpath(styledir);
-       if(stylefile == NULL && enablestyles) {
+       if(stylefile == NULL) {
                for(i = 0; i < LENGTH(styles); i++) {
                        if(regcomp(&(styles[i].re), styles[i].regex,
                                                REG_EXTENDED)) {
@@ -1356,13 +1386,8 @@ togglestyle(Client *c, const Arg *arg) {
        WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
        char *uri;
 
-       g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
-       if(stylefile == NULL && enablestyles) {
-               uri = (uri && uri[0])? g_strdup("") : getstyle(geturi(c));
-       } else {
-               uri = uri[0]? g_strdup("") : g_strconcat("file://",
-                               stylefile, NULL);
-       }
+       enablestyles = !enablestyles;
+       uri = enablestyles ? getstyle(geturi(c)) : g_strdup("");
        g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
 
        updatetitle(c);
@@ -1371,7 +1396,6 @@ togglestyle(Client *c, const Arg *arg) {
 static void
 gettogglestat(Client *c){
        gboolean value;
-       char *uri;
        int p = 0;
        WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
 
@@ -1394,8 +1418,7 @@ gettogglestat(Client *c){
        g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
        togglestat[p++] = value? 'V': 'v';
 
-       g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
-       togglestat[p++] = (uri && uri[0]) ? 'M': 'm';
+       togglestat[p++] = enablestyles ? 'M': 'm';
 
        togglestat[p] = '\0';
 }