File: simple-channel-dispatcher.c

package info (click to toggle)
telepathy-glib 0.24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 34,628 kB
  • sloc: ansic: 124,643; xml: 34,410; sh: 11,299; python: 3,520; makefile: 1,727; cpp: 16
file content (458 lines) | stat: -rw-r--r-- 13,455 bytes parent folder | download | duplicates (3)
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
451
452
453
454
455
456
457
458
/*
 * simple-channel-dispatcher.c - simple channel dispatcher service.
 *
 * Copyright © 2010 Collabora Ltd.
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.
 */

#include "config.h"

#include "simple-channel-dispatcher.h"

#include <telepathy-glib/telepathy-glib.h>
#include <telepathy-glib/telepathy-glib-dbus.h>

#include "simple-channel-request.h"
#include "simple-conn.h"

static void channel_dispatcher_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE (TpTestsSimpleChannelDispatcher,
    tp_tests_simple_channel_dispatcher,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CHANNEL_DISPATCHER,
        channel_dispatcher_iface_init);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
        tp_dbus_properties_mixin_iface_init)
    )

/* signals */
enum {
  CHANNEL_REQUEST_CREATED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

/* TP_IFACE_CHANNEL_DISPATCHER is implied */
static const char *CHANNEL_DISPATCHER_INTERFACES[] = { NULL };

enum
{
  PROP_0,
  PROP_INTERFACES,
  PROP_CONNECTION,
};

struct _TpTestsSimpleChannelDispatcherPrivate
{
  /* To keep things simpler, this CD can only create channels using one
   * connection */
  TpTestsSimpleConnection *conn;

  /* List of reffed TpTestsSimpleChannelRequest */
  GSList *requests;

  /* Used when ensuring a channel to store its handler.
   * If this is set we fake that the channel already exist and re-call
   * HandleChannels() on the handler rather than creating a new channel.
   * This is pretty stupid but good enough for our tests. */
  gchar *old_handler;
};

static gchar *
create_channel_request (TpTestsSimpleChannelDispatcher *self,
    const gchar *account,
    GHashTable *request,
    gint64 user_action_time,
    const gchar *preferred_handler,
    GHashTable *hints_)
{
  TpTestsSimpleChannelRequest *chan_request;
  GPtrArray *requests;
  static guint count = 0;
  gchar *path;
  TpDBusDaemon *dbus;
  GHashTable *hints;

  requests = g_ptr_array_sized_new (1);
  g_ptr_array_add (requests, request);

  path = g_strdup_printf ("/Request%u", count++);

  if (hints_ == NULL)
    hints = g_hash_table_new (NULL, NULL);
  else
    hints = g_hash_table_ref (hints_);

  chan_request = tp_tests_simple_channel_request_new (path,
      self->priv->conn, account, user_action_time, preferred_handler, requests,
      hints);

  g_hash_table_unref (hints);

  self->priv->requests = g_slist_append (self->priv->requests, chan_request);

  g_ptr_array_unref (requests);

  dbus = tp_dbus_daemon_dup (NULL);
  g_assert (dbus != NULL);

  tp_dbus_daemon_register_object (dbus, path, chan_request);

  g_object_unref (dbus);

  g_signal_emit (self, signals[CHANNEL_REQUEST_CREATED], 0, chan_request);

  return path;
}

static gchar *
create_channel (TpTestsSimpleChannelDispatcher *self,
    const gchar *account,
    GHashTable *request,
    gint64 user_action_time,
    const gchar *preferred_handler,
    GHashTable *hints,
    DBusGMethodInvocation *context)
{
  tp_clear_pointer (&self->last_request, g_hash_table_unref);
  self->last_request = g_boxed_copy (TP_HASH_TYPE_STRING_VARIANT_MAP, request);
  tp_clear_pointer (&self->last_hints, g_hash_table_unref);
  self->last_hints = g_boxed_copy (TP_HASH_TYPE_STRING_VARIANT_MAP, request);
  self->last_user_action_time = user_action_time;
  g_free (self->last_account);
  self->last_account = g_strdup (account);
  g_free (self->last_preferred_handler);
  self->last_preferred_handler = g_strdup (preferred_handler);

  if (tp_asv_get_boolean (request, "CreateChannelFail", NULL))
    {
      /* Fail to create the channel */
      GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
          "Computer says no" };

      dbus_g_method_return_error (context, &error);
      return NULL;
    }

  return create_channel_request (self, account, request, user_action_time,
      preferred_handler, hints);
}

static void
tp_tests_simple_channel_dispatcher_create_channel (
    TpSvcChannelDispatcher *dispatcher,
    const gchar *account,
    GHashTable *request,
    gint64 user_action_time,
    const gchar *preferred_handler,
    DBusGMethodInvocation *context)
{
  TpTestsSimpleChannelDispatcher *self = SIMPLE_CHANNEL_DISPATCHER (dispatcher);
  gchar *path;

  path = create_channel (self, account, request, user_action_time,
      preferred_handler, NULL, context);
  if (path == NULL)
    return;

  tp_svc_channel_dispatcher_return_from_create_channel (context, path);

  g_free (path);
}

static void
tp_tests_simple_channel_dispatcher_create_channel_with_hints (
    TpSvcChannelDispatcher *dispatcher,
    const gchar *account,
    GHashTable *request,
    gint64 user_action_time,
    const gchar *preferred_handler,
    GHashTable *hints,
    DBusGMethodInvocation *context)
{
  TpTestsSimpleChannelDispatcher *self = SIMPLE_CHANNEL_DISPATCHER (dispatcher);
  gchar *path;

  path = create_channel (self, account, request, user_action_time,
      preferred_handler, hints, context);
  if (path == NULL)
    return;

  tp_svc_channel_dispatcher_return_from_create_channel_with_hints (context,
      path);

  g_free (path);
}

static gchar *
ensure_channel (TpTestsSimpleChannelDispatcher *self,
    const gchar *account,
    GHashTable *request,
    gint64 user_action_time,
    const gchar *preferred_handler,
    GHashTable *hints,
    DBusGMethodInvocation *context)
{
  tp_clear_pointer (&self->last_request, g_hash_table_unref);
  self->last_request = g_boxed_copy (TP_HASH_TYPE_STRING_VARIANT_MAP, request);
  tp_clear_pointer (&self->last_hints, g_hash_table_unref);
  self->last_hints = g_boxed_copy (TP_HASH_TYPE_STRING_VARIANT_MAP, request);
  self->last_user_action_time = user_action_time;
  g_free (self->last_account);
  self->last_account = g_strdup (account);
  g_free (self->last_preferred_handler);
  self->last_preferred_handler = g_strdup (preferred_handler);

  if (self->priv->old_handler != NULL)
    {
      /* Pretend that the channel already exists */
      return create_channel_request (self, account, request, user_action_time,
          self->priv->old_handler, hints);
    }

  self->priv->old_handler = g_strdup (preferred_handler);

  return create_channel_request (self, account, request, user_action_time,
          preferred_handler, hints);
}

static void
tp_tests_simple_channel_dispatcher_ensure_channel (
    TpSvcChannelDispatcher *dispatcher,
    const gchar *account,
    GHashTable *request,
    gint64 user_action_time,
    const gchar *preferred_handler,
    DBusGMethodInvocation *context)
{
  TpTestsSimpleChannelDispatcher *self = SIMPLE_CHANNEL_DISPATCHER (dispatcher);
  gchar *path;

  path = ensure_channel (self, account, request, user_action_time,
      preferred_handler, NULL, context);

  tp_svc_channel_dispatcher_return_from_ensure_channel (context, path);

  g_free (path);
}

static void
tp_tests_simple_channel_dispatcher_ensure_channel_with_hints (
    TpSvcChannelDispatcher *dispatcher,
    const gchar *account,
    GHashTable *request,
    gint64 user_action_time,
    const gchar *preferred_handler,
    GHashTable *hints,
    DBusGMethodInvocation *context)
{
  TpTestsSimpleChannelDispatcher *self = SIMPLE_CHANNEL_DISPATCHER (dispatcher);
  gchar *path;

  path = ensure_channel (self, account, request, user_action_time,
      preferred_handler, hints, context);

  tp_svc_channel_dispatcher_return_from_ensure_channel_with_hints (context,
      path);

  g_free (path);
}

static void
free_not_delegated_error (gpointer data)
{
    g_boxed_free (TP_STRUCT_TYPE_NOT_DELEGATED_ERROR, data);
}


static void
tp_tests_simple_channel_dispatcher_delegate_channels (
    TpSvcChannelDispatcher *dispatcher,
    const GPtrArray *channels,
    gint64 user_action_time,
    const gchar *preferred_handler,
    DBusGMethodInvocation *context)
{
  TpTestsSimpleChannelDispatcher *self = (TpTestsSimpleChannelDispatcher *)
    dispatcher;
  GPtrArray *delegated;
  GHashTable *not_delegated;
  guint i;

  delegated = g_ptr_array_new ();
  not_delegated = g_hash_table_new_full (g_str_hash, g_str_equal,
      NULL, free_not_delegated_error);

  for (i = 0; i < channels->len; i++)
    {
      gpointer chan_path = g_ptr_array_index (channels, i);
      GValueArray *v;

      if (!self->refuse_delegate)
        {
          g_ptr_array_add (delegated, chan_path);
          continue;
        }

      v = tp_value_array_build (2,
        G_TYPE_STRING, TP_ERROR_STR_BUSY,
        G_TYPE_STRING, "Nah!",
        G_TYPE_INVALID);

      g_hash_table_insert (not_delegated, chan_path, v);
    }

  tp_svc_channel_dispatcher_return_from_delegate_channels (context, delegated,
      not_delegated);

  g_ptr_array_unref  (delegated);
  g_hash_table_unref (not_delegated);
}

static void
tp_tests_simple_channel_dispatcher_present_channel (
    TpSvcChannelDispatcher *dispatcher,
    const gchar *channel,
    gint64 user_action_time,
    DBusGMethodInvocation *context)
{
  tp_svc_channel_dispatcher_return_from_present_channel (context);
}

static void
channel_dispatcher_iface_init (gpointer klass,
    gpointer unused G_GNUC_UNUSED)
{
#define IMPLEMENT(x) tp_svc_channel_dispatcher_implement_##x (\
  klass, tp_tests_simple_channel_dispatcher_##x)
  IMPLEMENT (create_channel);
  IMPLEMENT (ensure_channel);
  IMPLEMENT (create_channel_with_hints);
  IMPLEMENT (ensure_channel_with_hints);
  IMPLEMENT (delegate_channels);
  IMPLEMENT (present_channel);
#undef IMPLEMENT
}


static void
tp_tests_simple_channel_dispatcher_init (TpTestsSimpleChannelDispatcher *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TP_TESTS_TYPE_SIMPLE_CHANNEL_DISPATCHER,
      TpTestsSimpleChannelDispatcherPrivate);
}

static void
tp_tests_simple_channel_dispatcher_get_property (GObject *object,
              guint property_id,
              GValue *value,
              GParamSpec *spec)
{
  switch (property_id) {
    case PROP_INTERFACES:
      g_value_set_boxed (value, CHANNEL_DISPATCHER_INTERFACES);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
      break;
  }
}

static void
tp_tests_simple_channel_dispatcher_set_property (GObject *object,
              guint property_id,
              const GValue *value,
              GParamSpec *spec)
{
  TpTestsSimpleChannelDispatcher *self = SIMPLE_CHANNEL_DISPATCHER (object);

  switch (property_id) {
    case PROP_CONNECTION:
      self->priv->conn = g_value_dup_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
      break;
  }
}

static void
tp_tests_simple_channel_dispatcher_dispose (GObject *object)
{
  TpTestsSimpleChannelDispatcher *self = SIMPLE_CHANNEL_DISPATCHER (object);

  tp_clear_object (&self->priv->conn);

  g_slist_foreach (self->priv->requests, (GFunc) g_object_unref, NULL);
  g_slist_free (self->priv->requests);

  g_free (self->priv->old_handler);

  tp_clear_pointer (&self->last_request, g_hash_table_unref);
  tp_clear_pointer (&self->last_hints, g_hash_table_unref);
  tp_clear_pointer (&self->last_account, g_free);
  tp_clear_pointer (&self->last_preferred_handler, g_free);

  if (G_OBJECT_CLASS (tp_tests_simple_channel_dispatcher_parent_class)->dispose != NULL)
    G_OBJECT_CLASS (tp_tests_simple_channel_dispatcher_parent_class)->dispose (object);
}

static void
tp_tests_simple_channel_dispatcher_class_init (
    TpTestsSimpleChannelDispatcherClass *klass)
{
  GObjectClass *object_class = (GObjectClass *) klass;
  GParamSpec *param_spec;

  static TpDBusPropertiesMixinPropImpl am_props[] = {
        { "Interfaces", "interfaces", NULL },
        { NULL }
  };

  static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
        { TP_IFACE_CHANNEL_DISPATCHER,
          tp_dbus_properties_mixin_getter_gobject_properties,
          NULL,
          am_props
        },
        { NULL },
  };

  g_type_class_add_private (klass, sizeof (TpTestsSimpleChannelDispatcherPrivate));
  object_class->get_property = tp_tests_simple_channel_dispatcher_get_property;
  object_class->set_property = tp_tests_simple_channel_dispatcher_set_property;

  object_class->dispose = tp_tests_simple_channel_dispatcher_dispose;

  param_spec = g_param_spec_boxed ("interfaces", "Extra D-Bus interfaces",
      "In this case we only implement ChannelDispatcher, so none.",
      G_TYPE_STRV,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_INTERFACES, param_spec);

  param_spec = g_param_spec_object ("connection", "TpTestsSimpleConnection",
      "connection to use when creating channels",
      TP_TESTS_TYPE_SIMPLE_CONNECTION,
      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CONNECTION, param_spec);

  /* Fired when we create a new channel request object. This can be used in
   * test to track the progression of a request. */
  signals[CHANNEL_REQUEST_CREATED] = g_signal_new ("channel-request-created",
      G_TYPE_FROM_CLASS (object_class),
      G_SIGNAL_RUN_LAST,
      0, NULL, NULL, NULL,
      G_TYPE_NONE, 1, TP_TESTS_TYPE_SIMPLE_CHANNEL_REQUEST);

  klass->dbus_props_class.interfaces = prop_interfaces;
  tp_dbus_properties_mixin_class_init (object_class,
      G_STRUCT_OFFSET (TpTestsSimpleChannelDispatcherClass, dbus_props_class));
}