From 74378335f529d237742841cd7bd62d1dccdcc8ec Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Tue, 20 Jun 2017 21:25:18 +0200 Subject: [PATCH] property-proxy: helper class for sending out DBus PropertiesChanged signals --- Makefile.am | 3 +- src/mconnect/property-proxy.vala | 101 +++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/mconnect/property-proxy.vala diff --git a/Makefile.am b/Makefile.am index 5620a01..4cced10 100644 --- a/Makefile.am +++ b/Makefile.am @@ -76,7 +76,8 @@ mconnect_SOURCES = \ src/mconnect/ping-proxy.vala \ src/mconnect/config.vala \ src/mconnect/application.vala \ - src/mconnect/utils.vala + src/mconnect/utils.vala \ + src/mconnect/property-proxy.vala mconnect_LDADD = \ libmconn-crypt.la \ diff --git a/src/mconnect/property-proxy.vala b/src/mconnect/property-proxy.vala new file mode 100644 index 0000000..3ab4778 --- /dev/null +++ b/src/mconnect/property-proxy.vala @@ -0,0 +1,101 @@ +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * AUTHORS + * Maciek Borzecki + */ + +/** + * DBusPropertyNotifier: + * + * Helper class for pushing out + * org.freedesktop.DBus.Properties.PropertiesChanged signals. + */ +class DBusPropertyNotifier : Object { + + private DBusConnection conn = null; + private string iface = ""; + private string path = ""; + private VariantBuilder builder = null; + private uint timeout_src = 0; + + public const uint TIMEOUT = 300; + + public DBusPropertyNotifier(DBusConnection conn, + string iface, + string path) { + this.conn = conn; + this.iface = iface; + this.path = path; + } + + /** + * queue_property_change: + * + * @name: property name (will be automatically capitalized if needed) + * @val: Variant holding property value + * + * This method will queue up property notifications for sending. By default + * it waits @TIMEOUT ms before sending the actual signal. + */ + public void queue_property_change(string name, Variant val) { + if (this.builder == null) { + this.builder = new VariantBuilder(VariantType.ARRAY); + } + + string nm = name; + if (name.get_char(0).islower()) { + nm = name.get_char(0).toupper().to_string() + name.substring(1); + } + + this.builder.add("{sv}", nm, val); + + if (this.timeout_src == 0) { + this.timeout_src = Timeout.add(300, + this.send_property_change); + } + } + + /** + * send_property_change: + * + * Send out actual PropertiesChanged signals + */ + private bool send_property_change() { + this.timeout_src = 0; + + if (this.builder == null) + return false;; + + try { + var invalid_builder = new VariantBuilder(new VariantType ("as")); + + this.conn.emit_signal(null, + this.path, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + new Variant ("(sa{sv}as)", + this.iface, + builder, + invalid_builder) + ); + } catch (Error e) { + warning("%s\n", e.message); + } + + this.builder = null; + + return false; + } +} \ No newline at end of file