X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=cd01b8336784ddcfbd78177b7851723d8d975831;hb=92afa03d80ba077f7a5b1dc1bd44d82ab1d35b8f;hp=0d05732ef90fb429d27276112e9abb716e68ca39;hpb=3657813fec9b1b81ecb7de9389eaade702112005;p=surf.git diff --git a/surf.c b/surf.c index 0d05732..cd01b83 100644 --- a/surf.c +++ b/surf.c @@ -62,7 +62,7 @@ typedef struct { static Display *dpy; static Atom uriprop; -static SoupCookieJar *cookiejar; +static SoupCookieJar *cookies; static SoupSession *session; static Client *clients = NULL; static GdkNativeWindow embed = 0; @@ -70,9 +70,11 @@ 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); @@ -102,11 +104,12 @@ 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 progresschange(WebKitWebView *v, gint p, Client *c); +static void reloadcookies(); static void reload(Client *c, const Arg *arg); +static void resize(GtkWidget *w, GtkAllocation *a, Client *c); static void sigchld(int unused); 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); @@ -154,6 +157,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) @@ -186,8 +210,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]); } } @@ -395,6 +417,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 @@ -447,14 +471,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); @@ -619,6 +646,23 @@ reload(Client *c, const Arg *arg) { webkit_web_view_reload(c->view); } +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 scroll(Client *c, const Arg *arg) { gdouble v; @@ -632,6 +676,19 @@ scroll(Client *c, const Arg *arg) { gtk_adjustment_set_value(a, v); } +void +resize(GtkWidget *w, GtkAllocation *a, Client *c) { + double zoom; + + zoom = webkit_web_view_get_zoom_level(c->view); + if(a->width * a->height < 300 * 300 && 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) @@ -651,7 +708,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); @@ -661,8 +718,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 @@ -718,7 +777,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); }