From cc8acfd5178dcc5fd94724a20a6f475c7c11a5a5 Mon Sep 17 00:00:00 2001
From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
Date: Sun, 27 Aug 2017 10:33:18 +0530
Subject: [PATCH] Web+: Use BDurationFormat to format time remaining in
Downloads.
---
src/apps/webpositive/DownloadProgressView.cpp | 65 ++++-----------------------
1 file changed, 9 insertions(+), 56 deletions(-)
diff --git a/src/apps/webpositive/DownloadProgressView.cpp b/src/apps/webpositive/DownloadProgressView.cpp
index 2e50c96..a76f1da 100644
a
|
b
|
|
15 | 15 | #include <Catalog.h> |
16 | 16 | #include <Clipboard.h> |
17 | 17 | #include <Directory.h> |
| 18 | #include <DurationFormat.h> |
18 | 19 | #include <Entry.h> |
19 | 20 | #include <FindDirectory.h> |
20 | 21 | #include <GroupLayoutBuilder.h> |
… |
… |
DownloadProgressView::_UpdateStatusText()
|
785 | 786 | * 1000000LL / (system_time() - fEstimatedFinishReferenceTime); |
786 | 787 | double secondsRemaining = (fExpectedSize - fCurrentSize) |
787 | 788 | / totalBytesPerSecond; |
788 | | time_t now = (time_t)real_time_clock(); |
789 | | time_t finishTime = (time_t)(now + secondsRemaining); |
790 | | |
791 | | tm _time; |
792 | | tm* time = localtime_r(&finishTime, &_time); |
793 | | int32 year = time->tm_year + 1900; |
794 | | |
795 | | char timeText[32]; |
796 | | time_t secondsPerDay = 24 * 60 * 60; |
797 | | // TODO: Localization of time string... |
798 | | if (now < finishTime - secondsPerDay) { |
799 | | // process is going to take more than a day! |
800 | | sprintf(timeText, "%0*d:%0*d %0*d/%0*d/%" B_PRId32, |
801 | | 2, time->tm_hour, 2, time->tm_min, |
802 | | 2, time->tm_mon + 1, 2, time->tm_mday, year); |
803 | | } else { |
804 | | sprintf(timeText, "%0*d:%0*d", |
805 | | 2, time->tm_hour, 2, time->tm_min); |
806 | | } |
807 | | |
808 | | BString buffer1(B_TRANSLATE_COMMENT("Finish: ", "Finishing time")); |
809 | | buffer1 << timeText; |
810 | | finishTime -= now; |
811 | | time = gmtime(&finishTime); |
812 | | |
813 | | BTimeUnitFormat timeFormat; |
814 | 789 | |
| 790 | BString buffer1(B_TRANSLATE("Time Left")); |
| 791 | buffer1 << ": "; |
815 | 792 | BString buffer2; |
816 | | if (finishTime > secondsPerDay) { |
817 | | int64 days = finishTime / secondsPerDay; |
818 | | |
819 | | BString time; |
820 | | timeFormat.Format(time, days, B_TIME_UNIT_DAY); |
821 | | |
822 | | buffer2 << B_TRANSLATE("Over %days left"); |
823 | | buffer2.ReplaceFirst("%days", time); |
824 | | } else if (finishTime > 60 * 60) { |
825 | | int64 hours = finishTime / (60 * 60); |
826 | | BString time; |
827 | | timeFormat.Format(time, hours, B_TIME_UNIT_HOUR); |
828 | | |
829 | | buffer2 << B_TRANSLATE("Over %hours left"); |
830 | | buffer2.ReplaceFirst("%hours", time); |
831 | | } else if (finishTime > 60) { |
832 | | int64 minutes = finishTime / 60; |
833 | | if (minutes == 1) |
834 | | buffer2 << B_TRANSLATE("Over 1 minute left"); |
835 | | else |
836 | | timeFormat.Format(buffer2, minutes, B_TIME_UNIT_MINUTE); |
837 | | } else { |
838 | | BString time; |
839 | | timeFormat.Format(time, finishTime, B_TIME_UNIT_SECOND); |
840 | | |
841 | | buffer2 << B_TRANSLATE("%seconds left"); |
842 | | buffer2.ReplaceFirst("%seconds", time); |
843 | | } |
| 793 | BDurationFormat().Format(buffer2, 0, secondsRemaining * 1000000); |
844 | 794 | |
845 | 795 | buffer = "("; |
846 | | buffer << buffer1 << " - " << buffer2 << ")"; |
| 796 | buffer << buffer1 << buffer2 << ")"; |
847 | 797 | |
848 | 798 | float stringWidth = fInfoView->StringWidth(buffer.String()); |
849 | 799 | if (stringWidth < fInfoView->Bounds().Width()) |
850 | 800 | fInfoView->SetText(buffer.String()); |
851 | 801 | else { |
852 | 802 | // complete string too wide, try with shorter version |
| 803 | buffer2 = ""; |
| 804 | BDurationFormat(", ", B_TIME_UNIT_ABBREVIATED).Format(buffer2, |
| 805 | 0, secondsRemaining * 1000000 ); |
853 | 806 | buffer = "("; |
854 | | buffer << buffer1 << ")"; |
| 807 | buffer << buffer1 << buffer2 << ")"; |
855 | 808 | stringWidth = fInfoView->StringWidth(buffer.String()); |
856 | 809 | if (stringWidth < fInfoView->Bounds().Width()) |
857 | 810 | fInfoView->SetText(buffer.String()); |