How to validate a D-Bus property when using gdbus-codegen skeleton
<p>I'm creating a service on D-Bus using <code>gdbus</code> and <code>gdbus-codegen</code>. </p>

<p>Introspection is this:</p>

<pre><code>&lt;!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
                      "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"&gt;
&lt;node&gt;
  &lt;interface name="com.example.foo"&gt;

    &lt;property name="Bar" type="s" access="readwrite" /&gt;

  &lt;/interface&gt;
&lt;/node&gt;
</code></pre>

<p>I'm executing <code>gdbus-codegen</code> like this:</p>

<pre><code>gdbus-codegen --interface-prefix com.example --generate-c-code=foo foo.xml
</code></pre>

<p>And my main.cpp looks like this:</p>

<pre><code>#include &lt;iostream&gt;

#include "foo.h"

void OnBarChanged(GObject * gobject, GParamSpec * pspec, gpointer user_data)
{
  std::cout &lt;&lt; "Bar: " &lt;&lt; foo_get_bar((Foo *)gobject) &lt;&lt; std::endl;
}

void OnBusNameAquired(GDBusConnection * connection,
                      const gchar *     name,
                      gpointer          user_data)
{
  Foo * foo = foo_skeleton_new();

  g_signal_connect(foo, "notify::bar", G_CALLBACK(&amp;OnBarChanged), NULL);

  g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(foo),
                                   connection,
                                   "/com/example/foo",
                                   NULL);
}

int main()
{
  std::cout &lt;&lt; "Testing DBus properties" &lt;&lt; std::endl;

  GMainLoop * loop;
  loop = g_main_loop_new(NULL, FALSE);
  g_bus_own_name(G_BUS_TYPE_SESSION,
                 "com.example.foo",
                 G_BUS_NAME_OWNER_FLAGS_NONE,
                 NULL,
                 OnBusNameAquired,
                 NULL,
                 NULL,
                 NULL);
  g_main_loop_run(loop);
  return 0;
}
</code></pre>

<p>This works as expected and I'm able to set and get properties using:</p>

<pre><code>gdbus call --session --dest com.example.foo --object-path /com/example/foo --method org.freedesktop.DBus.Properties.Set "com.example.foo" "Bar" "&lt;'baz'&gt;"
</code></pre>

<p>and</p>

<pre><code>gdbus call --session --dest com.example.foo --object-path /com/example/foo --method org.freedesktop.DBus.Properties.Get "com.example.foo" "Bar"
(&lt;'baz'&gt;,)
</code></pre>

<p><strong>The problem:</strong></p>

<p>I want to validate the setting of the property synchronously and be able to return an error if it fails. How can this be accomplished using the <code>gdbus-codegen</code>-generated code?</p>

<p><strong>PS:</strong></p>

<p>The code leaks and is generally not production ready. I'm fine with that for now :-)</p>

<p><strong>Edit</strong></p>

<p>After continues research, it seems like the D-Bus properties are using the underlying <code>GObject</code> property feature. Is it possible to install a custom validator when all of this is set up by the <code>gdbus-codegen</code>-code?</p>