X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=3e912d5facd6ca71345fc19ea281a49ec3749142;hb=2d9716907da6c5ae1c494958b25b2b7969adf28a;hp=64f2a753814238f0e9247d95c2cd167bd178aaa9;hpb=d536cb6fe1eca5e59adfe6cf20c30d43c7f72858;p=surf.git diff --git a/surf.c b/surf.c index 64f2a75..3e912d5 100644 --- a/surf.c +++ b/surf.c @@ -14,18 +14,21 @@ #include #include #include +#include #define LENGTH(x) (sizeof x / sizeof x[0]) Display *dpy; Atom urlprop; typedef struct Client { - GtkWidget *win, *scroll, *vbox, *urlbar, *urllist, *searchbar; + GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar; WebKitWebView *view; + WebKitDownload * dl; gchar *title; gint progress; struct Client *next; } Client; +SoupCookieJar *cookiejar; Client *clients = NULL; gboolean embed = FALSE; gboolean showxid = FALSE; @@ -40,7 +43,7 @@ static gboolean decidewindow(WebKitWebView *view, WebKitWebFrame *f, static void destroyclient(Client *c); static void destroywin(GtkWidget* w, gpointer d); static void die(char *str); -static gboolean download(WebKitWebView *view, GObject *o, gpointer d); +static gboolean download(WebKitWebView *view, WebKitDownload *o, gpointer d); static gchar *geturi(Client *c); static void hidesearch(Client *c); static void hideurl(Client *c); @@ -80,7 +83,6 @@ destroyclient(Client *c) { gtk_widget_destroy(GTK_WIDGET(webkit_web_view_new())); gtk_widget_destroy(c->scroll); gtk_widget_destroy(c->urlbar); - gtk_widget_destroy(c->urllist); gtk_widget_destroy(c->searchbar); gtk_widget_destroy(c->vbox); gtk_widget_destroy(c->win); @@ -107,9 +109,21 @@ void die(char *str) { } gboolean -download(WebKitWebView *view, GObject *o, gpointer d) { +download(WebKitWebView *view, WebKitDownload *o, gpointer d) { /* TODO */ - return FALSE; + const gchar *home; + gchar *uri, *filename; + + home = g_get_home_dir(); + filename = g_build_filename(home, ".surf", "dl", + webkit_download_get_suggested_filename(o), NULL); + g_mkdir(g_path_get_dirname(filename), 0755); + uri = g_strconcat("file://", filename, NULL); + webkit_download_set_destination_uri(o, uri); + g_free(filename); + g_free(uri); + webkit_download_start(o); + return TRUE; } gchar * @@ -172,7 +186,7 @@ keypress(GtkWidget* w, GdkEventKey *ev, gpointer d) { return FALSE; } } - if(ev->state == GDK_CONTROL_MASK || ev->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) { + else if(ev->state == GDK_CONTROL_MASK || ev->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) { switch(ev->keyval) { case GDK_r: case GDK_R: @@ -197,6 +211,16 @@ keypress(GtkWidget* w, GdkEventKey *ev, gpointer d) { return TRUE; } } + else { + switch(ev->keyval) { + case GDK_k: + webkit_web_view_move_cursor(c->view, GTK_MOVEMENT_DISPLAY_LINES, -1); + return TRUE; + case GDK_j: + webkit_web_view_move_cursor(c->view, GTK_MOVEMENT_DISPLAY_LINES, 1); + return TRUE; + } + } return FALSE; } @@ -233,11 +257,13 @@ loadfile(const Client *c, const gchar *f) { if(strcmp(f, "-") == 0) { chan = g_io_channel_unix_new(STDIN_FILENO); if (chan) { - while(g_io_channel_read_line(chan, &line, NULL, NULL, &e) == G_IO_STATUS_NORMAL) { + while(g_io_channel_read_line(chan, &line, NULL, NULL, + &e) == G_IO_STATUS_NORMAL) { g_string_append(code, line); g_free(line); } - webkit_web_view_load_html_string(c->view, code->str, NULL); + webkit_web_view_load_html_string(c->view, code->str, + "file://."); g_io_channel_shutdown(chan, FALSE, NULL); } } @@ -245,7 +271,7 @@ loadfile(const Client *c, const gchar *f) { g_string_prepend(uri, "file://"); loaduri(c, uri->str); } - + } void @@ -329,7 +355,6 @@ newclient(void) { WebKitWebView * newwindow(WebKitWebView *v, WebKitWebFrame *f, gpointer d) { - /* TODO */ Client *c = newclient(); return c->view; } @@ -353,7 +378,7 @@ processx(GdkXEvent *e, GdkEvent *event, gpointer d) { if(((XEvent *)e)->type == PropertyNotify) { ev = &((XEvent *)e)->xproperty; if(ignore_once == FALSE && ev->atom == urlprop && ev->state == PropertyNewValue) { - XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING, + XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING, &adummy, &idummy, &ldummy, &ldummy, &buf); loaduri(c, (gchar *)buf); XFree(buf); @@ -408,8 +433,10 @@ updatetitle(Client *c) { int main(int argc, char *argv[]) { gchar *uri = NULL, *file = NULL; + SoupSession *s; Client *c; int o; + const gchar *home, *filename; gtk_init(NULL, NULL); if (!g_thread_supported()) @@ -451,6 +478,14 @@ int main(int argc, char *argv[]) { goto argerr; if(!clients) newclient(); + + /* cookie persistance */ + s = webkit_get_default_session(); + home = g_get_home_dir(); + filename = g_build_filename(home, ".surf-cookies", NULL); + cookiejar = soup_cookie_jar_text_new(filename, FALSE); + soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar)); + gtk_main(); cleanup(); return EXIT_SUCCESS;