From 3e4f1b2aae15e3b7725addf499758bf6b37cbcd2 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 14 Dec 2024 16:09:00 +0900 Subject: [PATCH 1/8] Rename to has_adapter for readability --- src/Application.vala | 4 ++-- src/Services/Manager.vala | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 98ccd8e..c04143d 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -138,7 +138,7 @@ public class BluetoothApp : Gtk.Application { bt_receivers = new GLib.List (); bt_senders = new GLib.List (); object_manager = new Bluetooth.ObjectManager (); - object_manager.notify["has-object"].connect (() => { + object_manager.notify["has-adapter"].connect (() => { var build_path = Path.build_filename ( Environment.get_home_dir (), ".local", "share", "contractor" ); @@ -154,7 +154,7 @@ public class BluetoothApp : Gtk.Application { ) ); - if (object_manager.has_object) { + if (object_manager.has_adapter) { if (!active_once) { agent_obex = new Bluetooth.Obex.Agent (); agent_obex.transfer_view.connect (dialog_active); diff --git a/src/Services/Manager.vala b/src/Services/Manager.vala index 1c88adf..58e62a8 100644 --- a/src/Services/Manager.vala +++ b/src/Services/Manager.vala @@ -19,7 +19,7 @@ public class Bluetooth.ObjectManager : Object { public signal void device_added (Bluetooth.Device device); public signal void device_removed (Bluetooth.Device device); public signal void status_discovering (); - public bool has_object { get; private set; default = false; } + public bool has_adapter { get; private set; default = false; } public Settings settings; private GLib.DBusObjectManagerClient object_manager; @@ -93,7 +93,7 @@ public class Bluetooth.ObjectManager : Object { device_added (device); } else if (iface is Bluetooth.Adapter) { unowned var adapter = (Bluetooth.Adapter) iface; - has_object = true; + has_adapter = true; ((DBusProxy) adapter).g_properties_changed.connect ((changed, invalid) => { var discovering = changed.lookup_value ("Discovering", GLib.VariantType.BOOLEAN); if (discovering != null) { @@ -107,7 +107,7 @@ public class Bluetooth.ObjectManager : Object { if (iface is Bluetooth.Device) { device_removed ((Bluetooth.Device) iface); } else if (iface is Bluetooth.Adapter) { - has_object = !get_adapters ().is_empty; + has_adapter = !get_adapters ().is_empty; } } From 97ed1cc633f6e34a6c2cfb04f48753f75d832b69 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 14 Dec 2024 20:39:07 +0900 Subject: [PATCH 2/8] Require files for --send option - Check if --send option is passed by checking whether the files array pointer is null - Remove unnecessary clearing option variables --- src/Application.vala | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index c04143d..24920fa 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -23,8 +23,7 @@ public class BluetoothApp : Gtk.Application { public const OptionEntry[] OPTIONS_BLUETOOTH = { { "silent", 's', 0, OptionArg.NONE, out silent, "Run the Application in background", null}, - { "send", 'f', 0, OptionArg.NONE, out send, "Open file to send via Bluetooth", null }, - { "", 0, 0, OptionArg.STRING_ARRAY, out arg_files, "Get files", null }, + { "send", 'f', 0, OptionArg.FILENAME_ARRAY, out arg_files, "Open file to send via Bluetooth", "FILE..." }, { null } }; @@ -37,10 +36,9 @@ public class BluetoothApp : Gtk.Application { public GLib.List bt_receivers; public GLib.List bt_senders; public static bool silent = true; - public static bool send = false; public static bool active_once; [CCode (array_length = false, array_null_terminated = true)] - public static string[]? arg_files = {}; + public static string[]? arg_files = null; construct { application_id = "io.elementary.bluetooth"; @@ -61,7 +59,7 @@ public class BluetoothApp : Gtk.Application { activate (); - if (send) { + if (arg_files != null) { File [] files = {}; foreach (unowned string arg_file in arg_files) { var file = command.create_file_for_arg (arg_file); @@ -105,9 +103,6 @@ public class BluetoothApp : Gtk.Application { }); } }); - - arg_files = {}; - send = false; } } From b706c8751453a36f925deee2f404463f6b5e5839 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 14 Dec 2024 20:48:28 +0900 Subject: [PATCH 3/8] Split into methods --- src/Application.vala | 94 +++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 24920fa..fc7388a 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -46,6 +46,56 @@ public class BluetoothApp : Gtk.Application { Intl.setlocale (LocaleCategory.ALL, ""); } + private File[] create_files_for_arg (ApplicationCommandLine command, string[] args) { + File[] files = {}; + + foreach (unowned string arg in args) { + var file = command.create_file_for_arg (arg); + if (file.query_exists ()) { + files += file; + } else { + stderr.printf ( + "The file %s was not found and will not be sent.\n", + file.get_path () + ); + } + } + + return files; + } + + private void scan_and_send_files (File[] files) { + if (bt_scan == null) { + bt_scan = new BtScan (this, object_manager); + Idle.add (() => { // Wait for async BtScan initialisation + bt_scan.show_all (); + return Source.REMOVE; + }); + } else { + bt_scan.present (); + } + + bt_scan.destroy.connect (() => { + bt_scan = null; + }); + + bt_scan.send_file.connect ((device) => { + if (!insert_sender (files, device)) { + bt_sender = new BtSender (this); + bt_sender.add_files (files, device); + bt_senders.append (bt_sender); + bt_sender.show_all (); + bt_sender.destroy.connect (()=> { + bt_senders.foreach ((sender)=>{ + if (sender.device == bt_sender.device) { + bt_senders.remove_link (bt_senders.find (sender)); + } + }); + }); + } + }); + } + public override int command_line (ApplicationCommandLine command) { string [] args_cmd = command.get_arguments (); unowned string [] args = args_cmd; @@ -59,50 +109,12 @@ public class BluetoothApp : Gtk.Application { activate (); + // Handle "send" option if (arg_files != null) { - File [] files = {}; - foreach (unowned string arg_file in arg_files) { - var file = command.create_file_for_arg (arg_file); - if (file.query_exists ()) { - files += file; - } else { - stderr.printf ( - "The file %s was not found and will not be sent.\n", - file.get_path () - ); - } - } + File[] files = create_files_for_arg (command, arg_files); if (files.length > 0) { - if (bt_scan == null) { - bt_scan = new BtScan (this, object_manager); - Idle.add (() => { // Wait for async BtScan initialisation - bt_scan.show_all (); - return Source.REMOVE; - }); - } else { - bt_scan.present (); - } - - bt_scan.destroy.connect (() => { - bt_scan = null; - }); - - bt_scan.send_file.connect ((device) => { - if (!insert_sender (files, device)) { - bt_sender = new BtSender (this); - bt_sender.add_files (files, device); - bt_senders.append (bt_sender); - bt_sender.show_all (); - bt_sender.destroy.connect (()=> { - bt_senders.foreach ((sender)=>{ - if (sender.device == bt_sender.device) { - bt_senders.remove_link (bt_senders.find (sender)); - } - }); - }); - } - }); + scan_and_send_files (files); } } From 2419662298d29b19ad3ce9e7c3c2f71cb82f86c3 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 14 Dec 2024 20:53:17 +0900 Subject: [PATCH 4/8] Treat errors first and use shorten log message --- src/Application.vala | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index fc7388a..b1459bd 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -51,14 +51,15 @@ public class BluetoothApp : Gtk.Application { foreach (unowned string arg in args) { var file = command.create_file_for_arg (arg); - if (file.query_exists ()) { - files += file; - } else { + if (!file.query_exists ()) { stderr.printf ( - "The file %s was not found and will not be sent.\n", + "Ignoring not found file: %s\n", file.get_path () ); + continue; } + + files += file; } return files; From 58ca3a5656519a310be603dbad61cc842e7fc957 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 14 Dec 2024 20:53:47 +0900 Subject: [PATCH 5/8] Parse options automatically --- src/Application.vala | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index b1459bd..4e456c0 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -44,6 +44,7 @@ public class BluetoothApp : Gtk.Application { application_id = "io.elementary.bluetooth"; flags |= ApplicationFlags.HANDLES_COMMAND_LINE; Intl.setlocale (LocaleCategory.ALL, ""); + add_main_option_entries (OPTIONS_BLUETOOTH); } private File[] create_files_for_arg (ApplicationCommandLine command, string[] args) { @@ -98,16 +99,6 @@ public class BluetoothApp : Gtk.Application { } public override int command_line (ApplicationCommandLine command) { - string [] args_cmd = command.get_arguments (); - unowned string [] args = args_cmd; - var opt_context = new OptionContext (null); - opt_context.add_main_entries (OPTIONS_BLUETOOTH, null); - try { - opt_context.parse (ref args); - } catch (Error err) { - warning (err.message); - } - activate (); // Handle "send" option From 648d7afcd5b634a04b889aa94fe4ed50d4997b5b Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 14 Dec 2024 21:04:15 +0900 Subject: [PATCH 6/8] Fix typo --- src/Application.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 4e456c0..552877f 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -47,7 +47,7 @@ public class BluetoothApp : Gtk.Application { add_main_option_entries (OPTIONS_BLUETOOTH); } - private File[] create_files_for_arg (ApplicationCommandLine command, string[] args) { + private File[] create_files_for_args (ApplicationCommandLine command, string[] args) { File[] files = {}; foreach (unowned string arg in args) { @@ -103,7 +103,7 @@ public class BluetoothApp : Gtk.Application { // Handle "send" option if (arg_files != null) { - File[] files = create_files_for_arg (command, arg_files); + File[] files = create_files_for_args (command, arg_files); if (files.length > 0) { scan_and_send_files (files); From f7fa76ce9efb799403a5fe448a621ba95979b5e6 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sat, 14 Dec 2024 21:23:18 +0900 Subject: [PATCH 7/8] Fix lint --- src/Application.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application.vala b/src/Application.vala index 552877f..2cb03f3 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -23,7 +23,7 @@ public class BluetoothApp : Gtk.Application { public const OptionEntry[] OPTIONS_BLUETOOTH = { { "silent", 's', 0, OptionArg.NONE, out silent, "Run the Application in background", null}, - { "send", 'f', 0, OptionArg.FILENAME_ARRAY, out arg_files, "Open file to send via Bluetooth", "FILE..." }, + { "send", 'f', 0, OptionArg.FILENAME_ARRAY, out arg_files, "Open file to send via Bluetooth", "FILEā€¦" }, { null } }; From 0acdfa444eb29c598a6e18646461f3cc51f4513a Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Sun, 15 Dec 2024 00:42:54 +0900 Subject: [PATCH 8/8] Add missing space --- src/Application.vala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 2cb03f3..fff42d9 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -87,8 +87,8 @@ public class BluetoothApp : Gtk.Application { bt_sender.add_files (files, device); bt_senders.append (bt_sender); bt_sender.show_all (); - bt_sender.destroy.connect (()=> { - bt_senders.foreach ((sender)=>{ + bt_sender.destroy.connect (() => { + bt_senders.foreach ((sender) => { if (sender.device == bt_sender.device) { bt_senders.remove_link (bt_senders.find (sender)); } @@ -190,12 +190,12 @@ public class BluetoothApp : Gtk.Application { } private void dialog_active (string session_path) { - bt_receivers.foreach ((receiver)=>{ + bt_receivers.foreach ((receiver) => { if (receiver.transfer.session == session_path) { receiver.show_all (); } }); - bt_senders.foreach ((sender)=>{ + bt_senders.foreach ((sender) => { if (sender.transfer.session == session_path) { sender.show_all (); } @@ -204,7 +204,7 @@ public class BluetoothApp : Gtk.Application { private bool insert_sender (File[] files, Bluetooth.Device device) { bool exist = false; - bt_senders.foreach ((sender)=>{ + bt_senders.foreach ((sender) => { if (sender.device == device) { sender.insert_files (files); sender.present (); @@ -226,8 +226,8 @@ public class BluetoothApp : Gtk.Application { bt_receiver = new BtReceiver (this); bt_receivers.append (bt_receiver); - bt_receiver.destroy.connect (()=> { - bt_receivers.foreach ((receiver)=>{ + bt_receiver.destroy.connect (() => { + bt_receivers.foreach ((receiver) => { if (receiver.transfer.session == bt_receiver.session) { bt_receivers.remove_link (bt_receivers.find (receiver)); }