summaryrefslogtreecommitdiff
path: root/telepathy-glib/stream-tube-connection.c
blob: 03ea60dc1780ba143aebfbf928157fd279b78bca (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
/*
 * Object representing a connection on a Stream Tube
 *
 * Copyright © 2010 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
 */

/**
 * SECTION:stream-tube-connection
 * @title: TpStreamTubeConnection
 * @short_description: a connection on a Stream Tube
 *
 * Object used to represent a connection on a #TpStreamTubeChannel.
 *
 * Since: 0.13.2
 */

/**
 * TpStreamTubeConnection:
 *
 * Data structure representing a connection on a #TpStreamTubeChannel.
 *
 * Since: 0.13.2
 */

/**
 * TpStreamTubeConnectionClass:
 *
 * The class of a #TpStreamTubeConnection.
 *
 * Since: 0.13.2
 */

#include "config.h"

#include "telepathy-glib/stream-tube-connection-internal.h"
#include "telepathy-glib/stream-tube-connection.h"

#include <telepathy-glib/util.h>

#define DEBUG_FLAG TP_DEBUG_CHANNEL
#include "telepathy-glib/debug-internal.h"

struct _TpStreamTubeConnectionClass {
    /*<private>*/
    GObjectClass parent_class;
};

G_DEFINE_TYPE(TpStreamTubeConnection, tp_stream_tube_connection,
    G_TYPE_OBJECT)

enum {
    PROP_SOCKET_CONNECTION = 1,
    PROP_CHANNEL,
    PROP_CONTACT,
    N_PROPS
};

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

static guint _signals[LAST_SIGNAL] = { 0, };

struct _TpStreamTubeConnectionPrivate
{
  GSocketConnection *socket_connection;
  /* We don't introduce a circular reference as the channel keeps a weak ref
   * on us */
  TpStreamTubeChannel *channel;
  TpContact *contact;
};

static void
tp_stream_tube_connection_init (TpStreamTubeConnection *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TP_TYPE_STREAM_TUBE_CONNECTION, TpStreamTubeConnectionPrivate);
}

static void
tp_stream_tube_connection_dispose (GObject *object)
{
  TpStreamTubeConnection *self = TP_STREAM_TUBE_CONNECTION (object);
  void (*dispose) (GObject *) =
    G_OBJECT_CLASS (tp_stream_tube_connection_parent_class)->dispose;

  tp_clear_object (&self->priv->socket_connection);
  tp_clear_object (&self->priv->channel);
  tp_clear_object (&self->priv->contact);

  if (dispose != NULL)
    dispose (object);
}

static void
tp_stream_tube_connection_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  TpStreamTubeConnection *self = TP_STREAM_TUBE_CONNECTION (object);

  switch (property_id)
    {
      case PROP_SOCKET_CONNECTION:
        g_value_set_object (value, self->priv->socket_connection);
        break;
      case PROP_CHANNEL:
        g_value_set_object (value, self->priv->channel);
        break;
      case PROP_CONTACT:
        g_value_set_object (value, self->priv->contact);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
  }
}

static void
tp_stream_tube_connection_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TpStreamTubeConnection *self = TP_STREAM_TUBE_CONNECTION (object);

  switch (property_id)
    {
      case PROP_SOCKET_CONNECTION:
        g_assert (self->priv->socket_connection == NULL); /* construct only */
        self->priv->socket_connection = g_value_dup_object (value);
        break;
      case PROP_CHANNEL:
        g_assert (self->priv->channel == NULL); /* construct only */
        self->priv->channel = g_value_dup_object (value);
        break;
      case PROP_CONTACT:
        g_assert (self->priv->contact == NULL); /* construct only */
        self->priv->contact = g_value_dup_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
  }
}

static void
tp_stream_tube_connection_constructed (GObject *object)
{
  TpStreamTubeConnection *self = TP_STREAM_TUBE_CONNECTION (object);
  void (*chain_up) (GObject *) =
    ((GObjectClass *) tp_stream_tube_connection_parent_class)->constructed;

  if (chain_up != NULL)
    chain_up (object);

  g_assert (G_IS_SOCKET_CONNECTION (self->priv->socket_connection));
}

static void
tp_stream_tube_connection_class_init (TpStreamTubeConnectionClass *cls)
{
  GObjectClass *object_class = G_OBJECT_CLASS (cls);
  GParamSpec *param_spec;

  g_type_class_add_private (cls, sizeof (TpStreamTubeConnectionPrivate));

  object_class->get_property = tp_stream_tube_connection_get_property;
  object_class->set_property = tp_stream_tube_connection_set_property;
  object_class->constructed = tp_stream_tube_connection_constructed;
  object_class->dispose = tp_stream_tube_connection_dispose;

  /**
   * TpStreamTubeConnection:socket-connection:
   *
   * The #GSocketConnection used to transfer data through this connection.
   * Read-only except during construction.
   *
   * This property can't be %NULL.
   *
   * Since: 0.13.2
   */
  param_spec = g_param_spec_object ("socket-connection", "GSocketConnection",
      "GSocketConnection used to transfer data",
      G_TYPE_SOCKET_CONNECTION,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_SOCKET_CONNECTION,
      param_spec);

  /**
   * TpStreamTubeConnection:channel:
   *
   * The #TpStreamTubeChannel channel associated with this connection
   *
   * This property can't be %NULL.
   *
   * Since: 0.13.2
   */
  param_spec = g_param_spec_object ("channel", "TpStreamTubeChannel",
      "The channel associated with this connection",
      TP_TYPE_STREAM_TUBE_CHANNEL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CHANNEL,
      param_spec);

  /**
   * TpStreamTubeConnection:contact:
   *
   * The #TpContact with who we are exchanging data through this tube, or
   * %NULL if we can't safely identify the contact.
   *
   * If not %NULL, the #TpContact objects is guaranteed to have all of the
   * features previously passed to
   * tp_simple_client_factory_add_contact_features() prepared.
   *
   * Since: 0.13.2
   */
  param_spec = g_param_spec_object ("contact", "TpContact",
      "The TpContact of the connection",
      TP_TYPE_CONTACT,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CONTACT, param_spec);

  /**
   * TpStreamTubeConnection::closed:
   * @self: the #TpStreamTubeConnection
   * @error: (transfer none): a #GError representing the error reported by the
   * connection manager
   *
   * The ::closed signal is emitted when the connection manager reports that
   * a tube connection has been closed.
   *
   * Since: 0.13.2
   */
  _signals[CLOSED] = g_signal_new ("closed",
      G_OBJECT_CLASS_TYPE (cls),
      G_SIGNAL_RUN_LAST,
      0, NULL, NULL, NULL,
      G_TYPE_NONE,
      1, G_TYPE_ERROR);
}

TpStreamTubeConnection *
_tp_stream_tube_connection_new (
    GSocketConnection *socket_connection,
    TpStreamTubeChannel *channel)
{
  return g_object_new (TP_TYPE_STREAM_TUBE_CONNECTION,
      "socket-connection", socket_connection,
      "channel", channel,
      NULL);
}

/**
 * tp_stream_tube_connection_get_socket_connection:
 * @self: a #TpStreamTubeConnection
 *
 * Return the #TpStreamTubeConnection:socket-connection property
 *
 * Returns: (transfer none): the value of #TpStreamTubeConnection:socket-connection
 *
 * Since: 0.13.2
 */
GSocketConnection *
tp_stream_tube_connection_get_socket_connection (TpStreamTubeConnection *self)
{
  return self->priv->socket_connection;
}

/**
 * tp_stream_tube_connection_get_channel:
 * @self: a #TpStreamTubeConnection
 *
 * Return the #TpStreamTubeConnection:channel property
 *
 * Returns: (transfer none): the value of #TpStreamTubeConnection:channel
 *
 * Since: 0.13.2
 */
TpStreamTubeChannel *
tp_stream_tube_connection_get_channel (
    TpStreamTubeConnection *self)
{
  return self->priv->channel;
}

/**
 * tp_stream_tube_connection_get_contact:
 * @self: a #TpStreamTubeConnection
 *
 * Return the #TpStreamTubeConnection:contact property
 *
 * Returns: (transfer none): the value of #TpStreamTubeConnection:contact
 *
 * Since: 0.13.2
 */
TpContact *
tp_stream_tube_connection_get_contact (TpStreamTubeConnection *self)
{
  return self->priv->contact;
}

void
_tp_stream_tube_connection_set_contact (TpStreamTubeConnection *self,
    TpContact *contact)
{
  g_assert (self->priv->contact == NULL);

  self->priv->contact = g_object_ref (contact);
  g_object_notify (G_OBJECT (self), "contact");
}

void
_tp_stream_tube_connection_fire_closed (TpStreamTubeConnection *self,
    GError *error)
{
  g_signal_emit (self, _signals[CLOSED], 0, error);
}