summaryrefslogtreecommitdiff
path: root/gio/tests/gdbus-example-proxy-subclass.c
blob: 91b377c489be823dcb4175b5982e230501cd1610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

#include <gio/gio.h>

/* ---------------------------------------------------------------------------------------------------- */
/* The D-Bus interface definition we want to create a GDBusProxy-derived type for: */
/* ---------------------------------------------------------------------------------------------------- */

static const gchar introspection_xml[] =
  "<node>"
  "  <interface name='org.freedesktop.Accounts.User'>"
  "    <method name='Frobnicate'>"
  "      <arg name='flux' type='s' direction='in'/>"
  "      <arg name='baz' type='s' direction='in'/>"
  "      <arg name='result' type='s' direction='out'/>"
  "    </method>"
  "    <signal name='Changed'/>"
  "    <property name='AutomaticLogin' type='b' access='readwrite'/>"
  "    <property name='RealName' type='s' access='read'/>"
  "    <property name='UserName' type='s' access='read'/>"
  "  </interface>"
  "</node>";

/* ---------------------------------------------------------------------------------------------------- */
/* Definition of the AccountsUser type */
/* ---------------------------------------------------------------------------------------------------- */

#define ACCOUNTS_TYPE_USER         (accounts_user_get_type ())
#define ACCOUNTS_USER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), ACCOUNTS_TYPE_USER, AccountsUser))
#define ACCOUNTS_USER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), ACCOUNTS_TYPE_USER, AccountsUserClass))
#define ACCOUNTS_USER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), ACCOUNTS_TYPE_USER, AccountsUserClass))
#define ACCOUNTS_IS_USER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), ACCOUNTS_TYPE_USER))
#define ACCOUNTS_IS_USER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), ACCOUNTS_TYPE_USER))

typedef struct _AccountsUser        AccountsUser;
typedef struct _AccountsUserClass   AccountsUserClass;
typedef struct _AccountsUserPrivate AccountsUserPrivate;

struct _AccountsUser
{
  /*< private >*/
  GDBusProxy parent_instance;
  AccountsUserPrivate *priv;
};

struct _AccountsUserClass
{
  /*< private >*/
  GDBusProxyClass parent_class;
  void (*changed) (AccountsUser *user);
};

GType        accounts_user_get_type            (void) G_GNUC_CONST;

const gchar *accounts_user_get_user_name       (AccountsUser        *user);
const gchar *accounts_user_get_real_name       (AccountsUser        *user);
gboolean     accounts_user_get_automatic_login (AccountsUser        *user);

void         accounts_user_frobnicate          (AccountsUser        *user,
                                                const gchar         *flux,
                                                gint                 baz,
                                                GCancellable        *cancellable,
                                                GAsyncReadyCallback  callback,
                                                gpointer             user_data);
gchar       *accounts_user_frobnicate_finish   (AccountsUser        *user,
                                                GAsyncResult        *res,
                                                GError             **error);
gchar       *accounts_user_frobnicate_sync     (AccountsUser        *user,
                                                const gchar         *flux,
                                                gint                 baz,
                                                GCancellable        *cancellable,
                                                GError             **error);

/* ---------------------------------------------------------------------------------------------------- */
/* Implementation of the AccountsUser type */
/* ---------------------------------------------------------------------------------------------------- */

/* A more efficient approach than parsing XML is to use const static
 * GDBusInterfaceInfo, GDBusMethodInfo, ... structures
 */
static GDBusInterfaceInfo *
accounts_user_get_interface_info (void)
{
  static gsize has_info = 0;
  static GDBusInterfaceInfo *info = NULL;
  if (g_once_init_enter (&has_info))
    {
      GDBusNodeInfo *introspection_data;
      introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
      info = introspection_data->interfaces[0];
      g_once_init_leave (&has_info, 1);
    }
  return info;
}

enum
{
  PROP_0,
  PROP_USER_NAME,
  PROP_REAL_NAME,
  PROP_AUTOMATIC_LOGIN,
};

enum
{
  CHANGED_SIGNAL,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0};

G_DEFINE_TYPE (AccountsUser, accounts_user, G_TYPE_DBUS_PROXY)

static void
accounts_user_init (AccountsUser *user)
{
  /* Sets the expected interface */
  g_dbus_proxy_set_interface_info (G_DBUS_PROXY (user), accounts_user_get_interface_info ());
}

static void
accounts_user_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  AccountsUser *user = ACCOUNTS_USER (object);

  switch (prop_id)
    {
    case PROP_USER_NAME:
      g_value_set_string (value, accounts_user_get_user_name (user));
      break;

    case PROP_REAL_NAME:
      g_value_set_string (value, accounts_user_get_real_name (user));
      break;

    case PROP_AUTOMATIC_LOGIN:
      g_value_set_boolean (value, accounts_user_get_automatic_login (user));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

const gchar *
accounts_user_get_user_name (AccountsUser *user)
{
  GVariant *value;
  const gchar *ret;
  g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
  value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "UserName");
  ret = g_variant_get_string (value, NULL);
  g_variant_unref (value);
  return ret;
}

const gchar *
accounts_user_get_real_name (AccountsUser *user)
{
  GVariant *value;
  const gchar *ret;
  g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);
  value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "RealName");
  ret = g_variant_get_string (value, NULL);
  g_variant_unref (value);
  return ret;
}

gboolean
accounts_user_get_automatic_login (AccountsUser *user)
{
  GVariant *value;
  gboolean ret;
  g_return_val_if_fail (ACCOUNTS_IS_USER (user), FALSE);
  value = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (user), "AutomaticLogin");
  ret = g_variant_get_boolean (value);
  g_variant_unref (value);
  return ret;
}

static void
accounts_user_g_signal (GDBusProxy   *proxy,
                        const gchar  *sender_name,
                        const gchar  *signal_name,
                        GVariant     *parameters)
{
  AccountsUser *user = ACCOUNTS_USER (proxy);
  if (g_strcmp0 (signal_name, "Changed") == 0)
    g_signal_emit (user, signals[CHANGED_SIGNAL], 0);
}

static void
accounts_user_g_properties_changed (GDBusProxy          *proxy,
                                    GVariant            *changed_properties,
                                    const gchar* const  *invalidated_properties)
{
  AccountsUser *user = ACCOUNTS_USER (proxy);
  GVariantIter *iter;
  const gchar *key;

  if (changed_properties != NULL)
    {
      g_variant_get (changed_properties, "a{sv}", &iter);
      while (g_variant_iter_next (iter, "{&sv}", &key, NULL))
        {
          if (g_strcmp0 (key, "AutomaticLogin") == 0)
            g_object_notify (G_OBJECT (user), "automatic-login");
          else if (g_strcmp0 (key, "RealName") == 0)
            g_object_notify (G_OBJECT (user), "real-name");
          else if (g_strcmp0 (key, "UserName") == 0)
            g_object_notify (G_OBJECT (user), "user-name");
        }
      g_variant_iter_free (iter);
    }
}

static void
accounts_user_class_init (AccountsUserClass *klass)
{
  GObjectClass *gobject_class;
  GDBusProxyClass *proxy_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->get_property = accounts_user_get_property;

  proxy_class = G_DBUS_PROXY_CLASS (klass);
  proxy_class->g_signal             = accounts_user_g_signal;
  proxy_class->g_properties_changed = accounts_user_g_properties_changed;

  g_object_class_install_property (gobject_class,
                                   PROP_USER_NAME,
                                   g_param_spec_string ("user-name",
                                                        "User Name",
                                                        "The user name of the user",
                                                        NULL,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class,
                                   PROP_REAL_NAME,
                                   g_param_spec_string ("real-name",
                                                        "Real Name",
                                                        "The real name of the user",
                                                        NULL,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class,
                                   PROP_AUTOMATIC_LOGIN,
                                   g_param_spec_boolean ("automatic-login",
                                                         "Automatic Login",
                                                         "Whether the user is automatically logged in",
                                                         FALSE,
                                                         G_PARAM_READABLE |
                                                         G_PARAM_STATIC_STRINGS));

  signals[CHANGED_SIGNAL] = g_signal_new ("changed",
                                          ACCOUNTS_TYPE_USER,
                                          G_SIGNAL_RUN_LAST,
                                          G_STRUCT_OFFSET (AccountsUserClass, changed),
                                          NULL,
                                          NULL,
                                          g_cclosure_marshal_VOID__VOID,
                                          G_TYPE_NONE,
                                          0);
}

gchar *
accounts_user_frobnicate_sync (AccountsUser        *user,
                               const gchar         *flux,
                               gint                 baz,
                               GCancellable        *cancellable,
                               GError             **error)
{
  gchar *ret;
  GVariant *value;

  g_return_val_if_fail (ACCOUNTS_IS_USER (user), NULL);

  ret = NULL;

  value = g_dbus_proxy_call_sync (G_DBUS_PROXY (user),
                                  "Frobnicate",
                                  g_variant_new ("(si)",
                                                 flux,
                                                 baz),
                                  G_DBUS_CALL_FLAGS_NONE,
                                  -1,
                                  cancellable,
                                  error);
  if (value != NULL)
    {
      g_variant_get (value, "(s)", &ret);
      g_variant_unref (value);
    }
  return ret;
}

void
accounts_user_frobnicate (AccountsUser        *user,
                          const gchar         *flux,
                          gint                 baz,
                          GCancellable        *cancellable,
                          GAsyncReadyCallback  callback,
                          gpointer             user_data)
{
  g_return_if_fail (ACCOUNTS_IS_USER (user));
  g_dbus_proxy_call (G_DBUS_PROXY (user),
                     "Frobnicate",
                     g_variant_new ("(si)",
                                    flux,
                                    baz),
                     G_DBUS_CALL_FLAGS_NONE,
                     -1,
                     cancellable,
                     callback,
                     user_data);
}


gchar *
accounts_user_frobnicate_finish (AccountsUser        *user,
                                 GAsyncResult        *res,
                                 GError             **error)
{
  gchar *ret;
  GVariant *value;

  ret = NULL;
  value = g_dbus_proxy_call_finish (G_DBUS_PROXY (user), res, error);
  if (value != NULL)
    {
      g_variant_get (value, "(s)", &ret);
      g_variant_unref (value);
    }
  return ret;
}

/* ---------------------------------------------------------------------------------------------------- */
/* Example usage of the AccountsUser type */
/* ---------------------------------------------------------------------------------------------------- */

static void
print_user (AccountsUser *user)
{
  g_print ("  user-name       = `%s'\n", accounts_user_get_user_name (user));
  g_print ("  real-name       = `%s'\n", accounts_user_get_real_name (user));
  g_print ("  automatic-login = %s\n", accounts_user_get_automatic_login (user) ? "true" : "false");
}

static void
on_changed (AccountsUser *user,
            gpointer      user_data)
{
  g_print ("+++ Received the AccountsUser::changed signal\n");
  print_user (user);
}

static void
on_notify (GObject    *object,
           GParamSpec *pspec,
           gpointer    user_data)
{
  AccountsUser *user = ACCOUNTS_USER (object);
  g_print ("+++ Received the GObject::notify signal for property `%s'\n",
           pspec->name);
  print_user (user);
}

static void
on_accounts_proxy_available (GObject      *object,
                             GAsyncResult *result,
                             gpointer      user_data)
{
  GError *error = NULL;
  GObject *user_object;
  AccountsUser *user;

  user_object = g_async_initable_new_finish (G_ASYNC_INITABLE (object),
                                             result,
                                             &error);
  if (!user_object)
    {
      g_error ("Failed to create proxy: %s", error->message);
      g_clear_error (&error);
      return;
    }
  user = ACCOUNTS_USER (user_object);

  g_print ("+++ Acquired proxy for user\n");
  print_user (user);

  g_signal_connect (user,
                    "notify",
                    G_CALLBACK (on_notify),
                    NULL);
  g_signal_connect (user,
                    "changed",
                    G_CALLBACK (on_changed),
                    NULL);
}

static void
on_accounts_appeared (GDBusConnection *connection,
                      const gchar     *name,
                      const gchar     *name_owner,
                      gpointer         user_data)
{
  g_async_initable_new_async (ACCOUNTS_TYPE_USER, 0, NULL,
                              on_accounts_proxy_available,
                              "g-flags", 0,
                              "g-interface-info", NULL,
                              "g-unique-bus-name", name_owner,
                              "g-connection", connection,
                              "g-object-path", "/org/freedesktop/Accounts/User500",
                              "g-interface-name", "org.freedesktop.Accounts.User");
}

static void
on_accounts_vanished (GDBusConnection *connection,
                      const gchar     *name,
                      gpointer         user_data)
{
}

/* ---------------------------------------------------------------------------------------------------- */

gint
main (gint argc, gchar *argv[])
{
  guint watcher_id;
  GMainLoop *loop;

  watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
                                 "org.freedesktop.Accounts",
                                 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
                                 on_accounts_appeared,
                                 on_accounts_vanished,
                                 NULL, NULL);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);
  g_main_loop_unref (loop);
  g_bus_unwatch_name (watcher_id);

  return 0;
}