Skip to content

Commit

Permalink
Add notifications for events
Browse files Browse the repository at this point in the history
Also fix free space query
  • Loading branch information
v1993 committed Oct 26, 2023
1 parent d59a27d commit fc741ba
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 37 deletions.
59 changes: 51 additions & 8 deletions po/nxdumpclient.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ msgid ""
msgstr ""
"Project-Id-Version: nxdumpclient\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-10-25 14:10+0300\n"
"POT-Creation-Date: 2023-10-26 19:26+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"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: data/org.v1993.NXDumpClient.desktop.in:3
Expand All @@ -34,7 +34,7 @@ msgstr ""
msgid "Client for dumping over USB with nxdumptool"
msgstr ""

#: data/org.v1993.NXDumpClient.appdata.xml.in:15 src/Application.vala:80
#: data/org.v1993.NXDumpClient.appdata.xml.in:15 src/Application.vala:193
msgid "Client for dumping over USB with nxdumptool."
msgstr ""

Expand Down Expand Up @@ -142,24 +142,67 @@ msgstr ""
msgid "_Quit"
msgstr ""

#: src/Application.vala:74
#: src/Application.vala:85
msgid "nxdumptool device connected"
msgstr ""

#: src/Application.vala:93
msgid "nxdumptool device disconnected"
msgstr ""

#: src/Application.vala:110
msgid "File transfer started"
msgstr ""

#: src/Application.vala:135
msgid "File transfer complete"
msgstr ""

#: src/Application.vala:138 src/Application.vala:144
msgid "Show in folder"
msgstr ""

#: src/Application.vala:143
#, c-format
msgid "Transfer of file “%s” complete"
msgstr ""

#: src/Application.vala:165
msgid "File transfer canceled"
msgstr ""

#: src/Application.vala:165
msgid "File transfer failed"
msgstr ""

#: src/Application.vala:172
#, c-format
msgid "Transfer of file “%s” canceled"
msgstr ""

#: src/Application.vala:172
#, c-format
msgid "Transfer of file “%s” failed"
msgstr ""

#: src/Application.vala:187
msgid "Print application version and exit"
msgstr ""

#: src/Application.vala:75
#: src/Application.vala:188
msgid "Print udev rules required for USB access and exit"
msgstr ""

#: src/Application.vala:152
#: src/Application.vala:289
#, c-format
msgid "Failed to initialize USB context: %s\n"
msgstr ""

#: src/Application.vala:197
#: src/Application.vala:313
msgid "translator-credits"
msgstr ""

#: src/Application.vala:204
#: src/Application.vala:320
msgctxt "credits section header"
msgid "nxdumptool team"
msgstr ""
Expand Down
141 changes: 138 additions & 3 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,24 @@ namespace NXDumpClient {
}
#endif

private bool should_show_desktop_notification() {
return main_window == null || !main_window.is_active;
}

private bool should_show_toast() {
return main_window != null;
}

public void show_error(string desc, string message) {
if (main_window != null && main_window.is_active) {
if (should_show_toast()) {
var toast = new Adw.Toast.format("<span color=\"red\">%s</span>: %s", desc, message) {
timeout = 5,
priority = HIGH
};
main_window.show_toast((owned)toast);
} else {
}

if (should_show_desktop_notification()) {
var notif = new Notification(desc);
notif.set_body(message);
notif.set_category("device.error");
Expand All @@ -66,6 +76,109 @@ namespace NXDumpClient {
}
}

internal void device_added(UsbDeviceClient client) {
// I assume application outlives all clients. This should hold true.
client.connect("signal::transfer-started", on_file_transfer_started, this, null);
client.connect("signal::transfer-complete", on_file_transfer_complete, this, null);
client.connect("signal::transfer-failed", on_file_transfer_failed, this, null);
if (should_show_desktop_notification()) {
var notif = new Notification(_( "nxdumptool device connected"));
notif.set_category("device.added");
send_notification(@"nxdc-device-$(client.dev.get_bus())-$(client.dev.get_address())", notif);
}
}

internal void device_removed(GUsb.Device dev) {
if (should_show_desktop_notification()) {
var notif = new Notification(_("nxdumptool device disconnected"));
notif.set_category("device.removed");
send_notification(@"nxdc-device-$(dev.get_bus())-$(dev.get_address())", notif);
}
}

private static void on_file_transfer_started(UsbDeviceClient dev, File file, bool mass_transfer, Application app) {
app.file_transfer_started.begin(file, mass_transfer);
}

private async void file_transfer_started(File file, bool mass_transfer) {
try {
if (mass_transfer) {
return;
}

if (should_show_desktop_notification()) {
var notif = new Notification(_("File transfer started"));
var info = file.query_info(FileAttribute.STANDARD_DISPLAY_NAME, NONE, null); // TODO: cancellable
notif.set_body(info.get_display_name());
notif.set_category("device");
send_notification(@"nxdc-file-$(file.get_uri())", notif);
}
} catch(Error e) {
warning("Error sending notification: %s", e.message);
}
}

private static void on_file_transfer_complete(UsbDeviceClient dev, File file, bool mass_transfer, Application app) {
app.file_transfer_complete.begin(file, mass_transfer);
}

private async void file_transfer_complete(File file, bool mass_transfer) {
try {
if (mass_transfer) {
return;
}

var info = file.query_info(FileAttribute.STANDARD_DISPLAY_NAME, NONE, null); // TODO: cancellable
var fname = info.get_display_name();

if (should_show_desktop_notification()) {
var notif = new Notification(_("File transfer complete"));
notif.set_body(fname);
notif.set_category("device");
notif.add_button_with_target_value(_("Show in folder"), "app.show-file", new Variant.take_string(file.get_uri()));
send_notification(@"nxdc-file-$(file.get_uri())", notif);
}

if (should_show_toast()) {
var toast = new Adw.Toast.format(_("Transfer of file “%s” complete"), fname) {
button_label = _("Show in folder"),
action_name = "app.show-file",
action_target = new Variant.take_string(file.get_uri()),
};
main_window.show_toast((owned)toast);
}
} catch(Error e) {
warning("Error sending notification: %s", e.message);
}
}

private static void on_file_transfer_failed(UsbDeviceClient dev, File file, bool cancelled, Application app) {
app.file_transfer_failed.begin(file, cancelled);
}

private async void file_transfer_failed(File file, bool cancelled) {
try {
var info = file.query_info(FileAttribute.STANDARD_DISPLAY_NAME, NONE, null); // TODO: cancellable
var fname = info.get_display_name();

if (should_show_desktop_notification()) {
var notif = new Notification(cancelled ? _("File transfer canceled") : _("File transfer failed"));
notif.set_body(fname);
notif.set_category("device");
send_notification(@"nxdc-file-$(file.get_uri())", notif);
}

if (should_show_toast()) {
var toast = new Adw.Toast.format(cancelled ? _("Transfer of file “%s” canceled") : _("Transfer of file “%s” failed"), fname);
main_window.show_toast((owned)toast);
}
} catch(Error e) {
warning("Error sending notification: %s", e.message);
}
}

// TODO: add "on_transfer_cancelled"

construct {
application_id = "org.v1993.NXDumpClient";
flags = DEFAULT_FLAGS;
Expand All @@ -80,9 +193,13 @@ namespace NXDumpClient {
set_option_context_summary(_("Client for dumping over USB with nxdumptool."));

ActionEntry[] action_entries = {
// In-app only
{ "about", this.on_about_action },
{ "preferences", this.on_preferences_action },
{ "quit", this.quit }
{ "quit", this.quit },

// May be invoked externally (e.g. from a notification)
{ "show-file", this.on_show_file_action, "s" }
};

add_action_entries(action_entries, this);
Expand Down Expand Up @@ -218,5 +335,23 @@ namespace NXDumpClient {

preferences.present();
}

private void on_show_file_action(SimpleAction action, Variant? param)
requires(param != null && param.is_of_type(VariantType.STRING))
{
show_file.begin(File.new_for_uri(param.get_string()));
}

private async void show_file(File file) {
hold(); // Ensure that we won't exit if invoked without activation
try {
var launcher = new Gtk.FileLauncher(file);
yield launcher.open_containing_folder(main_window, null); // TODO: pass cancellable
} catch(Error e) {
warning("Failed to show file in directory: %s", e.message);
} finally {
release();
}
}
}
}
12 changes: 8 additions & 4 deletions src/DeviceStatusRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ namespace NXDumpClient {
update_version();
device_signals += Signal.connect(device, "notify::version-string", (Callback)((void*)on_version_changed), this);
setup_progress();
device_signals += Signal.connect(device, "transfer-started", (Callback)((void*)update_filenames_cb), this);
device_signals += Signal.connect(device, "transfer-started", (Callback)((void*)transfer_started_cb), this);
device_signals += Signal.connect(device, "transfer-next-inner-file", (Callback)((void*)update_filenames_cb), this);
device_signals += Signal.connect(device, "transfer-progress", (Callback)((void*)update_progress_cb), this);
device_signals += Signal.connect(device, "transfer-complete", (Callback)((void*)reset_transfer_view_cb), this);
device_signals += Signal.connect(device, "transfer-complete", (Callback)((void*)transfer_complete_cb), this);
device_signals += Signal.connect(device, "transfer-failed", (Callback)((void*)transfer_failed_cb), this);
}

Expand Down Expand Up @@ -123,6 +123,10 @@ namespace NXDumpClient {
);
}

private static void transfer_started_cb(UsbDeviceClient client, File file, bool mass_transfer, DeviceStatusRow row) {
row.update_filenames();
}

private static void update_filenames_cb(UsbDeviceClient client, DeviceStatusRow row) {
row.update_filenames();
}
Expand All @@ -134,11 +138,11 @@ namespace NXDumpClient {
file_name_inner = device.transfer_file_name_inner;
}

private static void transfer_failed_cb(UsbDeviceClient client, bool cancelled, DeviceStatusRow row) {
private static void transfer_failed_cb(UsbDeviceClient client, File file, bool cancelled, DeviceStatusRow row) {
row.reset_transfer_view();
}

private static void reset_transfer_view_cb(UsbDeviceClient client, DeviceStatusRow row) {
private static void transfer_complete_cb(UsbDeviceClient client, File file, bool mass_transfer, DeviceStatusRow row) {
row.reset_transfer_view();
}

Expand Down
5 changes: 4 additions & 1 deletion src/UsbContext.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ namespace NXDumpClient {
}

debug("Detected a suitable device! Product string: %s", dev.get_string_descriptor(dev.get_product_index()));
new Application().device_list.append(new UsbDeviceClient((owned)opener));
var client = new UsbDeviceClient((owned)opener);
new Application().device_list.append(client);
new Application().device_added(client);
} catch(GUsb.DeviceError.NO_DEVICE e) {
// Not really a problem - device was disconnected during setup
} catch(Error e) {
Expand All @@ -60,6 +62,7 @@ namespace NXDumpClient {
var list_dev = (UsbDeviceClient) devlist.get_object(i);
if (dev == list_dev.dev) {
devlist.remove(i);
new Application().device_removed(dev);
} else {
++i;
}
Expand Down
Loading

0 comments on commit fc741ba

Please sign in to comment.