Browse Source

property-proxy: helper class for sending out DBus PropertiesChanged signals

bboozzoo/dbus-support-wip
Maciek Borzecki 8 years ago
parent
commit
74378335f5
2 changed files with 103 additions and 1 deletions
  1. +2
    -1
      Makefile.am
  2. +101
    -0
      src/mconnect/property-proxy.vala

+ 2
- 1
Makefile.am View File

@ -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 \


+ 101
- 0
src/mconnect/property-proxy.vala View File

@ -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 <maciek.borzecki (at] gmail.com>
*/
/**
* 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;
}
}

Loading…
Cancel
Save