Style cleanup.
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 36f243e..0fae80b 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -24,6 +24,8 @@
 #include <libgen.h>
 #include <stdarg.h>
 #include <regex.h>
+#include <pwd.h>
+#include <string.h>
 
 #include "arg.h"
 
@@ -35,6 +37,15 @@ char *argv0;
 #define COOKIEJAR(obj)          (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
 
 enum { AtomFind, AtomGo, AtomUri, AtomLast };
+enum {
+       ClkDoc   = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
+       ClkLink  = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
+       ClkImg   = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
+       ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
+       ClkSel   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
+       ClkEdit  = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
+       ClkAny   = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
+};
 
 typedef union Arg Arg;
 union Arg {
@@ -61,6 +72,14 @@ typedef struct {
        const Arg arg;
 } Key;
 
+typedef struct {
+       unsigned int click;
+       unsigned int mask;
+       guint button;
+       void (*func)(Client *c, const Arg *arg);
+       const Arg arg;
+} Button;
+
 typedef struct {
        SoupCookieJarText parent_instance;
        int lock;
@@ -96,9 +115,9 @@ static void addaccelgroup(Client *c);
 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
                WebKitWebResource *r, WebKitNetworkRequest *req,
                WebKitNetworkResponse *resp, Client *c);
+static char *buildfile(const char *path);
 static char *buildpath(const char *path);
-static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
-               GList *gl);
+static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
 static void cleanup(void);
 static void clipboard(Client *c, const Arg *arg);
 
@@ -168,6 +187,8 @@ static void print(Client *c, const Arg *arg);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
                gpointer d);
 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
+static void linkopen(Client *c, const Arg *arg);
+static void linkopenembed(Client *c, const Arg *arg);
 static void reload(Client *c, const Arg *arg);
 static void scroll_h(Client *c, const Arg *arg);
 static void scroll_v(Client *c, const Arg *arg);
@@ -179,6 +200,7 @@ 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 *view, GParamSpec *pspec, Client *c);
+static void titlechangeleave(void *a, void *b, 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);
@@ -238,52 +260,78 @@ beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
 }
 
 static char *
-buildpath(const char *path) {
-       char *apath, *p;
+buildfile(const char *path) {
+       char *dname, *bname, *bpath, *fpath;
        FILE *f;
 
-       /* creating directory */
-       if(path[0] == '/') {
-               apath = g_strdup(path);
-       } else if(path[0] == '~') {
-               if(path[1] == '/') {
-                       apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
+       dname = g_path_get_dirname(path);
+       bname = g_path_get_basename(path);
+
+       bpath = buildpath(dname);
+       g_free(dname);
+
+       fpath = g_build_filename(bpath, bname, NULL);
+       g_free(bname);
+
+       if(!(f = fopen(fpath, "a")))
+               die("Could not open file: %s\n", fpath);
+
+       g_chmod(fpath, 0600); /* always */
+       fclose(f);
+
+       return fpath;
+}
+
+static char *
+buildpath(const char *path) {
+       struct passwd *pw;
+       char *apath, *name, *p, *fpath;
+
+       if(path[0] == '~') {
+               if(path[1] == '/' || path[1] == '\0') {
+                       p = (char *)&path[1];
+                       pw = getpwuid(getuid());
                } else {
-                       apath = g_strconcat(g_get_home_dir(), "/",
-                                       &path[1], NULL);
+                       if((p = strchr(path, '/')))
+                               name = g_strndup(&path[1], --p - path);
+                       else
+                               name = g_strdup(&path[1]);
+
+                       if(!(pw = getpwnam(name))) {
+                               die("Can't get user %s home directory: %s.\n",
+                                               name, path);
+                       }
+                       g_free(name);
                }
+               apath = g_build_filename(pw->pw_dir, p, NULL);
        } else {
-               apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
+               apath = g_strdup(path);
        }
 
-       if((p = strrchr(apath, '/'))) {
-               *p = '\0';
-               g_mkdir_with_parents(apath, 0700);
-               g_chmod(apath, 0700); /* in case it existed */
-               *p = '/';
-       }
-       /* creating file (gives error when apath ends with "/") */
-       if((f = fopen(apath, "a"))) {
-               g_chmod(apath, 0600); /* always */
-               fclose(f);
-       }
+       /* creating directory */
+       if(g_mkdir_with_parents(apath, 0700) < 0)
+               die("Could not access directory: %s\n", apath);
+
+       fpath = realpath(apath, NULL);
+       g_free(apath);
 
-       return apath;
+       return fpath;
 }
 
 static gboolean
-buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
+buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c) {
        WebKitHitTestResultContext context;
        WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
                        e);
        Arg arg;
+       unsigned int i;
 
        g_object_get(result, "context", &context, NULL);
-       if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
-               if(e->button == 2 ||
-                               (e->button == 1 && CLEANMASK(e->state) == CLEANMASK(MODKEY))) {
-                       g_object_get(result, "link-uri", &arg.v, NULL);
-                       newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
+       g_object_get(result, "link-uri", &arg.v, NULL);
+       for(i = 0; i < LENGTH(buttons); i++) {
+               if(context & buttons[i].click && e->button == buttons[i].button &&
+               CLEANMASK(e->state) == CLEANMASK(buttons[i].mask) && buttons[i].func) {
+                       buttons[i].func(c, buttons[i].click == ClkLink && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
                        return true;
                }
        }
@@ -805,6 +853,9 @@ newclient(void) {
        g_signal_connect(G_OBJECT(c->win),
                        "destroy",
                        G_CALLBACK(destroywin), c);
+       g_signal_connect(G_OBJECT(c->win),
+                       "leave_notify_event",
+                       G_CALLBACK(titlechangeleave), c);
 
        if(!kioskmode)
                addaccelgroup(c);
@@ -1114,6 +1165,16 @@ progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
        updatetitle(c);
 }
 
+static void
+linkopen(Client *c, const Arg *arg) {
+       newwindow(NULL, arg, 1);
+}
+
+static void
+linkopenembed(Client *c, const Arg *arg) {
+       newwindow(NULL, arg, 0);
+}
+
 static void
 reload(Client *c, const Arg *arg) {
        gboolean nocache = *(gboolean *)arg;
@@ -1188,8 +1249,8 @@ setup(void) {
        atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
 
        /* dirs and files */
-       cookiefile = buildpath(cookiefile);
-       scriptfile = buildpath(scriptfile);
+       cookiefile = buildfile(cookiefile);
+       scriptfile = buildfile(scriptfile);
        cachefolder = buildpath(cachefolder);
        styledir = buildpath(styledir);
        if(stylefile == NULL) {
@@ -1201,12 +1262,12 @@ setup(void) {
                                        styles[i].regex);
                                styles[i].regex = NULL;
                        }
-                       styles[i].style = buildpath(
+                       styles[i].style = buildfile(
                                        g_strconcat(styledir,
                                                styles[i].style, NULL));
                }
        } else {
-               stylefile = buildpath(stylefile);
+               stylefile = buildfile(stylefile);
        }
 
        /* request handler */
@@ -1237,8 +1298,12 @@ setup(void) {
 
        /* proxy */
        if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
-               new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
-                       g_strdup_printf("http://%s", proxy);
+               new_proxy = g_strrstr(proxy, "http://")
+            || g_strrstr(proxy, "socks://")
+            || g_strrstr(proxy, "socks4://")
+            || g_strrstr(proxy, "socks5://")
+            ? g_strdup(proxy)
+            : g_strdup_printf("http://%s", proxy);
                puri = soup_uri_new(new_proxy);
                g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
                soup_uri_free(puri);
@@ -1298,6 +1363,12 @@ titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
        }
 }
 
+static void
+titlechangeleave(void *a, void *b, Client *c) {
+       c->linkhover = NULL;
+       updatetitle(c);
+}
+
 static void
 toggle(Client *c, const Arg *arg) {
        WebKitWebSettings *settings;