Adding a WebKit-based backend to the download utility
This commit is contained in:
parent
5bdd1ee462
commit
e27c882c39
@ -30,7 +30,7 @@ surfer_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) -L $(PREFIX)/lib -Wl,-rpath,$(PREFIX)/li
|
|||||||
surfer: $(surfer_OBJS)
|
surfer: $(surfer_OBJS)
|
||||||
$(CC) -o surfer $(surfer_OBJS) $(surfer_LDFLAGS)
|
$(CC) -o surfer $(surfer_OBJS) $(surfer_LDFLAGS)
|
||||||
|
|
||||||
download.o: download.c
|
download.o: download.c ../config.h
|
||||||
$(CC) $(download_CFLAGS) -c download.c
|
$(CC) $(download_CFLAGS) -c download.c
|
||||||
|
|
||||||
surfer.o: surfer.c callbacks.h surfer.h ../config.h
|
surfer.o: surfer.c callbacks.h surfer.h ../config.h
|
||||||
|
@ -23,8 +23,12 @@
|
|||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
#define GNET_EXPERIMENTAL
|
#ifdef WITH_WEBKIT
|
||||||
#include <gnet.h>
|
# include <webkit/webkit.h>
|
||||||
|
#else
|
||||||
|
# define GNET_EXPERIMENTAL
|
||||||
|
# include <gnet.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* Download */
|
/* Download */
|
||||||
@ -43,7 +47,11 @@ typedef struct _Download
|
|||||||
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
|
#ifdef WITH_WEBKIT
|
||||||
|
WebKitDownload * conn;
|
||||||
|
#else
|
||||||
GConnHttp * conn;
|
GConnHttp * conn;
|
||||||
|
#endif
|
||||||
guint64 content_length;
|
guint64 content_length;
|
||||||
guint64 data_received;
|
guint64 data_received;
|
||||||
|
|
||||||
@ -64,14 +72,18 @@ static int _download(Prefs * prefs, char const * url);
|
|||||||
static int _download_cancel(Download * download);
|
static int _download_cancel(Download * download);
|
||||||
static int _download_error(Download * download, char const * message, int ret);
|
static int _download_error(Download * download, char const * message, int ret);
|
||||||
static void _download_refresh(Download * download);
|
static void _download_refresh(Download * download);
|
||||||
|
#ifndef WITH_WEBKIT
|
||||||
static int _download_write(Download * download);
|
static int _download_write(Download * download);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* callbacks */
|
/* callbacks */
|
||||||
static void _download_on_cancel(GtkWidget * widget, gpointer data);
|
static void _download_on_cancel(gpointer data);
|
||||||
static gboolean _download_on_closex(GtkWidget * widget, GdkEvent * event,
|
static gboolean _download_on_closex(GtkWidget * widget, GdkEvent * event,
|
||||||
gpointer data);
|
gpointer data);
|
||||||
|
#ifndef WITH_WEBKIT
|
||||||
static void _download_on_http(GConnHttp * conn, GConnHttpEvent * event,
|
static void _download_on_http(GConnHttp * conn, GConnHttpEvent * event,
|
||||||
gpointer data);
|
gpointer data);
|
||||||
|
#endif
|
||||||
static gboolean _download_on_idle(gpointer data);
|
static gboolean _download_on_idle(gpointer data);
|
||||||
static gboolean _download_on_timeout(gpointer data);
|
static gboolean _download_on_timeout(gpointer data);
|
||||||
|
|
||||||
@ -123,8 +135,8 @@ static int _download(Prefs * prefs, char const * url)
|
|||||||
/* button */
|
/* button */
|
||||||
hbox = gtk_hbox_new(FALSE, 0);
|
hbox = gtk_hbox_new(FALSE, 0);
|
||||||
download.cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
|
download.cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
|
||||||
g_signal_connect(G_OBJECT(download.cancel), "clicked", G_CALLBACK(
|
g_signal_connect_swapped(G_OBJECT(download.cancel), "clicked",
|
||||||
_download_on_cancel), &download);
|
G_CALLBACK(_download_on_cancel), &download);
|
||||||
gtk_box_pack_end(GTK_BOX(hbox), download.cancel, FALSE, TRUE, 0);
|
gtk_box_pack_end(GTK_BOX(hbox), download.cancel, FALSE, TRUE, 0);
|
||||||
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
|
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
|
||||||
gtk_container_set_border_width(GTK_CONTAINER(download.window), 4);
|
gtk_container_set_border_width(GTK_CONTAINER(download.window), 4);
|
||||||
@ -164,9 +176,11 @@ static int _download_cancel(Download * download)
|
|||||||
|
|
||||||
if(download->fp != NULL)
|
if(download->fp != NULL)
|
||||||
{
|
{
|
||||||
|
#ifndef WITH_WEBKIT
|
||||||
if(fclose(download->fp) != 0)
|
if(fclose(download->fp) != 0)
|
||||||
ret |= _download_error(download,
|
ret |= _download_error(download,
|
||||||
download->prefs->output, 1);
|
download->prefs->output, 1);
|
||||||
|
#endif
|
||||||
if(unlink(download->prefs->output) != 0)
|
if(unlink(download->prefs->output) != 0)
|
||||||
ret |= _download_error(download,
|
ret |= _download_error(download,
|
||||||
download->prefs->output, 1);
|
download->prefs->output, 1);
|
||||||
@ -245,6 +259,7 @@ static void _download_refresh(Download * download)
|
|||||||
|
|
||||||
|
|
||||||
/* download_write */
|
/* download_write */
|
||||||
|
#ifndef WITH_WEBKIT
|
||||||
static int _download_write(Download * download)
|
static int _download_write(Download * download)
|
||||||
{
|
{
|
||||||
gchar * buf;
|
gchar * buf;
|
||||||
@ -265,11 +280,12 @@ static int _download_write(Download * download)
|
|||||||
_download_cancel(download);
|
_download_cancel(download);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* callbacks */
|
/* callbacks */
|
||||||
/* download_on_cancel */
|
/* download_on_cancel */
|
||||||
static void _download_on_cancel(GtkWidget * widget, gpointer data)
|
static void _download_on_cancel(gpointer data)
|
||||||
{
|
{
|
||||||
Download * download = data;
|
Download * download = data;
|
||||||
|
|
||||||
@ -291,6 +307,7 @@ static gboolean _download_on_closex(GtkWidget * widget, GdkEvent * event,
|
|||||||
|
|
||||||
|
|
||||||
/* download_on_http */
|
/* download_on_http */
|
||||||
|
#ifndef WITH_WEBKIT
|
||||||
static void _http_connected(Download * download);
|
static void _http_connected(Download * download);
|
||||||
static void _http_error(GConnHttpEventError * event, Download * download);
|
static void _http_error(GConnHttpEventError * event, Download * download);
|
||||||
static void _http_data_complete(GConnHttpEventData * event,
|
static void _http_data_complete(GConnHttpEventData * event,
|
||||||
@ -418,6 +435,7 @@ static void _http_timeout(Download * download)
|
|||||||
gtk_label_set_text(GTK_LABEL(download->status), "Timeout");
|
gtk_label_set_text(GTK_LABEL(download->status), "Timeout");
|
||||||
/* FIXME implement */
|
/* FIXME implement */
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* download_on_idle */
|
/* download_on_idle */
|
||||||
@ -425,6 +443,9 @@ static gboolean _download_on_idle(gpointer data)
|
|||||||
{
|
{
|
||||||
Download * download = data;
|
Download * download = data;
|
||||||
Prefs * prefs = download->prefs;
|
Prefs * prefs = download->prefs;
|
||||||
|
#ifdef WITH_WEBKIT
|
||||||
|
WebKitNetworkRequest * request;
|
||||||
|
#endif
|
||||||
|
|
||||||
if((download->fp = fopen(prefs->output, "w")) == NULL)
|
if((download->fp = fopen(prefs->output, "w")) == NULL)
|
||||||
{
|
{
|
||||||
@ -433,6 +454,13 @@ static gboolean _download_on_idle(gpointer data)
|
|||||||
gtk_main_quit();
|
gtk_main_quit();
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
#ifdef WITH_WEBKIT
|
||||||
|
request = webkit_network_request_new(download->url);
|
||||||
|
download->conn = webkit_download_new(request);
|
||||||
|
webkit_download_set_destination_uri(download->conn,
|
||||||
|
download->prefs->output);
|
||||||
|
webkit_download_start(download->conn);
|
||||||
|
#else
|
||||||
download->conn = gnet_conn_http_new();
|
download->conn = gnet_conn_http_new();
|
||||||
if(gnet_conn_http_set_method(download->conn, GNET_CONN_HTTP_METHOD_GET,
|
if(gnet_conn_http_set_method(download->conn, GNET_CONN_HTTP_METHOD_GET,
|
||||||
NULL, 0) != TRUE)
|
NULL, 0) != TRUE)
|
||||||
@ -442,6 +470,7 @@ static gboolean _download_on_idle(gpointer data)
|
|||||||
gnet_conn_http_set_user_agent(download->conn,
|
gnet_conn_http_set_user_agent(download->conn,
|
||||||
prefs->user_agent);
|
prefs->user_agent);
|
||||||
gnet_conn_http_run_async(download->conn, _download_on_http, download);
|
gnet_conn_http_run_async(download->conn, _download_on_http, download);
|
||||||
|
#endif
|
||||||
download->timeout = g_timeout_add(250, _download_on_timeout, download);
|
download->timeout = g_timeout_add(250, _download_on_timeout, download);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
@ -451,7 +480,23 @@ static gboolean _download_on_idle(gpointer data)
|
|||||||
static gboolean _download_on_timeout(gpointer data)
|
static gboolean _download_on_timeout(gpointer data)
|
||||||
{
|
{
|
||||||
Download * download = data;
|
Download * download = data;
|
||||||
|
#ifdef WITH_WEBKIT
|
||||||
|
WebKitDownloadStatus status;
|
||||||
|
|
||||||
|
/* FIXME not very efficient */
|
||||||
|
download->content_length = webkit_download_get_total_size(
|
||||||
|
download->conn);
|
||||||
|
download->content_length = webkit_download_get_current_size(
|
||||||
|
download->conn);
|
||||||
|
status = webkit_download_get_status(download->conn);
|
||||||
|
switch(status)
|
||||||
|
{
|
||||||
|
case WEBKIT_DOWNLOAD_STATUS_ERROR:
|
||||||
|
gtk_label_set_text(GTK_LABEL(download->status),
|
||||||
|
"Error");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
_download_refresh(download);
|
_download_refresh(download);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,17 @@ dist=Makefile,callbacks.h,ghtml.h,surfer.h,ghtml-gtkhtml.c,ghtml-gtkmozembed.c,g
|
|||||||
|
|
||||||
[download]
|
[download]
|
||||||
type=binary
|
type=binary
|
||||||
|
#cppflags=-D WITH_WEBKIT
|
||||||
|
#cflags=`pkg-config --cflags gtk+-2.0 webkit-1.0`
|
||||||
|
#ldflags=-L $(PREFIX)/lib -Wl,-rpath,$(PREFIX)/lib -lSystem `pkg-config --libs gtk+-2.0 webkit-1.0`
|
||||||
cflags=`pkg-config --cflags gtk+-2.0 gnet-2.0`
|
cflags=`pkg-config --cflags gtk+-2.0 gnet-2.0`
|
||||||
ldflags=`pkg-config --libs gtk+-2.0 gnet-2.0`
|
ldflags=`pkg-config --libs gtk+-2.0 gnet-2.0`
|
||||||
sources=download.c
|
sources=download.c
|
||||||
install=$(BINDIR)
|
install=$(BINDIR)
|
||||||
|
|
||||||
|
[download.c]
|
||||||
|
depends=../config.h
|
||||||
|
|
||||||
[surfer]
|
[surfer]
|
||||||
type=binary
|
type=binary
|
||||||
#cflags=`pkg-config --cflags gtk+-2.0 firefox-gtkmozembed`
|
#cflags=`pkg-config --cflags gtk+-2.0 firefox-gtkmozembed`
|
||||||
|
Loading…
Reference in New Issue
Block a user