Trying a different cookie handling system.
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index abf254c..fefb263 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -45,14 +45,6 @@ typedef struct {
        const Arg arg;
 } Item;
 
-typedef struct Cookie {
-       char *name;
-       char *value;
-       char *domain;
-       char *path;
-       struct Cookie *next;
-} Cookie;
-
 typedef enum {
        Browser = 0x0001,
        SearchBar = 0x0010,
@@ -70,10 +62,7 @@ typedef struct {
 
 static Display *dpy;
 static Atom uriprop;
-static SoupCookieJar *cookiejar;
-static SoupSession *session;
 static Client *clients = NULL;
-/*static Cookie *cookies = NULL;*/
 static GdkNativeWindow embed = 0;
 static gboolean showxid = FALSE;
 static gboolean ignore_once = FALSE;
@@ -110,16 +99,13 @@ static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *
 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
 static void print(Client *c, const Arg *arg);
-static void proccookies(SoupMessage *m, Client *c);
 static void progresschange(WebKitWebView *v, gint p, Client *c);
-static void request(SoupSession *s, SoupMessage *m, Client *c);
 static void reload(Client *c, const Arg *arg);
-static void rereadcookies(void);
+static void request(SoupSession *s, SoupMessage *m, gpointer p);
 static void sigchld(int unused);
-static void setcookie(char *name, char *val, char *dom, char *path, long exp);
+static void setcookie(SoupMessage *m, gpointer p);
 static void setup(void);
 static void spawn(Client *c, const Arg *arg);
-static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
 static void scroll(Client *c, const Arg *arg);
 static void searchtext(Client *c, const Arg *arg);
 static void source(Client *c, const Arg *arg);
@@ -180,11 +166,15 @@ cleanup(void) {
 void
 clipboard(Client *c, const Arg *arg) {
        gboolean paste = *(gboolean *)arg;
+       const char *uri;
 
        if(paste)
                gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
-       else
-               gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
+       else {
+               if(!(uri = autouri(c)))
+                       uri = geturi(c);
+               gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri, -1);
+       }
 }
 
 void
@@ -199,8 +189,6 @@ context(WebKitWebView *v, GtkMenu *m, Client *c) {
                if(parent)
                        gtk_container_remove(parent, c->items[i]);
                gtk_menu_shell_append(GTK_MENU_SHELL(m), c->items[i]);
-               g_signal_connect(G_OBJECT(c->items[i]), "activate",
-                               G_CALLBACK(itemclick), c);
                gtk_widget_show(c->items[i]);
        }
 }
@@ -465,9 +453,11 @@ newclient(void) {
                die("Cannot malloc!\n");
 
        /* contextmenu */
-       for(i = 0; i < LENGTH(items); i++)
+       for(i = 0; i < LENGTH(items); i++) {
                c->items[i] = gtk_menu_item_new_with_label(items[i].label);
-
+               g_signal_connect(G_OBJECT(c->items[i]), "activate",
+                               G_CALLBACK(itemclick), c);
+       }
 
        /* VBox */
        c->vbox = gtk_vbox_new(FALSE, 0);
@@ -488,7 +478,6 @@ newclient(void) {
        g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
        g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
        g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
-       g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
        g_signal_connect(G_OBJECT(c->view), "focus-in-event", G_CALLBACK(focusview), c);
        g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(context), c);
 
@@ -580,7 +569,7 @@ createwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
 
 void
 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
-       Arg arg = {.v = text };
+       Arg arg = { .v = text };
        if(text != NULL)
                loaduri((Client *) d, &arg);
 }
@@ -618,33 +607,12 @@ print(Client *c, const Arg *arg) {
        webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
 }
 
-void
-proccookies(SoupMessage *m, Client *c) {
-       GSList *l;
-       SoupCookie *co;
-       long t;
-
-       rereadcookies();
-       for (l = soup_cookies_from_response(m); l; l = l->next){
-               co = (SoupCookie *)l->data;
-               t = co->expires ?  soup_date_to_time_t(co->expires) : 0;
-               setcookie(co->name, co->value, co->domain, co->value, t);
-       }
-       g_slist_free(l);
-}
-
 void
 progresschange(WebKitWebView *v, gint p, Client *c) {
        c->progress = p;
        update(c);
 }
 
-void
-request(SoupSession *s, SoupMessage *m, Client *c) {
-       soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
-                       G_CALLBACK(proccookies), c);
-}
-
 void
 reload(Client *c, const Arg *arg) {
        gboolean nocache = *(gboolean *)arg;
@@ -654,11 +622,6 @@ reload(Client *c, const Arg *arg) {
                 webkit_web_view_reload(c->view);
 }
 
-void
-rereadcookies(void) {
-
-}
-
 void
 scroll(Client *c, const Arg *arg) {
        gdouble v;
@@ -673,15 +636,46 @@ scroll(Client *c, const Arg *arg) {
 }
 
 void
-sigchld(int unused) {
-       if(signal(SIGCHLD, sigchld) == SIG_ERR)
-               die("Can't install SIGCHLD handler");
-       while(0 < waitpid(-1, NULL, WNOHANG));
+request(SoupSession *s, SoupMessage *m, gpointer p) {
+       SoupCookieJar *cookies;
+       SoupMessageHeaders *h;
+       char *cookiestr;
+       soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
+                       G_CALLBACK(setcookie), NULL);
+
+       h = m->request_headers;
+       cookies = soup_cookie_jar_text_new(cookiefile, TRUE);
+       cookiestr = soup_cookie_jar_get_cookies(cookies, soup_message_get_uri(m), FALSE);
+       if(cookiestr)
+               soup_message_headers_append(h, "Cookie", cookiestr);
+       g_object_unref(cookies);
 }
 
 void
-setcookie(char *name, char *val, char *dom, char *path, long exp) {
+setcookie(SoupMessage *m, gpointer p) {
+       SoupCookieJar *cookies;
+       SoupCookie *c;
+       SoupDate *e;
+       GSList *l;
 
+       cookies = soup_cookie_jar_text_new(cookiefile, FALSE);
+       for (l = soup_cookies_from_response(m); l; l = l->next){
+               c = (SoupCookie *)l->data;
+               if(c && c->expires == NULL) {
+                       e = soup_date_new_from_time_t(time(NULL) + sessiontime);
+                       c = soup_cookie_copy(c);
+                       soup_cookie_set_expires(c, e);
+               }
+               soup_cookie_jar_add_cookie(cookies, c);
+       }
+       g_slist_free(l);
+       g_object_unref(cookies);
+}
+void
+sigchld(int unused) {
+       if(signal(SIGCHLD, sigchld) == SIG_ERR)
+               die("Can't install SIGCHLD handler");
+       while(0 < waitpid(-1, NULL, WNOHANG));
 }
 
 void
@@ -695,8 +689,7 @@ setup(void) {
                g_thread_init(NULL);
 
        dpy = GDK_DISPLAY();
-       session = webkit_get_default_session();
-       uriprop = XInternAtom(dpy, "_SURF_uri", False);
+       uriprop = XInternAtom(dpy, "_SURF_URI", False);
 
        /* create dirs and files */
        cookiefile = buildpath(cookiefile);
@@ -706,8 +699,7 @@ setup(void) {
 
        /* cookie persistance */
        s = webkit_get_default_session();
-       cookiejar = soup_cookie_jar_text_new(cookiefile, FALSE);
-       soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
+       g_signal_connect_after(s, "request-queued", G_CALLBACK(request), NULL);
 }
 
 void
@@ -763,7 +755,7 @@ spawn(Client *c, const Arg *arg) {
                        close(ConnectionNumber(dpy));
                setsid();
                execvp(((char **)arg->v)[0], (char **)arg->v);
-               fprintf(stderr, "tabbed: execvp %s", ((char **)arg->v)[0]);
+               fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
                perror(" failed");
                exit(0);
        }