summaryrefslogtreecommitdiff
path: root/salut/channel-manager.c
blob: c8ba267ced5353c898ecdf3c922e32074434172d (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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * channel-manager.c - Source for YtstChannelManager
 * Copyright (C) 2011 Intel, Corp.
 * Copyright (C) 2005, 2011 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "message-channel.h"
#include "channel-manager.h"
#include "utils.h"

#include <telepathy-glib/channel-manager.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/util.h>

#include <salut/caps-channel-manager.h>

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

#define DEBUG(msg, ...) \
  g_debug ("%s: " msg, G_STRFUNC, ##__VA_ARGS__)

static void ytst_channel_manager_iface_init (gpointer g_iface,
    gpointer iface_data);
static void ytst_caps_channel_manager_iface_init (gpointer g_iface,
    gpointer iface_data);

G_DEFINE_TYPE_WITH_CODE (YtstChannelManager, ytst_channel_manager, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_CHANNEL_MANAGER,
        ytst_channel_manager_iface_init);
    G_IMPLEMENT_INTERFACE (GABBLE_TYPE_CAPS_CHANNEL_MANAGER,
        ytst_caps_channel_manager_iface_init);
);

/* properties */
enum
{
  PROP_CONNECTION = 1,
  LAST_PROPERTY
};

/* private structure */
struct _YtstChannelManagerPrivate
{
  SalutConnection *connection;
  GQueue *channels;
  gulong status_changed_id;
  guint message_handler_id;
  gboolean dispose_has_run;
};

/* -----------------------------------------------------------------------------
 * INTERNAL
 */

static void
on_channel_closed (YtstMessageChannel *channel,
    gpointer user_data)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (user_data);
  YtstChannelManagerPrivate *priv = self->priv;

  tp_channel_manager_emit_channel_closed_for_object (self,
    TP_EXPORTABLE_CHANNEL (channel));

  if (priv->channels != NULL)
    {
      DEBUG ("Removing channel %p", channel);
      g_queue_remove (priv->channels, channel);
    }
}

static void
manager_take_ownership_of_channel (YtstChannelManager *self,
    YtstMessageChannel *channel)
{
  YtstChannelManagerPrivate *priv = self->priv;

  /* Takes ownership of channel */
  g_assert (g_queue_index (priv->channels, channel) == -1);
  g_queue_push_tail (priv->channels, channel);

  g_signal_connect (channel, "closed", G_CALLBACK (on_channel_closed), self);
}

static gboolean
message_stanza_callback (WockyPorter *porter,
    WockyStanza *stanza,
    gpointer user_data)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (user_data);
  YtstChannelManagerPrivate *priv = self->priv;

  WockyNode *top;
  WockyStanzaSubType sub_type = WOCKY_STANZA_SUB_TYPE_NONE;

  TpBaseConnection *base_conn = TP_BASE_CONNECTION (priv->connection);
  TpHandleRepoIface *handle_repo = tp_base_connection_get_handles (base_conn,
       TP_HANDLE_TYPE_CONTACT);
  YtstMessageChannel *channel;
  TpHandle handle;
  WockyContact *contact = wocky_stanza_get_from_contact (stanza);
  gchar *jid;

  /* needs to be type get or set */
  wocky_stanza_get_type_info (stanza, NULL, &sub_type);
  if (sub_type != WOCKY_STANZA_SUB_TYPE_GET
      && sub_type != WOCKY_STANZA_SUB_TYPE_SET)
    return FALSE;

  /* we must have an ID */
  top = wocky_stanza_get_top_node (stanza);
  if (wocky_node_get_attribute (top, "id") == NULL)
    return FALSE;

  jid = wocky_contact_dup_jid (WOCKY_CONTACT (contact));
  handle = tp_handle_lookup (handle_repo, jid, NULL, NULL);
  if (handle == 0)
    {
      g_free (jid);
      return FALSE;
    }

  channel = ytst_message_channel_new (priv->connection,
      WOCKY_LL_CONTACT (contact), stanza, handle, handle, FALSE);
  manager_take_ownership_of_channel (self, channel);
  tp_channel_manager_emit_new_channel (self, TP_EXPORTABLE_CHANNEL (channel),
      NULL);

  g_free (jid);

  return TRUE;
}

static void
manager_close_all (YtstChannelManager *self)
{
  YtstChannelManagerPrivate *priv = self->priv;

  if (priv->channels != NULL)
    {
      DEBUG ("closing channels");
      g_queue_foreach (priv->channels, (GFunc) tp_base_channel_close, NULL);
      g_queue_foreach (priv->channels, (GFunc) g_object_unref, NULL);
      g_queue_free (priv->channels);
      priv->channels = NULL;
    }

  if (priv->status_changed_id != 0UL)
    {
      g_signal_handler_disconnect (priv->connection, priv->status_changed_id);
      priv->status_changed_id = 0UL;
    }
}

static void
on_connection_status_changed (SalutConnection *conn,
    guint status,
    guint reason,
    YtstChannelManager *self)
{
  if (status == TP_CONNECTION_STATUS_DISCONNECTED)
    manager_close_all (self);
}

/* -----------------------------------------------------------------------------
 * OBJECT
 */

static void
ytst_channel_manager_init (YtstChannelManager *self)
{
  YtstChannelManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      YTST_TYPE_CHANNEL_MANAGER, YtstChannelManagerPrivate);
  self->priv = priv;
}

static void
ytst_channel_manager_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (object);
  YtstChannelManagerPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CONNECTION:
        g_value_set_object (value, priv->connection);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
ytst_channel_manager_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (object);
  YtstChannelManagerPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_CONNECTION:
        priv->connection = g_value_get_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
ytst_channel_manager_constructed (GObject *object)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (object);
  YtstChannelManagerPrivate *priv = self->priv;
  WockySession *session;

  priv->channels = g_queue_new ();

  session = salut_connection_get_session (priv->connection);

  priv->message_handler_id = wocky_porter_register_handler_from_anyone (
      wocky_session_get_porter (session),
      WOCKY_STANZA_TYPE_IQ, WOCKY_STANZA_SUB_TYPE_NONE,
      WOCKY_PORTER_HANDLER_PRIORITY_NORMAL,
      message_stanza_callback, self,
      '(', "message",
        ':', YTST_MESSAGE_NS,
      ')', NULL);

  priv->status_changed_id = g_signal_connect (priv->connection,
      "status-changed", (GCallback) on_connection_status_changed, self);

  if (G_OBJECT_CLASS (ytst_channel_manager_parent_class)->constructed)
    G_OBJECT_CLASS (ytst_channel_manager_parent_class)->constructed (object);
}

static void
ytst_channel_manager_dispose (GObject *object)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (object);
  YtstChannelManagerPrivate *priv = self->priv;
  WockySession *session;

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  session = salut_connection_get_session (priv->connection);

  if (session != NULL)
    {
      wocky_porter_unregister_handler (
          wocky_session_get_porter (session),
          priv->message_handler_id);
    }
  priv->message_handler_id = 0;

  manager_close_all (self);

  if (G_OBJECT_CLASS (ytst_channel_manager_parent_class)->dispose)
    G_OBJECT_CLASS (ytst_channel_manager_parent_class)->dispose (object);
}

static void
ytst_channel_manager_class_init (YtstChannelManagerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *param_spec;

  g_type_class_add_private (klass, sizeof (YtstChannelManagerPrivate));

  object_class->constructed = ytst_channel_manager_constructed;
  object_class->dispose = ytst_channel_manager_dispose;
  object_class->get_property = ytst_channel_manager_get_property;
  object_class->set_property = ytst_channel_manager_set_property;

  param_spec = g_param_spec_object (
      "connection",
      "SalutConnection object",
      "Salut connection object that owns this channel factory object.",
      SALUT_TYPE_CONNECTION,
      G_PARAM_CONSTRUCT_ONLY |
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CONNECTION, param_spec);
}

typedef struct
{
  TpExportableChannelFunc func;
  gpointer data;
} foreach_closure;

static void
run_foreach_one (gpointer value,
    gpointer data)
{
  TpExportableChannel *chan = TP_EXPORTABLE_CHANNEL (value);
  foreach_closure *f = data;
  f->func (chan, f->data);
}

static void
ytst_channel_manager_foreach_channel (TpChannelManager *iface,
    TpExportableChannelFunc func,
    gpointer user_data)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (iface);
  YtstChannelManagerPrivate *priv = self->priv;
  foreach_closure f = { func, user_data };

  g_queue_foreach (priv->channels, run_foreach_one, &f);
}

static const gchar * const channel_fixed_properties[] = {
    TP_IFACE_CHANNEL ".ChannelType",
    TP_IFACE_CHANNEL ".TargetHandleType",
    TP_YTS_IFACE_CHANNEL ".RequestType",
    NULL
};


static const gchar * const channel_allowed_properties[] = {
    TP_IFACE_CHANNEL ".TargetHandle",
    TP_IFACE_CHANNEL ".TargetID",
    TP_YTS_IFACE_CHANNEL ".RequestType",
    TP_YTS_IFACE_CHANNEL ".RequestAttributes",
    TP_YTS_IFACE_CHANNEL ".RequestBody",
    TP_YTS_IFACE_CHANNEL ".TargetService",
    TP_YTS_IFACE_CHANNEL ".InitiatorService",
    NULL
};

static void
ytst_channel_manager_type_foreach_channel_class (GType type,
    TpChannelManagerTypeChannelClassFunc func,
    gpointer user_data)
{
  GHashTable *table = g_hash_table_new_full (g_str_hash, g_str_equal,
      NULL, (GDestroyNotify) tp_g_value_slice_free);
  GValue *value;

  value = tp_g_value_slice_new (G_TYPE_STRING);
  g_value_set_static_string (value, TP_YTS_IFACE_CHANNEL);
  g_hash_table_insert (table, (gchar *) channel_fixed_properties[0],
      value);

  value = tp_g_value_slice_new (G_TYPE_UINT);
  g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
  g_hash_table_insert (table, (gchar *) channel_fixed_properties[1],
      value);

  func (type, table, channel_allowed_properties, user_data);

  g_hash_table_destroy (table);
}

static gboolean
ytst_channel_manager_create_channel (TpChannelManager *manager,
    gpointer request_token,
    GHashTable *request_properties)
{
  YtstChannelManager *self = YTST_CHANNEL_MANAGER (manager);
  YtstChannelManagerPrivate *priv = self->priv;
  TpBaseConnection *base_conn = (TpBaseConnection *) priv->connection;
  TpHandleRepoIface *handle_repo = tp_base_connection_get_handles (
      base_conn, TP_HANDLE_TYPE_CONTACT);
  TpHandle handle;
  GError *error = NULL;
  const gchar *name;
  WockySession *session;
  WockyContactFactory *factory;
  WockyLLContact *contact;
  WockyStanza *request;
  GSList *tokens = NULL;
  YtstMessageChannel *channel;

  if (tp_strdiff (tp_asv_get_string (request_properties,
          TP_IFACE_CHANNEL ".ChannelType"),
          TP_YTS_IFACE_CHANNEL))
    return FALSE;

  if (tp_asv_get_uint32 (request_properties,
        TP_IFACE_CHANNEL ".TargetHandleType", NULL) != TP_HANDLE_TYPE_CONTACT)
    return FALSE;

  handle = tp_asv_get_uint32 (request_properties,
      TP_IFACE_CHANNEL ".TargetHandle", NULL);

  if (!tp_handle_is_valid (handle_repo, handle, &error))
    goto error;

  /* Check if there are any other properties that we don't understand */
  if (tp_channel_manager_asv_has_unknown_properties (request_properties,
          channel_fixed_properties, channel_allowed_properties, &error))
      goto error;

  name = tp_handle_inspect (handle_repo, handle);
  DEBUG ("Requested channel for handle: %u (%s)", handle, name);

  session = salut_connection_get_session (priv->connection);
  factory = wocky_session_get_contact_factory (session);
  contact = wocky_contact_factory_lookup_ll_contact (factory, name);
  if (contact == NULL)
    {
      g_set_error (&error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
          "%s is not online", name);
      goto error;
    }

  request = ytst_message_channel_build_request (request_properties,
      salut_connection_get_name (priv->connection), contact, &error);
  if (request == NULL)
    goto error;

  channel = ytst_message_channel_new (priv->connection, contact, request, handle,
      base_conn->self_handle, TRUE);
  manager_take_ownership_of_channel (self, channel);

  g_object_unref (request);

  if (request_token != NULL)
    tokens = g_slist_prepend (tokens, request_token);
  tp_channel_manager_emit_new_channel (self, TP_EXPORTABLE_CHANNEL (channel),
      tokens);

  return TRUE; /* We will handle this one */

error:
  g_return_val_if_fail (error, FALSE);
  tp_channel_manager_emit_request_failed (self, request_token,
      error->domain, error->code, error->message);
  g_error_free (error);
  return TRUE; /* We tried to handle */
}

static void
ytst_channel_manager_iface_init (gpointer g_iface,
    gpointer iface_data)
{
  TpChannelManagerIface *iface = g_iface;

  iface->foreach_channel = ytst_channel_manager_foreach_channel;
  iface->type_foreach_channel_class =
    ytst_channel_manager_type_foreach_channel_class;

  /*
   * Each channel only supports one request/reply, so we create
   * a channel and never reuse. Same implementation for all three.
   */
  iface->create_channel = ytst_channel_manager_create_channel;
  iface->request_channel = ytst_channel_manager_create_channel;
  iface->ensure_channel = ytst_channel_manager_create_channel;
}

static void
ytst_caps_channel_manager_iface_init (gpointer g_iface,
    gpointer iface_data)
{
  GabbleCapsChannelManagerIface *iface = g_iface;

  /* we don't need any of these */
  iface->reset_caps = NULL;
  iface->get_contact_caps = NULL;
  iface->represent_client = NULL;
}

/* public functions */
YtstChannelManager *
ytst_channel_manager_new (SalutConnection *connection)
{
  return g_object_new (YTST_TYPE_CHANNEL_MANAGER,
      "connection", connection,
      NULL);
}