Hello, I've a situation like the one in the example that I've attached
here, where I need to make an asynchronous call to a method from a dbus
signal callback function; to perform that I need to use the yield
modifier, and so I need the callback function method should be async.

Now, when I connect the async method to the dbus interface with
somethink like:

session_proxy = (FreeDesktopDBus) connection.get_object
                        (FreeDesktopDBus.UNIQUE_NAME,
                         FreeDesktopDBus.OBJECT_PATH,
                         FreeDesktopDBus.INTERFACE_NAME);

session_proxy.name_acquired.connect (this.name_acquired);

Where name_acquired is defined as:

private async void name_acquired(FreeDesktopDBus sender, string name) {
}

I get an error:
        Cannot convert from `Test.name_acquired' to 
`FreeDesktopDBus.name_acquired

As a workaround I added two sync signal methods to the dbus interface,
but not using async calls is not a good think here...

Other ways? Thanks!

Bye.

PS: I'm using dbus-glib since the project I'm working on, uses it and I
can't change that.
using GLib;

[DBus (name = "org.freedesktop.DBus")]
public interface FreeDesktopDBus : GLib.Object
{
	public const string UNIQUE_NAME = "org.freedesktop.DBus";
	public const string OBJECT_PATH = "/org/freedesktop/DBus";
	public const string INTERFACE_NAME = "org.freedesktop.DBus";

	public abstract async string[] list_names () throws DBus.Error;
	public abstract async string[] list_activatable_names () throws DBus.Error;
	public abstract async string get_id () throws DBus.Error;

	public signal void name_lost (string name);
	public signal void name_acquired (string name);
	public signal void name_owner_changed (string name, string old_name, string new_name);
}

public class Test : GLib.Object {
	private List<string> owned_names;
	private List<string> activatable_names;
	FreeDesktopDBus session_proxy;

	public Test() {
		owned_names = new List<string>();
		activatable_names = new List<string>();
		session_proxy = null;
	}

	/* FIXME vala (at least 0.12) doesn't allow to call an async method!
	* until vala fixes it, add the new name to the activables one 
	* error is : Cannot convert from `Test.name_acquired' to
	* `FreeDesktopDBus.name_acquired' */
	private /*async*/ void name_acquired(FreeDesktopDBus sender, string name)
	{
		if (name.has_prefix (":")) return;

		//if (name in yield sender.list_names ())
			owned_names.append (name);
		//else if (name in yield sender.list_activatable_names ())
			activatable_names.append (name);
		printData();
	}
	
	private void name_owner_changed (FreeDesktopDBus sender,
	                                 string name,
	                                 string old_owner,
	                                 string new_owner)
	{
		if (name.has_prefix (":")) return;

		if (old_owner == "")
		{
			owned_names.append(name);
		}
		else if (new_owner == "")
		{
			owned_names.remove(name);
		}
		printData();
	}
	
	public async void init() {
		DBus.Connection connection;
		string[] names;
		try
		{
			connection = DBus.Bus.get (DBus.BusType.SESSION);
			session_proxy = (FreeDesktopDBus)
			connection.get_object (FreeDesktopDBus.UNIQUE_NAME,
			                       FreeDesktopDBus.OBJECT_PATH,
			                       FreeDesktopDBus.INTERFACE_NAME);

			session_proxy.name_owner_changed.connect (this.name_owner_changed);
			session_proxy.name_acquired.connect (this.name_acquired);
			session_proxy.name_lost.connect ((p, name) => {
				activatable_names.remove(name);
				owned_names.remove(name);
			});

			names = yield session_proxy.list_names ();
			foreach (unowned string name in names)
			{
				if (name.has_prefix (":")) continue;
					owned_names.append(name);
			}

			names = yield session_proxy.list_activatable_names ();
			foreach (unowned string session_act in names)
			{
				activatable_names.append(session_act);
			}
			
			this.printData();
		}
		catch (Error err)
		{
			error ("%s", err.message);
		}
	}

	private void printData()
	{
		if (session_proxy == null)
			this.init.begin();

		print("Session Names:\n");
		foreach(string s in owned_names)
			print(" - "+s+"\n");
		
		print("\n");

		print("Session Activatable Names:\n");
		foreach(string s in activatable_names)
			print(" - "+s+"\n");

		print("\n");
	}
}

int main(string[] args) {
	var t = new Test();
	t.init.begin();

	var loop = new GLib.MainLoop (null, false);
    loop.run();
}
_______________________________________________
vala-list mailing list
vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to