Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-example-proxy-subclass.c
blobfb1a5287939e57d727048e0a88d79ed0111446da
2 #include <gio/gio.h>
4 /* ---------------------------------------------------------------------------------------------------- */
5 /* The D-Bus interface definition we want to create a GDBusProxy-derived type for: */
6 /* ---------------------------------------------------------------------------------------------------- */
8 static const gchar introspection_xml[] =
9 "<node>"
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'/>"
15 " </method>"
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'/>"
20 " </interface>"
21 "</node>";
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;
38 struct _AccountsUser
40 /*< private >*/
41 GDBusProxy parent_instance;
42 AccountsUserPrivate *priv;
45 struct _AccountsUserClass
47 /*< private >*/
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,
59 const gchar *flux,
60 gint baz,
61 GCancellable *cancellable,
62 GAsyncReadyCallback callback,
63 gpointer user_data);
64 gchar *accounts_user_frobnicate_finish (AccountsUser *user,
65 GAsyncResult *res,
66 GError **error);
67 gchar *accounts_user_frobnicate_sync (AccountsUser *user,
68 const gchar *flux,
69 gint baz,
70 GCancellable *cancellable,
71 GError **error);
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);
92 return info;
95 enum
97 PROP_0,
98 PROP_USER_NAME,
99 PROP_REAL_NAME,
100 PROP_AUTOMATIC_LOGIN,
103 enum
105 CHANGED_SIGNAL,
106 LAST_SIGNAL
109 static guint signals[LAST_SIGNAL] = {0};
111 G_DEFINE_TYPE (AccountsUser, accounts_user, G_TYPE_DBUS_PROXY);
113 static void
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);
122 static void
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 ());
129 static void
130 accounts_user_get_property (GObject *object,
131 guint prop_id,
132 GValue *value,
133 GParamSpec *pspec)
135 AccountsUser *user = ACCOUNTS_USER (object);
137 switch (prop_id)
139 case PROP_USER_NAME:
140 g_value_set_string (value, accounts_user_get_user_name (user));
141 break;
143 case PROP_REAL_NAME:
144 g_value_set_string (value, accounts_user_get_real_name (user));
145 break;
147 case PROP_AUTOMATIC_LOGIN:
148 g_value_set_boolean (value, accounts_user_get_automatic_login (user));
149 break;
151 default:
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 break;
157 const gchar *
158 accounts_user_get_user_name (AccountsUser *user)
160 GVariant *value;
161 const gchar *ret;
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);
166 return ret;
169 const gchar *
170 accounts_user_get_real_name (AccountsUser *user)
172 GVariant *value;
173 const gchar *ret;
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);
178 return ret;
181 gboolean
182 accounts_user_get_automatic_login (AccountsUser *user)
184 GVariant *value;
185 gboolean ret;
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);
190 return ret;
193 static void
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);
204 static void
205 accounts_user_g_properties_changed (GDBusProxy *proxy,
206 GVariant *changed_properties,
207 const gchar* const *invalidated_properties)
209 AccountsUser *user = ACCOUNTS_USER (proxy);
210 GVariantIter *iter;
211 const gchar *key;
213 if (changed_properties != NULL)
215 g_variant_get (changed_properties, "a{sv}", &iter);
216 while (g_variant_iter_next (iter, "{&sv}", &key, NULL))
218 if (g_strcmp0 (key, "AutomaticLogin") == 0)
219 g_object_notify (G_OBJECT (user), "automatic-login");
220 else if (g_strcmp0 (key, "RealName") == 0)
221 g_object_notify (G_OBJECT (user), "real-name");
222 else if (g_strcmp0 (key, "UserName") == 0)
223 g_object_notify (G_OBJECT (user), "user-name");
225 g_variant_iter_free (iter);
229 static void
230 accounts_user_class_init (AccountsUserClass *klass)
232 GObjectClass *gobject_class;
233 GDBusProxyClass *proxy_class;
235 gobject_class = G_OBJECT_CLASS (klass);
236 gobject_class->get_property = accounts_user_get_property;
237 gobject_class->finalize = accounts_user_finalize;
239 proxy_class = G_DBUS_PROXY_CLASS (klass);
240 proxy_class->g_signal = accounts_user_g_signal;
241 proxy_class->g_properties_changed = accounts_user_g_properties_changed;
243 g_object_class_install_property (gobject_class,
244 PROP_USER_NAME,
245 g_param_spec_string ("user-name",
246 "User Name",
247 "The user name of the user",
248 NULL,
249 G_PARAM_READABLE |
250 G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class,
253 PROP_REAL_NAME,
254 g_param_spec_string ("real-name",
255 "Real Name",
256 "The real name of the user",
257 NULL,
258 G_PARAM_READABLE |
259 G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (gobject_class,
262 PROP_AUTOMATIC_LOGIN,
263 g_param_spec_boolean ("automatic-login",
264 "Automatic Login",
265 "Whether the user is automatically logged in",
266 FALSE,
267 G_PARAM_READABLE |
268 G_PARAM_STATIC_STRINGS));
270 signals[CHANGED_SIGNAL] = g_signal_new ("changed",
271 ACCOUNTS_TYPE_USER,
272 G_SIGNAL_RUN_LAST,
273 G_STRUCT_OFFSET (AccountsUserClass, changed),
274 NULL,
275 NULL,
276 g_cclosure_marshal_VOID__VOID,
277 G_TYPE_NONE,
281 gchar *
282 accounts_user_frobnicate_sync (AccountsUser *user,
283 const gchar *flux,
284 gint baz,
285 GCancellable *cancellable,
286 GError **error)
288 gchar *ret;
289 GVariant *value;
291 g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
293 ret = NULL;
295 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (user),
296 "Frobnicate",
297 g_variant_new ("(si)",
298 flux,
299 baz),
300 G_DBUS_CALL_FLAGS_NONE,
302 cancellable,
303 error);
304 if (value != NULL)
306 g_variant_get (value, "(s)", &ret);
307 g_variant_unref (value);
309 return ret;
312 void
313 accounts_user_frobnicate (AccountsUser *user,
314 const gchar *flux,
315 gint baz,
316 GCancellable *cancellable,
317 GAsyncReadyCallback callback,
318 gpointer user_data)
320 g_return_if_fail (ACCOUNTS_IS_USER (user));
321 g_dbus_proxy_call (G_DBUS_PROXY (user),
322 "Frobnicate",
323 g_variant_new ("(si)",
324 flux,
325 baz),
326 G_DBUS_CALL_FLAGS_NONE,
328 cancellable,
329 callback,
330 user_data);
334 gchar *
335 accounts_user_frobnicate_finish (AccountsUser *user,
336 GAsyncResult *res,
337 GError **error)
339 gchar *ret;
340 GVariant *value;
342 ret = NULL;
343 value = g_dbus_proxy_call_finish (G_DBUS_PROXY (user), res, error);
344 if (value != NULL)
346 g_variant_get (value, "(s)", &ret);
347 g_variant_unref (value);
349 return ret;
352 /* ---------------------------------------------------------------------------------------------------- */
354 gint
355 main (gint argc, gchar *argv[])
357 return 0;