Re: [Vala] Using GDBus to Listen to Messages



Ok. That's easy ;)

The following example will add a subscription for a callback function
for the desired interface, owner, member, object path ...
null will subscribe all. The example subscribes "PropertiesChanged" with
"org.freedesktop.DBus.Properties" interface.
Please see gio documentation for more details.


class TestFilter : Object {
    private GLib.DBusConnection conn = null;
    private uint un;
    
    public TestFilter() {
        try {
            conn = Bus.get_sync(GLib.BusType.SESSION, null);
        }
        catch(IOError e) {
            print("%s", e.message);
        }
        un = conn.signal_subscribe(null,
"org.freedesktop.DBus.Properties", "PropertiesChanged", null, null,
DBusSignalFlags.NONE, sign_cb);
    }
    
    private void sign_cb(GLib.DBusConnection connection, string
sender_name, string object_path, string interface_name, string
signal_name, GLib.Variant parameters) {
        print("%s ::: %s  ::: %s\n", sender_name, interface_name,
signal_name);
    }

    public static int main () {
        var t = new TestFilter();
        new GLib.MainLoop(null, false).run();
        return 0;
    }
}






Thank you for the examples provided but that is not what I'm trying to accomplish. I am trying to capture 
all Desktop Notifications via DBUS. 

On 2011-02-24, at 3:46 AM, JM <interflug1 gmx net> wrote:

Hello
Here is a client notification example fitting the server notifications I
wrote some time ago(
http://live.gnome.org/Vala/DBusServerSample#Service_with_D-Bus_property_change_notifications
)
--------------
Client:

[DBus (name = "org.example.Demo")]
public interface Demo : Object {
   public abstract string pubprop { owned get; set; }
}

[DBus (name = "org.freedesktop.DBus.Properties")]
public interface NotifierProxy : Object {
   public abstract signal void PropertiesChanged (string ifc,
HashTable<string, Variant> ht, string[] inv_prop);
}

void main () {
   Demo demo = null;
   NotifierProxy np = null;
   try {
       demo = Bus.get_proxy_sync (BusType.SESSION, "org.example.Demo",

"/org/example/demo");
   } 
   catch (IOError e) {
       stderr.printf ("%s\n", e.message);
   }
   try {
       np = Bus.get_proxy_sync (BusType.SESSION, "org.example.Demo",

"/org/example/demo");
   } 
   catch (IOError er) {
       stderr.printf ("%s\n", er.message);
   }
   demo.pubprop = "jkfdgk";
   np.PropertiesChanged.connect( (x, ht, i) => { 
       print("PropertiesChanged for %s\n", x); 
   });
   new GLib.MainLoop().run();
}
-------------------


-------------------
Server:
[DBus (name = "org.example.Demo")]
public class DemoServer : Object {

   public string pubprop { owned get; set; }

   private weak DBusConnection conn;

   public DemoServer (DBusConnection conn) {
       this.conn = conn;
       this.notify.connect (send_property_change);
   }

   private void send_property_change (ParamSpec p) {
       var builder = new VariantBuilder (VariantType.ARRAY);
       var invalid_builder = new VariantBuilder (new VariantType
("as"));

       if (p.name == "pubprop") {
           Variant i = pubprop;
           builder.add ("{sv}", "pubprop", i);
       }

       try {
           conn.emit_signal (null, 
                             "/org/example/demo", 
                             "org.freedesktop.DBus.Properties", 
                             "PropertiesChanged", 
                             new Variant ("(sa{sv}as)", 
                                          "org.example.Demo", 
                                          builder, 
                                          invalid_builder)
                             );
       } catch (Error e) {
           stderr.printf ("%s\n", e.message);
       }
   }
}

public class NotificationsTest : Object {

   private DemoServer dserver;

   public NotificationsTest () {
       Bus.own_name (BusType.SESSION, "org.example.Demo",
BusNameOwnerFlags.NONE,
                     on_bus_acquired, on_name_acquired, on_name_lost);
   }

   private void on_bus_acquired (DBusConnection conn) {
       print ("bus acquired\n");
       try {
           this.dserver = new DemoServer (conn);
           conn.register_object ("/org/example/demo", this.dserver);
       } catch (IOError e) {
           print ("%s\n", e.message);
       }
   }

   private void on_name_acquired () {
       print ("name acquired\n");
   }  

   private void on_name_lost () {
       print ("name_lost\n");
   }

   public void setup_timeout () {
       Timeout.add_seconds (4, () => {
           dserver.pubprop = Random.next_int ().to_string ();
           return true;
       });
   }
}

void main () {
   var nt = new NotificationsTest ();
   nt.setup_timeout ();
   new MainLoop ().run ();
}
---------------------
Client and server should be compiled as usual with '--pkg gio-2.0'
option.
PS: I didn't check unpacking the PropertiesChanged args.
I also didn't check your code. 
Hope it still helps.

Best Regards





Hello,

I've been experimenting with the prowl protocol and transferring
notifications from a linux system to an iDevice with it. I've thus far come
up with a python proof of concept. I wanted to rewrite this into Vala as an
exercise for myself mostly.

A little background first on how I capture the notifications. I basically
use the message filter feature of DBus to get the notifications, filtering
for interface=org.freedesktop.Notifications and member=Notify.

This is how I've done so using Python: http://codepad.org/dauMCxXY

At first with Vala I came up with this using dbus-glib:
http://codepad.org/JiFvX4vK

But I've since found out that dbus-glib has been deprecated and is not
recommended for use. The alternative being GDBus. Thus I attempted to create
an equivalent program using it: http://codepad.org/RorMIB4O

Unfortunately, this does not work as expected, as in not capturing the
notifications as desired. I'm not sure how to this using GDBus. I've asked
around in #vala on GIMPNet as well as #gtk.
_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list







[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]