summaryrefslogtreecommitdiff
path: root/yell/telepathy-yell/call-stream.c
blob: 9a045811ae1b829523c2dc9fb4c518bdf65cf755 (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
/*
 * call-stream.c - Source for TpyCallStream
 * Copyright © 2009–2011 Collabora Ltd.
 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
 * @author Will Thompson <will.thompson@collabora.co.uk>
 * @author Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
 *
 * 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 "call-stream.h"

#define DEBUG_FLAG TPY_DEBUG_CALL
#include "debug.h"

#include <telepathy-yell/interfaces.h>
#include <telepathy-yell/gtypes.h>
#include <telepathy-yell/cli-call.h>

G_DEFINE_TYPE(TpyCallStream, tpy_call_stream, TP_TYPE_PROXY)

enum
{
  PROP_REMOTE_MEMBERS = 1,
  PROP_LOCAL_SENDING_STATE,
  PROP_CAN_REQUEST_RECEIVING,
  PROP_READY,
};

struct _TpyCallStreamPrivate
{
  GHashTable *remote_members;
  TpySendingState local_sending_state;
  gboolean can_request_receiving;
  gboolean ready;

  GSimpleAsyncResult *result;
};

static void
on_call_stream_get_all_properties_cb (TpProxy *proxy,
    GHashTable *properties,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallStream *self = TPY_CALL_STREAM (proxy);
  GHashTable *members;

  if (error != NULL)
    {
      g_warning ("Could not get the stream properties: %s", error->message);
      return;
    }

  self->priv->local_sending_state = tp_asv_get_uint32 (properties,
      "LocalSendingState", NULL);
  self->priv->can_request_receiving = tp_asv_get_boolean (properties,
      "CanRequestReceiving", NULL);

  tp_clear_pointer (&self->priv->remote_members, g_hash_table_unref);
  members = tp_asv_get_boxed (properties,
      "RemoteMembers", TPY_HASH_TYPE_CALL_MEMBER_MAP);
  if (members != NULL)
    self->priv->remote_members =
        g_boxed_copy (TPY_HASH_TYPE_CALL_MEMBER_MAP, members);

  self->priv->ready = TRUE;
  g_object_notify (G_OBJECT (self), "ready");
}

static void
on_remote_members_changed_cb (TpProxy *proxy,
    GHashTable *updates,
    const GArray *removed,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallStream *self = TPY_CALL_STREAM (proxy);
  GHashTableIter iter;
  gpointer key, value;
  guint i;

  for (i = 0; i < removed->len; i++)
    g_hash_table_remove (self->priv->remote_members,
        GUINT_TO_POINTER (g_array_index (removed, TpHandle, i)));

  g_hash_table_iter_init (&iter, updates);
  while (g_hash_table_iter_next (&iter, &key, &value))
    g_hash_table_insert (self->priv->remote_members, key, value);

  g_object_notify (G_OBJECT (self), "remote-members");
}

static void
on_local_sending_state_changed_cb (TpProxy *proxy,
    guint state,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallStream *self = TPY_CALL_STREAM (proxy);

  if (self->priv->local_sending_state == state)
    return;

  self->priv->local_sending_state = state;
  g_object_notify (G_OBJECT (self), "local-sending-state");
}

static void
tpy_call_stream_constructed (GObject *obj)
{
  TpyCallStream *self = (TpyCallStream *) obj;
  GError *err = NULL;

  ((GObjectClass *) tpy_call_stream_parent_class)->constructed (obj);

  tpy_cli_call_stream_connect_to_remote_members_changed (TP_PROXY (self),
      on_remote_members_changed_cb, NULL, NULL, G_OBJECT (self), &err);

  if (err != NULL)
    {
      g_critical ("Failed to connect to RemoteMembersChanged signal: %s",
          err->message);

      g_error_free (err);
      return;
    }

  tpy_cli_call_stream_connect_to_local_sending_state_changed (TP_PROXY (self),
      on_local_sending_state_changed_cb, NULL, NULL, G_OBJECT (self), &err);

  if (err != NULL)
    {
      g_critical ("Failed to connect to LocalSendingStateChanged signal: %s",
          err->message);

      g_error_free (err);
      return;
    }

  tp_cli_dbus_properties_call_get_all (self, -1,
      TPY_IFACE_CALL_STREAM,
      on_call_stream_get_all_properties_cb, NULL, NULL, G_OBJECT (self));
}

static void
tpy_call_stream_init (TpyCallStream *self)
{
  TpyCallStreamPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPY_TYPE_CALL_STREAM, TpyCallStreamPrivate);

  self->priv = priv;
  priv->remote_members = g_hash_table_new (g_direct_hash, g_direct_equal);
}

static void
tpy_call_stream_dispose (GObject *object)
{
  TpyCallStream *self = TPY_CALL_STREAM (object);

  tp_clear_object (&self->priv->result);
  tp_clear_pointer (&self->priv->remote_members, g_hash_table_unref);

  G_OBJECT_CLASS (tpy_call_stream_parent_class)->dispose (object);
}

static void
tpy_call_stream_get_property (
    GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  TpyCallStream *self = TPY_CALL_STREAM (object);
  TpyCallStreamPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_REMOTE_MEMBERS:
        g_value_set_boxed (value, priv->remote_members);
        break;
      case PROP_LOCAL_SENDING_STATE:
        g_value_set_uint (value, priv->local_sending_state);
        break;
      case PROP_CAN_REQUEST_RECEIVING:
        g_value_set_boolean (value, priv->can_request_receiving);
        break;
      case PROP_READY:
        g_value_set_boolean (value, priv->ready);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
tpy_call_stream_set_property (
    GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TpyCallStream *self = TPY_CALL_STREAM (object);

  switch (property_id)
    {
      case PROP_LOCAL_SENDING_STATE:
        self->priv->local_sending_state = g_value_get_uint (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
tpy_call_stream_class_init (TpyCallStreamClass *bsc_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (bsc_class);
  TpProxyClass *proxy_class = (TpProxyClass *) bsc_class;
  GParamSpec *param_spec;

  g_type_class_add_private (bsc_class, sizeof (TpyCallStreamPrivate));

  object_class->dispose = tpy_call_stream_dispose;
  object_class->constructed = tpy_call_stream_constructed;
  object_class->set_property = tpy_call_stream_set_property;
  object_class->get_property = tpy_call_stream_get_property;

  proxy_class->interface = TPY_IFACE_QUARK_CALL_STREAM;

  param_spec = g_param_spec_boxed ("remote-members", "Remote members",
      "Remote member map",
      TPY_HASH_TYPE_CONTACT_SENDING_STATE_MAP,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_REMOTE_MEMBERS,
      param_spec);

  param_spec = g_param_spec_uint ("local-sending-state", "LocalSendingState",
      "Local sending state",
      0, NUM_TPY_SENDING_STATES, 0,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_LOCAL_SENDING_STATE,
      param_spec);

  param_spec = g_param_spec_boolean ("can-request-receiving",
      "CanRequestReceiving",
      "If true, the user can request that a remote contact starts sending on"
      "this stream.",
      FALSE,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CAN_REQUEST_RECEIVING,
      param_spec);

  param_spec = g_param_spec_boolean ("ready",
      "Ready",
      "If true the stream has retrieved all async information from the CM",
      FALSE,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_READY,
      param_spec);
}

TpySendingState
tpy_call_stream_get_local_sending_state (
  TpyCallStream *self)
{
  return self->priv->local_sending_state;
}

static void
on_set_sending_cb (TpProxy *proxy,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallStream *self = TPY_CALL_STREAM (proxy);

  if (error != NULL)
    {
      DEBUG ("Failed to set sending: %s", error->message);

      g_simple_async_result_set_from_error (self->priv->result, error);
    }

  g_simple_async_result_set_op_res_gboolean (self->priv->result, TRUE);
  g_simple_async_result_complete (self->priv->result);
  tp_clear_object (&self->priv->result);
}

void
tpy_call_stream_set_sending_async (TpyCallStream *self,
    gboolean send,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  g_return_if_fail (TPY_IS_CALL_STREAM (self));
  g_return_if_fail (self->priv->result == NULL);

  self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
      user_data, tpy_call_stream_set_sending_async);

  tpy_cli_call_stream_call_set_sending (TP_PROXY (self), -1,
      send,
      on_set_sending_cb, NULL, NULL, G_OBJECT (self));
}

gboolean
tpy_call_stream_set_sending_finish (TpyCallStream *self,
    GAsyncResult *result,
    GError **error)
{
  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
      error))
    return FALSE;

  g_return_val_if_fail (g_simple_async_result_is_valid (result,
    G_OBJECT (self), tpy_call_stream_set_sending_async),
    FALSE);

  return g_simple_async_result_get_op_res_gboolean (
    G_SIMPLE_ASYNC_RESULT (result));
}

static void
on_request_receiving_cb (TpProxy *proxy,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallStream *self = TPY_CALL_STREAM (proxy);

  if (error != NULL)
    {
      DEBUG ("Failed to request receiving: %s", error->message);

      g_simple_async_result_set_from_error (self->priv->result, error);
    }

  g_simple_async_result_set_op_res_gboolean (self->priv->result, TRUE);
  g_simple_async_result_complete (self->priv->result);
  tp_clear_object (&self->priv->result);
}

void
tpy_call_stream_request_receiving_async (TpyCallStream *self,
    TpHandle handle,
    gboolean receiving,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  g_return_if_fail (TPY_IS_CALL_STREAM (self));
  g_return_if_fail (self->priv->result == NULL);

  self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
      user_data, tpy_call_stream_request_receiving_async);

  tpy_cli_call_stream_call_request_receiving (TP_PROXY (self), -1,
      handle, receiving,
      on_request_receiving_cb, NULL, NULL, G_OBJECT (self));
}

gboolean
tpy_call_stream_request_receiving_finish (TpyCallStream *self,
    GAsyncResult *result,
    GError **error)
{
  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
      error))
    return FALSE;

  g_return_val_if_fail (g_simple_async_result_is_valid (result,
    G_OBJECT (self), tpy_call_stream_request_receiving_async),
    FALSE);

  return g_simple_async_result_get_op_res_gboolean (
    G_SIMPLE_ASYNC_RESULT (result));
}