Browse Source

battery-proxy: use DBusPropertyNotifier helper for sending property notifications

bboozzoo/dbus-support-wip
Maciek Borzecki 8 years ago
parent
commit
99dd419d6a
1 changed files with 23 additions and 31 deletions
  1. +23
    -31
      src/mconnect/battery-proxy.vala

+ 23
- 31
src/mconnect/battery-proxy.vala View File

@ -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);
}
}

Loading…
Cancel
Save