fix for segfault when closing window while loading (via nibble)
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 5e6d777..5cb4824 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -23,7 +23,7 @@
 #define LENGTH(x)               (sizeof x / sizeof x[0])
 #define CLEANMASK(mask)         (mask & ~(GDK_MOD2_MASK))
 
-enum { AtomFind, AtomGo, AtomUri, AtomHiLight, AtomLast };
+enum { AtomFind, AtomGo, AtomUri, AtomLast };
 
 typedef union Arg Arg;
 union Arg {
@@ -81,13 +81,13 @@ static const char *getatom(Client *c, int a);
 static const char *getcookies(SoupURI *uri);
 static char *geturi(Client *c);
 void gotheaders(SoupMessage *msg, gpointer user_data);
+static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
 static void linkhover(WebKitWebView *v, const char* t, const char* l, Client *c);
 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
 static void loaduri(Client *c, const Arg *arg);
-static void mousemove(GtkWidget *w, GdkEventMotion *e, Client *c);
 static void navigate(Client *c, const Arg *arg);
-static Client *newclient(void);
+static Client *newclient(gboolean view);
 static void newwindow(Client *c, const Arg *arg);
 static void newrequest(SoupSession *s, SoupMessage *msg, gpointer v);
 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
@@ -181,17 +181,14 @@ copystr(char **str, const char *src) {
 
 WebKitWebView *
 createwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
-       Client *n = newclient();
+       Client *n = newclient(TRUE);
        return n->view;
 }
 
 gboolean
 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m,  WebKitWebPolicyDecision *p, Client *c) {
        if(!webkit_web_view_can_show_mime_type(v, m)) {
-               webkit_web_policy_decision_ignore(p);
-               webkit_web_view_load_html_string(c->view,
-                               "Can't display content.",
-                               webkit_network_request_get_uri(r));
+               webkit_web_policy_decision_download(p);
                return TRUE;
        }
        return FALSE;
@@ -329,6 +326,16 @@ gotheaders(SoupMessage *msg, gpointer v) {
        soup_cookies_free(l);
 }
 
+gboolean
+initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
+       Arg arg;
+
+       updatewinid(c);
+       arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o));
+       spawn(c, &arg);
+       return FALSE;
+}
+
 gboolean
 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
        guint i;
@@ -396,21 +403,6 @@ loaduri(Client *c, const Arg *arg) {
        }
 }
 
-void
-mousemove(GtkWidget *w, GdkEventMotion *e, Client *c) {
-       int result;
-       GdkEventButton coord;
-       WebKitHitTestResult *hit;
-
-       coord.x = e->x;
-       coord.y = e->y;
-       hit = webkit_web_view_get_hit_test_result(c->view, &coord);
-       g_object_get(hit, "context", &result, NULL);
-       if(result & WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE && !c->linkhover) {
-               puts("Picture");
-       }
-}
-
 void
 navigate(Client *c, const Arg *arg) {
        int steps = *(int *)arg;
@@ -418,7 +410,7 @@ navigate(Client *c, const Arg *arg) {
 }
 
 Client *
-newclient(void) {
+newclient(gboolean newview) {
        Client *c;
        WebKitWebSettings *settings;
        WebKitWebFrame *frame;
@@ -462,15 +454,18 @@ newclient(void) {
 
        /* Webview */
        c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
-       g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
-       g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
-       g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
-       g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
-       g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
-       g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
-       g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
-       g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
-       g_signal_connect(G_OBJECT(c->view), "motion-notify-event", G_CALLBACK(mousemove), c);
+       /* The widget to which the widget is added will handle the signals. */
+       if (!newview){
+               g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
+               g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
+               g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
+               g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
+               g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
+               g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
+               g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
+               g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
+               g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
+       }
 
        /* Indicator */
        c->indicator = gtk_drawing_area_new();
@@ -508,11 +503,12 @@ newclient(void) {
        g_object_set(G_OBJECT(settings), "auto-load-images", loadimage, NULL);
        g_object_set(G_OBJECT(settings), "enable-plugins", plugin, NULL);
        g_object_set(G_OBJECT(settings), "enable-scripts", script, NULL);
+       g_object_set(G_OBJECT(settings), "enable-spatial-navigation", true, NULL);
+
        g_free(uri);
 
        setatom(c, AtomFind, "");
        setatom(c, AtomUri, "about:blank");
-       setatom(c, AtomHiLight, "about:blank");
        if(NOBACKGROUND)
                webkit_web_view_set_transparent(c->view, TRUE);
 
@@ -693,7 +689,6 @@ setup(void) {
        atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
        atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
        atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
-       atoms[AtomHiLight] = XInternAtom(dpy, "_SURF_HILIGHT", False);
 
        /* dirs and files */
        cookiefile = buildpath(cookiefile);
@@ -762,14 +757,12 @@ void
 update(Client *c) {
        char *t;
 
-       if(c->progress != 100) {
+       if(c->progress != 100)
                t = g_strdup_printf("[%i%%] %s", c->progress, c->title);
-       }
        else if(c->linkhover)
                t = g_strdup(c->linkhover);
        else
                t = g_strdup(c->title);
-       setatom(c, AtomHiLight, c->linkhover ? c->linkhover : geturi(c));
        drawindicator(c);
        gtk_window_set_title(GTK_WINDOW(c->win), t);
        g_free(t);
@@ -846,7 +839,7 @@ main(int argc, char *argv[]) {
        if(i < argc)
                arg.v = argv[i];
        setup();
-       newclient();
+       newclient(FALSE);
        if(arg.v)
                loaduri(clients, &arg);
        gtk_main();