2121using Gee ;
2222using GUdev ;
2323
24+ static string _strip_quotes (string s) {
25+ if (s. has_prefix (" \" " ) && s. has_suffix (" \" " ) && (s. length >= 2 ))
26+ return s. substring (1 , s. length - 2 );
27+ return s;
28+ }
29+
2430public sealed class PreferencesDialog : Object {
2531 private Adw . PreferencesDialog dialog;
2632 private Adw . EntryRow row_callsign;
@@ -58,6 +64,8 @@ public sealed class PreferencesDialog : Object {
5864 private File ? logbook_csv = null ;
5965 private GUdev . Client udev_client;
6066
67+ private Dex . Future connect_future;
68+
6169 public PreferencesDialog () {
6270 Object ();
6371 }
@@ -97,6 +105,10 @@ public sealed class PreferencesDialog : Object {
97105 row_radio_model. model = radio_model_list;
98106 row_radio_model. enable_search = true ;
99107 row_radio_model. search_match_mode = Gtk . StringFilterMatchMode . SUBSTRING ;
108+ row_radio_model. notify [" selected" ]. connect (() = > {
109+ var model = radio_models[row_radio_model. selected - 1 ];
110+ print (" radio model selected %s " . printf (model. display_name));
111+ });
100112
101113 row_device_path = builder. get_object (" row_device_path" ) as Adw . ComboRow ;
102114 row_device_path. model = get_serial_devices ();
@@ -286,15 +298,14 @@ public sealed class PreferencesDialog : Object {
286298
287299 var selected_type = model. get_string (row_connection_type. selected);
288300
289- switch (selected_type. down ()) {
290- case " serial" :
291- case " usb" :
301+ switch (selected_type. up ()) {
302+ case " SERIAL/USB" :
292303 serial_settings_group. visible = true ;
293304 network_settings_group. visible = false ;
294305 row_radio_model. visible = true ;
295306 radio_test_group. visible = true ;
296307 break ;
297- case " network " :
308+ case " NETWORK " :
298309 row_radio_model. visible = true ;
299310 row_radio_model. selected = 1 ;
300311 row_radio_model. selectable = false ;
@@ -317,22 +328,66 @@ public sealed class PreferencesDialog : Object {
317328 connection_status_icon. icon_name = " content-loading-symbolic" ;
318329 connection_status_label. label = _ (" Testing…" );
319330
320- test_radio_connection. begin ((obj, res) = > {
321- bool success = test_radio_connection . end (res);
331+ test_radio_connection ();
332+ }
322333
323- test_connection_button. sensitive = true ;
324- if (success) {
325- connection_status_icon. icon_name = " network-idle-symbolic" ;
326- connection_status_label. label = _ (" Connected" );
327- } else {
328- connection_status_icon. icon_name = " network-offline-symbolic" ;
329- connection_status_label. label = _ (" Failed" );
334+ void test_radio_connection () {
335+ if (Application . radio_control == null || row_radio_model. selected == 0 ) return ;
336+ var radio_models = RadioControl . get_radio_models ();
337+ var radio_model = radio_models[row_radio_model. selected - 1 ]; // padded for "None" selection
338+
339+ var connection_type = row_connection_type. model. get_item (row_connection_type. selected) as Gtk . StringObject ;
340+ var device_path = row_device_path. model. get_item (row_device_path. selected) as Gtk . StringObject ;
341+ var baud_rate = row_baud_rate. model. get_item (row_baud_rate. selected) as Gtk . StringObject ;
342+
343+ var config = RadioConfiguration () {
344+ model_id = radio_model. model_id,
345+ connection_type = connection_type. get_string (). down () ?? " " ,
346+ device_path = device_path. get_string (),
347+ network_host = row_network_host. text,
348+ network_port = int . parse (row_network_port. text),
349+ baud_rate = int . parse (baud_rate. get_string ())
350+ };
351+ debug (" " " Testing radio connection with:
352+ \t Model ID: %s\n
353+ \t Connection type: %s\n
354+ \t Device path: %s\n
355+ \t Network host: %s\n
356+ \t Network port: %d\n
357+ \t Baud rate: %d\n
358+ " " " . printf (
359+ radio_model. display_name,
360+ config. connection_type,
361+ config. device_path,
362+ config. network_host,
363+ config. network_port,
364+ config. baud_rate
365+ ));
366+
367+ var is_connected = Application . radio_control. connect (config);
368+ new Dex .Future .then (is_connected, (result) = > {
369+ bool success = false ;
370+ try {
371+ success = result. await_boolean ();
372+ } catch (Error err) {
373+ error (err. message);
330374 }
331- });
332- }
333375
334- async bool test_radio_connection () {
335- return false ;
376+ Dex . Scheduler . get_default (). spawn (0 , () = > {
377+ test_connection_button. sensitive = true ;
378+ if (success) {
379+ connection_status_icon. icon_name = " network-idle-symbolic" ;
380+ connection_status_label. label = _(" Connected" );
381+ } else {
382+ connection_status_icon. icon_name = " network-offline-symbolic" ;
383+ connection_status_label. label = _(" Failed" );
384+ }
385+
386+ return null ;
387+ }). disown ();
388+
389+ return null ;
390+ }). disown ();
336391 }
337392
338393 void do_import_file () {
@@ -386,12 +441,6 @@ public sealed class PreferencesDialog : Object {
386441 alert. present (dialog. get_root ());
387442 } /* do_import_file */
388443
389- string _strip_quotes (string s ) {
390- if (s. has_prefix (" \" " ) && s. has_suffix (" \" " ) && (s. length >= 2 ))
391- return s. substring (1 , s. length - 2 );
392- return s;
393- }
394-
395444 void on_import_file () {
396445 var file_dialog = new Gtk .FileDialog ();
397446
0 commit comments