Dropping -f/-u arguments as suggested by Kai Hendry
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 3e8e07d..2a6d388 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -28,8 +28,19 @@ typedef struct Client {
        gint progress;
        struct Client *next;
 } Client;
+
+typedef struct Cookie {
+       char *name;
+       char *value;
+       char *domain;
+       char *path;
+       struct Cookie *next;
+} Cookie;
+
 SoupCookieJar *cookiejar;
+SoupSession *session;
 Client *clients = NULL;
+Cookie *cookies = NULL;
 gboolean embed = FALSE;
 gboolean showxid = FALSE;
 gboolean ignore_once = FALSE;
@@ -37,6 +48,7 @@ extern char *optarg;
 extern int optind;
 
 static void cleanup(void);
+static void proccookies(SoupMessage *m, Client *c);
 static void destroyclient(Client *c);
 static void destroywin(GtkWidget* w, Client *c);
 static void die(char *str);
@@ -56,11 +68,14 @@ static WebKitWebView *newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c)
 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
 static void progresschange(WebKitWebView *view, gint p, Client *c);
+static void request(SoupSession *s, SoupMessage *m, Client *c);
+static void setcookie(char *name, char *val, char *dom, char *path, long exp);
 static void setup(void);
 static void showsearch(Client *c);
 static void showurl(Client *c);
 static void stop(Client *c);
-static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
+static void titlechange(WebKitWebView* view, WebKitWebFrame* frame,
+               const gchar* title, Client *c);
 static void usage();
 static void updatetitle(Client *c, const gchar *title);
 
@@ -70,6 +85,20 @@ cleanup(void) {
                destroyclient(clients);
 }
 
+void
+proccookies(SoupMessage *m, Client *c) {
+       GSList *l;
+       SoupCookie *co;
+       long t;
+
+       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
 destroyclient(Client *c) {
        Client *p;
@@ -196,23 +225,19 @@ keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
        }
        if(ev->state & GDK_CONTROL_MASK) {
                switch(ev->keyval) {
-               case GDK_p:
                case GDK_P:
-                       if((ev->state & GDK_SHIFT_MASK))
-                               webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
-                       else
-                               gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
+                       webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
                        return TRUE;
-
+               case GDK_p:
+                       gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
                case GDK_y:
                        gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
                        return TRUE;
                case GDK_r:
+                       webkit_web_view_reload(c->view);
+                       return TRUE;
                case GDK_R:
-                       if((ev->state & GDK_SHIFT_MASK))
-                                webkit_web_view_reload_bypass_cache(c->view);
-                       else
-                                webkit_web_view_reload(c->view);
+                       webkit_web_view_reload_bypass_cache(c->view);
                        return TRUE;
                case GDK_b:
                        return TRUE;
@@ -240,10 +265,10 @@ keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
                                        !(ev->state & GDK_SHIFT_MASK),
                                        TRUE);
                        return TRUE;
-               case GDK_Left:
+               case GDK_h:
                        webkit_web_view_go_back(c->view);
                        return TRUE;
-               case GDK_Right:
+               case GDK_l:
                        webkit_web_view_go_forward(c->view);
                        return TRUE;
                }
@@ -359,6 +384,7 @@ newclient(void) {
        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(newwindow), c);
        g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
+       g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
 
        /* urlbar */
        c->urlbar = gtk_entry_new();
@@ -445,8 +471,21 @@ progresschange(WebKitWebView* view, gint p, Client *c) {
        updatetitle(c, NULL);
 }
 
-void setup(void) {
+void
+request(SoupSession *s, SoupMessage *m, Client *c) {
+       soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
+                       G_CALLBACK(proccookies), c);
+}
+
+void
+setcookie(char *name, char *val, char *dom, char *path, long exp) {
+       printf("%s %s %s %s %li\n", name, val, dom, path, exp);
+}
+
+void
+setup(void) {
        dpy = GDK_DISPLAY();
+       session = webkit_get_default_session();
        urlprop = XInternAtom(dpy, "_SURF_URL", False);
 }
 
@@ -485,7 +524,7 @@ titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
 void
 usage() {
        fputs("surf - simple browser\n", stderr);
-       die("usage: surf [-e] [-x] [-u uri] [-f file]\n");
+       die("usage: surf [-e] [-x] [uri]\n");
 }
 
 void
@@ -525,21 +564,21 @@ int main(int argc, char *argv[]) {
                        showxid = TRUE;
                        embed = TRUE;
                        break;
-               case 'u':
-                       c = newclient();
-                       loaduri(c, optarg);
-                       break;
-               case 'f':
-                       c = newclient();
-                       loadfile(c, optarg);
-                       break;
                case 'v':
                        die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
                        break;
                default:
                        usage();
                }
-       if(optind != argc)
+       if(optind + 1 == argc) {
+               c = newclient();
+               if(strchr("./", argv[optind][0]) || strcmp("-", argv[optind]) == 0)
+                       loadfile(c, argv[optind]);
+               else
+                       loaduri(c, argv[optind]);
+
+       }
+       else if(optind != argc)
                usage();
        if(!clients)
                newclient();