More code re-use and error checking
This commit is contained in:
parent
aa44e0103d
commit
298672abeb
103
src/download.c
103
src/download.c
@ -51,6 +51,7 @@ typedef struct _Download
|
|||||||
GtkWidget * status;
|
GtkWidget * status;
|
||||||
GtkWidget * speed;
|
GtkWidget * speed;
|
||||||
GtkWidget * progress;
|
GtkWidget * progress;
|
||||||
|
GtkWidget * cancel;
|
||||||
|
|
||||||
guint timeout;
|
guint timeout;
|
||||||
int pulse;
|
int pulse;
|
||||||
@ -59,8 +60,10 @@ typedef struct _Download
|
|||||||
|
|
||||||
/* prototypes */
|
/* prototypes */
|
||||||
static int _download(Prefs * prefs, char const * url);
|
static int _download(Prefs * prefs, char const * url);
|
||||||
|
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);
|
||||||
|
static int _download_write(Download * download);
|
||||||
|
|
||||||
/* callbacks */
|
/* callbacks */
|
||||||
static void _download_on_cancel(GtkWidget * widget, gpointer data);
|
static void _download_on_cancel(GtkWidget * widget, gpointer data);
|
||||||
@ -79,7 +82,7 @@ static void _download_label(GtkWidget * vbox, PangoFontDescription * bold,
|
|||||||
|
|
||||||
static int _download(Prefs * prefs, char const * url)
|
static int _download(Prefs * prefs, char const * url)
|
||||||
{
|
{
|
||||||
static Download download;
|
Download download;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
GtkWidget * vbox;
|
GtkWidget * vbox;
|
||||||
GtkWidget * hbox;
|
GtkWidget * hbox;
|
||||||
@ -102,7 +105,7 @@ static int _download(Prefs * prefs, char const * url)
|
|||||||
snprintf(buf, sizeof(buf), "%s %s", "Download", url);
|
snprintf(buf, sizeof(buf), "%s %s", "Download", url);
|
||||||
gtk_window_set_title(GTK_WINDOW(download.window), buf);
|
gtk_window_set_title(GTK_WINDOW(download.window), buf);
|
||||||
g_signal_connect(G_OBJECT(download.window), "delete-event", G_CALLBACK(
|
g_signal_connect(G_OBJECT(download.window), "delete-event", G_CALLBACK(
|
||||||
_download_on_closex), NULL);
|
_download_on_closex), &download);
|
||||||
vbox = gtk_vbox_new(FALSE, 0);
|
vbox = gtk_vbox_new(FALSE, 0);
|
||||||
bold = pango_font_description_new();
|
bold = pango_font_description_new();
|
||||||
pango_font_description_set_weight(bold, PANGO_WEIGHT_BOLD);
|
pango_font_description_set_weight(bold, PANGO_WEIGHT_BOLD);
|
||||||
@ -117,12 +120,12 @@ static int _download(Prefs * prefs, char const * url)
|
|||||||
/* progress bar */
|
/* progress bar */
|
||||||
download.progress = gtk_progress_bar_new();
|
download.progress = gtk_progress_bar_new();
|
||||||
gtk_box_pack_start(GTK_BOX(vbox), download.progress, TRUE, TRUE, 4);
|
gtk_box_pack_start(GTK_BOX(vbox), download.progress, TRUE, TRUE, 4);
|
||||||
/* cancel button */
|
/* button */
|
||||||
hbox = gtk_hbox_new(FALSE, 4);
|
hbox = gtk_hbox_new(FALSE, 0);
|
||||||
widget = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
|
download.cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
|
||||||
g_signal_connect(G_OBJECT(widget), "clicked", G_CALLBACK(
|
g_signal_connect(G_OBJECT(download.cancel), "clicked", G_CALLBACK(
|
||||||
_download_on_cancel), &download);
|
_download_on_cancel), &download);
|
||||||
gtk_box_pack_end(GTK_BOX(hbox), widget, FALSE, FALSE, 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);
|
||||||
gtk_container_add(GTK_CONTAINER(download.window), vbox);
|
gtk_container_add(GTK_CONTAINER(download.window), vbox);
|
||||||
@ -153,6 +156,26 @@ static void _download_label(GtkWidget * vbox, PangoFontDescription * bold,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* download_cancel */
|
||||||
|
static int _download_cancel(Download * download)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
if(download->fp != NULL)
|
||||||
|
{
|
||||||
|
if(fclose(download->fp) != 0)
|
||||||
|
ret |= _download_error(download,
|
||||||
|
download->prefs->output, 1);
|
||||||
|
if(unlink(download->prefs->output) != 0)
|
||||||
|
ret |= _download_error(download,
|
||||||
|
download->prefs->output, 1);
|
||||||
|
download->fp = NULL;
|
||||||
|
}
|
||||||
|
gtk_main_quit();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* download_error */
|
/* download_error */
|
||||||
static int _download_error(Download * download, char const * message, int ret)
|
static int _download_error(Download * download, char const * message, int ret)
|
||||||
{
|
{
|
||||||
@ -220,23 +243,48 @@ static void _download_refresh(Download * download)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* download_write */
|
||||||
|
static int _download_write(Download * download)
|
||||||
|
{
|
||||||
|
gchar * buf;
|
||||||
|
gsize size;
|
||||||
|
gsize s;
|
||||||
|
|
||||||
|
if(gnet_conn_http_steal_buffer(download->conn, &buf, &size) != TRUE)
|
||||||
|
return 0;
|
||||||
|
/* FIXME use a GIOChannel instead */
|
||||||
|
s = fwrite(buf, sizeof(*buf), size, download->fp);
|
||||||
|
g_free(buf);
|
||||||
|
if(s == size)
|
||||||
|
{
|
||||||
|
download->pulse = 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
_download_error(download, download->prefs->output, 0);
|
||||||
|
_download_cancel(download);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* callbacks */
|
/* callbacks */
|
||||||
|
/* download_on_cancel */
|
||||||
static void _download_on_cancel(GtkWidget * widget, gpointer data)
|
static void _download_on_cancel(GtkWidget * widget, gpointer data)
|
||||||
{
|
{
|
||||||
Download * download = data;
|
Download * download = data;
|
||||||
|
|
||||||
gtk_widget_hide(download->window);
|
gtk_widget_hide(download->window);
|
||||||
/* FIXME cleanup, unlink... */
|
_download_cancel(download);
|
||||||
gtk_main_quit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* download_on_closex */
|
||||||
static gboolean _download_on_closex(GtkWidget * widget, GdkEvent * event,
|
static gboolean _download_on_closex(GtkWidget * widget, GdkEvent * event,
|
||||||
gpointer data)
|
gpointer data)
|
||||||
{
|
{
|
||||||
|
Download * download = data;
|
||||||
|
|
||||||
gtk_widget_hide(widget);
|
gtk_widget_hide(widget);
|
||||||
/* FIXME cleanup, unlink... */
|
_download_cancel(download);
|
||||||
gtk_main_quit();
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,44 +350,29 @@ static void _http_error(GConnHttpEventError * event, Download * download)
|
|||||||
static void _http_data_complete(GConnHttpEventData * event,
|
static void _http_data_complete(GConnHttpEventData * event,
|
||||||
Download * download)
|
Download * download)
|
||||||
{
|
{
|
||||||
gchar * buf;
|
g_source_remove(download->timeout);
|
||||||
gsize size;
|
download->timeout = 0;
|
||||||
|
if(_download_write(download) != 0)
|
||||||
gtk_label_set_text(GTK_LABEL(download->status), "Complete");
|
return;
|
||||||
if(gnet_conn_http_steal_buffer(download->conn, &buf, &size))
|
|
||||||
{
|
|
||||||
/* FIXME use a GIOChannel instead, check errors */
|
|
||||||
fwrite(buf, sizeof(*buf), size, download->fp);
|
|
||||||
g_free(buf);
|
|
||||||
}
|
|
||||||
download->data_received = event->data_received;
|
download->data_received = event->data_received;
|
||||||
if(event->content_length == 0)
|
if(event->content_length == 0)
|
||||||
download->content_length = (event->content_length != 0)
|
download->content_length = (event->content_length != 0)
|
||||||
? event->content_length : event->data_received;
|
? event->content_length : event->data_received;
|
||||||
if(fclose(download->fp) != 0)
|
if(fclose(download->fp) != 0)
|
||||||
_download_error(download, download->prefs->output, 0);
|
_download_error(download, download->prefs->output, 0);
|
||||||
|
download->fp = NULL;
|
||||||
_download_refresh(download);
|
_download_refresh(download);
|
||||||
g_source_remove(download->timeout);
|
gtk_label_set_text(GTK_LABEL(download->status), "Complete");
|
||||||
download->timeout = 0;
|
gtk_button_set_label(GTK_BUTTON(download->cancel), GTK_STOCK_CLOSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _http_data_partial(GConnHttpEventData * event, Download * download)
|
static void _http_data_partial(GConnHttpEventData * event, Download * download)
|
||||||
{
|
{
|
||||||
gchar * buf;
|
|
||||||
gsize size;
|
|
||||||
|
|
||||||
if(download->content_length == 0 && download->data_received == 0)
|
if(download->content_length == 0 && download->data_received == 0)
|
||||||
gtk_label_set_text(GTK_LABEL(download->status), "Downloading");
|
gtk_label_set_text(GTK_LABEL(download->status), "Downloading");
|
||||||
/* FIXME code duplication with data_complete */
|
|
||||||
download->data_received = event->data_received;
|
download->data_received = event->data_received;
|
||||||
download->content_length = event->content_length;
|
download->content_length = event->content_length;
|
||||||
if(gnet_conn_http_steal_buffer(download->conn, &buf, &size))
|
_download_write(download);
|
||||||
{
|
|
||||||
/* FIXME use a GIOChannel instead, check errors */
|
|
||||||
fwrite(buf, sizeof(*buf), size, download->fp);
|
|
||||||
g_free(buf);
|
|
||||||
download->pulse = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _http_redirect(GConnHttpEventRedirect * event, Download * download)
|
static void _http_redirect(GConnHttpEventRedirect * event, Download * download)
|
||||||
@ -377,6 +410,7 @@ static void _http_timeout(Download * download)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* download_on_idle */
|
||||||
static gboolean _download_on_idle(gpointer data)
|
static gboolean _download_on_idle(gpointer data)
|
||||||
{
|
{
|
||||||
Download * download = data;
|
Download * download = data;
|
||||||
@ -403,6 +437,7 @@ static gboolean _download_on_idle(gpointer data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* download_on_timeout */
|
||||||
static gboolean _download_on_timeout(gpointer data)
|
static gboolean _download_on_timeout(gpointer data)
|
||||||
{
|
{
|
||||||
Download * download = data;
|
Download * download = data;
|
||||||
|
Loading…
Reference in New Issue
Block a user