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

Synchronise settings and global "is-powered" state #195

Merged
merged 18 commits into from
Jun 17, 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
4 changes: 2 additions & 2 deletions src/Indicator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public class BluetoothIndicator.Indicator : Wingpanel.Indicator {
object_manager.bind_property ("has-object", this, "visible", GLib.BindingFlags.SYNC_CREATE);
obex_manager = new BluetoothIndicator.Services.ObexManager ();
if (object_manager.has_object) {
object_manager.set_last_state.begin ();
object_manager.set_state_from_settings.begin ();
}

object_manager.notify["has-object"].connect (() => {
if (object_manager.has_object) {
object_manager.set_last_state.begin ();
object_manager.set_state_from_settings.begin ();
}
});
}
Expand Down
58 changes: 32 additions & 26 deletions src/Services/Manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BluetoothIndicator.Services.ObjectManager : Object {

public bool has_object { get; private set; default = false; }
public bool retrieve_finished { get; private set; default = false; }
public Settings settings;
public Settings settings { get; construct; }
private GLib.DBusObjectManagerClient object_manager;
public bool is_powered {get; private set; default = false; }
public bool is_connected {get; private set; default = false; }
Expand Down Expand Up @@ -53,6 +53,13 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
object_manager.object_removed.connect ((object) => {
object.get_interfaces ().foreach ((iface) => on_interface_removed (object, iface));
});

settings.changed["bluetooth-enabled"].connect (() => {
var enabled = settings.get_boolean ("bluetooth-enabled");
if (enabled != this.is_powered) {
set_state_from_settings.begin ();
}
});
} catch (Error e) {
critical (e.message);
}
Expand Down Expand Up @@ -91,18 +98,16 @@ public class BluetoothIndicator.Services.ObjectManager : Object {

((DBusProxy) device).g_properties_changed.connect ((changed, invalid) => {
var connected = changed.lookup_value ("Connected", new VariantType ("b"));
if (connected != null) {
check_global_state ();
}

var paired = changed.lookup_value ("Paired", new VariantType ("b"));
if (paired != null) {
if (device.paired) {
device_added (device);
} else {
device_removed (device);
}
}

if (connected != null || paired != null) {
check_global_state ();
}
});
Expand All @@ -115,9 +120,11 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
((DBusProxy) adapter).g_properties_changed.connect ((changed, invalid) => {
var powered = changed.lookup_value ("Powered", new VariantType ("b"));
if (powered != null) {
set_last_state.begin ();
check_global_state ();
}
});

check_global_state ();
}
}

Expand All @@ -127,6 +134,8 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
} else if (iface is BluetoothIndicator.Services.Adapter) {
has_object = !get_adapters ().is_empty;
}

check_global_state ();
}

public Gee.LinkedList<BluetoothIndicator.Services.Adapter> get_adapters () {
Expand Down Expand Up @@ -155,21 +164,22 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
return (owned) devices;
}

public void check_global_state () {
/* As this is called within a signal handler, it should be in a Idle loop else
* races occur */
Idle.add (() => {
var powered = get_global_state ();
var connected = get_connected ();
private void check_global_state () {
var powered = get_global_state ();
var connected = get_connected ();

/* Only signal if actually changed */
if (powered != is_powered || connected != is_connected) {
/* Only signal if actually changed */
if (powered != is_powered || connected != is_connected) {
if (powered != is_powered) {
is_powered = powered;
}

if (connected != is_connected) {
is_connected = connected;
global_state_changed (is_powered, is_connected);
}
return false;
});

global_state_changed (is_powered, is_connected);
}
}

public bool get_connected () {
Expand All @@ -194,10 +204,9 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
return false;
}

public async void set_global_state (bool state) {
private async void set_global_state (bool state) {
/* `is_powered` and `connected` properties will be set by the check_global state () callback when adapter or device
* properties change. Do not set now so that global_state_changed signal will be emitted. */

var adapters = get_adapters ();
foreach (var adapter in adapters) {
adapter.powered = state;
Expand All @@ -216,15 +225,12 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
}
}

settings.set_boolean ("bluetooth-enabled", state);
}

public async void set_last_state () {
bool last_state = settings.get_boolean ("bluetooth-enabled");
check_global_state ();

yield set_global_state (last_state);
}

check_global_state ();
public async void set_state_from_settings () {
yield set_global_state (settings.get_boolean ("bluetooth-enabled"));
}

public static bool compare_devices (Device device, Device other) {
Expand Down
5 changes: 4 additions & 1 deletion src/Widgets/DisplayWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public class BluetoothIndicator.Widgets.DisplayWidget : Gtk.Spinner {

button_press_event.connect ((e) => {
if (e.button == Gdk.BUTTON_MIDDLE) {
object_manager.set_global_state.begin (!object_manager.get_global_state ());
object_manager.settings.set_boolean (
"bluetooth-enabled",
!object_manager.settings.get_boolean ("bluetooth-enabled")
);
return Gdk.EVENT_STOP;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Widgets/PopoverWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public class BluetoothIndicator.Widgets.PopoverWidget : Gtk.Box {
});

main_switch.notify["active"].connect (() => {
object_manager.set_global_state.begin (main_switch.active);
warning ("main switch toggle - active %s", main_switch.active.to_string ());
object_manager.settings.set_boolean ("bluetooth-enabled", main_switch.active);
});

show_settings_button.clicked.connect (() => {
Expand Down