From 99dd419d6a3dd3a3d0a11fc11291c821a693fb55 Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Tue, 20 Jun 2017 21:25:58 +0200 Subject: [PATCH] battery-proxy: use DBusPropertyNotifier helper for sending property notifications --- src/mconnect/battery-proxy.vala | 54 ++++++++++++++------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/src/mconnect/battery-proxy.vala b/src/mconnect/battery-proxy.vala index cd6b08c..0f02e82 100644 --- a/src/mconnect/battery-proxy.vala +++ b/src/mconnect/battery-proxy.vala @@ -25,6 +25,7 @@ class BatteryHandlerProxy : Object, PacketHandlerInterfaceProxy { private BatteryHandler battery_handler = null; private uint register_id = 0; private ulong notify_id = 0; + private DBusPropertyNotifier prop_notifier = null; public uint level { get; private set; default = 0; } public bool charging { get; private set; default = false; } @@ -53,9 +54,11 @@ class BatteryHandlerProxy : Object, PacketHandlerInterfaceProxy { 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); - }); + this.prop_notifier = new DBusPropertyNotifier(conn, + "org.mconnect.Device.Battery", + path); + + this.notify.connect(this.send_property_change); } [DBus (visible = false)] @@ -64,38 +67,27 @@ class BatteryHandlerProxy : Object, PacketHandlerInterfaceProxy { conn.unregister_object(this.register_id); this.register_id = 0; - this.disconnect(this.notify_id); + this.notify.disconnect(this.send_property_change); 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); - } + private void send_property_change(ParamSpec p) { + assert(this.prop_notifier != null); + + Variant v = null; + + if (p.name == "level") { + v = this.level; + } + if (p.name == "charging") { + v = this.charging; + } + + if (v == null) + return; + + this.prop_notifier.queue_property_change(p.name, v); } } \ No newline at end of file