diff options
Downloads: show elapsed time
1 files changed, 43 insertions, 10 deletions
| -rw-r--r-- | downloads.c | 53 |
diff --git a/downloads.c b/downloads.c index 2443a3e..81a5dfc 100644 --- a/downloads.c +++ b/downloads.c @@ -19,6 +19,17 @@ download_stop_iconCb_clicked(GtkButton *stop_icon, gpointer user_data) } void +download_format_elapsed(char *formatted, size_t formatted_size, char *format, int total) +{ + snprintf(formatted, + formatted_size, + format, + total / 3600, /* hours */ + (total % 3600) / 60, /* minutes */ + total % 60); /* seconds */ +} + +void download_new_entry(WebKitDownload *webkit_download, struct Download *download) { download->error = 0; @@ -30,6 +41,8 @@ download_new_entry(WebKitDownload *webkit_download, struct Download *download) gtk_image_new_from_icon_name("network-idle-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR); download->stop_icon = gtk_button_new_from_icon_name("process-stop", GTK_ICON_SIZE_SMALL_TOOLBAR); + gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(download->progress), TRUE); + g_signal_connect( download->stop_icon, "clicked", G_CALLBACK(download_stop_iconCb_clicked), webkit_download); @@ -54,7 +67,8 @@ downloadCb_created_destination(WebKitDownload *webkit_download, char *markup; struct Download *download = (struct Download *)user_data; - markup = g_markup_printf_escaped("<a href=\"%s\">%s</a>", destination, destination); + markup = g_markup_printf_escaped( + "<a href=\"%s\">%s</a>", destination, webkit_uri_for_display(destination)); gtk_label_set_markup(GTK_LABEL(download->file_path), markup); g_free(markup); @@ -92,16 +106,23 @@ downloadCb_decide_destination(WebKitDownload *webkit_download, void downloadCb_failed(WebKitDownload *webkit_download, GError *error, gpointer user_data) { - (void)webkit_download; - (void)error; struct Download *download = (struct Download *)user_data; + char formatted[BUFSIZ]; + int total = (int)webkit_download_get_elapsed_time(webkit_download); + char *format; download->error = error; if(g_error_matches(error, WEBKIT_DOWNLOAD_ERROR, WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER)) - gtk_label_set_text(GTK_LABEL(download->status), _("Download cancelled")); + format = _("%02i:%02i:%02i Download cancelled"); else - gtk_label_set_text(GTK_LABEL(download->status), _("Download error")); + format = _("%02i:%02i:%02i Download error"); + + download_format_elapsed(formatted, sizeof(formatted), format, total); + + gtk_label_set_text(GTK_LABEL(download->status), formatted); + + gtk_widget_destroy(download->stop_icon); gtk_image_set_from_icon_name( GTK_IMAGE(download->icon), "network-error-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR); @@ -110,16 +131,22 @@ downloadCb_failed(WebKitDownload *webkit_download, GError *error, gpointer user_ void downloadCb_finished(WebKitDownload *webkit_download, gpointer user_data) { - (void)webkit_download; struct Download *download = (struct Download *)user_data; + char formatted[BUFSIZ]; + int total = (int)webkit_download_get_elapsed_time(webkit_download); + + gchar *format_size = g_format_size(webkit_download_get_received_data_length(webkit_download)); + + download_format_elapsed( + formatted, sizeof(formatted), _("%02i:%02i:%02i Download finished"), total); - // gtk_button_set_image(GTK_BUTTON(download->stop_icon), NULL); gtk_widget_destroy(download->stop_icon); if(download->error == 0) { gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(download->progress), 1); - gtk_label_set_text(GTK_LABEL(download->status), _("Download finished")); + gtk_progress_bar_set_text(GTK_PROGRESS_BAR(download->progress), format_size); + gtk_label_set_text(GTK_LABEL(download->status), formatted); gtk_image_set_from_icon_name( GTK_IMAGE(download->icon), "network-idle-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR); } @@ -132,13 +159,19 @@ downloadCb_received_data(WebKitDownload *webkit_download, guint64 data_lenght, g { (void)data_lenght; struct Download *download = (struct Download *)user_data; - gchar *format_size = g_format_size(data_lenght); + char formatted[BUFSIZ]; + int total = (int)webkit_download_get_elapsed_time(webkit_download); + + gchar *format_size = g_format_size(webkit_download_get_received_data_length(webkit_download)); + + download_format_elapsed(formatted, sizeof(formatted), _("%02i:%02i:%02i Downloading…"), total); gtk_image_set_from_icon_name( GTK_IMAGE(download->icon), "network-receive-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR); - gtk_label_set_text(GTK_LABEL(download->status), format_size); + gtk_label_set_text(GTK_LABEL(download->status), formatted); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(download->progress), webkit_download_get_estimated_progress(webkit_download)); + gtk_progress_bar_set_text(GTK_PROGRESS_BAR(download->progress), format_size); g_free(format_size); } |