X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=a229994cd3be5ad792170e7de32bfd42dfd38dde;hb=117d4848338315d15e1ab93cd551889fc6964a2b;hp=654353dd58886a740fbe5e6e81dfd8c46f394780;hpb=a8267f7b95f0dd7e830ed89511f49ca515e4f594;p=surf.git diff --git a/surf.c b/surf.c index 654353d..a229994 100644 --- a/surf.c +++ b/surf.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "arg.h" @@ -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; @@ -78,10 +85,12 @@ static GdkNativeWindow embed = 0; static gboolean showxid = FALSE; static char winid[64]; static gboolean usingproxy = 0; -static char togglestat[8]; +static char togglestat[9]; static char pagestat[3]; static GTlsDatabase *tlsdb; static int policysel = 0; +static char *stylefile = NULL; +static SoupCache *diskcache = NULL; static void addaccelgroup(Client *c); static void beforerequest(WebKitWebView *w, WebKitWebFrame *f, @@ -127,6 +136,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); @@ -260,6 +271,10 @@ buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) { static void cleanup(void) { + if (diskcache) { + soup_cache_flush(diskcache); + soup_cache_dump(diskcache); + } while(clients) destroyclient(clients); g_free(cookiefile); @@ -533,6 +548,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 +657,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,10 +673,19 @@ 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; updatetitle(c); + if (diskcache) { + soup_cache_flush(diskcache); + soup_cache_dump(diskcache); + } break; default: break; @@ -702,7 +740,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"); @@ -832,8 +870,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 +888,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 +928,8 @@ newclient(void) { fullscreen(c, NULL); } - g_free(uri); + if(stylefile != NULL) + g_free(uri); setatom(c, AtomFind, ""); setatom(c, AtomUri, "about:blank"); @@ -923,9 +966,11 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) { cmd[i++] = "-b"; if(embed && !noembed) { cmd[i++] = "-e"; - snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed); + snprintf(tmp, LENGTH(tmp), "%u", (int)embed); cmd[i++] = tmp; } + if(!allowgeolocation) + cmd[i++] = "-g"; if(!loadimages) cmd[i++] = "-i"; if(kioskmode) @@ -936,6 +981,8 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) { cmd[i++] = "-s"; if(showxid) cmd[i++] = "-x"; + if(enablediskcache) + cmd[i++] = "-D"; cmd[i++] = "-c"; cmd[i++] = cookiefile; cmd[i++] = "--"; @@ -966,6 +1013,7 @@ menuactivate(GtkMenuItem *item, Client *c) { * 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 @@ -973,8 +1021,8 @@ menuactivate(GtkMenuItem *item, 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(item)); if(a == NULL) @@ -984,6 +1032,12 @@ menuactivate(GtkMenuItem *item, 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); } } @@ -1085,6 +1139,7 @@ setatom(Client *c, int a, const char *v) { static void setup(void) { + int i; char *proxy; char *new_proxy; SoupURI *puri; @@ -1105,7 +1160,24 @@ setup(void) { /* dirs and files */ cookiefile = buildpath(cookiefile); scriptfile = buildpath(scriptfile); - stylefile = buildpath(stylefile); + cachefolder = buildpath(cachefolder); + 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(); @@ -1115,6 +1187,14 @@ setup(void) { SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE, cookiepolicy_get()))); + /* disk cache */ + if(enablediskcache) { + diskcache = soup_cache_new(cachefolder, SOUP_CACHE_SINGLE_USER); + soup_cache_set_max_size(diskcache, diskcachebytes); + soup_cache_load(diskcache); + soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache)); + } + /* ssl */ tlsdb = g_tls_file_database_new(cafile, &error); @@ -1273,12 +1353,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); @@ -1299,6 +1383,8 @@ gettogglestat(Client *c){ togglestat[p++] = allowgeolocation? 'G': 'g'; + togglestat[p++] = enablediskcache? 'D': 'd'; + g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL); togglestat[p++] = value? 'I': 'i'; @@ -1309,7 +1395,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'; } @@ -1415,6 +1501,12 @@ main(int argc, char *argv[]) { case 'c': cookiefile = EARGF(usage()); break; + case 'd': + enablediskcache = 0; + break; + case 'D': + enablediskcache = 1; + break; case 'e': embed = strtol(EARGF(usage()), NULL, 0); break; @@ -1442,6 +1534,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;