5e3fc12637b47f102835c61d18034e44bbbf9238
[surf.git] / surf.c
1 /* See LICENSE file for copyright and license details.
2  *
3  * To understand surf, start reading main().
4  */
5 #include <X11/X.h>
6 #include <X11/Xatom.h>
7 #include <gtk/gtk.h>
8 #include <gdk/gdkx.h>
9 #include <gdk/gdk.h>
10 #include <gdk/gdkkeysyms.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <getopt.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <webkit/webkit.h>
17 #include <glib/gstdio.h>
18
19 #define LENGTH(x) (sizeof x / sizeof x[0])
20 #define CLEANMASK(mask)         (mask & ~(GDK_MOD2_MASK))
21
22 Display *dpy;
23 Atom urlprop;
24 typedef union Arg Arg;
25 union Arg {
26         const gboolean b;
27         const gint i;
28         const void *v;
29 } ;
30
31 typedef struct Client {
32         GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar, *indicator;
33         WebKitWebView *view;
34         WebKitDownload *download;
35         gchar *title;
36         gint progress;
37         struct Client *next;
38 } Client;
39
40 typedef struct Cookie {
41         char *name;
42         char *value;
43         char *domain;
44         char *path;
45         struct Cookie *next;
46 } Cookie;
47
48 typedef enum {
49     BROWSER = 0x0001,
50     SEARCHBAR = 0x0010,
51     URLBAR = 0x0100,
52     ALWAYS = ~0,
53 } KeyFocus;
54
55 typedef struct {
56         guint mod;
57         guint keyval;
58         void (*func)(Client *c, const Arg *arg);
59         const Arg arg;
60         KeyFocus focus;
61 } Key;
62
63 SoupCookieJar *cookiejar;
64 SoupSession *session;
65 Client *clients = NULL;
66 Cookie *cookies = NULL;
67 gboolean embed = FALSE;
68 gboolean showxid = FALSE;
69 gboolean ignore_once = FALSE;
70 extern char *optarg;
71 extern gint optind;
72
73 static void cleanup(void);
74 static void clipboard(Client *c, const Arg *arg);
75 static void destroyclient(Client *c);
76 static void destroywin(GtkWidget* w, Client *c);
77 static void die(char *str);
78 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
79 static void drawindicator(Client *c);
80 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
81 static gboolean initdownload(WebKitWebView *view, WebKitDownload *o, Client *c);
82 static gchar *geturi(Client *c);
83 static void hidesearch(Client *c, const Arg *arg);
84 static void hideurl(Client *c, const Arg *arg);
85 static gboolean keypress(GtkWidget* w, GdkEventKey *ev, Client *c);
86 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c);
87 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c);
88 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c);
89 static void loadfile(Client *c, const gchar *f);
90 static void loaduri(Client *c, const Arg *arg);
91 static void navigate(Client *c, const Arg *arg);
92 static Client *newclient();
93 static WebKitWebView *newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c);
94 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
95 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
96 static void print(Client *c, const Arg *arg);
97 static void proccookies(SoupMessage *m, Client *c);
98 static void progresschange(WebKitWebView *view, gint p, Client *c);
99 static void request(SoupSession *s, SoupMessage *m, Client *c);
100 static void reload(Client *c, const Arg *arg);
101 static void rereadcookies();
102 static void setcookie(char *name, char *val, char *dom, char *path, long exp);
103 static void setup();
104 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame,
105                 const gchar* title, Client *c);
106 static void scroll(Client *c, const Arg *arg);
107 static void searchtext(Client *c, const Arg *arg);
108 static void source(Client *c, const Arg *arg);
109 static void showsearch(Client *c, const Arg *arg);
110 static void showurl(Client *c, const Arg *arg);
111 static void stop(Client *c, const Arg *arg);
112 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
113 static void usage();
114 static void update(Client *c, const gchar *title);
115 static void zoom(Client *c, const Arg *arg);
116
117 #include "config.h"
118
119 void
120 cleanup(void) {
121         while(clients)
122                 destroyclient(clients);
123 }
124
125 void
126 clipboard(Client *c, const Arg *arg) {
127         gboolean paste = *(gboolean *)arg;
128         if(paste)
129                 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
130         else
131                 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
132 }
133
134 void
135 destroyclient(Client *c) {
136         Client *p;
137
138         gtk_widget_destroy(GTK_WIDGET(webkit_web_view_new()));
139         gtk_widget_destroy(c->scroll);
140         gtk_widget_destroy(c->urlbar);
141         gtk_widget_destroy(c->searchbar);
142         gtk_widget_destroy(c->vbox);
143         gtk_widget_destroy(c->win);
144         for(p = clients; p && p->next != c; p = p->next);
145         if(p)
146                 p->next = c->next;
147         else
148                 clients = c->next;
149         free(c);
150         if(clients == NULL)
151                 gtk_main_quit();
152 }
153
154 void
155 destroywin(GtkWidget* w, Client *c) {
156         destroyclient(c);
157 }
158
159 void
160 die(char *str) {
161         fputs(str, stderr);
162         exit(EXIT_FAILURE);
163 }
164
165 void
166 drawindicator(Client *c) {
167         gint width;
168         gchar *uri;
169         GtkWidget *w;
170         GdkGC *gc;
171         GdkColor fg;
172
173         uri = geturi(c);
174         w = c->indicator;
175         width = c->progress * w->allocation.width / 100;
176         gc = gdk_gc_new(w->window);
177         gdk_color_parse(strstr(uri, "https://") == uri ?
178                         progress_trust : progress, &fg);
179         gdk_gc_set_rgb_fg_color(gc, &fg);
180         gdk_draw_rectangle(w->window,
181                         w->style->bg_gc[GTK_WIDGET_STATE(w)],
182                         TRUE, 0, 0, w->allocation.width, w->allocation.height);
183         gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
184                         w->allocation.height);
185         g_object_unref(gc);/*g_free(gc);*/
186 }
187
188 gboolean
189 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
190         drawindicator(c);
191         return TRUE;
192 }
193
194 void
195 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
196         WebKitDownloadStatus status;
197
198         status = webkit_download_get_status(c->download);
199         if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
200                 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
201         }
202         update(c, NULL);
203 }
204
205 gboolean
206 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
207         const gchar *home, *filename;
208         gchar *uri, *path, *html;
209
210         stop(c, NULL);
211         c->download = o;
212         home = g_get_home_dir();
213         filename = webkit_download_get_suggested_filename(o);
214         path = g_build_filename(home, ".surf", "dl", 
215                         filename, NULL);
216         uri = g_strconcat("file://", path, NULL);
217         webkit_download_set_destination_uri(c->download, uri);
218         c->progress = 0;
219         g_free(uri);
220         html = g_strdup_printf("Download <b>%s</b>...", filename);
221         webkit_web_view_load_html_string(c->view, html,
222                         webkit_download_get_uri(c->download));
223         g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
224         g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
225         webkit_download_start(c->download);
226         update(c, filename);
227         g_free(html);
228         return TRUE;
229 }
230
231 gchar *
232 geturi(Client *c) {
233         gchar *uri;
234
235         if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
236                 uri = g_strdup("about:blank");
237         return uri;
238 }
239
240 void
241 hidesearch(Client *c, const Arg *arg) {
242         gtk_widget_hide(c->searchbar);
243         gtk_widget_grab_focus(GTK_WIDGET(c->view));
244 }
245
246 void
247 hideurl(Client *c, const Arg *arg) {
248         gtk_widget_hide(c->urlbar);
249         gtk_widget_grab_focus(GTK_WIDGET(c->view));
250 }
251
252 gboolean
253 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
254         guint i, focus;
255         gboolean processed = FALSE;
256
257         if(ev->type != GDK_KEY_PRESS)
258                 return FALSE;
259         if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
260                 focus = SEARCHBAR;
261         else if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
262                 focus = URLBAR;
263         else
264                 focus = BROWSER;
265         for(i = 0; i < LENGTH(keys); i++) {
266                 if(focus & keys[i].focus && ev->keyval == keys[i].keyval &&
267                                 CLEANMASK(ev->state) == keys[i].mod
268                                 && keys[i].func) {
269                         keys[i].func(c, &(keys[i].arg));
270                         processed = TRUE;
271                 }
272         }
273         return processed;
274 }
275
276 void
277 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c) {
278         if(l)
279                 gtk_window_set_title(GTK_WINDOW(c->win), l);
280         else
281                 update(c, NULL);
282 }
283
284 void
285 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
286         gchar *uri;
287
288         ignore_once = TRUE;
289         uri = geturi(c);
290         XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
291                         XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
292                         strlen(uri) + 1);
293 }
294
295 void
296 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
297         c->progress = 0;
298         update(c, NULL);
299 }
300
301 void
302 loadfile(Client *c, const gchar *f) {
303         GIOChannel *chan = NULL;
304         GError *e = NULL;
305         GString *code;
306         gchar *line, *uri;
307         Arg arg;
308
309         if(strcmp(f, "-") == 0) {
310                 chan = g_io_channel_unix_new(STDIN_FILENO);
311                 if (chan) {
312                         code = g_string_new("");
313                         while(g_io_channel_read_line(chan, &line, NULL, NULL,
314                                                 &e) == G_IO_STATUS_NORMAL) {
315                                 g_string_append(code, line);
316                                 g_free(line);
317                         }
318                         webkit_web_view_load_html_string(c->view, code->str,
319                                         "file://.");
320                         g_io_channel_shutdown(chan, FALSE, NULL);
321                         g_string_free(code, TRUE);
322                 }
323                 arg.v = uri = g_strdup("stdin");
324         }
325         else {
326                 arg.v = uri = g_strdup_printf("file://%s", f);
327                 loaduri(c, &arg);
328         }
329         update(c, uri);
330         g_free(uri);
331 }
332
333 void
334 loaduri(Client *c, const Arg *arg) {
335         gchar *u;
336         const gchar *uri = (gchar *)arg->v;
337         if(!uri)
338                 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
339         u = g_strrstr(uri, "://") ? g_strdup(uri)
340                 : g_strdup_printf("http://%s", uri);
341         webkit_web_view_load_uri(c->view, u);
342         c->progress = 0;
343         update(c, u);
344         g_free(u);
345 }
346
347 void
348 navigate(Client *c, const Arg *arg) {
349         gint steps = *(gint *)arg;
350         webkit_web_view_go_back_or_forward(c->view, steps);
351 }
352
353 Client *
354 newclient(void) {
355         Client *c;
356         if(!(c = calloc(1, sizeof(Client))))
357                 die("Cannot malloc!\n");
358         /* Window */
359         if(embed) {
360                 c->win = gtk_plug_new(0);
361         }
362         else {
363                 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
364                 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
365         }
366         gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
367         g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
368         g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
369
370         /* VBox */
371         c->vbox = gtk_vbox_new(FALSE, 0);
372
373         /* scrolled window */
374         c->scroll = gtk_scrolled_window_new(NULL, NULL);
375         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
376                         GTK_POLICY_NEVER, GTK_POLICY_NEVER);
377
378         /* webview */
379         c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
380         g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
381         g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
382         g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
383         g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
384         g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
385         g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(newwindow), c);
386         g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
387         g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
388
389         /* urlbar */
390         c->urlbar = gtk_entry_new();
391         gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
392
393         /* searchbar */
394         c->searchbar = gtk_entry_new();
395         gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
396
397         /* indicator */
398         c->indicator = gtk_drawing_area_new();
399         gtk_widget_set_size_request(c->indicator, 0, 2);
400         g_signal_connect (G_OBJECT (c->indicator), "expose_event",
401                         G_CALLBACK (exposeindicator), c);
402
403         /* downloadbar */
404
405         /* Arranging */
406         gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
407         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
408         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
409         gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
410         gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
411         gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
412
413         /* Setup */
414         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
415         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
416         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
417         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
418         gtk_widget_grab_focus(GTK_WIDGET(c->view));
419         gtk_widget_hide_all(c->searchbar);
420         gtk_widget_hide_all(c->urlbar);
421         gtk_widget_show(c->vbox);
422         gtk_widget_show(c->indicator);
423         gtk_widget_show(c->scroll);
424         gtk_widget_show(GTK_WIDGET(c->view));
425         gtk_widget_show(c->win);
426         gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
427         gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
428         webkit_web_view_set_full_content_zoom(c->view, TRUE);
429         c->download = NULL;
430         c->title = NULL;
431         c->next = clients;
432         clients = c;
433         if(showxid) {
434                 gdk_display_sync(gtk_widget_get_display(c->win));
435                 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
436                 fflush(NULL);
437         }
438         return c;
439 }
440
441 WebKitWebView *
442 newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
443         Client *n = newclient();
444         return n->view;
445 }
446
447  
448 void
449 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
450         Arg arg = {.v = text };
451         if(text != NULL)
452                 loaduri((Client *) d, &arg);
453 }
454
455 GdkFilterReturn
456 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
457         Client *c = (Client *)d;
458         XPropertyEvent *ev;
459         Atom adummy;
460         gint idummy;
461         unsigned long ldummy;
462         unsigned char *buf = NULL;
463         Arg arg;
464
465         if(((XEvent *)e)->type == PropertyNotify) {
466                 ev = &((XEvent *)e)->xproperty;
467                 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
468                         if(ignore_once)
469                                ignore_once = FALSE;
470                         else {
471                                 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
472                                         &adummy, &idummy, &ldummy, &ldummy, &buf);
473                                 arg.v = buf;
474                                 loaduri(c, &arg);
475                                 XFree(buf);
476                         }
477                         return GDK_FILTER_REMOVE;
478                 }
479         }
480         return GDK_FILTER_CONTINUE;
481 }
482
483 void
484 print(Client *c, const Arg *arg) {
485         webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
486 }
487
488 void
489 proccookies(SoupMessage *m, Client *c) {
490         GSList *l;
491         SoupCookie *co;
492         long t;
493
494         rereadcookies();
495         for (l = soup_cookies_from_response(m); l; l = l->next){
496                 co = (SoupCookie *)l->data;
497                 t = co->expires ?  soup_date_to_time_t(co->expires) : 0;
498                 setcookie(co->name, co->value, co->domain, co->value, t);
499         }
500         g_slist_free(l);
501 }
502
503 void
504 progresschange(WebKitWebView* view, gint p, Client *c) {
505         c->progress = p;
506         update(c, NULL);
507 }
508
509 void
510 request(SoupSession *s, SoupMessage *m, Client *c) {
511         soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
512                         G_CALLBACK(proccookies), c);
513 }
514
515 void
516 reload(Client *c, const Arg *arg) {
517         gboolean nocache = *(gboolean *)arg;
518         if(nocache)
519                  webkit_web_view_reload_bypass_cache(c->view);
520         else
521                  webkit_web_view_reload(c->view);
522 }
523
524 void
525 rereadcookies() {
526         const gchar *filename, *home;
527
528         home = g_get_home_dir();
529         filename = g_build_filename(home, ".surf", "cookies", NULL);
530 }
531
532 void
533 scroll(Client *c, const Arg *arg) {
534         gdouble v;
535         GtkAdjustment *a;
536
537         a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
538         v = gtk_adjustment_get_value(a);
539         v += gtk_adjustment_get_step_increment(a) * arg->i;
540         v = MAX(v, 0.0);
541         v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
542         gtk_adjustment_set_value(a, v);
543 }
544
545 void
546 setcookie(char *name, char *val, char *dom, char *path, long exp) {
547
548 }
549
550 void
551 setup() {
552         dpy = GDK_DISPLAY();
553         session = webkit_get_default_session();
554         urlprop = XInternAtom(dpy, "_SURF_URL", False);
555 }
556
557 void
558 showsearch(Client *c, const Arg *arg) {
559         hideurl(c, NULL);
560         gtk_widget_show(c->searchbar);
561         gtk_widget_grab_focus(c->searchbar);
562 }
563
564 void
565 source(Client *c, const Arg *arg) {
566         Arg a = { .b = FALSE };
567         /*gboolean s;
568
569         s = webkit_web_view_get_view_source_mode(c->view);
570         webkit_web_view_set_view_source_mode(c->view, c->source);*/
571         reload(c, &a);
572 }
573
574 void
575 searchtext(Client *c, const Arg *arg) {
576         gboolean forward = *(gboolean *)arg;
577         webkit_web_view_search_text(c->view,
578                         gtk_entry_get_text(GTK_ENTRY(c->searchbar)),
579                         FALSE,
580                         forward,
581                         TRUE);
582 }
583
584 void
585 showurl(Client *c, const Arg *arg) {
586         gchar *uri;
587
588         hidesearch(c, NULL);
589         uri = geturi(c);
590         gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
591         gtk_widget_show(c->urlbar);
592         gtk_widget_grab_focus(c->urlbar);
593 }
594
595 void
596 stop(Client *c, const Arg *arg) {
597         if(c->download)
598                 webkit_download_cancel(c->download);
599         else
600                 webkit_web_view_stop_loading(c->view);
601         c->download = NULL;
602 }
603
604 void
605 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
606         update(c, t);
607 }
608
609 void
610 usage() {
611         fputs("surf - simple browser\n", stderr);
612         die("usage: surf [-e] [-x] [uri]\n");
613 }
614
615 void
616 update(Client *c, const char *title) {
617         gchar *t;
618
619         if(title) {
620                 if(c->title)
621                         g_free(c->title);
622                 c->title = g_strdup(title);
623         }
624         if(c->progress == 100)
625                 t = g_strdup(c->title);
626         else
627                 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
628         drawindicator(c);
629         gtk_window_set_title(GTK_WINDOW(c->win), t);
630         g_free(t);
631
632 }
633
634 void
635 zoom(Client *c, const Arg *arg) {
636         if(arg->i < 0)          /* zoom out */
637                 webkit_web_view_zoom_out(c->view);
638         else if(arg->i > 0)     /* zoom in */
639                 webkit_web_view_zoom_in(c->view);
640         else                    /* reset */
641                 webkit_web_view_set_zoom_level(c->view, 1.0);
642 }
643
644 int main(int argc, char *argv[]) {
645         SoupSession *s;
646         Client *c;
647         gint o;
648         const gchar *home, *filename;
649         Arg arg;
650
651         gtk_init(NULL, NULL);
652         if (!g_thread_supported())
653                 g_thread_init(NULL);
654         setup();
655         while((o = getopt(argc, argv, "vhxeu:f:")) != -1)
656                 switch(o) {
657                 case 'x':
658                         showxid = TRUE;
659                         break;
660                 case 'e':
661                         showxid = TRUE;
662                         embed = TRUE;
663                         break;
664                 case 'v':
665                         die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
666                         break;
667                 default:
668                         usage();
669                 }
670         if(optind + 1 == argc) {
671                 c = newclient();
672                 arg.v = argv[optind];
673                 if(strchr("./", argv[optind][0]) || strcmp("-", argv[optind]) == 0)
674                         loadfile(c, argv[optind]);
675                 else
676                         loaduri(c, &arg);
677
678         }
679         else if(optind != argc)
680                 usage();
681         if(!clients)
682                 newclient();
683
684         /* make dirs */
685         home = g_get_home_dir();
686         filename = g_build_filename(home, ".surf", "dl", NULL);
687         g_mkdir_with_parents(filename, 0755);
688
689         /* cookie persistance */
690         s = webkit_get_default_session();
691         filename = g_build_filename(home, ".surf", "cookies.jar", NULL);
692         cookiejar = soup_cookie_jar_text_new(filename, FALSE);
693         soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
694
695         gtk_main();
696         cleanup();
697         return EXIT_SUCCESS;
698 }