Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic speed/remaining time estimation #16

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions po/nxdumpclient.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: nxdumpclient\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-10-31 18:55+0300\n"
"POT-Creation-Date: 2023-11-07 03:53+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -194,50 +194,62 @@ msgstr ""
msgid "Transfer progress"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:86
#: src/widgets/DeviceStatusRow.vala:88
msgctxt "status"
msgid "Uninitialized"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:89
#: src/widgets/DeviceStatusRow.vala:91
msgctxt "status"
msgid "Connected"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:92
#: src/widgets/DeviceStatusRow.vala:94
msgctxt "status"
msgid "Transferring file"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:95
#: src/widgets/DeviceStatusRow.vala:97
msgctxt "status"
msgid "Fatal error"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:121
#. TODO: use StringBuilder.take on GLib >= 2.78
#: src/widgets/DeviceStatusRow.vala:124
#, c-format
msgctxt "file transfer progress"
msgid "%s / %s"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:162
#: src/widgets/DeviceStatusRow.vala:138
#, c-format
msgctxt "file transfer speed and min:sec remaining"
msgid "(%s/s, %02lld:%02lld remaining)"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:144
msgctxt "file transfer speed and min:sec placeholder"
msgid "(-- B/s, --:-- remaining)"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:186
msgid "USB 1.1"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:165
#: src/widgets/DeviceStatusRow.vala:189
msgid "USB 2.0"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:168
#: src/widgets/DeviceStatusRow.vala:192
msgid "USB 3.0"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:171
#: src/widgets/DeviceStatusRow.vala:195
msgctxt "speed string"
msgid "N/A"
msgstr ""

#: src/widgets/DeviceStatusRow.vala:179
#: src/widgets/DeviceStatusRow.vala:203
msgctxt "version string"
msgid "N/A"
msgstr ""
Expand Down Expand Up @@ -299,15 +311,15 @@ msgstr ""
msgid "Not enough space for dump (%s required)"
msgstr ""

#: src/UsbDeviceClient.vala:566
#: src/UsbDeviceClient.vala:573
msgid "NCA checksum verification failed (non-standard dump options?)"
msgstr ""

#: src/UsbDeviceClient.vala:600
#: src/UsbDeviceClient.vala:607
msgid "NSP header"
msgstr ""

#: src/UsbDeviceClient.vala:650
#: src/UsbDeviceClient.vala:657
msgid "NCA checksum verification failed"
msgstr ""

Expand Down
9 changes: 8 additions & 1 deletion src/UsbDeviceClient.vala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace NXDumpClient {
private const uint32 RESPONSE_SIZE = 0x10;
private const uint DEFAULT_TIMEOUT = 5000;
private const uint32 STATUS_SUCCESS = 0x0;
private const uint32 BLOCK_SIZE = 0x800000;
internal const uint32 BLOCK_SIZE = 0x800000;

private const string NSP_MAGIC = "PFS0";

Expand Down Expand Up @@ -181,6 +181,7 @@ namespace NXDumpClient {
public string transfer_file_name_inner { get; private set; default = ""; } // Used in NSP mode
public int64 transfer_total_bytes { get; private set; default = 0; }
public int64 transfer_current_bytes { get; private set; default = 0; }
public int64 transfer_started_time { get; private set; default = 0; }

public uint16 max_packet_size { get {
if (endpoint_input != null) {
Expand Down Expand Up @@ -415,6 +416,7 @@ namespace NXDumpClient {
transfer_file_name_inner = "";
transfer_total_bytes = file_size;
transfer_current_bytes = 0;
transfer_started_time = 0;
status = TRANSFER;
} finally {
thaw_notify();
Expand Down Expand Up @@ -460,6 +462,7 @@ namespace NXDumpClient {
transfer_file_name_inner = "";
transfer_total_bytes = file_size;
transfer_current_bytes = 0;
transfer_started_time = 0;
} finally {
thaw_notify();
}
Expand Down Expand Up @@ -530,6 +533,10 @@ namespace NXDumpClient {
checksum.update(incoming_data, incoming_data.length);
}

if (transfer_started_time == 0) {
transfer_started_time = get_monotonic_time();
}

transfer_progress.emit();
}

Expand Down
28 changes: 26 additions & 2 deletions src/widgets/DeviceStatusRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/

namespace NXDumpClient {
private const uint32 MIN_TRANSFER_SPEED_SIZE = BLOCK_SIZE * 5;

[GtkTemplate (ui = "/org/v1993/NXDumpClient/widgets/DeviceStatusRow.ui")]
class DeviceStatusRow: Adw.Bin {
protected UsbDeviceClient? device { get; set; default = null; }
Expand Down Expand Up @@ -118,10 +120,32 @@ namespace NXDumpClient {
requires(device != null)
{
transfer_fraction = ((double)device.transfer_current_bytes) / ((double)device.transfer_total_bytes);
transfer_text = C_("file transfer progress", "%s / %s").printf(
// TODO: use StringBuilder.take on GLib >= 2.78
var builder = new StringBuilder(C_("file transfer progress", "%s / %s").printf(
format_size(device.transfer_current_bytes),
format_size(device.transfer_total_bytes)
);
));

if (device.transfer_total_bytes >= MIN_TRANSFER_SPEED_SIZE) {
var time_passed = get_monotonic_time() - device.transfer_started_time;
var current_bytes_adjusted = device.transfer_current_bytes - BLOCK_SIZE;
builder.append_c(' ');
if (current_bytes_adjusted > 0 && time_passed > 0) {
// Time is measured after the first transfer, so skip it
var bytes_remaining = device.transfer_total_bytes - current_bytes_adjusted;
var time_remaining = time_passed * bytes_remaining / current_bytes_adjusted / 1000000;
var bytes_per_second = current_bytes_adjusted * 1000000 / time_passed;
builder.append_printf(C_("file transfer speed and min:sec remaining", "(%s/s, %02lld:%02lld remaining)"),
format_size(bytes_per_second),
time_remaining / 60,
time_remaining % 60
);
} else {
builder.append(C_("file transfer speed and min:sec placeholder", "(-- B/s, --:-- remaining)"));
}
}

transfer_text = builder.free_and_steal();
}

private void transfer_started_cb(UsbDeviceClient client, File file, bool mass_transfer) {
Expand Down