#include <sys/file.h>
#include <libgen.h>
#include <stdarg.h>
+#include <regex.h>
#include "arg.h"
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;
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,
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);
static void gettogglestat(Client *c);
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);
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 *
static void
cleanup(void) {
+ if (diskcache) {
+ soup_cache_flush(diskcache);
+ soup_cache_dump(diskcache);
+ }
while(clients)
destroyclient(clients);
g_free(cookiefile);
return uri;
}
+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)) {
+ return g_strconcat("file://", styles[i].style, NULL);
+ }
+ }
+ 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;
WebKitWebFrame *frame;
WebKitWebDataSource *src;
WebKitNetworkRequest *request;
+ WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
SoupMessage *msg;
char *uri;
& SOUP_MESSAGE_CERTIFICATE_TRUSTED);
}
setatom(c, AtomUri, uri);
+
+ if(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;
GdkGeometry hints = { 1, 1 };
GdkScreen *screen;
gdouble dpi;
- char *uri, *ua;
+ char *ua;
if(!(c = calloc(1, sizeof(Client))))
die("Cannot malloc!\n");
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 (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);
g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
fullscreen(c, NULL);
}
- g_free(uri);
-
setatom(c, AtomFind, "");
setatom(c, AtomUri, "about:blank");
if(hidebackground)
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];
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)
cmd[i++] = "-s";
if(showxid)
cmd[i++] = "-x";
+ if(enablediskcache)
+ cmd[i++] = "-D";
cmd[i++] = "-c";
cmd[i++] = cookiefile;
cmd[i++] = "--";
* 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
*/
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)
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);
}
}
static void
setup(void) {
+ int i;
char *proxy;
char *new_proxy;
SoupURI *puri;
/* dirs and files */
cookiefile = buildpath(cookiefile);
scriptfile = buildpath(scriptfile);
- stylefile = buildpath(stylefile);
+ cachefolder = buildpath(cachefolder);
+ styledir = buildpath(styledir);
+ if(stylefile == NULL) {
+ 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();
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);
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);
+ enablestyles = !enablestyles;
+ uri = enablestyles ? getstyle(geturi(c)) : g_strdup("");
g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
updatetitle(c);
static void
gettogglestat(Client *c){
gboolean value;
- char *uri;
int p = 0;
WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
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';
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[0] ? 'M': 'm';
+ togglestat[p++] = enablestyles ? 'M': 'm';
togglestat[p] = '\0';
}
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;
case 'K':
kioskmode = 1;
break;
+ case 'm':
+ enablestyles = 0;
+ break;
+ case 'M':
+ enablestyles = 1;
+ break;
case 'n':
enableinspector = 0;
break;