small cleanups
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index abf254c..5d2a30d 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -37,6 +37,7 @@ typedef struct Client {
        char *title, *linkhover;
        gint progress;
        struct Client *next;
+       gboolean zoomed;
 } Client;
 
 typedef struct {
@@ -45,14 +46,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,18 +63,19 @@ typedef struct {
 
 static Display *dpy;
 static Atom uriprop;
-static SoupCookieJar *cookiejar;
+static SoupCookieJar *cookies;
 static SoupSession *session;
 static Client *clients = NULL;
-/*static Cookie *cookies = NULL;*/
 static GdkNativeWindow embed = 0;
 static gboolean showxid = FALSE;
 static gboolean ignore_once = FALSE;
 static char winid[64];
 static char *progname;
+static gboolean lockcookie = FALSE;
 
 static const char *autouri(Client *c);
 static char *buildpath(const char *path);
+static void changecookie(SoupCookieJar *jar, SoupCookie *o, SoupCookie *n, gpointer p);
 static void cleanup(void);
 static void clipboard(Client *c, const Arg *arg);
 static void context(WebKitWebView *v, GtkMenu *m, Client *c);
@@ -110,16 +104,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 reloadcookies();
 static void reload(Client *c, const Arg *arg);
-static void rereadcookies(void);
+static void resize(GtkWidget *w, GtkAllocation *a, Client *c);
 static void sigchld(int unused);
-static void setcookie(char *name, char *val, char *dom, char *path, long exp);
 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);
@@ -167,6 +158,27 @@ buildpath(const char *path) {
        return apath;
 }
 
+void
+changecookie(SoupCookieJar *j, SoupCookie *oc, SoupCookie *c, gpointer p) {
+       SoupDate *e;
+       SoupCookieJar *jar;
+
+       if(lockcookie)
+               return;
+       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);
+       }
+       
+       jar = soup_cookie_jar_text_new(cookiefile, FALSE);
+       if(c)
+               soup_cookie_jar_add_cookie(jar, soup_cookie_copy(c));
+       else
+               soup_cookie_jar_delete_cookie(jar, oc);
+       g_object_unref(jar);
+}
+
 void
 cleanup(void) {
        while(clients)
@@ -199,8 +211,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]);
        }
 }
@@ -335,7 +345,7 @@ geturi(Client *c) {
        char *uri;
 
        if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
-               uri = copystr(NULL, "about:blank");
+               uri = "about:blank";
        return uri;
 }
 
@@ -408,6 +418,8 @@ loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
        XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), uriprop,
                        XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
                        strlen(uri) + 1);
+
+       reloadcookies();
 }
 
 void
@@ -460,14 +472,17 @@ newclient(void) {
        gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
        g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
        g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
+       g_signal_connect(G_OBJECT(c->win), "size-allocate", G_CALLBACK(resize), c);
 
        if(!(c->items = calloc(1, sizeof(GtkWidget *) * LENGTH(items))))
                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 +503,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);
 
@@ -618,33 +632,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;
@@ -655,8 +648,20 @@ reload(Client *c, const Arg *arg) {
 }
 
 void
-rereadcookies(void) {
+reloadcookies() {
+       SoupCookieJar *jar;
+       GSList *l, *e;
 
+       lockcookie = TRUE;
+       for(l = e = soup_cookie_jar_all_cookies(cookies); e; e = e->next)
+               soup_cookie_jar_delete_cookie(cookies, (SoupCookie *)e->data);
+       soup_cookies_free(l);
+       jar = soup_cookie_jar_text_new(cookiefile, TRUE);
+       for(l = e = soup_cookie_jar_all_cookies(jar); e; e = e->next)
+               soup_cookie_jar_add_cookie(cookies, (SoupCookie *)e->data);
+       g_slist_free(l);
+       lockcookie = FALSE;
+       g_object_unref(jar);
 }
 
 void
@@ -672,6 +677,19 @@ scroll(Client *c, const Arg *arg) {
        gtk_adjustment_set_value(a, v);
 }
 
+void
+resize(GtkWidget *w, GtkAllocation *a, Client *c) {
+       double zoom;
+
+       if(c->zoomed)
+               return;
+       zoom = webkit_web_view_get_zoom_level(c->view);
+       if(a->width * a->height < 300 * 400 && zoom != 0.2)
+               webkit_web_view_set_zoom_level(c->view, 0.2);
+       else if(zoom != 1.0)
+               webkit_web_view_set_zoom_level(c->view, 1.0);
+}
+
 void
 sigchld(int unused) {
        if(signal(SIGCHLD, sigchld) == SIG_ERR)
@@ -679,11 +697,6 @@ sigchld(int unused) {
        while(0 < waitpid(-1, NULL, WNOHANG));
 }
 
-void
-setcookie(char *name, char *val, char *dom, char *path, long exp) {
-
-}
-
 void
 setup(void) {
        SoupSession *s;
@@ -696,7 +709,7 @@ setup(void) {
 
        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 +719,10 @@ 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));
+       cookies = soup_cookie_jar_new();
+       soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookies));
+       g_signal_connect(cookies, "changed", G_CALLBACK(changecookie), NULL);
+       reloadcookies();
 }
 
 void
@@ -763,7 +778,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);
        }
@@ -825,12 +840,15 @@ windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObje
 
 void
 zoom(Client *c, const Arg *arg) {
+       c->zoomed = TRUE;
        if(arg->i < 0)          /* zoom out */
                webkit_web_view_zoom_out(c->view);
        else if(arg->i > 0)     /* zoom in */
                webkit_web_view_zoom_in(c->view);
-       else                    /* reset */
+       else {                  /* reset */
+               c->zoomed = FALSE;
                webkit_web_view_set_zoom_level(c->view, 1.0);
+       }
 }
 
 int main(int argc, char *argv[]) {