Major styles update.
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index c598404..6f71657 100644 (file)
--- a/surf.c
+++ b/surf.c
 #include <sys/file.h>
 #include <libgen.h>
 #include <stdarg.h>
+#include <regex.h>
 
 #include "arg.h"
 
 char *argv0;
 
 #define LENGTH(x)               (sizeof x / sizeof x[0])
-#define CLEANMASK(mask)                (mask & (MODKEY|GDK_SHIFT_MASK))
+#define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
 #define COOKIEJAR_TYPE          (cookiejar_get_type ())
 #define COOKIEJAR(obj)          (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
 
@@ -71,6 +72,12 @@ typedef struct {
 
 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
 
+typedef struct {
+       char *regex;
+       char *style;
+       regex_t re;
+} SiteStyle;
+
 static Display *dpy;
 static Atom atoms[AtomLast];
 static Client *clients = NULL;
@@ -82,6 +89,7 @@ static char togglestat[8];
 static char pagestat[3];
 static GTlsDatabase *tlsdb;
 static int policysel = 0;
+static char *stylefile = NULL;
 
 static void addaccelgroup(Client *c);
 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
@@ -127,6 +135,8 @@ static const char *getatom(Client *c, int a);
 static void gettogglestat(Client *c);
 static void getpagestat(Client *c);
 static char *geturi(Client *c);
+static gchar *getstyle(const char *uri);
+
 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
 
 static void inspector(Client *c, const Arg *arg);
@@ -148,8 +158,9 @@ static void navigate(Client *c, const Arg *arg);
 static Client *newclient(void);
 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
-static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
-static void popupactivate(GtkMenuItem *menu, Client *);
+static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
+               WebKitHitTestResult *target, gboolean keyboard, Client *c);
+static void menuactivate(GtkMenuItem *item, Client *c);
 static void print(Client *c, const Arg *arg);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
                gpointer d);
@@ -164,8 +175,7 @@ static void sigchld(int unused);
 static void source(Client *c, const Arg *arg);
 static void spawn(Client *c, const Arg *arg);
 static void stop(Client *c, const Arg *arg);
-static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
-               const char *title, Client *c);
+static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
 static void toggle(Client *c, const Arg *arg);
 static void togglecookiepolicy(Client *c, const Arg *arg);
 static void togglegeolocation(Client *c, const Arg *arg);
@@ -533,6 +543,19 @@ geturi(Client *c) {
        return uri;
 }
 
+static gchar *
+getstyle(const char *uri) {
+       int i;
+
+       for(i = 0; i < LENGTH(styles); i++) {
+               if(styles[i].regex && !regexec(&(styles[i].re), uri, 0,
+                                       NULL, 0)) {
+                       return g_strconcat("file://", styles[i].style, NULL);
+               }
+       }
+       return g_strdup("");
+}
+
 static gboolean
 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
        Arg arg;
@@ -629,6 +652,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
        WebKitWebFrame *frame;
        WebKitWebDataSource *src;
        WebKitNetworkRequest *request;
+       WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
        SoupMessage *msg;
        char *uri;
 
@@ -644,6 +668,11 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
                                        & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
                }
                setatom(c, AtomUri, uri);
+
+               if(stylefile == NULL && enablestyles) {
+                       g_object_set(G_OBJECT(set), "user-stylesheet-uri",
+                                       getstyle(uri), NULL);
+               }
                break;
        case WEBKIT_LOAD_FINISHED:
                c->progress = 100;
@@ -656,7 +685,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
 
 static void
 loaduri(Client *c, const Arg *arg) {
-       char *u, *rp;
+       char *u = NULL, *rp;
        const char *uri = (char *)arg->v;
        Arg a = { .b = FALSE };
        struct stat st;
@@ -683,9 +712,9 @@ loaduri(Client *c, const Arg *arg) {
                webkit_web_view_load_uri(c->view, u);
                c->progress = 0;
                c->title = copystr(&c->title, u);
-               g_free(u);
                updatetitle(c);
        }
+       g_free(u);
 }
 
 static void
@@ -702,7 +731,7 @@ newclient(void) {
        GdkGeometry hints = { 1, 1 };
        GdkScreen *screen;
        gdouble dpi;
-       char *uri, *ua;
+       char *uri = NULL, *ua;
 
        if(!(c = calloc(1, sizeof(Client))))
                die("Cannot malloc!\n");
@@ -749,7 +778,7 @@ newclient(void) {
        c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
 
        g_signal_connect(G_OBJECT(c->view),
-                       "title-changed",
+                       "notify::title",
                        G_CALLBACK(titlechange), c);
        g_signal_connect(G_OBJECT(c->view),
                        "hovering-over-link",
@@ -782,8 +811,8 @@ newclient(void) {
                        "button-release-event",
                        G_CALLBACK(buttonrelease), c);
        g_signal_connect(G_OBJECT(c->view),
-                       "populate-popup",
-                       G_CALLBACK(populatepopup), c);
+                       "context-menu",
+                       G_CALLBACK(contextmenu), c);
        g_signal_connect(G_OBJECT(c->view),
                        "resource-request-starting",
                        G_CALLBACK(beforerequest), c);
@@ -832,8 +861,10 @@ newclient(void) {
        if(!(ua = getenv("SURF_USERAGENT")))
                ua = useragent;
        g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
-       uri = g_strconcat("file://", stylefile, NULL);
-       g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
+       if (stylefile != NULL) {
+               uri = g_strconcat("file://", stylefile, NULL);
+               g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
+       }
        g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
                        NULL);
        g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
@@ -848,6 +879,8 @@ newclient(void) {
                        kioskmode ^ 1, NULL);
        g_object_set(G_OBJECT(settings), "default-font-size",
                        defaultfontsize, NULL);
+       g_object_set(G_OBJECT(settings), "resizable-text-areas",
+                       1, NULL);
 
        /*
         * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
@@ -886,7 +919,8 @@ newclient(void) {
                fullscreen(c, NULL);
        }
 
-       g_free(uri);
+       if(stylefile != NULL)
+               g_free(uri);
 
        setatom(c, AtomFind, "");
        setatom(c, AtomUri, "about:blank");
@@ -946,24 +980,27 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) {
        spawn(NULL, &a);
 }
 
-static void
-populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
-       GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
+static gboolean
+contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
+               gboolean keyboard, Client *c) {
+       GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
 
        for(GList *l = items; l; l = l->next) {
-               g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
+               g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
        }
 
        g_list_free(items);
+       return FALSE;
 }
 
 static void
-popupactivate(GtkMenuItem *menu, Client *c) {
+menuactivate(GtkMenuItem *item, Client *c) {
        /*
         * context-menu-action-2000     open link
         * context-menu-action-1        open link in window
         * context-menu-action-2        download linked file
         * context-menu-action-3        copy link location
+        * context-menu-action-7        copy image address
         * context-menu-action-13       reload
         * context-menu-action-10       back
         * context-menu-action-11       forward
@@ -971,10 +1008,10 @@ popupactivate(GtkMenuItem *menu, Client *c) {
         */
 
        GtkAction *a = NULL;
-       const char *name;
-       GtkClipboard *prisel;
+       const char *name, *uri;
+       GtkClipboard *prisel, *clpbrd;
 
-       a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
+       a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
        if(a == NULL)
                return;
 
@@ -982,6 +1019,12 @@ popupactivate(GtkMenuItem *menu, Client *c) {
        if(!g_strcmp0(name, "context-menu-action-3")) {
                prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
                gtk_clipboard_set_text(prisel, c->linkhover, -1);
+       } else if(!g_strcmp0(name, "context-menu-action-7")) {
+               prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+               clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
+               uri = gtk_clipboard_wait_for_text(clpbrd);
+               if(uri)
+                       gtk_clipboard_set_text(prisel, uri, -1);
        }
 }
 
@@ -1083,6 +1126,7 @@ setatom(Client *c, int a, const char *v) {
 
 static void
 setup(void) {
+       int i;
        char *proxy;
        char *new_proxy;
        SoupURI *puri;
@@ -1103,7 +1147,23 @@ setup(void) {
        /* dirs and files */
        cookiefile = buildpath(cookiefile);
        scriptfile = buildpath(scriptfile);
-       stylefile = buildpath(stylefile);
+       styledir = buildpath(styledir);
+       if(stylefile == NULL && enablestyles) {
+               for(i = 0; i < LENGTH(styles); i++) {
+                       if(regcomp(&(styles[i].re), styles[i].regex,
+                                               REG_EXTENDED)) {
+                               fprintf(stderr,
+                                       "Could not compile regex: %s\n",
+                                       styles[i].regex);
+                               styles[i].regex = NULL;
+                       }
+                       styles[i].style = buildpath(
+                                       g_strconcat(styledir,
+                                               styles[i].style, NULL));
+               }
+       } else {
+               stylefile = buildpath(stylefile);
+       }
 
        /* request handler */
        s = webkit_get_default_session();
@@ -1178,9 +1238,12 @@ stop(Client *c, const Arg *arg) {
 }
 
 static void
-titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
-       c->title = copystr(&c->title, t);
-       updatetitle(c);
+titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
+       const gchar *t = webkit_web_view_get_title(view);
+       if (t) {
+               c->title = copystr(&c->title, t);
+               updatetitle(c);
+       }
 }
 
 static void
@@ -1268,12 +1331,16 @@ togglescrollbars(Client *c, const Arg *arg) {
 
 static void
 togglestyle(Client *c, const Arg *arg) {
-       WebKitWebSettings *settings;
+       WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
        char *uri;
 
-       settings = webkit_web_view_get_settings(c->view);
        g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
-       uri = uri[0] ? g_strdup("") : g_strconcat("file://", stylefile, 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);
+       }
        g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
 
        updatetitle(c);
@@ -1304,7 +1371,7 @@ gettogglestat(Client *c){
        togglestat[p++] = value? 'V': 'v';
 
        g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
-       togglestat[p++] = uri[0] ? 'M': 'm';
+       togglestat[p++] = (uri && uri[0]) ? 'M': 'm';
 
        togglestat[p] = '\0';
 }
@@ -1437,6 +1504,12 @@ main(int argc, char *argv[]) {
        case 'K':
                kioskmode = 1;
                break;
+       case 'm':
+               enablestyles = 0;
+               break;
+       case 'M':
+               enablestyles = 1;
+               break;
        case 'n':
                enableinspector = 0;
                break;