Show estimated time remaining in transferbar tooltip

This commit is contained in:
Paweł Forysiuk 2011-04-20 20:52:07 +02:00 committed by Christian Dywan
parent 28035567db
commit 3eb3481812
3 changed files with 88 additions and 31 deletions

View file

@ -2208,3 +2208,82 @@ sokoke_build_thumbnail_path (const gchar* name)
}
return path;
}
gchar*
midori_download_prepare_tooltip_text (WebKitDownload* download)
{
gdouble* last_time;
guint64* last_size;
gint hour = 3600, min = 60;
gint hours_left, minutes_left, seconds_left;
guint64 total_size = webkit_download_get_total_size (download);
guint64 current_size = webkit_download_get_current_size (download);
gdouble time_elapsed = webkit_download_get_elapsed_time (download);
gdouble time_estimated, time_diff;
gchar* current, *total, *download_speed;
gchar* hours_str, *minutes_str, *seconds_str;
GString* tooltip = g_string_new (NULL);
time_diff = time_elapsed / current_size;
time_estimated = (total_size - current_size) * time_diff;
hours_left = time_estimated / hour;
minutes_left = (time_estimated - (hours_left * hour)) / min;
seconds_left = (time_estimated - (hours_left * hour) - (minutes_left * min));
hours_str = g_strdup_printf (ngettext ("%d hour", "%d hours", hours_left), hours_left);
minutes_str = g_strdup_printf (ngettext ("%d minute", "%d minutes", minutes_left), minutes_left);
seconds_str = g_strdup_printf (ngettext ("%d second", "%d seconds", seconds_left), seconds_left);
current = g_format_size_for_display (current_size);
total = g_format_size_for_display (total_size);
last_time = g_object_get_data (G_OBJECT (download), "last-time");
last_size = g_object_get_data (G_OBJECT (download), "last-size");
/* i18n: Download tooltip (size): 4KB of 43MB */
g_string_append_printf (tooltip, _("%s of %s"), current, total);
g_free (current);
g_free (total);
if (time_elapsed != *last_time)
download_speed = g_format_size_for_display (
(current_size - *last_size) / (time_elapsed - *last_time));
else
/* i18n: Unknown number of bytes, used for transfer rate like ?B/s */
download_speed = g_strdup (_("?B"));
/* i18n: Download tooltip (transfer rate): (130KB/s) */
g_string_append_printf (tooltip, _(" (%s/s)"), download_speed);
g_free (download_speed);
if (time_estimated > 0)
{
gchar* eta;
if (hours_left > 0)
eta = g_strdup_printf ("%s, %s", hours_str, minutes_str);
else if (minutes_left >= 10)
eta = g_strdup_printf ("%s", minutes_str);
else if (minutes_left < 10 && minutes_left > 0)
eta = g_strdup_printf ("%s, %s", minutes_str, seconds_str);
else if (seconds_left > 0)
eta = g_strdup_printf ("%s", seconds_str);
else
eta = g_strdup ("");
/* i18n: Download tooltip (estimated time) : - 1 hour, 5 minutes remaning */
g_string_append_printf (tooltip, _(" - %s remaining"), eta);
g_free (eta);
}
g_free (hours_str);
g_free (seconds_str);
g_free (minutes_str);
if (time_elapsed - *last_time > 5.0)
{
*last_time = time_elapsed;
*last_size = current_size;
}
return g_string_free (tooltip, FALSE);
}

View file

@ -286,4 +286,7 @@ sokoke_widget_copy_clipboard (GtkWidget* widget,
gchar*
sokoke_build_thumbnail_path (const gchar* name);
gchar*
midori_download_prepare_tooltip_text (WebKitDownload* download);
#endif /* !__SOKOKE_H__ */

View file

@ -75,14 +75,8 @@ midori_transferbar_download_notify_progress_cb (WebKitDownload* download,
GParamSpec* pspec,
GtkWidget* progress)
{
gchar* current;
gchar* total;
gchar* size_text;
gchar* tooltip;
gchar* text;
gchar* transfer;
gdouble* last_time;
guint64* last_size;
gdouble timestamp;
guint64 size;
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress),
@ -96,31 +90,12 @@ midori_transferbar_download_notify_progress_cb (WebKitDownload* download,
return;
}
current = g_format_size_for_display (webkit_download_get_current_size (download));
total = g_format_size_for_display (webkit_download_get_total_size (download));
last_time = g_object_get_data (G_OBJECT (download), "last-time");
last_size = g_object_get_data (G_OBJECT (download), "last-size");
timestamp = webkit_download_get_elapsed_time (download);
if (timestamp != *last_time)
transfer = g_format_size_for_display ((size - *last_size) / (timestamp - *last_time));
else
/* i18n: Unknown number of bytes, used for transfer rate like ?B/s */
transfer = g_strdup (_("?B"));
/* i18n: Download tooltip, 4KB of 43MB, 130KB/s */
size_text = g_strdup_printf (_("%s of %s, %s/s"), current, total, transfer);
if (timestamp - *last_time > 5.0)
{
*last_time = timestamp;
*last_size = size;
}
g_free (current);
g_free (total);
text = g_strdup_printf ("%s (%s)",
gtk_progress_bar_get_text (GTK_PROGRESS_BAR (progress)),
size_text);
tooltip = midori_download_prepare_tooltip_text (download);
text = g_strdup_printf ("%s\n%s",
gtk_progress_bar_get_text (GTK_PROGRESS_BAR (progress)), tooltip);
gtk_widget_set_tooltip_text (progress, text);
g_free (size_text);
g_free (transfer);
g_free (tooltip);
g_free (text);
}