fixing downloads based on Evan Gates' patch.
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 50c3be0..50b98dc 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -76,9 +76,10 @@ static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkR
 static void destroyclient(Client *c);
 static void destroywin(GtkWidget* w, Client *c);
 static void die(char *str);
-static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
+static void download(Client *c, const Arg *arg);
 static void drawindicator(Client *c);
 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
+static char *geturi(Client *c);
 static const char *getatom(Client *c, Atom a);
 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
 static void itemclick(GtkMenuItem *mi, Client *c);
@@ -109,6 +110,7 @@ static void stop(Client *c, const Arg *arg);
 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
 static void usage(void);
 static void update(Client *c);
+static void updatedownload(WebKitDownload *o, GParamSpec *pspec, Client *c);
 static void updatewinid(Client *c);
 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
 static void zoom(Client *c, const Arg *arg);
@@ -175,7 +177,7 @@ clipboard(Client *c, const Arg *arg) {
        if(paste)
                gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
        else
-               gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
+               gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
 }
 
 void
@@ -230,6 +232,15 @@ destroyclient(Client *c) {
                gtk_main_quit();
 }
 
+gboolean
+mime_policy_cb(WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, gchar *mime_type,  WebKitWebPolicyDecision *policy_decision, gpointer user_data) {
+       if(webkit_web_view_can_show_mime_type(web_view, mime_type))
+               webkit_web_policy_decision_use(policy_decision);
+       else
+               webkit_web_policy_decision_download(policy_decision);
+       return TRUE;
+}
+
 gboolean
 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
        Arg arg;
@@ -284,14 +295,18 @@ exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
 }
 
 void
-download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
-       WebKitDownloadStatus status;
+download(Client *c, const Arg *arg) {
+       char *uri;
+       WebKitNetworkRequest *r;
+       WebKitDownload       *dl;
 
-       status = webkit_download_get_status(c->download);
-       if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
-               c->progress = (gint)(webkit_download_get_progress(c->download)*100);
-       }
-       update(c);
+       if(arg->v)
+               uri = (char *)arg->v;
+       else
+               uri = c->linkhover ? c->linkhover : geturi(c);
+       r = webkit_network_request_new(uri);
+       dl = webkit_download_new(r);
+       initdownload(c->view, dl, c);
 }
 
 const char *
@@ -321,6 +336,8 @@ initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
        stop(c, NULL);
        c->download = o;
        filename = webkit_download_get_suggested_filename(o);
+       if(!strcmp("", filename))
+               filename = "index.html";
        uri = g_strconcat("file://", dldir, "/", filename, NULL);
        webkit_download_set_destination_uri(c->download, uri);
        c->progress = 0;
@@ -328,8 +345,8 @@ initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
        html = g_strdup_printf("Download <b>%s</b>...", filename);
        webkit_web_view_load_html_string(c->view, html,
                        webkit_download_get_uri(c->download));
-       g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
-       g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
+       g_signal_connect(c->download, "notify::progress", G_CALLBACK(updatedownload), c);
+       g_signal_connect(c->download, "notify::status", G_CALLBACK(updatedownload), c);
        webkit_download_start(c->download);
        
        c->title = copystr(&c->title, filename);
@@ -388,14 +405,14 @@ linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
 
 void
 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
-       setatom(c, uriprop, webkit_web_view_get_uri(c->view));
-       reloadcookies();
+       setatom(c, uriprop, geturi(c));
 }
 
 void
 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
        c->progress = 0;
        update(c);
+       reloadcookies();
 }
 
 void
@@ -423,6 +440,7 @@ newclient(void) {
        int i;
        Client *c;
        WebKitWebSettings *settings;
+       GdkGeometry hints = { 1, 1 };
        char *uri;
 
        if(!(c = calloc(1, sizeof(Client))))
@@ -492,6 +510,7 @@ newclient(void) {
        gtk_widget_show(c->scroll);
        gtk_widget_show(GTK_WIDGET(c->view));
        gtk_widget_show(c->win);
+       gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
        gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
        gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
        webkit_web_view_set_full_content_zoom(c->view, TRUE);
@@ -750,7 +769,17 @@ update(Client *c) {
        drawindicator(c);
        gtk_window_set_title(GTK_WINDOW(c->win), t);
        g_free(t);
+}
+
+void
+updatedownload(WebKitDownload *o, GParamSpec *pspec, Client *c) {
+       WebKitDownloadStatus status;
 
+       status = webkit_download_get_status(c->download);
+       if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
+               c->progress = (gint)(webkit_download_get_progress(c->download)*100);
+       }
+       update(c);
 }
 
 void