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 static GType
my_object_get_type (void);
31 G_DEFINE_TYPE (MyObject
, my_object
, G_TYPE_OBJECT
);
34 my_object_finalize (GObject
*object
)
36 MyObject
*myobj
= (MyObject
*)object
;
40 G_OBJECT_CLASS (my_object_parent_class
)->finalize (object
);
44 my_object_init (MyObject
*object
)
51 my_object_get_property (GObject
*object
,
56 MyObject
*myobj
= (MyObject
*)object
;
61 g_value_set_int (value
, myobj
->count
);
65 g_value_set_string (value
, myobj
->name
);
69 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
74 my_object_set_property (GObject
*object
,
79 MyObject
*myobj
= (MyObject
*)object
;
84 myobj
->count
= g_value_get_int (value
);
89 myobj
->name
= g_value_dup_string (value
);
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
98 my_object_class_init (MyObjectClass
*class)
100 GObjectClass
*gobject_class
= G_OBJECT_CLASS (class);
102 gobject_class
->finalize
= my_object_finalize
;
103 gobject_class
->set_property
= my_object_set_property
;
104 gobject_class
->get_property
= my_object_get_property
;
106 g_object_class_install_property (gobject_class
,
108 g_param_spec_int ("count",
114 g_object_class_install_property (gobject_class
,
116 g_param_spec_string ("name",
123 /* A method that we want to export */
125 my_object_change_count (MyObject
*myobj
,
128 myobj
->count
= 2 * myobj
->count
+ change
;
130 g_object_notify (G_OBJECT (myobj
), "count");
133 /* ---------------------------------------------------------------------------------------------------- */
135 static GDBusNodeInfo
*introspection_data
= NULL
;
137 /* Introspection data for the service we are exporting */
138 static const gchar introspection_xml
[] =
140 " <interface name='org.myorg.MyObject'>"
141 " <method name='ChangeCount'>"
142 " <arg type='i' name='change' direction='in'/>"
144 " <property type='i' name='Count' access='read'/>"
145 " <property type='s' name='Name' access='readwrite'/>"
151 handle_method_call (GDBusConnection
*connection
,
153 const gchar
*object_path
,
154 const gchar
*interface_name
,
155 const gchar
*method_name
,
156 GVariant
*parameters
,
157 GDBusMethodInvocation
*invocation
,
160 MyObject
*myobj
= user_data
;
162 if (g_strcmp0 (method_name
, "ChangeCount") == 0)
165 g_variant_get (parameters
, "(i)", &change
);
167 my_object_change_count (myobj
, change
);
169 g_dbus_method_invocation_return_value (invocation
, NULL
);
174 handle_get_property (GDBusConnection
*connection
,
176 const gchar
*object_path
,
177 const gchar
*interface_name
,
178 const gchar
*property_name
,
183 MyObject
*myobj
= user_data
;
186 if (g_strcmp0 (property_name
, "Count") == 0)
188 ret
= g_variant_new_int32 (myobj
->count
);
190 else if (g_strcmp0 (property_name
, "Name") == 0)
192 ret
= g_variant_new_string (myobj
->name
? myobj
->name
: "");
199 handle_set_property (GDBusConnection
*connection
,
201 const gchar
*object_path
,
202 const gchar
*interface_name
,
203 const gchar
*property_name
,
208 MyObject
*myobj
= user_data
;
210 if (g_strcmp0 (property_name
, "Count") == 0)
212 g_object_set (myobj
, "count", g_variant_get_int32 (value
), NULL
);
214 else if (g_strcmp0 (property_name
, "Name") == 0)
216 g_object_set (myobj
, "name", g_variant_get_string (value
, NULL
), NULL
);
224 static const GDBusInterfaceVTable interface_vtable
=
232 send_property_change (GObject
*obj
,
234 GDBusConnection
*connection
)
236 GVariantBuilder
*builder
;
237 GVariantBuilder
*invalidated_builder
;
238 MyObject
*myobj
= (MyObject
*)obj
;
240 builder
= g_variant_builder_new (G_VARIANT_TYPE_ARRAY
);
241 invalidated_builder
= g_variant_builder_new (G_VARIANT_TYPE ("as"));
243 if (g_strcmp0 (pspec
->name
, "count") == 0)
244 g_variant_builder_add (builder
,
246 "Count", g_variant_new_int32 (myobj
->count
));
247 else if (g_strcmp0 (pspec
->name
, "name") == 0)
248 g_variant_builder_add (builder
,
250 "Name", g_variant_new_string (myobj
->name
? myobj
->name
: ""));
252 g_dbus_connection_emit_signal (connection
,
254 "/org/myorg/MyObject",
255 "org.freedesktop.DBus.Properties",
257 g_variant_new ("(sa{sv}as)",
258 "org.myorg.MyObject",
260 invalidated_builder
),
265 on_bus_acquired (GDBusConnection
*connection
,
269 MyObject
*myobj
= user_data
;
270 guint registration_id
;
272 g_signal_connect (myobj
, "notify",
273 G_CALLBACK (send_property_change
), connection
);
274 registration_id
= g_dbus_connection_register_object (connection
,
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
[])
306 /* We are lazy here - we don't want to manually provide
307 * the introspection data structures - so we just build
310 introspection_data
= g_dbus_node_info_new_for_xml (introspection_xml
, NULL
);
311 g_assert (introspection_data
!= NULL
);
313 myobj
= g_object_new (my_object_get_type (), NULL
);
315 owner_id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
316 "org.myorg.MyObject",
317 G_BUS_NAME_OWNER_FLAGS_NONE
,
324 loop
= g_main_loop_new (NULL
, FALSE
);
325 g_main_loop_run (loop
);
327 g_bus_unown_name (owner_id
);
329 g_dbus_node_info_unref (introspection_data
);
331 g_object_unref (myobj
);