Code cleanup

This commit is contained in:
Pierre Pronchery 2012-06-02 10:17:56 +00:00
parent 8d172d4637
commit 864dc1e6a8

View File

@ -209,6 +209,7 @@ Download * download_new(DownloadPrefs * prefs, char const * url)
gtk_editable_set_editable(GTK_EDITABLE(download->address), FALSE); gtk_editable_set_editable(GTK_EDITABLE(download->address), FALSE);
gtk_box_pack_start(GTK_BOX(hbox), download->address, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox), download->address, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
/* labels */
_download_label(vbox, bold, left, _("File: "), &download->filename, _download_label(vbox, bold, left, _("File: "), &download->filename,
download->prefs.output); download->prefs.output);
_download_label(vbox, bold, left, _("Status: "), &download->status, _download_label(vbox, bold, left, _("Status: "), &download->status,
@ -381,6 +382,9 @@ static int _download_set_proxy(Download * download, char const * http,
/* download_refresh */ /* download_refresh */
static void _refresh_unit(guint64 total, double * fraction, char const ** unit,
double * current);
static void _download_refresh(Download * download) static void _download_refresh(Download * download)
{ {
char buf[256]; char buf[256];
@ -405,25 +409,18 @@ static void _download_refresh(Download * download)
tv.tv_sec--; tv.tv_sec--;
tv.tv_usec += 1000000; tv.tv_usec += 1000000;
} }
rate_fraction = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); _refresh_unit(download->data_received * 1024
rate_fraction = download->data_received / rate_fraction; / ((tv.tv_sec * 1000) + (tv.tv_usec / 1000)),
if(rate_fraction > 1024) &rate_fraction, &rate_unit, NULL);
{
rate_fraction /= 1024;
rate_unit = N_("MB");
}
} }
if(download->content_length == 0) if(download->content_length == 0)
{ {
/* the total size is not known */ /* the total size is not known */
if((total_fraction = download->data_received / 1024) > 1024) _refresh_unit(download->data_received, &total_fraction,
{ &total_unit, NULL);
total_fraction /= 1024; snprintf(buf, sizeof(buf), _("%.1f %s (%.1f %s/s)"),
total_unit = N_("MB"); total_fraction, total_unit, rate_fraction,
} rate_unit);
snprintf(buf, sizeof(buf), _("%.1f %s (%.1f %s)"),
total_fraction, _(total_unit), rate_fraction,
_(rate_unit));
gtk_label_set_text(GTK_LABEL(download->received), buf); gtk_label_set_text(GTK_LABEL(download->received), buf);
snprintf(buf, sizeof(buf), " "); snprintf(buf, sizeof(buf), " ");
/* pulse the progress bar if any data was received */ /* pulse the progress bar if any data was received */
@ -437,13 +434,9 @@ static void _download_refresh(Download * download)
else else
{ {
/* the total size is known */ /* the total size is known */
current_fraction = download->data_received / 1024; current_fraction = download->data_received;
if((total_fraction = download->content_length / 1024) > 1024) _refresh_unit(download->content_length, &total_fraction,
{ &total_unit, &current_fraction);
current_fraction /= 1024;
total_fraction /= 1024;
total_unit = N_("MB");
}
snprintf(buf, sizeof(buf), _("%.1f of %.1f %s (%.1f %s/s)"), snprintf(buf, sizeof(buf), _("%.1f of %.1f %s (%.1f %s/s)"),
current_fraction, total_fraction, _(total_unit), current_fraction, total_fraction, _(total_unit),
rate_fraction, _(rate_unit)); rate_fraction, _(rate_unit));
@ -457,6 +450,28 @@ static void _download_refresh(Download * download)
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(download->progress), buf); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(download->progress), buf);
} }
static void _refresh_unit(guint64 total, double * fraction, char const ** unit,
double * current)
{
/* bytes */
*fraction = total;
*unit = _("bytes");
if(*fraction < 1024)
return;
/* kilobytes */
*fraction /= 1024;
if(current != NULL)
*current /= 1024;
*unit = _("kB");
if(*fraction < 1024)
return;
/* megabytes */
*fraction /= 1024;
if(current != NULL)
*current /= 1024;
*unit = _("MB");
}
/* download_write */ /* download_write */
#ifndef WITH_WEBKIT #ifndef WITH_WEBKIT