diff --git a/src/mconnect/battery-proxy.vala b/src/mconnect/battery-proxy.vala index 14da156..cd6b08c 100644 --- a/src/mconnect/battery-proxy.vala +++ b/src/mconnect/battery-proxy.vala @@ -24,6 +24,7 @@ class BatteryHandlerProxy : Object, PacketHandlerInterfaceProxy { private Device device = null; private BatteryHandler battery_handler = null; private uint register_id = 0; + private ulong notify_id = 0; public uint level { get; private set; default = 0; } public bool charging { get; private set; default = false; } @@ -51,6 +52,10 @@ class BatteryHandlerProxy : Object, PacketHandlerInterfaceProxy { public void bus_register(DBusConnection conn, string path) throws IOError { if (this.register_id == 0) this.register_id = conn.register_object(path, this); + + this.notify_id = this.notify.connect((spec) => { + this.send_property_change(conn, path, spec); + }); } [DBus (visible = false)] @@ -58,7 +63,39 @@ class BatteryHandlerProxy : Object, PacketHandlerInterfaceProxy { if (this.register_id != 0) conn.unregister_object(this.register_id); this.register_id = 0; + + this.disconnect(this.notify_id); + this.notify_id = 0; } public signal void battery(uint level, bool charging); + + private void send_property_change(DBusConnection conn, string path, ParamSpec p) { + var builder = new VariantBuilder (VariantType.ARRAY); + var invalid_builder = new VariantBuilder (new VariantType ("as")); + + if (p.name == "level") { + Variant i = this.level; + builder.add ("{sv}", "level", i); + } + + if (p.name == "charging") { + Variant i = this.charging; + builder.add ("{sv}", "charging", i); + } + + try { + conn.emit_signal(null, + path, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + new Variant ("(sa{sv}as)", + "org.mconnect.Device.Battery", + builder, + invalid_builder) + ); + } catch (Error e) { + warning("%s\n", e.message); + } + } } \ No newline at end of file