4 /* ---------------------------------------------------------------------------------------------------- */
5 /* The D-Bus interface definition we want to create a GDBusProxy-derived type for: */
6 /* ---------------------------------------------------------------------------------------------------- */
8 static const gchar introspection_xml[] =
10 " <interface name='org.freedesktop.Accounts.User'>"
11 " <method name='Frobnicate'>"
12 " <arg name='flux' type='s' direction='in'/>"
13 " <arg name='baz' type='s' direction='in'/>"
14 " <arg name='result' type='s' direction='out'/>"
16 " <signal name='Changed'/>"
17 " <property name='AutomaticLogin' type='b' access='readwrite'/>"
18 " <property name='RealName' type='s' access='read'/>"
19 " <property name='UserName' type='s' access='read'/>"
23 /* ---------------------------------------------------------------------------------------------------- */
24 /* Definition of the AccountsUser type */
25 /* ---------------------------------------------------------------------------------------------------- */
27 #define ACCOUNTS_TYPE_USER (accounts_user_get_type ())
28 #define ACCOUNTS_USER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), ACCOUNTS_TYPE_USER, AccountsUser))
29 #define ACCOUNTS_USER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), ACCOUNTS_TYPE_USER, AccountsUserClass))
30 #define ACCOUNTS_USER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), ACCOUNTS_TYPE_USER, AccountsUserClass))
31 #define ACCOUNTS_IS_USER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), ACCOUNTS_TYPE_USER))
32 #define ACCOUNTS_IS_USER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), ACCOUNTS_TYPE_USER))
34 typedef struct _AccountsUser AccountsUser;
35 typedef struct _AccountsUserClass AccountsUserClass;
36 typedef struct _AccountsUserPrivate AccountsUserPrivate;
41 GDBusProxy parent_instance;
42 AccountsUserPrivate *priv;
45 struct _AccountsUserClass
48 GDBusProxyClass parent_class;
49 void (*changed) (AccountsUser *user);
52 GType accounts_user_get_type (void) G_GNUC_CONST;
54 const gchar *accounts_user_get_user_name (AccountsUser *user);
55 const gchar *accounts_user_get_real_name (AccountsUser *user);
56 gboolean accounts_user_get_automatic_login (AccountsUser *user);
58 void accounts_user_frobnicate (AccountsUser *user,
61 GCancellable *cancellable,
62 GAsyncReadyCallback callback,
64 gchar *accounts_user_frobnicate_finish (AccountsUser *user,
67 gchar *accounts_user_frobnicate_sync (AccountsUser *user,
70 GCancellable *cancellable,
73 /* ---------------------------------------------------------------------------------------------------- */
74 /* Implementation of the AccountsUser type */
75 /* ---------------------------------------------------------------------------------------------------- */
77 /* A more efficient approach than parsing XML is to use const static
78 * GDBusInterfaceInfo, GDBusMethodInfo, ... structures
80 static GDBusInterfaceInfo *
81 accounts_user_get_interface_info (void)
83 static gsize has_info = 0;
84 static GDBusInterfaceInfo *info = NULL;
85 if (g_once_init_enter (&has_info))
87 GDBusNodeInfo *introspection_data;
88 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
89 info = introspection_data->interfaces[0];
90 g_once_init_leave (&has_info, 1);
100 PROP_AUTOMATIC_LOGIN,
109 static guint signals[LAST_SIGNAL] = {0};
111 G_DEFINE_TYPE (AccountsUser, accounts_user, G_TYPE_DBUS_PROXY);
114 accounts_user_finalize (GObject *object)
116 G_GNUC_UNUSED AccountsUser *user = ACCOUNTS_USER (object);
118 if (G_OBJECT_CLASS (accounts_user_parent_class)->finalize != NULL)
119 G_OBJECT_CLASS (accounts_user_parent_class)->finalize (object);
123 accounts_user_init (AccountsUser *user)
125 /* Sets the expected interface */
126 g_dbus_proxy_set_interface_info (G_DBUS_PROXY (user), accounts_user_get_interface_info ());
130 accounts_user_get_property (GObject *object,
135 AccountsUser *user = ACCOUNTS_USER (object);
140 g_value_set_string (value, accounts_user_get_user_name (user));
144 g_value_set_string (value, accounts_user_get_real_name (user));
147 case PROP_AUTOMATIC_LOGIN:
148 g_value_set_boolean (value, accounts_user_get_automatic_login (user));
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 accounts_user_get_user_name (AccountsUser *user)
162 g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
163 value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "UserName");
164 ret = g_variant_get_string (value, NULL);
165 g_variant_unref (value);
170 accounts_user_get_real_name (AccountsUser *user)
174 g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
175 value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "RealName");
176 ret = g_variant_get_string (value, NULL);
177 g_variant_unref (value);
182 accounts_user_get_automatic_login (AccountsUser *user)
186 g_return_val_if_fail (ACCOUNTS_IS_USER (user), FALSE);
187 value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "AutomaticLogin");
188 ret = g_variant_get_boolean (value);
189 g_variant_unref (value);
194 accounts_user_g_signal (GDBusProxy *proxy,
195 const gchar *sender_name,
196 const gchar *signal_name,
197 GVariant *parameters)
199 AccountsUser *user = ACCOUNTS_USER (proxy);
200 if (g_strcmp0 (signal_name, "Changed") == 0)
201 g_signal_emit (user, signals[CHANGED_SIGNAL], 0);
205 accounts_user_g_properties_changed (GDBusProxy *proxy,
206 GVariant *changed_properties,
207 const gchar* const *invalidated_properties)
209 AccountsUser *user = ACCOUNTS_USER (proxy);
213 if (changed_properties != NULL)
215 g_variant_get (changed_properties, "a{sv}", &iter);
216 while ((item = g_variant_iter_next_value (iter)) != NULL)
223 if (g_strcmp0 (key, "AutomaticLogin") == 0)
224 g_object_notify (G_OBJECT (user), "automatic-login");
225 else if (g_strcmp0 (key, "RealName") == 0)
226 g_object_notify (G_OBJECT (user), "real-name");
227 else if (g_strcmp0 (key, "UserName") == 0)
228 g_object_notify (G_OBJECT (user), "user-name");
234 accounts_user_class_init (AccountsUserClass *klass)
236 GObjectClass *gobject_class;
237 GDBusProxyClass *proxy_class;
239 gobject_class = G_OBJECT_CLASS (klass);
240 gobject_class->get_property = accounts_user_get_property;
241 gobject_class->finalize = accounts_user_finalize;
243 proxy_class = G_DBUS_PROXY_CLASS (klass);
244 proxy_class->g_signal = accounts_user_g_signal;
245 proxy_class->g_properties_changed = accounts_user_g_properties_changed;
247 g_object_class_install_property (gobject_class,
249 g_param_spec_string ("user-name",
251 "The user name of the user",
254 G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_string ("real-name",
260 "The real name of the user",
263 G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class,
266 PROP_AUTOMATIC_LOGIN,
267 g_param_spec_boolean ("automatic-login",
269 "Whether the user is automatically logged in",
272 G_PARAM_STATIC_STRINGS));
274 signals[CHANGED_SIGNAL] = g_signal_new ("changed",
277 G_STRUCT_OFFSET (AccountsUserClass, changed),
280 g_cclosure_marshal_VOID__VOID,
286 accounts_user_frobnicate_sync (AccountsUser *user,
289 GCancellable *cancellable,
295 g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
299 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (user),
301 g_variant_new ("(si)",
304 G_DBUS_CALL_FLAGS_NONE,
310 g_variant_get (value, "(s)", &ret);
311 ret = g_strdup (ret);
312 g_variant_unref (value);
318 accounts_user_frobnicate (AccountsUser *user,
321 GCancellable *cancellable,
322 GAsyncReadyCallback callback,
325 g_return_if_fail (ACCOUNTS_IS_USER (user));
326 g_dbus_proxy_call (G_DBUS_PROXY (user),
328 g_variant_new ("(si)",
331 G_DBUS_CALL_FLAGS_NONE,
340 accounts_user_frobnicate_finish (AccountsUser *user,
348 value = g_dbus_proxy_call_finish (G_DBUS_PROXY (user), res, error);
351 g_variant_get (value, "(s)", &ret);
352 ret = g_strdup (ret);
353 g_variant_unref (value);
358 /* ---------------------------------------------------------------------------------------------------- */
359 /* Example usage of the AccountsUser type */
360 /* ---------------------------------------------------------------------------------------------------- */
363 print_user (AccountsUser *user)
365 g_print (" user-name = `%s'\n", accounts_user_get_user_name (user));
366 g_print (" real-name = `%s'\n", accounts_user_get_real_name (user));
367 g_print (" automatic-login = %s\n", accounts_user_get_automatic_login (user) ? "true" : "false");
371 on_changed (AccountsUser *user,
374 g_print ("+++ Received the AccountsUser::changed signal\n");
379 on_notify (GObject *object,
383 AccountsUser *user = ACCOUNTS_USER (object);
384 g_print ("+++ Received the GObject::notify signal for property `%s'\n",
390 on_proxy_appeared (GDBusConnection *connection,
392 const gchar *name_owner,
396 AccountsUser *user = ACCOUNTS_USER (proxy);
398 g_print ("+++ Acquired proxy for user\n");
401 g_signal_connect (proxy,
403 G_CALLBACK (on_notify),
405 g_signal_connect (user,
407 G_CALLBACK (on_changed),
412 on_proxy_vanished (GDBusConnection *connection,
416 g_print ("--- Cannot create proxy for user: no remote object\n");
419 /* ---------------------------------------------------------------------------------------------------- */
422 main (gint argc, gchar *argv[])
429 watcher_id = g_bus_watch_proxy (G_BUS_TYPE_SYSTEM,
430 "org.freedesktop.Accounts",
431 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
432 "/org/freedesktop/Accounts/User500",
433 "org.freedesktop.Accounts.User",
435 G_DBUS_PROXY_FLAGS_NONE,
441 loop = g_main_loop_new (NULL, FALSE);
442 g_main_loop_run (loop);
443 g_main_loop_unref (loop);
444 g_bus_unwatch_proxy (watcher_id);