4 /* ---------------------------------------------------------------------------------------------------- */
6 /* The object we want to export */
7 typedef struct _MyObjectClass MyObjectClass;
8 typedef struct _MyObject MyObject;
12 GObjectClass parent_class;
17 GObject parent_instance;
30 G_DEFINE_TYPE (MyObject, my_object, G_TYPE_OBJECT);
33 my_object_finalize (GObject *object)
35 MyObject *myobj = (MyObject*)object;
39 G_OBJECT_CLASS (my_object_parent_class)->finalize (object);
43 my_object_init (MyObject *object)
50 my_object_get_property (GObject *object,
55 MyObject *myobj = (MyObject*)object;
60 g_value_set_int (value, myobj->count);
64 g_value_set_string (value, myobj->name);
68 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
73 my_object_set_property (GObject *object,
78 MyObject *myobj = (MyObject*)object;
83 myobj->count = g_value_get_int (value);
88 myobj->name = g_value_dup_string (value);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 my_object_class_init (MyObjectClass *class)
99 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
101 gobject_class->finalize = my_object_finalize;
102 gobject_class->set_property = my_object_set_property;
103 gobject_class->get_property = my_object_get_property;
105 g_object_class_install_property (gobject_class,
107 g_param_spec_int ("count",
113 g_object_class_install_property (gobject_class,
115 g_param_spec_string ("name",
122 /* A method that we want to export */
124 my_object_change_count (MyObject *myobj,
127 myobj->count = 2 * myobj->count + change;
129 g_object_notify (G_OBJECT (myobj), "count");
132 /* ---------------------------------------------------------------------------------------------------- */
134 static GDBusNodeInfo *introspection_data = NULL;
136 /* Introspection data for the service we are exporting */
137 static const gchar introspection_xml[] =
139 " <interface name='org.myorg.MyObject'>"
140 " <method name='ChangeCount'>"
141 " <arg type='i' name='change' direction='in'/>"
143 " <property type='i' name='Count' access='read'/>"
144 " <property type='s' name='Name' access='readwrite'/>"
150 handle_method_call (GDBusConnection *connection,
152 const gchar *object_path,
153 const gchar *interface_name,
154 const gchar *method_name,
155 GVariant *parameters,
156 GDBusMethodInvocation *invocation,
159 MyObject *myobj = user_data;
161 if (g_strcmp0 (method_name, "ChangeCount") == 0)
164 g_variant_get (parameters, "(i)", &change);
166 my_object_change_count (myobj, change);
168 g_dbus_method_invocation_return_value (invocation, NULL);
173 handle_get_property (GDBusConnection *connection,
175 const gchar *object_path,
176 const gchar *interface_name,
177 const gchar *property_name,
182 MyObject *myobj = user_data;
185 if (g_strcmp0 (property_name, "Count") == 0)
187 ret = g_variant_new_int32 (myobj->count);
189 else if (g_strcmp0 (property_name, "Name") == 0)
191 ret = g_variant_new_string (myobj->name ? myobj->name : "");
198 handle_set_property (GDBusConnection *connection,
200 const gchar *object_path,
201 const gchar *interface_name,
202 const gchar *property_name,
207 MyObject *myobj = user_data;
209 if (g_strcmp0 (property_name, "Count") == 0)
211 g_object_set (myobj, "count", g_variant_get_int32 (value), NULL);
213 else if (g_strcmp0 (property_name, "Name") == 0)
215 g_object_set (myobj, "name", g_variant_get_string (value, NULL), NULL);
223 static const GDBusInterfaceVTable interface_vtable =
231 send_property_change (GObject *obj,
233 GDBusConnection *connection)
235 GVariantBuilder *builder;
236 GVariantBuilder *invalidated_builder;
237 MyObject *myobj = (MyObject *)obj;
239 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
240 invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
242 if (g_strcmp0 (pspec->name, "count") == 0)
243 g_variant_builder_add (builder,
245 "Count", g_variant_new_int32 (myobj->count));
246 else if (g_strcmp0 (pspec->name, "name") == 0)
247 g_variant_builder_add (builder,
249 "Name", g_variant_new_string (myobj->name ? myobj->name : ""));
251 g_dbus_connection_emit_signal (connection,
253 "/org/myorg/MyObject",
254 "org.freedesktop.DBus.Properties",
256 g_variant_new ("(sa{sv}as)",
257 "org.myorg.MyObject",
259 invalidated_builder),
264 on_bus_acquired (GDBusConnection *connection,
268 MyObject *myobj = user_data;
269 guint registration_id;
271 g_signal_connect (myobj, "notify",
272 G_CALLBACK (send_property_change), connection);
273 registration_id = g_dbus_connection_register_object (connection,
274 "/org/myorg/MyObject",
275 "org.myorg.MyObject",
276 introspection_data->interfaces[0],
279 NULL, /* user_data_free_func */
280 NULL); /* GError** */
281 g_assert (registration_id > 0);
285 on_name_acquired (GDBusConnection *connection,
292 on_name_lost (GDBusConnection *connection,
300 main (int argc, char *argv[])
308 /* We are lazy here - we don't want to manually provide
309 * the introspection data structures - so we just build
312 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
313 g_assert (introspection_data != NULL);
315 myobj = g_object_new (my_object_get_type (), NULL);
317 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
318 "org.myorg.MyObject",
319 G_BUS_NAME_OWNER_FLAGS_NONE,
326 loop = g_main_loop_new (NULL, FALSE);
327 g_main_loop_run (loop);
329 g_bus_unown_name (owner_id);
331 g_dbus_node_info_unref (introspection_data);
333 g_object_unref (myobj);