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 static GDBusNodeInfo *introspection_data = NULL;
134 /* Introspection data for the service we are exporting */
135 static const gchar introspection_xml[] =
137 " <interface name='org.myorg.MyObject'>"
138 " <method name='ChangeCount'>"
139 " <arg type='i' name='change' direction='in'/>"
141 " <property type='i' name='Count' access='read'/>"
142 " <property type='s' name='Name' access='readwrite'/>"
148 handle_method_call (GDBusConnection *connection,
150 const gchar *object_path,
151 const gchar *interface_name,
152 const gchar *method_name,
153 GVariant *parameters,
154 GDBusMethodInvocation *invocation,
157 MyObject *myobj = user_data;
159 if (g_strcmp0 (method_name, "ChangeCount") == 0)
162 g_variant_get (parameters, "(i)", &change);
164 my_object_change_count (myobj, change);
166 g_dbus_method_invocation_return_value (invocation, NULL);
171 handle_get_property (GDBusConnection *connection,
173 const gchar *object_path,
174 const gchar *interface_name,
175 const gchar *property_name,
180 MyObject *myobj = user_data;
183 if (g_strcmp0 (property_name, "Count") == 0)
185 ret = g_variant_new_int32 (myobj->count);
187 else if (g_strcmp0 (property_name, "Name") == 0)
189 ret = g_variant_new_string (myobj->name ? myobj->name : "");
196 handle_set_property (GDBusConnection *connection,
198 const gchar *object_path,
199 const gchar *interface_name,
200 const gchar *property_name,
205 MyObject *myobj = user_data;
207 if (g_strcmp0 (property_name, "Count") == 0)
209 g_object_set (myobj, "count", g_variant_get_int32 (value), NULL);
211 else if (g_strcmp0 (property_name, "Name") == 0)
213 g_object_set (myobj, "name", g_variant_get_string (value, NULL), NULL);
221 static const GDBusInterfaceVTable interface_vtable =
229 send_property_change (GObject *obj,
231 GDBusConnection *connection)
233 GVariantBuilder *builder;
234 MyObject *myobj = (MyObject *)obj;
236 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
238 if (g_strcmp0 (pspec->name, "count") == 0)
239 g_variant_builder_add (builder,
241 "Count", g_variant_new_int32 (myobj->count));
242 else if (g_strcmp0 (pspec->name, "name") == 0)
243 g_variant_builder_add (builder,
245 "Name", g_variant_new_string (myobj->name ? myobj->name : ""));
247 g_dbus_connection_emit_signal (connection,
249 "/org/myorg/MyObject",
250 "org.myorg.MyObject",
252 g_variant_new ("(sa{sv})",
253 "org.myorg.MyObject",
259 on_bus_acquired (GDBusConnection *connection,
263 MyObject *myobj = user_data;
264 guint registration_id;
266 g_signal_connect (myobj, "notify",
267 G_CALLBACK (send_property_change), connection);
268 registration_id = g_dbus_connection_register_object (connection,
269 "/org/myorg/MyObject",
270 "org.myorg.MyObject",
271 introspection_data->interfaces[0],
274 NULL, /* user_data_free_func */
275 NULL); /* GError** */
276 g_assert (registration_id > 0);
280 on_name_acquired (GDBusConnection *connection,
287 on_name_lost (GDBusConnection *connection,
295 main (int argc, char *argv[])
303 /* We are lazy here - we don't want to manually provide
304 * the introspection data structures - so we just build
307 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
308 g_assert (introspection_data != NULL);
310 myobj = g_object_new (my_object_get_type (), NULL);
312 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
313 "org.myorg.MyObject",
314 G_BUS_NAME_OWNER_FLAGS_NONE,
321 loop = g_main_loop_new (NULL, FALSE);
322 g_main_loop_run (loop);
324 g_bus_unown_name (owner_id);
326 g_dbus_node_info_unref (introspection_data);
328 g_object_unref (myobj);