Dbus a{sv} – sending and receiving a hash with a variant

Passing data as a hash over the DBus interface is a very flexible, easy and extensible way to interact with other programs.
When looking for examples online my google-fu did not yield any result. Many DBus howtos and GTK documents just end before it gets really interesting.

I wasted more time on searching for documents than on experimenting and figuring it out myself. To make it easier for everyone after me, here a short description.

The examples are taken from a code I am writing for seahorse. I removed some parts not relevant for the examples to guide you to the interesting stuff..

dbus-send

Forget it. The format is to complex for the version of dbus-send I have. I had to write my own python sender to test my program interface.

Python sender

#!/usr/bin/env python
import dbus

bus=dbus.SessionBus()

genkey = dbus.Interface(bus.get_object('org.gnome.seahorse','/org/gnome/seahorse/keys'),

"org.gnome.seahorse.KeyService")

rskey = genkey.GenerateCredentials("first_item",({"name":dbus.String("thorsten",variant_level=1),

"email":dbus.String("thorsten@foo.bar",variant_level=1)}))

The XML interface definition

<method name="GenerateCredentials">
   <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="seahorse_service_generate_credentials"/>
   <arg type="s" name="keytype" direction="in"/>
   <arg type="a{sv}" name="values" direction="in"/>
   <arg type="s" name="key" direction="out"/>                        
 </method>

The C Code that receives the dbus call

generate_credentials (...GHashTable *values,...)
{
    gchar   *name=NULL;
    gchar   *email=NULL;

    pval = (GValue *)g_hash_table_lookup(values,"name");
    if ((pval) && (G_VALUE_TYPE (pval) == G_TYPE_STRING))
        name=g_value_dup_string (pval);

    pval = g_hash_table_lookup(values,"email");
    if ((pval) && (G_VALUE_TYPE (pval) == G_TYPE_STRING))
        email=g_value_dup_string (pval);
}
Advertisements

~ by thorstensick on October 27, 2009.

One Response to “Dbus a{sv} – sending and receiving a hash with a variant”

  1. Great! I was not actually looking to this, but it is nice to know how this interface works.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: