Adapted buttonrelease()
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 4f7b691..b79438d 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -60,11 +60,12 @@ typedef struct Client {
        WebKitWebView *view;
        WebKitWebInspector *inspector;
        WebKitHitTestResult *mousepos;
+       GTlsCertificateFlags tlsflags;
        const char *title, *targeturi;
        const char *needle;
        gint progress;
        struct Client *next;
-       gboolean zoomed, fullscreen, isinspecting, sslfailed;
+       gboolean zoomed, fullscreen, isinspecting;
 } Client;
 
 typedef struct {
@@ -75,11 +76,12 @@ typedef struct {
 } Key;
 
 typedef struct {
-       unsigned int click;
+       unsigned int target;
        unsigned int mask;
        guint button;
-       void (*func)(Client *c, const Arg *arg);
+       void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
        const Arg arg;
+       unsigned int stopevent;
 } Button;
 
 typedef struct {
@@ -106,7 +108,7 @@ static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
                           WebKitNetworkResponse *resp, Client *c);
 static char *buildfile(const char *path);
 static char *buildpath(const char *path);
-static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
+static gboolean buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c);
 static void cleanup(void);
 static void clipboard(Client *c, const Arg *arg);
 
@@ -140,7 +142,10 @@ static void setstyle(Client *c, const char *style);
 
 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
 
-static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
+static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
+               Client *c);
+static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
+static void download(Client *c, WebKitURIResponse *r);
 
 static void inspector(Client *c, const Arg *arg);
 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
@@ -153,8 +158,7 @@ static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
                          GdkModifierType mods, Client *c);
 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
                guint modifiers, Client *c);
-static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
-                             Client *c);
+static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
 static void loaduri(Client *c, const Arg *arg);
 static void navigate(Client *c, const Arg *arg);
 static Client *newclient(Client *c);
@@ -169,7 +173,7 @@ static void menuactivate(GtkMenuItem *item, Client *c);
 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 progresschanged(WebKitWebView *v, GParamSpec *ps, 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);
@@ -305,27 +309,25 @@ buildpath(const char *path)
 }
 
 gboolean
-buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
+buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
 {
-       WebKitHitTestResultContext context;
-       WebKitHitTestResult *result;
-       Arg arg;
-       unsigned int i;
-
-       result = webkit_web_view_get_hit_test_result(web, e);
-       g_object_get(result, "context", &context, NULL);
-       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;
+       WebKitHitTestResultContext element;
+       GdkEventButton *eb = (GdkEventButton*)e;
+       int i;
+
+       element = webkit_hit_test_result_get_context(c->mousepos);
+
+       for (i = 0; i < LENGTH(buttons); ++i) {
+               if (element & buttons[i].target &&
+                   eb->button == buttons[i].button &&
+                   CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
+                   buttons[i].func) {
+                       buttons[i].func(c, &buttons[i].arg, c->mousepos);
+                       return buttons[i].stopevent;
                }
        }
-       return false;
+
+       return FALSE;
 }
 
 void
@@ -698,15 +700,27 @@ handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
        spawn(c, &arg);
 }
 
-gboolean
-initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
+void
+downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
 {
-       Arg arg;
+       g_signal_connect(G_OBJECT(d), "notify::response",
+           G_CALLBACK(responsereceived), c);
+}
 
-       updatewinid(c);
-       arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
-       spawn(c, &arg);
-       return FALSE;
+void
+responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
+{
+       download(c, webkit_download_get_response(d));
+       webkit_download_cancel(d);
+}
+
+void
+download(Client *c, WebKitURIResponse *r)
+{
+       Arg a;
+
+       a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
+       spawn(c, &a);
 }
 
 void
@@ -809,37 +823,34 @@ mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
 }
 
 void
-loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
+loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
 {
-       WebKitWebFrame *frame;
-       WebKitWebDataSource *src;
-       WebKitNetworkRequest *request;
-       SoupMessage *msg;
-       char *uri;
-
-       switch (webkit_web_view_get_load_status (c->view)) {
+       switch (e) {
+       case WEBKIT_LOAD_STARTED:
+               c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
+               break;
+       case WEBKIT_LOAD_REDIRECTED:
+               setatom(c, AtomUri, geturi(c));
+               break;
        case WEBKIT_LOAD_COMMITTED:
-               uri = geturi(c);
-               if (strstr(uri, "https://") == uri) {
-                       frame = webkit_web_view_get_main_frame(c->view);
-                       src = webkit_web_frame_get_data_source(frame);
-                       request = webkit_web_data_source_get_request(src);
-                       msg = webkit_network_request_get_message(request);
-                       c->sslfailed = !(soup_message_get_flags(msg)
-                                      & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
-               }
-               setatom(c, AtomUri, uri);
+               if (!webkit_web_view_get_tls_info(c->view, NULL, &(c->tlsflags)))
+                       c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
+
+               setatom(c, AtomUri, geturi(c));
 
                if (enablestyle)
-                       setstyle(c, getstyle(uri));
+                       setstyle(c, getstyle(geturi(c)));
                break;
        case WEBKIT_LOAD_FINISHED:
-               c->progress = 100;
-               updatetitle(c);
-               break;
-       default:
+               /* Disabled until we write some WebKitWebExtension for
+                * manipulating the DOM directly.
+               evalscript(c, "document.documentElement.style.overflow = '%s'",
+                   enablescrollbars ? "auto" : "hidden");
+               */
+               runscript(c);
                break;
        }
+       updatetitle(c);
 }
 
 void
@@ -900,6 +911,7 @@ newclient(Client *rc)
        clients = c;
 
        c->view = newview(c, rc ? rc->view : NULL);
+       c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
 
        return c;
 }
@@ -964,6 +976,9 @@ newview(Client *c, WebKitWebView *rv)
                    webkit_web_context_get_cookie_manager(context),
                    cookiepolicy_get());
 
+               g_signal_connect(G_OBJECT(context), "download-started",
+                   G_CALLBACK(downloadstarted), c);
+
                v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
                    "settings", settings,
                    "user-content-manager", contentmanager,
@@ -992,17 +1007,14 @@ newview(Client *c, WebKitWebView *rv)
                         "window-object-cleared",
                         G_CALLBACK(windowobjectcleared), c);
        g_signal_connect(G_OBJECT(v),
-                        "notify::load-status",
-                        G_CALLBACK(loadstatuschange), c);
-       g_signal_connect(G_OBJECT(v),
-                        "notify::progress",
-                        G_CALLBACK(progresschange), c);
+                        "load-changed",
+                        G_CALLBACK(loadchanged), c);
        g_signal_connect(G_OBJECT(v),
-                        "download-requested",
-                        G_CALLBACK(initdownload), c);
+                        "notify::estimated-load-progress",
+                        G_CALLBACK(progresschanged), c);
        g_signal_connect(G_OBJECT(v),
                         "button-release-event",
-                        G_CALLBACK(buttonrelease), c);
+                        G_CALLBACK(buttonreleased), c);
        g_signal_connect(G_OBJECT(v),
                         "context-menu",
                         G_CALLBACK(contextmenu), c);
@@ -1222,9 +1234,10 @@ processx(GdkXEvent *e, GdkEvent *event, gpointer d)
 }
 
 void
-progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
+progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
 {
-       c->progress = webkit_web_view_get_progress(c->view) * 100;
+       c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
+           100;
        updatetitle(c);
 }
 
@@ -1522,11 +1535,8 @@ getpagestat(Client *c)
 {
        const char *uri = geturi(c);
 
-       if (strstr(uri, "https://") == uri)
-               pagestat[0] = c->sslfailed ? 'U' : 'T';
-       else
-               pagestat[0] = '-';
-
+       pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
+           c->tlsflags > 0 ? 'U' : 'T';
        pagestat[1] = '\0';
 }